views:

243

answers:

5

If I'm in a shell script that has no shebang line… Hum, that doesn't matter, because indeed sh is always used. Maybe not so.

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?

A: 

I'm not sure of the protocol on stackOverflow for editing answers after people have commented, but here goes. Anyway, the command used to invoke the currently running shell is stored in the environment variable "$0".

echo $0

This outputs your currently running shell OR the path to your currently running shell, depending on how it was invoked, so you might need to do some processing:

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

My original answer of "$SHELL" apparently echos the user's preferred shell.

Mala
No, that is the user's preferred shell. Not the current shell. `$ /bin/sh` ... `sh-3.2$ echo $SHELL` ... `/bin/bash` Clearly not correct.
derobert
Ah, my apologies. Thanks for pointing that out derobert. I edited my answer to be correct.
Mala
I'm all for editing all the time. And then removing the comments which are no longer relevant. I'm all on the wiki side of the thing.
kch
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
Ah darn it. Oh well, I guess I'm learning too...
Mala
Well, your answers are still valid as a list of things not to do. You could update it as such and earn a few upvotes while at it.
kch
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
+1  A: 

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
Sane way to do that is: `ps $$`
derobert
more precisely, ps -o command $$
kch
Yep, I agree these are more precise commands. This probably one of the "lazy" habits to use the same command for all tasks :)
dimba
@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
+2  A: 

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
FYI, I'm on OS X, and we don't have /proc.
kch
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
So much for my quest for a simple solution. But it's been fun so far. Using lsof was way far out there.
kch
OTOH there's no lsof on solaris, so, no cross-unix way…
kch
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
+1  A: 

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
A: 

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