Shells
On every Unix system there are a lot of programs running in parallel, called "processes" in Unix parlance. Whenever you log in, a special process called "shell" is started for you. There are several shell-programs available; unless you change the default, a "tcsh" is started for you on RZG machines.
This shell then reads your input from the keyboard. When you hit the return-key it will examine the line you typed. If the shell knows the command you typed ("intrinsic" commands) it will take the appropriate action. If it doesn't recognize the first word of the line, it assumes that this is the name of an executable program, searches the file system for it and then starts a new process to execute this program. This may be a standard Unix command or a program written by you.
Where does the shell look for executable programs?
The shell, like any other process, has an "environment" attached to it, that is a list of key-value pairs that control the behaviour of the process. If you look at the environment of your shell by typing env you'll see that the value of the environment-variable PATH is a list of paths in the file system. This is where the shell looks for executable programs.
You can use the environment-variables on the command line by prepending the name with a "$":
echo $SHELL
This will print out the value of the SHELL variable (i.e. the name of your current shell).
Among other things these variables are set by system-wide and personal configuration scripts every time a new shell is started. When you have acquainted yourself with Unix and know exactly what you are doing you may want to take a look at ".tcshrc" in your home directory and modify your environment. But be very careful.
Wildcard expansion
The shell does a lot of other things for you. Before it starts a program, it will parse the command line for special characters like the wildcards "?" (matching exactly one character) and "*" (zero, one or more characters). These wildcarded file names are compared against the contents of the current directory and the command line is expanded.
Returning to our example directory,
rm a*
would be expanded to
rm a.very.secret.file a.out
so both files are removed.
Be careful: In some cases this may not work as intended:
cp a* b*
expands to
cp a.very.secret.file a.out
"b*" matches nothing, so "a.out" is overwritten by a copy of "a.very.secret.file".
There are other possibilities for wildcarding, ranges like "[a-c]" for example. Take a look at the man page of your shell.
Command line editing
The "tcsh" allows you to retrieve previous commands by the "up" cursor key. You can use the other cursor keys to edit the line.
The tcsh will also complete partially typed command and file names for you when you hit the TAB key (of course, the first characters you typed must be unambiguous).
Input/Output Redirection and Pipes
For most Unix commands you may specify filenames for the input and output of the command. But you don't need to. In fact, some programs don't even allow that. In such a case a command just reads the input from "stdin" (by default your keyboard) and writes to "stdout" (by default your screen). This feature comes in handy if you want to try out a command. (Use control-D to specify an "end-of-file" when a program is reading stdin from the keyboard).
All shells allow the redirection of stdin and stdout with the "<" and ">" characters. If you want to save the output of the ls command in a file named "lsout", just type
ls > lsout
To mail a file named "secret" to the EMail address "spezi@foo.edu" you might use
mail spezi@foo.edu < secret
All shells also allow "piping" with the "|" symbol. If you type
ls -la | lpr -Po1
both the ls and the lpr programs are started, and the output of the ls command is fed to the lpr command. So your file listing is printed at the printer "o1".
This is one of the most powerful features of Unix. You may also construct "pipe-chains" by using the "|" several times. There are some commands designed to be used as filters in such a context. The most useful is more:
ls -la | more
If the output of the ls command doesn't fit on the screen, more will output only the first screen and then wait for you to press the space bar.
head and tail will show only the top or bottom lines of the stream.
grep will show only lines that contain a given string. So
cat /etc/passwd | grep Hans
or
grep Hans /etc/passwd
will show you all users whose name is 'Hans'. Other useful filter commands include awk, sed, sort and others (just read the man-pages!).
These piping capabilities are also the reason for Unix commands being so terse. You will never see a message like "Command completed successfully" - this would be rather annoying if you use the command in a pipe chain.
Other Useful Shell Stuff
If you need to specify a character on the command line that would otherwise be treated specially by the shell, you can escape it by prepending a "\", or you could enclose a string in single (') or double (") quotes. See the shell's man page for details.
Back-quotes (`) act differently: The string in back-quotes is executed as a command and the output of this command is inserted into the command line.
You may specify several commands on the same line by separating them by a ";".
You may also write "shell scripts": just put a sequence of commands in a file and switch on the "x" permission of the file. Then start it by typing the file's name at the shell prompt. For more sophisticated usage (like control constructs) see the shell's man page.
Process control
If you start a program like
xclock -update 1
a clock will appear on your screen. The shell waits for the process to finish, but "xclock" will run forever so you won't get another shell-prompt to do other work. You can do several things:
- Type control-C (use the control-key like the shift-key). This will terminate the "xclock" and you are back at the shell prompt.
- Type control-Z. That will suspend the process (the clock stops) and the shell issues a prompt. Then type the command bg (for "background"). The clock is restarted, but now "detached" from the shell.
- What you might have done in the first place:
xclock -update 1 &
This starts the program in the background and the shell returns immediately.
So there is now a process running in background, forever... How can you ever stop it?
It's simple in this case, since the xclock process depends on the little window on your screen: it will die if you close the window.
Be careful: This is not the case for arbitrary processes. Just closing a window will not stop them and they may use up a lot of processor cycles. Make sure and use
ps and kill
You can list your processes with the ps command:
ps
PID TTY STAT TIME COMMAND
58 v01 S 0:00 -tcsh
234 pp0 S 0:00 xclock -update 1
235 pp0 R 0:00 ps
(the output may look different on some systems). The first column tells you the "Process ID". Then you can
kill -9 234
the xclock process. Consult the man page if you are wondering about the magic "-9".
Previous: Filesystems Next: Editors
