Click to See Complete Forum and Search --> : Determining the current shell
Architect
06-06-2007, 03:24 AM
How do I determine the current shell I am in?
I tried echo $SHELL but that only showed me the correct shell when I was in the bash shell.
So what is the right way to determine this? Any help with this would be great.
TIA
deathadder
06-06-2007, 05:24 AM
Have a look in /etc/passwd
adam:x:1002:100:adam jefferiss:/home/adam:/bin/bash
Shows that my login shell is bash, the output from pstree would show you too...
Architect
06-06-2007, 06:11 AM
That only gives me the default shell.
If my default shell is csh and after logging in I then change to the ksh shell I want to be able to determine I am in ksh.
deathadder
06-06-2007, 06:28 AM
Using pstree / ps?
user@host:~> echo $SHELL
/bin/bash
user@host:~> csh
/home/user> pstree
init--
|--gnome-terminal---bash---csh---pstree
/home/user> exit
user@host:~>
C bashed shells have $shell, but apart from that I'm not too sure...I'll have a google
hotcold
06-06-2007, 10:09 AM
Hi.
The ps command usually works, as in:
#!/bin/sh
# @(#) s1 Demonstrate display of current shell.
## command "ps $$" produces something like:
# 19746 pts/1 S+ 0:00 bash
# 1 2 3 4 5
set -o nounset
echo " sh version: $BASH_VERSION"
echo
bash <<'EOF'
ps $$
EOF
echo
echo " Extraction of command:"
bash <<'EOF'
ps $$ |
tr -s " " |
tail -1 |
cut -d" " -f5
EOF
echo
echo " Various shells, note rc difference:"
bash3 <<'EOF'
ps $$
EOF
tcsh <<'EOF'
ps $$
EOF
ksh <<'EOF'
ps $$
EOF
zsh <<'EOF'
ps $$
EOF
dash <<'EOF'
ps $$
EOF
rc <<'EOF'
ps $pid
EOF
exit 0
Producing:
% ./s1
sh version: 2.05b.0(1)-release
PID TTY STAT TIME COMMAND
21492 pts/1 S+ 0:00 bash
Extraction of command:
bash
Various shells, note rc difference:
PID TTY STAT TIME COMMAND
21499 pts/1 R+ 0:00 bash3
PID TTY STAT TIME COMMAND
21501 pts/1 R+ 0:00 tcsh
PID TTY STAT TIME COMMAND
21537 pts/1 R+ 0:00 ksh
PID TTY STAT TIME COMMAND
21539 pts/1 R+ 0:00 zsh
PID TTY STAT TIME COMMAND
21541 pts/1 R+ 0:00 dash
PID TTY STAT TIME COMMAND
21543 pts/1 R+ 0:00 rc
cheers, hotcold
bwkaz
06-06-2007, 07:54 PM
ls -l /proc/$$/exe
would also tell you which shell is running, assuming your shell supports "$$". Bourne shells do; I'm not sure about C shells.
happybunny
06-06-2007, 09:41 PM
env | grep SHELL
hotcold
06-07-2007, 09:29 AM
Hi.
I added the suggestions of others to the script I posted earlier. Briefly, here is my take on the question.
1) not all shells set and / or export variables SHELL or shell, most of the runs below show tcsh even while in other shells.
2) the /proc/$$ idea seems to work on Linux, and also on FreeBSD. You may need to poke around for the best ASCII file to use.
3) I didn't have a Solaris handy, but at least on the older Solaris, /proc was not available.
Conclusion: if you are always using recent Linux distributions, /proc seems easy and reliable. If you are using other systems or are generally interested in portability, $$ seems safest, but is slightly more cumbersome. There are differences in utilities, so cut and ps may need to be looked at to extract the correct field.
I displayed one sequence to extract the shell name from ps output, and then just showed the output for the other shells I had handy, Here is the longish script (slightly patched on the fly):
#!/bin/sh
# @(#) s2 Demonstrate display of current shell.
## command "ps $$" produces something like:
# 19746 pts/1 S+ 0:00 bash
# 1 2 3 4 5
set -o nounset
echo " sh version: $BASH_VERSION"
echo
bash <<'EOF'
ps $$
EOF
echo
echo " Extraction of command:"
bash <<'EOF'
ps $$ |
tr -s " " |
tail -1 |
cut -d" " -f6
EOF
echo
echo " Various shells, note rc difference:"
echo " using
1) ps
2) proc/\$$/cmdline (needs newline)
3) \$SHELL
4) env | grep -i shell
"
echo
echo " Expecting bash3"
bash3 <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
echo
echo " Expecting tcsh"
tcsh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
echo
echo " Expecting ksh"
ksh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
echo
echo " Expecting zsh"
zsh <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
echo
echo " Expecting dash"
dash <<'EOF'
ps $$
cat /proc/$$/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
echo
echo " Expecting rc"
rc <<'EOF'
ps $pid
# cat /proc/$$/cmdline
cat /proc/$pid/cmdline
echo
echo $SHELL
env | grep -i shell
EOF
exit 0
And results on Debian-derived Xandros:
% ./s2
sh version: 2.05b.0(1)-release
PID TTY STAT TIME COMMAND
3729 pts/1 R+ 0:00 bash
Extraction of command:
bash
Various shells, note rc difference:
using
1) ps
2) proc/$$/cmdline (needs newline)
3) $SHELL
4) env | grep -i shell
Expecting bash3
PID TTY STAT TIME COMMAND
3736 pts/1 R+ 0:00 bash3
bash3
/bin/tcsh
SHELL=/bin/tcsh
Expecting tcsh
(source file is :/home/dennisl/.tcshrc:, Rev 2006.12.17)
PID TTY STAT TIME COMMAND
3741 pts/1 S+ 0:00 tcsh
tcsh
/bin/tcsh
SHELL=/bin/tcsh
Expecting ksh
PID TTY STAT TIME COMMAND
3780 pts/1 S+ 0:00 ksh
ksh
/bin/tcsh
SHELL=/bin/tcsh
Expecting zsh
PID TTY STAT TIME COMMAND
3785 pts/1 S+ 0:00 zsh
zsh
/bin/tcsh
SHELL=/bin/tcsh
Expecting dash
PID TTY STAT TIME COMMAND
3790 pts/1 S+ 0:00 dash
dash
/bin/tcsh
SHELL=/bin/tcsh
Expecting rc
PID TTY STAT TIME COMMAND
3795 pts/1 R+ 0:00 rc
rc
/bin/tcsh
SHELL=/bin/tcsh
cheers, hotcold
blackbelt_jones
06-07-2007, 09:37 AM
you can use the chsh (change shell) command it'll tell you what shell you're running before it asks you what shell you want to change to.
[oscar @ debian>chsh [9:36:28 on 07-06-07]
Password:
Changing the login shell for oscar
Enter the new value, or press ENTER for the default
Login Shell [/usr/bin/zsh]:
actually, I guess that only works if you're running the login shell, but I have to assume that if you weren't, you'd know what shell you're running.
blackbelt_jones
06-07-2007, 09:55 AM
Oh wait! This just in
echo $SHELL
will do the trick.
E1PHOTON
06-07-2007, 01:32 PM
I tried echo $SHELL but that only showed me the correct shell when I was in the bash shell.
Looks like he tried that, BlackBelt.