Possible Duplicate:
How to test what shell I am using in a terminal?
How do I determine the current shell path or shell name?
I only really need to distinguish sh and bash, however a portable solution would be nice.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
-
Quick and dirty (and possibly wrong):
There is almost always an environment variable
$SHELLthat you can learn this from. There are some edge cases where this will fail, particularly if one interactive shell is used to launch another. Most recent shells also set a variable about themselves in the form of$BASH_VERSIONor$ZSH_VERSION. I don’t know thatshdoes this. -
A bit fancier:
Alternatively, most interactive shells will know what they are and give you some useful output to if you run something like
echo $0. This appears to rely on about the same information as you would get if you ranps -fp $$, which would retrieve the process data for the process that launched ps. -
More robust:
A way that might work for some shells and environments that don’t have convenience things such as environment variables set would be to rip the data you want out of proc and figure out what the running executable path is actually pointing to using
readlink -f /proc/$$/exe. This has the advantage of giving you information about what executable was lauched even if it was a symlink to something else, in which case theSHELLvariable might be lieing to you.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0