How can I determine the current shell I am working on?
Would the output of the ps command alone be sufficient?
How can this be done in different flavors of Unix?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are three approaches to finding the name of the current shell’s executable:
Please note that all three approaches can be fooled if the executable of the shell is
/bin/sh, but it’s really a renamedbash, for example (which frequently happens).Thus your second question of whether
psoutput will do is answered with "not always".echo $0– will print the program name… which in the case of the shell is the actual shell.ps -ef | grep $$ | grep -v grep– this will look for the current process ID in the list of running processes. Since the current process is the shell, it will be included.This is not 100% reliable, as you might have other processes whose
pslisting includes the same number as shell’s process ID, especially if that ID is a small number (for example, if the shell’s PID is "5", you may find processes called "java5" or "perl5" in the samegrepoutput!). This is the second problem with the "ps" approach, on top of not being able to rely on the shell name.echo $SHELL– The path to the current shell is stored as theSHELLvariable for any shell. The caveat for this one is that if you launch a shell explicitly as a subprocess (for example, it’s not your login shell), you will get your login shell’s value instead. If that’s a possibility, use thepsor$0approach./bin/shis actually bash or ksh), you need heuristics. Here are some environmental variables specific to various shells:$versionis set on tcsh$BASHis set on bash$shell(lowercase) is set to actual shell name in csh or tcsh$ZSH_NAMEis set on zshksh has
$PS3and$PS4set, whereas the normal Bourne shell (sh) only has$PS1and$PS2set. This generally seems like the hardest to distinguish – the only difference in the entire set of environment variables betweenshandkshwe have installed on Solaris boxen is$ERRNO,$FCEDIT,$LINENO,$PPID,$PS3,$PS4,$RANDOM,$SECONDS, and$TMOUT.UPDATE: Someone brought up "
ash" (Almquist Shell) in comments. There seem to be 2001 variants of it includingdash; so in the interest of not blowing up the answer unnecessarily, here’s a very useful page listing a ton of various flavours ofashand their differences from each other and often from stanard Bournesh: https://www.in-ulm.de/~mascheck/various/ash/