- Help v^
File Edit Bookmark Help
[Contents] [Search] [Back] [History] [Glossary]

About Shells

Presented here is a brief list of common questions about shells. All the shells on the Computer Society systems are installed with their full manual pages which may be consulted for more detailed information.

What does a shell do?
When you log in to one of the Computer Society machines, the shell is the program which takes the commands that you write and interprets them. Normally it is used simply as a "front end" to run other programs from. However, it is more powerful than this. Shells can be programmed in a similar way to batch files in MS-DOS, although most shells have considerably more commands than MS-DOS.

A shell can also be used to set environment variables which determine the operation of the programs which you might run from it. The way you do this is described below and it differs between the two main families of shell. You only need read the section on how to do this for the shell that you use normally. If you do not know which shell you use you can find out by doing ypmatch username passwd where username is replaced by your user name. The shell is the last of the colon separated entries printed, and its full path is specified.

If you want to do more complicated things with your shell then read the manual pages and look for a good book on the subject.

Common types of Shell
There are several common shells. They come in two main families, the Bourne compatible shells, such as sh and bash, and C Shell compatible shells such as csh and the enhanced tcsh. The tcsh and bash shells are usually the best choices to use as they support the cursor keys for command history and the use of the TAB key for command/filename completion.

Other types include the Korn shell, ksh, which is a superset of sh and the Z shell, zsh, which is most similar ksh but with some differences and enhancements. Also there is ash which is like sh but different again.

To find out which shells are available on your system, type cat /etc/shells and a list will be produced. Read the instructions below to change your login shell to any one of these. If you want to try out the shell before making it your main shell then just type its name like any other program, and type exit to return to your original shell. I have yet to come across a shell where the exit command does not do the obvious.

How to change your login shell
There is a system command ypchsh which allows any user to change their shell to any of the programs specified in the file /etc/shells. Upon running it you will probably prompted for your password (not all versions of chsh do this) followed by the new shell that you wish to use as your login shell. You can try a shell out with out making it your default login shell as described above.

At Login
When you log in, your shell reads various configuration files. Which files it reads depends upon your shell. If you create shells at other times, for example if you run one from a program to allow the execution of commands without exiting the program, then a different set of configuration files is read. There is a total of three possible ways of running a shell, each of which typically results in a different set of files being read:

In this document, I will only examine the bash and tcsh shells; for other shells please consult the manual pages. In general, the principles still apply, but the actual details are likely to be different.

Bash does the following:

Tcsh, however does this:

Both shells can be made to read files upon logout too: see their respective manual pages for that.

Setting Environment Variables under Bourne like Shells
To set an environment variable under a Bourne like shell, you can do the following:


VAR="A String"

which will set the value of VAR to "A String". This however is not very useful, as the variable is only visible to the current shell. Normally you want to set the variable so that all the programs which are run from the shell see it too. To do this you type

export VAR

or combine the two...

export VAR="A String"

If there are no spaces in the value that you are setting, then you can omit the inverted commas around it. To view the environment you can do

echo $VAR

to see the value of variable VAR or you can do

printenv

to see all the environment variables currently set.

Setting Environment Variables under C Shells like Shells
In C like shells, you set the environment variables in a similar way to the Bourne like shells. In this case


setenv VAR "A String"

is used to set a variable. You can view the variables however using exactly the same commands, namely

echo $VAR

to see a single variable and

printenv

to see the whole lot.

The Path
Now you've read the above description of how to set environment variables, it is time to put that to use. The most important environment variable from the point of view of the average user is the path. The path is a list of directories which the shell looks through to find the program you want to run when you type its name on the command line.

Programs on UNIX like operating systems are normally held in a few directories (often containing many files). The path is then set up by the system administrator so that it points to the correct directories to find the system commands.

Suppose for a moment that you write your own program. You cannot put it in the system directories, because you only have permission to your own directory structure. So you create your own directory to put programs in, called bin for the purposes of this example. The next thing you want to do is to be able to run these programs without having to write ~/bin/myprogram every time. Here is how you do it, with one example each for bash and tcsh.


# Bash Example
cd ~
mkdir bin
mv myprogram bin
export PATH=$PATH:~/bin

# Tcsh example
cd ~
mkdir bin
mv myprogram bin
setenv PATH $PATH:~/bin

In each case, you might want to move the last line of the example into the files which get run at system startup. However beware, if you mess up the path, you may only be able to run system commands by entering their full path, so test it before committing your self. You can either do this by logging in again, but keeping the original session going so that you can change things back if they do not work, or alternatively by creating a new login script and typing source newloginscript in your current shell.

Well thats just about all you need to know about shells for basic use. Check the manual pages and have fun.