Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

marked as duplicate by Stéphane Gimenez, manatwork, Mat, Mikel, Caleb Apr 28 '12 at 16:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 4 down vote accepted
  1. Quick and dirty (and possibly wrong):

    There is almost always an environment variable $SHELL that 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_VERSION or $ZSH_VERSION. I don't know that sh does this.

  2. 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 ran ps -fp $$, which would retrieve the process data for the process that launched ps.

  3. 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 the SHELL variable might be lieing to you.

share|improve this answer
1  
+1 for readlink method. That should be added to unix.stackexchange.com/questions/9501/… as well. –  Mikel Apr 28 '12 at 16:39
    
Only number 2 works for me –  Tyilo Apr 28 '12 at 16:44
    
@Tyilo: Interesting that #3 doesn't work. What OS are you on, and do you have a readlink command at your disposal? –  Caleb Apr 28 '12 at 16:45
    
@Caleb Mac OS X (readlink v???): readlink: illegal option -- f. Jailbreaked iOS (readlink v8.12): empty –  Tyilo Apr 28 '12 at 16:48
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.