Although most of you may know how to do this, it’s just a personal note for myself and for others who might get something out of it.
There are a few ways to do this, we will not be depending on environment variables to tell us what shell we are on, since it’s a variable that echoes anything that it’s set to and hence is not always right.
1. $ echo $0 # prints the name of the current program running. So when entered on shell prompt, returns the name of the shell.
nits@excalibur:~$ echo $0
bash
nits@excalibur:~$ ksh
$ echo $0
ksh
2. $ ps -p $$ # uses the PID of the currently running program to get information for the ps tool for it. The last column, CMD, can be used to identify which shell you’re on.
nits@excalibur:~$ ps -p $$
PID TTY TIME CMD
6426 pts/7 00:00:00 bash
nits@excalibur:~$ ksh
$ ps -p $$
PID TTY TIME CMD
6609 pts/7 00:00:00 ksh
If you already haven’t figured out what $0 and $$ do,
- $0 : returns the name of the currently running program
- $$ : returns the PID (Process ID) of the currently running program
nits@excalibur:~$ echo $0;echo $$
bash
6426
nits@excalibur:~$ ksh
$ echo $0; echo $$
ksh
6609
Source : thevoidghost[dot]wordpress[dot]com
0 comments:
Post a Comment