tags:

views:

329

answers:

3

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?

+5  A: 

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 FRED
My name is Fred.
Charlie Martin
+5  A: 

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.

Bill Karwin
You may also want to mention the SHLVL environment variable.
Rob Kennedy
+3  A: 

$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

hhafez