About Shells

Index

While "graphical shells" exist for UNIX that imitate the Apple Macintosh or MIcrosoft Windows, most users stick with the traditional command line interface. You type commands one by one on your terminal or into a terminal window, press ``return'', and they are executed. The program which interprets your command is called a ``shell''. By default, users at Mathnet and PIC are given the ``C-shell''; however, you can specify a different shell if you want.

This writeup describes a few features of the shells that can make your life easier. See the man page of your shell for complete features.

Input/Output Redirection

When there is no file specified on the command line, UNIX commands usually read from ``standard input'', which is normally the terminal. (If a command just sits there, is it expecting input from you on standard input?) Output generally goes to ``standard output'' or ``standard error'', also normally directed to the terminal. It is often useful, however, to redirect input and output elsewhere. Here is the syntax to do it; for the most part it works the same on both C-shell and Bourne shell. In the examples the word ``command'' represents the command being redirected.

command < file
Standard input will be that file.
command > file
Standard output will be that file. It will be created if not existing, or overwritten from the beginning.
command >> file
Standard output will be appended at the end of the file.
command >& file
Both standard output and standard error will be written to the file. Use >>& to append. The Bourne shell syntax is different; it can send standard error to a different file.
command1 | command2
The standard output of command1 is piped to the standard input of command2. Use |& to pipe both standard output and standard error (Bourne shell is different). The vertical bar is generally pronounced ``pipe'' in UNIX environments.

In the man page for your C-shell variant look for the variable ``noclobber'' which affects what the shell does if redirect output files do or don't pre-exist.

Filename Pattern Substitution

Most UNIX commands, when given a sequence of filenames, will do their function on each file in turn: for example, to remove the file, or to edit it. The shell provides a syntax to specify many files based on patterns. The effect of a filename pattern is as if each filename were written out in full on the command line. Unlike in other operating systems, the command does not see the pattern. (For the rare command that can handle a filename pattern itself, like the ``find'' command, enclose the pattern in quotes.)

* (asterisk)
Matches any zero or more characters. Examples:
*.gif
All names ending in .gif
program*
All names beginning ``program'', including just plain ``program''
m271*.f
Matches all files with the given beginning and ending.
? (question mark)
Matches any single character.
[] (brackets)
Matches specific characters within the brackets; for example, [AB] would match an ``A'' (upper case) or a ``B''. For a range, separate the endpoints by a hyphen: [A-Z] matches one upper case alphabetic letter; [A-Za-z] matches any letter. To complement the set, let the first character be ^ (capital 6 on most keyboards): [^0-9] matches anything but a digit.
{} (braces)
Enclose an explicit list of alternatives, comma separated (no spaces). Example: Thesis.{aux,log} designates Thesis.aux and Thesis.log (without matching Thesis.tex, an important feature when removing files).

You can have more than one metacharacter in one filename pattern, and they may occur in directory names. The leftmost one is matched first, then out of all names matching up to that point, subsequent matches are attempted.

Variable, Command, Alias and History Substitution

You can set a named variable (these examples use the name ``vbl'') and later have the value substituted in command lines by writing ``$vbl'' (using the name you chose).

set vbl = value
C-shell syntax to set a variable.
setenv vbl value
C-shell syntax to set an environment variable, which is available to your programs. Note, no equal sign.
vbl=value
Bourne shell syntax to set a variable.
export vbl
In Bourne shell, this makes it an environment variable.

You can use the standard output of a command to be one or more words in another command line. Surround the subcommand with grave accents. Newlines in the output are irrelevant. For example:

    rup `hostgroup math@fs`

The hostgroup command produces a list of computers' names, and ``rup'' then queries each one for its load and uptime.

In shells (like C-shell) that support history substitution, an exclamation point (pronounced ``bang'') causes material to be inserted from previous command lines, like this:

!$
Repeats the last argument (usually a filename) from the previous command.
$^
Repeats the first argument of the previous command.
!!
Repeats the entire previous command. You may add material at the end, e.g. ``!! another.file''.
!<nbr>
Repeats command number N.
!<ltrs>
Repeats the recent command beginning with the specified letters.
^abc^xyz
As a special case, replaces the first string with the second (which may have zero letters). This is useful for fixing typing errors.

You can abbreviate complicated or often-used command lines like this:

    alias cpl "cc -c -I../include -DMATHLIB"
When you give the alias, here the command ``cpl'', the effect is as if the whole definition had been written out. Arguments are appended after the definition. Except, you can include !$ or !^ or similar history substitutions and they will bring in arguments from the command line as typed, for more complicated effects. Use a backslash before the bang so it isn't substituted during the definition.

Special Keyboard Characters

The symbol `^' indicates the control key. For example, ^C means hold down the control key (like a shift key) and press the `C'.

^H
Erase the previous character. Sometimes the erase character ends up as ^? (the del key). If your backspace key isn't erasing, try del, and vice versa. This behavior is seen on ASCII terminals when you specify the terminal type incorrectly.
^U
Erase the whole line. On other UNIX systems ^X may be seen for this.
^W
Erase the previous word (delimited by spaces).
^R
Reprint the command line, in case extraneous output messed it up. Some full-screen editors look for ^L to refresh the whole screen.
^V
Literal next: to type a control character and not have it do its special function, precede it with ^V. Particularly needed for tabs in tcsh.
\ (backslash)
Shell metacharacters such as asterisk and apostrophe must be preceeded by a backslash if you want the shell to not process them in the normal way.
^D
End of file. When reading input from the terminal, most programs will interpret ^D as the end of data.
^C
Control-C will usually kill whatever you are doing and signal the shell to accept the next command. Use ^C only if you are stuck, since work you did during the killed command may be lost.
^Z
Suspend the current job (see the next section, ``Job Control''.)

Job Control

jobs
List your current jobs (if any). Each line begins with a number. The term <job> means this number preceeded by a percent sign; e.g. %2 means job number two. %+ refers to the job most recently commanded, whose number is followed by a plus sign (and this job is implied if you omit the <job> designation); %- refers to the second most recent job, marked with a minus sign.
^Z
Suspend the current job. It doesn't execute, nor read terminal input, but it does occupy system memory. See the caveat below. Don't just leave it or you'll regret it. Beware, ^Z is sometimes typed by accident and the user gets into trouble due to later executing a second instance of the command before finishing the first.
fg <job>
Resume a suspended job.
bg <job>
Run a suspended job in the background. It executes independently of your terminal input.
<command> &
Start the command in the background. This is the normal way to initiate background jobs. Input and output redirection are recommended in the command.
kill <job>
Make a job terminate processing abnormally, similar to using ^C on a foreground job. Test your program in the foreground on a small problem before letting it loose as a big background job.
kill -STOP <job>
Suspend a background job, similar to using ^Z on a foreground job.
ps
This command will identify your background jobs (running on the same computer). If you log out, then log in again later, the background job(s) do not belong to your shell any more, and you need to use the process ID found by ``ps'' to kill the job, if necessary.

Caveat: UNIX commands by default do not do file locking. This means that if you have multiple jobs writing into the same file, the output can be intermingled and unuseable. If you suspend a command, such as an editor or ``mail'', particularly if by accident, be sure to restart it and finish it before starting a new command that operates on the same file, or the file (such as your mailbox) may be corrupted.

It's always safe for multiple jobs to read from the same file, and it's usually safe for multiple jobs to append to the same file (if line buffered).

Other Shells

By default you get the C-shell (csh) at login, but you may specify a different shell if you prefer. These are the shells currently supported. For complete details about their capabilities and attractive features, see the respective man pages.
/bin/csh
The C-shell, which comes standard with almost every type of UNIX. It has many attractive features for interactive use, such as aliases, history subsitution, filename completion, job control, "and much, much more!" Source: Solaris.
/bin/sh
The Bourne shell, which comes standard with every type of UNIX. It is favored by many for script writing, but does not have very nice interactive features. Source: Solaris.
/bin/jsh
An enhanced Bourne shell with csh-style job control. Source: Solaris.
/bin/ksh
A modernized Bourne shell with syntax enhancements making it a competitor to the C-shell for interactive use. Source: Solaris.
/usr/local/bin/bash
The ``born-again'' shell. It has Bourne-shell syntax, but adds interactive features that /bin/sh lacks, like aliases, history substitution, filename completion, and command-line editing. It also improves on some of the useful scripting features of /bin/sh, and adds some scripting features of the C-shell. Source: Gnu.
/usr/local/bin/tcsh
Tcsh is basically a superset of the C-shell, although it does correct some of the shell-scripting weaknesses of /bin/csh. In addition to all the C-shell features, tcsh provides command-line editing, better filename completion, command-line spelling correction, "and much, much more!" Source: Christos Zoulas <christos@ee.cornell.edu>

To use a shell other than the default shell, /bin/csh, simply put the full pathname of the shell in a file in your home directory called .shell. For example, if you want bash to be your shell, you could execute:

    echo "/usr/local/bin/bash" > $HOME/.shell
The next time you log in, bash will read and execute your commands instead of csh, which you can recognize by the different style of prompt. To go back to the default shell, simply remove $HOME/.shell. Shells are either in /bin or /usr/local/bin; to find out where your favorite shell is, do 'which ' (e.g. 'which tcsh').

Technical Details

[Note: if you're not familiar with the Math or PIC login setup, you might want to browse the handout ``Dotfiles'' now or skip this section altogether.]

The /usr/math/default/login_bridge script checks the user's home directory for a file called .shell . If this file contains the full pathname of a supported shell, then the environment variable $SHELL is set to that pathname. (Technically, login_bridge only requires that the pathname appear alone on the last line of the file; everything else in the file is ignored.) If the user has no $HOME/.shell, or if the file does not list a supported shell, then $SHELL is set to the default shell, currently /bin/csh. Finally, the script /usr/math/bin/uinit then exec's the program named by the environment variable $SHELL.