How to determine the current interactive shell that I'm in (command-line)

Viewed 605928

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?

28 Answers

I like Nahuel Fouilleul's solution particularly, but I had to run the following variant of it on Ubuntu 18.04 (Bionic Beaver) with the built-in Bash shell:

bash -c 'shellPID=$$; ps -ocomm= -q $shellPID'

Without the temporary variable shellPID, e.g. the following:

bash -c 'ps -ocomm= -q $$'

Would just output ps for me. Maybe you aren't all using non-interactive mode, and that makes a difference.

Get it with the $SHELL environment variable. A simple sed could remove the path:

echo $SHELL | sed -E 's/^.*\/([aA-zZ]+$)/\1/g'

Output:

bash

It was tested on macOS, Ubuntu, and CentOS.

One way is:

ps -p $$ -o exe=

which is IMO better than using -o args or -o comm as suggested in another answer (these may use, e.g., some symbolic link like when /bin/sh points to some specific shell as Dash or Bash).

The above returns the path of the executable, but beware that due to /usr-merge, one might need to check for multiple paths (e.g., /bin/bash and /usr/bin/bash).

Also note that the above is not fully POSIX-compatible (POSIX ps doesn't have exe).

You can use echo $SHELL|sed "s/\/bin\///g"

And I came up with this:

sed 's/.*SHELL=//; s/[[:upper:]].*//' /proc/$$/environ
Related