Am I using bash or sh? [duplicate]

I'm connected to a remote shell and many keys aren't working properly such as arrows and the escape key. This answer Arrow keys, tab-complete not working suggested I might be in the wrong shell, so I referenced How to determine the current shell I'm working on?. Using the methods described, I get strange results.

echo $SHELL 
/bin/bash

but

echo $0
sh

Which shell am I using?


Use proc filesystem to know shell's process name

When in doubt, use /proc file system. There's folders per each process, with comm file, where that process's command is described. So, knowing shell's PID, we can know what shell it is.

$ echo $$
4824
$ cat /proc/4824/comm                                                          
mksh
$ bash
xieerqi@eagle:~$ echo $$
6197
xieerqi@eagle:~$ cat /proc/6197/comm
bash

There's also other files that you can reference to extract that same info:

  • /proc/$$/stat
  • /proc/$$/status
  • /proc/$$/cmdline
  • /proc/$$/exe (symlink to the executable itself)

This may not work on older kernels or systems that don't support /proc filesystem ( Mac OS X, for instance ).

Variation on the same approach with ps command

ps command has -p flag which allows you to specify pid. We're still using the same idea of referencing the $$ variable for that.

$ ps -p $$                                                                     
  PID TTY          TIME CMD
 7728 pts/5    00:00:00 mksh
$ bash
xieerqi@eagle:~$ ps -p $$
  PID TTY          TIME CMD
 7776 pts/5    00:00:00 bash

$0 vs $SHELL.

According to Arch Wiki ,

SHELL contains the path to the user's preferred shell. Note that this is not necessarily the shell that is currently running, although Bash sets this variable on startup.

In other words, this is the users' default interactive shell, the same one that is set in /etc/passwd. This also is the reason why $SHELL variable doesn't change for subshells . For instance, no matter how many shells i spawn, $SHELL variable is unchanged:

$ echo $SHELL                                                                  
/bin/mksh
$ bash --posix
bash-4.3$ echo $SHELL
/bin/mksh
bash-4.3$ dash 
$ echo $SHELL
/bin/mksh

$0 argument will display the "self" - the command with which a program or name of the file. So a simple script like this:

#!/bin/bash
echo $0

Will give output like this:

$ ./test_script.sh                                                             
./test_script.sh

This is also apparent when you do something like this:

$ echo 'one two three' | xargs bash -c 'echo $0' 
one

For all shells , -c option places first command line argument into $0 variable.

As far as interactive shell goes, $0 usually would be sufficient, but as you can see, it is not reliable way to know what shell you're using. Knowing the process is far more reliable


You're using sh. Because $SHELL refers to the login shell, not the currently one being used.

Your provided StackOverflow question link has the correct method to correctly determine the currently running shell.