Bash – Difference between “echo $SHELL” and “which bash”

bashshellsolaris

I am trying to determine the location of bash interpreter on my Solaris system and I am a bit confused. When I type:

echo $SHELL

The resulting path is:

/bin/bash

And when I type:

which bash

I get:

/usr/bin/bash

Can anyone please explain this discrepancy?

Best Answer

Your system probably has bash installed in multiple locations, whether as different versions of bash or just symbolic links.

which is not really a useful command for most purposes - it's not really portable or very usable in scripts. In general, type is better. The idea behind which is to do a PATH search for the command you give it as an argument.

$SHELL does not necessarily reflect the currently running shell. Instead, $SHELL is the user's preferred shell, which is typically the one set in /etc/passwd. If you start a different shell after logging in, you can not necessarily expect $SHELL to match the current shell anymore.

As you can see, $SHELL and which are completely unrelated. Neither of these will tell you what shell you are actually running.

A side note: Unfortunately, matching the shell you are currently running to a location in the filesystem is harder than one might think. This is because binaries are loaded into memory in order to run, and in most systems the copy in memory will continue to run fine even after you delete the original from disk (the kernel may keep the disk copy around in "limbo" until it is really no longer needed). I don't think there is any portable way to go about this - you'd have to resort to platform specific methods. For example, on Linux, examining the link /proc/$$/exe should give you a decent idea of what file is running (where $$ is the process ID of your running shell). Unfortunately I am not familiar with Solaris, so I can't help you there.

Related Question