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.

How to check what shell I am using in a terminal? What is the shell I am using in MacOS?

share|improve this question
    
I'm pretty sure this is a dupe... do I want to find it... –  xenoterracide Mar 18 '11 at 11:55
1  
@xenoterracide: at least unix.stackexchange.com/questions/3645/… . geekosaur's answer is more complete, so i voted to close the earlier question. –  Gilles Mar 30 '11 at 17:33
    
echo $SHELL, no? –  innocent-world Aug 23 '13 at 11:49
add comment

6 Answers

I've found that the following works in the four shells I have installed on my system (bash, dash, zsh, csh):

$ ps -p $$

The following works on zsh, bash, and dash, but not on csh:

$ echo $0
share|improve this answer
add comment

Several ways, from most to least reliable (and most-to-least "heavy"):

  1. ps -p$$ -ocmd=. (On Solaris, this may need to be fname instead of cmd.)
  2. Check for $BASH_VERSION, $ZSH_VERSION, and other shell-specific variables.
  3. Check $SHELL; this is a last resort, as it specifies your default shell and not necessarily the current shell.
share|improve this answer
2  
Should you mention $0 too? –  Mikel Mar 18 '11 at 2:41
    
I don't like $0 because it's more complicated: (1) it may be just the basename, (2) it may have '-' on the front to designate it as a login shell. –  geekosaur Mar 18 '11 at 2:44
    
ps -p$$ -ocmd="" is prettier :-) –  asoundmove Mar 18 '11 at 3:51
    
@geekosaur: maybe so, but $0 still seems more useful than $SHELL: wouldn't you agree? You could always pipe it through sed to remove the '-'. –  iconoclast Aug 29 '12 at 21:49
    
On Mac, #1 is ps -p $$ -o comm="". Also, for those wondering, $$ is the shell process ID. –  duozmo Oct 19 '13 at 18:00
show 1 more comment

A note about some lighter implementations (Android phones, busybox, etc.): ps doesn't always have support for the -p switch, but you can accomplish the search with a command like ps | grep $$.

share|improve this answer
    
ps | grep $$ can still give false positives if, for example, your current process is 1234 and there's a process 12345. –  Keith Thompson Mar 18 at 1:48
add comment

This works too:

env | grep SHELL
share|improve this answer
3  
Or not. See point 3. in geekosaur's answer. –  manatwork Mar 14 '12 at 17:02
add comment

As the question asks for the shell used and does not talk about the potential arguments passed to it, here is a way that avoid showing them:

$ ps -o comm= -p $$
ksh93 
share|improve this answer
add comment

A mix of all the other answers, compatible with Mac (comm), Solaris (fname) and Linux (cmd):

ps -p$$ -o cmd="",comm="",fname="" 2>/dev/null | sed 's/^-//' | grep -oE '\w+' | head -n1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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