kch
kch

Reputation: 79552

What shell am I in?

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?

Upvotes: 43

Views: 33231

Answers (8)

Upul Doluweera
Upul Doluweera

Reputation: 2326

You can use the following in Arch Linux.

echo $SHELL

Upvotes: 1

Arsen
Arsen

Reputation: 664

This shows me more reliable result.

ls -la /bin/sh

Upvotes: -1

Mala
Mala

Reputation: 14803

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.

Upvotes: 55

Jackie Yeh
Jackie Yeh

Reputation: 1627

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

finger `whoami`

Upvotes: 2

kch
kch

Reputation: 79552

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.

Upvotes: -2

William Pursell
William Pursell

Reputation: 212248

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")

Upvotes: 1

dimba
dimba

Reputation: 27581

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)

Upvotes: 5

derobert
derobert

Reputation: 51147

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)

Upvotes: 7

Related Questions