1SH(1)                       General Commands Manual                      SH(1)


6       sh,  for, case, if, while, :, ., break, continue, cd, eval, exec, exit,
7       export, login, read, readonly, set, shift, times, trap, umask,  wait  -
8       command language


11       sh [ -ceiknrstuvx ] [ arg ] ...


14       Sh is a command programming language that executes commands read from a
15       terminal or a file.  See invocation for the meaning of arguments to the
16       shell.
18       Commands.
19       A  simple-command  is a sequence of non blank words separated by blanks
20       (a blank is a tab or a space).  The first word specifies  the  name  of
21       the  command  to  be executed.  Except as specified below the remaining
22       words are passed as arguments to the invoked command.  The command name
23       is passed as argument 0 (see execve(2)).  The value of a simple-command
24       is its exit status if it terminates normally or 200+status if it termi‐
25       nates abnormally (see sigvec(2) for a list of status values).
27       A  pipeline  is a sequence of one or more commands separated by |.  The
28       standard output of each command but the last is connected by a  pipe(2)
29       to  the  standard  input of the next command.  Each command is run as a
30       separate process; the shell waits for the last command to terminate.
32       A list is a sequence of one or more pipelines separated by ;, &, &&  or
33       ||  and optionally terminated by ; or &.  ; and & have equal precedence
34       which is lower than that of && and ||, && and || also have equal prece‐
35       dence.   A  semicolon  causes sequential execution; an ampersand causes
36       the preceding pipeline to be executed without waiting for it to finish.
37       The symbol && (||) causes the list following to be executed only if the
38       preceding pipeline returns a  zero  (non  zero)  value.   Newlines  may
39       appear in a list, instead of semicolons, to delimit commands.
41       A  command  is  either  a  simple-command or one of the following.  The
42       value returned by a command is that of the last simple-command executed
43       in the command.
45       for name [in word ...] do list done
46              Each time a for command is executed name is set to the next word
47              in the for word list.  If in word ...  is omitted,  in  "$@"  is
48              assumed.   Execution  ends  when  there are no more words in the
49              list.
51       case word in [pattern [ | pattern ] ... ) list ;;] ... esac
52              A case command executes the list associated with the first  pat‐
53              tern that matches word.  The form of the patterns is the same as
54              that used for file name generation.
56       if list then list [elif list then list] ... [else list] fi
57              The list following if is executed and if  it  returns  zero  the
58              list  following then is executed.  Otherwise, the list following
59              elif is executed and if its value is  zero  the  list  following
60              then is executed.  Failing that the else list is executed.
62       while list [do list] done
63              A  while  command  repeatedly executes the while list and if its
64              value is zero executes the do list; otherwise  the  loop  termi‐
65              nates.   The  value  returned  by a while command is that of the
66              last executed command in the do list.   until  may  be  used  in
67              place of while to negate the loop termination test.
69       ( list )
70              Execute list in a subshell.
72       { list }
73              list is simply executed.
75       The  following words are only recognized as the first word of a command
76       and when not quoted.
78              if then else elif fi case in esac for while until do done { }
80       Command substitution.
81       The standard output from a command enclosed in a pair  of  back  quotes
82       (``)  may  be  used  as  part  or  all of a word; trailing newlines are
83       removed.
85       Parameter substitution.
86       The character $ is used to introduce substitutable  parameters.   Posi‐
87       tional  parameters may be assigned values by set.  Variables may be set
88       by writing
90              name=value [ name=value ] ...
92       ${parameter}
93              A parameter is a sequence of letters, digits or  underscores  (a
94              name),  a  digit,  or  any of the characters * @ # ? - $ !.  The
95              value, if any, of the parameter is substituted.  The braces  are
96              required  only when parameter is followed by a letter, digit, or
97              underscore that is not to be interpreted as part  of  its  name.
98              If  parameter  is  a  digit,  it  is a positional parameter.  If
99              parameter is * or @ then all the positional parameters, starting
100              with  $1,  are  substituted separated by spaces.  $0 is set from
101              argument zero when the shell is invoked.
103       ${parameter-word}
104              If parameter is set, substitute its value; otherwise  substitute
105              word.
107       ${parameter=word}
108              If parameter is not set, set it to word; the value of the param‐
109              eter is then substituted.   Positional  parameters  may  not  be
110              assigned to in this way.
112       ${parameter?word}
113              If parameter is set, substitute its value; otherwise, print word
114              and exit from the shell.  If word is omitted, a standard message
115              is printed.
117       ${parameter+word}
118              If parameter is set, substitute word; otherwise substitute noth‐
119              ing.
121       In the above word is not evaluated unless it is to be used as the  sub‐
122       stituted string.  (So that, for example, echo ${d-´pwd´} will only exe‐
123       cute pwd if d is unset.)
125       The following parameters are automatically set by the shell.
127              #      The number of positional parameters in decimal.
128              -      Options supplied to the shell on invocation or by set.
129              ?      The value returned by the last executed command in  deci‐
130                     mal.
131              $      The process number of this shell.
132              !      The   process  number  of  the  last  background  command
133                     invoked.
135       The following parameters are used but not set by the shell.
137              HOME   The default argument (home directory) for the cd command.
138              PATH   The search path for commands (see execution).
139              MAIL   If this variable is set to the name of a mail  file,  the
140                     shell  informs  the  user  of  the arrival of mail in the
141                     specified file.
142              PS1    Primary prompt string, by default '$ '.
143              PS2    Secondary prompt string, by default '> '.
144              IFS    Internal field separators, normally space, tab, and  new‐
145                     line.   IFS is ignored if sh is running as root or if the
146                     effective user id differs from the real user id.
148       Blank interpretation.
149       After parameter and command substitution, any results  of  substitution
150       are  scanned  for  internal  field separator characters (those found in
151       $IFS) and split into  distinct  arguments  where  such  characters  are
152       found.  Explicit null arguments ("" or ´´) are retained.  Implicit null
153       arguments (those resulting from parameters that  have  no  values)  are
154       removed.
156       File name generation.
157       Following substitution, each command word is scanned for the characters
158       *, ?  and [.  If one of these characters appears, the word is  regarded
159       as  a  pattern.   The  word is replaced with alphabetically sorted file
160       names that match the pattern.  If no file name is  found  that  matches
161       the pattern, the word is left unchanged.  The character .  at the start
162       of a file name or immediately following a /, and the character /,  must
163       be matched explicitly.
165       *      Matches any string, including the null string.
166       ?      Matches any single character.
167       [...]  Matches  any  one of the characters enclosed.  A pair of charac‐
168              ters separated by - matches any character lexically between  the
169              pair.
171       Quoting.
172       The  following characters have a special meaning to the shell and cause
173       termination of a word unless quoted.
175            ;   &   (   )   |   <   >   newline   space   tab
177       A character may be quoted by  preceding  it  with  a  \.   \newline  is
178       ignored.   All  characters enclosed between a pair of quote marks (´´),
179       except a single quote, are quoted.  Inside double quotes ("") parameter
180       and  command  substitution occurs and \ quotes the characters \ ´ " and
181       $.
183       "$*" is equivalent to "$1 $2 ..."  whereas
184       "$@" is equivalent to "$1" "$2" ... .
186       Prompting.
187       When used interactively, the shell prompts with the value of PS1 before
188       reading a command.  If at any time a newline is typed and further input
189       is needed to complete a command, the secondary prompt ($PS2) is issued.
191       Input output.
192       Before a command is executed its input and  output  may  be  redirected
193       using  a  special notation interpreted by the shell.  The following may
194       appear anywhere in a simple-command or may precede or follow a  command
195       and  are  not  passed  on  to the invoked command.  Substitution occurs
196       before word or digit is used.
198       <word  Use file word as standard input (file descriptor 0).
200       >word  Use file word as standard output (file descriptor  1).   If  the
201              file does not exist, it is created; otherwise it is truncated to
202              zero length.
204       >>word Use file word as standard output.  If the file exists, output is
205              appended (by seeking to the end); otherwise the file is created.
207       <<word The shell input is read up to a line the same as word, or end of
208              file.  The resulting document becomes the  standard  input.   If
209              any  character  of  word  is quoted, no interpretation is placed
210              upon the characters of the document;  otherwise,  parameter  and
211              command  substitution occurs, \newline is ignored, and \ is used
212              to quote the characters \ $ ´ and the first character of word.
214       <&digit
215              The standard input is duplicated from file descriptor digit; see
216              dup(2).  Similarly for the standard output using >.
218       <&-    The standard input is closed.  Similarly for the standard output
219              using >.
221       If one of the above is preceded by a digit, the file descriptor created
222       is  that  specified  by the digit (instead of the default 0 or 1).  For
223       example,
225            ... 2>&1
227       creates file descriptor 2 to be a duplicate of file descriptor 1.
229       If a command is followed by & then the default standard input  for  the
230       command  is the empty file (/dev/null).  Otherwise, the environment for
231       the execution of a command contains the file descriptors of the  invok‐
232       ing shell as modified by input output specifications.
234       Environment.
235       The environment is a list of name-value pairs that is passed to an exe‐
236       cuted program in the same way as a normal argument list; see  execve(2)
237       and  environ(7).   The  shell interacts with the environment in several
238       ways.  On invocation, the shell scans the  environment  and  creates  a
239       parameter for each name found, giving it the corresponding value.  Exe‐
240       cuted commands inherit the same environment.  If the user modifies  the
241       values  of  these parameters or creates new ones, none of these affects
242       the environment unless the export command is used to bind  the  shell's
243       parameter  to  the  environment.   The environment seen by any executed
244       command is thus composed of any unmodified name-value pairs  originally
245       inherited  by  the  shell,  plus any modifications or additions, all of
246       which must be noted in export commands.
248       The environment for any simple-command may be augmented by prefixing it
249       with  one  or more assignments to parameters.  Thus these two lines are
250       equivalent
252              TERM=450 cmd args
253              (export TERM; TERM=450; cmd args)
255       If the -k flag is set, all keyword arguments are placed in the environ‐
256       ment,  even  if the occur after the command name.  The following prints
257       'a=b c' and 'c':
258       echo a=b c
259       set -k
260       echo a=b c
262       Signals.
263       The INTERRUPT and QUIT signals for an invoked command  are  ignored  if
264       the  command is followed by &; otherwise signals have the values inher‐
265       ited by the shell from its parent.  (But see also trap.)
267       Execution.
268       Each time a command is executed the  above  substitutions  are  carried
269       out.   Except  for the 'special commands' listed below a new process is
270       created and an attempt is made to execute the command via an execve(2).
272       The shell parameter $PATH defines the search  path  for  the  directory
273       containing  the  command.  Each alternative directory name is separated
274       by a colon (:).  The default path is :/bin:/usr/bin.   If  the  command
275       name contains a /, the search path is not used.  Otherwise, each direc‐
276       tory in the path is searched for an executable file.  If the  file  has
277       execute permission but is not an a.out file, it is assumed to be a file
278       containing shell commands.  A subshell (i.e., a  separate  process)  is
279       spawned to read it.  A parenthesized command is also executed in a sub‐
280       shell.
282       Special commands.
283       The following commands are executed in the  shell  process  and  except
284       where  specified no input output redirection is permitted for such com‐
285       mands.
287       #      For  non-interactive  shells,  everything  following  the  #  is
288              treated as a comment, i.e. the rest of the line is ignored.  For
289              interactive shells, the # has no special effect.
291       :      No effect; the command does nothing.
292       . file Read and execute commands from file and return.  The search path
293              $PATH is used to find the directory containing file.
294       break [n]
295              Exit  from  the  enclosing  for  or while loop, if any.  If n is
296              specified, break n levels.
297       continue [n]
298              Resume the next iteration of the enclosing for  or  while  loop.
299              If n is specified, resume at the n-th enclosing loop.
300       cd [arg]
301              Change  the current directory to arg.  The shell parameter $HOME
302              is the default arg.
303       eval [arg ...]
304              The arguments are read as input to the shell and  the  resulting
305              command(s) executed.
306       exec [arg ...]
307              The  command  specified by the arguments is executed in place of
308              this shell without creating a new process.  Input  output  argu‐
309              ments  may  appear and if no other arguments are given cause the
310              shell input output to be modified.
311       exit [n]
312              Causes a non interactive shell to  exit  with  the  exit  status
313              specified by n.  If n is omitted, the exit status is that of the
314              last command executed.  (An end of file will also exit from  the
315              shell.)
316       export [name ...]
317              The  given names are marked for automatic export to the environ‐
318              ment of subsequently-executed commands.   If  no  arguments  are
319              given, a list of exportable names is printed.
320       login [arg ...]
321              Equivalent to 'exec login arg ...'.
322       read name ...
323              One  line  is  read from the standard input; successive words of
324              the input are assigned to the  variables  name  in  order,  with
325              leftover  words  to  the  last  variable.   The return code is 0
326              unless the end-of-file is encountered.
327       readonly [name ...]
328              The given names are marked readonly and the values of the  these
329              names  may not be changed by subsequent assignment.  If no argu‐
330              ments are given, a list of all readonly names is printed.
331       set [-eknptuvx [arg ...]]
332              -e If non interactive, exit immediately if a command fails.
333              -k All keyword arguments are placed in  the  environment  for  a
334                 command, not just those that precede the command name.
335              -n Read commands but do not execute them.
336              -t Exit after reading and executing one command.
337              -u Treat unset variables as an error when substituting.
338              -v Print shell input lines as they are read.
339              -x Print commands and their arguments as they are executed.
340              -  Turn off the -x and -v options.
342              These  flags can also be used upon invocation of the shell.  The
343              current set of flags may be found in $-.
345              Remaining arguments are positional parameters and are  assigned,
346              in order, to $1, $2, etc.  If no arguments are given, the values
347              of all names are printed.
349       shift  The positional parameters from $2...  are renamed $1...
351       times  Print the accumulated user and system times  for  processes  run
352              from the shell.
354       trap [arg] [n] ...
355              Arg is a command to be read and executed when the shell receives
356              signal(s) n.  (Note that arg is scanned once when  the  trap  is
357              set  and  once  when the trap is taken.)  Trap commands are exe‐
358              cuted in order of signal number.  If arg is absent, all  trap(s)
359              n  are  reset  to  their  original  values.   If arg is the null
360              string, this signal is ignored by the shell and by invoked  com‐
361              mands.   If n is 0, the command arg is executed on exit from the
362              shell, otherwise  upon  receipt  of  signal  n  as  numbered  in
363              sigvec(2).   Trap  with  no  arguments prints a list of commands
364              associated with each signal number.
366       umask [ nnn ]
367              The user file creation mask is set to the octal value  nnn  (see
368              umask(2)).   If nnn is omitted, the current value of the mask is
369              printed.
371       wait [n]
372              Wait for the specified process and report its  termination  sta‐
373              tus.   If  n  is not given, all currently active child processes
374              are waited for.  The return code from this command  is  that  of
375              the process waited for.
377       Invocation.
378       If  the  first  character of argument zero is -, commands are read from
379       $HOME/.profile, if such a file  exists.   Commands  are  then  read  as
380       described below.  The following flags are interpreted by the shell when
381       it is invoked.
382       -c string  If the -c flag is present, commands are read from string.
383       -s         If the -s flag is present or if  no  arguments  remain  then
384                  commands  are read from the standard input.  Shell output is
385                  written to file descriptor 2.
386       -i         If the -i flag is present or if the shell input  and  output
387                  are attached to a terminal (as told by gtty) then this shell
388                  is interactive.  In this case the terminate  signal  SIGTERM
389                  (see  sigvec(2))  is ignored (so that 'kill 0' does not kill
390                  an interactive shell) and the  interrupt  signal  SIGINT  is
391                  caught  and ignored (so that wait is interruptible).  In all
392                  cases SIGQUIT is ignored by the shell.
394       The remaining flags and arguments are described under the set command.


397       $HOME/.profile
398       /tmp/sh*
399       /dev/null


402       csh(1), test(1), execve(2), environ(7)


405       Errors detected by the shell, such as syntax errors cause the shell  to
406       return a non zero exit status.  If the shell is being used non interac‐
407       tively then execution of the shell file is abandoned.   Otherwise,  the
408       shell  returns  the  exit status of the last command executed (see also
409       exit).


412       If << is used to provide standard  input  to  an  asynchronous  process
413       invoked  by &, the shell gets mixed up about naming the input document.
414       A garbage file /tmp/sh* is created, and the shell complains  about  not
415       being able to find the file by another name.
4197th Edition                       May 5, 1986                            SH(1)