Shell Basics

The fundamental process for an interactive shell is

  • The shell first displays some sort of "prompt".
  • You type some command, which can either be a "built-in" (i.e., the shell just does what you ask rather than starting a separate child process), or can name a program to be executed as a separate process.
  • Whatever you type, when you hit return, the shell first takes a good long look at your input: it evaluates and substitutes for any metacharacters, it expands any variables, and then it tries to match the command against its internal command set. If it doesn't find anything there, it finally looks around the directories listed in the $PATH variable to see if it can find a program of that name.
  • Finally, once the program is complete (or you put it in the "background"), you get a new prompt.

Environment variables
and their effects

You can get a list of the current shell's environmental variables in various ways: env, printenv; (in Bash, you can also use set, but that also gives additional shell variables that aren't actually in the process's environment.)

Important environment variables:

  • PATH
  • HOME
  • USER
  • SHELL

Local variables

In addition to environmental variables which are passed on to child processes, you can create local variables.

% export x=12    # create an environmental variable
% y=15           # create a local variable
% echo x = $x and y = $y
x = 12 and y = 15
% bash           # now create a new child process
% echo x = $x and y = $y
x = 12 and y =

Foreground/background

One of the great strengths of Unix has been its very clean "job control". You can easily send a process into the background with a simple ampersand:

% ( ls -R / | wc -l ) 2>/dev/null &
% jobs
[1]+  Running                 ( ls -R / | wc -l ) 2> /dev/null &

You can start many jobs:

% cat &
[1] 7200
% cat &
[2] 7201
% cat &
[3] 7203
% cat &
[4] 7209
% fg %1      # now bring job #1 back
cat
some input
some input
[CTRL-D]
% jobs       # okay, we see that job #1 has finished
[2]   Stopped                 cat
[3]-  Stopped                 cat
[4]+  Stopped                 cat
% fg %3      # now bring job #3
cat
more input
more input
[CTRL-D]
% jobs       # and now we see that it's finished also
[2]-  Stopped                 cat
[4]+  Stopped                 cat
%            # and so forth...

Killing a stubborn job
(think LaTeX!)

If you get stuck in a LaTeX session (or emacs, for that matter), you can often use CTRL-Z to get out. Once you do, you can use kill to get rid of it:

pdflatex file1.tex
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
! I can't find file `file1.tex'.
<*> file1.tex
             
(Press Enter to retry, or you will never exit!)
Please type another input file name: 

[1]+  Stopped                 pdflatex file1.tex
[CTRL-Z]
% kill -9 %1