I know this is supposed to output what kind of shell I'm using, which I think it does, because it outputs "bash-3.2", but it doesn't quite do that, because it actually changes my prompt to "bash-3.2$". What else is going on? When I do Ctrl+D, I go back to my original prompt. Is this starting the bash shell? I thought by opening a terminal window (I'm in Mac OS X), I was opening a shell program.

I tried to Google, but I couldn't get any good results for $SHELL. As an aside, how do I get Google to include the "$" in my search?

4

Best Answer


Yes, when you open a terminal window you are starting a shell.

The name of the shell program is stored in the $SHELL variable. Running simply "$SHELL" at the command line uses the value in that variable, and then interprets it as a command -- which runs a new shell. When you exit that shell, you return to the shell where it left off.

Sort of like when you're working on something at your desk, and your boss drops some new work on you, so you pause the original work while you do this new thing, and then you can pick up where you left off once you're done with the new task. :-)

This new shell is not a login shell, so it uses a default prompt instead of the prompt specified in your shell login configuration file.

$SHELL is useful when you need to run a command from within another program. For example, if you're in a text editor and you want to interpolate the output of a shell command, the editor knows which shell program to run by your $SHELL environment variable.


Rob Kennedy comments about the $SHLVL variable. This indicates how "deep" you are in shells. For example, when you log in, "echo $SHLVL" prints 1. If you open a new shell, the same command prints 2. After you quit that subshell and return to your login shell, the command prints 1 again. This is kind of beyond what you were asking, and its uses are more esoteric, but it's kind of interesting.

All $SHELL is a macro or shell variable that contains the name of the shell. So when you type

$ $SHELL

the line is immediately expanded to

$ bash-3.2

which on your system is the name of a bash executable. So it runs the executable.

If you want to just see what's IN the variable, type

$ echo $SHELL

which just copies the arguments to stdout.

You can create your own variables as well, eg,

$ FRED="My name is Fred"

after which

$ echo $FRED

will print

$ echo FREDMy name is Fred.

The other answers have correctly pointed out that SHELL is a variable which contains the name of the shell program – but they don’t specify which shell this refers to. One of the answers perpetrates the common mis-understanding that the SHELL variable indicates that the shell currently being used. This is not the case.

It’s actually an environment variable set by the system login process to the default login shell for the user. In Unix systems, this is usually configured in /etc/passwd – unless using NIS, LDAP, Active Directory, or some other authentication mechanism.

From the POSIX 2008 section on Environment Variables

SHELL

This variable shall represent a pathname of the user's preferred commandlanguage interpreter.

From the Bash manual section on Bash Variables

SHELL

The full pathname to the shell is kept in this environment variable. If itis not set when the shell starts, Bash assigns to it the full pathname ofthe current user’s login shell.

Typical uses

It’s used by

  • programs featuring shell escapes to allow shell commands to be run from within the program, e.g., text editors such as Vim.

  • GNU Screen to determine which shell should be run as the default shell when opening new windows.

$SHELL itself is not a command; it is a variable storing the name of the shell that you are currently running

try echo $SHELL at the command prompt

you will get something like this

/bin/bash

so what you are really doing when you type

$SHELL

is

/bin/bash

which is why you get a new shell