So I was browsing the web today and came across a post where an individual was interested in finding out what shell they are using. There were several responses: echo $0, ps -p $$, echo $SHELL, however, none of them hit the mark (IMHO). If you were, for example, using Ubuntu 9.04 /bin/sh is a symlink to /bin/dash. If you ran echo $0 or ps -p $$ while using /bin/sh (/bin/dash) they would both report /bin/sh as the shell, not /bin/dash (the real interpretor). Furthermore if you ran echo $SHELL while using /bin/sh it would report /bin/bash (the login shell).
I wrote a little snippet of code to try to figure out what shell is actually being used, however, have hit a little bit of a thunker.
var=`ps -p $$ | sed 's/[0-9]*//g' | sed -re 's/^.+\///g' | sed -re 's/://g' | sed -re 's/[A-Z]*//g' | sed -re 's/ //g'`
echo $var | grep "/" > /dev/null
if ! [ $? -eq 0 ]; then fullvar=`echo "/bin/"$var | sed -re 's/ //g'`; fi
readlink $fullvar > /dev/null
if ! [ $? -eq 0 ]
then echo $fullvar" is not a link. It must be the REAL shell interpretor."
fullvar=`readlink -f $fullvar` > /dev/null
var=`echo $var | sed -re 's/ //g'`
echo $fullvar" is the REAL shell interpretor, not /bin/"$var
Obviously the first thing that stands out is how sloppy it is.
The second thing that stands out is that if the interpretor is outside of /bin the output would be wrong.
The third is using multiple symlinks to "fool" the script.
Any ideas on how to "fix" it?
Or better yet is there a one liner to figure out the current shell interpretor?