1SH(1) BSD General Commands Manual SH(1)
2
4 sh — command interpreter (shell)
5
7 sh [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]
8 [+o option_name] [command_file [argument ...]]
9 sh -c [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]
10 [+o option_name] command_string [command_name [argument ...]]
11 sh -s [-aCefnuvxIimqVEb] [+aCefnuvxIimqVEb] [-o option_name]
12 [+o option_name] [argument ...]
13
15 sh is the standard command interpreter for the system. The current ver‐
16 sion of sh is in the process of being changed to conform with the POSIX
17 1003.2 and 1003.2a specifications for the shell. This version has many
18 features which make it appear similar in some respects to the Korn shell,
19 but it is not a Korn shell clone (see ksh(1)). Only features designated
20 by POSIX, plus a few Berkeley extensions, are being incorporated into
21 this shell. We expect POSIX conformance by the time 4.4 BSD is released.
22 This man page is not intended to be a tutorial or a complete specifica‐
23 tion of the shell.
24
25 Overview
26 The shell is a command that reads lines from either a file or the termi‐
27 nal, interprets them, and generally executes other commands. It is the
28 program that is running when a user logs into the system (although a user
29 can select a different shell with the chsh(1) command). The shell imple‐
30 ments a language that has flow control constructs, a macro facility that
31 provides a variety of features in addition to data storage, along with
32 built in history and line editing capabilities. It incorporates many
33 features to aid interactive use and has the advantage that the interpre‐
34 tative language is common to both interactive and non-interactive use
35 (shell scripts). That is, commands can be typed directly to the running
36 shell or can be put into a file and the file can be executed directly by
37 the shell.
38
39 Invocation
40 If no args are present and if the standard input of the shell is con‐
41 nected to a terminal (or if the -i flag is set), and the -c option is not
42 present, the shell is considered an interactive shell. An interactive
43 shell generally prompts before each command and handles programming and
44 command errors differently (as described below). When first starting,
45 the shell inspects argument 0, and if it begins with a dash ‘-’, the
46 shell is also considered a login shell. This is normally done automati‐
47 cally by the system when the user first logs in. A login shell first
48 reads commands from the files /etc/profile and .profile if they exist.
49 If the environment variable ENV is set on entry to an interactive shell,
50 or is set in the .profile of a login shell, the shell next reads commands
51 from the file named in ENV. Therefore, a user should place commands that
52 are to be executed only at login time in the .profile file, and commands
53 that are executed for every interactive shell inside the ENV file. To
54 set the ENV variable to some file, place the following line in your
55 .profile of your home directory
56
57 ENV=$HOME/.shinit; export ENV
58
59 substituting for “.shinit” any filename you wish.
60
61 If command line arguments besides the options have been specified, then
62 the shell treats the first argument as the name of a file from which to
63 read commands (a shell script), and the remaining arguments are set as
64 the positional parameters of the shell ($1, $2, etc). Otherwise, the
65 shell reads commands from its standard input.
66
67 Argument List Processing
68 All of the single letter options that have a corresponding name can be
69 used as an argument to the -o option. The set -o name is provided next
70 to the single letter option in the description below. Specifying a dash
71 “-” turns the option on, while using a plus “+” disables the option. The
72 following options can be set from the command line or with the set
73 builtin (described later).
74
75 -a allexport Export all variables assigned to.
76
77 -c Read commands from the command_string operand
78 instead of from the standard input. Special
79 parameter 0 will be set from the command_name op‐
80 erand and the positional parameters ($1, $2, etc.)
81 set from the remaining argument operands.
82
83 -C noclobber Don't overwrite existing files with “>”.
84
85 -e errexit If not interactive, exit immediately if any
86 untested command fails. The exit status of a com‐
87 mand is considered to be explicitly tested if the
88 command is used to control an if, elif, while, or
89 until; or if the command is the left hand operand
90 of an “&&” or “||” operator.
91
92 -f noglob Disable pathname expansion.
93
94 -n noexec If not interactive, read commands but do not exe‐
95 cute them. This is useful for checking the syntax
96 of shell scripts.
97
98 -u nounset Write a message to standard error when attempting
99 to expand a variable that is not set, and if the
100 shell is not interactive, exit immediately.
101
102 -v verbose The shell writes its input to standard error as it
103 is read. Useful for debugging.
104
105 -x xtrace Write each command to standard error (preceded by
106 a ‘+ ’) before it is executed. Useful for debug‐
107 ging.
108
109 -I ignoreeof Ignore EOF's from input when interactive.
110
111 -i interactive Force the shell to behave interactively.
112
113 -l Make dash act as if it had been invoked as a login
114 shell.
115
116 -m monitor Turn on job control (set automatically when inter‐
117 active).
118
119 -s stdin Read commands from standard input (set automati‐
120 cally if no file arguments are present). This
121 option has no effect when set after the shell has
122 already started running (i.e. with set).
123
124 -V vi Enable the built-in vi(1) command line editor
125 (disables -E if it has been set).
126
127 -E emacs Enable the built-in emacs(1) command line editor
128 (disables -V if it has been set).
129
130 -b notify Enable asynchronous notification of background job
131 completion. (UNIMPLEMENTED for 4.4alpha)
132
133 Lexical Structure
134 The shell reads input in terms of lines from a file and breaks it up into
135 words at whitespace (blanks and tabs), and at certain sequences of char‐
136 acters that are special to the shell called “operators”. There are two
137 types of operators: control operators and redirection operators (their
138 meaning is discussed later). Following is a list of operators:
139
140 Control operators:
141 & && ( ) ; ;; | || <newline>
142
143 Redirection operators:
144 < > >| << >> <& >& <<- <>
145
146 Quoting
147 Quoting is used to remove the special meaning of certain characters or
148 words to the shell, such as operators, whitespace, or keywords. There
149 are three types of quoting: matched single quotes, matched double quotes,
150 and backslash.
151
152 Backslash
153 A backslash preserves the literal meaning of the following character,
154 with the exception of ⟨newline⟩. A backslash preceding a ⟨newline⟩ is
155 treated as a line continuation.
156
157 Single Quotes
158 Enclosing characters in single quotes preserves the literal meaning of
159 all the characters (except single quotes, making it impossible to put
160 single-quotes in a single-quoted string).
161
162 Double Quotes
163 Enclosing characters within double quotes preserves the literal meaning
164 of all characters except dollarsign ($), backquote (`), and backslash
165 (\). The backslash inside double quotes is historically weird, and
166 serves to quote only the following characters:
167 $ ` " \ <newline>.
168 Otherwise it remains literal.
169
170 Reserved Words
171 Reserved words are words that have special meaning to the shell and are
172 recognized at the beginning of a line and after a control operator. The
173 following are reserved words:
174
175 ! elif fi while case
176 else for then { }
177 do done until if esac
178
179 Their meaning is discussed later.
180
181 Aliases
182 An alias is a name and corresponding value set using the alias(1) builtin
183 command. Whenever a reserved word may occur (see above), and after
184 checking for reserved words, the shell checks the word to see if it
185 matches an alias. If it does, it replaces it in the input stream with
186 its value. For example, if there is an alias called “lf” with the value
187 “ls -F”, then the input:
188
189 lf foobar ⟨return⟩
190
191 would become
192
193 ls -F foobar ⟨return⟩
194
195 Aliases provide a convenient way for naive users to create shorthands for
196 commands without having to learn how to create functions with arguments.
197 They can also be used to create lexically obscure code. This use is dis‐
198 couraged.
199
200 Commands
201 The shell interprets the words it reads according to a language, the
202 specification of which is outside the scope of this man page (refer to
203 the BNF in the POSIX 1003.2 document). Essentially though, a line is
204 read and if the first word of the line (or after a control operator) is
205 not a reserved word, then the shell has recognized a simple command.
206 Otherwise, a complex command or some other special construct may have
207 been recognized.
208
209 Simple Commands
210 If a simple command has been recognized, the shell performs the following
211 actions:
212
213 1. Leading words of the form “name=value” are stripped off and
214 assigned to the environment of the simple command. Redirect‐
215 ion operators and their arguments (as described below) are
216 stripped off and saved for processing.
217
218 2. The remaining words are expanded as described in the section
219 called “Expansions”, and the first remaining word is consid‐
220 ered the command name and the command is located. The remain‐
221 ing words are considered the arguments of the command. If no
222 command name resulted, then the “name=value” variable assign‐
223 ments recognized in item 1 affect the current shell.
224
225 3. Redirections are performed as described in the next section.
226
227 Redirections
228 Redirections are used to change where a command reads its input or sends
229 its output. In general, redirections open, close, or duplicate an exist‐
230 ing reference to a file. The overall format used for redirection is:
231
232 [n] redir-op file
233
234 where redir-op is one of the redirection operators mentioned previously.
235 Following is a list of the possible redirections. The [n] is an optional
236 number, as in ‘3’ (not ‘[3]’), that refers to a file descriptor.
237
238 [n]> file Redirect standard output (or n) to file.
239
240 [n]>| file Same, but override the -C option.
241
242 [n]>> file Append standard output (or n) to file.
243
244 [n]< file Redirect standard input (or n) from file.
245
246 [n1]<&n2 Duplicate standard input (or n1) from file descriptor
247 n2.
248
249 [n]<&- Close standard input (or n).
250
251 [n1]>&n2 Duplicate standard output (or n1) to n2.
252
253 [n]>&- Close standard output (or n).
254
255 [n]<> file Open file for reading and writing on standard input (or
256 n).
257
258 The following redirection is often called a “here-document”.
259
260 [n]<< delimiter
261 here-doc-text ...
262 delimiter
263
264 All the text on successive lines up to the delimiter is saved away and
265 made available to the command on standard input, or file descriptor n if
266 it is specified. If the delimiter as specified on the initial line is
267 quoted, then the here-doc-text is treated literally, otherwise the text
268 is subjected to parameter expansion, command substitution, and arithmetic
269 expansion (as described in the section on “Expansions”). If the operator
270 is “<<-” instead of “<<”, then leading tabs in the here-doc-text are
271 stripped.
272
273 Search and Execution
274 There are three types of commands: shell functions, builtin commands, and
275 normal programs -- and the command is searched for (by name) in that
276 order. They each are executed in a different way.
277
278 When a shell function is executed, all of the shell positional parameters
279 (except $0, which remains unchanged) are set to the arguments of the
280 shell function. The variables which are explicitly placed in the envi‐
281 ronment of the command (by placing assignments to them before the func‐
282 tion name) are made local to the function and are set to the values
283 given. Then the command given in the function definition is executed.
284 The positional parameters are restored to their original values when the
285 command completes. This all occurs within the current shell.
286
287 Shell builtins are executed internally to the shell, without spawning a
288 new process.
289
290 Otherwise, if the command name doesn't match a function or builtin, the
291 command is searched for as a normal program in the file system (as
292 described in the next section). When a normal program is executed, the
293 shell runs the program, passing the arguments and the environment to the
294 program. If the program is not a normal executable file (i.e., if it
295 does not begin with the "magic number" whose ASCII representation is
296 "#!", so execve(2) returns ENOEXEC then) the shell will interpret the
297 program in a subshell. The child shell will reinitialize itself in this
298 case, so that the effect will be as if a new shell had been invoked to
299 handle the ad-hoc shell script, except that the location of hashed com‐
300 mands located in the parent shell will be remembered by the child.
301
302 Note that previous versions of this document and the source code itself
303 misleadingly and sporadically refer to a shell script without a magic
304 number as a "shell procedure".
305
306 Path Search
307 When locating a command, the shell first looks to see if it has a shell
308 function by that name. Then it looks for a builtin command by that name.
309 If a builtin command is not found, one of two things happen:
310
311 1. Command names containing a slash are simply executed without per‐
312 forming any searches.
313
314 2. The shell searches each entry in PATH in turn for the command. The
315 value of the PATH variable should be a series of entries separated
316 by colons. Each entry consists of a directory name. The current
317 directory may be indicated implicitly by an empty directory name, or
318 explicitly by a single period.
319
320 Command Exit Status
321 Each command has an exit status that can influence the behaviour of other
322 shell commands. The paradigm is that a command exits with zero for nor‐
323 mal or success, and non-zero for failure, error, or a false indication.
324 The man page for each command should indicate the various exit codes and
325 what they mean. Additionally, the builtin commands return exit codes, as
326 does an executed shell function.
327
328 If a command consists entirely of variable assignments then the exit sta‐
329 tus of the command is that of the last command substitution if any, oth‐
330 erwise 0.
331
332 Complex Commands
333 Complex commands are combinations of simple commands with control opera‐
334 tors or reserved words, together creating a larger complex command. More
335 generally, a command is one of the following:
336
337 · simple command
338
339 · pipeline
340
341 · list or compound-list
342
343 · compound command
344
345 · function definition
346
347 Unless otherwise stated, the exit status of a command is that of the last
348 simple command executed by the command.
349
350 Pipelines
351 A pipeline is a sequence of one or more commands separated by the control
352 operator |. The standard output of all but the last command is connected
353 to the standard input of the next command. The standard output of the
354 last command is inherited from the shell, as usual.
355
356 The format for a pipeline is:
357
358 [!] command1 [| command2 ...]
359
360 The standard output of command1 is connected to the standard input of
361 command2. The standard input, standard output, or both of a command is
362 considered to be assigned by the pipeline before any redirection speci‐
363 fied by redirection operators that are part of the command.
364
365 If the pipeline is not in the background (discussed later), the shell
366 waits for all commands to complete.
367
368 If the reserved word ! does not precede the pipeline, the exit status is
369 the exit status of the last command specified in the pipeline. Other‐
370 wise, the exit status is the logical NOT of the exit status of the last
371 command. That is, if the last command returns zero, the exit status is
372 1; if the last command returns greater than zero, the exit status is
373 zero.
374
375 Because pipeline assignment of standard input or standard output or both
376 takes place before redirection, it can be modified by redirection. For
377 example:
378
379 $ command1 2>&1 | command2
380
381 sends both the standard output and standard error of command1 to the
382 standard input of command2.
383
384 A ; or ⟨newline⟩ terminator causes the preceding AND-OR-list (described
385 next) to be executed sequentially; a & causes asynchronous execution of
386 the preceding AND-OR-list.
387
388 Note that unlike some other shells, each process in the pipeline is a
389 child of the invoking shell (unless it is a shell builtin, in which case
390 it executes in the current shell -- but any effect it has on the environ‐
391 ment is wiped).
392
393 Background Commands -- &
394 If a command is terminated by the control operator ampersand (&), the
395 shell executes the command asynchronously -- that is, the shell does not
396 wait for the command to finish before executing the next command.
397
398 The format for running a command in background is:
399
400 command1 & [command2 & ...]
401
402 If the shell is not interactive, the standard input of an asynchronous
403 command is set to /dev/null.
404
405 Lists -- Generally Speaking
406 A list is a sequence of zero or more commands separated by newlines,
407 semicolons, or ampersands, and optionally terminated by one of these
408 three characters. The commands in a list are executed in the order they
409 are written. If command is followed by an ampersand, the shell starts
410 the command and immediately proceed onto the next command; otherwise it
411 waits for the command to terminate before proceeding to the next one.
412
413 Short-Circuit List Operators
414 “&&” and “||” are AND-OR list operators. “&&” executes the first com‐
415 mand, and then executes the second command iff the exit status of the
416 first command is zero. “||” is similar, but executes the second command
417 iff the exit status of the first command is nonzero. “&&” and “||” both
418 have the same priority.
419
420 Flow-Control Constructs -- if, while, for, case
421 The syntax of the if command is
422
423 if list
424 then list
425 [ elif list
426 then list ] ...
427 [ else list ]
428 fi
429
430 The syntax of the while command is
431
432 while list
433 do list
434 done
435
436 The two lists are executed repeatedly while the exit status of the first
437 list is zero. The until command is similar, but has the word until in
438 place of while, which causes it to repeat until the exit status of the
439 first list is zero.
440
441 The syntax of the for command is
442
443 for variable [ in [ word ... ] ]
444 do list
445 done
446
447 The words following in are expanded, and then the list is executed
448 repeatedly with the variable set to each word in turn. Omitting in word
449 ... is equivalent to in "$@".
450
451 The syntax of the break and continue command is
452
453 break [ num ]
454 continue [ num ]
455
456 Break terminates the num innermost for or while loops. Continue contin‐
457 ues with the next iteration of the innermost loop. These are implemented
458 as builtin commands.
459
460 The syntax of the case command is
461
462 case word in
463 pattern) list ;;
464 ...
465 esac
466
467 The pattern can actually be one or more patterns (see Shell Patterns
468 described later), separated by “|” characters.
469
470 Grouping Commands Together
471 Commands may be grouped by writing either
472
473 (list)
474
475 or
476
477 { list; }
478
479 The first of these executes the commands in a subshell. Builtin commands
480 grouped into a (list) will not affect the current shell. The second form
481 does not fork another shell so is slightly more efficient. Grouping com‐
482 mands together this way allows you to redirect their output as though
483 they were one program:
484
485 { printf " hello " ; printf " world\n" ; } > greeting
486
487 Note that “}” must follow a control operator (here, “;”) so that it is
488 recognized as a reserved word and not as another command argument.
489
490 Functions
491 The syntax of a function definition is
492
493 name () command
494
495 A function definition is an executable statement; when executed it
496 installs a function named name and returns an exit status of zero. The
497 command is normally a list enclosed between “{” and “}”.
498
499 Variables may be declared to be local to a function by using a local com‐
500 mand. This should appear as the first statement of a function, and the
501 syntax is
502
503 local [variable | -] ...
504
505 Local is implemented as a builtin command.
506
507 When a variable is made local, it inherits the initial value and exported
508 and readonly flags from the variable with the same name in the surround‐
509 ing scope, if there is one. Otherwise, the variable is initially unset.
510 The shell uses dynamic scoping, so that if you make the variable x local
511 to function f, which then calls function g, references to the variable x
512 made inside g will refer to the variable x declared inside f, not to the
513 global variable named x.
514
515 The only special parameter that can be made local is “-”. Making “-”
516 local any shell options that are changed via the set command inside the
517 function to be restored to their original values when the function
518 returns.
519
520 The syntax of the return command is
521
522 return [exitstatus]
523
524 It terminates the currently executing function. Return is implemented as
525 a builtin command.
526
527 Variables and Parameters
528 The shell maintains a set of parameters. A parameter denoted by a name
529 is called a variable. When starting up, the shell turns all the environ‐
530 ment variables into shell variables. New variables can be set using the
531 form
532
533 name=value
534
535 Variables set by the user must have a name consisting solely of alphabet‐
536 ics, numerics, and underscores - the first of which must not be numeric.
537 A parameter can also be denoted by a number or a special character as
538 explained below.
539
540 Positional Parameters
541 A positional parameter is a parameter denoted by a number (n > 0). The
542 shell sets these initially to the values of its command line arguments
543 that follow the name of the shell script. The set builtin can also be
544 used to set or reset them.
545
546 Special Parameters
547 A special parameter is a parameter denoted by one of the following spe‐
548 cial characters. The value of the parameter is listed next to its char‐
549 acter.
550
551 * Expands to the positional parameters, starting from one.
552 When the expansion occurs within a double-quoted string it
553 expands to a single field with the value of each parameter
554 separated by the first character of the IFS variable, or by
555 a ⟨space⟩ if IFS is unset.
556
557 @ Expands to the positional parameters, starting from one.
558 When the expansion occurs within double-quotes, each posi‐
559 tional parameter expands as a separate argument. If there
560 are no positional parameters, the expansion of @ generates
561 zero arguments, even when @ is double-quoted. What this
562 basically means, for example, is if $1 is “abc” and $2 is
563 “def ghi”, then "$@" expands to the two arguments:
564
565 "abc" "def ghi"
566
567 # Expands to the number of positional parameters.
568
569 ? Expands to the exit status of the most recent pipeline.
570
571 - (Hyphen.) Expands to the current option flags (the single-letter
572 option names concatenated into a string) as specified on
573 invocation, by the set builtin command, or implicitly by the
574 shell.
575
576 $ Expands to the process ID of the invoked shell. A subshell
577 retains the same value of $ as its parent.
578
579 ! Expands to the process ID of the most recent background com‐
580 mand executed from the current shell. For a pipeline, the
581 process ID is that of the last command in the pipeline.
582
583 0 (Zero.) Expands to the name of the shell or shell script.
584
585 Word Expansions
586 This clause describes the various expansions that are performed on words.
587 Not all expansions are performed on every word, as explained later.
588
589 Tilde expansions, parameter expansions, command substitutions, arithmetic
590 expansions, and quote removals that occur within a single word expand to
591 a single field. It is only field splitting or pathname expansion that
592 can create multiple fields from a single word. The single exception to
593 this rule is the expansion of the special parameter @ within double-
594 quotes, as was described above.
595
596 The order of word expansion is:
597
598 1. Tilde Expansion, Parameter Expansion, Command Substitution, Arith‐
599 metic Expansion (these all occur at the same time).
600
601 2. Field Splitting is performed on fields generated by step (1) unless
602 the IFS variable is null.
603
604 3. Pathname Expansion (unless set -f is in effect).
605
606 4. Quote Removal.
607
608 The $ character is used to introduce parameter expansion, command substi‐
609 tution, or arithmetic evaluation.
610
611 Tilde Expansion (substituting a user's home directory)
612 A word beginning with an unquoted tilde character (~) is subjected to
613 tilde expansion. All the characters up to a slash (/) or the end of the
614 word are treated as a username and are replaced with the user's home
615 directory. If the username is missing (as in ~/foobar), the tilde is
616 replaced with the value of the HOME variable (the current user's home
617 directory).
618
619 Parameter Expansion
620 The format for parameter expansion is as follows:
621
622 ${expression}
623
624 where expression consists of all characters until the matching “}”. Any
625 “}” escaped by a backslash or within a quoted string, and characters in
626 embedded arithmetic expansions, command substitutions, and variable
627 expansions, are not examined in determining the matching “}”.
628
629 The simplest form for parameter expansion is:
630
631 ${parameter}
632
633 The value, if any, of parameter is substituted.
634
635 The parameter name or symbol can be enclosed in braces, which are
636 optional except for positional parameters with more than one digit or
637 when parameter is followed by a character that could be interpreted as
638 part of the name. If a parameter expansion occurs inside double-quotes:
639
640 1. Pathname expansion is not performed on the results of the expansion.
641
642 2. Field splitting is not performed on the results of the expansion,
643 with the exception of @.
644
645 In addition, a parameter expansion can be modified by using one of the
646 following formats.
647
648 ${parameter:-word} Use Default Values. If parameter is unset or null,
649 the expansion of word is substituted; otherwise,
650 the value of parameter is substituted.
651
652 ${parameter:=word} Assign Default Values. If parameter is unset or
653 null, the expansion of word is assigned to parame‐
654 ter. In all cases, the final value of parameter is
655 substituted. Only variables, not positional param‐
656 eters or special parameters, can be assigned in
657 this way.
658
659 ${parameter:?[word]} Indicate Error if Null or Unset. If parameter is
660 unset or null, the expansion of word (or a message
661 indicating it is unset if word is omitted) is writ‐
662 ten to standard error and the shell exits with a
663 nonzero exit status. Otherwise, the value of
664 parameter is substituted. An interactive shell
665 need not exit.
666
667 ${parameter:+word} Use Alternative Value. If parameter is unset or
668 null, null is substituted; otherwise, the expansion
669 of word is substituted.
670
671 In the parameter expansions shown previously, use of the colon in the
672 format results in a test for a parameter that is unset or null; omission
673 of the colon results in a test for a parameter that is only unset.
674
675 ${#parameter} String Length. The length in characters of the
676 value of parameter.
677
678 The following four varieties of parameter expansion provide for substring
679 processing. In each case, pattern matching notation (see Shell
680 Patterns), rather than regular expression notation, is used to evaluate
681 the patterns. If parameter is * or @, the result of the expansion is
682 unspecified. Enclosing the full parameter expansion string in double-
683 quotes does not cause the following four varieties of pattern characters
684 to be quoted, whereas quoting characters within the braces has this
685 effect.
686
687 ${parameter%word} Remove Smallest Suffix Pattern. The word is
688 expanded to produce a pattern. The parameter
689 expansion then results in parameter, with the
690 smallest portion of the suffix matched by the pat‐
691 tern deleted.
692
693 ${parameter%%word} Remove Largest Suffix Pattern. The word is
694 expanded to produce a pattern. The parameter
695 expansion then results in parameter, with the
696 largest portion of the suffix matched by the pat‐
697 tern deleted.
698
699 ${parameter#word} Remove Smallest Prefix Pattern. The word is
700 expanded to produce a pattern. The parameter
701 expansion then results in parameter, with the
702 smallest portion of the prefix matched by the pat‐
703 tern deleted.
704
705 ${parameter##word} Remove Largest Prefix Pattern. The word is
706 expanded to produce a pattern. The parameter
707 expansion then results in parameter, with the
708 largest portion of the prefix matched by the pat‐
709 tern deleted.
710
711 Command Substitution
712 Command substitution allows the output of a command to be substituted in
713 place of the command name itself. Command substitution occurs when the
714 command is enclosed as follows:
715
716 $(command)
717
718 or (“backquoted” version):
719
720 `command`
721
722 The shell expands the command substitution by executing command in a sub‐
723 shell environment and replacing the command substitution with the stan‐
724 dard output of the command, removing sequences of one or more ⟨newline⟩s
725 at the end of the substitution. (Embedded ⟨newline⟩s before the end of
726 the output are not removed; however, during field splitting, they may be
727 translated into ⟨space⟩s, depending on the value of IFS and quoting that
728 is in effect.)
729
730 Arithmetic Expansion
731 Arithmetic expansion provides a mechanism for evaluating an arithmetic
732 expression and substituting its value. The format for arithmetic expan‐
733 sion is as follows:
734
735 $((expression))
736
737 The expression is treated as if it were in double-quotes, except that a
738 double-quote inside the expression is not treated specially. The shell
739 expands all tokens in the expression for parameter expansion, command
740 substitution, and quote removal.
741
742 Next, the shell treats this as an arithmetic expression and substitutes
743 the value of the expression.
744
745 White Space Splitting (Field Splitting)
746 After parameter expansion, command substitution, and arithmetic expansion
747 the shell scans the results of expansions and substitutions that did not
748 occur in double-quotes for field splitting and multiple fields can
749 result.
750
751 The shell treats each character of the IFS as a delimiter and uses the
752 delimiters to split the results of parameter expansion and command sub‐
753 stitution into fields.
754
755 Pathname Expansion (File Name Generation)
756 Unless the -f flag is set, file name generation is performed after word
757 splitting is complete. Each word is viewed as a series of patterns, sep‐
758 arated by slashes. The process of expansion replaces the word with the
759 names of all existing files whose names can be formed by replacing each
760 pattern with a string that matches the specified pattern. There are two
761 restrictions on this: first, a pattern cannot match a string containing a
762 slash, and second, a pattern cannot match a string starting with a period
763 unless the first character of the pattern is a period. The next section
764 describes the patterns used for both Pathname Expansion and the case com‐
765 mand.
766
767 Shell Patterns
768 A pattern consists of normal characters, which match themselves, and
769 meta-characters. The meta-characters are “!”, “*”, “?”, and “[”. These
770 characters lose their special meanings if they are quoted. When command
771 or variable substitution is performed and the dollar sign or back quotes
772 are not double quoted, the value of the variable or the output of the
773 command is scanned for these characters and they are turned into meta-
774 characters.
775
776 An asterisk (“*”) matches any string of characters. A question mark
777 matches any single character. A left bracket (“[”) introduces a charac‐
778 ter class. The end of the character class is indicated by a (“]”); if
779 the “]” is missing then the “[” matches a “[” rather than introducing a
780 character class. A character class matches any of the characters between
781 the square brackets. A range of characters may be specified using a
782 minus sign. The character class may be complemented by making an excla‐
783 mation point the first character of the character class.
784
785 To include a “]” in a character class, make it the first character listed
786 (after the “!”, if any). To include a minus sign, make it the first or
787 last character listed.
788
789 Builtins
790 This section lists the builtin commands which are builtin because they
791 need to perform some operation that can't be performed by a separate
792 process. In addition to these, there are several other commands that may
793 be builtin for efficiency (e.g. printf(1), echo(1), test(1), etc).
794
795 :
796
797 true A null command that returns a 0 (true) exit value.
798
799 . file
800 The commands in the specified file are read and executed by the
801 shell.
802
803 alias [name[=string ...]]
804 If name=string is specified, the shell defines the alias name with
805 value string. If just name is specified, the value of the alias
806 name is printed. With no arguments, the alias builtin prints the
807 names and values of all defined aliases (see unalias).
808
809 bg [job] ...
810 Continue the specified jobs (or the current job if no jobs are
811 given) in the background.
812
813 command [-p] [-v] [-V] command [arg ...]
814 Execute the specified command but ignore shell functions when
815 searching for it. (This is useful when you have a shell function
816 with the same name as a builtin command.)
817
818 -p search for command using a PATH that guarantees to find all
819 the standard utilities.
820
821 -V Do not execute the command but search for the command and
822 print the resolution of the command search. This is the
823 same as the type builtin.
824
825 -v Do not execute the command but search for the command and
826 print the absolute pathname of utilities, the name for
827 builtins or the expansion of aliases.
828
829 cd -
830
831 cd [-LP] [directory]
832 Switch to the specified directory (default HOME). If an entry for
833 CDPATH appears in the environment of the cd command or the shell
834 variable CDPATH is set and the directory name does not begin with
835 a slash, then the directories listed in CDPATH will be searched
836 for the specified directory. The format of CDPATH is the same as
837 that of PATH. If a single dash is specified as the argument, it
838 will be replaced by the value of OLDPWD. The cd command will
839 print out the name of the directory that it actually switched to
840 if this is different from the name that the user gave. These may
841 be different either because the CDPATH mechanism was used or
842 because the argument is a single dash. The -P option causes the
843 physical directory structure to be used, that is, all symbolic
844 links are resolved to their respective values. The -L option
845 turns off the effect of any preceding -P options.
846
847 echo [-n] args...
848 Print the arguments on the standard output, separated by spaces.
849 Unless the -n option is present, a newline is output following the
850 arguments.
851
852 If any of the following sequences of characters is encountered
853 during output, the sequence is not output. Instead, the specified
854 action is performed:
855
856 \b A backspace character is output.
857
858 \c Subsequent output is suppressed. This is normally used at
859 the end of the last argument to suppress the trailing new‐
860 line that echo would otherwise output.
861
862 \f Output a form feed.
863
864 \n Output a newline character.
865
866 \r Output a carriage return.
867
868 \t Output a (horizontal) tab character.
869
870 \v Output a vertical tab.
871
872 \0digits
873 Output the character whose value is given by zero to three
874 octal digits. If there are zero digits, a nul character
875 is output.
876
877 \\ Output a backslash.
878
879 All other backslash sequences elicit undefined behaviour.
880
881 eval string ...
882 Concatenate all the arguments with spaces. Then re-parse and exe‐
883 cute the command.
884
885 exec [command arg ...]
886 Unless command is omitted, the shell process is replaced with the
887 specified program (which must be a real program, not a shell
888 builtin or function). Any redirections on the exec command are
889 marked as permanent, so that they are not undone when the exec
890 command finishes.
891
892 exit [exitstatus]
893 Terminate the shell process. If exitstatus is given it is used as
894 the exit status of the shell; otherwise the exit status of the
895 preceding command is used.
896
897 export name ...
898
899 export -p
900 The specified names are exported so that they will appear in the
901 environment of subsequent commands. The only way to un-export a
902 variable is to unset it. The shell allows the value of a variable
903 to be set at the same time it is exported by writing
904
905 export name=value
906
907 With no arguments the export command lists the names of all
908 exported variables. With the -p option specified the output will
909 be formatted suitably for non-interactive use.
910
911 fc [-e editor] [first [last]]
912
913 fc -l [-nr] [first [last]]
914
915 fc -s [old=new] [first]
916 The fc builtin lists, or edits and re-executes, commands previ‐
917 ously entered to an interactive shell.
918
919 -e editor
920 Use the editor named by editor to edit the commands. The
921 editor string is a command name, subject to search via the
922 PATH variable. The value in the FCEDIT variable is used as
923 a default when -e is not specified. If FCEDIT is null or
924 unset, the value of the EDITOR variable is used. If EDITOR
925 is null or unset, ed(1) is used as the editor.
926
927 -l (ell)
928 List the commands rather than invoking an editor on them.
929 The commands are written in the sequence indicated by the
930 first and last operands, as affected by -r, with each com‐
931 mand preceded by the command number.
932
933 -n Suppress command numbers when listing with -l.
934
935 -r Reverse the order of the commands listed (with -l) or
936 edited (with neither -l nor -s).
937
938 -s Re-execute the command without invoking an editor.
939
940 first
941
942 last Select the commands to list or edit. The number of previ‐
943 ous commands that can be accessed are determined by the
944 value of the HISTSIZE variable. The value of first or last
945 or both are one of the following:
946
947 [+]number
948 A positive number representing a command number;
949 command numbers can be displayed with the -l option.
950
951 -number
952 A negative decimal number representing the command
953 that was executed number of commands previously.
954 For example, -1 is the immediately previous command.
955
956 string
957 A string indicating the most recently entered command that
958 begins with that string. If the old=new operand is not
959 also specified with -s, the string form of the first oper‐
960 and cannot contain an embedded equal sign.
961
962 The following environment variables affect the execution of fc:
963
964 FCEDIT Name of the editor to use.
965
966 HISTSIZE The number of previous commands that are accessible.
967
968 fg [job]
969 Move the specified job or the current job to the foreground.
970
971 getopts optstring var
972 The POSIX getopts command, not to be confused with the Bell Labs
973 -derived getopt(1).
974
975 The first argument should be a series of letters, each of which
976 may be optionally followed by a colon to indicate that the option
977 requires an argument. The variable specified is set to the parsed
978 option.
979
980 The getopts command deprecates the older getopt(1) utility due to
981 its handling of arguments containing whitespace.
982
983 The getopts builtin may be used to obtain options and their argu‐
984 ments from a list of parameters. When invoked, getopts places the
985 value of the next option from the option string in the list in the
986 shell variable specified by var and its index in the shell vari‐
987 able OPTIND. When the shell is invoked, OPTIND is initialized to
988 1. For each option that requires an argument, the getopts builtin
989 will place it in the shell variable OPTARG. If an option is not
990 allowed for in the optstring, then OPTARG will be unset.
991
992 optstring is a string of recognized option letters (see
993 getopt(3)). If a letter is followed by a colon, the option is
994 expected to have an argument which may or may not be separated
995 from it by white space. If an option character is not found where
996 expected, getopts will set the variable var to a “?”; getopts will
997 then unset OPTARG and write output to standard error. By specify‐
998 ing a colon as the first character of optstring all errors will be
999 ignored.
1000
1001 A nonzero value is returned when the last option is reached. If
1002 there are no remaining arguments, getopts will set var to the spe‐
1003 cial option, “--”, otherwise, it will set var to “?”.
1004
1005 The following code fragment shows how one might process the argu‐
1006 ments for a command that can take the options [a] and [b], and the
1007 option [c], which requires an argument.
1008
1009 while getopts abc: f
1010 do
1011 case $f in
1012 a | b) flag=$f;;
1013 c) carg=$OPTARG;;
1014 \?) echo $USAGE; exit 1;;
1015 esac
1016 done
1017 shift `expr $OPTIND - 1`
1018
1019 This code will accept any of the following as equivalent:
1020
1021 cmd -acarg file file
1022 cmd -a -c arg file file
1023 cmd -carg -a file file
1024 cmd -a -carg -- file file
1025
1026 hash -rv command ...
1027 The shell maintains a hash table which remembers the locations of
1028 commands. With no arguments whatsoever, the hash command prints
1029 out the contents of this table. Entries which have not been
1030 looked at since the last cd command are marked with an asterisk;
1031 it is possible for these entries to be invalid.
1032
1033 With arguments, the hash command removes the specified commands
1034 from the hash table (unless they are functions) and then locates
1035 them. With the -v option, hash prints the locations of the com‐
1036 mands as it finds them. The -r option causes the hash command to
1037 delete all the entries in the hash table except for functions.
1038
1039 pwd [-LP]
1040 builtin command remembers what the current directory is rather
1041 than recomputing it each time. This makes it faster. However, if
1042 the current directory is renamed, the builtin version of pwd will
1043 continue to print the old name for the directory. The -P option
1044 causes the physical value of the current working directory to be
1045 shown, that is, all symbolic links are resolved to their respec‐
1046 tive values. The -L option turns off the effect of any preceding
1047 -P options.
1048
1049 read [-p prompt] [-r] variable [...]
1050 The prompt is printed if the -p option is specified and the stan‐
1051 dard input is a terminal. Then a line is read from the standard
1052 input. The trailing newline is deleted from the line and the line
1053 is split as described in the section on word splitting above, and
1054 the pieces are assigned to the variables in order. At least one
1055 variable must be specified. If there are more pieces than vari‐
1056 ables, the remaining pieces (along with the characters in IFS that
1057 separated them) are assigned to the last variable. If there are
1058 more variables than pieces, the remaining variables are assigned
1059 the null string. The read builtin will indicate success unless
1060 EOF is encountered on input, in which case failure is returned.
1061
1062 By default, unless the -r option is specified, the backslash “\”
1063 acts as an escape character, causing the following character to be
1064 treated literally. If a backslash is followed by a newline, the
1065 backslash and the newline will be deleted.
1066
1067 readonly name ...
1068
1069 readonly -p
1070 The specified names are marked as read only, so that they cannot
1071 be subsequently modified or unset. The shell allows the value of
1072 a variable to be set at the same time it is marked read only by
1073 writing
1074
1075 readonly name=value
1076
1077 With no arguments the readonly command lists the names of all read
1078 only variables. With the -p option specified the output will be
1079 formatted suitably for non-interactive use.
1080
1081 printf format [arguments ...]
1082 printf formats and prints its arguments, after the first, under
1083 control of the format. The format is a character string which
1084 contains three types of objects: plain characters, which are sim‐
1085 ply copied to standard output, character escape sequences which
1086 are converted and copied to the standard output, and format speci‐
1087 fications, each of which causes printing of the next successive
1088 argument.
1089
1090 The arguments after the first are treated as strings if the corre‐
1091 sponding format is either b, c or s; otherwise it is evaluated as
1092 a C constant, with the following extensions:
1093
1094 · A leading plus or minus sign is allowed.
1095 · If the leading character is a single or double quote,
1096 the value is the ASCII code of the next character.
1097
1098 The format string is reused as often as necessary to satisfy the
1099 arguments. Any extra format specifications are evaluated with
1100 zero or the null string.
1101
1102 Character escape sequences are in backslash notation as defined in
1103 ANSI X3.159-1989 (“ANSI C89”). The characters and their meanings
1104 are as follows:
1105
1106 \a Write a <bell> character.
1107
1108 \b Write a <backspace> character.
1109
1110 \f Write a <form-feed> character.
1111
1112 \n Write a <new-line> character.
1113
1114 \r Write a <carriage return> character.
1115
1116 \t Write a <tab> character.
1117
1118 \v Write a <vertical tab> character.
1119
1120 \\ Write a backslash character.
1121
1122 \num Write an 8-bit character whose ASCII value is the
1123 1-, 2-, or 3-digit octal number num.
1124
1125 Each format specification is introduced by the percent character
1126 (``%''). The remainder of the format specification includes, in
1127 the following order:
1128
1129 Zero or more of the following flags:
1130
1131 # A `#' character specifying that the value should
1132 be printed in an ``alternative form''. For b, c,
1133 d, and s formats, this option has no effect. For
1134 the o format the precision of the number is
1135 increased to force the first character of the out‐
1136 put string to a zero. For the x (X) format, a
1137 non-zero result has the string 0x (0X) prepended
1138 to it. For e, E, f, g, and G formats, the result
1139 will always contain a decimal point, even if no
1140 digits follow the point (normally, a decimal point
1141 only appears in the results of those formats if a
1142 digit follows the decimal point). For g and G
1143 formats, trailing zeros are not removed from the
1144 result as they would otherwise be.
1145
1146 - A minus sign `-' which specifies left adjustment
1147 of the output in the indicated field;
1148
1149 + A `+' character specifying that there should
1150 always be a sign placed before the number when
1151 using signed formats.
1152
1153 ‘ ’ A space specifying that a blank should be left
1154 before a positive number for a signed format. A
1155 `+' overrides a space if both are used;
1156
1157 0 A zero `0' character indicating that zero-padding
1158 should be used rather than blank-padding. A `-'
1159 overrides a `0' if both are used;
1160
1161 Field Width:
1162 An optional digit string specifying a field width; if the
1163 output string has fewer characters than the field width it
1164 will be blank-padded on the left (or right, if the left-
1165 adjustment indicator has been given) to make up the field
1166 width (note that a leading zero is a flag, but an embedded
1167 zero is part of a field width);
1168
1169 Precision:
1170 An optional period, ‘.’, followed by an optional digit
1171 string giving a precision which specifies the number of
1172 digits to appear after the decimal point, for e and f for‐
1173 mats, or the maximum number of characters to be printed
1174 from a string (b and s formats); if the digit string is
1175 missing, the precision is treated as zero;
1176
1177 Format:
1178 A character which indicates the type of format to use (one
1179 of diouxXfwEgGbcs).
1180
1181 A field width or precision may be ‘*’ instead of a digit string.
1182 In this case an argument supplies the field width or precision.
1183
1184 The format characters and their meanings are:
1185
1186 diouXx The argument is printed as a signed decimal (d or i),
1187 unsigned octal, unsigned decimal, or unsigned hexadec‐
1188 imal (X or x), respectively.
1189
1190 f The argument is printed in the style [-]ddd.ddd where
1191 the number of d's after the decimal point is equal to
1192 the precision specification for the argument. If the
1193 precision is missing, 6 digits are given; if the pre‐
1194 cision is explicitly 0, no digits and no decimal point
1195 are printed.
1196
1197 eE The argument is printed in the style [-]d.ddde±dd
1198 where there is one digit before the decimal point and
1199 the number after is equal to the precision specifica‐
1200 tion for the argument; when the precision is missing,
1201 6 digits are produced. An upper-case E is used for an
1202 `E' format.
1203
1204 gG The argument is printed in style f or in style e (E)
1205 whichever gives full precision in minimum space.
1206
1207 b Characters from the string argument are printed with
1208 backslash-escape sequences expanded.
1209 The following additional backslash-escape sequences
1210 are supported:
1211
1212 \c Causes sh to ignore any remaining characters
1213 in the string operand containing it, any
1214 remaining string operands, and any additional
1215 characters in the format operand.
1216
1217 \0num Write an 8-bit character whose ASCII value is
1218 the 1-, 2-, or 3-digit octal number num.
1219
1220 c The first character of argument is printed.
1221
1222 s Characters from the string argument are printed until
1223 the end is reached or until the number of characters
1224 indicated by the precision specification is reached;
1225 if the precision is omitted, all characters in the
1226 string are printed.
1227
1228 % Print a `%'; no argument is used.
1229
1230 In no case does a non-existent or small field width cause trunca‐
1231 tion of a field; padding takes place only if the specified field
1232 width exceeds the actual width.
1233
1234 set [{ -options | +options | -- }] arg ...
1235 The set command performs three different functions.
1236
1237 With no arguments, it lists the values of all shell variables.
1238
1239 If options are given, it sets the specified option flags, or
1240 clears them as described in the section called Argument List
1241 Processing. As a special case, if the option is -o or +o and no
1242 argument is supplied, the shell prints the settings of all its
1243 options. If the option is -o, the settings are printed in a
1244 human-readable format; if the option is +o, the settings are
1245 printed in a format suitable for reinput to the shell to affect
1246 the same option settings.
1247
1248 The third use of the set command is to set the values of the
1249 shell's positional parameters to the specified args. To change
1250 the positional parameters without changing any options, use “--”
1251 as the first argument to set. If no args are present, the set
1252 command will clear all the positional parameters (equivalent to
1253 executing “shift $#”.)
1254
1255 shift [n]
1256 Shift the positional parameters n times. A shift sets the value
1257 of $1 to the value of $2, the value of $2 to the value of $3, and
1258 so on, decreasing the value of $# by one. If n is greater than
1259 the number of positional parameters, shift will issue an error
1260 message, and exit with return status 2.
1261
1262 test expression
1263
1264 [ expression ]
1265 The test utility evaluates the expression and, if it evaluates to
1266 true, returns a zero (true) exit status; otherwise it returns 1
1267 (false). If there is no expression, test also returns 1 (false).
1268
1269 All operators and flags are separate arguments to the test util‐
1270 ity.
1271
1272 The following primaries are used to construct expression:
1273
1274 -b file True if file exists and is a block special file.
1275
1276 -c file True if file exists and is a character special file.
1277
1278 -d file True if file exists and is a directory.
1279
1280 -e file True if file exists (regardless of type).
1281
1282 -f file True if file exists and is a regular file.
1283
1284 -g file True if file exists and its set group ID flag is
1285 set.
1286
1287 -h file True if file exists and is a symbolic link.
1288
1289 -k file True if file exists and its sticky bit is set.
1290
1291 -n string True if the length of string is nonzero.
1292
1293 -p file True if file is a named pipe (FIFO).
1294
1295 -r file True if file exists and is readable.
1296
1297 -s file True if file exists and has a size greater than
1298 zero.
1299
1300 -t file_descriptor
1301 True if the file whose file descriptor number is
1302 file_descriptor is open and is associated with a
1303 terminal.
1304
1305 -u file True if file exists and its set user ID flag is set.
1306
1307 -w file True if file exists and is writable. True indicates
1308 only that the write flag is on. The file is not
1309 writable on a read-only file system even if this
1310 test indicates true.
1311
1312 -x file True if file exists and is executable. True indi‐
1313 cates only that the execute flag is on. If file is
1314 a directory, true indicates that file can be
1315 searched.
1316
1317 -z string True if the length of string is zero.
1318
1319 -L file True if file exists and is a symbolic link. This
1320 operator is retained for compatibility with previous
1321 versions of this program. Do not rely on its exis‐
1322 tence; use -h instead.
1323
1324 -O file True if file exists and its owner matches the effec‐
1325 tive user id of this process.
1326
1327 -G file True if file exists and its group matches the effec‐
1328 tive group id of this process.
1329
1330 -S file True if file exists and is a socket.
1331
1332 file1 -nt file2
1333 True if file1 and file2 exist and file1 is newer
1334 than file2.
1335
1336 file1 -ot file2
1337 True if file1 and file2 exist and file1 is older
1338 than file2.
1339
1340 file1 -ef file2
1341 True if file1 and file2 exist and refer to the same
1342 file.
1343
1344 string True if string is not the null string.
1345
1346 s1 = s2 True if the strings s1 and s2 are identical.
1347
1348 s1 != s2 True if the strings s1 and s2 are not identical.
1349
1350 s1 < s2 True if string s1 comes before s2 based on the ASCII
1351 value of their characters.
1352
1353 s1 > s2 True if string s1 comes after s2 based on the ASCII
1354 value of their characters.
1355
1356 n1 -eq n2 True if the integers n1 and n2 are algebraically
1357 equal.
1358
1359 n1 -ne n2 True if the integers n1 and n2 are not algebraically
1360 equal.
1361
1362 n1 -gt n2 True if the integer n1 is algebraically greater than
1363 the integer n2.
1364
1365 n1 -ge n2 True if the integer n1 is algebraically greater than
1366 or equal to the integer n2.
1367
1368 n1 -lt n2 True if the integer n1 is algebraically less than
1369 the integer n2.
1370
1371 n1 -le n2 True if the integer n1 is algebraically less than or
1372 equal to the integer n2.
1373
1374 These primaries can be combined with the following operators:
1375
1376 ! expression True if expression is false.
1377
1378 expression1 -a expression2
1379 True if both expression1 and expression2 are true.
1380
1381 expression1 -o expression2
1382 True if either expression1 or expression2 are true.
1383
1384 (expression) True if expression is true.
1385
1386 The -a operator has higher precedence than the -o operator.
1387
1388 times Print the accumulated user and system times for the shell and for
1389 processes run from the shell. The return status is 0.
1390
1391 trap [action signal ...]
1392 Cause the shell to parse and execute action when any of the speci‐
1393 fied signals are received. The signals are specified by signal
1394 number or as the name of the signal. If signal is 0, the action
1395 is executed when the shell exits. action may be null, which cause
1396 the specified signals to be ignored. With action omitted or set
1397 to `-' the specified signals are set to their default action.
1398 When the shell forks off a subshell, it resets trapped (but not
1399 ignored) signals to the default action. The trap command has no
1400 effect on signals that were ignored on entry to the shell. trap
1401 without any arguments cause it to write a list of signals and
1402 their associated action to the standard output in a format that is
1403 suitable as an input to the shell that achieves the same trapping
1404 results.
1405
1406 Examples:
1407
1408 trap
1409
1410 List trapped signals and their corresponding action
1411
1412 trap '' INT QUIT tstp 30
1413
1414 Ignore signals INT QUIT TSTP USR1
1415
1416 trap date INT
1417
1418 Print date upon receiving signal INT
1419
1420 type [name ...]
1421 Interpret each name as a command and print the resolution of the
1422 command search. Possible resolutions are: shell keyword, alias,
1423 shell builtin, command, tracked alias and not found. For aliases
1424 the alias expansion is printed; for commands and tracked aliases
1425 the complete pathname of the command is printed.
1426
1427 ulimit [-H | -S] [-a | -tfdscmlpn [value]]
1428 Inquire about or set the hard or soft limits on processes or set
1429 new limits. The choice between hard limit (which no process is
1430 allowed to violate, and which may not be raised once it has been
1431 lowered) and soft limit (which causes processes to be signaled but
1432 not necessarily killed, and which may be raised) is made with
1433 these flags:
1434
1435 -H set or inquire about hard limits
1436
1437 -S set or inquire about soft limits. If neither -H nor
1438 -S is specified, the soft limit is displayed or both
1439 limits are set. If both are specified, the last one
1440 wins.
1441
1442 The limit to be interrogated or set, then, is chosen by specifying
1443 any one of these flags:
1444
1445 -a show all the current limits
1446
1447 -t show or set the limit on CPU time (in seconds)
1448
1449 -f show or set the limit on the largest file that can be
1450 created (in 512-byte blocks)
1451
1452 -d show or set the limit on the data segment size of a
1453 process (in kilobytes)
1454
1455 -s show or set the limit on the stack size of a process
1456 (in kilobytes)
1457
1458 -c show or set the limit on the largest core dump size
1459 that can be produced (in 512-byte blocks)
1460
1461 -m show or set the limit on the total physical memory
1462 that can be in use by a process (in kilobytes)
1463
1464 -l show or set the limit on how much memory a process can
1465 lock with mlock(2) (in kilobytes)
1466
1467 -p show or set the limit on the number of processes this
1468 user can have at one time
1469
1470 -n show or set the limit on the number files a process
1471 can have open at once
1472
1473 If none of these is specified, it is the limit on file size that
1474 is shown or set. If value is specified, the limit is set to that
1475 number; otherwise the current limit is displayed.
1476
1477 Limits of an arbitrary process can be displayed or set using the
1478 sysctl(8) utility.
1479
1480 umask [mask]
1481 Set the value of umask (see umask(2)) to the specified octal
1482 value. If the argument is omitted, the umask value is printed.
1483
1484 unalias [-a] [name]
1485 If name is specified, the shell removes that alias. If -a is
1486 specified, all aliases are removed.
1487
1488 unset [-fv] name ...
1489 The specified variables and functions are unset and unexported.
1490 If -f or -v is specified, the corresponding function or variable
1491 is unset, respectively. If a given name corresponds to both a
1492 variable and a function, and no options are given, only the vari‐
1493 able is unset.
1494
1495 wait [job]
1496 Wait for the specified job to complete and return the exit status
1497 of the last process in the job. If the argument is omitted, wait
1498 for all jobs to complete and the return an exit status of zero.
1499
1500 Command Line Editing
1501 When sh is being used interactively from a terminal, the current command
1502 and the command history (see fc in Builtins) can be edited using vi-mode
1503 command-line editing. This mode uses commands, described below, similar
1504 to a subset of those described in the vi man page. The command ‘set -o
1505 vi’ enables vi-mode editing and place sh into vi insert mode. With vi-
1506 mode enabled, sh can be switched between insert mode and command mode.
1507 The editor is not described in full here, but will be in a later docu‐
1508 ment. It's similar to vi: typing ⟨ESC⟩ will throw you into command VI
1509 command mode. Hitting ⟨return⟩ while in command mode will pass the line
1510 to the shell.
1511
1513 Errors that are detected by the shell, such as a syntax error, will cause
1514 the shell to exit with a non-zero exit status. If the shell is not an
1515 interactive shell, the execution of the shell file will be aborted. Oth‐
1516 erwise the shell will return the exit status of the last command exe‐
1517 cuted, or if the exit builtin is used with a numeric argument, it will
1518 return the argument.
1519
1521 HOME Set automatically by login(1) from the user's login directory
1522 in the password file (passwd(4)). This environment variable
1523 also functions as the default argument for the cd builtin.
1524
1525 PATH The default search path for executables. See the above sec‐
1526 tion Path Search.
1527
1528 CDPATH The search path used with the cd builtin.
1529
1530 MAIL The name of a mail file, that will be checked for the arrival
1531 of new mail. Overridden by MAILPATH.
1532
1533 MAILCHECK The frequency in seconds that the shell checks for the arrival
1534 of mail in the files specified by the MAILPATH or the MAIL
1535 file. If set to 0, the check will occur at each prompt.
1536
1537 MAILPATH A colon “:” separated list of file names, for the shell to
1538 check for incoming mail. This environment setting overrides
1539 the MAIL setting. There is a maximum of 10 mailboxes that can
1540 be monitored at once.
1541
1542 PS1 The primary prompt string, which defaults to “$ ”, unless you
1543 are the superuser, in which case it defaults to “# ”.
1544
1545 PS2 The secondary prompt string, which defaults to “> ”.
1546
1547 PS4 Output before each line when execution trace (set -x) is
1548 enabled, defaults to “+ ”.
1549
1550 IFS Input Field Separators. This is normally set to ⟨space⟩,
1551 ⟨tab⟩, and ⟨newline⟩. See the White Space Splitting section
1552 for more details.
1553
1554 TERM The default terminal setting for the shell. This is inherited
1555 by children of the shell, and is used in the history editing
1556 modes.
1557
1558 HISTSIZE The number of lines in the history buffer for the shell.
1559
1560 PWD The logical value of the current working directory. This is
1561 set by the cd command.
1562
1563 OLDPWD The previous logical value of the current working directory.
1564 This is set by the cd command.
1565
1566 PPID The process ID of the parent process of the shell.
1567
1569 $HOME/.profile
1570
1571 /etc/profile
1572
1574 csh(1), echo(1), getopt(1), ksh(1), login(1), printf(1), test(1),
1575 getopt(3), passwd(5), environ(7), sysctl(8)
1576
1578 A sh command appeared in Version 1 AT&T UNIX. It was, however, unmain‐
1579 tainable so we wrote this one.
1580
1582 Setuid shell scripts should be avoided at all costs, as they are a sig‐
1583 nificant security risk.
1584
1585 PS1, PS2, and PS4 should be subject to parameter expansion before being
1586 displayed.
1587
1588BSD January 19, 2003 BSD