Working With the Unix ShellIncluded on this page:
- Control Codes
- Redirecting Input and Output
- Managing Your Processes
- List and Terminate Your Processes
- Run a Process in the Foreground or the Background
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>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.|
Use a File as Input For a CommandYou 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 OutputOutput 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 OutputOutput 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 CommandsWith 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
To show who is on the system one screen at a time, enter:
w | more
For additional information about more see View a File.
Any of your processes can be stopped with the kill command.
To kill a process, enter:
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
To start a process in the background type an & after the command. Enter:
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:
- Press <Control>z
- Enter bg
To bring this background process to the foreground again, enter:
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.