1SH(1) General Commands Manual SH(1)
2
3
4
6 sh, for, case, if, while, :, ., break, continue, cd, eval, exec, exit,
7 export, login, newgrp, read, readonly, set, shift, times, trap, umask,
8 wait - command language
9
11 sh [ -ceiknrstuvx ] [ arg ] ...
12
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.
17
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 exec(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 signal(2) for a list of status values).
26
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.
31
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.
40
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.
44
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 then in "$@" is
48 assumed. Execution ends when there are no more words in the
49 list.
50
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.
55
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.
61
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.
68
69 ( list )
70 Execute list in a subshell.
71
72 { list }
73 list is simply executed.
74
75 The following words are only recognized as the first word of a command
76 and when not quoted.
77
78 if then else elif fi case in esac for while until do done { }
79
80 Command substitution.
81 The standard output from a command enclosed in a pair of grave accents
82 (``) may be used as part or all of a word; trailing newlines are
83 removed.
84
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
89
90 name=value [ name=value ] ...
91
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 then 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.
102
103 ${parameter-word}
104 If parameter is set then substitute its value; otherwise substi‐
105 tute word.
106
107 ${parameter=word}
108 If parameter is not set then set it to word; the value of the
109 parameter is then substituted. Positional parameters may not be
110 assigned to in this way.
111
112 ${parameter?word}
113 If parameter is set then substitute its value; otherwise, print
114 word and exit from the shell. If word is omitted then a stan‐
115 dard message is printed.
116
117 ${parameter+word}
118 If parameter is set then substitute word; otherwise substitute
119 nothing.
120
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.)
124
125 The following parameters are automatically set by the shell.
126
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.
134
135 The following parameters are used but not set by the shell.
136
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 then
140 the 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.
146
147 Blank interpretation.
148 After parameter and command substitution, any results of substitution
149 are scanned for internal field separator characters (those found in
150 $IFS) and split into distinct arguments where such characters are
151 found. Explicit null arguments ("" or ´´) are retained. Implicit null
152 arguments (those resulting from parameters that have no values) are
153 removed.
154
155 File name generation.
156 Following substitution, each command word is scanned for the characters
157 *, ? and [. If one of these characters appears then the word is
158 regarded as a pattern. The word is replaced with alphabetically sorted
159 file names that match the pattern. If no file name is found that
160 matches the pattern then the word is left unchanged. The character .
161 at the start of a file name or immediately following a /, and the char‐
162 acter /, must be matched explicitly.
163
164 * Matches any string, including the null string.
165 ? Matches any single character.
166 [...] Matches any one of the characters enclosed. A pair of charac‐
167 ters separated by - matches any character lexically between the
168 pair.
169
170 Quoting.
171 The following characters have a special meaning to the shell and cause
172 termination of a word unless quoted.
173
174 ; & ( ) | < > newline space tab
175
176 A character may be quoted by preceding it with a \. \newline is
177 ignored. All characters enclosed between a pair of quote marks (´´),
178 except a single quote, are quoted. Inside double quotes ("") parameter
179 and command substitution occurs and \ quotes the characters \ ` " and
180 $.
181
182 "$*" is equivalent to "$1 $2 ..." whereas
183 "$@" is equivalent to "$1" "$2" ... .
184
185 Prompting.
186 When used interactively, the shell prompts with the value of PS1 before
187 reading a command. If at any time a newline is typed and further input
188 is needed to complete a command then the secondary prompt ($PS2) is
189 issued.
190
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.
197
198 <word Use file word as standard input (file descriptor 0).
199
200 >word Use file word as standard output (file descriptor 1). If the
201 file does not exist then it is created; otherwise it is trun‐
202 cated to zero length.
203
204 >>word Use file word as standard output. If the file exists then out‐
205 put is appended (by seeking to the end); otherwise the file is
206 created.
207
208 <<word The shell input is read up to a line the same as word, or end of
209 file. The resulting document becomes the standard input. If
210 any character of word is quoted then no interpretation is placed
211 upon the characters of the document; otherwise, parameter and
212 command substitution occurs, \newline is ignored, and \ is used
213 to quote the characters \ $ ` and the first character of word.
214
215 <&digit
216 The standard input is duplicated from file descriptor digit; see
217 dup(2). Similarly for the standard output using >.
218
219 <&- The standard input is closed. Similarly for the standard output
220 using >.
221
222 If one of the above is preceded by a digit then the file descriptor
223 created is that specified by the digit (instead of the default 0 or 1).
224 For example,
225
226 ... 2>&1
227
228 creates file descriptor 2 to be a duplicate of file descriptor 1.
229
230 If a command is followed by & then the default standard input for the
231 command is the empty file (/dev/null). Otherwise, the environment for
232 the execution of a command contains the file descriptors of the invok‐
233 ing shell as modified by input output specifications.
234
235 Environment.
236 The environment is a list of name-value pairs that is passed to an exe‐
237 cuted program in the same way as a normal argument list; see exec(2)
238 and environ(5). The shell interacts with the environment in several
239 ways. On invocation, the shell scans the environment and creates a
240 parameter for each name found, giving it the corresponding value. Exe‐
241 cuted commands inherit the same environment. If the user modifies the
242 values of these parameters or creates new ones, none of these affects
243 the environment unless the export command is used to bind the shell's
244 parameter to the environment. The environment seen by any executed
245 command is thus composed of any unmodified name-value pairs originally
246 inherited by the shell, plus any modifications or additions, all of
247 which must be noted in export commands.
248
249 The environment for any simple-command may be augmented by prefixing it
250 with one or more assignments to parameters. Thus these two lines are
251 equivalent
252
253 TERM=450 cmd args
254 (export TERM; TERM=450; cmd args)
255
256 If the -k flag is set, all keyword arguments are placed in the environ‐
257 ment, even if the occur after the command name. The following prints
258 `a=b c' and `c':
259 echo a=b c
260 set -k
261 echo a=b c
262
263 Signals.
264 The INTERRUPT and QUIT signals for an invoked command are ignored if
265 the command is followed by &; otherwise signals have the values inher‐
266 ited by the shell from its parent. (But see also trap.)
267
268 Execution.
269 Each time a command is executed the above substitutions are carried
270 out. Except for the `special commands' listed below a new process is
271 created and an attempt is made to execute the command via an exec(2).
272
273 The shell parameter $PATH defines the search path for the directory
274 containing the command. Each alternative directory name is separated
275 by a colon (:). The default path is :/bin:/usr/bin. If the command
276 name contains a / then the search path is not used. Otherwise, each
277 directory in the path is searched for an executable file. If the file
278 has execute permission but is not an a.out file, it is assumed to be a
279 file containing shell commands. A subshell (i.e., a separate process)
280 is spawned to read it. A parenthesized command is also executed in a
281 subshell.
282
283 Special commands.
284 The following commands are executed in the shell process and except
285 where specified no input output redirection is permitted for such com‐
286 mands.
287
288 : No effect; the command does nothing.
289 . file Read and execute commands from file and return. The search path
290 $PATH is used to find the directory containing file.
291 break [n]
292 Exit from the enclosing for or while loop, if any. If n is
293 specified then break n levels.
294 continue [n]
295 Resume the next iteration of the enclosing for or while loop.
296 If n is specified then resume at the n-th enclosing loop.
297 cd [arg]
298 Change the current directory to arg. The shell parameter $HOME
299 is the default arg.
300 eval [arg ...]
301 The arguments are read as input to the shell and the resulting
302 command(s) executed.
303 exec [arg ...]
304 The command specified by the arguments is executed in place of
305 this shell without creating a new process. Input output argu‐
306 ments may appear and if no other arguments are given cause the
307 shell input output to be modified.
308 exit [n]
309 Causes a non interactive shell to exit with the exit status
310 specified by n. If n is omitted then the exit status is that of
311 the last command executed. (An end of file will also exit from
312 the shell.)
313 export [name ...]
314 The given names are marked for automatic export to the environ‐
315 ment of subsequently-executed commands. If no arguments are
316 given then a list of exportable names is printed.
317 login [arg ...]
318 Equivalent to `exec login arg ...'.
319 newgrp [arg ...]
320 Equivalent to `exec newgrp arg ...'.
321 read name ...
322 One line is read from the standard input; successive words of
323 the input are assigned to the variables name in order, with
324 leftover words to the last variable. The return code is 0
325 unless the end-of-file is encountered.
326 readonly [name ...]
327 The given names are marked readonly and the values of the these
328 names may not be changed by subsequent assignment. If no argu‐
329 ments are given then a list of all readonly names is printed.
330 set [-eknptuvx [arg ...]]
331 -e If non interactive then exit immediately if a command fails.
332 -k All keyword arguments are placed in the environment for a
333 command, not just those that precede the command name.
334 -n Read commands but do not execute them.
335 -t Exit after reading and executing one command.
336 -u Treat unset variables as an error when substituting.
337 -v Print shell input lines as they are read.
338 -x Print commands and their arguments as they are executed.
339 - Turn off the -x and -v options.
340
341 These flags can also be used upon invocation of the shell. The
342 current set of flags may be found in $-.
343
344 Remaining arguments are positional parameters and are assigned,
345 in order, to $1, $2, etc. If no arguments are given then the
346 values of all names are printed.
347
348 shift The positional parameters from $2... are renamed $1...
349
350 times Print the accumulated user and system times for processes run
351 from the shell.
352
353 trap [arg] [n] ...
354 Arg is a command to be read and executed when the shell receives
355 signal(s) n. (Note that arg is scanned once when the trap is
356 set and once when the trap is taken.) Trap commands are exe‐
357 cuted in order of signal number. If arg is absent then all
358 trap(s) n are reset to their original values. If arg is the
359 null string then this signal is ignored by the shell and by
360 invoked commands. If n is 0 then the command arg is executed on
361 exit from the shell, otherwise upon receipt of signal n as num‐
362 bered in signal(2). Trap with no arguments prints a list of
363 commands associated with each signal number.
364
365 umask [ nnn ]
366 The user file creation mask is set to the octal value nnn (see
367 umask(2)). If nnn is omitted, the current value of the mask is
368 printed.
369
370 wait [n]
371 Wait for the specified process and report its termination sta‐
372 tus. If n is not given then all currently active child pro‐
373 cesses are waited for. The return code from this command is
374 that of the process waited for.
375
376 Invocation.
377 If the first character of argument zero is -, commands are read from
378 $HOME/.profile, if such a file exists. Commands are then read as
379 described below. The following flags are interpreted by the shell when
380 it is invoked.
381 -c string If the -c flag is present then commands are read from
382 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 signal(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 interruptable). In all
392 cases SIGQUIT is ignored by the shell.
393
394 The remaining flags and arguments are described under the set command.
395
397 $HOME/.profile
398 /tmp/sh*
399 /dev/null
400
402 test(1), exec(2),
403
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).
410
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.
416
417
418
419 SH(1)