








                AAnn IInnttrroodduuccttiioonn ttoo tthhee UUNNIIXX SShheellll


                          _S_. _R_. _B_o_u_r_n_e
                         Murray Hill, NJ

               _(_U_p_d_a_t_e_d _f_o_r _4_._3_B_S_D _b_y _M_a_r_k _S_e_i_d_e_n_)
            _(_F_u_r_t_h_e_r _u_p_d_a_t_e_d _b_y _P_e_r_r_y _E_. _M_e_t_z_g_e_r_)_[_+_]


                            _A_B_S_T_R_A_C_T


     The  _s_h_e_l_l  is a command programming language that pro-
     vides an interface to  the  UNIX(R)  operating  system.
     Its features include control-flow primitives, parameter
     passing, variables and string substitution.  Constructs
     such  as  _w_h_i_l_e_,  _i_f _t_h_e_n _e_l_s_e_, _c_a_s_e and _f_o_r are avail-
     able.  Two-way communication is  possible  between  the
     _s_h_e_l_l  and  commands.   String-valued parameters, typi-
     cally file names or flags, may be passed to a  command.
     A  return  code  is set by commands that may be used to
     determine control-flow, and the standard output from  a
     command may be used as shell input.

     The  _s_h_e_l_l can modify the environment in which commands
     run.  Input and output can be redirected to files,  and
     processes  that  communicate  through  `pipes'  can  be
     invoked.  Commands are found by  searching  directories
     in the file system in a sequence that can be defined by
     the user.  Commands can be read either from the  termi-
     nal  or from a file, which allows command procedures to
     be stored for later use.


11..00 IInnttrroodduuccttiioonn

The shell is both a command language and a  programming  language
that  provides  an  interface to the UNIX operating system.  This
memorandum describes, with examples, the UNIX shell.   The  first
section  covers  most  of  the  everyday requirements of terminal
-----------
[+]  This  paper  was  updated in 2010 to reflect most
features of modern POSIX shells, which all follow  the
design of S.R. Bourne's original v7 Unix shell.  Among
these are ash, bash, ksh and others.  Typically one of
these will be installed as /bin/sh on a modern system.
It does not describe  the  behavior  of  the  c  shell
(csh).   If  it's  the c shell (csh) you're interested
in, a good place to begin is William Joy's  paper  "An
Introduction to the C shell" (USD:4).









USD:3-2                         An Introduction to the UNIX Shell


users.  Some familiarity with UNIX is an advantage  when  reading
this  section; see, for example, "UNIX for beginners".1 Section 2
describes those features of the shell primarily intended for  use
within  shell  procedures.  These include the control-flow primi-
tives and string-valued  variables  provided  by  the  shell.   A
knowledge  of a programming language would be a help when reading
this section.  The last section describes the more advanced  fea-
tures of the shell.  References of the form "see _p_i_p_e (2)" are to
a section of the UNIX manual.2

11..11 SSiimmppllee ccoommmmaannddss

Simple commands consist of one or more words separated by blanks.
The  first  word  is  the name of the command to be executed; any
remaining words are passed as  arguments  to  the  command.   For
example,

          who

is  a command that prints the names of users logged in.  The com-
mand

          ls -l

prints a list of files in the current directory.  The argument _-_l
tells  _l_s to print status information, size and the creation date
for each file.

11..22 IInnppuutt oouuttppuutt rreeddiirreeccttiioonn

Most commands produce output on the standard output that is  ini-
tially  connected  to the terminal.  This output may be sent to a
file by writing, for example,

          ls -l >file

The notation _>_f_i_l_e is interpreted by the shell and is not  passed
as an argument to _l_s_.  If _f_i_l_e does not exist then the shell cre-
ates it; otherwise the original contents  of  _f_i_l_e  are  replaced
with  the output from _l_s_.  Output may be appended to a file using
the notation

          ls -l >>file

In this case _f_i_l_e is also created if it does not already exist.

The standard input of a command may be taken from a file  instead
of the terminal by writing, for example,

          wc <file

The  command _w_c reads its standard input (in this case redirected
from _f_i_l_e) and prints the number of characters, words  and  lines
found.  If only the number of lines is required then









An Introduction to the UNIX Shell                         USD:3-3


          wc -l <file

could be used.

11..33 PPiippeelliinneess aanndd ffiilltteerrss

The  standard output of one command may be connected to the stan-
dard input of another by writing the `pipe'  operator,  indicated
by ||, as in,

          ls -l || wc

Two  commands connected in this way constitute a _p_i_p_e_l_i_n_e and the
overall effect is the same as

          ls -l >file; wc <file

except that no _f_i_l_e is used.  Instead the two _p_r_o_c_e_s_s_e_s are  con-
nected  by  a  pipe (see _p_i_p_e(2)) and are run in parallel.  Pipes
are unidirectional and synchronization is achieved by halting  _w_c
when  there  is  nothing  to read and halting _l_s when the pipe is
full.

A _f_i_l_t_e_r is a command that reads its standard  input,  transforms
it  in  some way, and prints the result as output.  One such fil-
ter, _g_r_e_p_, selects from its input those lines that  contain  some
specified string.  For example,

          ls || grep old

prints  those  lines,  if any, of the output from _l_s that contain
the string _o_l_d_.  Another useful filter is _s_o_r_t.  For example,

          who || sort

will print an alphabetically sorted list of logged in users.

A pipeline may consist of more than two commands, for example,

          ls || grep old || wc -l

prints the number of file names in the current directory contain-
ing the string _o_l_d_.

11..44 BBaacckkggrroouunndd ccoommmmaannddss

To execute a command (or pipeline) the shell normally creates the
new _p_r_o_c_e_s_s_e_s and waits for them to finish.  A command may be run
without waiting for it to finish.  For example,

          cc pgm.c &

calls  the  C compiler to compile the file _p_g_m_._c_.  The trailing &&
is an operator that instructs the  shell  not  to  wait  for  the









USD:3-4                         An Introduction to the UNIX Shell


command  to  finish.   To  help  keep track of such a process the
shell reports its job number (see below) and process id following
its  creation.  Such a command is said to be running in the _b_a_c_k_-
_g_r_o_u_n_d.  By contrast, a command executed without the && is said to
be running in the _f_o_r_e_g_r_o_u_n_d.[+]

A  list of currently active processes, including ones not associ-
ated with the current shell, may be obtained using the _p_s(1) com-
mand.

11..55 FFiillee nnaammee ggeenneerraattiioonn

Many  commands  accept arguments which are file names.  For exam-
ple,

          ls -l main.c

prints information relating to the file _m_a_i_n_._c.

The shell provides a mechanism for  generating  a  list  of  file
names that match a pattern.  For example,

          ls -l *.c

generates,  as  arguments  to  _l_s_,  all file names in the current
directory that end in _._c_.  The character * is a pattern that will
match  any string including the null string.  In general _p_a_t_t_e_r_n_s
are specified as follows.

     **       Matches any string of characters including the  null
             string.

     ??       Matches any single character.

     [[......]]   Matches  any one of the characters enclosed.  A pair
             of characters separated by a minus  will  match  any
             character lexically between the pair.

For example,

          [a-z]*

matches  all names in the current directory beginning with one of
the letters _a through _z_.

          /usr/fred/test/?

matches all names in the directory //uussrr//ffrreedd//tteesstt that consist of
-----------
[+]  Even  after execution, one may move commands from
the foreground to the background, or temporarily  sus-
pend  their  execution  (which  is known as _s_t_o_p_p_i_n_g a
command.  This is described in detail in section  3.10
on _J_o_b _C_o_n_t_r_o_l..









An Introduction to the UNIX Shell                         USD:3-5


a  single  character.   If no file name is found that matches the
pattern then the pattern is passed, unchanged, as an argument.

This mechanism is useful both to save typing and to select  names
according  to  some  pattern.  It may also be used to find files.
For example,

          echo /usr/fred/*/core

finds and prints the names of all _c_o_r_e files  in  sub-directories
of  //uussrr//ffrreedd..   (_e_c_h_o is a standard UNIX command that prints its
arguments, separated by blanks.)  This last feature can be expen-
sive, requiring a scan of all sub-directories of //uussrr//ffrreedd..

There  is  one exception to the general rules given for patterns.
The character `..'  at the start of a file name must be explicitly
matched.

          echo *

will  therefore  echo all file names in the current directory not
beginning with `..'.

          echo ..*

will echo all those file names that begin with `..'.  This  avoids
inadvertent  matching  of the names `..' and `....'  which mean `the
current  directory'  and  `the  parent  directory'  respectively.
(Notice  that  _l_s  suppresses  information  for the files `..' and
`....'.)

Finally, the tilde character, `~~', may be used  to  indicate  the
home  directory  of  a  user.  The `~~' at the beginning of a path
name followed by a non-alphabetic character expands to  the  cur-
rent  user's  home  directory.  If the `~~' is followed by a login
name, it expands to the named user's home directory.   For  exam-
ple:

          ls ~
          cd ~egbert/

will  list  the  contents  of  the user's home directory and then
change to the home directory of the user ``egbert''.

11..66 QQuuoottiinngg

Characters that have a special meaning to the shell, such as <<  >>
** ?? || &&,, are called metacharacters.  A complete list of metachar-
acters is given in appendix B.  Any character preceded by a \\  is
_q_u_o_t_e_d and loses its special meaning, if any.  The \\ is elided so
that

          echo \?










USD:3-6                         An Introduction to the UNIX Shell


will echo a single ??,, and

          echo \\

will echo a single \\..  To allow long strings to be continued over
more than one line the sequence \\nneewwlliinnee is ignored.

\\  is  convenient  for quoting single characters.  When more than
one character needs quoting the above  mechanism  is  clumsy  and
error  prone.   A string of characters may be quoted by enclosing
the string between single quotes.  For example,

          echo xx'****'xx

will echo

          xx****xx

The quoted string may not contain a single quote but may  contain
newlines,  which  are  preserved.   This quoting mechanism is the
most simple and is recommended for casual use.

A third quoting mechanism using double quotes is  also  available
that  prevents interpretation of some but not all metacharacters.
Discussion of the details is deferred to section 3.5.

11..77 PPrroommppttiinngg

When the shell is used from a terminal it  will  issue  a  prompt
before  reading  a  command.  By default this prompt is `$$ '.  It
may be changed by saying, for example,

          PS1="yesdear$ "

that sets the prompt to be the string _y_e_s_d_e_a_r_$_.  If a newline  is
typed  and  further input is needed then the shell will issue the
prompt `>> '.  Sometimes this can be caused by mistyping  a  quote
mark.   If it is unexpected then entering the interrupt character
(typically CONTROL-C) will return the shell to read another  com-
mand.  This prompt may be changed by saying, for example,

          PS2=more

Entering  the  interrupt  character may also be used to terminate
most programs running as the current foreground job.

(PS1 and PS2 are _s_h_e_l_l _v_a_r_i_a_b_l_e_s, which will be described in sec-
tion 2.4 below.)

11..88 TThhee sshheellll aanndd llooggiinn

Following  _l_o_g_i_n(1)  the shell is called to read and execute com-
mands typed at the terminal.  If the user's login directory  con-
tains  the  file  ..pprrooffiillee then it is assumed to contain commands









An Introduction to the UNIX Shell                         USD:3-7


and is read by the shell before reading  any  commands  from  the
terminal.

(Most  versions of the shell also specify a file that is read and
executed on start-up whether or  not  the  shell  is  invoked  by
login.   The  ENV shell variable, described in section 2.4 below,
can be used to override the name of this  file.   See  the  shell
manual page for further information.)

11..99 SSuummmmaarryy


     +o    llss
          Print the names of files in the current directory.

     +o    llss >>ffiillee
          Put the output from _l_s into _f_i_l_e_.

     +o    llss || wwcc --ll
          Print the number of files in the current directory.

     +o    llss || ggrreepp oolldd
          Print those file names containing the string _o_l_d_.

     +o    llss || ggrreepp oolldd || wwcc --ll
          Print  the  number  of  files  whose  name contains the
          string _o_l_d_.

     +o    cccc ppggmm..cc &&
          Run _c_c in the background.

22..00 SShheellll ssccrriippttss

The shell may be used to read and execute commands contained in a
file.  For example,

          sh file [ args ...... ]

calls  the  shell  to  read  commands  from _f_i_l_e_.  Such a file is
called a _s_h_e_l_l _s_c_r_i_p_t_.  Arguments may be supplied with  the  call
and  are  referred to in _f_i_l_e using the positional parameters $$11,,
$$22,, ........

For example, if the file _w_g contains

          who || grep $1

then

          sh wg fred

is equivalent to











USD:3-8                         An Introduction to the UNIX Shell


          who || grep fred


UNIX files have three independent  attributes,  _r_e_a_d_,  _w_r_i_t_e  and
_e_x_e_c_u_t_e_.   The  UNIX  command _c_h_m_o_d(1) may be used to make a file
executable.  For example,

          chmod +x wg

will ensure that the file _w_g has execute status.  Following this,
the command

          wg fred

is equivalent to

          sh wg fred

This  allows  shell  scripts and other programs to be used inter-
changeably.  In either case a new process is created to  run  the
command.

The  `##'  character  is used as a comment character by the shell.
All characters following the `#' on a line are ignored.

A typical modern system has several different shells,  some  with
differing  command  syntax,  and it is desirable to specify which
one should be invoked when an executable script is  invoked.   If
the special comment

          #!/_p_a_t_h/_t_o/_i_n_t_e_r_p_r_e_t_e_r

appears  as the first line in a script, it is used to specify the
absolute pathname of the shell (or other interpreter) that should
be  used  to  execute the file.  (Without such a line, //bbiinn//sshh is
assumed.)  It is best if a script explicitly states what shell it
is intended for in this manner.

As  well  as  providing  names for the positional parameters, the
number of positional parameters to a script is available  as  $$##..
The name of the file being executed is available as $$00..

A  special shell parameter $$** is used to substitute for all posi-
tional parameters except $$00..  A typical use of this is to provide
some default arguments, as in,

          nroff -T450 -ms $*

which  simply  prepends  some  arguments  to those already given.
(The variable $$@@ also expands to ``all  positional  parameters'',
but is subtly different when expanded inside quotes.  See section
3.5, below.)











An Introduction to the UNIX Shell                         USD:3-9


22..11 CCoonnttrrooll ffllooww -- ffoorr

A frequent use of shell scripts is to loop through the  arguments
($$11,,  $$22,,  ......)   executing  commands once for each argument.  An
example  of  such  a  script  is  _t_e_l  that  searches  the   file
//uussrr//sshhaarree//tteellnnooss that contains lines of the form

          ......
          fred mh0123
          bert mh0789
          ......

The text of _t_e_l is

          #!/bin/sh

          for i
          do
               grep $i /usr/share/telnos
          done

The command

          tel fred

prints  those  lines in //uussrr//sshhaarree//tteellnnooss that contain the string
_f_r_e_d_.

          tel fred bert

prints those lines containing _f_r_e_d followed by those for _b_e_r_t_.

The ffoorr loop notation is recognized by the shell and has the gen-
eral form

          ffoorr _n_a_m_e iinn _w_1 _w_2 ......
          ddoo _c_o_m_m_a_n_d_-_l_i_s_t
          ddoonnee

A _c_o_m_m_a_n_d_-_l_i_s_t is a sequence of one or more simple commands sepa-
rated or terminated by  a  newline  or  semicolon.   Furthermore,
reserved  words  like ddoo and ddoonnee are only recognized following a
newline or semicolon.  _n_a_m_e is a shell variable that  is  set  to
the  words _w_1 _w_2 ...... in turn each time the _c_o_m_m_a_n_d_-_l_i_s_t following
ddoo is executed.  If iinn _w_1 _w_2 ......  is omitted  then  the  loop  is
executed  once  for  each positional parameter; that is, iinn _$_* is
assumed.

Another example of the use of the ffoorr loop is the _c_r_e_a_t_e  command
whose text is

          for i do >$i; done

The command









USD:3-10                        An Introduction to the UNIX Shell


          create alpha beta

ensures  that two empty files _a_l_p_h_a and _b_e_t_a exist and are empty.
The notation _>_f_i_l_e may be used on its own to create or clear  the
contents of a file.  Notice also that a semicolon (or newline) is
required before ddoonnee..

22..22 CCoonnttrrooll ffllooww -- ccaassee

A multiple way branch is provided for by the ccaassee notation.   For
example,

          case $# in
               1)   cat >>$1 ;;
               2)   cat >>$2 <$1 ;;
               *)   echo 'usage: append [ from ] to' ;;
          esac

is an _a_p_p_e_n_d command.  When called with one argument as

          append file

$$##  is the string _1 and the standard input is copied onto the end
of _f_i_l_e using the _c_a_t command.

          append file1 file2

appends the contents of _f_i_l_e_1 onto _f_i_l_e_2_.  If the number of argu-
ments  supplied  to _a_p_p_e_n_d is other than 1 or 2 then a message is
printed indicating proper usage.

The general form of the ccaassee command is

          ccaassee _w_o_r_d iinn
               _p_a_t_t_e_r_n)) _c_o_m_m_a_n_d_-_l_i_s_t;;;;
               ......
          eessaacc

The shell attempts to match _w_o_r_d with each _p_a_t_t_e_r_n_, in the  order
in which the patterns appear.  If a match is found the associated
_c_o_m_m_a_n_d_-_l_i_s_t is executed and execution of the ccaassee  is  complete.
Since * is the pattern that matches any string it can be used for
the default case.

A word of caution: no check is made to ensure that only one  pat-
tern  matches  the  case argument.  The first match found defines
the set of commands to be executed.  In  the  example  below  the
commands following the second * will never be executed.

          case $# in
               *) ...... ;;
               *) ...... ;;
          esac










An Introduction to the UNIX Shell                        USD:3-11


Another example of the use of the ccaassee construction is to distin-
guish between different forms  of  an  argument.   The  following
example is a fragment of a _c_c command.

          for i
          do case $i in
                  -[ocs])     ...... ;;
                  -*)    echo "unknown flag $i" ;;
                  *.c)   /lib/c0 $i ...... ;;
                  *)     echo "unexpected argument $i" ;;
             esac
          done


To  allow  the  same commands to be associated with more than one
pattern the ccaassee command provides for alternative patterns  sepa-
rated by a ||.  For example,

          case $i in
               -x||-y)    ......
          esac

is equivalent to

          case $i in
               -[xy])    ......
          esac


The usual quoting conventions apply so that

          case $i in
               \?)  ......

will match the character ??..

22..33 HHeerree ddooccuummeennttss

The shell script _t_e_l in section 2.1 uses the file //uussrr//sshhaarree//tteell--
nnooss to supply the data for _g_r_e_p_.  An alternative  is  to  include
this data within the shell script as a _h_e_r_e document, as in,

          for i
          do grep $i <<!
             ......
             fred mh0123
             bert mh0789
             ......
          !
          done

In  this  example the shell takes the lines between <<<<!! and !!  as
the standard input for _g_r_e_p_.  The string !! is arbitrary, the doc-
ument  being  terminated  by  a  line that consists of the string









USD:3-12                        An Introduction to the UNIX Shell


following <<.

Parameters are substituted in the  document  before  it  is  made
available  to  _g_r_e_p as illustrated by the following script called
_e_d_g_.

          ed $3 <<%
          g/$1/s//$2/g
          w
          %

The call

          edg string1 string2 file

is then equivalent to the command

          ed file <<%
          g/string1/s//string2/g
          w
          %

and changes all occurrences of _s_t_r_i_n_g_1 in _f_i_l_e to _s_t_r_i_n_g_2_.   Sub-
stitution can be prevented using \ to quote the special character
$$ as in

          ed $3 <<+
          1,\$s/$1/$2/g
          w
          +

(This version of _e_d_g is equivalent to the first  except  that  _e_d
will  print  a  ??  if there are no occurrences of the string $$11..)
Substitution within a _h_e_r_e document may be prevented entirely  by
quoting the terminating string, for example,

          grep $i <<'end'
          ......
          end

The  document  is  presented  without  modification  to _g_r_e_p_.  If
parameter substitution is not required in a  _h_e_r_e  document  this
latter form is more efficient.

22..44 SShheellll vvaarriiaabblleess[[++]]

The shell provides string-valued variables.  Variable names begin
with  a  letter  and  consist of letters, digits and underscores.
Variables may be given values by writing, for example,


-----------
Also  known  as  _e_n_v_i_r_o_n_m_e_n_t  _v_a_r_i_a_b_l_e_s,,  sseeee _e_n_v_i_r_o_n_-
_m_e_n_t((77))..









An Introduction to the UNIX Shell                        USD:3-13


          user=fred box=m000 acct=mh0000

which assigns values to the variables  uusseerr,,  bbooxx  and  aacccctt..   A
variable may be set to the null string by saying, for example,

          null=

The value of a variable is substituted by preceding its name with
$$; for example,

          echo $user

will echo _f_r_e_d_.

Variables may be used interactively to provide abbreviations  for
frequently used strings.  For example,

          b=/usr/fred/bin
          mv pgm $b

will  move  the file _p_g_m from the current directory to the direc-
tory //uussrr//ffrreedd//bbiinn..  A more general  notation  is  available  for
parameter (or variable) substitution, as in,

          echo ${user}

which is equivalent to

          echo $user

and  is  used  when the parameter name is followed by a letter or
digit.  For example,

          tmp=/tmp/ps
          ps a >${tmp}a

will direct the output of _p_s to the file //ttmmpp//ppssaa,, whereas,

          ps a >$tmpa

would cause the value of the variable ttmmppaa to be substituted.

Except for $$?? the following are set initially by the  shell.   $$??
is set after executing each command.

     $$??      The  exit  status  (return code) of the last command
             executed as a decimal string.  Most commands  return
             a  zero  exit  status if they complete successfully,
             otherwise a non-zero exit status is returned.  Test-
             ing  the  value  of return codes is dealt with later
             under iiff and wwhhiillee commands.

     $$##      The number of positional  parameters  (in  decimal).
             Used,  for  example,  in the _a_p_p_e_n_d command to check









USD:3-14                        An Introduction to the UNIX Shell


             the number of parameters.

     $$$$      The process  number  of  this  shell  (in  decimal).
             Since  process numbers are unique among all existing
             processes, this string is frequently used to  gener-
             ate unique temporary file names.  For example,

                       ps a >/tmp/ps$$
                       ......
                       rm /tmp/ps$$


     $$!!      The  process  number  of the last process run in the
             background (in decimal).

     $$--      The current shell flags, such as --xx and --vv..

Some variables have a special meaning to the shell and should  be
avoided for general use.

     $$MMAAIILL   When  used interactively the shell looks at the file
             specified  by  this  variable  before  it  issues  a
             prompt.   If  the  specified  file has been modified
             since it was last looked at  the  shell  prints  the
             message  _y_o_u _h_a_v_e _m_a_i_l before prompting for the next
             command.  This variable is typically set in the file
             ..pprrooffiillee,,  in the user's login directory.  For exam-
             ple,

                       MAIL=/usr/spool/mail/fred


     $$HHOOMMEE   The default argument for the _c_d command.   The  cur-
             rent  directory  is used to resolve file name refer-
             ences that do not begin with a  //,,  and  is  changed
             using the _c_d command.  For example,

                       cd /usr/fred/bin

             makes the current directory //uussrr//ffrreedd//bbiinn..

                       cat wn

             will  print  on  the  terminal  the  file _w_n in this
             directory.  The  command  _c_d  with  no  argument  is
             equivalent to

                       cd $HOME

             This  variable  is  also  typically  set  in the the
             user's login profile.

     $$PPWWDD    The current working directory. Set by  the  _c_d  ccoomm--
             mmaanndd..









An Introduction to the UNIX Shell                        USD:3-15


     $$PPAATTHH   A  list  of  directories  that contain commands (the
             _s_e_a_r_c_h _p_a_t_h).  Each time a command  is  executed  by
             the  shell  a list of directories is searched for an
             executable file.  If $$PPAATTHH is not set then the  cur-
             rent  directory,  //bbiinn, and //uussrr//bbiinn are searched by
             default.   Otherwise  $$PPAATTHH  consists  of  directory
             names separated by ::..  For example,

                       PATH=::/usr/fred/bin::/bin::/usr/bin

             specifies  that  the  current  directory  (the  null
             string before the first ::), //uussrr//ffrreedd//bbiinn,, //bbiinn  and
             //uussrr//bbiinn  are to be searched in that order.  In this
             way individual users can have  their  own  `private'
             commands  that  are  accessible independently of the
             current directory.  If the command name contains a //
             then  this  directory  search  is not used; a single
             attempt is made to execute the command.

     $$PPSS11    The primary shell prompt string, by default, `$$ '.

     $$PPSS22    The shell prompt when further input  is  needed,  by
             default, `>> '.

     $$IIFFSS    The  set  of characters used by _b_l_a_n_k _i_n_t_e_r_p_r_e_t_a_t_i_o_n
             (see section 3.5).

     $$EENNVV    The shell reads and executes  the  commands  in  the
             file specified by this variable when it is initially
             started.  Unlike the ..pprrooffiillee file,  these  commands
             are executed by all shells, not just the one started
             at login.  (Most versions of  the  shell  specify  a
             filename  that is used if ENV is not explicitly set.
             See the manual page for your shell.)

22..55 TThhee tteesstt ccoommmmaanndd

The _t_e_s_t command, although not part of the shell, is intended for
use by shell programs.  For example,

          test -f file

returns  zero exit status if _f_i_l_e exists and non-zero exit status
otherwise.  In general _t_e_s_t evaluates a predicate and returns the
result as its exit status.  Some of the more frequently used _t_e_s_t
arguments are given here, see _t_e_s_t(1) for a  complete  specifica-
tion.

          test s         true if the argument _s is not the null string
          test -f file   true if _f_i_l_e exists
          test -r file   true if _f_i_l_e is readable
          test -w file   true if _f_i_l_e is writable
          test -d file   true if _f_i_l_e is a directory










USD:3-16                        An Introduction to the UNIX Shell


The _t_e_s_t command is known as `[[' and may be invoked as such.  For
aesthetic reasons, the command ignores a close bracket `]]'  given
at the end of a command so

          [ -f filename ]

and

          test -f filename

are  completely  equivalent.   Typically, the bracket notation is
used when _t_e_s_t is invoked inside shell control constructs.

22..66 CCoonnttrrooll ffllooww -- wwhhiillee

The actions of the ffoorr loop and the ccaassee branch are determined by
data  available  to  the  shell.  A wwhhiillee or uunnttiill loop and an iiff
tthheenn eellssee branch are also provided whose actions  are  determined
by  the  exit  status returned by commands.  A wwhhiillee loop has the
general form

          wwhhiillee _c_o_m_m_a_n_d_-_l_i_s_t_1
          ddoo _c_o_m_m_a_n_d_-_l_i_s_t_2
          ddoonnee


The value tested by the wwhhiillee command is the exit status  of  the
last  simple  command  following wwhhiillee..  Each time round the loop
_c_o_m_m_a_n_d_-_l_i_s_t_1 is executed; if a zero exit status is returned then
_c_o_m_m_a_n_d_-_l_i_s_t_2  is  executed; otherwise, the loop terminates.  For
example,

          while [ $1 ]
          do ......
             shift
          done

is equivalent to

          for i
          do ......
          done

_s_h_i_f_t is a shell command that renames the  positional  parameters
$$22,, $$33,, ...... as $$11,, $$22,, ......  and loses $$11..

Another  kind  of  use  for the wwhhiillee//uunnttiill loop is to wait until
some external event occurs and then run  some  commands.   In  an
uunnttiill loop the termination condition is reversed.  For example,

          until [ -f file ]
          do sleep 300; done
          _c_o_m_m_a_n_d_s










An Introduction to the UNIX Shell                        USD:3-17


will  loop  until _f_i_l_e exists.  Each time round the loop it waits
for 5 minutes before trying again.  (Presumably  another  process
will eventually create the file.)

The  most recent enclosing loop may be exited with the bbrreeaakk com-
mand, or the rest of the body  skipped  and  the  next  iteration
begun with the ccoonnttiinnuuee command.

The commands _t_r_u_e(1) and _f_a_l_s_e(1) return 0 and non-zero exit sta-
tuses respectively. They are sometimes of use  in  control  flow,
e.g.:

          while true
          do date; sleep 5
          done

is an infinite loop that prints the date and time every five sec-
onds.

22..77 CCoonnttrrooll ffllooww -- iiff

Also available is a general conditional branch of the form,

          iiff _c_o_m_m_a_n_d_-_l_i_s_t
          tthheenn _c_o_m_m_a_n_d_-_l_i_s_t
          eellssee _c_o_m_m_a_n_d_-_l_i_s_t
          ffii

that tests the value returned by the last simple command  follow-
ing iiff..

The  iiff  command may be used in conjunction with the _t_e_s_t command
to test for the existence of a file as in

          if [ -f file ]
          then _p_r_o_c_e_s_s _f_i_l_e
          else _d_o _s_o_m_e_t_h_i_n_g _e_l_s_e
          fi


An example of the use of iiff,, ccaassee and ffoorr constructions is  given
in section 2.10.

A multiple test iiff command of the form



















USD:3-18                        An Introduction to the UNIX Shell


          if ......
          then ......
          else if ......
               then ......
               else if ......
                    ......
                    fi
               fi
          fi

may be written using an extension of the iiff notation as,

          if ......
          then ......
          elif ......
          then ......
          elif ......
          ......
          fi


The  following  example is an implementation of the _t_o_u_c_h command
which changes the `last modified' time for a list of files.   The
command may be used in conjunction with _m_a_k_e(1) to force recompi-
lation of a list of files.

          #!/bin/sh

          flag=
          for i
          do case $i in
                  -c)    flag=N ;;
                  *)     if [ -f $i ]
                    then cp $i junk$$; mv junk$$ $i
                    elif [ $flag ]
                    then echo file \'$i\' does not exist
                    else >$i
                    fi
              esac
          done

The --cc flag is used in this command to force subsequent files  to
be  created if they do not already exist.  Otherwise, if the file
does not exist, an error message is printed.  The shell  variable
_f_l_a_g is set to some non-null string if the --cc argument is encoun-
tered.  The commands

          cp ......; mv ......

copy the file and then overwrite it with the copy,  thus  causing
the last modified date to be updated.

The sequence










An Introduction to the UNIX Shell                        USD:3-19


          if command1
          then command2
          fi

may be written

          command1 && command2

Conversely,

          command1 |||| command2

executes _c_o_m_m_a_n_d_2 only if _c_o_m_m_a_n_d_1 fails.  In each case the value
returned is that of the last simple command executed.

Placing a `!!' in front of a pipeline  inverts  its  exit  status,
almost in the manner of the C operator of the same name.  Thus:

          if ! [ -d $1 ]
          then
               echo $1 is not a directory
          fi

will print a message only if $1 is not a directory.

22..88 CCoommmmaanndd ggrroouuppiinngg

Commands may be grouped in two ways,

          {{ _c_o_m_m_a_n_d_-_l_i_s_t ;; }}

and

          (( _c_o_m_m_a_n_d_-_l_i_s_t ))


In  the  first  _c_o_m_m_a_n_d_-_l_i_s_t is simply executed.  The second form
executes _c_o_m_m_a_n_d_-_l_i_s_t as a separate process.  For example,

          (cd x; rm junk )

executes _r_m _j_u_n_k in the directory xx without changing the  current
directory of the invoking shell.

The commands

          cd x; rm junk

have  the  same effect but leave the invoking shell in the direc-
tory xx..













USD:3-20                        An Introduction to the UNIX Shell


22..99 SShheellll FFuunnccttiioonnss

A function may be defined by the syntax

          _f_u_n_c_n_a_m_e() {{ _c_o_m_m_a_n_d_-_l_i_s_t ;; }}

Functions are invoked within a script as though they  were  sepa-
rate  commands  of  the  same name.  While they are executed, the
positional parameters $$11,, $$22,, ......  are  temporarily  set  to  the
arguments passed to the function. For example:

          count() {
               echo $2 : $#
          }

          count a b c

would print `b : 3'.

22..1100 DDeebbuuggggiinngg sshheellll ssccrriippttss

The  shell provides two tracing mechanisms to help when debugging
shell scripts.  The first is invoked within the script as

          set -v

(vv for verbose) and causes lines of the script to be  printed  as
they  are  read.  It is useful to help isolate syntax errors.  It
may be invoked without modifying the script by saying

          sh -v _s_c_r_i_p_t ......

where _s_c_r_i_p_t is the name of the shell script.  This flag  may  be
used  in conjunction with the --nn flag which prevents execution of
subsequent commands.  (Note that saying _s_e_t _-_n at a terminal will
render the terminal useless until an end-of-file is typed.)

The command

          set -x

will  produce  an execution trace.  Following parameter substitu-
tion each command is printed as it is executed.   (Try  these  at
the  terminal  to  see what effect they have.)  Both flags may be
turned off by saying

          set -

and the current setting of the shell flags is available as $$--.

22..1111 TThhee mmaann ccoommmmaanndd

The following is a simple  implementation  of  the  _m_a_n  command,
which  is  used  to  display  sections of the UNIX manual on your









An Introduction to the UNIX Shell                        USD:3-21


terminal.  It is called, for example, as

               man sh
               man -t ed
               man 2 fork

In the first the manual section for _s_h is displayed..   Since  no
section is specified, section 1 is used.  The second example will
typeset (--tt option) the manual section for _e_d_.  The  last  prints
the _f_o_r_k manual page from section 2, which covers system calls.



          #!/bin/sh

          cd /usr/share/man

          # "#" is the comment character
          # default is nroff ($N), section 1 ($s)
          N=n s=1

          for i
          do case $i in
                  [1-9]*)     s=$i ;;
                  -t)    N=t ;;
                  -n)    N=n ;;
                  -*)    echo unknown flag \'$i\' ;;
                  *)     if [ -f man$s/$i.$s ]
                    then
                         ${N}roff -man man$s/$i.$s
                    else # look through all manual sections
                         found=no
                         for j in 1 2 3 4 5 6 7 8 9
                         do
                            if [ -f man$j/$i.$j ]
                            then
                                 man $j $i
                                 found=yes
                                 break
                            fi
                         done
                         case $found in
                              no) echo \'$i: manual page not found\'
                         esac
                    fi
             esac
          done

             FFiigguurree 11.. AA vveerrssiioonn ooff tthhee mmaann ccoommmmaanndd

33..00 KKeeyywwoorrdd ppaarraammeetteerrss

Shell variables may be given values by assignment or when a shell
script is  invoked.   An  argument  to  a  command  of  the  form









USD:3-22                        An Introduction to the UNIX Shell


_n_a_m_e_=_v_a_l_u_e  that  precedes  the  command  name causes _v_a_l_u_e to be
assigned to _n_a_m_e before execution of  the  command  begins.   The
value  of  _n_a_m_e in the invoking shell is not affected.  For exam-
ple,

          user=fred command

will execute _c_o_m_m_a_n_d with uusseerr set to _f_r_e_d.

The _s_e_t command may also be used  to  set  positional  parameters
from within a script.  For example,

          set -- *

will  set  $$11 to the first file name in the current directory, $$22
to the next, and so  on.   Note  that  the  first  argument,  --,
ensures  correct treatment when the first file name begins with a
-.


33..11 PPaarraammeetteerr ttrraannssmmiissssiioonn

When a command is executed both positional parameters  and  shell
variables  may  be  set  on  invocation.  Variables are also made
available implicitly to a command by specifying in  advance  that
such  parameters are to be exported from the invoking shell.  For
example,

          export user box=red

marks the variables uusseerr and  bbooxx  for  export  (setting  bbooxx  to
``red''  in  the  process).  When a command is invoked copies are
made of all exportable variables (also known as _e_n_v_i_r_o_n_m_e_n_t _v_a_r_i_-
_a_b_l_e_s)  for use within the invoked program.  Modification of such
variables within an invoked command does not affect the values in
the  invoking  shell.   It is generally true of a shell script or
other program that it cannot modify the state of its caller with-
out explicit actions on the part of the caller.

Names  whose value is intended to remain constant may be declared
_r_e_a_d_o_n_l_y_.  The form of this command is the same as  that  of  the
_e_x_p_o_r_t command,

          readonly name[=value] ......

Subsequent attempts to set readonly variables are illegal.

33..22 PPaarraammeetteerr ssuubbssttiittuuttiioonn

If  a  shell parameter is not set then the null string is substi-
tuted for it.  For example, if the variable dd is not set

          echo $d










An Introduction to the UNIX Shell                        USD:3-23


or

          echo ${d}

will echo nothing.  A default string may be given as in

          echo ${d:-..}

which will echo the value of the variable dd if it is set and  not
null  and  `..'  otherwise.  The default string is evaluated using
the usual quoting conventions so that

          echo ${d:-'*'}

will echo ** if the variable dd is not set or null.  Similarly

          echo ${d:-$1}

will echo the value of dd if it is set and not null and the  value
(if any) of $$11 otherwise.

The  notation  ${d:+..} performs the inverse operation. It substi-
tutes `..' if dd is set or  not  null,  and  otherwise  substitutes
null.

A variable may be assigned a default value using the notation

          echo ${d:=..}

which substitutes the same string as

          echo ${d:-..}

and  if  dd were not previously set or null then it will be set to
the string `..'.

If there is no sensible default then the notation

          echo ${d:?_m_e_s_s_a_g_e}

will echo the value of the variable dd if it is set and not  null,
otherwise  _m_e_s_s_a_g_e  is  printed by the shell and execution of the
shell script is abandoned.  If _m_e_s_s_a_g_e is absent then a  standard
message  is printed.  A shell script that requires some variables
to be set might start as follows:

          : ${user:?} ${acct:?} ${bin:?}
          ......

Colon (::) is a command that is built in to  the  shell  and  does
nothing  once  its  arguments have been evaluated.  If any of the
variables uusseerr,, aacccctt or bbiinn are not set then the shell will aban-
don execution of the script.










USD:3-24                        An Introduction to the UNIX Shell


33..33 CCoommmmaanndd ssuubbssttiittuuttiioonn

The  standard output from a command can be substituted in a simi-
lar way to parameters.  The command _p_w_d prints  on  its  standard
output  the  name  of the current directory.  For example, if the
current directory is //uussrr//ffrreedd//bbiinn then the commands

          d=$(pwd)

(or the older notation d=`pwd`) is equivalent to

          d=/usr/fred/bin


The entire string inside $(......) (or between grave accents  `......`)
is  taken  as the command to be executed and is replaced with the
output from the command.  (The difference between the $(......)  and
`......`  notations is that the former may be nested, while the lat-
ter cannot be.)

The command is  written  using  the  usual  quoting  conventions,
except  that  inside  `......`  a  `` must be escaped using a \\.  For
example,

          ls $(echo "$HOME")

is equivalent to

          ls $HOME

Command substitution occurs in all contexts where parameter  sub-
stitution  occurs (including _h_e_r_e documents) and the treatment of
the resulting text is the same in  both  cases.   This  mechanism
allows  string  processing  commands  to  be  used  within  shell
scripts.  An example of such a command is _b_a_s_e_n_a_m_e which  removes
a specified suffix from a string.  For example,

          basename main..c ..c

will  print  the string _m_a_i_n_.  Its use is illustrated by the fol-
lowing fragment from a _c_c command.

          case $A in
               ......
               *..c) B=$(basename $A ..c)
               ......
          esac

that sets BB to the part of $$AA with the suffix ..cc stripped.

Here are some composite examples.

     +o    ffoorr ii iinn ``llss --tt``;; ddoo ......
          The variable ii is set to the names  of  files  in  time









An Introduction to the UNIX Shell                        USD:3-25


          order, most recent first.

     +o    sseett ---- ``ddaattee``;; eecchhoo $$66 $$22 $$33,, $$44
          will print, e.g., _1_9_7_7 _N_o_v _1_, _2_3_:_5_9_:_5_9

33..44 AArriitthhmmeettiicc EExxppaannssiioonn

Within  a  $((......))  construct, integer arithmetic operations are
evaluated.  (The $ in front of variable names is optional  within
$((......)).  For example:

          x=5; y=1
          echo $(($x+3*2))
          echo $((y+=x))
          echo $y

will  print  `11',  then  `6',  then `6' again.  Most of the con-
structs permitted in C arithmetic operations are permitted though
some  (like  `++') are not universally supported -- see the shell
manual page for details.

33..55 EEvvaalluuaattiioonn aanndd qquuoottiinngg

The shell is a macro processor that provides parameter  substitu-
tion, command substitution and file name generation for the argu-
ments to commands.  This section discusses  the  order  in  which
these  evaluations  occur  and the effects of the various quoting
mechanisms.

Commands are parsed initially according to the grammar  given  in
appendix A.  Before a command is executed the following substitu-
tions occur.

     +o    parameter substitution, e.g. $$uusseerr

     +o    command substitution, e.g. $$((ppwwdd)) or ``ppwwdd``

     +o    arithmetic expansion, e.g. $$(((($$ccoouunntt++11))))

          Only one evaluation occurs so that if, for example, the
          value of the variable XX is the string _$_y then

                    echo $X

          will echo _$_y_.

     +o    blank interpretation

          Following the above substitutions the resulting charac-
          ters are broken into non-blank words (_b_l_a_n_k _i_n_t_e_r_p_r_e_t_a_-
          _t_i_o_n).  For this purpose `blanks' are the characters of
          the string $$IIFFSS.  By default, this string  consists  of
          blank,  tab  and  newline.   The  null  string  is  not
          regarded as a word unless it is quoted.  For example,









USD:3-26                        An Introduction to the UNIX Shell


                    echo ''

          will pass on the null string as the first  argument  to
          _e_c_h_o, whereas

                    echo $null

          will  call  _e_c_h_o with no arguments if the variable nnuullll
          is not set or set to the null string.

     +o    file name generation

          Each word is then scanned for the file pattern  charac-
          ters  **,,  ?? and [[......]]  and an alphabetical list of file
          names is generated to replace the word.  Each such file
          name is a separate argument.

The  evaluations  just  described also occur in the list of words
associated with a ffoorr loop.  Only substitution occurs in the _w_o_r_d
used for a ccaassee branch.

As  well  as the quoting mechanisms described earlier using \\ and
''......'' a third quoting mechanism is provided using double  quotes.
Within  double  quotes  parameter and command substitution occurs
but file name generation and the interpretation  of  blanks  does
not.  The following characters have a special meaning within dou-
ble quotes and may be quoted using \\..

          $$    parameter substitution
          $$(())  command substitution
          ``    command substitution
          ""    ends the quoted string
          \\    quotes the special characters $$ `` "" \\

For example,

          echo "$x"

will pass the value of the variable xx as  a  single  argument  to
_e_c_h_o_.  Similarly,

          echo "$*"

will  pass  the positional parameters as a single argument and is
equivalent to

          echo "$1 $2 ......"

The notation $$@@ is the same as $$** except when it is quoted.

          echo "$@"

will pass the positional parameters, unevaluated, to _e_c_h_o and  is
equivalent to









An Introduction to the UNIX Shell                        USD:3-27


          echo "$1" "$2" ......


The  following table gives, for each quoting mechanism, the shell
metacharacters that are evaluated.

                            _m_e_t_a_c_h_a_r_a_c_t_e_r
                    \    $    *    `    "    '
               '    n    n    n    n    n    t
               `    y    n    n    t    n    n
               "    y    y    n    y    t    n

                    t    terminator
                    y    interpreted
                    n    not interpreted

                     FFiigguurree 22.. QQuuoottiinngg mmeecchhaanniissmmss


In cases where more than one evaluation of a string  is  required
the built-in command _e_v_a_l may be used.  For example, if the vari-
able XX has the value _$_y, and if yy has the value _p_q_r then

          eval echo $X

will echo the string _p_q_r_.

In general the _e_v_a_l command evaluates its arguments  (as  do  all
commands) and treats the result as input to the shell.  The input
is read and the resulting command(s) executed.  For example,

          wg='eval who||grep'
          $wg fred

is equivalent to

          who||grep fred

In this example, _e_v_a_l is required since there is  no  interpreta-
tion of metacharacters, such as ||, following substitution.

33..66 EErrrroorr hhaannddlliinngg

The treatment of errors detected by the shell depends on the type
of error and on whether the shell is  being  used  interactively.
An  interactive shell is one whose input and output are connected
to a terminal.  A shell invoked with the --ii flag is also interac-
tive.

Execution  of  a  command  (see also 3.7) may fail for any of the
following reasons.

+o    Input output redirection may fail.  For example, if  a  file
     does not exist or cannot be created.









USD:3-28                        An Introduction to the UNIX Shell


+o    The command itself does not exist or cannot be executed.

+o    The  command terminates abnormally, for example, with a "bus
     error" or "memory fault".  See Figure 2 below for a complete
     list of UNIX signals.

+o    The  command terminates normally but returns a non-zero exit
     status.

In all of these cases the shell will go on to  execute  the  next
command.   Except  for  the  last  case  an error message will be
printed by the shell.  All remaining errors cause  the  shell  to
exit  from  a  script.   An interactive shell will return to read
another command from the terminal.  Such errors include the  fol-
lowing.

+o    Syntax errors.  e.g., if ...... then ...... done

+o    A signal such as interrupt.  The shell waits for the current
     command, if any, to finish execution and then  either  exits
     or returns to the terminal.

+o    Failure of any of the built-in commands such as _c_d_.

The  shell  flag --ee causes the shell to terminate if any error is
detected.

     1    hangup
     2    interrupt
     3*   quit
     4*   illegal instruction
     5*   trace trap
     6*   IOT instruction
     7*   EMT instruction
     8*   floating point exception
     9    kill (cannot be caught or ignored)
     10*  bus error
     11*  segmentation violation
     12*  bad argument to system call
     13   write on a pipe with no one to read it
     14   alarm clock
     15   software termination (from _k_i_l_l (1))


                    FFiigguurree 33.. UUNNIIXX ssiiggnnaallss[[++]]
Those  signals marked with an asterisk produce a core dump if not
caught.  However, the shell itself ignores quit which is the only
external  signal that can cause a dump.  The signals in this list
of potential interest to shell programs are 1, 2, 3, 14 and 15.


-----------
[+] Additional signals have been added in modern Unix.
See _s_i_g_v_e_c(2) or _s_i_g_n_a_l(3) for an up-to-date list.









An Introduction to the UNIX Shell                        USD:3-29


33..77 FFaauulltt hhaannddlliinngg

shell scripts normally terminate when an  interrupt  is  received
from  the terminal.  The _t_r_a_p command is used if some cleaning up
is required, such as removing temporary files.  For example,

          trap 'rm /tmp/ps$$; exit' 2

sets a trap for signal 2 (terminal interrupt), and if this signal
is received will execute the commands

          rm /tmp/ps$$; exit

_e_x_i_t  is  another built-in command that terminates execution of a
shell script.  The _e_x_i_t is required; otherwise,  after  the  trap
has been taken, the shell will resume executing the script at the
place where it was interrupted.

UNIX signals can be handled in one of three ways.   They  can  be
ignored,  in  which case the signal is never sent to the process.
They can be caught, in which case the process  must  decide  what
action  to take when the signal is received.  Lastly, they can be
left to cause termination of the process  without  it  having  to
take  any  further action.  If a signal is being ignored on entry
to the shell script, for example, by invoking  it  in  the  back-
ground (see 3.7) then _t_r_a_p commands (and the signal) are ignored.

The use of _t_r_a_p is illustrated by this modified  version  of  the
_t_o_u_c_h  command  (Figure  4).  The cleanup action is to remove the
file jjuunnkk$$$$.

          #!/bin/sh

          flag=
          trap 'rm -f junk$$; exit' 1 2 3 15
          for i
          do case $i in
                  -c)    flag=N ;;
                  *)     if test -f $i
                    then cp $i junk$$; mv junk$$ $i
                    elif test $flag
                    then echo file \'$i\' does not exist
                    else >$i
                    fi
             esac
          done


                   FFiigguurree 44.. TThhee ttoouucchh ccoommmmaanndd

The _t_r_a_p command appears before the  creation  of  the  temporary
file; otherwise it would be possible for the process to die with-
out removing the file.










USD:3-30                        An Introduction to the UNIX Shell


Since there is no signal 0 in UNIX it is used  by  the  shell  to
indicate  the  commands  to  be  executed  on exit from the shell
script.

A script may, itself, elect to ignore signals by  specifying  the
null  string  as the argument to trap.  The following fragment is
taken from the _n_o_h_u_p command.

          trap '' 1 2 3 15

which causes _h_a_n_g_u_p_, _i_n_t_e_r_r_u_p_t_, _q_u_i_t and _k_i_l_l to be ignored  both
by the script and by invoked commands.

Traps may be reset by saying

          trap 2 3

which  resets the traps for signals 2 and 3 to their default val-
ues.  A list of the current values of traps may  be  obtained  by
writing

          trap


The script _s_c_a_n (Figure 5) is an example of the use of _t_r_a_p where
there is no exit in the trap command.  _s_c_a_n takes each  directory
in  the  current  directory, prompts with its name, and then exe-
cutes commands typed at the terminal until an end of file  or  an
interrupt  is  received.   Interrupts are ignored while executing
the requested commands but cause termination when _s_c_a_n is waiting
for input.

          d=`pwd`
          for i in *
          do if test -d $d/$i
             then cd $d/$i
                  while echo "$i:"
                        trap exit 2
                        read x
                  do trap : 2; eval $x; done
             fi
          done


                   FFiigguurree 55.. TThhee ssccaann ccoommmmaanndd

_r_e_a_d  _x  is a built-in command that reads one line from the stan-
dard input and places the result in the variable xx..  It returns a
non-zero  exit  status  if  either  an  end-of-file is read or an
interrupt is received.













An Introduction to the UNIX Shell                        USD:3-31


33..88 CCoommmmaanndd eexxeeccuuttiioonn

To run a command (other than a built-in) the shell first  creates
a new process using the system call _f_o_r_k_.  The execution environ-
ment for the command includes input, output  and  the  states  of
signals,  and is established in the child process before the com-
mand is executed.  The built-in command _e_x_e_c is used in the  rare
cases when no fork is required and simply replaces the shell with
a new command.  For example, a simple version of the  _n_o_h_u_p  com-
mand looks like

          trap \'\' 1 2 3 15
          exec $*

The _t_r_a_p turns off the signals specified so that they are ignored
by subsequently created commands and _e_x_e_c replaces the  shell  by
the command specified.

Most   forms  of  input  output  redirection  have  already  been
described.  In the following _w_o_r_d is only  subject  to  parameter
and  command  substitution.   No  file  name  generation or blank
interpretation takes place so that, for example,

               echo ...... >*.c

will write its output into a file whose name is **..cc..  Input  out-
put  specifications are evaluated left to right as they appear in
the command.

> _w_o_r_d      The standard output (file descriptor 1)  is  sent  to
            the file _w_o_r_d which is created if it does not already
            exist.

>> _w_o_r_d     The standard output is sent to  file  _w_o_r_d_.   If  the
            file  exists  then  output is appended (by seeking to
            the end); otherwise the file is created.

< _w_o_r_d      The standard input (file descriptor 0) is taken  from
            the file _w_o_r_d_.

<< _w_o_r_d     The  standard  input is taken from the lines of shell
            input that follow up to but not including a line con-
            sisting  only  of  _w_o_r_d_.   If  _w_o_r_d is quoted then no
            interpretation of the document occurs.   If  _w_o_r_d  is
            not  quoted  then  parameter and command substitution
            occur and \\ is used to quote the characters \\ $$ `` and
            the  first  character  of  _w_o_r_d_.   In the latter case
            \\nneewwlliinnee is ignored (c.f. quoted strings).

>& _d_i_g_i_t    The file descriptor _d_i_g_i_t  is  duplicated  using  the
            system  call  _d_u_p  (2)  and the result is used as the
            standard output.











USD:3-32                        An Introduction to the UNIX Shell


<& _d_i_g_i_t    The standard input is duplicated from file descriptor
            _d_i_g_i_t_.

<&-         The standard input is closed.

>&-         The standard output is closed.

Any  of  the  above  may be preceded by a digit in which case the
file descriptor created is that specified by the digit instead of
the default 0 or 1.  For example,

          ...... 2>file

runs  a  command with message output (file descriptor 2) directed
to _f_i_l_e_.

          ...... 2>&1

runs a command  with  its  standard  output  and  message  output
merged.   (Strictly  speaking  file  descriptor  2  is created by
duplicating file descriptor 1 but the effect is usually to  merge
the two streams.)

33..99 IInnvvookkiinngg tthhee sshheellll

The  following  flags  are  interpreted  by  the shell when it is
invoked.  If the first character of argument  zero  is  a  minus,
then commands are read from the file ..pprrooffiillee..

--cc _s_t_r_i_n_g
     If  the  --cc  flag  is  present  then  commands are read from
     _s_t_r_i_n_g_.

--ss   If the --ss flag is present or if  no  arguments  remain  then
     commands  are read from the standard input.  Shell output is
     written to file descriptor 2.

--ii   If the --ii flag is present or if the shell input  and  output
     are attached to a terminal (as told by _g_t_t_y) then this shell
     is _i_n_t_e_r_a_c_t_i_v_e_.  In this case TERMINATE is ignored (so  that
     kkiillll  00 does not kill an interactive shell) and INTERRUPT is
     caught and ignored (so that wwaaiitt is interruptable).  In  all
     cases QUIT is ignored by the shell.

33..1100 JJoobb CCoonnttrrooll

When  a  command  or pipeline (also known as a _j_o_b) is running in
the foreground, entering the stop character (typically  CONTROL-Z
but  user  settable  with the _s_t_t_y(1) command) will usually cause
the job to stop.

The jobs associated with the  current  shell  may  be  listed  by
entering  the  _j_o_b_s command.  Each job has an associated _j_o_b _n_u_m_-
_b_e_r.  Jobs that are stopped may be continued by entering









An Introduction to the UNIX Shell                        USD:3-33


          bg %_j_o_b_n_u_m_b_e_r

and jobs may be moved to the foreground by entering

          fg %_j_o_b_n_u_m_b_e_r

If there is a sole job with  a  particular  name  (say  only  one
instance  of _c_c running), _f_g and _b_g may also use name of the com-
mand in place of the number, as in:

          bg %cc

If no `%%' clause is entered, most recently stopped job (indicated
with  a `+' by the _j_o_b_s command) will be assumed.  See the manual
page for the shell for more details.

33..1111 AAlliiaasseess

The _a_l_i_a_s command creates a so-called shell alias,  which  is  an
abbreviation  that macro-expands at run time into some other com-
mand.  For example:

          alias ls="ls -F"

would cause the command sequence llss --FF to  be  executed  whenever
the user types llss into the shell.  Note that if the user types llss
--aa, the shell will in fact execute llss --FF --aa.  The  command  aalliiaass
on  its own prints out all current aliases.  The _u_n_a_l_i_a_s command,
as in:

          unalias ls

will remove an existing alias.  Aliases can  shadow  pre-existing
commands,  as  in  the example above.  They are typically used to
override the interactive behavior of commands in small ways,  for
example to always invoke some program with a favorite option, and
are almost never found in scripts.

33..1122 CCoommmmaanndd LLiinnee EEddiittiinngg aanndd RReeccaallll

When working interactively with the shell, it is often tedious to
retype  previously  entered commands, especially if they are com-
plicated.  The shell therefore  maintains  a  so-called  _h_i_s_t_o_r_y,
which is stored in the file specified by the HHIISSTTFFIILLEE environment
variable if it is set.  Users may view, edit, and re-enter previ-
ous  lines  of  input using a small subset of the commands of the
_v_i(1)  or  _e_m_a_c_s(1)[+]  editors.   Emacs  style  editing  may  be
selected by entering


-----------
Technically, vi command  editing  is  standardized  by
POSIX  while emacs is not.  However, all modern shells
support both styles.









USD:3-34                        An Introduction to the UNIX Shell


          set -o emacs

and vi style editing may be selected with

          set -o vi

The  details  of  how  command  line editing works are beyond the
scope of this document.  See the shell manual page for details.

AAcckknnoowwlleeddggeemmeennttss

The design of the shell is based in part  on  the  original  UNIX
shell3  and  the PWB/UNIX shell,4 some features having been taken
from both.  Similarities also exist with the command interpreters
of the Cambridge Multiple Access System5 and of CTSS.6

I  would  like  to  thank Dennis Ritchie and John Mashey for many
discussions during the design of the shell.  I am  also  grateful
to  the  members  of the Computing Science Research Center and to
Joe Maranzano for their comments on drafts of this document.



RReeffeerreenncceess

1.

2.

3.

4.

5.

6.



























An Introduction to the UNIX Shell                        USD:3-35


AAppppeennddiixx AA -- GGrraammmmaarr

Note: This grammar needs updating, it is obsolete.


_i_t_e_m_:          _w_o_r_d
          _i_n_p_u_t_-_o_u_t_p_u_t
          _n_a_m_e _= _v_a_l_u_e

_s_i_m_p_l_e_-_c_o_m_m_a_n_d_: _i_t_e_m
          _s_i_m_p_l_e_-_c_o_m_m_a_n_d _i_t_e_m

_c_o_m_m_a_n_d_:  _s_i_m_p_l_e_-_c_o_m_m_a_n_d
          (( _c_o_m_m_a_n_d_-_l_i_s_t ))
          {{ _c_o_m_m_a_n_d_-_l_i_s_t }}
          ffoorr _n_a_m_e ddoo _c_o_m_m_a_n_d_-_l_i_s_t ddoonnee
          ffoorr _n_a_m_e iinn _w_o_r_d ...... ddoo _c_o_m_m_a_n_d_-_l_i_s_t ddoonnee
          wwhhiillee _c_o_m_m_a_n_d_-_l_i_s_t ddoo _c_o_m_m_a_n_d_-_l_i_s_t ddoonnee
          uunnttiill _c_o_m_m_a_n_d_-_l_i_s_t ddoo _c_o_m_m_a_n_d_-_l_i_s_t ddoonnee
          ccaassee _w_o_r_d iinn _c_a_s_e_-_p_a_r_t ...... eessaacc
          iiff _c_o_m_m_a_n_d_-_l_i_s_t tthheenn _c_o_m_m_a_n_d_-_l_i_s_t _e_l_s_e_-_p_a_r_t ffii

_p_i_p_e_l_i_n_e_: _c_o_m_m_a_n_d
          _p_i_p_e_l_i_n_e || _c_o_m_m_a_n_d

_a_n_d_o_r_:    _p_i_p_e_l_i_n_e
          _a_n_d_o_r &&&& _p_i_p_e_l_i_n_e
          _a_n_d_o_r |||| _p_i_p_e_l_i_n_e

_c_o_m_m_a_n_d_-_l_i_s_t_:  _a_n_d_o_r
          _c_o_m_m_a_n_d_-_l_i_s_t ;;
          _c_o_m_m_a_n_d_-_l_i_s_t &&
          _c_o_m_m_a_n_d_-_l_i_s_t ;; _a_n_d_o_r
          _c_o_m_m_a_n_d_-_l_i_s_t && _a_n_d_o_r

_i_n_p_u_t_-_o_u_t_p_u_t_:  >> _f_i_l_e
          << _f_i_l_e
          >>>> _w_o_r_d
          <<<< _w_o_r_d

_f_i_l_e_:          _w_o_r_d
          && _d_i_g_i_t
          && _-

_c_a_s_e_-_p_a_r_t_:     _p_a_t_t_e_r_n )) _c_o_m_m_a_n_d_-_l_i_s_t ;;;;

_p_a_t_t_e_r_n_:  _w_o_r_d
          _p_a_t_t_e_r_n || _w_o_r_d

_e_l_s_e_-_p_a_r_t_:     eelliiff _c_o_m_m_a_n_d_-_l_i_s_t tthheenn _c_o_m_m_a_n_d_-_l_i_s_t _e_l_s_e_-_p_a_r_t
          eellssee _c_o_m_m_a_n_d_-_l_i_s_t
          _e_m_p_t_y

_e_m_p_t_y_:









USD:3-36                        An Introduction to the UNIX Shell


_w_o_r_d_:          a sequence of non-blank characters

_n_a_m_e_:          a sequence of letters, digits or underscores starting with a letter

_d_i_g_i_t_:         00 11 22 33 44 55 66 77 88 99


























































An Introduction to the UNIX Shell                        USD:3-37


AAppppeennddiixx BB -- MMeettaa--cchhaarraacctteerrss aanndd RReesseerrvveedd WWoorrddss

a) syntactic

     ||     pipe symbol

     &&&&    `andf' symbol

     ||||    `orf' symbol

     ;;       command separator

     ;;;;      case delimiter

     &&       background commands

     (( ))     command grouping

     <<       input redirection

     <<<<      input from a here document

     >>       output creation

     >>>>      output append



b) patterns

     **       match any character(s) including none

     ??       match any single character

     [[......]]   match any of the enclosed characters



c) substitution

     $${{......}}  substitute shell variable

     $$((......))  substitute command output

     ``......``   substitute command output

     $$((((......))))
             substitute arithmetic expression



d) quoting











USD:3-38                        An Introduction to the UNIX Shell


     \\       quote the next character

     ''......''   quote the enclosed characters except for '

     ""......""   quote the enclosed characters except for $$ `` \\ ""



e) reserved words

     iiff tthheenn eellssee eelliiff ffii
     ccaassee iinn eessaacc
     ffoorr wwhhiillee uunnttiill ddoo ddoonnee
     !! {{  }}














































