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