1sh(1) User Commands sh(1)
2
3
4
6 sh, jsh - standard and job control shell and command interpreter
7
9 /usr/bin/sh [-acefhiknprstuvx] [argument]...
10
11
12 /usr/xpg4/bin/sh [± abCefhikmnoprstuvx]
13 [± o option]... [-c string] [arg]...
14
15
16 /usr/bin/jsh [-acefhiknprstuvx] [argument]...
17
18
20 The /usr/bin/sh utility is a command programming language that executes
21 commands read from a terminal or a file.
22
23
24 The /usr/xpg4/bin/sh utility is a standards compliant shell. This util‐
25 ity provides all the functionality of ksh(1), except in cases discussed
26 in ksh(1) where differences in behavior exist.
27
28
29 The jsh utility is an interface to the shell that provides all of the
30 functionality of sh and enables job control (see Job Control section
31 below).
32
33
34 Arguments to the shell are listed in the Invocation section below.
35
36 Definitions
37 A blank is a tab or a space. A name is a sequence of ASCII letters,
38 digits, or underscores, beginning with a letter or an underscore. A
39 parameter is a name, a digit, or any of the characters *, @, #, ?, −,
40 $, and !.
41
43 Commands
44 A simple-command is a sequence of non-blank words separated by blanks.
45 The first word specifies the name of the command to be executed. Except
46 as specified below, the remaining words are passed as arguments to the
47 invoked command. The command name is passed as argument 0 (see
48 exec(2)). The value of a simple-command is its exit status if it termi‐
49 nates normally, or (octal) 200+status if it terminates abnormally. See
50 signal.h(3HEAD) for a list of status values.
51
52
53 A pipeline is a sequence of one or more commands separated by |. The
54 standard output of each command but the last is connected by a pipe(2)
55 to the standard input of the next command. Each command is run as a
56 separate process. The shell waits for the last command to terminate.
57 The exit status of a pipeline is the exit status of the last command in
58 the pipeline.
59
60
61 A list is a sequence of one or more pipelines separated by ;, &, &&, or
62 ||, and optionally terminated by ; or &. Of these four symbols, ; and &
63 have equal precedence, which is lower than that of && and ||. The sym‐
64 bols && and || also have equal precedence. A semicolon (;) causes
65 sequential execution of the preceding pipeline, that is, the shell
66 waits for the pipeline to finish before executing any commands follow‐
67 ing the semicolon. An ampersand (&) causes asynchronous execution of
68 the preceding pipeline, that is, the shell does not wait for that pipe‐
69 line to finish. The symbol && (||) causes the list following it to be
70 executed only if the preceding pipeline returns a zero (non-zero) exit
71 status. An arbitrary number of newlines can appear in a list, instead
72 of semicolons, to delimit commands.
73
74
75 A command is either a simple-command or one of the following. Unless
76 otherwise stated, the value returned by a command is that of the last
77 simple-command executed in the command.
78
79 for name [ in word ... ] do list done
80
81 Each time a for command is executed, name is set to the next word
82 taken from the in word list. If in word ... is omitted, then the
83 for command executes the do list once for each positional parameter
84 that is set (see Parameter Substitution section below). Execution
85 ends when there are no more words in the list.
86
87
88 case word in [ pattern [ | pattern ] ) list ;; ] ... esac
89
90 A case command executes the list associated with the first pattern
91 that matches word. The form of the patterns is the same as that
92 used for file-name generation (see File Name Generation section),
93 except that a slash, a leading dot, or a dot immediately following
94 a slash need not be matched explicitly.
95
96
97
98 if list ; then list elif list ; then list ; ] ... [ else list ; ] fi
99
100
101 The list following if is executed and, if it returns a zero exit sta‐
102 tus, the list following the first then is executed. Otherwise, the list
103 following elif is executed and, if its value is zero, the list follow‐
104 ing the next then is executed. Failing that, the else list is executed.
105 If no else list or then list is executed, then the if command returns a
106 zero exit status.
107
108 while list do list done A while command repeatedly executes the
109 while list and, if the exit status of the
110 last command in the list is zero, executes
111 the do list; otherwise the loop terminates.
112 If no commands in the do list are executed,
113 then the while command returns a zero exit
114 status; until can be used in place of while
115 to negate the loop termination test.
116
117
118 (list) Execute list in a sub-shell.
119
120
121 { list;} list is executed in the current (that is,
122 parent) shell. The { must be followed by a
123 space.
124
125
126 name () { list;} Define a function which is referenced by
127 name. The body of the function is the list
128 of commands between { and }. The { must be
129 followed by a space. Execution of functions
130 is described below (see Execution section).
131 The { and } are unnecessary if the body of
132 the function is a command as defined above,
133 under Commands.
134
135
136
137 The following words are only recognized as the first word of a command
138 and when not quoted:
139
140
141 if then else elif fi case esac for while until do done { }
142
143 Comments Lines
144 A word beginning with # causes that word and all the following charac‐
145 ters up to a newline to be ignored.
146
147 Command Substitution
148 The shell reads commands from the string between two grave accents (``)
149 and the standard output from these commands can be used as all or part
150 of a word. Trailing newlines from the standard output are removed.
151
152
153 No interpretation is done on the string before the string is read,
154 except to remove backslashes (\) used to escape other characters. Back‐
155 slashes can be used to escape a grave accent (`) or another backslash
156 (\) and are removed before the command string is read. Escaping grave
157 accents allows nested command substitution. If the command substitution
158 lies within a pair of double quotes (" ...` ...` ... "), a backslash
159 used to escape a double quote (\") is removed. Otherwise, it is left
160 intact.
161
162
163 If a backslash is used to escape a newline character (\newline), both
164 the backslash and the newline are removed (see the later section on
165 Quoting). In addition, backslashes used to escape dollar signs (\$) are
166 removed. Since no parameter substitution is done on the command string
167 before it is read, inserting a backslash to escape a dollar sign has no
168 effect. Backslashes that precede characters other than \, `, ", new‐
169 line, and $ are left intact when the command string is read.
170
171 Parameter Substitution
172 The character $ is used to introduce substitutable parameters. There
173 are two types of parameters, positional and keyword. If parameter is a
174 digit, it is a positional parameter. Positional parameters can be
175 assigned values by set. Keyword parameters (also known as variables)
176 can be assigned values by writing:
177
178
179 name=value [ name=value ] ...
180
181
182 Pattern-matching is not performed on value. There cannot be a function
183 and a variable with the same name.
184
185 ${parameter} The value, if any, of the parameter is substi‐
186 tuted. The braces are required only when
187 parameter is followed by a letter, digit, or
188 underscore that is not to be interpreted as
189 part of its name. If parameter is * or @, all
190 the positional parameters, starting with $1,
191 are substituted (separated by spaces). Parame‐
192 ter $0 is set from argument zero when the
193 shell is invoked.
194
195
196 ${parameter:−word} Use Default Values. If parameter is unset or
197 null, the expansion of word is substituted;
198 otherwise, the value of parameter is substi‐
199 tuted.
200
201
202 ${parameter:=word} Assign Default Values. If parameter is unset
203 or null, the expansion of word is assigned to
204 parameter. In all cases, the final value of
205 parameter is substituted. Only variables, not
206 positional parameters or special parameters,
207 can be assigned in this way.
208
209
210 ${parameter:?word} If parameter is set and is non-null, substi‐
211 tute its value; otherwise, print word and exit
212 from the shell. If word is omitted, the mes‐
213 sage "parameter null or not set" is printed.
214
215
216 ${parameter:+word} If parameter is set and is non-null, substi‐
217 tute word; otherwise substitute nothing.
218
219
220
221 In the above, word is not evaluated unless it is to be used as the sub‐
222 stituted string, so that, in the following example, pwd is executed
223 only if d is not set or is null:
224
225 echo ${d:−`pwd`}
226
227
228
229
230 If the colon (:) is omitted from the above expressions, the shell only
231 checks whether parameter is set or not.
232
233
234 The following parameters are automatically set by the shell.
235
236 # The number of positional parameters in decimal.
237
238
239 − Flags supplied to the shell on invocation or by the set com‐
240 mand.
241
242
243 ? The decimal value returned by the last synchronously executed
244 command.
245
246
247 $ The process number of this shell.
248
249
250 ! The process number of the last background command invoked.
251
252
253
254 The following parameters are used by the shell. The parameters in this
255 section are also referred to as environment variables.
256
257 HOME The default argument (home directory) for the cd command,
258 set to the user's login directory by login(1) from the
259 password file (see passwd(4)).
260
261
262 PATH The search path for commands (see Execution section
263 below).
264
265
266 CDPATH The search path for the cd command.
267
268
269 MAIL If this parameter is set to the name of a mail file and
270 the MAILPATH parameter is not set, the shell informs the
271 user of the arrival of mail in the specified file.
272
273
274 MAILCHECK This parameter specifies how often (in seconds) the shell
275 checks for the arrival of mail in the files specified by
276 the MAILPATH or MAIL parameters. The default value is 600
277 seconds (10 minutes). If set to 0, the shell checks before
278 each prompt.
279
280
281 MAILPATH A colon-separated list of file names. If this parameter is
282 set, the shell informs the user of the arrival of mail in
283 any of the specified files. Each file name can be followed
284 by % and a message that is e printed when the modification
285 time changes. The default message is, you have mail.
286
287
288 PS1 Primary prompt string, by default " $ ".
289
290
291 PS2 Secondary prompt string, by default " > ".
292
293
294 IFS Internal field separators, normally space, tab, and new‐
295 line (see Blank Interpretation section).
296
297
298 SHACCT If this parameter is set to the name of a file writable by
299 the user, the shell writes an accounting record in the
300 file for each shell procedure executed.
301
302
303 SHELL When the shell is invoked, it scans the environment (see
304 Environment section below) for this name.
305
306
307
308 See environ(5) for descriptions of the following environment variables
309 that affect the execution of sh: LC_CTYPE and LC_MESSAGES.
310
311
312 The shell gives default values to PATH, PS1, PS2, MAILCHECK, and IFS.
313 Default values for HOME and MAIL are set by login(1).
314
315 Blank Interpretation
316 After parameter and command substitution, the results of substitution
317 are scanned for internal field separator characters (those found in
318 IFS) and split into distinct arguments where such characters are found.
319 Explicit null arguments ("" or '') are retained. Implicit null argu‐
320 ments (those resulting from parameters that have no values) are
321 removed.
322
323 Input/Output Redirection
324 A command's input and output can be redirected using a special notation
325 interpreted by the shell. The following can appear anywhere in a sim‐
326 ple-command or can precede or follow a command and are not passed on as
327 arguments to the invoked command. Note: Parameter and command substitu‐
328 tion occurs before word or digit is used.
329
330 <word Use file word as standard input (file descriptor 0).
331
332
333 >word Use file word as standard output (file descriptor 1).
334 If the file does not exist, it is created; otherwise,
335 it is truncated to zero length.
336
337
338 >>word Use file word as standard output. If the file exists,
339 output is appended to it by first seeking to the EOF.
340 Otherwise, the file is created.
341
342
343 <>word Open file word for reading and writing as standard
344 input.
345
346
347 <<[−]word After parameter and command substitution is done on
348 word, the shell input is read up to the first line that
349 literally matches the resulting word, or to an EOF. If,
350 however, the hyphen (−) is appended to <<:
351
352 1. leading tabs are stripped from word before
353 the shell input is read (but after parameter
354 and command substitution is done on word);
355
356 2. leading tabs are stripped from the shell
357 input as it is read and before each line is
358 compared with word; and
359
360 3. shell input is read up to the first line
361 that literally matches the resulting word,
362 or to an EOF.
363 If any character of word is quoted (see Quoting section
364 later), no additional processing is done to the shell
365 input. If no characters of word are quoted:
366
367 1. parameter and command substitution occurs;
368
369 2. (escaped) \newlines are removed; and
370
371 3. \ must be used to quote the characters \, $,
372 and `.
373 The resulting document becomes the standard input.
374
375
376 <&digit Use the file associated with file descriptor digit as
377 standard input. Similarly for the standard output using
378 >&digit.
379
380
381 <&− The standard input is closed. Similarly for the stan‐
382 dard output using >&−.
383
384
385
386 If any of the above is preceded by a digit, the file descriptor which
387 is associated with the file is that specified by the digit (instead of
388 the default 0 or 1). For example:
389
390 ... 2>&1
391
392
393
394
395 associates file descriptor 2 with the file currently associated with
396 file descriptor 1.
397
398
399 The order in which redirections are specified is significant. The shell
400 evaluates redirections left-to-right. For example:
401
402 ... 1>xxx 2>&1
403
404
405
406
407 first associates file descriptor 1 with file xxx. It associates file
408 descriptor 2 with the file associated with file descriptor 1 (that is,
409 xxx). If the order of redirections were reversed, file descriptor 2
410 would be associated with the terminal (assuming file descriptor 1 had
411 been) and file descriptor 1 would be associated with file xxx.
412
413
414 Using the terminology introduced on the first page, under Commands, if
415 a command is composed of several simple commands, redirection is evalu‐
416 ated for the entire command before it is evaluated for each simple com‐
417 mand. That is, the shell evaluates redirection for the entire list,
418 then each pipeline within the list, then each command within each pipe‐
419 line, then each list within each command.
420
421
422 If a command is followed by &, the default standard input for the com‐
423 mand is the empty file, /dev/null. Otherwise, the environment for the
424 execution of a command contains the file descriptors of the invoking
425 shell as modified by input/output specifications.
426
427 File Name Generation
428 Before a command is executed, each command word is scanned for the
429 characters *, ?, and [. If one of these characters appears the word is
430 regarded as a pattern. The word is replaced with alphabetically sorted
431 file names that match the pattern. If no file name is found that
432 matches the pattern, the word is left unchanged. The character . at the
433 start of a file name or immediately following a /, as well as the char‐
434 acter / itself, must be matched explicitly.
435
436 * Matches any string, including the null string.
437
438
439 ? Matches any single character.
440
441
442 [...] Matches any one of the enclosed characters. A pair of
443 characters separated by − matches any character lexically
444 between the pair, inclusive. If the first character fol‐
445 lowing the opening [ is a !, any character not enclosed is
446 matched.
447
448
449
450 Notice that all quoted characters (see below) must be matched explic‐
451 itly in a filename.
452
453 Quoting
454 The following characters have a special meaning to the shell and cause
455 termination of a word unless quoted:
456
457
458 ; & ( ) | ^ < > newline space tab
459
460
461 A character can be quoted (that is, made to stand for itself) by pre‐
462 ceding it with a backslash (\) or inserting it between a pair of quote
463 marks ('' or ""). During processing, the shell can quote certain char‐
464 acters to prevent them from taking on a special meaning. Backslashes
465 used to quote a single character are removed from the word before the
466 command is executed. The pair \newline is removed from a word before
467 command and parameter substitution.
468
469
470 All characters enclosed between a pair of single quote marks (''),
471 except a single quote, are quoted by the shell. Backslash has no spe‐
472 cial meaning inside a pair of single quotes. A single quote can be
473 quoted inside a pair of double quote marks (for example, "'"), but a
474 single quote can not be quoted inside a pair of single quotes.
475
476
477 Inside a pair of double quote marks (""), parameter and command substi‐
478 tution occurs and the shell quotes the results to avoid blank interpre‐
479 tation and file name generation. If $* is within a pair of double
480 quotes, the positional parameters are substituted and quoted, separated
481 by quoted spaces ("$1 $2 ..."). However, if $@ is within a pair of dou‐
482 ble quotes, the positional parameters are substituted and quoted, sepa‐
483 rated by unquoted spaces ("$1""$2" ... ). \ quotes the characters \,
484 `, , (comma), and $. The pair \newline is removed before parameter and
485 command substitution. If a backslash precedes characters other than \,
486 `, , (comma), $, and newline, then the backslash itself is quoted by
487 the shell.
488
489 Prompting
490 When used interactively, the shell prompts with the value of PS1 before
491 reading a command. If at any time a newline is typed and further input
492 is needed to complete a command, the secondary prompt (that is, the
493 value of PS2) is issued.
494
495 Environment
496 The environment (see environ(5)) is a list of name-value pairs that is
497 passed to an executed program in the same way as a normal argument
498 list. The shell interacts with the environment in several ways. On
499 invocation, the shell scans the environment and creates a parameter for
500 each name found, giving it the corresponding value. If the user modi‐
501 fies the value of any of these parameters or creates new parameters,
502 none of these affects the environment unless the export command is used
503 to bind the shell's parameter to the environment (see also set -a). A
504 parameter can be removed from the environment with the unset command.
505 The environment seen by any executed command is thus composed of any
506 unmodified name-value pairs originally inherited by the shell, minus
507 any pairs removed by unset, plus any modifications or additions, all of
508 which must be noted in export commands.
509
510
511 The environment for any simple-command can be augmented by prefixing it
512 with one or more assignments to parameters. Thus:
513
514 TERM=450 command
515
516
517
518
519 and
520
521 (export TERM; TERM=450; command
522
523
524
525
526 are equivalent as far as the execution of command is concerned if com‐
527 mand is not a Special Command. If command is a Special Command, then
528
529 TERM=450 command
530
531
532
533
534 modifies the TERM variable in the current shell.
535
536
537 If the -k flag is set, all keyword arguments are placed in the environ‐
538 ment, even if they occur after the command name. The following example
539 first prints a=b c and c:
540
541 echo a=b c
542
543 a=b c
544
545 set −k
546
547 echo a=b c
548
549 c
550
551
552
553 Signals
554 The INTERRUPT and QUIT signals for an invoked command are ignored if
555 the command is followed by &. Otherwise, signals have the values inher‐
556 ited by the shell from its parent, with the exception of signal 11 (but
557 see also the trap command below).
558
559 Execution
560 Each time a command is executed, the command substitution, parameter
561 substitution, blank interpretation, input/output redirection, and file‐
562 name generation listed above are carried out. If the command name
563 matches the name of a defined function, the function is executed in the
564 shell process (note how this differs from the execution of shell script
565 files, which require a sub-shell for invocation). If the command name
566 does not match the name of a defined function, but matches one of the
567 Special Commands listed below, it is executed in the shell process.
568
569
570 The positional parameters $1, $2, ... are set to the arguments of the
571 function. If the command name matches neither a Special Command nor the
572 name of a defined function, a new process is created and an attempt is
573 made to execute the command via exec(2).
574
575
576 The shell parameter PATH defines the search path for the directory con‐
577 taining the command. Alternative directory names are separated by a
578 colon (:). The default path is /usr/bin. The current directory is spec‐
579 ified by a null path name, which can appear immediately after the equal
580 sign, between two colon delimiters anywhere in the path list, or at the
581 end of the path list. If the command name contains a / the search path
582 is not used. Otherwise, each directory in the path is searched for an
583 executable file. If the file has execute permission but is not an a.out
584 file, it is assumed to be a file containing shell commands. A sub-shell
585 is spawned to read it. A parenthesized command is also executed in a
586 sub-shell.
587
588
589 The location in the search path where a command was found is remembered
590 by the shell (to help avoid unnecessary execs later). If the command
591 was found in a relative directory, its location must be re-determined
592 whenever the current directory changes. The shell forgets all remem‐
593 bered locations whenever the PATH variable is changed or the hash -r
594 command is executed (see below).
595
596 Special Commands
597 Input/output redirection is now permitted for these commands. File
598 descriptor 1 is the default output location. When Job Control is
599 enabled, additional Special Commands are added to the shell's environ‐
600 ment (see Job Control section below).
601
602 :
603
604 No effect; the command does nothing. A zero exit code is returned.
605
606
607 . filename
608
609 Read and execute commands from filename and return. The search path
610 specified by PATH is used to find the directory containing file‐
611 name.
612
613
614 bg [%jobid ...]
615
616 When Job Control is enabled, the bg command is added to the user's
617 environment to manipulate jobs. Resumes the execution of a stopped
618 job in the background. If %jobid is omitted the current job is
619 assumed. (See Job Control section below for more detail.)
620
621
622 break [ n ]
623
624 Exit from the enclosing for or while loop, if any. If n is speci‐
625 fied, break n levels.
626
627
628 cd [ argument ]
629
630 Change the current directory to argument. The shell parameter HOME
631 is the default argument. The shell parameter CDPATH defines the
632 search path for the directory containing argument. Alternative
633 directory names are separated by a colon (:). The default path is
634 <null> (specifying the current directory). Note: The current direc‐
635 tory is specified by a null path name, which can appear immediately
636 after the equal sign or between the colon delimiters anywhere else
637 in the path list. If argument begins with a / the search path is
638 not used. Otherwise, each directory in the path is searched for
639 argument.
640
641
642 chdir [ dir ]
643
644 chdir changes the shell's working directory to directory dir. If no
645 argument is given, change to the home directory of the user. If dir
646 is a relative pathname not found in the current directory, check
647 for it in those directories listed in the CDPATH variable. If dir
648 is the name of a shell variable whose value starts with a /, change
649 to the directory named by that value.
650
651
652 continue [ n ]
653
654 Resume the next iteration of the enclosing for or while loop. If n
655 is specified, resume at the n-th enclosing loop.
656
657
658 echo [ arguments ... ]
659
660 The words in arguments are written to the shell's standard output,
661 separated by space characters. See echo(1) for fuller usage and
662 description.
663
664
665 eval [ argument ... ]
666
667 The arguments are read as input to the shell and the resulting com‐
668 mand(s) executed.
669
670
671 exec [ argument ... ]
672
673 The command specified by the arguments is executed in place of this
674 shell without creating a new process. Input/output arguments can
675 appear and, if no other arguments are given, cause the shell
676 input/output to be modified.
677
678
679 exit [ n ]
680
681 Causes the calling shell or shell script to exit with the exit sta‐
682 tus specified by n. If n is omitted the exit status is that of the
683 last command executed (an EOF also causes the shell to exit.)
684
685
686 export [ name ... ]
687
688 The given names are marked for automatic export to the environment
689 of subsequently executed commands. If no arguments are given, vari‐
690 able names that have been marked for export during the current
691 shell's execution are listed. (Variable names exported from a par‐
692 ent shell are listed only if they have been exported again during
693 the current shell's execution.) Function names are not exported.
694
695
696 fg [%jobid ...]
697
698 When Job Control is enabled, the fg command is added to the user's
699 environment to manipulate jobs. This command resumes the execution
700 of a stopped job in the foreground and also moves an executing
701 background job into the foreground. If %jobid is omitted, the cur‐
702 rent job is assumed. (See Job Control section below for more
703 detail.)
704
705
706 getopts
707
708 Use in shell scripts to support command syntax standards (see
709 Intro(1)). This command parses positional parameters and checks for
710 legal options. See getoptcvt(1) for usage and description.
711
712
713 hash [ -r ] [ name ... ]
714
715 For each name, the location in the search path of the command spec‐
716 ified by name is determined and remembered by the shell. The -r
717 option causes the shell to forget all remembered locations. If no
718 arguments are given, information about remembered commands is pre‐
719 sented. Hits is the number of times a command has been invoked by
720 the shell process. Cost is a measure of the work required to locate
721 a command in the search path. If a command is found in a "relative"
722 directory in the search path, after changing to that directory, the
723 stored location of that command is recalculated. Commands for which
724 this are done are indicated by an asterisk (*) adjacent to the hits
725 information. Cost is incremented when the recalculation is done.
726
727
728 jobs [-p|-l] [%jobid ...]
729 jobs -x command [arguments]
730
731 Reports all jobs that are stopped or executing in the background.
732 If %jobid is omitted, all jobs that are stopped or running in the
733 background are reported. (See Job Control section below for more
734 detail.)
735
736
737 kill [ -sig ] %job ...
738 kill -l
739
740 Sends either the TERM (terminate) signal or the specified signal to
741 the specified jobs or processes. Signals are either given by number
742 or by names (as given in signal.h(3HEAD) stripped of the prefix
743 "SIG" with the exception that SIGCHD is named CHLD). If the signal
744 being sent is TERM (terminate) or HUP (hangup), then the job or
745 process is sent a CONT (continue) signal if it is stopped. The
746 argument job can be the process id of a process that is not a mem‐
747 ber of one of the active jobs. See Job Control section below for a
748 description of the format of job. In the second form, kill -l, the
749 signal numbers and names are listed. (See kill(1)).
750
751
752 login [ argument ... ]
753
754 Equivalent to `exec login argument....' See login(1) for usage and
755 description.
756
757
758 newgrp [ argument ]
759
760 Equivalent to exec newgrp argument. See newgrp(1) for usage and
761 description.
762
763
764 pwd
765
766 Print the current working directory. See pwd(1) for usage and
767 description.
768
769
770 read name ...
771
772 One line is read from the standard input and, using the internal
773 field separator, IFS (normally space or tab), to delimit word
774 boundaries, the first word is assigned to the first name, the sec‐
775 ond word to the second name, and so forth, with leftover words
776 assigned to the last name. Lines can be continued using \newline.
777 Characters other than newline can be quoted by preceding them with
778 a backslash. These backslashes are removed before words are
779 assigned to names, and no interpretation is done on the character
780 that follows the backslash. The return code is 0, unless an EOF is
781 encountered.
782
783
784 readonly [ name ... ]
785
786 The given names are marked readonly and the values of the these
787 names can not be changed by subsequent assignment. If no arguments
788 are given, a list of all readonly names is printed.
789
790
791 return [ n ]
792
793 Causes a function to exit with the return value specified by n. If
794 n is omitted, the return status is that of the last command exe‐
795 cuted.
796
797
798 set [ -aefhkntuvx [ argument ... ] ]
799
800
801 -a Mark variables which are modified or created for export.
802
803
804 -e Exit immediately if a command exits with a non-zero exit sta‐
805 tus.
806
807
808 -f Disable file name generation.
809
810
811 -h Locate and remember function commands as functions are
812 defined (function commands are normally located when the
813 function is executed).
814
815
816 -k All keyword arguments are placed in the environment for a
817 command, not just those that precede the command name.
818
819
820 -n Read commands but do not execute them.
821
822
823 -t Exit after reading and executing one command.
824
825
826 -u Treat unset variables as an error when substituting.
827
828
829 -v Print shell input lines as they are read.
830
831
832 -x Print commands and their arguments as they are executed.
833
834
835 - Do not change any of the flags; useful in setting $1 to −.
836
837 Using + rather than − causes these flags to be turned off. These
838 flags can also be used upon invocation of the shell. The current
839 set of flags can be found in $−. The remaining arguments are posi‐
840 tional parameters and are assigned, in order, to $1, $2, ... If no
841 arguments are given, the values of all names are printed.
842
843
844 shift [ n ]
845
846 The positional parameters from $n+1 ... are renamed $1 ... . If n
847 is not given, it is assumed to be 1.
848
849
850 stop pid ...
851
852 Halt execution of the process number pid. (see ps(1)).
853
854
855 suspend
856
857 Stops the execution of the current shell (but not if it is the
858 login shell).
859
860
861 test
862
863 Evaluate conditional expressions. See test(1) for usage and
864 description.
865
866
867 times
868
869 Print the accumulated user and system times for processes run from
870 the shell.
871
872
873 trap [ argument n [ n2 ... ]]
874
875 The command argument is to be read and executed when the shell
876 receives numeric or symbolic signal(s) (n). (Note: argument is
877 scanned once when the trap is set and once when the trap is taken.)
878 Trap commands are executed in order of signal number or correspond‐
879 ing symbolic names. Any attempt to set a trap on a signal that was
880 ignored on entry to the current shell is ineffective. An attempt to
881 trap on signal 11 (memory fault) produces an error. If argument is
882 absent, all trap(s) n are reset to their original values. If argu‐
883 ment is the null string, this signal is ignored by the shell and by
884 the commands it invokes. If n is 0, the command argument is exe‐
885 cuted on exit from the shell. The trap command with no arguments
886 prints a list of commands associated with each signal number.
887
888
889 type [ name ... ]
890
891 For each name, indicate how it would be interpreted if used as a
892 command name.
893
894
895 ulimit [ [-HS] [-a | -cdfnstv] ]
896 ulimit [ [-HS] [-c | -d | -f | -n | -s | -t | -v] ] limit
897
898 ulimit prints or sets hard or soft resource limits. These limits
899 are described in getrlimit(2).
900
901 If limit is not present, ulimit prints the specified limits. Any
902 number of limits can be printed at one time. The -a option prints
903 all limits.
904
905 If limit is present, ulimit sets the specified limit to limit. The
906 string unlimited requests that the current limit, if any, be
907 removed. Any user can set a soft limit to any value less than or
908 equal to the hard limit. Any user can lower a hard limit. Only a
909 user with appropriate privileges can raise or remove a hard limit.
910 See getrlimit(2).
911
912 The -H option specifies a hard limit. The -S option specifies a
913 soft limit. If neither option is specified, ulimit sets both limits
914 and print the soft limit.
915
916 The following options specify the resource whose limits are to be
917 printed or set. If no option is specified, the file size limit is
918 printed or set.
919
920 -c maximum core file size (in 512-byte blocks)
921
922
923 -d maximum size of data segment or heap (in kbytes)
924
925
926 -f maximum file size (in 512-byte blocks)
927
928
929 -n maximum file descriptor plus 1
930
931
932 -s maximum size of stack segment (in kbytes)
933
934
935 -t maximum CPU time (in seconds)
936
937
938 -v maximum size of virtual memory (in kbytes)
939
940 Run the sysdef(1M) command to obtain the maximum possible limits
941 for your system. The values reported are in hexadecimal, but can be
942 translated into decimal numbers using the bc(1) utility. See
943 swap(1M).)
944
945 As an example of ulimit, to limit the size of a core file dump to 0
946 Megabytes, type the following:
947
948 ulimit -c 0
949
950
951
952
953 umask [ nnn ]
954
955 The user file-creation mask is set to nnn (see umask(1)). If nnn is
956 omitted, the current value of the mask is printed.
957
958
959 unset [ name ... ]
960
961 For each name, remove the corresponding variable or function value.
962 The variables PATH, PS1, PS2, MAILCHECK, and IFS cannot be unset.
963
964
965 wait [ n ]
966
967 Wait for your background process whose process id is n and report
968 its termination status. If n is omitted, all your shell's currently
969 active background processes are waited for and the return code is
970 zero.
971
972
973 Invocation
974 If the shell is invoked through exec(2) and the first character of
975 argument zero is −, commands are initially read from /etc/profile and
976 from $HOME/.profile, if such files exist. Thereafter, commands are read
977 as described below, which is also the case when the shell is invoked as
978 /usr/bin/sh. The flags below are interpreted by the shell on invocation
979 only. Note: Unless the -c or -s flag is specified, the first argument
980 is assumed to be the name of a file containing commands, and the
981 remaining arguments are passed as positional parameters to that command
982 file:
983
984 -c string If the -c flag is present commands are read from string.
985
986
987 -i If the -i flag is present or if the shell input and out‐
988 put are attached to a terminal, this shell is interac‐
989 tive. In this case, TERMINATE is ignored (so that kill 0
990 does not kill an interactive shell) and INTERRUPT is
991 caught and ignored (so that wait is interruptible). In
992 all cases, QUIT is ignored by the shell.
993
994
995 -p If the -p flag is present, the shell does not set the
996 effective user and group IDs to the real user and group
997 IDs.
998
999
1000 -r If the -r flag is present the shell is a restricted
1001 shell (see rsh(1M)).
1002
1003
1004 -s If the -s flag is present or if no arguments remain,
1005 commands are read from the standard input. Any remaining
1006 arguments specify the positional parameters. Shell out‐
1007 put (except for Special Commands) is written to file
1008 descriptor 2.
1009
1010
1011
1012 The remaining flags and arguments are described under the set command
1013 above.
1014
1015 Job Control (jsh)
1016 When the shell is invoked as jsh, Job Control is enabled in addition to
1017 all of the functionality described previously for sh. Typically, Job
1018 Control is enabled for the interactive shell only. Non-interactive
1019 shells typically do not benefit from the added functionality of Job
1020 Control.
1021
1022
1023 With Job Control enabled, every command or pipeline the user enters at
1024 the terminal is called a job. All jobs exist in one of the following
1025 states: foreground, background, or stopped. These terms are defined as
1026 follows:
1027
1028 1. A job in the foreground has read and write access to the
1029 controlling terminal.
1030
1031 2. A job in the background is denied read access and has condi‐
1032 tional write access to the controlling terminal (see
1033 stty(1)).
1034
1035 3. A stopped job is a job that has been placed in a suspended
1036 state, usually as a result of a SIGTSTP signal (see sig‐
1037 nal.h(3HEAD)).
1038
1039
1040 Every job that the shell starts is assigned a positive integer, called
1041 a job number which is tracked by the shell and is used as an identifier
1042 to indicate a specific job. Additionally, the shell keeps track of the
1043 current and previous jobs. The current job is the most recent job to be
1044 started or restarted. The previous job is the first non-current job.
1045
1046
1047 The acceptable syntax for a Job Identifier is of the form:
1048
1049
1050 %jobid
1051
1052
1053 where jobid can be specified in any of the following formats:
1054
1055 % or + For the current job.
1056
1057
1058 − For the previous job.
1059
1060
1061 ?<string> Specify the job for which the command line uniquely con‐
1062 tains string.
1063
1064
1065 n For job number n.
1066
1067
1068 pref Where pref is a unique prefix of the command name. For
1069 example, if the command ls -l name were running in the
1070 background, it could be referred to as %ls. pref cannot
1071 contain blanks unless it is quoted.
1072
1073
1074
1075 When Job Control is enabled, the following commands are added to the
1076 user's environment to manipulate jobs:
1077
1078 bg [%jobid ...]
1079
1080 Resumes the execution of a stopped job in the background. If %jobid
1081 is omitted the current job is assumed.
1082
1083
1084 fg [%jobid ...]
1085
1086 Resumes the execution of a stopped job in the foreground, also
1087 moves an executing background job into the foreground. If %jobid is
1088 omitted the current job is assumed.
1089
1090
1091 jobs [-p|-l] [%jobid ...]
1092 jobs -x command [arguments]
1093
1094 Reports all jobs that are stopped or executing in the background.
1095 If %jobid is omitted, all jobs that are stopped or running in the
1096 background is reported. The following options modify/enhance the
1097 output of jobs:
1098
1099 -l Report the process group ID and working directory of the
1100 jobs.
1101
1102
1103 -p Report only the process group ID of the jobs.
1104
1105
1106 -x Replace any jobid found in command or arguments with the cor‐
1107 responding process group ID, and then execute command passing
1108 it arguments.
1109
1110
1111
1112 kill [ -signal ] %jobid
1113
1114 Builtin version of kill to provide the functionality of the kill
1115 command for processes identified with a jobid.
1116
1117
1118 stop %jobid ...
1119
1120 Stops the execution of a background job(s).
1121
1122
1123 suspend
1124
1125 Stops the execution of the current shell (but not if it is the
1126 login shell).
1127
1128
1129 wait [%jobid ...]
1130
1131 wait builtin accepts a job identifier. If %jobid is omitted wait
1132 behaves as described above under Special Commands.
1133
1134
1135 Large File Behavior
1136 See largefile(5) for the description of the behavior of sh and jsh when
1137 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
1138
1140 Errors detected by the shell, such as syntax errors, cause the shell to
1141 return a non-zero exit status. If the shell is being used non-interac‐
1142 tively execution of the shell file is abandoned. Otherwise, the shell
1143 returns the exit status of the last command executed (see also the exit
1144 command above).
1145
1146 jsh Only
1147 If the shell is invoked as jsh and an attempt is made to exit the shell
1148 while there are stopped jobs, the shell issues one warning:
1149
1150
1151 There are stopped jobs.
1152
1153
1154 This is the only message. If another exit attempt is made, and there
1155 are still stopped jobs they are sent a SIGHUP signal from the kernel
1156 and the shell is exited.
1157
1159 $HOME/.profile
1160
1161
1162 /dev/null
1163
1164
1165 /etc/profile
1166
1167
1168 /tmp/sh*
1169
1171 See attributes(5) for descriptions of the following attributes:
1172
1173 /usr/bin/sh, /usr/bin/jsh
1174 ┌─────────────────────────────┬─────────────────────────────┐
1175 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
1176 ├─────────────────────────────┼─────────────────────────────┤
1177 │Availability │SUNWcsu │
1178 ├─────────────────────────────┼─────────────────────────────┤
1179 │CSI │Enabled │
1180 └─────────────────────────────┴─────────────────────────────┘
1181
1182 /usr/xpg4/bin/sh
1183 ┌─────────────────────────────┬─────────────────────────────┐
1184 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
1185 ├─────────────────────────────┼─────────────────────────────┤
1186 │Availability │SUNWxcu4 │
1187 ├─────────────────────────────┼─────────────────────────────┤
1188 │CSI │Enabled │
1189 └─────────────────────────────┴─────────────────────────────┘
1190
1192 Intro(1), bc(1), echo(1), getoptcvt(1), kill(1), ksh(1), login(1), new‐
1193 grp(1), pfsh(1), pfexec(1), ps(1), pwd(1), set(1), shell_builtins(1),
1194 stty(1), test(1), umask(1), wait(1), rsh(1M), su(1M), swap(1M), sys‐
1195 def(1M), dup(2), exec(2), fork(2), getrlimit(2), pipe(2), ulimit(2),
1196 setlocale(3C), signal.h(3HEAD), passwd(4), profile(4), attributes(5),
1197 environ(5), largefile(5), XPG4(5)
1198
1200 The use of setuid shell scripts is strongly discouraged.
1201
1203 Words used for filenames in input/output redirection are not inter‐
1204 preted for filename generation (see File Name Generation section
1205 above). For example, cat file1 >a* createsa file named a*.
1206
1207
1208 Because commands in pipelines are run as separate processes, variables
1209 set in a pipeline have no effect on the parent shell.
1210
1211
1212 If the input or the output of a while or until loop is redirected, the
1213 commands in the loop are run in a sub-shell, and variables set or
1214 changed there have no effect on the parent process:
1215
1216 lastline=
1217 while read line
1218 do
1219
1220 lastline=$line
1221 done < /etc/passwd
1222 echo "lastline=$lastline" # lastline is empty!
1223
1224
1225
1226
1227 In these cases, the input or output can be redirected by using exec, as
1228 in the following example:
1229
1230 # Save standard input (file descriptor 0) as file
1231 # descriptor 3, and redirect standard input from the file
1232 /etc/passwd:
1233
1234 exec 3<&0 # save standard input as fd 3
1235 exec </etc/passwd # redirect input from file
1236
1237 lastline=
1238 while read line
1239 do
1240 lastline=$line
1241 done
1242
1243 exec 0<&3 # restore standard input
1244 exec 3<&- # close file descriptor 3
1245 echo "$lastline" # lastline
1246
1247
1248
1249
1250 If you get the error message, "cannot fork, too many processes", try
1251 using the wait(1) command to clean up your background processes. If
1252 this doesn't help, the system process table is probably full or you
1253 have too many active foreground processes. There is a limit to the num‐
1254 ber of process ids associated with your login, and to the number the
1255 system can keep track of.
1256
1257
1258 Only the last process in a pipeline can be waited for.
1259
1260
1261 If a command is executed, and a command with the same name is installed
1262 in a directory in the search path before the directory where the origi‐
1263 nal command was found, the shell continues to exec the original com‐
1264 mand. Use the hash command to correct this situation.
1265
1266
1267 The Bourne shell has a limitation on the effective UID for a process.
1268 If this UID is less than 100 (and not equal to the real UID of the
1269 process), then the UID is reset to the real UID of the process.
1270
1271
1272 Because the shell implements both foreground and background jobs in the
1273 same process group, they all receive the same signals, which can lead
1274 to unexpected behavior. It is, therefore, recommended that other job
1275 control shells be used, especially in an interactive environment.
1276
1277
1278 When the shell executes a shell script that attempts to execute a non-
1279 existent command interpreter, the shell returns an erroneous diagnostic
1280 message that the shell script file does not exist.
1281
1282
1283
1284SunOS 5.11 3 Apr 2008 sh(1)