BASH | SH |
---|---|
Bourne Again SHell | SHell |
Developed by Brain Fox | Developed by Bourne |
Successor of sh | Predecessor of bash |
bash is the default SHELL | sh is the NOT default SHELL |
#!/bin/bash | #!/bin/sh |
It has more Functionality with up-gradation | It has less functionality |
bash is not a valid POSIX shell | sh is a valid POSIX shell |
Easy to use | not as easy as bash |
less portable than sh. | more portable than bash |
Extended version of language | Original language |
Bash scripting is scripting specifically for Bash | Shell scripting is scripting in any shell |
supports command history | does not supports command history |
supports job controls | does not support job control |
you may have the following:
echo $SHELL
/bin/bash
but:
echo $0
sh
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
$ echo $$
6197
$ 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/systems that don’t support /proc
filesystem.
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
$ 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