Why is echo $SHELL always showing /bin/bash?

$SHELL is the environment variable that holds your preferred shell, not the currently running shell. It's initialised by login or any other application that logs you in based on the shell field in your user entry in the passwd database (your login shell).

That variable is used to tell applications like xterm, vim... what shell they should start for you when they start a shell. You typically change it when you want to use another shell than the one set for you in the passwd database.

To get a path of the current shell interpreter, on Linux, and with Bourne or csh like shells, you can do:

readlink "/proc/$$/exe"

The rc equivalent:

readlink /proc/$pid/exe

The fish equivalent:

set pid %self
readlink /proc/$pid/exe

csh/tcsh set the $shell variable to the path of the shell.

In Bourne-like shells, $0 will contain the first argument that the shell received (argv[0]) which by convention is the name of the command being invoked (though login applications, again by convention make the first character a - to tell the shell it's a login shell and should for instance source the .profile or .login file containing your login session customisations) when not called to interpret a script or when called with shell -c '...' without extra arguments.

In:

$ bash -c 'echo "$0"'
bash
$ /bin/bash -c 'echo "$0"'
/bin/bash

It's my shell that calls /bin/bash in both cases, but in the first case with bash as its first argument, and in the second case with /bin/bash. Several shells allow passing arbitrary strings instead like:

$ (exec -a whatever bash -c 'echo "$0"')
whatever

In ksh/bash/zsh/yash.


If the output of echo $0 command is -bash it means that bash was invoked as a login shell. If the output is only bash, then you are in a non-login shell.

As the ENV is triggered to show the desired output it is giving you /bin/bash

[root@dcconnect ~]# env | grep -i SHELL
SHELL=/bin/bash

Out put of echo #SHELL is pulled from the ENV variable