Stanford University
Search Directory News & Events Computing Resources Home
School of Earth Sciences
  

Using the UNIX Shell

last revision September 29, 2002

Table of Contents

  1. What is actually happening when you log in to a UNIX system?
  2. How does the UNIX shell interpret what you type?
  3. Editing command lines with UNIX shells.
  4. What to do if you get stuck in a UNIX login session?
  5. What happens when a program executes?
  6. Some simple commands that you can try.
  7. Fuller discussion of UNIX command syntax.

  8. Controlling Processes
  9. Setting up your login environment the way you like

Related topics:

What is actually happening when you log in to a UNIX system?

These notes describe the process of using a command-line shell interface to UNIX. If you are using an X-window terminal or the console of a UNIX workstation, you must first establish your X-window session and then open a "shell" or "command" window (one is often opened automatically). These notes will apply to what happens within that shell window.

The preferred method of logging into a command-line shell over the network is to use an encrypted protocol such as kerberos or ssh. See the section on UNIX accounts and passwords for a description of these protocols and the programs you can get to use them on your computer.

UNIX treats each login window, whether via kerberos, ssh, X-windows, plain telnet, rlogin, or other protocol, as a separate "terminal", and I will use that word to refer to the window on your computer in which you interact with the remote UNIX system. After the login connection is made, and account name and password are provided as needed, you will be running a single process on the UNIX system that is attached to your terminal for input and output. This process is owned by your account and the program it runs is called the "shell".

The shell interprets what you type to determine what you want to do. It then makes calls to the kernel (UNIX) or starts up other processes running other programs as needed to carry out your commands.

There is more than one shell program. The one you use is set in the system accounts (password) file. All examples in these notes use the "C-shell" (program named csh), which is most commonly used on Berkeley UNIX systems, and is the default shell for pangea accounts in the School of Earth Sciences. Many people now prefer tcsh, which is a newer superset of csh with additional features such as automatic filename completion. All features of csh described here should also work in tcsh.

Back to Table of Contents

How does the UNIX shell interpret what you type?

The shell uses a "prompt" to let you know that it is ready to accept commands. The standard UNIX prompt is a percent sign (%). On pangea, we have changed this to be pangea>. You can set your own prompt, which will be described in the section on controlling your shell environment.

Case is important! Uppercase A and lowercase a are not the same. UNIX is strongly oriented to lower case letters. In general, everything is in lower case, unless the documentation specifically says to use upper case, for example, for certain command options.

Everything between blanks (spaces) or tabs, unless quoted, is considered to be a separate "word" or "token". Pressing the RETURN key signals the shell to start interpreting the line. You can keep typing a long command past the end of the screen and it will wrap around. Just do not press the RETURN key until you have typed the complete command.

The first word typed on a line is generally interpreted as the name of a program to run. The shell looks in a set of pre-defined directories (which you can alter) to find an executable file of the same name and then starts a process to execute that program. Remaining "ordinary" words or tokens on the line are interpreted as an "argument list" of information to be passed along to the program. This list describes the files to be affected or the options to be used.

Some words that you type are "reserved keywords" that are interpreted as commands directly to the shell to change some attribute of its own process, not as programs to be run. An example is the cd command, which changes the current working directory of the shell process.

Finally, many non-alphanumeric characters that can be included in command lines have special meanings to the shell. They tell it how to set up the environment of the program you wish to run or how to combine processes, etc. This is one of the most powerful and most frustrating features of UNIX. You try to use a non-alphanumeric character as an argument to a command or in a filename and something very strange happens because the shell interprets that character specially.

A general rule for beginners is to avoid using non-alphanumeric characters (except for dot and underscore), for example, as part of a filename. Only use them where they are specifically called for as part of the command syntax. If you have to use them, "escape" them by preceding with a backslash (\) character, or enclose the entire word or string of words containing the special character in single quote (') characters.

I have created a complete list of all characters that have special meanings to the C-shell, which is the most common command-line shell.

Back to Table of Contents

Editing command lines with UNIX shells.

Forget about the # and @ characters described in some introductory UNIX documents. They have no special meaning to the shell on modern UNIX systems.

"Control" characters are used for some terminal control functions. These are made by holding down the CONTROL (or CNTRL or CTRL) key like a shift key and then pressing another key at the same time. Control characters normally do not print on your terminal screen. They are indicated in these notes and most books by the string CTRL- followed by the other key to press, e.g., CTRL-H. Sometimes, control characters are represented in books as a combination of the hat or caret (^) followed by the other key to press, e.g., ^H. In either case, it is really only one character formed by pressing the normal letter key while the CONTROL key is held down.

Use the BACKSPACE key (or CTRL-H) to erase back over mistyped characters one at a time. If the BACKSPACE key does not properly erase characters, try the DELETE key. If neither works, giving this command will reset the session to use the BACKSPACE key on most terminals:

    stty erase '^H'
In this stty command, you actually type a caret (^) character followed by an H character. This is a way for you to represent the actual CTRL-H control key, which will not show on your screen.

Use CTRL-U to erase back over the entire line.

Back to Table of Contents

What to do if you get stuck in a UNIX login session?

First, be patient. Wait a minute or so to make sure you are really stuck, particularly if you have any reason to believe that the command may have to do considerable computing before it is done. Also, system loads and response times vary a lot. 10 am to 5 pm, with a break at lunch, is the peak time for computer use on pangea.

CTRL-S and CTRL-Q are used to control the flow of output to the terminal. CTRL-S tells the system not to send any more data to the screen until a CTRL-Q is pressed. You can use these to control fast scrolling output, although they do not work as well as you would like on network connections, since much data has already been sent to your computer over the network by the time the UNIX system receives the CTRL-S character.

You might press CTRL-S by mistake and your terminal will appear to be frozen. Try pressing CTRL-Q first when your terminal appears stuck to cancel any mistakenly pressed CTRL-S characters (extra CTRL-Q characters do not cause any harm).

To stop a program, send an interrupt signal by pressing the CTRL-C key. This signal allows the program to clean up before it must exit. Some programs "capture" the normal interrupt and do not die. Try CTRL-\ to kill them. This "kill" signal forces the program to exit immediately, with no chance to clean up, so it should only be used when CTRL-C does not work.

In an emergency, close the session window or turn off the terminal and wait a few minutes. The kernel should eventually notice and kill your login process.

If nothing works, and the terminal still appears to be frozen, on pangea the Computer Systems Manager, Phil Farrell, or the School UNIX Support Consultant, Kai Lanz, can find and kill the offending process that is "freezing" the terminal. You can do this yourself when you learn how to use the ps and kill commands.

Back to Table of Contents

What happens when a program executes?

UNIX is very terse. The general rule is that the program simply does its job without any explanatory responses to the screen. If it succeeds, you just get another shell prompt.

Programs that generate output to the terminal, such as a listing of files in your directory, often do not print any explanatory headers. The philosophy of UNIX is that you output only the actual data, so that it can be further processed by other programs that then do not have to worry about header formats. Consequently, for most programs, you have to look at the on-line documentation to learn how to interpret the output.

If the program cannot complete, it usually puts out a cryptic one-line error message. Try looking up the description of the command in the Programmer's Manual. Unfortunately, there is no master index of error messages, like IBM mainframes or VMS.

A command that wants more input (like mail) just puts the cursor to the next line and waits for you to type. Usually, there is no prompt character. When you are done typing input, you signal end of input by pressing CTRL-D by itself on a new line.

UNIX input for command-line programs like mail is like an old-fashioned typewriter: you should press the RETURN key at the end of each line. Typed information will be wrapped around if it is too long for the line, but unlike Macintosh and many PC programs, it will not be neatly broken at a word boundary. Also, the maximum number of characters that will be wrapped around is limited, generally to 255 characters. So remember to use that RETURN key at the end of each input line.

Back to Table of Contents

Some simple commands that you can try.

who am i
whoami
(with or without the spaces) tells who is logged onto this terminal - useful to find out who is logged into a terminal that has been left with no one around.

date
Prints out current date and time to the terminal.

uptime
Shows how long the system has been running (since last start), number of users logged in, and the "load" factor (a measure of congestion).

who
Tells who is logged onto the system, when they logged in, and from which computer on the network.

w
Same idea as who, but gives information about how long the user's process has been idle, how much CPU time he has used so far, and what program he is currently running.

passwd
Use to change your local password (does not affect your SUNet ID kerberos password). You should change your password at least once a year. Do not set your local UNIX password to be the same as your SUNet ID password, for security reasons.

Back to Table of Contents

Fuller discussion of UNIX command syntax.

The general syntax for UNIX commands is:

programname [options] [expressions] [filenames]

Options are usually in the form of a hyphen (-) followed by a single character. Multiple options can usually be specified either by multiple hyphen-character tokens, for example, -l -t, or by one hyphen followed by a string of characters (no embedded blanks), for example, -lt.

Some options require additional information, either a string immediately contiguous with the option, for example, -Pprinter, or a blank after the option followed by the additional information, for example, -f /dev/rmt0h.

Some locally written Earth Sciences programs follow an alternate standard of specifying additional information options with a keyword=value sequence (no spaces around the equals sign). Many common "GNU" (Free Software Foundation) public domain programs also use the keyword=value syntax for options, or use doubled hyphens (--) followed by a full option name, rather than a single letter option character.

In general, order of arguments to the program is important.

Some commands want an expression, for example, a string of characters to look for in a file.

Most commands work with files, and generally allow more than one filename to be specified, to be processed in order.

Remember, the shell breaks up the line you type into the arguments for the command using blanks or tabs as delimiters. To embed a blank in an argument to a program, enclose that argument in quotes.

Back to Table of Contents

Controlling Processes

Continue on to this next section to learn how you can control and monitor the processes that you run from the shell. You can run multiple processes in the background and switch the attention of your login terminal between them. You can also start long running programs, logoff, and then monitor their progress from another login session.

You can also customize your shell environment through startup files and aliases, and save typing by recalling previous commands.


Copyright Phillip Farrell