How to tell which shell is running a script

I've been searching for a while and haven't found this answer anywhere.

How can I tell which shell is running my script from within the script? For example, I have lots of older scripts that we are porting to a new Linux system. Many of the scripts start with ":" alone on a line, which I think long ago forced /bin/sh.

Is there a way to tell within the script what it's being interpreted by? I tried "ps", but that only shows the name of the script.

Thanks,

Sean.

Modern shells support the $SHELL variable.

echo $SHELL

$SHELL only shows the shell set at login. So if I login, $SHELL = /bin/ksh. If I start a new /bin/sh from my /bin/ksh, $SHELL still = /bin/ksh.

Sean.

That doesn't tell you what shell is currently running; it tells you your default shell, which may or may not be your login shell.

I always thought that the : in the beginning of a script meant that whatever shell you are in is what the script would use.

I've always found it safer to always specify #!/bin/?shell? at the beginning to make sure.

I usually use a shebang only when on a system that doesn't have a POSIX shell in /bin/sh or when I need some feature not supported by the standard shell.

ps -p$$ | xargs -n1 | tail -1

linux:

ps p$$ ocomm h

Both of these return the name of the script. If I have a script named test.sh:

#!/bin/sh
ps -p$$ | xargs -n1 | tail -1

or

#!/bin/sh
ps p$$ ocomm h

both return "test.sh". I'm on RHEL 5.3.

I'm hoping to find somethat that will tell me "sh" or "ksh" or whatever shell is actually interpreting the script.

It's better to test whether the current shell has the features your script needs.

a variation on a theme (tested on Solaris):

#!/bin/ksh

echo "myShell->[$(ps -o 'pid,comm' -p "${$}" | xargs -n1 | tail -1)]"
# OR
# echo "myShell->[$(ps -o comm -p "$$" | { read; read comm; echo "$comm"; })]"

This works for me on AIX, but on Linux it always returns:

myShell->[test.sh]

I don't have linux in front of me, but most likely it should be a combination of what you do on above with what 'Scrutinizer' suggested previously.
Maybe others could 'port' this to linux for you.

This should work in RHEL 5.3:

#!/bin/sh
comm=$(ps -p $$ -o command h|cut -d' ' -f1)
shell=${comm##*/}
echo $shell