Skip to content
Menu
myCloud myCloud

Personal short notes about Cloud

  • XMPie
  • AWS
    • AWS Topics
    • AWS Architecture
    • AWS CLI
    • AWS Health
    • AWS Policies
    • AWS Cost
  • CHEAT SHEETS
myCloud myCloud

Personal short notes about Cloud

bash or shell

By mikado on October 22, 2022October 28, 2022

BASHSH
Bourne Again SHellSHell
Developed by Brain FoxDeveloped by Bourne
Successor of shPredecessor of bash
bash is the default SHELLsh is the NOT default SHELL
#!/bin/bash#!/bin/sh
It has more Functionality with up-gradationIt has less functionality
bash is not a valid POSIX shellsh is a valid POSIX shell
Easy to usenot as easy as bash
less portable than sh.more portable than bash
Extended version of languageOriginal language
Bash scripting is scripting specifically for BashShell scripting is scripting in any shell
supports command historydoes not supports command history
supports job controlsdoes 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

Category: Linux, SCRIPTS

Categories

  • AWS (4)
  • AWS Architecture (8)
  • AWS CLI (5)
  • AWS Cost (3)
  • AWS Health (4)
  • AWS Policies (2)
  • AWS Topics (24)
  • CHEAT SHEETS (16)
  • Container (21)
  • Datadog (4)
  • Jenkins (2)
  • Linux (9)
  • Microsoft (7)
  • Python (1)
  • SCRIPTS (9)
  • Terraform (5)
  • XMPie (6)
©2025 myCloud
Click to Copy