43

Is there a command to identify the name/type of current shell, the path to the shell binary, and the version of the shell?

I don't need all of that, but the more I can get, the better.

I want something that has the same feel of uname, pwd, whoami. Just a plain utility with a simple output. (which so far hasn't showed up :/ )

re ps

$ ps -o comm $$
COMM
-bash

Why -bash instead of the full path as it would be with everything else? What's the deal with the dash there?

5
  • -bash is because its a login shell. See my comment about $0 in one of the answers below.
    – derobert
    Commented Aug 15, 2009 at 10:27
  • I'm starting to think the 'test for features not for browsers' thinking we use in javascript for the web may apply here.
    – kch
    Commented Aug 15, 2009 at 10:44
  • I always like to counter a question with a question of my own: why do you need to know this--how do you intend to use that information and to what purpose? Commented Aug 15, 2009 at 11:50
  • 1
    @wez just general curiosity. I have no particularly glaring need for it at the moment.
    – kch
    Commented Aug 15, 2009 at 12:16
  • Possible duplicate of stackoverflow.com/questions/5166657
    – Jens
    Commented Aug 29, 2011 at 13:23

8 Answers 8

55

The command or path to the currently running shell is stored in the environment variable $0. To see its value, use:

echo $0

This outputs either your currently running shell or the path to your currently running shell, depending on how it was invoked. Some processing might be required:

prompt:~$ echo $0
/bin/bash
prompt:~$ sh
sh-4.0$ echo $0
sh
sh-4.0$ exit
exit
prompt:~$ /bin/sh
sh-4.0$ echo $0
/bin/sh
sh-4.0$

The $SHELL environment variable contains the user's preferred shell, not necessarily the currently running shell.

3
  • 1
    So, $0 actually gives you the name of the currently running script. So, inside a script it'll give you the name of the script, not the name of the shell that's interpreting the script.
    – kch
    Commented Aug 15, 2009 at 9:59
  • 6
    Well, technically, $0 gives you whatever the program executing your program decided to pass to the exec-family of syscalls. Try it on a login shell, for example, and you may see -bash — with the - in front.
    – derobert
    Commented Aug 15, 2009 at 10:13
  • 1
    The fun part is this is a sh (and shlike) thing. If your shell is say, fish it doesn't work.
    – leff
    Commented Jun 7, 2017 at 15:56
7

If you don't specify a program in the shebang line, I believe /bin/sh will be used. Unfortunately, I don't believe there is a good portable way to determine what that shell is.

If you're on e.g., Linux, you can find out the executable path through /proc:

$ readlink "/proc/$$/exe"
/bin/dash

and getting the executable name is easy through ps $$.

But that won't help you with the type of shell (except via a lookup table of known shells) nor with the version (AFAICT, there isn't even a way to get the version from dash)

4
  • FYI, I'm on OS X, and we don't have /proc.
    – kch
    Commented Aug 15, 2009 at 10:04
  • 2
    Hmm, you could use lsof -p $$ and look for the first txt entry, that seemed to do it on my Mac... But wow is that fragile.
    – derobert
    Commented Aug 15, 2009 at 10:24
  • So much for my quest for a simple solution. But it's been fun so far. Using lsof was way far out there.
    – kch
    Commented Aug 15, 2009 at 10:32
  • 2
    If no interpreter is specified, the currently running shell will be used rather than /bin/sh. (This is why people that use csh as their login shell and write scripts with no shabang belong in the 4th circle of hell.) Commented Aug 15, 2009 at 11:48
5

Try ($$ is shell variable set to process id of the shell):

ps -ef | grep $$

or try this (/proc/self is aloso process id of the shell):

ps -ef | grep /proc/self

As regards to "-bash" - dash means it's login shell. Type bash again and now you'll see that the shell is just "bash" (without dash)

3
  • 3
    Sane way to do that is: ps $$
    – derobert
    Commented Aug 15, 2009 at 9:59
  • Yep, I agree these are more precise commands. This probably one of the "lazy" habits to use the same command for all tasks :)
    – dimba
    Commented Aug 15, 2009 at 10:16
  • 1
    @kch: Actually, -o comm according to POSIX. They had to abbreviate it for some reason. But it may have the same problems as $0.
    – derobert
    Commented Aug 15, 2009 at 10:18
2

I think 'finger' is the right one you are looking for. Try this command:

finger `whoami`
1
  • 1
    That echoes the preferred shell, not the one that is currently running.
    – HerbCSO
    Commented Mar 24, 2014 at 15:12
1

Rather than trying to determine the shell currently being used, it is typically more appropriate to simply re-exec as the desired shell. This may be nothing more than an historical workaround of the fact that there is no reliable, portable way to determine the shell you are currently using. The best thing to do is to write your script to work in as many shells as possible so that it's not an issue. (eg, portability matters, no matter how many people want to claim that "bash is everywhere")

1

You can use the following in Arch Linux.

echo $SHELL
-1

This shows me more reliable result.

ls -la /bin/sh
-2

I want something that has the same feel of uname, pwd, whoami. Just a plain utility with a simple output.

So apparently the conclusion is that the tool I want does not exist, and there's no simple cross-platform way to go about this.

Some answers here work fine on Linux.

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