thinker3
thinker3

Reputation: 13301

The value of $0 in .bashrc

Why the value of $0 in ~/.bashrc is bash?

echo "`pwd`/$0 loaded"

I expected /home/thinker3/.bashrc loaded but the result:

thinker3@ubuntu:~$ source .bashrc
/home/thinker3/bash loaded

My problem was solved, thanks to Michael Hoffman, I added

echo $BASH_SOURCE loaded

to ~/.bashrc and /etc/profile, then:

/home/thinker3/.bashrc loaded
thinker3@ubuntu:~/addons$ su - root
Password: 
/etc/profile loaded
root@ubuntu:~# 

Upvotes: 2

Views: 449

Answers (2)

bitmask
bitmask

Reputation: 34626

It's bash. This can be easily tested by saying

echo "$0"

in your .bashrc and starting a new bash shell.

The reason for this, is that $0 is the name of the binary that is being run, which is not .bashrc but bash (usually resolving to /bin/bash due to the value of $PATH).

Upvotes: 0

Michael Hoffman
Michael Hoffman

Reputation: 34334

As Uroc327 points out, .bashrc is sourced so $0 is just the name of the called process (probably bash, just as if you echoed $0 from a command-line). While .bashrc is loading, the value of $BASH_SOURCE will contain the file's location.

Upvotes: 3

Related Questions