next up previous
Next: More Shell Commands Up: Introduction to the Linux Previous: Aliases

Shells

Whenever you type a wildcard like '*' as an argument to a command, ls for example, it is not ls itself that looks for matching files. In fact the asterisk is expanded before ls is even run! Every command is interpreted by a special program called a shell that looks at what you type, and works out what it should do with it. It is the shell that knows that ls is a command, and that replaces the * with everything.

There are three shells you are likely to come across. The sh shell is one of the original shells, and is called the Bourne shell after its author. It is not usually used for interactive terminals, but it has some nice features and is often used in scripts (more later). The Bourne shell was reworked and gave rise to the second shell, called the bash shell, which stands for Bourne again shell. Don't blame me, I didn't make it up.

The third common shell is the csh shell, or C-shell. No, I don't know who makes these up. The original csh didn't have tab-completion and a few other nice features, so these days almost everyone uses a variant called tcsh.

For the sorts of things we've been discussing so far, both bash and csh will work fine. However they use separate configuration files, .bashrc and .cshrc respectively, and if things like aliases are set in these then it is possible for you to see different behaviour with different shells.

You can see which shell you are using by typing

[phasnip@mijpnb1 ~]$ echo $SHELL
/bin/tcsh

We haven't used the echo command before, but essentially it writes text to terminal window.

[phasnip@mijpnb1 ~]$ echo Hello
Hello

Prefixing text by a dollar sign $ tells the shell that you are referring not to a bit of text you typed, but to an environment variable. Environment variables are simple strings of characters that are given names, and are then available to other processes. The $SHELL variable is set by the shell itself when it starts, precisely so that processes (and you!) can see which shell is running. Another important environment variable is $PATH.

[phasnip@mijpnb1 ~]$ echo $PATH
/usr//bin:/bin:/usr/bin::/usr/local/bin:/usr/X11R6/bin:/usr/games:/home/phasnip/bin
which tells you which directories the shell looks in for any commands you type.

There is also an environment variable to tell you what kind of terminal you are using

[phasnip@mijpnb1 ~]$ echo $TERM
xterm

You can set your own environment variables, but this is where the shells differ slightly. In the bash shell:

[phasnip@mijpnb1 phasnip]$ MYVAR=hello
[phasnip@mijpnb1 phasnip]$ echo $MYVAR
hello

However any processes launched from this shell will not inherit this environment variable. In order to change that we use the bash command export.

[phasnip@mijpnb1 phasnip]$ export MYVAR

We can see what environment variables are set by using the set command, and use unset MYVAR to unset and remove a variable.

In the csh shell we set variables using

[phasnip@mijpnb1 phasnip]$ setenv MYVAR hello
[phasnip@mijpnb1 phasnip]$ echo $MYVAR
hello

Variables defined in the csh shell are automatically inherited by any process launched from that shell. To see what variables are defined in the csh shell simply type setenv, and you can use unsetenv to remove a variable.

You can always change your default shell using the command chsh.


next up previous
Next: More Shell Commands Up: Introduction to the Linux Previous: Aliases
Phil Hasnip 2007-08-23