311, Introduction to Systems Programming
Shell Scripts, Part 2
- Large parts of this presentation are taken from the
Solaris man page for sh.
Special Commands (but not the
Items in square brackets 
Loop control, etc.
||Exit from the enclosing for
or while loop,
If n is
specified, break n
||Resume the next iteration of the enclosing for or while
If n is
at the n-th
||Causes the calling shell or shell script to exit with the
exit status specified by n.
If n is
omitted the exit
status is that of
the last command executed.
An EOF (control-D as keyboard input) will
also cause the shell to exit.
||Causes a function to exit with the return value specified
If n is
return status is that of the last command
||The positional parameters from $n+1 ... are renamed $1 ... .
If n is not
given, it is
assumed to be 1.
Job control, intended for interactive use only.
- This is the description for the Bourne shell, but you should
the C shell or Bash for interactive use. These concepts
commands work for those shells also.
- This description leaves out some details.
- A job is a command
pipeline the user enters at the terminal.
- All jobs exist in one of the following states:
background, or stopped.
- Foreground and background jobs are running, stopped jobs are
not (for now).
- A job in the foreground has read and write access to the
- A job in the background is denied read access and has
conditional write access to the controlling terminal.
- A stopped job is a job that has been placed in a suspended
state, usually as a result of a SIGTSTP signal (terminal stop,
originated by control-Z, for ex.).
- Job numbers (job identifiers) are maintained by the shell;
are not the same as process numbers, but they can be mapped to
numbers by the shell.
- %% refers to the
- %n refers to job
bg [%jobid ...]
- Resumes the
execution of a stopped job in the background. If
is omitted the current job is assumed.
fg [%jobid ...]
- Resumes the execution of a stopped job in the foreground, or
moves an executing background job into the foreground.
is omitted, the current job is assumed.
jobs [%jobid ...]
- Reports jobs that are stopped or executing in the
background. If %jobid
is omitted, all jobs that are stopped or running in the
jobs -x command [arguments]
- Replace any jobid found in command or arguments with the
corresponding process group ID, and then
kill [ -sig ] %job ...
- Sends either the TERM (terminate)
signal or the specified signal to the specified jobs or
Signals are either given by
number or by names (as given in <signal.h>
stripped of the
prefix "SIG" with the exception that SIGCLD is named
signal being sent is TERM (terminate) or HUP (hangup), then
the job or
process will be sent a CONT (continue) signal if it is
argument job can
process id of a process that is not a member of one of the
- List the signal numbers and names.
stop %jobid ...
- Stops the execution of a background job(s).
stop pid ...
- Stop execution of the process number
- Stops the execution of the current shell (but not if it is
wait [%jobid ...]
- As next, but with a job identifier.
wait [ n ]
- Wait for your background process whose process id is
and report its termination status. If
all your shell's currently active background processes are
and the return code will be zero.
More special commands
- No effect; the command
does nothing. A zero exit code is returned.
- . filename
- Read and execute
commands from filename
and return. The search path specified
by PATH is used to find the directory containing
- cd [ argument ]
- Change the
current directory to argument.
argument. The shell parameter CDPATH defines
the search path for the directory containing argument.
Alternative directory names are separated by a colon
(:). The default path is <null>
the current directory). Note: The current directory
by a null path name, which can appear
immediately after the equal sign or between the colon
anywhere else in the path list. If
begins with a / the search path is not used.
directory in the path is searched for argument.
- echo [ arguments ... ]
- The words
are written to the
output, separated by space characters. See echo(1).
- eval [ argument ... ]
- The arguments are read
as input to the shell and the resulting
- exec [ argument ... ]
- The command specified
by the arguments is executed in place of
this shell without creating a new process.
may appear and, if no other arguments are
given, cause the shell input/output to be modified.
- Use in shell scripts
to support command syntax standards (see intro(1)).
This command parses positional parameters and checks
legal options. See getoptcvt(1).
- Print the
current working directory. See pwd(1).
- read name ...
- One line is read from
the standard input and, using the internal
field separator, IFS (normally space or tab), to delimit word
boundaries, the first word is assigned to the
first name, the second word to the second name, and so forth,
leftover words assigned to the last name. Lines can
be continued using \newline. Characters other than
be quoted by preceding them with a backslash.
These backslashes are removed before words are assigned to
and no interpretation is done on the
character that follows the backslash. The
return code is 0, unless an
EOF is encountered.
Shell variables (not a complete description)
- set [ -aefhkntuvx [ argument
- Using + rather
causes these flags to be turned off.
- The current set
of flags may be found in $-
- unset [ name ... ]
- export [ name ... ]
Some more features
- If the shell is invoked through exec(2) and
the first character of argument zero
is -, commands
initially read from /etc/profile
and from $HOME/.profile,
such files exist.
- Because the shell implements both foreground
and background jobs in the same process
group, they all receive the same signals, which can lead
unexpected behavior. It is, therefore,
other job control shells be used, especially in an
- The flags below are interpreted
by the shell on invocation only.
- If the -i flag
present or if the shell input and output are attached
to a terminal, this shell is interactive. In
this case, TERMINATE is ignored (so that kill 0
does not kill an interactive shell) and INTERRUPT is caught
and ignored (so that wait
is interruptible). In all cases,
QUIT is ignored by the shell.
Last revised, 25 Feb. 2013