Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect > Unix > Unix Basics 

Working With the Unix Shell

Included on this page:

Unix is an operating system that controls the computer and your interaction with it. The shell lets you communicate with Unix by providing you with the system prompt. When you see a system prompt, it means the shell is ready for a command. Once you enter a command, the shell interprets it and is responsible for its execution. Up until now, each time you have entered a command, you have been using the shell. The shell also controls your environment and contains a programming language.

Most Unix computers run several shells, allowing you to choose the one you prefer. By default, on many Unix computers you start in the C-shell.


Control Codes

The C-shell offers a number of special commands known as control codes. Control codes define commands specific to the operating system. To issue a control code, hold down <Control> and press the corresponding letter key. The following summarizes some of the most commonly used control codes:
  <Control>d   Signals the end of a file you are entering from the terminal if typed at the beginning of a line or if typed twice elsewhere in a line.
  <Control>c   Cancels a command or interrupts a running program.
  <Control>z   Suspends a process or job but does not terminate it: use fg to restart suspended process or job.
  <Control>u   Clears the command line.

Redirecting Input and Output

Most Unix commands receive input from what is called "standard input" and send output to "standard output". Typically, standard input is from the keyboard, and standard output is displayed to the screen. The routing of standard input and output can be changed by using redirection and piping. Redirection and piping let you connect commands together.

Use a File as Input For a Command

You can use the less-than symbol (<) to use a file as input for a command.

To redirect a file as input for a command, enter:

    command  <  file

Create a File by Redirecting Output

Output of a command can be redirected into a file by using the greater-than symbol (>). You can then use this file as you would any other file.

To redirect command output to a file, enter:

    command  >  file

Note: If you specify a file that already exists, its contents will be overwritten.

You can use output redirect to save information such as a help note into a file.

To save the email help note as a file, enter:

    help email > email.info

This creates the file named email.info and places the text from the email help note into this file, rather than displaying it on the screen.

Add to a File by Redirecting Output

Output can also be appended to the end of an existing file by using two greater-than symbols (>>).

To append command output to an existing file, enter:

    command  >>  file

Chaining Commands

With the pipe symbol ( | ) you can have the output of one command fed (piped) to the input of another command (the | is usually found above the \ key).

To chain two commands, enter:

    command1  |  command2

The w command displays a list of who is on the system. This list is usually very long, and it scrolls by quickly. to view the output of the w command one screen at a time, you can link it to the more program.

To show who is on the system one screen at a time, enter:

    w | more

For additional information about more see View a File.

Managing Your Processes

Each time you issue a command, you start a process (when you logged in, you started your first process).

List and Terminate Your Processes

To display a complete list of your current processes and their corresponding process identification numbers (PIDs), use the ps (process status) command along with the ux options. Enter:

    ps ux

Any of your processes can be stopped with the kill command.

To kill a process, enter:

    kill  pid

Where pid is the number of the process you want to stop. Type ps again to verify that the process has been killed. Some processes require a stronger form of the kill command. When the kill command alone does not terminate a process, try using the kill command with the -9 option.

To kill a process using the -9 option, enter:

    kill -9  pid

Run a Process in the Foreground or the Background

You can run a process in two ways: as a foreground process or as a background process. When you run a process in the foreground, you must wait for the process to complete before you can run another one (all the commands you have issued to this point have been foreground processes). In the background, however, you can run several processes without waiting for them to complete. The background is a very useful place to run a process that takes a long time to finish. When a process needs user input, it will interrupt you regardless of where it is running; therefore, you should avoid putting processes that require user interaction into the background.

To start a process in the background type an & after the command. Enter:

    command  &

This starts the process, from the command you specify, in the background.

To suspend a foreground process and place it in the background, use the following steps:

  1. Press <Control>z
  2. Enter bg

To bring this background process to the foreground again, enter:

    fg

Note: the fg command is useful to remember because occasionally you might type <Control>z accidentally or, if you connect with a modem, noise in the phone lines might magically suspend a process. Just enter fg to restart such a suspended process.