1ksh(1) User Commands ksh(1)
2
3
4
6 ksh, rksh - KornShell, a standard/restricted command and programming
7 language
8
10 /usr/bin/ksh [± abCefhikmnoprstuvx] [± o option]...
11 [arg]...
12
13
14 /usr/bin/ksh -c [± abCefhikmnoprstuvx]
15 [± o option]... command_string
16 [command_name [arg...]]
17
18
19 /usr/xpg4/bin/sh [± abCefhikmnoprstuvx]
20 [± o option]... [arg]...
21
22
23 /usr/xpg4/bin/sh -c [± abCefhikmnoprstuvx]
24 [± o option]... command_string
25 [command_name [arg...]]
26
27
28 /usr/bin/rksh [± abCefhikmnoprstuvx] [± o option]...
29 [arg]...
30
31
32 /usr/bin/rksh -c [± abCefhikmnoprstuvx]
33 [± o option]... command_string
34 [command_name [arg...]]
35
36
38 The /usr/xpg4/bin/sh utility is a standards compliant shell. This util‐
39 ity provides all the functionality of /usr/bin/ksh, except in cases
40 where differences in behavior exist. See Arithmetic Expansions section
41 for details.
42
43
44 /usr/bin/ksh is a command and programming language that executes com‐
45 mands read from a terminal or a file. rksh is a restricted version of
46 the command interpreter ksh; it is used to set up login names and exe‐
47 cution environments whose capabilities are more controlled than those
48 of the standard shell. See the Invocation section for the meaning of
49 arguments to the shell.
50
51 Definitions
52 A metacharacter is one of the following characters:
53
54
55 ; & ( ) | < > NEWLINE SPACE TAB
56
57
58 A blank is a TAB or a SPACE. An identifier is a sequence of letters,
59 digits, or underscores starting with a letter or underscore. Identi‐
60 fiers are used as names for functions and variables. A word is a
61 sequence of characters separated by one or more non-quoted metacharac‐
62 ters.
63
64
65 A command is a sequence of characters in the syntax of the shell lan‐
66 guage. The shell reads each command and carries out the desired action
67 either directly or by invoking separate utilities. A special-command is
68 a command that is carried out by the shell without creating a separate
69 process. Except for documented side effects, most special commands can
70 be implemented as separate utilities.
71
72 Commands
73 A simple-command is a sequence of blank-separated words which can be
74 preceded by a variable assignment list. See Environment. The first word
75 specifies the name of the command to be executed. Except as specified,
76 the remaining words are passed as arguments to the invoked command. The
77 command name is passed as argument 0 (see exec(2)). The value of a sim‐
78 ple-command is its exit status if it terminates normally. If it termi‐
79 nates abnormally due to receipt of a signal, the value is the signal
80 number plus 128. See signal.h(3HEAD) for a list of signal values. Obvi‐
81 ously, normal exit status values 129 to 255 cannot be distinguished
82 from abnormal exit caused by receiving signal numbers 1 to 127.
83
84
85 A pipeline is a sequence of one or more commands separated by |. The
86 standard output of each command but the last is connected by a pipe(2)
87 to the standard input of the next command. Each command is run as a
88 separate process; the shell waits for the last command to terminate.
89 The exit status of a pipeline is the exit status of the last command.
90
91
92 A list is a sequence of one or more pipelines separated by ;, &, &&, or
93 ||, and optionally terminated by ;, &, or |&. Of these five symbols, ;,
94 &, and |& have equal precedence, which is lower than that of && and ||.
95 The symbols && and || also have equal precedence. A semicolon (;)
96 causes sequential execution of the preceding pipeline; an ampersand (&)
97 causes asynchronous execution of the preceding pipeline (that is, the
98 shell does not wait for that pipeline to finish). The symbol |& causes
99 asynchronous execution of the preceding command or pipeline with a two-
100 way pipe established to the parent shell.
101
102
103 The standard input and output of the spawned command can be written to
104 and read from by the parent shell using the -p option of the special
105 commands read and print described in Special Commands. The symbol
106 &&(||) causes the list following it to be executed only if the preced‐
107 ing pipeline returns 0 (or a non-zero) value. An arbitrary number of
108 new-lines can appear in a list, instead of a semicolon, to delimit a
109 command.
110
111
112 A command is either a simple-command or one of the following. Unless
113 otherwise stated, the value returned by a command is that of the last
114 simple-command executed in the command.
115
116 for identifier [ in word ... ] ; do list ; done
117
118 Each time a for command is executed, identifier is set to the next
119 word taken from the in word list. If in word ... is omitted, then
120 the for command executes the do list once for each positional
121 parameter that is set. See Parameter Substitution. Execution ends
122 when there are no more words in the list.
123
124
125 select identifier [ in word ... ] ; do list ; done
126
127 A select command prints to standard error (file descriptor 2), the
128 set of words, each preceded by a number. If in word ... is omitted,
129 then the positional parameters are used instead. See Parameter Sub‐
130 stitution. The PS3 prompt is printed and a line is read from the
131 standard input. If this line consists of the number of one of the
132 listed words, then the value of the variable identifier is set to
133 the word corresponding to this number. If this line is empty the
134 selection list is printed again. Otherwise the value of the vari‐
135 able identifier is set to NULL. (See Blank Interpretation about
136 NULL). The contents of the line read from standard input is saved
137 in the shell variable REPLY. The list is executed for each selec‐
138 tion until a break or EOF is encountered. If the REPLY variable is
139 set to NULL by the execution of list, then the selection list is
140 printed before displaying the PS3 prompt for the next selection.
141
142
143 case word in [ pattern [ | pattern ] ) list ;; ] ... esac
144
145 A case command executes the list associated with the first pattern
146 that matches word. The form of the patterns is the same as that
147 used for file-name generation. See File Name Generation.
148
149
150 if list ; then list ; [ elif list ; then list ; ... ] [ else list ; ]
151 fi
152
153 The list following if is executed and, if it returns an exit status
154 of 0, the list following the first then is executed. Otherwise, the
155 list following elif is executed and, if its value is 0, the list
156 following the next then is executed. Failing that, the else list is
157 executed. If no else list or then list is executed, then the if
158 command returns 0 exit status.
159
160
161 while list ; do list ; done
162 until list ; do list ; done
163
164 A while command repeatedly executes the while list and, if the exit
165 status of the last command in the list is 0, executes the do list;
166 otherwise the loop terminates. If no commands in the do list are
167 executed, then the while command returns 0 exit status. until can
168 be used in place of while to negate the loop termination test.
169
170
171 (list)
172
173 Execute list in a separate environment. If two adjacent open paren‐
174 theses are needed for nesting, a space must be inserted to avoid
175 arithmetic evaluation.
176
177
178 {list}
179
180 list is simply executed. Unlike the metacharacters ( and ), { and }
181 are reserved words and must occur at the beginning of a line or
182 after a ; in order to be recognized.
183
184
185 [[expression]]
186
187 Evaluates expression and returns 0 exit status when expression is
188 true. See Conditional Expressions for a description of expression.
189
190
191 function identifier { list ;}
192 identifier( ) { list ;}
193
194 Define a function which is referenced by identifier. The body of
195 the function is the list of commands between { and }. See Func‐
196 tions.
197
198
199 time pipeline
200
201 The pipeline is executed and the elapsed time as well as the user
202 and system time are printed to standard error.
203
204
205
206 The following reserved words are only recognized as the first word of a
207 command and when not quoted:
208
209 ! if then else elif fi case
210 esac for while until do done { }
211 function select time [[ ]]
212
213
214
215 Comments
216 A word beginning with # causes that word and all the following charac‐
217 ters up to a new-line to be ignored.
218
219 Aliasing
220 The first word of each command is replaced by the text of an alias if
221 an alias for this word has been defined. An alias name consists of any
222 number of characters excluding metacharacters, quoting characters, file
223 expansion characters, parameter and command substitution characters,
224 and =. The replacement string can contain any valid shell script
225 including the metacharacters listed above. The first word of each com‐
226 mand in the replaced text, other than any that are in the process of
227 being replaced, is tested for aliases. If the last character of the
228 alias value is a blank then the word following the alias is also be
229 checked for alias substitution. Aliases can be used to redefine special
230 builtin commands but cannot be used to redefine the reserved words
231 listed above. Aliases can be created, listed, and exported with the
232 alias command and can be removed with the unalias command. Exported
233 aliases remain in effect for scripts invoked by name, but must be
234 reinitialized for separate invocations of the shell. See Invocation. To
235 prevent infinite loops in recursive aliasing, if the shell is not cur‐
236 rently processing an alias of the same name, the word is replaced by
237 the value of the alias; otherwise, it is not be replaced.
238
239
240 Aliasing is performed when scripts are read, not while they are exe‐
241 cuted. Therefore, for an alias to take effect, the alias definition
242 command has to be executed before the command which references the
243 alias is read.
244
245
246 Aliases are frequently used as a short hand for full path names. An
247 option to the aliasing facility allows the value of the alias to be
248 automatically set to the full pathname of the corresponding command.
249 These aliases are called tracked aliases. The value of a tracked alias
250 is defined the first time the corresponding command is looked up and
251 becomes undefined each time the PATH variable is reset. These aliases
252 remain tracked so that the next subsequent reference redefines the
253 value. Several tracked aliases are compiled into the shell. The -h
254 option of the set command makes each referenced command name into a
255 tracked alias.
256
257
258 The following exported aliases are compiled into (and built-in to) the
259 shell but can be unset or redefined:
260
261 autoload='typeset −fu'
262 functions='typeset −f'
263 history='fc −l'
264 integer='typeset −i'
265 nohup='nohup '
266 r='fc −e −'
267
268
269
270
271 An example concerning trailing blank characters and reserved words fol‐
272 lows. If the user types:
273
274 $ alias foo="/bin/ls "
275 $ alias while="/"
276
277
278
279
280 the effect of executing:
281
282 $ while true
283 > do
284 > echo "Hello, World"
285 > done
286
287
288
289
290 is a never-ending sequence of Hello, World strings to the screen. How‐
291 ever, if the user types:
292
293 $ foo while
294
295
296
297
298 the result is an ls listing of /. Since the alias substitution for foo
299 ends in a space character, the next word is checked for alias substitu‐
300 tion. The next word, while, has also been aliased, so it is substituted
301 as well. Since it is not in the proper position as a command word, it
302 is not recognized as a reserved word.
303
304
305 If the user types:
306
307 $ foo; while
308
309
310
311
312 while retains its normal reserved-word properties.
313
314 Tilde Substitution
315 After alias substitution is performed, each word is checked to see if
316 it begins with an unquoted ~. If it does, then the word up to a / is
317 checked to see if it matches a user name. If a match is found, the ~
318 and the matched login name are replaced by the login directory of the
319 matched user. This is called a tilde substitution. If no match is
320 found, the original text is left unchanged. A ~ by itself, or in front
321 of a /, is replaced by $HOME. A ~ followed by a + or − is replaced by
322 $PWD and $OLDPWD, respectively.
323
324
325 In addition, tilde substitution is attempted when the value of a vari‐
326 able assignment begins with a ~.
327
328 Tilde Expansion
329 A tilde-prefix consists of an unquoted tilde character at the beginning
330 of a word, followed by all of the characters preceding the first
331 unquoted slash in the word, or all the characters in the word if there
332 is no slash. In an assignment, multiple tilde-prefixes can be used: at
333 the beginning of the word (that is, following the equal sign of the
334 assignment), following any unquoted colon or both. A tilde-prefix in an
335 assignment is terminated by the first unquoted colon or slash. If none
336 of the characters in the tilde-prefix are quoted, the characters in the
337 tilde-prefix following the tilde are treated as a possible login name
338 from the user database.
339
340
341 A portable login name cannot contain characters outside the set given
342 in the description of the LOGNAME environment variable. If the login
343 name is null (that is, the tilde-prefix contains only the tilde), the
344 tilde-prefix is replaced by the value of the variable HOME. If HOME is
345 unset, the results are unspecified. Otherwise, the tilde-prefix is
346 replaced by a pathname of the home directory associated with the login
347 name obtained using the getpwnam function. If the system does not rec‐
348 ognize the login name, the results are undefined.
349
350
351 Tilde expansion generally occurs only at the beginning of words, but an
352 exception based on historical practice has been included:
353
354 PATH=/posix/bin:~dgk/bin
355
356
357
358 is eligible for tilde expansion because tilde follows a colon and none
359 of the relevant characters is quoted. Consideration was given to pro‐
360 hibiting this behavior because any of the following are reasonable sub‐
361 stitutes:
362
363 PATH=$(printf %s ~karels/bin : ~bostic/bin)
364 for Dir in ~maart/bin ~srb/bin .
365 do
366 PATH=${PATH:+$PATH:}$Dir
367 done
368
369
370
371 With the first command, explicit colons are used for each directory. In
372 all cases, the shell performs tilde expansion on each directory because
373 all are separate words to the shell.
374
375
376 Expressions in operands such as:
377
378 make -k mumble LIBDIR=~chet/lib
379
380
381
382 do not qualify as shell variable assignments and tilde expansion is not
383 performed (unless the command does so itself, which make does not).
384
385
386 The special sequence $~ has been designated for future implementations
387 to evaluate as a means of forcing tilde expansion in any word.
388
389
390 Because of the requirement that the word not be quoted, the following
391 are not equivalent; only the last causes tilde expansion:
392
393 \~hlj/ ~h\lj/ ~"hlj"/ ~hlj\/ ~hlj/
394
395
396
397
398 The results of giving tilde with an unknown login name are undefined
399 because the KornShell ~+ and ~− constructs make use of this condition,
400 but, in general it is an error to give an incorrect login name with
401 tilde. The results of having HOME unset are unspecified because some
402 historical shells treat this as an error.
403
404 Command Substitution
405 The standard output from a command enclosed in parenthesis preceded by
406 a dollar sign (that is, $(command)) or a pair of grave accents (``) can
407 be used as part or all of a word. Trailing new-lines are removed. In
408 the second (archaic) form, the string between the quotes is processed
409 for special quoting characters before the command is executed. See
410 Quoting. The command substitution $(cat file) can be replaced by the
411 equivalent but faster $(<file). Command substitution of most special
412 commands that do not perform input/output redirection are carried out
413 without creating a separate process.
414
415
416 Command substitution allows the output of a command to be substituted
417 in place of the command name itself. Command substitution occurs when
418 the command is enclosed as follows:
419
420 $(command)
421
422
423
424
425 or (backquoted version):
426
427 `command`
428
429
430
431
432 The shell expands the command substitution by executing command in a
433 subshell environment and replacing the command substitution (the text
434 of command plus the enclosing $() or backquotes) with the standard out‐
435 put of the command, removing sequences of one or more newline charac‐
436 ters at the end of the substitution. Embedded newline characters before
437 the end of the output is not be removed; however, they can be treated
438 as field delimiters and eliminated during field splitting, depending on
439 the value of IFS and quoting that is in effect.
440
441
442 Within the backquoted style of command substitution, backslash shall
443 retain its literal meaning, except when followed by:
444
445 $ ` \
446
447
448
449
450 (dollar-sign, backquote, backslash). The search for the matching back‐
451 quote is satisfied by the first backquote found without a preceding
452 backslash. During this search, if a non-escaped backquote is encoun‐
453 tered within a shell comment, a here-document, an embedded command sub‐
454 stitution of the $(command) form, or a quoted string, undefined results
455 occur. A single- or double-quoted string that begins, but does not end,
456 within the `...` sequence produces undefined results.
457
458
459 With the $(command) form, all characters following the open parenthesis
460 to the matching closing parenthesis constitute the command. Any valid
461 shell script can be used for command, except:
462
463 o A script consisting solely of redirections produces unspeci‐
464 fied results.
465
466 o See the restriction on single subshells.
467
468
469 The results of command substitution are not field splitting and path‐
470 name expansion processed for further tilde expansion, parameter expan‐
471 sion, command substitution or arithmetic expansion. If a command sub‐
472 stitution occurs inside double-quotes, it is not be performed on the
473 results of the substitution.
474
475
476 Command substitution can be nested. To specify nesting within the back‐
477 quoted version, the application must precede the inner backquotes with
478 backslashes; for example:
479
480 `\`command\``
481
482
483
484
485 The $() form of command substitution solves a problem of inconsistent
486 behavior when using backquotes. For example:
487
488
489
490
491 ┌───────────────────────────────────────────────────────────┐
492 │ Command Output │
493 ├───────────────────────────────────────────────────────────┤
494 │echo '\$x' \$x │
495 │echo `echo '\$x'` $x │
496 │echo $(echo '\$x') \$x │
497 └───────────────────────────────────────────────────────────┘
498
499
500 Additionally, the backquoted syntax has historical restrictions on the
501 contents of the embedded command. While the new $() form can process
502 any kind of valid embedded script, the backquoted form cannot handle
503 some valid scripts that include backquotes. For example, these other‐
504 wise valid embedded scripts do not work in the left column, but do work
505 on the right:
506
507
508
509
510 ┌───────────────────────────────────────────────────────────┐
511 │echo ` echo $( │
512 │cat <<eeof cat <<eeof │
513 │a here-doc with ` a here-doc with ) │
514 │eof eof │
515 │` ) │
516 │echo ` echo $( │
517 │echo abc # a comment with ` echo abc # a comment with ) │
518 │` ) │
519 │echo ` echo $( │
520 │echo '`' echo ')' │
521 │` ) │
522 └───────────────────────────────────────────────────────────┘
523
524
525 Because of these inconsistent behaviors, the backquoted variety of com‐
526 mand substitution is not recommended for new applications that nest
527 command substitutions or attempt to embed complex scripts.
528
529
530 If the command substitution consists of a single subshell, such as:
531
532 $( (command) )
533
534
535
536
537 a portable application must separate the $( and ( into two tokens (that
538 is, separate them with white space). This is required to avoid any
539 ambiguities with arithmetic expansion.
540
541 Arithmetic Expansion
542 An arithmetic expression enclosed in double parentheses preceded by a
543 dollar sign ( $((arithmetic-expression)) ) is replaced by the value of
544 the arithmetic expression within the double parenthesis. Arithmetic
545 expansion provides a mechanism for evaluating an arithmetic expression
546 and substituting its value. The format for arithmetic expansion is as
547 follows:
548
549 $((expression))
550
551
552
553
554 The expression is treated as if it were in double-quotes, except that a
555 double-quote inside the expression is not treated specially. The shell
556 expands all tokens in the expression for parameter expansion, command
557 substitution and quote removal.
558
559
560 Next, the shell treats this as an arithmetic expression and substitute
561 the value of the expression. The arithmetic expression is processed
562 according to the rules of the ISO C with the following exceptions:
563
564 o Only integer arithmetic is required.
565
566 o The sizeof() operator and the prefix and postfix ++ and −−
567 operators are not required.
568
569 o Selection, iteration, and jump statements are not supported.
570
571 o /usr/bin/ksh and /usr/bin/rksh treat prefix 0 through 9 as
572 decimal constants. See the following examples:
573
574
575
576
577 Command Result in /bin/ksh Result in /usr/xpg4/bin/sh
578 echo $((010+10)) 20 18
579 echo $((019+10)) 29 error
580 [ 10 —le $((011)) ] true false
581
582
583
584 As an extension, the shell can recognize arithmetic expressions beyond
585 those listed. If the expression is invalid, the expansion fails and the
586 shell writes a message to standard error indicating the failure.
587
588
589 A simple example using arithmetic expansion:
590
591 # repeat a command 100 times
592 x=100
593 while [ $x −gt 0 ]
594 do
595 command
596 x=$(($x−1))
597 done
598
599
600 Process Substitution
601 This feature is available in SunOS and only on versions of the UNIX
602 operating system that support the /dev/fd directory for naming open
603 files. Each command argument of the form <(list) or >(list) runs
604 process list asynchronously connected to some file in /dev/fd. The name
605 of this file becomes the argument to the command. If the form with > is
606 selected, then writing on this file provides input for list. If < is
607 used, then the file passed as an argument contains the output of the
608 list process. For example:
609
610 paste <(cut -f1 file1) <(cut -f3 file2) | tee >(process1) >(process2)
611
612
613
614
615 cuts fields 1 and 3 from the files file1 and file2, respectively,
616 pastes the results together, and sends it to the processes process1 and
617 process2, as well as putting it onto the standard output. The file,
618 which is passed as an argument to the command, is a UNIX pipe(2) so
619 programs that expect to lseek(2) on the file does not work.
620
621 Parameter Substitution
622 A parameter is an identifier, one or more digits, or any of the charac‐
623 ters *, @, #, ?, −, $, and !. A variable (a parameter denoted by an
624 identifier) has a value and zero or more attributes. variables can be
625 assigned values and attributes by using the typeset special command.
626 The attributes supported by the shell are described later with the
627 typeset special command. Exported variables pass values and attributes
628 to the environment.
629
630
631 The shell supports a one-dimensional array facility. An element of an
632 array variable is referenced by a subscript. A subscript is denoted by
633 a [, followed by an arithmetic expression, followed by a ]. See Arith‐
634 metic Evaluation. To assign values to an array, use set -A name value
635 .... The value of all subscripts must be in the range of 0 through
636 4095. Arrays need not be declared. Any reference to a variable with a
637 valid subscript is legal and an array is created if necessary. Refer‐
638 encing an array without a subscript is equivalent to referencing the
639 element 0. If an array identifier with subscript * or @ is used, then
640 the value for each of the elements is substituted (separated by a field
641 separator character).
642
643
644 The value of a variable can be assigned by writing:
645
646 name=value [ name=value ] ...
647
648
649
650
651 If the integer attribute, -i, is set for name, the value is subject to
652 arithmetic evaluation.
653
654
655 Positional parameters, parameters denoted by a number, can be assigned
656 values with the set special command. Parameter $0 is set from argument
657 zero when the shell is invoked. If parameter is one or more digits then
658 it is a positional parameter. A positional parameter of more than one
659 digit must be enclosed in braces.
660
661 Parameter Expansion
662 The format for parameter expansion is as follows:
663
664 ${expression}
665
666
667
668
669 where expression consists of all characters until the matching }. Any }
670 escaped by a backslash or within a quoted string, and characters in
671 embedded arithmetic expansions, command substitutions and variable
672 expansions, are not examined in determining the matching }.
673
674
675 The simplest form for parameter expansion is:
676
677 ${parameter}
678
679
680
681
682 The value, if any, of parameter is substituted.
683
684
685 The parameter name or symbol can be enclosed in braces, which are
686 optional except for positional parameters with more than one digit or
687 when parameter is followed by a character that could be interpreted as
688 part of the name. The matching closing brace are determined by counting
689 brace levels, skipping over enclosed quoted strings and command substi‐
690 tutions.
691
692
693 If the parameter name or symbol is not enclosed in braces, the expan‐
694 sion uses the longest valid name whether or not the symbol represented
695 by that name exists. When the shell is scanning its input to determine
696 the boundaries of a name, it is not bound by its knowledge of what
697 names are already defined. For example, if F is a defined shell vari‐
698 able, the command:
699
700 echo $Fred
701
702
703
704
705 does not echo the value of $F followed by red; it selects the longest
706 possible valid name, Fred, which in this case might be unset.
707
708
709 If a parameter expansion occurs inside double-quotes:
710
711 o Pathname expansion is not be performed on the results of the
712 expansion.
713
714 o Field splitting is not performed on the results of the
715 expansion, with the exception of @.
716
717
718 In addition, a parameter expansion can be modified by using one of the
719 following formats. In each case that a value of word is needed (based
720 on the state of parameter), word is subjected to tilde expansion,
721 parameter expansion, command substitution and arithmetic expansion. If
722 word is not needed, it is not expanded. The } character that delimits
723 the following parameter expansion modifications is determined as
724 described previously in this section and in dquote. (For example,
725 ${foo-bar}xyz} would result in the expansion of foo followed by the
726 string xyz} if foo is set, else the string barxyz}).
727
728 ${parameter:−word} Use Default Values. If parameter is unset or
729 null, the expansion of word is substituted.
730 Otherwise, the value of parameter is substi‐
731 tuted.
732
733
734 ${parameter:=word} Assign Default Values. If parameter is unset
735 or null, the expansion of word is assigned to
736 parameter. In all cases, the final value of
737 parameter is substituted. Only variables, not
738 positional parameters or special parameters,
739 can be assigned in this way.
740
741
742 ${parameter:?[word]} Indicate Error if Null or Unset. If parameter
743 is unset or null, the expansion of word (or a
744 message indicating it is unset if word is
745 omitted) is written to standard error and the
746 shell exits with a non-zero exit status. Oth‐
747 erwise, the value of parameter is substituted.
748 An interactive shell need not exit.
749
750
751 ${parameter:+[word]} Use Alternative Value. If parameter is unset
752 or null, null is substituted. Otherwise, the
753 expansion of word is substituted.
754
755
756
757 In the parameter expansions shown previously, use of the colon in the
758 format results in a test for a parameter that is unset or null. Omis‐
759 sion of the colon results in a test for a parameter that is only unset.
760 The following two tables summarize the effect of the colon:
761
762
763
764 │ │
765 │parameter set and not null │parameter set and null
766 ────────────────────┼────────────────────────────┼───────────────────────
767 ${parameter:-word} │substitute parameter │substitute word
768 ────────────────────┼────────────────────────────┼───────────────────────
769 ${parameter−word} │substitute parameter │substitute null
770 ────────────────────┼────────────────────────────┼───────────────────────
771 ${parameter:=word} │substitute parameter │assign word
772 ────────────────────┼────────────────────────────┼───────────────────────
773 ${parameter=word} │substitute parameter │substitute parameter
774 ────────────────────┼────────────────────────────┼───────────────────────
775 ${parameter:?word} │substitute parameter │error, exit
776 ────────────────────┼────────────────────────────┼───────────────────────
777 ${parameter?word} │substitute parameter │substitute null
778 ────────────────────┼────────────────────────────┼───────────────────────
779 ${parameter:+word} │substitute word │substitute null
780 ────────────────────┼────────────────────────────┼───────────────────────
781 ${parameter+word} │substitute word │substitute word
782
783
784
785
786 │
787 │ parameter unset
788 ─────────────────────────────┼─────────────────────────────
789 ${parameter:-word} │substitute word
790 ─────────────────────────────┴─────────────────────────────
791
792
793 ${parameter−word} │substitute word
794 ─────────────────────────────┼─────────────────────────────
795 ${parameter:=word} │assign word
796 ─────────────────────────────┼─────────────────────────────
797 ${parameter=word} │assign null
798 ─────────────────────────────┼─────────────────────────────
799 ${parameter:?word} │error, exit
800 ─────────────────────────────┼─────────────────────────────
801 ${parameter?word} │error,exit
802 ─────────────────────────────┼─────────────────────────────
803 ${parameter:+word} │substitute null
804 ─────────────────────────────┼─────────────────────────────
805 ${parameter+word} │substitute null
806
807
808
809 In all cases shown with "substitute", the expression is replaced with
810 the value shown. In all cases shown with "assign", parameter is
811 assigned that value, which also replaces the expression.
812
813 ${#parameter} String Length. The length in characters of the value
814 of parameter. If parameter is * or @, then all the
815 positional parameters, starting with $1, are substi‐
816 tuted (separated by a field separator character).
817
818
819
820 The following four varieties of parameter expansion provide for sub‐
821 string processing. In each case, pattern matching notation (see pat‐
822 mat), rather than regular expression notation, is used to evaluate the
823 patterns. If parameter is * or @, then all the positional parameters,
824 starting with $1, are substituted (separated by a field separator char‐
825 acter). Enclosing the full parameter expansion string in double-quotes
826 does not cause the following four varieties of pattern characters to be
827 quoted, whereas quoting characters within the braces has this effect.
828
829 ${parameter%word} Remove Smallest Suffix Pattern. The word is
830 expanded to produce a pattern. The parameter
831 expansion then results in parameter, with the
832 smallest portion of the suffix matched by the
833 pattern deleted.
834
835
836 ${parameter%%word} Remove Largest Suffix Pattern. The word is
837 expanded to produce a pattern. The parameter
838 expansion then results in parameter, with the
839 largest portion of the suffix matched by the pat‐
840 tern deleted.
841
842
843 ${parameter#word} Remove Smallest Prefix Pattern. The word is
844 expanded to produce a pattern. The parameter
845 expansion then results in parameter, with the
846 smallest portion of the prefix matched by the
847 pattern deleted.
848
849
850 ${parameter##word} Remove Largest Prefix Pattern. The word is
851 expanded to produce a pattern. The parameter
852 expansion then results in parameter, with the
853 largest portion of the prefix matched by the pat‐
854 tern deleted.
855
856
857
858 Examples:
859
860
861 ${parameter:−word}
862
863
864 In this example, ls is executed only if x is null or unset. (The $(ls)
865 command substitution notation is explained in Command Substitution
866 above.)
867
868 ${x:-$(ls)}
869
870
871
872 ${parameter:=word}
873
874 unset X
875 echo ${X:=abc}
876 abc
877
878
879
880 ${parameter:?word}
881
882 unset posix
883 echo ${posix:?}
884 sh: posix: parameter null or not set
885
886
887
888 ${parameter:+word}
889
890 set a b c
891 echo ${3:+posix}
892 posix
893
894
895
896 ${#parameter}
897
898 HOME=/usr/posix
899 echo ${#HOME}
900 10
901
902
903
904 ${parameter%word}
905
906 x=file.c
907 echo ${x%.c}.o
908 file.o
909
910
911
912 ${parameter%%word}
913
914 x=posix/src/std
915 echo ${x%%/*}
916 posix
917
918
919
920 ${parameter#word}
921
922 x=$HOME/src/cmd
923 echo ${x#$HOME}
924 /src/cmd
925
926
927
928 ${parameter##word}
929
930 x=/one/two/three
931 echo ${x##*/}
932 three
933
934
935 Parameters Set by Shell
936 The following parameters are automatically set by the shell:
937
938 # The number of positional parameters in decimal.
939
940
941 − Flags supplied to the shell on invocation or by the set com‐
942 mand.
943
944
945 ? The decimal value returned by the last executed command.
946
947
948 $ The process number of this shell.
949
950
951 _ Initially, the value of _ is an absolute pathname of the
952 shell or script being executed as passed in the environment.
953 Subsequently it is assigned the last argument of the previ‐
954 ous command. This parameter is not set for commands which
955 are asynchronous. This parameter is also used to hold the
956 name of the matching MAIL file when checking for mail.
957
958
959 ! The process number of the last background command invoked.
960
961
962 ERRNO The value of errno as set by the most recently failed system
963 call. This value is system dependent and is intended for
964 debugging purposes.
965
966
967 LINENO The line number of the current line within the script or
968 function being executed.
969
970
971 OLDPWD The previous working directory set by the cd command.
972
973
974 OPTARG The value of the last option argument processed by the
975 getopts special command.
976
977
978 OPTIND The index of the last option argument processed by the
979 getopts special command.
980
981
982 PPID The process number of the parent of the shell.
983
984
985 PWD The present working directory set by the cd command.
986
987
988 RANDOM Each time this variable is referenced, a random integer,
989 uniformly distributed between 0 and 32767, is generated. The
990 sequence of random numbers can be initialized by assigning a
991 numeric value to RANDOM.
992
993
994 REPLY This variable is set by the select statement and by the read
995 special command when no arguments are supplied.
996
997
998 SECONDS Each time this variable is referenced, the number of seconds
999 since shell invocation is returned. If this variable is
1000 assigned a value, then the value returned upon reference is
1001 the value that was assigned plus the number of seconds since
1002 the assignment.
1003
1004
1005 Variables Used by Shell
1006 The following variables are used by the shell:
1007
1008 CDPATH The search path for the cd command.
1009
1010
1011 COLUMNS If this variable is set, the value is used to define the
1012 width of the edit window for the shell edit modes and
1013 for printing select lists.
1014
1015
1016 EDITOR If the value of this variable ends in emacs, gmacs, or
1017 vi and the VISUAL variable is not set, then the corre‐
1018 sponding option is turned on. See the set special com‐
1019 mand.
1020
1021
1022 ENV This variable, when and only when an interactive shell
1023 is invoked, is subjected to parameter expansion by the
1024 shell and the resulting value is used as a pathname of a
1025 file containing shell commands to execute in the cur‐
1026 rent environment. The file need not be executable. If
1027 the expanded value of ENV is not an absolute pathname,
1028 the results are unspecified. ENV is ignored if the
1029 user's real and effective user IDs or real and effective
1030 group IDs are different.
1031
1032 This variable can be used to set aliases and other items
1033 local to the invocation of a shell. The file referred to
1034 by ENV differs from $HOME/.profile in that .profile is
1035 typically executed at session startup, whereas the ENV
1036 file is executed at the beginning of each shell invoca‐
1037 tion. The ENV value is interpreted in a manner similar
1038 to a dot script, in that the commands are executed in
1039 the current environment and the file needs to be read‐
1040 able, but not executable. However, unlike dot scripts,
1041 no PATH searching is performed. This is used as a guard
1042 against Trojan Horse security breaches.
1043
1044
1045 FCEDIT The default editor name for the fc command.
1046
1047
1048 FPATH The search path for function definitions. By default,
1049 the FPATH directories are searched after the PATH vari‐
1050 able. If an executable file is found, then it is read
1051 and executed in the current environment. FPATH is
1052 searched before PATH when a function with the -u
1053 attribute is referenced. The preset alias autoload
1054 causes a function with the -u attribute to be created.
1055
1056
1057 HISTFILE If this variable is set when the shell is invoked, then
1058 the value is the pathname of the file that is used to
1059 store the command history. See Command re-entry.
1060
1061
1062 HISTSIZE If this variable is set when the shell is invoked, then
1063 the number of previously entered commands that are
1064 accessible by this shell is greater than or equal to
1065 this number. The default is 128.
1066
1067
1068 HOME The default argument (home directory) for the cd com‐
1069 mand.
1070
1071
1072 IFS Internal field separators, normally space, tab, and new-
1073 line that are used to separate command words which
1074 result from command or parameter substitution and for
1075 separating words with the special command read. The
1076 first character of the IFS variable is used to separate
1077 arguments for the $* substitution. See Quoting.
1078
1079
1080 LANG Provide a default value for the internationalization
1081 variables that are unset or null. If any of the interna‐
1082 tionalization variables contains an invalid setting, the
1083 utility behaves as if none of the variables had been
1084 defined.
1085
1086
1087 LC_ALL This variable provides a default value for the LC_*
1088 variables.
1089
1090
1091 LC_COLLATE This variable determines the behavior of range expres‐
1092 sions, equivalence classes and multi-byte character col‐
1093 lating elements within pattern matching.
1094
1095
1096 LC_CTYPE Determines how the shell handles characters. When
1097 LC_CTYPE is set to a valid value, the shell can display
1098 and handle text and filenames containing valid charac‐
1099 ters for that locale. If LC_CTYPE (see environ(5)) is
1100 not set in the environment, the operational behavior of
1101 the shell is determined by the value of the LANG envi‐
1102 ronment variable. If LC_ALL is set, its contents are
1103 used to override both the LANG and the other LC_* vari‐
1104 ables.
1105
1106
1107 LC_MESSAGES This variable determines the language in which messages
1108 should be written.
1109
1110
1111 LINENO This variable is set by the shell to a decimal number
1112 representing the current sequential line number (num‐
1113 bered starting with 1) within a script or function
1114 before it executes each command. If the user unsets or
1115 resets LINENO, the variable can lose its special meaning
1116 for the life of the shell. If the shell is not currently
1117 executing a script or function, the value of LINENO is
1118 unspecified.
1119
1120
1121 LINES If this variable is set, the value is used to determine
1122 the column length for printing select lists. Select
1123 lists print vertically until about two-thirds of LINES
1124 lines are filled.
1125
1126
1127 MAIL If this variable is set to the name of a mail file and
1128 the MAILPATH variable is not set, then the shell informs
1129 the user of arrival of mail in the specified file.
1130
1131
1132 MAILCHECK This variable specifies how often (in seconds) the shell
1133 checks for changes in the modification time of any of
1134 the files specified by the MAILPATH or MAIL variables.
1135 The default value is 600 seconds. When the time has
1136 elapsed the shell checks before issuing the next prompt.
1137
1138
1139 MAILPATH A colon (:) separated list of file names. If this vari‐
1140 able is set, then the shell informs the user of any mod‐
1141 ifications to the specified files that have occurred
1142 within the last MAILCHECK seconds. Each file name can be
1143 followed by a ? and a message that is printed. The mes‐
1144 sage undergoes parameter substitution with the variable
1145 $_ defined as the name of the file that has changed. The
1146 default message is you have mail in $_.
1147
1148
1149 NLSPATH Determine the location of message catalogues for the
1150 processing of LC_MESSAGES.
1151
1152
1153 PATH The search path for commands. See Execution. The user
1154 cannot change PATH if executing under rksh (except in
1155 .profile).
1156
1157
1158 PPID This variable is set by the shell to the decimal process
1159 ID of the process that invoked the shell. In a subshell,
1160 PPID is set to the same value as that of the parent of
1161 the current shell. For example, echo $PPID and (echo
1162 $PPID) would produce the same value.
1163
1164
1165 PS1 The value of this variable is expanded for parameter
1166 substitution to define the primary prompt string which
1167 by default is ``$ ''. The character ! in the primary
1168 prompt string is replaced by the command number. See
1169 Command Re-entry. Two successive occurrences of ! pro‐
1170 duces a single ! when the prompt string is printed.
1171
1172
1173 PS2 Secondary prompt string, by default ``> ''.
1174
1175
1176 PS3 Selection prompt string used within a select loop, by
1177 default ``#? ''.
1178
1179
1180 PS4 The value of this variable is expanded for parameter
1181 substitution and precedes each line of an execution
1182 trace. If omitted, the execution trace prompt is ``+ ''.
1183
1184
1185 PWD Set by the shell to be an absolute pathname of the cur‐
1186 rent working directory, containing no components of type
1187 symbolic link, no components that are dot, and no compo‐
1188 nents that are dot-dot when the shell is initialized. If
1189 an application sets or unsets the value of PWD, the
1190 behaviors of the cd and pwd utilities are unspecified
1191
1192
1193 SHELL The pathname of the shell is kept in the environment. At
1194 invocation, if the basename of this variable is rsh,
1195 rksh, or krsh, then the shell becomes restricted.
1196
1197
1198 TMOUT If set to a value greater than zero, the shell termi‐
1199 nates if a command is not entered within the prescribed
1200 number of seconds after issuing the PS1 prompt. The
1201 shell can be compiled with a maximum bound for this
1202 value which cannot be exceeded.
1203
1204
1205 VISUAL If the value of this variable ends in emacs, gmacs, or
1206 vi, then the corresponding option is turned on. See Spe‐
1207 cial Command set.
1208
1209
1210
1211 The shell gives default values to PATH, PS1, PS2, PS3, PS4, MAILCHECK,
1212 FCEDIT, TMOUT, and IFS, while HOME, SHELL, ENV, and MAIL are not set at
1213 all by the shell (although HOME is set by login(1)). On some systems
1214 MAIL and SHELL are also set by login.
1215
1216 Blank Interpretation
1217 After parameter and command substitution, the results of substitutions
1218 are scanned for the field separator characters (those found in IFS) and
1219 split into distinct arguments where such characters are found. Explicit
1220 null arguments ( "" ) or ('') are retained. Implicit null arguments
1221 (those resulting from parameters that have no values) are removed.
1222
1223 File Name Generation
1224 Following substitution, each command word is scanned for the characters
1225 *, ?, and [ unless the -f option has been set. If one of these charac‐
1226 ters appears, the word is regarded as a pattern. The word is replaced
1227 with lexicographically sorted file names that match the pattern. If no
1228 file name is found that matches the pattern, the word is left
1229 unchanged. When a pattern is used for file name generation, the charac‐
1230 ter period (.) at the start of a file name or immediately following a
1231 /, as well as the character / itself, must be matched explicitly. A
1232 file name beginning with a period is not matched with a pattern with
1233 the period inside parentheses. That is, ls .@(r*) would locate a file
1234 named .restore, but ls @(.r*) would not. In other instances of pattern
1235 matching, the / and . are not treated specially.
1236
1237 * Matches any string, including the null string.
1238
1239
1240 ? Matches any single character.
1241
1242
1243 [...] Matches any one of the enclosed characters. A pair of charac‐
1244 ters separated by − matches any character lexically between
1245 the pair, inclusive. If the first character following the
1246 opening "[" is a "! ", then any character not enclosed is
1247 matched. A − can be included in the character set by putting
1248 it as the first or last character.
1249
1250
1251
1252 A pattern-list is a list of one or more patterns separated from each
1253 other with a |. Composite patterns can be formed with one or more of
1254 the following:
1255
1256 ?(pattern-list) Optionally matches any one of the given patterns.
1257
1258
1259 *(pattern-list) Matches zero or more occurrences of the given pat‐
1260 terns.
1261
1262
1263 +(pattern-list) Matches one or more occurrences of the given pat‐
1264 terns.
1265
1266
1267 @(pattern-list) Matches exactly one of the given patterns.
1268
1269
1270 !(pattern-list) Matches anything, except one of the given patterns.
1271
1272
1273 Quoting
1274 Each of the metacharacters listed above (see Definitions) has a special
1275 meaning to the shell and causes termination of a word unless quoted. A
1276 character can be quoted (that is, made to stand for itself) by preced‐
1277 ing it with a \. The pair \NEWLINE is removed. All characters enclosed
1278 between a pair of single quote marks (' ') are quoted. A single quote
1279 cannot appear within single quotes. Inside double quote marks (""),
1280 parameter and command substitution occur and \ quotes the characters \,
1281 `, ", and $. The meaning of $* and $@ is identical when not quoted or
1282 when used as a parameter assignment value or as a file name. However,
1283 when used as a command argument, $* is equivalent to ``$1d$2d...'',
1284 where d is the first character of the IFS variable, whereas $@ is
1285 equivalent to $1 $2 .... Inside grave quote marks (``), \ quotes the
1286 characters \, ', and $. If the grave quotes occur within double quotes,
1287 then \ also quotes the character ".
1288
1289
1290 The special meaning of reserved words or aliases can be removed by
1291 quoting any character of the reserved word. The recognition of function
1292 names or special command names listed cannot be altered by quoting
1293 them.
1294
1295 Arithmetic Evaluation
1296 An ability to perform integer arithmetic is provided with the special
1297 command let. Evaluations are performed using long arithmetic. Constants
1298 are of the form [ base# ] n where base is a decimal number between two
1299 and thirty-six representing the arithmetic base and n is a number in
1300 that base. If base is omitted then base 10 is used.
1301
1302
1303 An arithmetic expression uses the same syntax, precedence, and associa‐
1304 tivity of expression as the C language. All the integral operators,
1305 other than ++, -;, ?:, and , are supported. Variables can be referenced
1306 by name within an arithmetic expression without using the parameter
1307 substitution syntax. When a variable is referenced, its value is evalu‐
1308 ated as an arithmetic expression.
1309
1310
1311 An internal integer representation of a variable can be specified with
1312 the -i option of the typeset special command. Arithmetic evaluation is
1313 performed on the value of each assignment to a variable with the -i
1314 attribute. If you do not specify an arithmetic base, the first assign‐
1315 ment to the variable determines the arithmetic base. This base is used
1316 when parameter substitution occurs.
1317
1318
1319 Since many of the arithmetic operators require quoting, an alternative
1320 form of the let command is provided. For any command which begins with
1321 a ((, all the characters until a matching )) are treated as a quoted
1322 expression. More precisely, ((...)) is equivalent to let "...".
1323
1324 Prompting
1325 When used interactively, the shell prompts with the parameter expanded
1326 value of PS1 before reading a command. If at any time a new-line is
1327 typed and further input is needed to complete a command, then the sec‐
1328 ondary prompt (that is, the value of PS2) is issued.
1329
1330 Conditional Expressions
1331 A conditional expression is used with the [[ compound command to test
1332 attributes of files and to compare strings. Word splitting and file
1333 name generation are not performed on the words between [[ and ]]. Each
1334 expression can be constructed from one or more of the following unary
1335 or binary expressions:
1336
1337 -a file True, if file exists.
1338
1339
1340 -b file True, if file exists and is a block special file.
1341
1342
1343 -c file True, if file exists and is a character special
1344 file.
1345
1346
1347 -d file True, if file exists and is a directory.
1348
1349
1350 -e file True, if file exists.
1351
1352
1353 -f file True, if file exists and is an ordinary file.
1354
1355
1356 -g file True, if file exists and has its setgid bit set.
1357
1358
1359 -h file True, if file exists and is a symbolic link.
1360
1361
1362 -k file True, if file exists and has its sticky bit set.
1363
1364
1365 -n string True, if length of string is non-zero.
1366
1367
1368 -o option True, if option named option is on.
1369
1370
1371 -p file True, if file exists and is a fifo special file or
1372 a pipe.
1373
1374
1375 -r file True, if file exists and is readable by current
1376 process.
1377
1378
1379 -s file True, if file exists and has size greater than
1380 zero.
1381
1382
1383 -t fildes True, if file descriptor number fildes is open and
1384 associated with a terminal device.
1385
1386
1387 -u file True, if file exists and has its setuid bit set.
1388
1389
1390 -w file True, if file exists and is writable by current
1391 process.
1392
1393
1394 -x file True, if file exists and is executable by current
1395 process. If file exists and is a directory, then
1396 the current process has permission to search in
1397 the directory.
1398
1399
1400 -z string True, if length of string is zero.
1401
1402
1403 -L file True, if file exists and is a symbolic link.
1404
1405
1406 -O file True, if file exists and is owned by the effective
1407 user id of this process.
1408
1409
1410 -G file True, if file exists and its group matches the
1411 effective group id of this process.
1412
1413
1414 -S file True, if file exists and is a socket.
1415
1416
1417 file1 -nt file2 True, if file1 exists and is newer than file2.
1418
1419
1420 file1 -ot file2 True, if file1 exists and is older than file2.
1421
1422
1423 file1 -ef file2 True, if file1 and file2 exist and refer to the
1424 same file.
1425
1426
1427 string True if the string string is not the null string.
1428
1429
1430 string = pattern True, if string matches pattern.
1431
1432
1433 string != pattern True, if string does not match pattern.
1434
1435
1436 string1 < string2 True, if string1 comes before string2 based on
1437 strings interpreted as appropriate to the locale
1438 setting for category LC_COLLATE.
1439
1440
1441 string1 > string2 True, if string1 comes after string2 based on
1442 strings interpreted as appropriate to the locale
1443 setting for category LC_COLLATE.
1444
1445
1446 exp1 -eq exp2 True, if exp1 is equal to exp2.
1447
1448
1449 exp1 -ne exp2 True, if exp1 is not equal to exp2.
1450
1451
1452 exp1 -lt exp2 True, if exp1 is less than exp2.
1453
1454
1455 exp1 -gt exp2 True, if exp1 is greater than exp2.
1456
1457
1458 exp1 -le exp2 True, if exp1 is less than or equal to exp2.
1459
1460
1461 exp1 -ge exp2 True, if exp1 is greater than or equal to exp2.
1462
1463
1464
1465 In each of the above expressions, if file is of the form /dev/fd/n,
1466 where n is an integer, then the test is applied to the open file whose
1467 descriptor number is n.
1468
1469
1470 A compound expression can be constructed from these primitives by using
1471 any of the following, listed in decreasing order of precedence.
1472
1473 (expression) True, if expression is true. Used to
1474 group expressions.
1475
1476
1477 ! expression True if expression is false.
1478
1479
1480 expression1 && expression2 True, if expression1 and expression2 are
1481 both true.
1482
1483
1484 expression1 || expression2 True, if either expression1 or expres‐
1485 sion2 is true.
1486
1487
1488 Input/Output
1489 Before a command is executed, its input and output can be redirected
1490 using a special notation interpreted by the shell. The following can
1491 appear anywhere in a simple-command or can precede or follow a command
1492 and are not passed on to the invoked command. Command and parameter
1493 substitution occur before word or digit is used except as noted. File
1494 name generation occurs only if the pattern matches a single file, and
1495 blank interpretation is not performed.
1496
1497 <word Use file word as standard input (file descriptor 0).
1498
1499
1500 >word Use file word as standard output (file descriptor 1). If
1501 the file does not exist then it is created. If the file
1502 exists, and the -noclobber option is on, this causes an
1503 error; otherwise, it is truncated to zero length.
1504
1505
1506 >|word Sames as >, except that it overrides the -noclobber
1507 option.
1508
1509
1510 >>word Use file word as standard output. If the file exists,
1511 output is appended to it (by first seeking to the EOF).
1512 Otherwise, the file is created.
1513
1514
1515 <>word Open file word for reading and writing as standard input.
1516
1517
1518 << [-]word The shell input is read up to a line that is the same as
1519 word, or to an EOF. No parameter substitution, command
1520 substitution, or file name generation is performed on
1521 word. The resulting document, called a here-document,
1522 becomes the standard input. If any character of word is
1523 quoted, no interpretation is placed upon the characters
1524 of the document. Otherwise, parameter and command substi‐
1525 tution occur, \NEWLINE is ignored, and \ must be used to
1526 quote the characters \, $, `, and the first character of
1527 word. If − is appended to <<, then all leading tabs are
1528 stripped from word and from the document.
1529
1530
1531 <&digit The standard input is duplicated from file descriptor
1532 digit (see dup(2)). Similarly for the standard output
1533 using >&digit.
1534
1535
1536 <&− The standard input is closed. Similarly for the standard
1537 output using >&−.
1538
1539
1540 <&p The input from the co-process is moved to standard input.
1541
1542
1543 >&p The output to the co-process is moved to standard output.
1544
1545
1546
1547 If one of the above is preceded by a digit, then the file descriptor
1548 number referred to is that specified by the digit (instead of the
1549 default 0 or 1). For example:
1550
1551 ... 2>&1
1552
1553
1554
1555
1556 means file descriptor 2 is to be opened for writing as a duplicate of
1557 file descriptor 1.
1558
1559
1560 The order in which redirections are specified is significant. The shell
1561 evaluates each redirection in terms of the (file descriptor, file)
1562 association at the time of evaluation. For example:
1563
1564 ... 1>fname 2>&1
1565
1566
1567
1568
1569 first associates file descriptor 1 with file fname. It then associates
1570 file descriptor 2 with the file associated with file descriptor 1 (that
1571 is, fname). If the order of redirections were reversed, file descriptor
1572 2 would be associated with the terminal (assuming file descriptor 1 had
1573 been) and then file descriptor 1 would be associated with file fname.
1574
1575
1576 If a command is followed by & and job control is not active, then the
1577 default standard input for the command is the empty file /dev/null.
1578 Otherwise, the environment for the execution of a command contains the
1579 file descriptors of the invoking shell as modified by input/output
1580 specifications.
1581
1582 Environment
1583 The environment (see environ(5)) is a list of name-value pairs that is
1584 passed to an executed program in the same way as a normal argument
1585 list. The names must be identifiers and the values are character
1586 strings. The shell interacts with the environment in several ways. On
1587 invocation, the shell scans the environment and creates a variable for
1588 each name found, giving it the corresponding value and marking it
1589 export. Executed commands inherit the environment. If the user modifies
1590 the values of these variables or creates new ones, using the export or
1591 typeset -x commands, they become part of the environment. The environ‐
1592 ment seen by any executed command is thus composed of any name-value
1593 pairs originally inherited by the shell, whose values can be modified
1594 by the current shell, plus any additions which must be noted in export
1595 or typeset -x commands.
1596
1597
1598 The environment for any simple-command or function can be augmented by
1599 prefixing it with one or more variable assignments. A variable assign‐
1600 ment argument is a word of the form identifier=value. Thus:
1601
1602 TERM=450 cmd args
1603
1604
1605
1606
1607 and
1608
1609 (export TERM; TERM=450; cmd args)
1610
1611
1612
1613
1614 are equivalent (as far as the above execution of cmd is concerned,
1615 except for special commands listed that are preceded with an asterisk).
1616
1617
1618 If the -k flag is set, all variable assignment arguments are placed in
1619 the environment, even if they occur after the command name. The follow‐
1620 ing first prints a=b c and then c:
1621
1622 echo a=b c
1623 set −k echo
1624 a=b c
1625
1626
1627
1628
1629 This feature is intended for use with scripts written for early ver‐
1630 sions of the shell and its use in new scripts is strongly discouraged.
1631 It is likely to disappear someday.
1632
1633 Functions
1634 The function reserved word, described in the Commands section above, is
1635 used to define shell functions. Shell functions are read in and stored
1636 internally. Alias names are resolved when the function is read. Func‐
1637 tions are executed like commands with the arguments passed as posi‐
1638 tional parameters. See Execution.
1639
1640
1641 Functions execute in the same process as the caller and share all files
1642 and present working directory with the caller. Traps caught by the
1643 caller are reset to their default action inside the function. A trap
1644 condition that is not caught or ignored by the function causes the
1645 function to terminate and the condition to be passed on to the caller.
1646
1647
1648 A trap on EXIT set inside a function is executed after the function
1649 completes in the environment of the caller. This is true only for non-
1650 POSIX-style functions, that is, functions declared as
1651
1652 function func
1653
1654
1655
1656 as opposed to POSIX-style functions, declared as
1657
1658 func()
1659
1660
1661
1662 Ordinarily, variables are shared between the calling program and the
1663 function. However, the typeset special command used within a function
1664 defines local variables whose scope includes the current function and
1665 all functions it calls.
1666
1667
1668 The special command return is used to return from function calls.
1669 Errors within functions return control to the caller.
1670
1671
1672 The names of all functions can be listed with typeset-f. typeset -f
1673 lists all function names as well as the text of all functions. typeset
1674 -f function-names lists the text of the named functions only. Functions
1675 can be undefined with the -f option of the unset special command.
1676
1677
1678 Ordinarily, functions are unset when the shell executes a shell script.
1679 The -xf option of the typeset command allows a function to be exported
1680 to scripts that are executed without a separate invocation of the
1681 shell. Functions that need to be defined across separate invocations of
1682 the shell should be specified in the ENV file with the -xf option of
1683 typeset.
1684
1685 Function Definition Command
1686 A function is a user-defined name that is used as a simple command to
1687 call a compound command with new positional parameters. A function is
1688 defined with a function definition command.
1689
1690
1691 The format of a function definition command is as follows:
1692
1693 fname() compound-command[io-redirect ...]
1694
1695
1696
1697
1698 The function is named fname; it must be a name. An implementation can
1699 allow other characters in a function name as an extension. The imple‐
1700 mentation maintains separate name spaces for functions and variables.
1701
1702
1703 The () in the function definition command consists of two operators.
1704 Therefore, intermixing blank characters with the fname, (, and ) is
1705 allowed, but unnecessary.
1706
1707
1708 The argument compound-command represents a compound command.
1709
1710
1711 When the function is declared, none of the expansions in wordexp is
1712 performed on the text in compound-command or io-redirect; all expan‐
1713 sions is performed as normal each time the function is called. Simi‐
1714 larly, the optional io-redirect redirections and any variable assign‐
1715 ments within compound-command is performed during the execution of the
1716 function itself, not the function definition.
1717
1718
1719 When a function is executed, it has the syntax-error and variable-
1720 assignment properties described for the special built-in utilities.
1721
1722
1723 The compound-command is executed whenever the function name is speci‐
1724 fied as the name of a simple command The operands to the command tempo‐
1725 rarily becomes the positional parameters during the execution of the
1726 compound-command; the special parameter # is also changed to reflect
1727 the number of operands. The special parameter 0 is unchanged. When the
1728 function completes, the values of the positional parameters and the
1729 special parameter # is restored to the values they had before the func‐
1730 tion was executed. If the special built-in return is executed in the
1731 compound-command, the function completes and execution resumes with the
1732 next command after the function call.
1733
1734
1735 An example of how a function definition can be used wherever a simple
1736 command is allowed:
1737
1738 # If variable i is equal to "yes",
1739 # define function foo to be ls −l
1740 #
1741 [ "$i" = yes ] && foo() {
1742 ls −l
1743 }
1744
1745
1746
1747
1748 The exit status of a function definition is 0 if the function was
1749 declared successfully; otherwise, it is greater than zero. The exit
1750 status of a function invocation is the exit status of the last command
1751 executed by the function.
1752
1753 Jobs
1754 If the monitor option of the set command is turned on, an interactive
1755 shell associates a job with each pipeline. It keeps a table of current
1756 jobs, printed by the jobs command, and assigns them small integer num‐
1757 bers. When a job is started asynchronously with &, the shell prints a
1758 line which looks like:
1759
1760 [1] 1234
1761
1762
1763
1764
1765 indicating that the job, which was started asynchronously, was job num‐
1766 ber 1 and had one (top-level) process, whose process id was 1234.
1767
1768
1769 If you are running a job and wish to do something else you can press
1770 the key ^Z (Control-Z) which sends a STOP signal to the current job.
1771 The shell normally indicates that the job has been `Stopped', and print
1772 another prompt. You can then manipulate the state of this job, putting
1773 it in the background with the bg command, or run some other commands
1774 and then eventually bring the job back into the foreground with the
1775 foreground command fg. A ^Z takes effect immediately and is like an
1776 interrupt in that pending output and unread input are discarded when it
1777 is typed.
1778
1779
1780 A job being run in the background stops if it tries to read from the
1781 terminal. Background jobs are normally allowed to produce output, but
1782 this can be disabled by giving the command "stty tostop". If you set
1783 this tty option, then background jobs stop when they try to produce
1784 output as they do when they try to read input.
1785
1786
1787 There are several ways to refer to jobs in the shell. A job can be
1788 referred to by the process id of any process of the job or by one of
1789 the following:
1790
1791 %number The job with the given number.
1792
1793
1794 %string Any job whose command line begins with string.
1795
1796
1797 %?string Any job whose command line contains string.
1798
1799
1800 %% Current job.
1801
1802
1803 %+ Equivalent to %%.
1804
1805
1806 %− Previous job.
1807
1808
1809
1810 The shell learns immediately whenever a process changes state. It nor‐
1811 mally informs you whenever a job becomes blocked so that no further
1812 progress is possible, but only just before it prints a prompt. This is
1813 done so that it does not otherwise disturb your work.
1814
1815
1816 When the monitor mode is on, each background job that completes trig‐
1817 gers any trap set for CHLD.
1818
1819
1820 When you try to leave the shell while jobs are running or stopped, you
1821 are warned with the message, `You have stopped(running) jobs.' You can
1822 use the jobs command to see what they are. If you do this or immedi‐
1823 ately try to exit again, the shell does not warn you a second time, and
1824 the stopped jobs is terminated. If you have jobs running for which the
1825 nohup command was invoked and attempt to logout, you are warned with
1826 the message:
1827
1828
1829 You have jobs running.
1830
1831
1832 You need to logout a second time to actually logout. However, your
1833 background jobs continue to run.
1834
1835 Signals
1836 The INT and QUIT signals for an invoked command are ignored if the com‐
1837 mand is followed by & and the -monitor option is not active. Otherwise,
1838 signals have the values inherited by the shell from its parent. See the
1839 trap special command section.
1840
1841 Execution
1842 Each time a command is executed, the above substitutions are carried
1843 out. If the command name matches one of the Special Commands listed, it
1844 is executed within the current shell process. Next, the command name is
1845 checked to see if it matches one of the user defined functions. If it
1846 does, the positional parameters are saved and then reset to the argu‐
1847 ments of the function call. When the function completes or issues a
1848 return, the positional parameter list is restored and any trap set on
1849 EXIT within the function is executed. The value of a function is the
1850 value of the last command executed. A function is also executed in the
1851 current shell process. If a command name is not a special command or a
1852 user defined function, a process is created and an attempt is made to
1853 execute the command using exec(2).
1854
1855
1856 The shell variable PATH defines the search path for the directory con‐
1857 taining the command. Alternative directory names are separated by a
1858 colon (:). The default path is /bin:/usr/bin: (specifying /bin,
1859 /usr/bin, and the current directory in that order). The current direc‐
1860 tory can be specified by two or more adjacent colons, or by a colon at
1861 the beginning or end of the path list. If the command name contains a /
1862 then the search path is not used. Otherwise, each directory in the path
1863 is searched for an executable file. If the file has execute permission
1864 but is not a directory or an a.out file, it is assumed to be a file
1865 containing shell commands. A sub-shell is spawned to read it. All non-
1866 exported aliases, functions, and variables are removed in this case. A
1867 parenthesized command is executed in a sub-shell without removing non-
1868 exported quantities.
1869
1870 Command Re-entry
1871 The text of the last HISTSIZE (default 128) commands entered from a
1872 terminal device is saved in a history file. The file $HOME/.sh_history
1873 is used if the HISTFILE variable is not set or if the file it names is
1874 not writable. A shell can access the commands of all interactive shells
1875 which use the same named HISTFILE. The special command fc is used to
1876 list or edit a portion of this file. The portion of the file to be
1877 edited or listed can be selected by number or by giving the first char‐
1878 acter or characters of the command. A single command or range of com‐
1879 mands can be specified. If you do not specify an editor program as an
1880 argument to fc then the value of the variable FCEDIT is used. If FCEDIT
1881 is not defined, then /bin/ed is used. The edited command(s) is printed
1882 and re-executed upon leaving the editor. The editor name − is used to
1883 skip the editing phase and to re-execute the command. In this case a
1884 substitution parameter of the form old=new can be used to modify the
1885 command before execution. For example, if r is aliased to 'fc -e -'
1886 then typing 'r bad=good c' re-executes the most recent command which
1887 starts with the letter c, replacing the first occurrence of the string
1888 bad with the string good.
1889
1890 In-line Editing Option
1891 Normally, each command line entered from a terminal device is simply
1892 typed followed by a new-line (RETURN or LINEFEED). If either the emacs,
1893 gmacs, or vi option is active, the user can edit the command line. To
1894 be in either of these edit modes set the corresponding option. An edit‐
1895 ing option is automatically selected each time the VISUAL or EDITOR
1896 variable is assigned a value ending in either of these option names.
1897
1898
1899 The editing features require that the user's terminal accept RETURN as
1900 carriage return without line feed and that a space must overwrite the
1901 current character on the screen.
1902
1903
1904 The editing modes implement a concept where the user is looking through
1905 a window at the current line. The window width is the value of COLUMNS
1906 if it is defined, otherwise 80. If the window width is too small to
1907 display the prompt and leave at least 8 columns to enter input, the
1908 prompt is truncated from the left. If the line is longer than the win‐
1909 dow width minus two, a mark is displayed at the end of the window to
1910 notify the user. As the cursor moves and reaches the window boundaries
1911 the window are centered about the cursor. The mark is a > if the line
1912 extends on the right side of the window, < if the line extends on the
1913 left, and * if the line extends on both sides of the window.
1914
1915
1916 The search commands in each edit mode provide access to the history
1917 file. Only strings are matched, not patterns, although a leading caret
1918 (^) in the string restricts the match to begin at the first character
1919 in the line.
1920
1921 emacs Editing Mode
1922 This mode is entered by enabling either the emacs or gmacs option. The
1923 only difference between these two modes is the way they handle ^T. To
1924 edit, move the cursor to the point needing correction and then insert
1925 or delete characters or words as needed. All the editing commands are
1926 control characters or escape sequences. The notation for control char‐
1927 acters is caret ( ^ ) followed by the character. For example, ^F is the
1928 notation for control F. This is entered by depressing `f' while holding
1929 down the CTRL (control) key. The SHIFT key is not depressed. (The nota‐
1930 tion ^? indicates the DEL (delete) key.)
1931
1932
1933 The notation for escape sequences is M- followed by a character. For
1934 example, M-f (pronounced Meta f) is entered by depressing ESC (ascii
1935 033) followed by `f'. (M-F would be the notation for ESC followed by
1936 SHIFT (capital) `F'.)
1937
1938
1939 All edit commands operate from any place on the line (not just at the
1940 beginning). Neither the RETURN nor the LINEFEED key is entered after
1941 edit commands except when noted.
1942
1943 ^F Move cursor forward (right) one character.
1944
1945
1946 M-f Move cursor forward one word. (The emacs editor's idea of
1947 a word is a string of characters consisting of only let‐
1948 ters, digits and underscores.)
1949
1950
1951 ^B Move cursor backward (left) one character.
1952
1953
1954 M-b Move cursor backward one word.
1955
1956
1957 ^A Move cursor to start of line.
1958
1959
1960 ^E Move cursor to end of line.
1961
1962
1963 ^]char Move cursor forward to character char on current line.
1964
1965
1966 M-^]char Move cursor backward to character char on current line.
1967
1968
1969 ^X^X Interchange the cursor and mark.
1970
1971
1972 erase (User defined erase character as defined by the stty(1)
1973 command, usually ^H or #.) Delete previous character.
1974
1975
1976 ^D Delete current character.
1977
1978
1979 M-d Delete current word.
1980
1981
1982 M-^H (Meta-backspace) Delete previous word.
1983
1984
1985 M-h Delete previous word.
1986
1987
1988 M-^? (Meta-DEL) Delete previous word (if your interrupt charac‐
1989 ter is ^? (DEL, the default) then this command does not
1990 work).
1991
1992
1993 ^T Transpose current character with next character in emacs
1994 mode. Transpose two previous characters in gmacs mode.
1995
1996
1997 ^C Capitalize current character.
1998
1999
2000 M-c Capitalize current word.
2001
2002
2003 M-l Change the current word to lower case.
2004
2005
2006 ^K Delete from the cursor to the end of the line. If preceded
2007 by a numerical parameter whose value is less than the cur‐
2008 rent cursor position, then delete from given position up
2009 to the cursor. If preceded by a numerical parameter whose
2010 value is greater than the current cursor position, then
2011 delete from cursor up to given cursor position.
2012
2013
2014 ^W Kill from the cursor to the mark.
2015
2016
2017 M-p Push the region from the cursor to the mark on the stack.
2018
2019
2020 kill (User defined kill character as defined by the stty(1)
2021 command, usually ^G or @.) Kill the entire current line.
2022 If two kill characters are entered in succession, all kill
2023 characters from then on cause a line feed (useful when
2024 using paper terminals).
2025
2026
2027 ^Y Restore last item removed from line. (Yank item back to
2028 the line.)
2029
2030
2031 ^L Line feed and print current line.
2032
2033
2034 ^@ (null character) Set mark.
2035
2036
2037 M-space (Meta space) Set mark.
2038
2039
2040 J (New line) Execute the current line.
2041
2042
2043 M (Return) Execute the current line.
2044
2045
2046 eof End-of-file character, normally ^D, is processed as an
2047 End-of-file only if the current line is null.
2048
2049
2050 ^P Fetch previous command. Each time ^P is entered the previ‐
2051 ous command back in time is accessed. Moves back one line
2052 when not on the first line of a multi-line command.
2053
2054
2055 M-< Fetch the least recent (oldest) history line.
2056
2057
2058 M-> Fetch the most recent (youngest) history line.
2059
2060
2061 ^N Fetch next command line. Each time ^N is entered the next
2062 command line forward in time is accessed.
2063
2064
2065 ^Rstring Reverse search history for a previous command line con‐
2066 taining string. If a parameter of zero is given, the
2067 search is forward. string is terminated by a RETURN or NEW
2068 LINE. If string is preceded by a ^, the matched line must
2069 begin with string. If string is omitted, then the next
2070 command line containing the most recent string is
2071 accessed. In this case a parameter of zero reverses the
2072 direction of the search.
2073
2074
2075 ^O Operate. Execute the current line and fetch the next line
2076 relative to current line from the history file.
2077
2078
2079 M-digits (Escape) Define numeric parameter, the digits are taken as
2080 a parameter to the next command. The commands that accept
2081 a parameter are ^F, ^B, erase, ^C, ^D, ^K, ^R, ^P, ^N, ^],
2082 M-., M-^], M-_, M-b, M-c, M-d, M-f, M-h, M-l and M-^H.
2083
2084
2085 M-letter Soft-key. Your alias list is searched for an alias by the
2086 name _letter and if an alias of this name is defined, its
2087 value is inserted on the input queue. The letter must not
2088 be one of the above meta-functions.
2089
2090
2091 M-[letter Soft-key. Your alias list is searched for an alias by the
2092 name __letter and if an alias of this name is defined, its
2093 value is inserted on the input queue. The can be used to
2094 program functions keys on many terminals.
2095
2096
2097 M−. The last word of the previous command is inserted on the
2098 line. If preceded by a numeric parameter, the value of
2099 this parameter determines which word to insert rather than
2100 the last word.
2101
2102
2103 M−_ Same as M−..
2104
2105
2106 M−* An asterisk is appended to the end of the word and a file
2107 name expansion is attempted.
2108
2109
2110 M−ESC File name completion. Replaces the current word with the
2111 longest common prefix of all filenames matching the cur‐
2112 rent word with an asterisk appended. If the match is
2113 unique, a / is appended if the file is a directory and a
2114 space is appended if the file is not a directory.
2115
2116
2117 M−= List files matching current word pattern if an asterisk
2118 were appended.
2119
2120
2121 ^U Multiply parameter of next command by 4.
2122
2123
2124 \ Escape next character. Editing characters, the user's
2125 erase, kill and interrupt (normally ^?) characters can be
2126 entered in a command line or in a search string if pre‐
2127 ceded by a \. The \ removes the next character's editing
2128 features (if any).
2129
2130
2131 ^V Display version of the shell.
2132
2133
2134 M-# Insert a # at the beginning of the line and execute it.
2135 This causes a comment to be inserted in the history file.
2136
2137
2138 vi Editing Mode
2139 There are two typing modes. Initially, when you enter a command you are
2140 in the input mode. To edit, enter control mode by typing ESC (033) and
2141 move the cursor to the point needing correction and then insert or
2142 delete characters or words as needed. Most control commands accept an
2143 optional repeat count prior to the command.
2144
2145
2146 When in vi mode on most systems, canonical processing is initially
2147 enabled and the command is echoed again if the speed is 1200 baud or
2148 greater and it contains any control characters or less than one second
2149 has elapsed since the prompt was printed. The ESC character terminates
2150 canonical processing for the remainder of the command and the user can
2151 then modify the command line. This scheme has the advantages of canoni‐
2152 cal processing with the type-ahead echoing of raw mode.
2153
2154
2155 If the option viraw is also set, the terminal always have canonical
2156 processing disabled. This mode is implicit for systems that do not sup‐
2157 port two alternate end of line delimiters, and can be helpful for cer‐
2158 tain terminals.
2159
2160 Input Edit Commands
2161 By default the editor is in input mode.
2162
2163 erase (User defined erase character as defined by the stty(1) com‐
2164 mand, usually ^H or #.) Delete previous character.
2165
2166
2167 ^W Delete the previous blank separated word.
2168
2169
2170 ^D Terminate the shell.
2171
2172
2173 ^V Escape next character. Editing characters and the user's erase
2174 or kill characters can be entered in a command line or in a
2175 search string if preceded by a ^V. The ^V removes the next
2176 character's editing features (if any).
2177
2178
2179 \ Escape the next erase or kill character.
2180
2181
2182 Motion Edit Commands
2183 The following commands move the cursor:
2184
2185 [count]l Cursor forward (right) one character.
2186
2187
2188 [count]w Cursor forward one alpha-numeric word.
2189
2190
2191 [count]W Cursor to the beginning of the next word that follows a
2192 blank.
2193
2194
2195 [count]e Cursor to end of word.
2196
2197
2198 [count]E Cursor to end of the current blank delimited word.
2199
2200
2201 [count]h Cursor backward (left) one character.
2202
2203
2204 [count]b Cursor backward one word.
2205
2206
2207 [count]B Cursor to preceding blank separated word.
2208
2209
2210 [count]| Cursor to column count.
2211
2212
2213 [count]fc Find the next character c in the current line.
2214
2215
2216 [count]Fc Find the previous character c in the current line.
2217
2218
2219 [count]tc Equivalent to f followed by h.
2220
2221
2222 [count]Tc Equivalent to F followed by l.
2223
2224
2225 [count]; Repeats count times, the last single character find com‐
2226 mand, f, F, t, or T.
2227
2228
2229 [count], Reverses the last single character find command count
2230 times.
2231
2232
2233 0 Cursor to start of line.
2234
2235
2236 ^ Cursor to first non-blank character in line.
2237
2238
2239 $ Cursor to end of line.
2240
2241
2242 % Moves to balancing (, ), {, }, [, or ]. If cursor is not
2243 on one of the above characters, the remainder of the line
2244 is searched for the first occurrence of one of the above
2245 characters first.
2246
2247
2248 Search Edit Commands
2249 These commands access your command history.
2250
2251 [count]k Fetch previous command. Each time k is entered the pre‐
2252 vious command back in time is accessed.
2253
2254
2255 [count]− Equivalent to k.
2256
2257
2258 [count]j Fetch next command. Each time j is entered, the next
2259 command forward in time is accessed.
2260
2261
2262 [count]+ Equivalent to j.
2263
2264
2265 [count]G The command number count is fetched. The default is the
2266 least recent history command.
2267
2268
2269 /string Search backward through history for a previous command
2270 containing string. string is terminated by a RETURN or
2271 NEWLINE. If string is preceded by a ^, the matched line
2272 must begin with string. If string is NULL, the previous
2273 string is used.
2274
2275
2276 ?string Same as / except that search is in the forward direc‐
2277 tion.
2278
2279
2280 n Search for next match of the last pattern to / or ? com‐
2281 mands.
2282
2283
2284 N Search for next match of the last pattern to / or ?, but
2285 in reverse direction. Search history for the string
2286 entered by the previous / command.
2287
2288
2289 Text Modification Edit Commands
2290 These commands modifies the line.
2291
2292 a Enter input mode and enter text after the current
2293 character.
2294
2295
2296 A Append text to the end of the line. Equivalent to $a.
2297
2298
2299 [count]cmotion Delete current character through the character that
2300 c[count]motion motion would move the cursor to and enter input mode.
2301 If motion is c, the entire line is deleted and input
2302 mode entered.
2303
2304
2305 C Delete the current character through the end of line
2306 and enter input mode. Equivalent to c$.
2307
2308
2309 [count]s Delete count characters and enter input mode.
2310
2311
2312 S Equivalent to cc.
2313
2314
2315 D Delete the current character through the end of line.
2316 Equivalent to d$.
2317
2318
2319 [count]dmotion Delete current character through the character that
2320 d[count]motion motion would move to. If motion is d, the entire line
2321 is deleted.
2322
2323
2324 i Enter input mode and insert text before the current
2325 character.
2326
2327
2328 I Insert text before the beginning of the line. Equiva‐
2329 lent to 0i.
2330
2331
2332 [count]P Place the previous text modification before the cur‐
2333 sor.
2334
2335
2336 [count]p Place the previous text modification after the cur‐
2337 sor.
2338
2339
2340 R Enter input mode and replace characters on the screen
2341 with characters you type overlay fashion.
2342
2343
2344 [count]rc Replace the count character(s) starting at the cur‐
2345 rent cursor position with c, and advance the cursor.
2346
2347
2348 [count]x Delete current character.
2349
2350
2351 [count]X Delete preceding character.
2352
2353
2354 [count]. Repeat the previous text modification command.
2355
2356
2357 [count]~ Invert the case of the count character(s) starting at
2358 the current cursor position and advance the cursor.
2359
2360
2361 [count]_ Causes the count word of the previous command to be
2362 appended and input mode entered. The last word is
2363 used if count is omitted.
2364
2365
2366 * Causes an * to be appended to the current word and
2367 file name generation attempted. If no match is found,
2368 it rings the bell. Otherwise, the word is replaced by
2369 the matching pattern and input mode is entered.
2370
2371
2372 \ Filename completion. Replaces the current word with
2373 the longest common prefix of all filenames matching
2374 the current word with an asterisk appended. If the
2375 match is unique, a / is appended if the file is a
2376 directory and a space is appended if the file is not
2377 a directory.
2378
2379
2380 Other Edit Commands
2381 Miscellaneous commands.
2382
2383 [count]ymotion Yank current character through character that motion
2384 y[count]motion would move the cursor to and puts them into the
2385 delete buffer. The text and cursor are unchanged.
2386
2387
2388 Y Yanks from current position to end of line. Equiva‐
2389 lent to y$.
2390
2391
2392 u Undo the last text modifying command.
2393
2394
2395 U Undo all the text modifying commands performed on the
2396 line.
2397
2398
2399 [count]v Returns the command fc -e ${VISUAL:-${EDITOR:-vi}}
2400 count in the input buffer. If count is omitted, then
2401 the current line is used.
2402
2403
2404 ^L Line feed and print current line. Has effect only in
2405 control mode.
2406
2407
2408 J (New line) Execute the current line, regardless of
2409 mode.
2410
2411
2412 M (Return) Execute the current line, regardless of
2413 mode.
2414
2415
2416 # If the first character of the command is a #, then
2417 this command deletes this # and each # that follows a
2418 newline. Otherwise, sends the line after inserting a
2419 # in front of each line in the command. Useful for
2420 causing the current line to be inserted in the his‐
2421 tory as a comment and removing comments from previous
2422 comment commands in the history file.
2423
2424
2425 = List the file names that match the current word if an
2426 asterisk were appended it.
2427
2428
2429 @letter Your alias list is searched for an alias by the name
2430 _letter and if an alias of this name is defined, its
2431 value is inserted on the input queue for processing.
2432
2433
2434 Special Commands
2435 The following simple-commands are executed in the shell process.
2436 Input/Output redirection is permitted. Unless otherwise indicated, the
2437 output is written on file descriptor 1 and the exit status, when there
2438 is no syntax error, is 0. Commands that are preceded by one or two *
2439 (asterisks) are treated specially in the following ways:
2440
2441 1. Variable assignment lists preceding the command remain in
2442 effect when the command completes.
2443
2444 2. I/O redirections are processed after variable assignments.
2445
2446 3. Errors cause a script that contains them to abort.
2447
2448 4. Words, following a command preceded by ** that are in the
2449 format of a variable assignment, are expanded with the same
2450 rules as a variable assignment. This means that tilde sub‐
2451 stitution is performed after the = sign and word splitting
2452 and file name generation are not performed.
2453
2454 * : [ arg ... ]
2455
2456 The command only expands parameters.
2457
2458
2459 * . file [ arg ... ]
2460
2461 Read the complete file then execute the commands. The commands are
2462 executed in the current shell environment. The search path speci‐
2463 fied by PATH is used to find the directory containing file. If any
2464 arguments arg are given, they become the positional parameters.
2465 Otherwise the positional parameters are unchanged. The exit status
2466 is the exit status of the last command executed.
2467
2468
2469 ** alias [ -tx ] [ name[ =value ] ] ...
2470
2471 alias with no arguments prints the list of aliases in the form
2472 name=value on standard output. An alias is defined for each name
2473 whose value is given. A trailing space in value causes the next
2474 word to be checked for alias substitution. The -t flag is used to
2475 set and list tracked aliases. The value of a tracked alias is the
2476 full pathname corresponding to the given name. The value becomes
2477 undefined when the value of PATH is reset but the aliases remained
2478 tracked. Without the -t flag, for each name in the argument list
2479 for which no value is given, the name and value of the alias is
2480 printed. The -x flag is used to set or print exported aliases. An
2481 exported alias is defined for scripts invoked by name. The exit
2482 status is non-zero if a name is given, but no value, and no alias
2483 has been defined for the name.
2484
2485
2486 bg [ %job... ]
2487
2488 This command is only on systems that support job control. Puts each
2489 specified job into the background. The current job is put in the
2490 background if job is not specified. See Jobs section above for a
2491 description of the format of job.
2492
2493
2494 * break [ n ]
2495
2496 Exit from the enclosed for, while, until, or select loop, if any.
2497 If n is specified then break n levels. If n is greater than the
2498 number of enclosing loops, the outermost enclosing loop shall be
2499 exited.
2500
2501
2502 * continue [ n ]
2503
2504 Resume the next iteration of the enclosed for, while, until, or
2505 select loop. If n is specified then resume at the n-th enclosed
2506 loop. If n is greater than the number of enclosing loops, the out‐
2507 ermost enclosing loop shall be used.
2508
2509
2510 cd [ -L ] [ -P ] [ arg ]
2511 cd old new
2512
2513 This command can be in either of two forms. In the first form it
2514 changes the current directory to arg. If arg is − the directory is
2515 changed to the previous directory. The shell variable HOME is the
2516 default arg. The environment variable PWD is set to the current
2517 directory. If the PWD is changed, the OLDPWD environment variable
2518 shall also be changed to the value of the old working directory,
2519 that is, the current working directory immediately prior to the
2520 call to change directory (cd). The shell variable CDPATH defines
2521 the search path for the directory containing arg. Alternative
2522 directory names are separated by a colon (:). The default path is
2523 null (specifying the current directory). The current directory is
2524 specified by a null path name, which can appear immediately after
2525 the equal sign or between the colon delimiters anywhere else in the
2526 path list. If arg begins with a / then the search path is not used.
2527 Otherwise, each directory in the path is searched for arg. If
2528 unsuccessful, cd attempts to change directories to the pathname
2529 formed by the concatenation of the value of PWD, a slash character,
2530 and arg.
2531
2532 -L Handles the operation dot-dot (..) logically. Symbolic link
2533 components are not resolved before dot-dot components are
2534 processed.
2535
2536
2537 -P Handles the operand dot-dot physically. Symbolic link compo‐
2538 nents are resolved before dot-dot components are processed.
2539
2540 If both -L and -P options are specified, the last option to be
2541 invoked is used and the other is ignored. If neither -L nor -P is
2542 specified, the operand is handled dot-dot logically.
2543
2544 The second form of cd substitutes the string new for the string old
2545 in the current directory name, PWD, and tries to change to this new
2546 directory. The cd command cannot be executed by rksh.
2547
2548
2549 command [-p] [command_name] [argument ...]
2550 command [-v | -V] command_name
2551
2552 The command utility causes the shell to treat the arguments as a
2553 simple command, suppressing the shell function lookup. The -p flag
2554 performs the command search using a default value for PATH that is
2555 guaranteed to find all of the standard utilities. The -v flag
2556 writes a string to standard output that indicates the pathname or
2557 command that is used by the shell, in the current shell execution
2558 environment, to invoke command_name. The -V flag writes a string to
2559 standard output that indicates how the name given in the com‐
2560 mand_name operand is interpreted by the shell, in the current shell
2561 execution environment.
2562
2563
2564 echo [ arg ... ]
2565
2566 See echo(1) for usage and description.
2567
2568
2569 * eval [ arg ... ]
2570
2571 The arguments are read as input to the shell and the resulting com‐
2572 mand(s) executed.
2573
2574
2575 * exec [ arg ... ]
2576
2577 If arg is given, the command specified by the arguments is executed
2578 in place of this shell without creating a new process. Input/output
2579 arguments can appear and affect the current process. If no argu‐
2580 ments are given the effect of this command is to modify file
2581 descriptors as prescribed by the input/output redirection list. In
2582 this case, any file descriptor numbers greater than 2 that are
2583 opened with this mechanism are closed when invoking another pro‐
2584 gram.
2585
2586
2587 * exit [ n ]
2588
2589 Causes the calling shell or shell script to exit with the exit sta‐
2590 tus specified by n. The value is the least significant 8 bits of
2591 the specified status. If n is omitted then the exit status is that
2592 of the last command executed. When exit occurs when executing a
2593 trap, the last command refers to the command that executed before
2594 the trap was invoked. An EOF also causes the shell to exit except
2595 for a shell which has the ignoreeof option turned on. See set.
2596
2597
2598 ** export [ name[=value] ] ...
2599 ** export -p
2600
2601 The given names are marked for automatic export to the environment
2602 of subsequently-executed commands.
2603
2604 When -p is specified, export writes to the standard output the
2605 names and values of all exported variables in the following format:
2606
2607 "export %s=%s\n", name, value
2608
2609
2610 if name is set, and:
2611
2612 "export %s\n", name
2613
2614
2615 if name is unset.
2616
2617 The shell formats the output, including the proper use of quoting,
2618 so that it is suitable for reinput to the shell as commands that
2619 achieve the same exporting results, except for the following:
2620
2621 1. Read-only variables with values cannot be reset.
2622
2623 2. Variables that were unset at the time they were output
2624 are not reset to the unset state if a value is assigned
2625 to the variable between the time the state was saved and
2626 the time at which the saved output is reinput to the
2627 shell.
2628
2629
2630 fc [ -e ename ] [ -nlr ] [ first [ last ] ]
2631 fc -e - [ old=new ] [ command ]
2632 fc -s [ old=new ] [ command ]
2633
2634 In the first form, a range of commands from first to last is
2635 selected from the last HISTSIZE commands that were typed at the
2636 terminal. The arguments first and last can be specified as a number
2637 or as a string. A string is used to locate the most recent command
2638 starting with the given string. A negative number is used as an
2639 offset to the current command number. If the -l flag is selected,
2640 the commands are listed on standard output. Otherwise, the editor
2641 program ename is invoked on a file containing these keyboard com‐
2642 mands. If ename is not supplied, then the value of the variable
2643 FCEDIT (default /bin/ed) is used as the editor. When editing is
2644 complete, the edited command(s) is executed. If last is not speci‐
2645 fied then it is set to first. If first is not specified the default
2646 is the previous command for editing and −16 for listing. The flag
2647 -r reverses the order of the commands and the flag -n suppresses
2648 command numbers when listing. In the second form the command is re-
2649 executed after the substitution old=new is performed. If there is
2650 not a command argument, the most recent command typed at this ter‐
2651 minal is executed.
2652
2653
2654 fg [ %job... ]
2655
2656 This command is only on systems that support job control. Each job
2657 specified is brought to the foreground. Otherwise, the current job
2658 is brought into the foreground. See "Jobs" section above for a
2659 description of the format of job.
2660
2661
2662 getopts optstring name [ arg ... ]
2663
2664 Checks arg for legal options. If arg is omitted, the positional
2665 parameters are used. An option argument begins with a + or a −. An
2666 option not beginning with + or − or the argument - ends the
2667 options. optstring contains the letters that getopts recognizes.
2668 If a letter is followed by a :, that option is expected to have an
2669 argument. The options can be separated from the argument by blanks.
2670
2671 getopts places the next option letter it finds inside variable name
2672 each time it is invoked with a + prepended when arg begins with a
2673 +. The index of the next arg is stored in OPTIND. The option argu‐
2674 ment, if any, gets stored in OPTARG.
2675
2676 A leading : in optstring causes getopts to store the letter of an
2677 invalid option in OPTARG, and to set name to ? for an unknown
2678 option and to : when a required option is missing. Otherwise,
2679 getopts prints an error message. The exit status is non-zero when
2680 there are no more options. See getoptcvt(1) for usage and descrip‐
2681 tion.
2682
2683 getopts supports both traditional single-character short options
2684 and long options defined by Sun's Command Line Interface Paradigm
2685 (CLIP).
2686
2687 Each long option is an alias for a short option and is specified in
2688 parentheses following its equivalent short option. For example,
2689 you can specify the long option file as an alias for the short
2690 option f using the following script line:
2691
2692 getopts "f(file)" opt
2693
2694
2695 Precede long options on the command line with -- or ++. In the
2696 example above, --file on the command line would be the equivalent
2697 of -f, and ++file on the command line would be the equivalent of
2698 +f.
2699
2700 Each short option can have multiple long option equivalents,
2701 although this is in violation of the CLIP specification and should
2702 be used with caution. You must enclose each long option equivalent
2703 parentheses, as follows:
2704
2705 getopts "f:(file)(input-file)o:(output-file)"
2706
2707
2708 In the above example, both --file and --input-file are the equiva‐
2709 lent of -f, and --output-file is the equivalent of -o.
2710
2711 The variable name is always set to a short option. When a long
2712 option is specified on the command line, name is set to the short-
2713 option equivalent.
2714
2715
2716 hash [ name ... ]
2717 hash [ -r ]
2718
2719 For each name, the location in the search path of the command spec‐
2720 ified by name is determined and remembered by the shell. The -r
2721 option causes the shell to forget all remembered locations. If no
2722 arguments are given, information about remembered commands is pre‐
2723 sented. Hits is the number of times a command has been invoked by
2724 the shell process. Cost is a measure of the work required to
2725 locate a command in the search path. If a command is found in a
2726 relative directory in the search path, after changing to that
2727 directory, the stored location of that command is recalculated.
2728 Commands for which this is done are indicated by an asterisk (*)
2729 adjacent to the hits information. Cost is incremented when the
2730 recalculation is done.
2731
2732
2733 jobs [ -lnp ] [ %job ... ]
2734
2735 Lists information about each given job; or all active jobs if job
2736 is omitted. The -l flag lists process ids in addition to the normal
2737 information. The -n flag displays only jobs that have stopped or
2738 exited since last notified. The -p flag causes only the process
2739 group to be listed. See "Jobs" section above and jobs(1) for a
2740 description of the format of job.
2741
2742
2743 kill [ -sig ] %job ...
2744 kill [ -sig ] pid ...
2745 kill -l
2746
2747 Sends either the TERM (terminate) signal or the specified signal to
2748 the specified jobs or processes. Signals are either given by number
2749 or by names (as given in signal.h(3HEAD) stripped of the prefix
2750 ``SIG'' with the exception that SIGCHD is named CHLD). If the sig‐
2751 nal being sent is TERM (terminate) or HUP (hangup), then the job or
2752 process is sent a CONT (continue) signal if it is stopped. The
2753 argument job can be the process id of a process that is not a mem‐
2754 ber of one of the active jobs. See Jobs for a description of the
2755 format of job. In the second form, kill -l, the signal numbers and
2756 names are listed.
2757
2758
2759 let arg...
2760
2761 Each arg is a separate arithmetic expression to be evaluated. See
2762 the Arithmetic Evaluation section above, for a description of
2763 arithmetic expression evaluation.
2764
2765 The exit status is 0 if the value of the last expression is non-
2766 zero, and 1 otherwise.
2767
2768
2769 login argument ...
2770
2771 Equivalent to `exec login argument....' See login(1) for usage and
2772 description.
2773
2774
2775 * newgrp [ arg ... ]
2776
2777 Equivalent to exec /bin/newgrp arg ....
2778
2779
2780 print [ -Rnprsu[n ] ] [ arg ... ]
2781
2782 The shell output mechanism. With no flags or with flag − or -, the
2783 arguments are printed on standard output as described by echo(1).
2784 The exit status is 0, unless the output file is not open for writ‐
2785 ing.
2786
2787 -n Suppresses NEWLINE from being added to the output.
2788
2789
2790 -R | -r Raw mode. Ignores the escape conventions of echo. The
2791 -R option prints all subsequent arguments and options
2792 other than -n.
2793
2794
2795 -p Writes the arguments to the pipe of the process spawned
2796 with |& instead of standard output.
2797
2798
2799 -s Writes the arguments to the history file instead of
2800 standard output.
2801
2802
2803 -u [ n ] Specifies a one digit file descriptor unit number n on
2804 which the output is placed. The default is 1.
2805
2806
2807
2808 pwd [ -L | -P ]
2809
2810 Writes to the standard output an absolute pathname of the current
2811 working directory, which does not contain the filenames dot (.) or
2812 dot-dot (..).
2813
2814 -L If the PWD environment variable contains an absolute pathname
2815 of the current directory that does not contain the filenames
2816 dot or dot-dot, pwd writes this pathname to standard output.
2817 Otherwise, the -L option behaves like the -P option.
2818
2819
2820 -P The absolute pathname written shall not contain filenames
2821 that, in the context of the pathname, refer to files of type
2822 symbolic link.
2823
2824 If both -L and -P are specified, the last one applies. If neither
2825 -L nor -P is specified, pwd behaves as if -L had been specified.
2826
2827
2828 read [ -prsu[ n ] ] [ name?prompt ] [ name ... ]
2829
2830 The shell input mechanism. One line is read and is broken up into
2831 fields using the characters in IFS as separators. The escape char‐
2832 acter, (\), is used to remove any special meaning for the next
2833 character and for line continuation. In raw mode, -r, the \ charac‐
2834 ter is not treated specially. The first field is assigned to the
2835 first name, the second field to the second name, etc., with left‐
2836 over fields assigned to the last name. The -p option causes the
2837 input line to be taken from the input pipe of a process spawned by
2838 the shell using |&. If the -s flag is present, the input is saved
2839 as a command in the history file. The flag -u can be used to spec‐
2840 ify a one digit file descriptor unit n to read from. The file
2841 descriptor can be opened with the exec special command. The default
2842 value of n is 0. If name is omitted then REPLY is used as the
2843 default name. The exit status is 0 unless the input file is not
2844 open for reading or an EOF is encountered. An EOF with the -p
2845 option causes cleanup for this process so that another can be
2846 spawned. If the first argument contains a ?, the remainder of this
2847 word is used as a prompt on standard error when the shell is inter‐
2848 active. The exit status is 0 unless an EOF is encountered.
2849
2850
2851 ** readonly [ name[=value] ] ...
2852 ** readonly -p
2853
2854 The given names are marked readonly and these names cannot be
2855 changed by subsequent assignment.
2856
2857 When -p is specified, readonly writes to the standard output the
2858 names and values of all read-only variables, in the following for‐
2859 mat:
2860
2861 "readonly %s=%s\n", name, value
2862
2863
2864 if name is set, and:
2865
2866 "readonly $s\n", name
2867
2868
2869 if name is unset.
2870
2871 The shell formats the output, including the proper use of quoting,
2872 so that it is suitable for reinput to the shell as commands that
2873 achieve the same value and readonly attribute-setting results in a
2874 shell execution environment in which:
2875
2876 1. Variables with values set at the time they were output
2877 do not have the readonly attribute set.
2878
2879 2. Variables that were unset at the time they were output
2880 do not have a value at the time at which the saved out‐
2881 put is reinput to the shell.
2882
2883
2884 * return [ n ]
2885
2886 Causes a shell function or '.' script to return to the invoking
2887 script with the return status specified by n. The value is the
2888 least significant 8 bits of the specified status. If n is omitted
2889 then the return status is that of the last command executed. If
2890 return is invoked while not in a function or a '.' script, then it
2891 is the same as an exit.
2892
2893
2894 set [ ±abCefhkmnopstuvx ] [ ±o option ]... [ ±A name ] [ arg ... ]
2895
2896 The flags for this command have meaning as follows:
2897
2898 -A Array assignment. Unsets the variable name and assigns
2899 values sequentially from the list arg. If +A is used,
2900 the variable name is not unset first.
2901
2902
2903 -a All subsequent variables that are defined are automati‐
2904 cally exported.
2905
2906
2907 -b Causes the shell to notify the user asynchronously of
2908 background job completions. The following message is
2909 written to standard error:
2910
2911 "[%d]%c %s%s\n", <job-number>, <current>, <status>, \
2912 whe<job-name>
2913
2914
2915 where the fields are as follows:
2916
2917 <current> The character + identifies the job that
2918 would be used as a default for the fg
2919 or bg utilities. This job can also be
2920 specified using the job_id %+ or %%.
2921 The character − identifies the job that
2922 would become the default if the current
2923 default job were to exit; this job can
2924 also be specified using the job_id %−.
2925 For other jobs, this field is a space
2926 character. At most one job can be iden‐
2927 tified with + and at most one job can
2928 be identified with −. If there is any
2929 suspended job, then the current job is
2930 a suspended job. If there are at least
2931 two suspended jobs, then the previous
2932 job is also a suspended job.
2933
2934
2935 <job-number> A number that can be used to identify
2936 the process group to the wait, fg, bg,
2937 and kill utilities. Using these utili‐
2938 ties, the job can be identified by pre‐
2939 fixing the job number with %.
2940
2941
2942 <status> Unspecified.
2943
2944
2945 <job-name> Unspecified.
2946
2947 When the shell notifies the user a job has been com‐
2948 pleted, it can remove the job's process ID from the
2949 list of those known in the current shell execution
2950 environment. Asynchronous notification is not enabled
2951 by default.
2952
2953
2954 -C Prevents existing files from being overwritten by the
2955 shell's > redirection operator. The >| redirection
2956 operator overrides this -noclobber option for an indi‐
2957 vidual file.
2958
2959
2960 -e If a command has a non-zero exit status, executes the
2961 ERR trap, if set, and exit. This mode is disabled while
2962 reading profiles.
2963
2964
2965 -f Disables file name generation.
2966
2967
2968 -h Each command becomes a tracked alias when first encoun‐
2969 tered.
2970
2971
2972 -k All variable assignment arguments are placed in the
2973 environment for a command, not just those that precede
2974 the command name.
2975
2976
2977 -m Background jobs runs in a separate process group and a
2978 line prints upon completion. The exit status of back‐
2979 ground jobs is reported in a completion message. On
2980 systems with job control, this flag is turned on auto‐
2981 matically for interactive shells.
2982
2983
2984 -n Reads commands and check them for syntax errors, but do
2985 not execute them. Ignored for interactive shells.
2986
2987
2988 -o Writes the current option settings to standard output
2989 in a format that is suitable for reinput to the shell
2990 as commands that achieve the same option settings.
2991
2992
2993 -o The following argument can be one of the following
2994 option names:
2995
2996 allexport Same as -a.
2997
2998
2999 errexit Same as -e.
3000
3001
3002 bgnice All background jobs are run at a lower
3003 priority. This is the default mode.
3004
3005
3006 emacs Puts you in an emacs style in-line editor
3007 for command entry.
3008
3009
3010 gmacs Puts you in a gmacs style in-line editor
3011 for command entry.
3012
3013
3014 ignoreeof The shell does not exit onEOF. The com‐
3015 mand exit must be used.
3016
3017
3018 keyword Same as -k.
3019
3020
3021 markdirs All directory names resulting from file
3022 name generation have a trailing /
3023 appended.
3024
3025
3026 monitor Same as -m.
3027
3028
3029 noclobber Prevents redirection > from truncating
3030 existing files. Require >| to truncate a
3031 file when turned on. Equivalent to -C.
3032
3033
3034 noexec Same as -n.
3035
3036
3037 noglob Same as -f.
3038
3039
3040 nolog Do not save function definitions in his‐
3041 tory file.
3042
3043
3044 notify Equivalent to -b.
3045
3046
3047 nounset Same as -u.
3048
3049
3050 privileged Same as -p.
3051
3052
3053 verbose Same as -v.
3054
3055
3056 trackall Same as -h.
3057
3058
3059 vi Puts you in insert mode of a vi style in-
3060 line editor until you hit escape charac‐
3061 ter 033. This puts you in control mode. A
3062 return sends the line.
3063
3064
3065 viraw Each character is processed as it is
3066 typed in vi mode.
3067
3068
3069 xtrace Same as -x.
3070
3071 If no option name is supplied, the current option set‐
3072 tings are printed.
3073
3074
3075 -p Disables processing of the $HOME/.profile file and uses
3076 the file /etc/suid_profile instead of the ENV file.
3077 This mode is on whenever the effective uid is not equal
3078 to the real uid, or when the effective gid is not equal
3079 to the real gid. Turning this off causes the effective
3080 uid and gid to be set to the real uid and gid.
3081
3082
3083 -s Sorts the positional parameters lexicographically.
3084
3085
3086 -t Exits after reading and executing one command.
3087
3088
3089 -u Treats unset parameters as an error when substituting.
3090
3091
3092 -v Prints shell input lines as they are read.
3093
3094
3095 -x Prints commands and their arguments as they are exe‐
3096 cuted.
3097
3098
3099 − Turns off -x and -v flags and stops examining arguments
3100 for flags.
3101
3102
3103 −− Does not change any of the flags. Useful in setting $1
3104 to a value beginning with −. If no arguments follow
3105 this flag then the positional parameters are unset.
3106
3107 Using + rather than − causes these flags to be turned
3108 off. These flags can also be used upon invocation of
3109 the shell. The current set of flags can be found in $−.
3110 Unless -A is specified, the remaining arguments are
3111 positional parameters and are assigned, in order, to $1
3112 $2 .... If no arguments are given, the names and values
3113 of all variables are printed on the standard output.
3114
3115
3116
3117 * shift [ n ]
3118
3119 The positional parameters from $n+1 $n+1 ... are renamed $1 ...,
3120 default n is 1. The parameter n can be any arithmetic expression
3121 that evaluates to a non-negative number less than or equal to $#.
3122
3123
3124 stop%jobid ...
3125 stop pid ...
3126
3127 stop stops the execution of a background job(s) by using its jobid,
3128 or of any process by using its pid. See ps(1).
3129
3130
3131 suspend
3132
3133 Stops the execution of the current shell (but not if it is the
3134 login shell).
3135
3136
3137 test expression
3138
3139 Evaluates conditional expressions. See Conditional Expressions sec‐
3140 tion above and test(1) for usage and description.
3141
3142
3143 * times
3144
3145 Prints the accumulated user and system times for the shell and for
3146 processes run from the shell.
3147
3148
3149 * trap [ arg sig ... ]
3150
3151 arg is a command to be read and executed when the shell receives
3152 signal(s) sig. arg is scanned once when the trap is set and once
3153 when the trap is taken. sig can be specified as a signal number or
3154 signal name. trap commands are executed in order of signal number.
3155 Any attempt to set a trap on a signal number that was ignored on
3156 entry to the current shell is ineffective.
3157
3158 If arg is −, the shell resets each sig to the default value. If arg
3159 is null (''), the shell ignores each specified sig if it arises.
3160 Otherwise, arg is read and executed by the shell when one of the
3161 corresponding sigs arises. The action of the trap overrides a pre‐
3162 vious action (either default action or one explicitly set). The
3163 value of $? after the trap action completes is the value it had
3164 before the trap was invoked.
3165
3166 sig can be EXIT, 0 (equivalent to EXIT) or a signal specified using
3167 a symbolic name, without the SIG prefix, for example, HUP, INT,
3168 QUIT, TERM. If sig is 0 or EXIT and the trap statement is executed
3169 inside the body of a function, then the command arg is executed
3170 after the function completes. If sig is 0 or EXIT for a trap set
3171 outside any function, the command arg is executed on exit from the
3172 shell. If sig is ERR, arg is executed whenever a command has a non-
3173 zero exit status. If sig is DEBUG, arg is executed after each com‐
3174 mand.
3175
3176 The environment in which the shell executes a trap on EXIT is iden‐
3177 tical to the environment immediately after the last command exe‐
3178 cuted before the trap on EXIT was taken.
3179
3180 Each time the trap is invoked, arg is processed in a manner equiva‐
3181 lent to eval "$arg".
3182
3183 Signals that were ignored on entry to a non-interactive shell can‐
3184 not be trapped or reset, although no error need be reported when
3185 attempting to do so. An interactive shell can reset or catch sig‐
3186 nals ignored on entry. Traps remain in place for a given shell
3187 until explicitly changed with another trap command.
3188
3189 When a subshell is entered, traps are set to the default args. This
3190 does not imply that the trap command cannot be used within the sub‐
3191 shell to set new traps.
3192
3193 The trap command with no arguments writes to standard output a list
3194 of commands associated with each sig. The format is:
3195
3196 trap −− %s %s ... <arg>, <sig> ...
3197
3198
3199 The shell formats the output, including the proper use of quoting,
3200 so that it is suitable for reinput to the shell as commands that
3201 achieve the same trapping results. For example:
3202
3203 save_traps=$(trap)
3204 ...
3205 eval "$save_traps"
3206
3207
3208 If the trap name or number is invalid, a non-zero exit status is
3209 returned. Otherwise, 0 is returned. For both interactive and non-
3210 interactive shells, invalid signal names or numbers are not consid‐
3211 ered a syntax error and dol not cause the shell to abort.
3212
3213 Traps are not processed while a job is waiting for a foreground
3214 process. Thus, a trap on CHLD won't be executed until the fore‐
3215 ground job terminates.
3216
3217
3218 type name ...
3219
3220 For each name, indicates how it would be interpreted if used as a
3221 command name.
3222
3223
3224 ** typeset [ ±HLRZfilrtux[n] ] [ name[=value ] ] ...
3225
3226 Sets attributes and values for shell variables and functions. When
3227 typeset is invoked inside a function, a new instance of the vari‐
3228 ables name is created. The variables value and type are restored
3229 when the function completes. The following list of attributes can
3230 be specified:
3231
3232 -H This flag provides UNIX to host-name file mapping on non-UNIX
3233 machines.
3234
3235
3236 -L Left justifies and removes leading blanks from value. If n is
3237 non-zero it defines the width of the field. Otherwise, it is
3238 determined by the width of the value of first assignment.
3239 When the variable is assigned to, it is filled on the right
3240 with blanks or truncated, if necessary, to fit into the
3241 field. Leading zeros are removed if the -Z flag is also set.
3242 The -R flag is turned off.
3243
3244
3245 -R Right justifies and fills with leading blanks. If n is non-
3246 zero it defines the width of the field, otherwise it is
3247 determined by the width of the value of first assignment. The
3248 field is left filled with blanks or truncated from the end if
3249 the variable is reassigned. The -L flag is turned off.
3250
3251
3252 -Z Right justifies and fills with leading zeros if the first
3253 non-blank character is a digit and the -L flag has not been
3254 set. If n is non-zero it defines the width of the field. Oth‐
3255 erwise, it is determined by the width of the value of first
3256 assignment.
3257
3258
3259 -f The names refer to function names rather than variable names.
3260 No assignments can be made and the only other valid flags are
3261 -t, -u, and -x. The flag -t turns on execution tracing for
3262 this function. The flag -u causes this function to be marked
3263 undefined. The FPATH variable is searched to find the func‐
3264 tion definition when the function is referenced. The flag -x
3265 allows the function definition to remain in effect across
3266 shell procedures invoked by name.
3267
3268
3269 -i Parameter is an integer. This makes arithmetic faster. If n
3270 is non-zero it defines the output arithmetic base; otherwise,
3271 the first assignment determines the output base.
3272
3273
3274 -l All upper-case characters are converted to lower-case. The
3275 upper-case flag, -u is turned off.
3276
3277
3278 -r The given names are marked readonly and these names cannot be
3279 changed by subsequent assignment.
3280
3281
3282 -t Tags the variables. Tags are user definable and have no spe‐
3283 cial meaning to the shell.
3284
3285
3286 -u All lower-case characters are converted to upper-case charac‐
3287 ters. The lower-case flag, -l is turned off.
3288
3289
3290 -x The given names are marked for automatic export to the envi‐
3291 ronment of subsequently-executed commands.
3292
3293 The -i attribute cannot be specified along with -R, -L, -Z, or -f.
3294
3295 Using + rather than − causes these flags to be turned off. If no
3296 name arguments are given but flags are specified, a list of names
3297 (and optionally the values) of the variables which have these flags
3298 set is printed. (Using + rather than − keeps the values from being
3299 printed.) If no names and flags are given, the names and attributes
3300 of all variables are printed.
3301
3302
3303 ulimit [ -HSacdfnstv ] [ limit ]
3304
3305 Sets or displays a resource limit. The available resources limits
3306 are listed in the following section. Many systems do not contain
3307 one or more of these limits. The limit for a specified resource is
3308 set when limit is specified. The value of limit can be a number in
3309 the unit specified with each resource, or the value unlimited. The
3310 string unlimited requests that the current limit, if any, be
3311 removed. The -H and -S flags specify whether the hard limit or the
3312 soft limit for the given resource is set. A hard limit cannot be
3313 increased once it is set. A soft limit can be increased up to the
3314 value of the hard limit. If neither the -H or -S options is speci‐
3315 fied, the limit applies to both. The current resource limit is
3316 printed when limit is omitted. In this case, the soft limit is
3317 printed unless -H is specified. When more than one resource is
3318 specified, the limit name and unit is printed before the value.
3319
3320 -a Lists all of the current resource limits.
3321
3322
3323 -c The number of 512-byte blocks on the size of core dumps.
3324
3325
3326 -d The number of K-bytes on the size of the data area.
3327
3328
3329 -f The number of 512-byte blocks on files written by child pro‐
3330 cesses (files of any size can be read).
3331
3332
3333 -n The number of file descriptors plus 1.
3334
3335
3336 -s The number of K-bytes on the size of the stack area.
3337
3338
3339 -t The number of seconds to be used by each process.
3340
3341
3342 -v The number of K-bytes for virtual memory.
3343
3344 If no option is given, -f is assumed.
3345
3346
3347 umask [-S] [ mask ]
3348
3349 The user file-creation mask is set to mask (see umask(2)). mask can
3350 either be an octal number or a symbolic value as described in
3351 chmod(1). If a symbolic value is given, the new umask value is the
3352 complement of the result of applying mask to the complement of the
3353 previous umask value. If mask is omitted, the current value of the
3354 mask is printed. The -S flag produces symbolic output.
3355
3356
3357 unalias name ...
3358 unalias -a
3359
3360 The aliases given by the list of names are removed from the alias
3361 list. The -a option removes all alias definitions from the current
3362 execution environment.
3363
3364
3365 unset [ -f ] name ...
3366
3367 The variables given by the list of names are unassigned, that is,
3368 their values and attributes are erased. readonly variables cannot
3369 be unset. If the -f, flag is set, then the names refer to function
3370 names. Unsetting ERRNO, LINENO, MAILCHECK, OPTARG, OPTIND, RANDOM,
3371 SECONDS, TMOUT, and _ removes their special meaning even if they
3372 are subsequently assigned to.
3373
3374
3375 * wait [ job ]
3376
3377 Waits for the specified job and report its termination status. If
3378 job is not given then all currently active child processes are
3379 waited for. The exit status from this command is that of the
3380 process waited for. See Jobs for a description of the format of
3381 job.
3382
3383
3384 whence [ -pv ] name ...
3385
3386 For each name, indicates how it would be interpreted if used as a
3387 command name.
3388
3389 The -v flag produces a more verbose report.
3390
3391 The -p flag does a path search for name even if name is an alias, a
3392 function, or a reserved word.
3393
3394
3395 Invocation
3396 If the shell is invoked by exec(2), and the first character of argument
3397 zero ($0) is −, then the shell is assumed to be a login shell and com‐
3398 mands are read from /etc/profile and then from either .profile in the
3399 current directory or $HOME/.profile, if either file exists. Next, com‐
3400 mands are read from the file named by performing parameter substitution
3401 on the value of the environment variable ENV if the file exists. If the
3402 -s flag is not present and arg is, then a path search is performed on
3403 the first arg to determine the name of the script to execute. The
3404 script arg must have read permission and any setuid and setgid settings
3405 are ignored. If the script is not found on the path, arg is processed
3406 as if it named a builtin command or function. Commands are then read as
3407 described as follows. The following flags are interpreted by the shell
3408 when it is invoked:
3409
3410 -c Reads commands from the command_string operand. Sets the value of
3411 special parameter 0 from the value of the command_name operand
3412 and the positional parameters ($1, $2, and so on) in sequence
3413 from the remaining arg operands. No commands are read from the
3414 standard input.
3415
3416
3417 -s If the -s flag is present or if no arguments remain, commands are
3418 read from the standard input. Shell output, except for the output
3419 of the Special Commands listed above, is written to file descrip‐
3420 tor 2.
3421
3422
3423 -i If the -i flag is present or if the shell input and output are
3424 attached to a terminal (as told by ioctl(2)), then this shell is
3425 interactive. In this case, TERM is ignored (so that kill 0 does
3426 not kill an interactive shell) and INTR is caught and ignored (so
3427 that wait is interruptible). In all cases, QUIT is ignored by the
3428 shell.
3429
3430
3431 -r If the -r flag is present the shell is a restricted shell.
3432
3433
3434
3435 The remaining flags and arguments are described under the set command
3436 above.
3437
3438 rksh Only
3439 rksh is used to set up login names and execution environments whose
3440 capabilities are more controlled than those of the standard shell. The
3441 actions of rksh are identical to those of ksh, except that the follow‐
3442 ing are disallowed:
3443
3444 o changing directory (see cd(1))
3445
3446 o setting the value of SHELL, ENV, or PATH
3447
3448 o specifying path or command names containing /
3449
3450 o redirecting output (>, >|, <>, and >>)
3451
3452 o changing group (see newgrp(1)).
3453
3454
3455 The restrictions above are enforced after .profile and the ENV files
3456 are interpreted.
3457
3458
3459 When a command to be executed is found to be a shell procedure, rksh
3460 invokes ksh to execute it. Thus, it is possible to provide to the end-
3461 user shell procedures that have access to the full power of the stan‐
3462 dard shell, while imposing a limited menu of commands; this scheme
3463 assumes that the end-user does not have write and execute permissions
3464 in the same directory.
3465
3466
3467 The net effect of these rules is that the writer of the .profile has
3468 complete control over user actions, by performing guaranteed setup
3469 actions and leaving the user in an appropriate directory (probably not
3470 the login directory).
3471
3472
3473 The system administrator often sets up a directory of commands (that
3474 is, /usr/rbin) that can be safely invoked by rksh.
3475
3477 Errors detected by the shell, such as syntax errors, cause the shell to
3478 return a non-zero exit status. Otherwise, the shell returns the exit
3479 status of the last command executed (see also the exit command above).
3480 If the shell is being used non-interactively then execution of the
3481 shell file is abandoned. Run time errors detected by the shell are
3482 reported by printing the command or function name and the error condi‐
3483 tion. If the line number that the error occurred on is greater than
3484 one, then the line number is also printed in square brackets ([]) after
3485 the command or function name.
3486
3487
3488 For a non-interactive shell, an error condition encountered by a spe‐
3489 cial built-in or other type of utility causes the shell to write a
3490 diagnostic message to standard error and exit as shown in the following
3491 table:
3492
3493
3494
3495
3496 ┌──────────────────────────────────────────────────────────────────────┐
3497 │ Error Special Built-in Other Utilities │
3498 ├──────────────────────────────────────────────────────────────────────┤
3499 │Shell language syntax error exits exits │
3500 │Utility syntax error (option or exits does not exit │
3501 │operand error) │
3502 │Redirection error exits does not exit │
3503 │Variable assignment error exits does not exit │
3504 │Expansion error exits exits │
3505 │Command not found n/a might exit │
3506 │Dot script not found exits n/a │
3507 └──────────────────────────────────────────────────────────────────────┘
3508
3509
3510 An expansion error is one that occurs when the shell expansions are
3511 carried out (for example, ${x!y}, because ! is not a valid operator).
3512 An implementation can treat these as syntax errors if it is able to
3513 detect them during tokenization, rather than during expansion.
3514
3515
3516 If any of the errors shown as "might exit" or "exits" occur in a sub‐
3517 shell, the subshell exits or might exit with a non-zero status, but the
3518 script containing the subshell does not exit because of the error.
3519
3520
3521 In all of the cases shown in the table, an interactive shell writes a
3522 diagnostic message to standard error without exiting.
3523
3525 See largefile(5) for the description of the behavior of ksh and rksh
3526 when encountering files greater than or equal to 2 Gbyte (2^31 bytes).
3527
3529 Each command has an exit status that can influence the behavior of
3530 other shell commands. The exit status of commands that are not utili‐
3531 ties is documented in this section. The exit status of the standard
3532 utilities is documented in their respective sections.
3533
3534
3535 If a command is not found, the exit status is 127. If the command name
3536 is found, but it is not an executable utility, the exit status is 126.
3537 Applications that invoke utilities without using the shell should use
3538 these exit status values to report similar errors.
3539
3540
3541 If a command fails during word expansion or redirection, its exit sta‐
3542 tus is greater than zero.
3543
3544
3545 When reporting the exit status with the special parameter ?, the shell
3546 reports the full eight bits of exit status available. The exit status
3547 of a command that terminated because it received a signal reported as
3548 greater than 128.
3549
3551 /etc/profile
3552
3553
3554 /etc/suid_profile
3555
3556
3557 $HOME/.profile
3558
3559
3560 /tmp/sh*
3561
3562
3563 /dev/null
3564
3566 See attributes(5) for descriptions of the following attributes:
3567
3568 /usr/bin/ksh, /usr/bin/rksh
3569 ┌─────────────────────────────┬─────────────────────────────┐
3570 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
3571 ├─────────────────────────────┼─────────────────────────────┤
3572 │Availability │SUNWcsu │
3573 ├─────────────────────────────┼─────────────────────────────┤
3574 │CSI │Enabled │
3575 └─────────────────────────────┴─────────────────────────────┘
3576
3577 /usr/xpg4/bin/sh
3578 ┌─────────────────────────────┬─────────────────────────────┐
3579 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
3580 ├─────────────────────────────┼─────────────────────────────┤
3581 │Availability │SUNWxcu4 │
3582 ├─────────────────────────────┼─────────────────────────────┤
3583 │CSI │Enabled │
3584 ├─────────────────────────────┼─────────────────────────────┤
3585 │Interface Stability │Standard │
3586 └─────────────────────────────┴─────────────────────────────┘
3587
3589 cat(1), cd(1), chmod(1), cut(1), echo(1), env(1), getoptcvt(1),
3590 jobs(1), login(1), newgrp(1), paste(1), pfksh(1), pfexec(1), ps(1),
3591 shell_builtins(1), stty(1), test(1), vi(1), dup(2), exec(2), fork(2),
3592 ioctl(2), lseek(2), pipe(2), ulimit(2), umask(2), rand(3C), signal(3C),
3593 signal.h(3HEAD), wait(3C), a.out(4), profile(4), attributes(5), envi‐
3594 ron(5), largefile(5), standards(5)
3595
3596
3597 Morris I. Bolsky and David G. Korn, The KornShell Command and Program‐
3598 ming Language, Prentice Hall, 1989.
3599
3601 The use of setuid shell scripts is strongly discouraged.
3602
3604 If a command which is a tracked alias is executed, and then a command
3605 with the same name is installed in a directory in the search path
3606 before the directory where the original command was found, the shell
3607 continues to exec the original command. Use the -t option of the alias
3608 command to correct this situation.
3609
3610
3611 Some very old shell scripts contain a ^ as a synonym for the pipe char‐
3612 acter |.
3613
3614
3615 Using the fc built-in command within a compound command causes the
3616 whole command to disappear from the history file.
3617
3618
3619 The built-in command .file reads the whole file before any commands are
3620 executed. Therefore, alias and unalias commands in the file does not
3621 apply to any functions defined in the file.
3622
3623
3624 When the shell executes a shell script that attempts to execute a non-
3625 existent command interpreter, the shell returns an erroneous diagnostic
3626 message that the shell script file does not exist.
3627
3628
3629
3630SunOS 5.11 29 Mar 2007 ksh(1)