Difference between "echo $SHELL" and "which bash"
Solution 1
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.
Solution 2
The other answers are good, but I like to give a demonstration.
% echo $SHELL
/bin/zsh
% which bash
/bin/bash
- The first command tells me which shell will be executed by
login
when you log in. In my case,/bin/zsh
. - The second command tells me the first occurrence in my
$PATH
thebash
command can be found.
One does not imply the second, nor vice versa.
I'm not going to restate other people's answers but in addition to that I want to point out some things.
- The default shell on Solaris is
/bin/bash
- On Solaris
/bin
is a symlink to./usr/bin
- The default
$PATH
on Solaris is/usr/bin:/bin
That's why you're seeing this behavior.
Solution 3
echo $SHELL
shows the value of the SHELL
environment variable. This is a user configuration, which you can set to the path to your favorite interactive shell. Many programs invoke $SHELL
when asked to invoke a shell.
I think all shells leave this variable alone unless it is unset when they start. Bash sets SHELL
to its own path if the variable is unset when it starts. ATT ksh93 sets SHELL
to /bin/sh
if unset (even if /bin/sh
is some unrelated shell). Ksh (all versions) checks whether SHELL
is rsh
when it starts; if it is, it starts as a restricted shell.
which bash
shows the path to the bash executable (except when it doesn't — you should use type bash
instead). More precisely, it searches the directories in $PATH
for an executable called bash
.
echo $0
, in an interactive shell, shows the command name that was used to invoke the shell.
ps $$
(typed from a shell) displays information about the shell process ($$
is expanded to the process ID of the shell).
ls -l /proc/$$/exe
shows the full path to the executable for the shell
For example, my favorite shell is zsh, but here I've just started a home-compiled version of bash that isn't in the $PATH
.
% ./bash
$ echo $SHELL
/bin/zsh4
$ type bash
bash is /usr/bin/bash
$ echo $0
./bash
$ readlink /proc/$$/exe
/home/gilles/src/bash-git/bash
$ pwd
/home/gilles/src/bash-git
$ rm bash
$ echo $0
./bash
$ readlink /proc/$$/exe
/home/gilles/src/bash-git/bash (deleted)
Related videos on Youtube
Preeyah
Professional Software Testing Engineer specializing in automation with Open Source tools. Interested in: Test automation: Selenium, BDD, Cucumber Programming: Java, Python, Spring Data scraping Audio: recording/mixing
Updated on September 18, 2022Comments
-
Preeyah over 1 year
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?
-
jw013 almost 12 yearsA good thing to note is that
which
is not really a useful command. -
jw013 almost 12 yearsAlso, neither of the two answers are correct about
$SHELL
. It 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. -
Preeyah almost 12 years@jw013 Thank you for your comments and explanation, please post it as an answer so I can accept it.
-
Mikel almost 12 yearsSee also How to test what shell I am using
-
janmoesen almost 12 yearsAllow me to spamvertise the
wtfis
function from my dotfiles: github.com/janmoesen/tilde/blob/01a9f86/.bash/commands#L719 — it combinestype
,file
andls
to show as much information as possible.
-
-
jordanm almost 12 yearsThe first command shows your default shell, which may be the shell you are currently running, but not always.
-
bahamat almost 12 years@jordanm: ah, yes you're right. Fixing.
-
Mikel almost 12 yearsDepends on your definition of "currently active". It's your login shell, not the shell you're currently typing into.
-
Mikel almost 12 yearsIt's not the file currently running, it's the default shell. See other answers. Also, for what it's worth, I think the symlink is the other way around on Solaris.
-
kenny almost 12 yearsYes, that's what I meant; edited. Thanks!
-
jlliagre almost 12 yearsThe default shell is /usr/bin/bash only on Solaris 11. Under Solaris 10 and older it is /bin/sh which is the legacy Bourne shell implementation. Since Solaris 11, /bin/sh is ksh93. Solaris default PATH for non root users is just /usr/bin, having both /usr/bin and /bin would make no sense.
-
bahamat almost 12 years@jlliagre: You're right regarding S10/S11. About
/bin/
and/usr/bin
I agree it doesn't make sense, but if you don't have aPATH
declared that's what the system gives you. -
jlliagre almost 12 yearsThe PATH is always defined by default on Solaris unless you mess with configuration files. It is set on Solaris 11 to
/usr/bin
for regular users and/usr/bin:/usr/sbin
for root. The default (and redundant) PATH set by bash if unset at launchtime is/usr/gnu/bin:/bin:/usr/bin:/sbin:/usr/sbin:.
This is perhaps where you observe/bin:/usr/bin
. -
bahamat almost 12 yearsI just did it on Solaris 11 and reported exactly what I got.
-
jlliagre almost 12 yearsMore precisely, /bin/bash and /usr/bin/bash are the very same file, /bin being a symbolic link to /usr/bin.