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?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
kch
  • 77,385
  • 46
  • 136
  • 148
  • `-bash` is because its a login shell. See my comment about `$0` in one of the answers below. – derobert Aug 15 '09 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 Aug 15 '09 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? – Wez Furlong Aug 15 '09 at 11:50
  • 1
    @wez just general curiosity. I have no particularly glaring need for it at the moment. – kch Aug 15 '09 at 12:16
  • Possible duplicate of http://stackoverflow.com/questions/5166657 – Jens Aug 29 '11 at 13:23

8 Answers8

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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Mala
  • 14,178
  • 25
  • 88
  • 119
  • 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 Aug 15 '09 at 09: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 Aug 15 '09 at 10:13
  • 1
    The fun part is this is a `sh` (and `sh`like) thing. If your shell is say, `fish` it doesn't work. – leff Jun 07 '17 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)

derobert
  • 49,731
  • 15
  • 94
  • 124
  • FYI, I'm on OS X, and we don't have /proc. – kch Aug 15 '09 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 Aug 15 '09 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 Aug 15 '09 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.) – William Pursell Aug 15 '09 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)

dimba
  • 26,717
  • 34
  • 141
  • 196
  • 3
    Sane way to do that is: `ps $$` – derobert Aug 15 '09 at 09: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 Aug 15 '09 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 Aug 15 '09 at 10:18
2

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

finger `whoami`
Jackie Yeh
  • 1,567
  • 1
  • 9
  • 11
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")

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

You can use the following in Arch Linux.

echo $SHELL
Upul Doluweera
  • 2,146
  • 1
  • 23
  • 28
-1

This shows me more reliable result.

ls -la /bin/sh
Arsen
  • 654
  • 8
  • 20
-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.

kch
  • 77,385
  • 46
  • 136
  • 148