How do I tell what type my shell is How do I tell what type my shell is shell shell

How do I tell what type my shell is


This is what I use in my .profile:

# .profile is sourced at login by sh and ksh. The zsh sources .zshrc and# bash sources .bashrc. To get the same behaviour from zsh and bash as well# I suggest "cd; ln -s .profile .zshrc; ln -s .profile .bashrc".# Determine what (Bourne compatible) shell we are running under. Put the result# in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type.if test -n "$ZSH_VERSION"; then  PROFILE_SHELL=zshelif test -n "$BASH_VERSION"; then  PROFILE_SHELL=bashelif test -n "$KSH_VERSION"; then  PROFILE_SHELL=kshelif test -n "$FCEDIT"; then  PROFILE_SHELL=kshelif test -n "$PS3"; then  PROFILE_SHELL=unknownelse  PROFILE_SHELL=shfi

It does not make fine distinctions between ksh88, ksh95, pdksh or mksh etc., but in more than ten years it has proven to work for me as designed on all the systems I were at home on (BSD, SunOS, Solaris, Linux, Unicos, HP-UX, AIX, IRIX, MicroStation, Cygwin.)

I don't see the need to check for csh in .profile, as csh sources other files at startup.Any script you write does not need to check for csh vs Bourne-heritage because you explicitly name the interpreter in the shebang line.


Try to locate the shell path using the current shell PID:

ps -p $$

It should work at least with sh, bash and ksh.


If the reason you're asking is to try to write portable shell code, then spotting the shell type, and switching based on it, is an unreliable strategy. There's just too much variation possible.

Depending on what you're doing here, you might want to look at the relevant part of the autoconf documentation. That includes an interesting (and in some respects quite dismal) zoology of different shell aberrations.

For the goal of portable code, this section should be very helpful. If you do need to spot shell variants, then there might be some code buried in autoconf (or at least in one of the ./configure scripts it generates) which will help with the sniffing.