don't bother resolving onbld python module deps
[unleashed.git] / bin / ksh / ksh.1
blob5dcefe1d456b6dc8a86c7ca068b37b0372b7f65b
1 .\"     $OpenBSD: ksh.1,v 1.201 2018/06/18 17:03:58 millert Exp $
2 .\"
3 .\"     Public Domain
4 .\"
5 .Dd $Mdocdate: June 18 2018 $
6 .Dt KSH 1
7 .Os
8 .Sh NAME
9 .Nm ksh ,
10 .Nm rksh
11 .Nd public domain Korn shell
12 .Sh SYNOPSIS
13 .Nm ksh
14 .Bk -words
15 .Op Fl +abCefhiklmnpruvXx
16 .Op Fl +o Ar option
17 .Op Fl c Ar string | Fl s | Ar file Op Ar argument ...
18 .Ek
19 .Sh DESCRIPTION
20 .Nm
21 is a command interpreter intended for both interactive and shell
22 script use.
23 Its command language is a superset of the
24 .Xr sh 1
25 shell language.
26 .Pp
27 The options are as follows:
28 .Bl -tag -width Ds
29 .It Fl c Ar string
30 .Nm
31 will execute the command(s) contained in
32 .Ar string .
33 .It Fl i
34 Interactive shell.
35 A shell is
36 .Dq interactive
37 if this
38 option is used or if both standard input and standard error are attached
39 to a
40 .Xr tty 4 .
41 An interactive shell has job control enabled, ignores the
42 .Dv SIGINT ,
43 .Dv SIGQUIT ,
44 and
45 .Dv SIGTERM
46 signals, and prints prompts before reading input (see the
47 .Ev PS1
48 and
49 .Ev PS2
50 parameters).
51 For non-interactive shells, the
52 .Ic trackall
53 option is on by default (see the
54 .Ic set
55 command below).
56 .It Fl l
57 Login shell.
58 If the basename the shell is called with (i.e. argv[0])
59 starts with
60 .Ql -
61 or if this option is used,
62 the shell is assumed to be a login shell and the shell reads and executes
63 the contents of
64 .Pa /etc/profile
65 and
66 .Pa $HOME/.profile
67 if they exist and are readable.
68 .It Fl p
69 Privileged shell.
70 A shell is
71 .Dq privileged
72 if this option is used
73 or if the real user ID or group ID does not match the
74 effective user ID or group ID (see
75 .Xr getuid 2
76 and
77 .Xr getgid 2 ) .
78 A privileged shell does not process
79 .Pa $HOME/.profile
80 nor the
81 .Ev ENV
82 parameter (see below).
83 Instead, the file
84 .Pa /etc/suid_profile
85 is processed.
86 Clearing the privileged option causes the shell to set
87 its effective user ID (group ID) to its real user ID (group ID).
88 .It Fl r
89 Restricted shell.
90 A shell is
91 .Dq restricted
92 if this
93 option is used;
94 if the basename the shell was invoked with was
95 .Dq rksh ;
96 or if the
97 .Ev SHELL
98 parameter is set to
99 .Dq rksh .
100 The following restrictions come into effect after the shell processes any
101 profile and
102 .Ev ENV
103 files:
105 .Bl -bullet -compact
108 .Ic cd
109 command is disabled.
112 .Ev SHELL ,
113 .Ev ENV ,
115 .Ev PATH
116 parameters cannot be changed.
118 Command names can't be specified with absolute or relative paths.
121 .Fl p
122 option of the built-in command
123 .Ic command
124 can't be used.
126 Redirections that create files can't be used (i.e.\&
127 .Sq Cm > ,
128 .Sq Cm >| ,
129 .Sq Cm >> ,
130 .Sq Cm <> ) .
132 .It Fl s
133 The shell reads commands from standard input; all non-option arguments
134 are positional parameters.
137 In addition to the above, the options described in the
138 .Ic set
139 built-in command can also be used on the command line:
140 both
141 .Op Fl +abCefhkmnuvXx
143 .Op Fl +o Ar option
144 can be used for single letter or long options, respectively.
146 If neither the
147 .Fl c
148 nor the
149 .Fl s
150 option is specified, the first non-option argument specifies the name
151 of a file the shell reads commands from.
152 If there are no non-option
153 arguments, the shell reads commands from the standard input.
154 The name of the shell (i.e. the contents of $0)
155 is determined as follows: if the
156 .Fl c
157 option is used and there is a non-option argument, it is used as the name;
158 if commands are being read from a file, the file is used as the name;
159 otherwise, the basename the shell was called with (i.e. argv[0]) is used.
161 If the
162 .Ev ENV
163 parameter is set when an interactive shell starts (or,
164 in the case of login shells,
165 after any profiles are processed), its value is subjected to parameter,
166 command, arithmetic, and tilde
167 .Pq Sq ~
168 substitution and the resulting file
169 (if any) is read and executed.
170 In order to have an interactive (as opposed to login) shell
171 process a startup file,
172 .Ev ENV
173 may be set and exported (see below) in
174 .Pa $HOME/.profile
175 \- future interactive shell invocations will process any file pointed to by
176 .Ev $ENV :
178 .Dl export ENV=$HOME/.kshrc
180 .Pa $HOME/.kshrc
181 is then free to specify instructions for interactive shells.
182 For example, the global configuration file may be sourced:
183 .Bd -literal -offset indent
184 \&. /etc/ksh.kshrc
187 The above strategy may be employed to keep
188 setup procedures for login shells in
189 .Pa $HOME/.profile
190 and setup procedures for interactive shells in
191 .Pa $HOME/.kshrc .
192 Of course, since login shells are also interactive,
193 any commands placed in
194 .Pa $HOME/.kshrc
195 will be executed by login shells too.
197 The exit status of the shell is 127 if the command file specified on the
198 command line could not be opened, or non-zero if a fatal syntax error
199 occurred during the execution of a script.
200 In the absence of fatal errors,
201 the exit status is that of the last command executed, or zero, if no
202 command is executed.
203 .Ss Command syntax
204 The shell begins parsing its input by breaking it into
205 .Em words .
206 Words, which are sequences of characters, are delimited by unquoted whitespace
207 characters (space, tab, and newline) or meta-characters
209 .Ql < ,
210 .Ql > ,
211 .Ql | ,
212 .Ql \&; ,
213 .Ql \&( ,
214 .Ql \&) ,
216 .Ql &
217 .Pc .
218 Aside from delimiting words, spaces and tabs are ignored, while newlines
219 usually delimit commands.
220 The meta-characters are used in building the following
221 .Em tokens :
222 .Sq Cm < ,
223 .Sq Cm <& ,
224 .Sq Cm << ,
225 .Sq Cm > ,
226 .Sq Cm >& ,
227 .Sq Cm >> ,
228 etc. are used to specify redirections (see
229 .Sx Input/output redirection
230 below);
231 .Ql |
232 is used to create pipelines;
233 .Ql |&
234 is used to create co-processes (see
235 .Sx Co-processes
236 below);
237 .Ql \&;
238 is used to separate commands;
239 .Ql &
240 is used to create asynchronous pipelines;
241 .Ql &&
243 .Ql ||
244 are used to specify conditional execution;
245 .Ql ;;
246 is used in
247 .Ic case
248 statements;
249 .Ql (( .. ))
250 is used in arithmetic expressions;
251 and lastly,
252 .Ql \&( .. )\&
253 is used to create subshells.
255 Whitespace and meta-characters can be quoted individually using a backslash
256 .Pq Sq \e ,
257 or in groups using double
258 .Pq Sq \&"
259 or single
260 .Pq Sq '
261 quotes.
262 The following characters are also treated specially by the
263 shell and must be quoted if they are to represent themselves:
264 .Ql \e ,
265 .Ql \&" ,
266 .Ql ' ,
267 .Ql # ,
268 .Ql $ ,
269 .Ql ` ,
270 .Ql ~ ,
271 .Ql { ,
272 .Ql } ,
273 .Ql * ,
274 .Ql \&? ,
276 .Ql \&[ .
277 The first three of these are the above mentioned quoting characters (see
278 .Sx Quoting
279 below);
280 .Ql # ,
281 if used at the beginning of a word, introduces a comment \(em everything after
283 .Ql #
284 up to the nearest newline is ignored;
285 .Ql $
286 is used to introduce parameter, command, and arithmetic substitutions (see
287 .Sx Substitution
288 below);
289 .Ql `
290 introduces an old-style command substitution (see
291 .Sx Substitution
292 below);
293 .Ql ~
294 begins a directory expansion (see
295 .Sx Tilde expansion
296 below);
297 .Ql {
299 .Ql }
300 delimit
301 .Xr csh 1 Ns -style
302 alternations (see
303 .Sx Brace expansion
304 below);
305 and finally,
306 .Ql * ,
307 .Ql \&? ,
309 .Ql \&[
310 are used in file name generation (see
311 .Sx File name patterns
312 below).
314 As words and tokens are parsed, the shell builds commands, of which there
315 are two basic types:
316 .Em simple-commands ,
317 typically programs that are executed, and
318 .Em compound-commands ,
319 such as
320 .Ic for
322 .Ic if
323 statements, grouping constructs, and function definitions.
325 A simple-command consists of some combination of parameter assignments
326 (see
327 .Sx Parameters
328 below),
329 input/output redirections (see
330 .Sx Input/output redirections
331 below),
332 and command words; the only restriction is that parameter assignments come
333 before any command words.
334 The command words, if any, define the command
335 that is to be executed and its arguments.
336 The command may be a shell built-in command, a function,
337 or an external command
338 (i.e. a separate executable file that is located using the
339 .Ev PATH
340 parameter; see
341 .Sx Command execution
342 below).
344 All command constructs have an exit status.
345 For external commands,
346 this is related to the status returned by
347 .Xr wait 2
348 (if the command could not be found, the exit status is 127; if it could not
349 be executed, the exit status is 126).
350 The exit status of other command
351 constructs (built-in commands, functions, compound-commands, pipelines, lists,
352 etc.) are all well-defined and are described where the construct is
353 described.
354 The exit status of a command consisting only of parameter
355 assignments is that of the last command substitution performed during the
356 parameter assignment or 0 if there were no command substitutions.
358 Commands can be chained together using the
359 .Ql |
360 token to form pipelines, in which the standard output of each command but the
361 last is piped (see
362 .Xr pipe 2 )
363 to the standard input of the following command.
364 The exit status of a pipeline is that of its last command.
365 A pipeline may be prefixed by the
366 .Ql \&!
367 reserved word, which causes the exit status of the pipeline to be logically
368 complemented: if the original status was 0, the complemented status will be 1;
369 if the original status was not 0, the complemented status will be 0.
371 .Em Lists
372 of commands can be created by separating pipelines by any of the following
373 tokens:
374 .Ql && ,
375 .Ql || ,
376 .Ql & ,
377 .Ql |& ,
379 .Ql \&; .
380 The first two are for conditional execution:
381 .Dq Ar cmd1 No && Ar cmd2
382 executes
383 .Ar cmd2
384 only if the exit status of
385 .Ar cmd1
386 is zero;
387 .Ql ||
388 is the opposite \(em
389 .Ar cmd2
390 is executed only if the exit status of
391 .Ar cmd1
392 is non-zero.
393 .Ql &&
395 .Ql ||
396 have equal precedence which is higher than that of
397 .Ql & ,
398 .Ql |& ,
400 .Ql \&; ,
401 which also have equal precedence.
403 .Ql &&
405 .Ql ||
406 operators are
407 .Qq left-associative .
408 For example, both of these commands will print only
409 .Qq bar :
410 .Bd -literal -offset indent
411 $ false && echo foo || echo bar
412 $ true || echo foo && echo bar
416 .Ql &
417 token causes the preceding command to be executed asynchronously; that is,
418 the shell starts the command but does not wait for it to complete (the shell
419 does keep track of the status of asynchronous commands; see
420 .Sx Job control
421 below).
422 When an asynchronous command is started when job control is disabled
423 (i.e. in most scripts), the command is started with signals
424 .Dv SIGINT
426 .Dv SIGQUIT
427 ignored and with input redirected from
428 .Pa /dev/null
429 (however, redirections specified in the asynchronous command have precedence).
431 .Ql |&
432 operator starts a co-process which is a special kind of asynchronous process
433 (see
434 .Sx Co-processes
435 below).
436 A command must follow the
437 .Ql &&
439 .Ql ||
440 operators, while it need not follow
441 .Ql & ,
442 .Ql |& ,
444 .Ql \&; .
445 The exit status of a list is that of the last command executed, with the
446 exception of asynchronous lists, for which the exit status is 0.
448 Compound commands are created using the following reserved words.
449 These words
450 are only recognized if they are unquoted and if they are used as the first
451 word of a command (i.e. they can't be preceded by parameter assignments or
452 redirections):
453 .Bd -literal -offset indent
454 case   esac       in       until   ((   }
455 do     fi         name     while   ))
456 done   for        select   !       [[
457 elif   function   then     (       ]]
458 else   if         time     )       {
461 .Sy Note :
462 Some shells (but not this one) execute control structure commands in a
463 subshell when one or more of their file descriptors are redirected, so any
464 environment changes inside them may fail.
465 To be portable, the
466 .Ic exec
467 statement should be used instead to redirect file descriptors before the
468 control structure.
470 In the following compound command descriptions, command lists (denoted as
471 .Em list )
472 that are followed by reserved words must end with a semicolon, a newline, or
473 a (syntactically correct) reserved word.
474 For example, the following are all valid:
475 .Bd -literal -offset indent
476 $ { echo foo; echo bar; }
477 $ { echo foo; echo bar<newline> }
478 $ { { echo foo; echo bar; } }
481 This is not valid:
483 .Dl $ { echo foo; echo bar }
484 .Bl -tag -width Ds
485 .It Pq Ar list
486 Execute
487 .Ar list
488 in a subshell.
489 There is no implicit way to pass environment changes from a
490 subshell back to its parent.
491 .It { Ar list ; No }
492 Compound construct;
493 .Ar list
494 is executed, but not in a subshell.
495 Note that
496 .Ql {
498 .Ql }
499 are reserved words, not meta-characters.
500 .It Xo Ic case Ar word Cm in
501 .Oo Op \&(
502 .Ar pattern
503 .Op | Ar pattern
504 .No ... )
505 .Ar list No ;;\ \& Oc ... Cm esac
508 .Ic case
509 statement attempts to match
510 .Ar word
511 against a specified
512 .Ar pattern ;
514 .Ar list
515 associated with the first successfully matched pattern is executed.
516 Patterns used in
517 .Ic case
518 statements are the same as those used for file name patterns except that the
519 restrictions regarding
520 .Ql \&.
522 .Ql /
523 are dropped.
524 Note that any unquoted space before and after a pattern is
525 stripped; any space within a pattern must be quoted.
526 Both the word and the
527 patterns are subject to parameter, command, and arithmetic substitution, as
528 well as tilde substitution.
529 For historical reasons, open and close braces may be used instead of
530 .Cm in
532 .Cm esac
533 e.g.\&
534 .Ic case $foo { *) echo bar; } .
535 The exit status of a
536 .Ic case
537 statement is that of the executed
538 .Ar list ;
539 if no
540 .Ar list
541 is executed, the exit status is zero.
542 .It Xo Ic for Ar name
543 .Oo Cm in Ar word No ... Oc ;
544 .Cm do Ar list ; Cm done
546 For each
547 .Ar word
548 in the specified word list, the parameter
549 .Ar name
550 is set to the word and
551 .Ar list
552 is executed.
554 .Cm in
555 is not used to specify a word list, the positional parameters
556 ($1, $2, etc.)\&
557 are used instead.
558 For historical reasons, open and close braces may be used instead of
559 .Cm do
561 .Cm done
562 e.g.\&
563 .Ic for i; { echo $i; } .
564 The exit status of a
565 .Ic for
566 statement is the last exit status of
567 .Ar list ;
569 .Ar list
570 is never executed, the exit status is zero.
571 .It Xo Ic if Ar list ;
572 .Cm then Ar list ;
573 .Oo Cm elif Ar list ;
574 .Cm then Ar list ; Oc ...
575 .Oo Cm else Ar list ; Oc
576 .Cm fi
578 If the exit status of the first
579 .Ar list
580 is zero, the second
581 .Ar list
582 is executed; otherwise, the
583 .Ar list
584 following the
585 .Cm elif ,
586 if any, is executed with similar consequences.
587 If all the lists following the
588 .Ic if
590 .Cm elif Ns s
591 fail (i.e. exit with non-zero status), the
592 .Ar list
593 following the
594 .Cm else
595 is executed.
596 The exit status of an
597 .Ic if
598 statement is that of non-conditional
599 .Ar list
600 that is executed; if no non-conditional
601 .Ar list
602 is executed, the exit status is zero.
603 .It Xo Ic select Ar name
604 .Oo Cm in Ar word No ... Oc ;
605 .Cm do Ar list ; Cm done
608 .Ic select
609 statement provides an automatic method of presenting the user with a menu and
610 selecting from it.
611 An enumerated list of the specified
612 .Ar word Ns (s)
613 is printed on standard error, followed by a prompt
615 .Ev PS3 :
616 normally
617 .Sq #?\ \&
618 .Pc .
619 A number corresponding to one of the enumerated words is then read from
620 standard input,
621 .Ar name
622 is set to the selected word (or unset if the selection is not valid),
623 .Ev REPLY
624 is set to what was read (leading/trailing space is stripped), and
625 .Ar list
626 is executed.
627 If a blank line (i.e. zero or more
628 .Ev IFS
629 characters) is entered, the menu is reprinted without executing
630 .Ar list .
632 When
633 .Ar list
634 completes, the enumerated list is printed if
635 .Ev REPLY
637 .Dv NULL ,
638 the prompt is printed, and so on.
639 This process continues until an end-of-file
640 is read, an interrupt is received, or a
641 .Ic break
642 statement is executed inside the loop.
644 .Dq in word ...
645 is omitted, the positional parameters are used
646 (i.e. $1, $2, etc.).
647 For historical reasons, open and close braces may be used instead of
648 .Cm do
650 .Cm done
651 e.g.\&
652 .Ic select i; { echo $i; } .
653 The exit status of a
654 .Ic select
655 statement is zero if a
656 .Ic break
657 statement is used to exit the loop, non-zero otherwise.
658 .It Xo Ic until Ar list ;
659 .Cm do Ar list ;
660 .Cm done
662 This works like
663 .Ic while ,
664 except that the body is executed only while the exit status of the first
665 .Ar list
666 is non-zero.
667 .It Xo Ic while Ar list ;
668 .Cm do Ar list ;
669 .Cm done
672 .Ic while
673 is a pre-checked loop.
674 Its body is executed as often as the exit status of the first
675 .Ar list
676 is zero.
677 The exit status of a
678 .Ic while
679 statement is the last exit status of the
680 .Ar list
681 in the body of the loop; if the body is not executed, the exit status is zero.
682 .It Xo Ic function Ar name
683 .No { Ar list ; No }
685 Defines the function
686 .Ar name
687 (see
688 .Sx Functions
689 below).
690 Note that redirections specified after a function definition are
691 performed whenever the function is executed, not when the function definition
692 is executed.
693 .It Ar name Ns () Ar command
694 Mostly the same as
695 .Ic function
696 (see
697 .Sx Functions
698 below).
699 .It Xo Ic time Op Fl p
700 .Op Ar pipeline
703 .Ic time
704 reserved word is described in the
705 .Sx Command execution
706 section.
707 .It Ic (( Ar expression Cm ))
708 The arithmetic expression
709 .Ar expression
710 is evaluated; equivalent to
711 .Ic let Ar expression
712 (see
713 .Sx Arithmetic expressions
714 and the
715 .Ic let
716 command, below).
717 .It Ic [[ Ar expression Cm ]]
718 Similar to the
719 .Ic test
721 .Ic \&[ No ... Cm \&]
722 commands (described later), with the following exceptions:
723 .Bl -bullet -offset indent
725 Field splitting and file name generation are not performed on arguments.
728 .Fl a
729 .Pq AND
731 .Fl o
732 .Pq OR
733 operators are replaced with
734 .Ql &&
736 .Ql || ,
737 respectively.
739 Operators (e.g.\&
740 .Sq Fl f ,
741 .Sq = ,
742 .Sq \&! )
743 must be unquoted.
745 The second operand of the
746 .Sq !=
748 .Sq =
749 expressions are patterns (e.g. the comparison
750 .Ic [[ foobar = f*r ]]
751 succeeds).
753 There are two additional binary operators,
754 .Ql <
756 .Ql > ,
757 which return true if their first string operand is less than, or greater than,
758 their second string operand, respectively.
760 The single argument form of
761 .Ic test ,
762 which tests if the argument has a non-zero length, is not valid; explicit
763 operators must always be used e.g. instead of
764 .No \&[ Ar str No \&]
766 .No \&[[ -n Ar str No \&]] .
768 Parameter, command, and arithmetic substitutions are performed as expressions
769 are evaluated and lazy expression evaluation is used for the
770 .Ql &&
772 .Ql ||
773 operators.
774 This means that in the following statement,
775 .Ic $(< foo)
776 is evaluated if and only if the file
777 .Pa foo
778 exists and is readable:
779 .Bd -literal -offset indent
780 $ [[ -r foo && $(< foo) = b*r ]]
784 .Ss Quoting
785 Quoting is used to prevent the shell from treating characters or words
786 specially.
787 There are three methods of quoting.
788 First,
789 .Ql \e
790 quotes the following character, unless it is at the end of a line, in which
791 case both the
792 .Ql \e
793 and the newline are stripped.
794 Second, a single quote
795 .Pq Sq '
796 quotes everything up to the next single quote (this may span lines).
797 Third, a double quote
798 .Pq Sq \&"
799 quotes all characters, except
800 .Ql $ ,
801 .Ql `
803 .Ql \e ,
804 up to the next unquoted double quote.
805 .Ql $
807 .Ql `
808 inside double quotes have their usual meaning (i.e. parameter, command, or
809 arithmetic substitution) except no field splitting is carried out on the
810 results of double-quoted substitutions.
811 If a
812 .Ql \e
813 inside a double-quoted string is followed by
814 .Ql \e ,
815 .Ql $ ,
816 .Ql ` ,
818 .Ql \&" ,
819 it is replaced by the second character; if it is followed by a newline, both
821 .Ql \e
822 and the newline are stripped; otherwise, both the
823 .Ql \e
824 and the character following are unchanged.
825 .Ss Aliases
826 There are two types of aliases: normal command aliases and tracked aliases.
827 Command aliases are normally used as a short hand for a long or often used
828 command.
829 The shell expands command aliases (i.e. substitutes the alias name
830 for its value) when it reads the first word of a command.
831 An expanded alias is re-processed to check for more aliases.
832 If a command alias ends in a
833 space or tab, the following word is also checked for alias expansion.
834 The alias expansion process stops when a word that is not an alias is found,
835 when a quoted word is found, or when an alias word that is currently being
836 expanded is found.
838 The following command aliases are defined automatically by the shell:
840 .Bl -item -compact -offset indent
842 .Ic autoload Ns ='typeset -fu'
844 .Ic functions Ns ='typeset -f'
846 .Ic hash Ns ='alias -t'
848 .Ic history Ns ='fc -l'
850 .Ic integer Ns ='typeset -i'
852 .Ic local Ns ='typeset'
854 .Ic login Ns ='exec login'
856 .Ic nohup Ns ='nohup '
858 .Ic r Ns ='fc -s'
860 .Ic stop Ns ='kill -STOP'
863 Tracked aliases allow the shell to remember where it found a particular
864 command.
865 The first time the shell does a path search for a command that is
866 marked as a tracked alias, it saves the full path of the command.
867 The next
868 time the command is executed, the shell checks the saved path to see that it
869 is still valid, and if so, avoids repeating the path search.
870 Tracked aliases can be listed and created using
871 .Ic alias -t .
872 Note that changing the
873 .Ev PATH
874 parameter clears the saved paths for all tracked aliases.
875 If the
876 .Ic trackall
877 option is set (i.e.\&
878 .Ic set -o Ic trackall
880 .Ic set -h ) ,
881 the shell tracks all commands.
882 This option is set automatically for non-interactive shells.
883 For interactive shells, only the following commands are
884 automatically tracked:
885 .Xr cat 1 ,
886 .Xr cc 1 ,
887 .Xr chmod 1 ,
888 .Xr cp 1 ,
889 .Xr date 1 ,
890 .Xr ed 1 ,
891 .Sy emacs ,
892 .Xr grep 1 ,
893 .Xr ls 1 ,
894 .Xr mail 1 ,
895 .Xr make 1 ,
896 .Xr mv 1 ,
897 .Xr pr 1 ,
898 .Xr rm 1 ,
899 .Xr sed 1 ,
900 .Xr sh 1 ,
901 .Xr vi 1 ,
903 .Xr who 1 .
904 .Ss Substitution
905 The first step the shell takes in executing a simple-command is to perform
906 substitutions on the words of the command.
907 There are three kinds of
908 substitution: parameter, command, and arithmetic.
909 Parameter substitutions,
910 which are described in detail in the next section, take the form
911 .Pf $ Ar name
913 .Pf ${ Ar ... Ns } ;
914 command substitutions take the form
915 .Pf $( Ar command )
917 .Pf ` Ar command Ns ` ;
918 and arithmetic substitutions take the form
919 .Pf $(( Ar expression ) ) .
921 If a substitution appears outside of double quotes, the results of the
922 substitution are generally subject to word or field splitting according to
923 the current value of the
924 .Ev IFS
925 parameter.
927 .Ev IFS
928 parameter specifies a list of characters which are used to break a string up
929 into several words; any characters from the set space, tab, and newline that
930 appear in the
931 .Ev IFS
932 characters are called
933 .Dq IFS whitespace .
934 Sequences of one or more
935 .Ev IFS
936 whitespace characters, in combination with zero or one
937 .Pf non- Ev IFS
938 whitespace
939 characters, delimit a field.
940 As a special case, leading and trailing
941 .Ev IFS
942 whitespace is stripped (i.e. no leading or trailing empty field is created by
943 it); leading
944 .Pf non- Ev IFS
945 whitespace does create an empty field.
947 Example: If
948 .Ev IFS
949 is set to
950 .Dq <space>: ,
951 and VAR is set to
952 .Dq <space>A<space>:<space><space>B::D ,
953 the substitution for $VAR results in four fields:
954 .Sq A ,
955 .Sq B ,
957 (an empty field),
959 .Sq D .
960 Note that if the
961 .Ev IFS
962 parameter is set to the
963 .Dv NULL
964 string, no field splitting is done; if the parameter is unset, the default
965 value of space, tab, and newline is used.
967 Also, note that the field splitting applies only to the immediate result of
968 the substitution.
969 Using the previous example, the substitution for $VAR:E
970 results in the fields:
971 .Sq A ,
972 .Sq B ,
973 .Sq ,
975 .Sq D:E ,
977 .Sq A ,
978 .Sq B ,
979 .Sq ,
980 .Sq D ,
982 .Sq E .
983 This behavior is POSIX compliant, but incompatible with some other shell
984 implementations which do field splitting on the word which contained the
985 substitution or use
986 .Dv IFS
987 as a general whitespace delimiter.
989 The results of substitution are, unless otherwise specified, also subject to
990 brace expansion and file name expansion (see the relevant sections below).
992 A command substitution is replaced by the output generated by the specified
993 command, which is run in a subshell.
995 .Pf $( Ar command )
996 substitutions, normal quoting rules are used when
997 .Ar command
998 is parsed; however, for the
999 .Pf ` Ar command Ns `
1000 form, a
1001 .Ql \e
1002 followed by any of
1003 .Ql $ ,
1004 .Ql ` ,
1006 .Ql \e
1007 is stripped (a
1008 .Ql \e
1009 followed by any other character is unchanged).
1010 As a special case in command substitutions, a command of the form
1011 .Pf < Ar file
1012 is interpreted to mean substitute the contents of
1013 .Ar file .
1014 Note that
1015 .Ic $(< foo)
1016 has the same effect as
1017 .Ic $(cat foo) ,
1018 but it is carried out more efficiently because no process is started.
1020 Arithmetic substitutions are replaced by the value of the specified expression.
1021 For example, the command
1022 .Ic echo $((2+3*4))
1023 prints 14.
1025 .Sx Arithmetic expressions
1026 for a description of an expression.
1027 .Ss Parameters
1028 Parameters are shell variables; they can be assigned values and their values
1029 can be accessed using a parameter substitution.
1030 A parameter name is either one
1031 of the special single punctuation or digit character parameters described
1032 below, or a letter followed by zero or more letters or digits
1034 .Ql _
1035 counts as a letter
1036 .Pc .
1037 The latter form can be treated as arrays by appending an array index of the
1038 form
1039 .Op Ar expr
1040 where
1041 .Ar expr
1042 is an arithmetic expression.
1043 Parameter substitutions take the form
1044 .Pf $ Ar name ,
1045 .Pf ${ Ar name Ns } ,
1047 .Sm off
1048 .Pf ${ Ar name Bo Ar expr Bc }
1049 .Sm on
1050 where
1051 .Ar name
1052 is a parameter name.
1054 .Ar expr
1055 is a literal
1056 .Ql @
1057 then the named array is expanded using the same quoting rules as
1058 .Ql $@ ,
1059 while if
1060 .Ar expr
1061 is a literal
1062 .Ql *
1063 then the named array is expanded using the same quoting rules as
1064 .Ql $* .
1065 If substitution is performed on a parameter
1066 (or an array parameter element)
1067 that is not set, a null string is substituted unless the
1068 .Ic nounset
1069 option
1071 .Ic set Fl o Ic nounset
1073 .Ic set Fl u
1075 is set, in which case an error occurs.
1077 Parameters can be assigned values in a number of ways.
1078 First, the shell implicitly sets some parameters like
1079 .Ql # ,
1080 .Ql PWD ,
1082 .Ql $ ;
1083 this is the only way the special single character parameters are set.
1084 Second, parameters are imported from the shell's environment at startup.
1085 Third, parameters can be assigned values on the command line: for example,
1086 .Ic FOO=bar
1087 sets the parameter
1088 .Dq FOO
1090 .Dq bar ;
1091 multiple parameter assignments can be given on a single command line and they
1092 can be followed by a simple-command, in which case the assignments are in
1093 effect only for the duration of the command (such assignments are also
1094 exported; see below for the implications of this).
1095 Note that both the parameter name and the
1096 .Ql =
1097 must be unquoted for the shell to recognize a parameter assignment.
1098 The fourth way of setting a parameter is with the
1099 .Ic export ,
1100 .Ic readonly ,
1102 .Ic typeset
1103 commands; see their descriptions in the
1104 .Sx Command execution
1105 section.
1106 Fifth,
1107 .Ic for
1109 .Ic select
1110 loops set parameters as well as the
1111 .Ic getopts ,
1112 .Ic read ,
1114 .Ic set -A
1115 commands.
1116 Lastly, parameters can be assigned values using assignment operators
1117 inside arithmetic expressions (see
1118 .Sx Arithmetic expressions
1119 below) or using the
1120 .Pf ${ Ar name Ns = Ns Ar value Ns }
1121 form of the parameter substitution (see below).
1123 Parameters with the export attribute (set using the
1124 .Ic export
1126 .Ic typeset Fl x
1127 commands, or by parameter assignments followed by simple commands) are put in
1128 the environment (see
1129 .Xr environ 7 )
1130 of commands run by the shell as
1131 .Ar name Ns = Ns Ar value
1132 pairs.
1133 The order in which parameters appear in the environment of a command is
1134 unspecified.
1135 When the shell starts up, it extracts parameters and their values
1136 from its environment and automatically sets the export attribute for those
1137 parameters.
1139 Modifiers can be applied to the
1140 .Pf ${ Ar name Ns }
1141 form of parameter substitution:
1142 .Bl -tag -width Ds
1143 .Sm off
1144 .It ${ Ar name No :- Ar word No }
1145 .Sm on
1147 .Ar name
1148 is set and not
1149 .Dv NULL ,
1150 it is substituted; otherwise,
1151 .Ar word
1152 is substituted.
1153 .Sm off
1154 .It ${ Ar name No :+ Ar word No }
1155 .Sm on
1157 .Ar name
1158 is set and not
1159 .Dv NULL ,
1160 .Ar word
1161 is substituted; otherwise, nothing is substituted.
1162 .Sm off
1163 .It ${ Ar name No := Ar word No }
1164 .Sm on
1166 .Ar name
1167 is set and not
1168 .Dv NULL ,
1169 it is substituted; otherwise, it is assigned
1170 .Ar word
1171 and the resulting value of
1172 .Ar name
1173 is substituted.
1174 .Sm off
1175 .It ${ Ar name No :? Ar word No }
1176 .Sm on
1178 .Ar name
1179 is set and not
1180 .Dv NULL ,
1181 it is substituted; otherwise,
1182 .Ar word
1183 is printed on standard error (preceded by
1184 .Ar name : )
1185 and an error occurs (normally causing termination of a shell script, function,
1186 or script sourced using the
1187 .Sq \&.
1188 built-in).
1190 .Ar word
1191 is omitted, the string
1192 .Dq parameter null or not set
1193 is used instead.
1196 In the above modifiers, the
1197 .Ql \&:
1198 can be omitted, in which case the conditions only depend on
1199 .Ar name
1200 being set (as opposed to set and not
1201 .Dv NULL ) .
1203 .Ar word
1204 is needed, parameter, command, arithmetic, and tilde substitution are performed
1205 on it; if
1206 .Ar word
1207 is not needed, it is not evaluated.
1209 The following forms of parameter substitution can also be used:
1211 .Bl -tag -width Ds -compact
1212 .It Pf ${# Ar name Ns }
1213 The number of positional parameters if
1214 .Ar name
1216 .Ql * ,
1217 .Ql @ ,
1218 or not specified; otherwise the length of the string value of parameter
1219 .Ar name .
1221 .It Pf ${# Ar name Ns [*]}
1222 .It Pf ${# Ar name Ns [@]}
1223 The number of elements in the array
1224 .Ar name .
1226 .It Pf ${ Ar name Ns # Ns Ar pattern Ns }
1227 .It Pf ${ Ar name Ns ## Ns Ar pattern Ns }
1229 .Ar pattern
1230 matches the beginning of the value of parameter
1231 .Ar name ,
1232 the matched text is deleted from the result of substitution.
1233 A single
1234 .Ql #
1235 results in the shortest match, and two
1236 of them result in the longest match.
1238 .It Pf ${ Ar name Ns % Ns Ar pattern Ns }
1239 .It Pf ${ Ar name Ns %% Ns Ar pattern Ns }
1240 Like ${..#..} substitution, but it deletes from the end of the value.
1243 The following special parameters are implicitly set by the shell and cannot be
1244 set directly using assignments:
1245 .Bl -tag -width "1 ... 9"
1246 .It Ev \&!
1247 Process ID of the last background process started.
1248 If no background processes have been started, the parameter is not set.
1249 .It Ev \&#
1250 The number of positional parameters ($1, $2, etc.).
1251 .It Ev \&$
1252 The PID of the shell, or the PID of the original shell if it is a subshell.
1254 .Em NOT
1255 use this mechanism for generating temporary file names; see
1256 .Xr mktemp 1
1257 instead.
1258 .It Ev -
1259 The concatenation of the current single letter options (see the
1260 .Ic set
1261 command below for a list of options).
1262 .It Ev \&?
1263 The exit status of the last non-asynchronous command executed.
1264 If the last command was killed by a signal,
1265 .Ic $?\&
1266 is set to 128 plus the signal number.
1267 .It Ev 0
1268 The name of the shell, determined as follows:
1269 the first argument to
1271 if it was invoked with the
1272 .Fl c
1273 option and arguments were given; otherwise the
1274 .Ar file
1275 argument, if it was supplied;
1276 or else the basename the shell was invoked with (i.e.\&
1277 .Li argv[0] ) .
1278 .Ev $0
1279 is also set to the name of the current script or
1280 the name of the current function, if it was defined with the
1281 .Ic function
1282 keyword (i.e. a Korn shell style function).
1283 .It Ev 1 No ... Ev 9
1284 The first nine positional parameters that were supplied to the shell, function,
1285 or script sourced using the
1286 .Sq \&.
1287 built-in.
1288 Further positional parameters may be accessed using
1289 .Pf ${ Ar number Ns } .
1290 .It Ev *
1291 All positional parameters (except parameter 0) i.e. $1, $2, $3, ...
1292 If used
1293 outside of double quotes, parameters are separate words (which are subjected
1294 to word splitting); if used within double quotes, parameters are separated
1295 by the first character of the
1296 .Ev IFS
1297 parameter (or the empty string if
1298 .Ev IFS
1300 .Dv NULL ) .
1301 .It Ev @
1302 Same as
1303 .Ic $* ,
1304 unless it is used inside double quotes, in which case a separate word is
1305 generated for each positional parameter.
1306 If there are no positional parameters, no word is generated.
1307 .Ic $@
1308 can be used to access arguments, verbatim, without losing
1309 .Dv NULL
1310 arguments or splitting arguments with spaces.
1313 The following parameters are set and/or used by the shell:
1314 .Bl -tag -width "EXECSHELL"
1315 .It Ev _ No (underscore)
1316 When an external command is executed by the shell, this parameter is set in the
1317 environment of the new process to the path of the executed command.
1318 In interactive use, this parameter is also set in the parent shell to the last
1319 word of the previous command.
1320 When
1321 .Ev MAILPATH
1322 messages are evaluated, this parameter contains the name of the file that
1323 changed (see the
1324 .Ev MAILPATH
1325 parameter, below).
1326 .It Ev CDPATH
1327 Search path for the
1328 .Ic cd
1329 built-in command.
1330 It works the same way as
1331 .Ev PATH
1332 for those directories not beginning with
1333 .Ql /
1335 .Ql .\&
1337 .Ic cd
1338 commands.
1339 Note that if
1340 .Ev CDPATH
1341 is set and does not contain
1342 .Sq \&.
1343 or contains an empty path, the current directory is not searched.
1344 Also, the
1345 .Ic cd
1346 built-in command will display the resulting directory when a match is found
1347 in any search path other than the empty path.
1348 .It Ev COLUMNS
1349 Set to the number of columns on the terminal or window.
1350 Currently set to the
1351 .Dq cols
1352 value as reported by
1353 .Xr stty 1
1354 if that value is non-zero.
1355 This parameter is used by the interactive line editing modes, and by the
1356 .Ic select ,
1357 .Ic set -o ,
1359 .Ic kill -l
1360 commands to format information columns.
1361 .It Ev EDITOR
1362 If the
1363 .Ev VISUAL
1364 parameter is not set, this parameter controls the command-line editing mode for
1365 interactive shells.
1366 See the
1367 .Ev VISUAL
1368 parameter below for how this works.
1370 Note:
1371 traditionally,
1372 .Ev EDITOR
1373 was used to specify the name of an (old-style) line editor, such as
1374 .Xr ed 1 ,
1376 .Ev VISUAL
1377 was used to specify a (new-style) screen editor, such as
1378 .Xr vi 1 .
1379 Hence if
1380 .Ev VISUAL
1381 is set, it overrides
1382 .Ev EDITOR .
1383 .It Ev ENV
1384 If this parameter is found to be set after any profile files are executed, the
1385 expanded value is used as a shell startup file.
1386 It typically contains function and alias definitions.
1387 .It Ev EXECSHELL
1388 If set, this parameter is assumed to contain the shell that is to be used to
1389 execute commands that
1390 .Xr execve 2
1391 fails to execute and which do not start with a
1392 .Dq #! Ns Ar shell
1393 sequence.
1394 .It Ev FCEDIT
1395 The editor used by the
1396 .Ic fc
1397 command (see below).
1398 .It Ev FPATH
1399 Like
1400 .Ev PATH ,
1401 but used when an undefined function is executed to locate the file defining the
1402 function.
1403 It is also searched when a command can't be found using
1404 .Ev PATH .
1406 .Sx Functions
1407 below for more information.
1408 .It Ev HISTCONTROL
1409 A colon separated list of history settings.
1411 .Li ignoredups
1412 is present, lines identical to the previous history line will not be saved.
1414 .Li ignorespace
1415 is present, lines starting with a space will not be saved.
1416 Unknown settings are ignored.
1417 .It Ev HISTFILE
1418 The name of the file used to store command history.
1419 When assigned to, history is loaded from the specified file.
1420 Also, several invocations of the shell
1421 running on the same machine will share history if their
1422 .Ev HISTFILE
1423 parameters all point to the same file.
1425 .Sy Note :
1427 .Ev HISTFILE
1428 isn't set, no history file is used.
1429 This is different from the original Korn shell, which uses
1430 .Pa $HOME/.sh_history .
1431 .It Ev HISTSIZE
1432 The number of commands normally stored for history.
1433 The default is 500.
1434 .It Ev HOME
1435 The default directory for the
1436 .Ic cd
1437 command and the value substituted for an unqualified
1438 .Ic ~
1439 (see
1440 .Sx Tilde expansion
1441 below).
1442 .It Ev IFS
1443 Internal field separator, used during substitution and by the
1444 .Ic read
1445 command, to split values into distinct arguments; normally set to space, tab,
1446 and newline.
1448 .Sx Substitution
1449 above for details.
1451 .Sy Note :
1452 This parameter is not imported from the environment when the shell is
1453 started.
1454 .It Ev KSH_VERSION
1455 The version of the shell and the date the version was created (read-only).
1456 .It Ev LINENO
1457 The line number of the function or shell script that is currently being
1458 executed.
1459 .It Ev LINES
1460 Set to the number of lines on the terminal or window.
1461 .It Ev MAIL
1462 If set, the user will be informed of the arrival of mail in the named file.
1463 This parameter is ignored if the
1464 .Ev MAILPATH
1465 parameter is set.
1466 .It Ev MAILCHECK
1467 How often, in seconds, the shell will check for mail in the file(s) specified
1469 .Ev MAIL
1471 .Ev MAILPATH .
1472 If set to 0, the shell checks before each prompt.
1473 The default is 600 (10 minutes).
1474 .It Ev MAILPATH
1475 A list of files to be checked for mail.
1476 The list is colon separated, and each file may be followed by a
1477 .Ql \&?
1478 and a message to be printed if new mail has arrived.
1479 Command, parameter, and
1480 arithmetic substitution is performed on the message and, during substitution,
1481 the parameter
1482 .Ic $_
1483 contains the name of the file.
1484 The default message is
1485 .Dq you have mail in $_ .
1486 .It Ev OLDPWD
1487 The previous working directory.
1488 Unset if
1489 .Ic cd
1490 has not successfully changed directories since the shell started, or if the
1491 shell doesn't know where it is.
1492 .It Ev OPTARG
1493 When using
1494 .Ic getopts ,
1495 it contains the argument for a parsed option, if it requires one.
1496 .It Ev OPTIND
1497 The index of the next argument to be processed when using
1498 .Ic getopts .
1499 Assigning 1 to this parameter causes
1500 .Ic getopts
1501 to process arguments from the beginning the next time it is invoked.
1502 .It Ev PATH
1503 A colon separated list of directories that are searched when looking for
1504 commands and files sourced using the
1505 .Sq \&.
1506 command (see below).
1507 An empty string resulting from a leading or trailing
1508 colon, or two adjacent colons, is treated as a
1509 .Sq \&.
1510 (the current directory).
1511 .It Ev POSIXLY_CORRECT
1512 If set, this parameter causes the
1513 .Ic posix
1514 option to be enabled.
1516 .Sx POSIX mode
1517 below.
1518 .It Ev PPID
1519 The process ID of the shell's parent (read-only).
1520 .It Ev PS1
1521 The primary prompt for interactive shells.
1522 Parameter, command, and arithmetic
1523 substitutions are performed,
1524 and the prompt string can be customised using
1525 backslash-escaped special characters.
1527 Note that since the command-line editors try to figure out how long the prompt
1528 is (so they know how far it is to the edge of the screen), escape codes in
1529 the prompt tend to mess things up.
1530 You can tell the shell not to count certain
1531 sequences (such as escape codes) by using the
1532 .Li \e[ Ns Ar ... Ns Li \e]
1533 substitution (see below) or by prefixing your prompt with a non-printing
1534 character (such as control-A) followed by a carriage return and then delimiting
1535 the escape codes with this non-printing character.
1536 By the way, don't blame me for
1537 this hack; it's in the original
1538 .Nm .
1540 The default prompt is the first part of the hostname, followed by
1541 .Sq $\ \&
1542 for non-root users,
1543 .Sq #\ \&
1544 for root.
1546 The following backslash-escaped special characters can be used
1547 to customise the prompt:
1549 .Bl -tag -width "\eD{format}XX" -compact
1550 .It Li \ea
1551 Insert an ASCII bell character.
1552 .It Li \ed
1553 The current date, in the format
1554 .Dq Day Month Date
1555 for example
1556 .Dq Wed Nov 03 .
1557 .It Li \eD Ns Brq Ar format
1558 The current date, with
1559 .Ar format
1560 converted by
1561 .Xr strftime 3 .
1562 The braces must be specified.
1563 .It Li \ee
1564 Insert an ASCII escape character.
1565 .It Li \eh
1566 The hostname, minus domain name.
1567 .It Li \eH
1568 The full hostname, including domain name.
1569 .It Li \ej
1570 Current number of jobs running
1571 (see
1572 .Sx Job control
1573 below).
1574 .It Li \el
1575 The controlling terminal.
1576 .It Li \en
1577 Insert a newline character.
1578 .It Li \er
1579 Insert a carriage return character.
1580 .It Li \es
1581 The name of the shell.
1582 .It Li \et
1583 The current time, in 24-hour HH:MM:SS format.
1584 .It Li \eT
1585 The current time, in 12-hour HH:MM:SS format.
1586 .It Li \e@
1587 The current time, in 12-hour HH:MM:SS AM/PM format.
1588 .It Li \eA
1589 The current time, in 24-hour HH:MM format.
1590 .It Li \eu
1591 The current user's username.
1592 .It Li \ev
1593 The current version of
1594 .Nm .
1595 .It Li \eV
1596 Like
1597 .Sq \ev ,
1598 but more verbose.
1599 .It Li \ew
1600 The current working directory.
1601 .Dv $HOME
1602 is abbreviated as
1603 .Sq ~ .
1604 .It Li \eW
1605 The basename of
1606 the current working directory.
1607 .Dv $HOME
1608 is abbreviated as
1609 .Sq ~ .
1610 .It Li \e!
1611 The current history number.
1612 An unescaped
1613 .Ql !\&
1614 will produce the current history number too,
1615 as per the POSIX specification.
1616 A literal
1617 .Ql \&!
1618 can be put in the prompt by placing
1619 .Ql !!
1621 .Ev PS1 .
1622 .It Li \e#
1623 The current command number.
1624 This could be different to the current history number,
1626 .Ev HISTFILE
1627 contains a history list from a previous session.
1628 .It Li \e$
1629 The default prompt character i.e.\&
1630 .Sq #
1631 if the effective UID is 0,
1632 otherwise
1633 .Sq $ .
1634 Since the shell interprets
1635 .Sq $
1636 as a special character within double quotes,
1637 it is safer in this case to escape the backslash
1638 than to try quoting it.
1639 .It Li \e Ns Ar nnn
1640 The octal character
1641 .Ar nnn .
1642 .It Li \e\e
1643 Insert a single backslash character.
1644 .It Li \e[
1645 Normally the shell keeps track of the number of characters in the prompt.
1646 Use of this sequence turns off that count.
1647 .It Li \e]
1648 Use of this sequence turns the count back on.
1651 Note that the backslash itself may be interpreted by the shell.
1652 Hence, to set
1653 .Ev PS1
1654 either escape the backslash itself,
1655 or use double quotes.
1656 The latter is more practical:
1657 .Bd -literal -offset indent
1658 PS1="\eu "
1661 This is a more complex example,
1662 which does not rely on the above backslash-escaped sequences.
1663 It embeds the current working directory,
1664 in reverse video,
1665 in the prompt string:
1666 .Bd -literal -offset indent
1667 x=$(print \e\e001)
1668 PS1="$x$(print \e\er)$x$(tput so)$x\e$PWD$x$(tput se)$x> "
1670 .It Ev PS2
1671 Secondary prompt string, by default
1672 .Sq >\ \& ,
1673 used when more input is needed to complete a command.
1674 .It Ev PS3
1675 Prompt used by the
1676 .Ic select
1677 statement when reading a menu selection.
1678 The default is
1679 .Sq #?\ \& .
1680 .It Ev PS4
1681 Used to prefix commands that are printed during execution tracing (see the
1682 .Ic set Fl x
1683 command below).
1684 Parameter, command, and arithmetic substitutions are performed
1685 before it is printed.
1686 The default is
1687 .Sq +\ \& .
1688 .It Ev PWD
1689 The current working directory.
1690 May be unset or
1691 .Dv NULL
1692 if the shell doesn't know where it is.
1693 .It Ev RANDOM
1694 A random number generator.
1695 Every time
1696 .Ev RANDOM
1697 is referenced, it is assigned the next random number in the range
1698 0\-32767.
1699 By default,
1700 .Xr arc4random 3
1701 is used to produce values.
1702 If the variable
1703 .Ev RANDOM
1704 is assigned a value, the value is used as the seed to
1705 .Xr srand_deterministic 3
1706 and subsequent references of
1707 .Ev RANDOM
1708 produce a predictable sequence.
1709 .It Ev REPLY
1710 Default parameter for the
1711 .Ic read
1712 command if no names are given.
1713 Also used in
1714 .Ic select
1715 loops to store the value that is read from standard input.
1716 .It Ev SECONDS
1717 The number of seconds since the shell started or, if the parameter has been
1718 assigned an integer value, the number of seconds since the assignment plus the
1719 value that was assigned.
1720 .It Ev TERM
1721 The user's terminal type.
1722 If set, it will be used to determine the escape sequence used to
1723 clear the screen.
1724 .It Ev TMOUT
1725 If set to a positive integer in an interactive shell, it specifies the maximum
1726 number of seconds the shell will wait for input after printing the primary
1727 prompt
1728 .Pq Ev PS1 .
1729 If the time is exceeded, the shell exits.
1730 .It Ev TMPDIR
1731 The directory temporary shell files are created in.
1732 If this parameter is not
1733 set, or does not contain the absolute path of a writable directory, temporary
1734 files are created in
1735 .Pa /tmp .
1736 .It Ev VISUAL
1737 If set, this parameter controls the command-line editing mode for interactive
1738 shells.
1739 If the last component of the path specified in this parameter contains
1740 the string
1741 .Dq vi ,
1742 .Dq emacs ,
1744 .Dq gmacs ,
1746 .Xr vi 1 ,
1747 emacs, or gmacs (Gosling emacs) editing mode is enabled, respectively.
1748 See also the
1749 .Ev EDITOR
1750 parameter, above.
1752 .Ss Tilde expansion
1753 Tilde expansion, which is done in parallel with parameter substitution, is done
1754 on words starting with an unquoted
1755 .Ql ~ .
1756 The characters following the tilde, up to the first
1757 .Ql / ,
1758 if any, are assumed to be a login name.
1759 If the login name is empty,
1760 .Ql + ,
1762 .Ql - ,
1763 the value of the
1764 .Ev HOME ,
1765 .Ev PWD ,
1767 .Ev OLDPWD
1768 parameter is substituted, respectively.
1769 Otherwise, the password file is
1770 searched for the login name, and the tilde expression is substituted with the
1771 user's home directory.
1772 If the login name is not found in the password file or
1773 if any quoting or parameter substitution occurs in the login name, no
1774 substitution is performed.
1776 In parameter assignments
1777 (such as those preceding a simple-command or those occurring
1778 in the arguments of
1779 .Ic alias ,
1780 .Ic export ,
1781 .Ic readonly ,
1783 .Ic typeset ) ,
1784 tilde expansion is done after any assignment
1785 (i.e. after the equals sign)
1786 or after an unquoted colon
1787 .Pq Sq \&: ;
1788 login names are also delimited by colons.
1790 The home directory of previously expanded login names are cached and re-used.
1792 .Ic alias -d
1793 command may be used to list, change, and add to this cache (e.g.\&
1794 .Ic alias -d fac=/usr/local/facilities; cd ~fac/bin ) .
1795 .Ss Brace expansion (alternation)
1796 Brace expressions take the following form:
1797 .Bd -unfilled -offset indent
1798 .Sm off
1800 .Ar prefix No { Ar str1 No ,...,
1801 .Ar strN No } Ar suffix
1803 .Sm on
1806 The expressions are expanded to
1807 .Ar N
1808 words, each of which is the concatenation of
1809 .Ar prefix ,
1810 .Ar str Ns i ,
1812 .Ar suffix
1813 (e.g.\&
1814 .Dq a{c,b{X,Y},d}e
1815 expands to four words:
1816 .Dq ace ,
1817 .Dq abXe ,
1818 .Dq abYe ,
1820 .Dq ade ) .
1821 As noted in the example, brace expressions can be nested and the resulting
1822 words are not sorted.
1823 Brace expressions must contain an unquoted comma
1824 .Pq Sq \&,
1825 for expansion to occur (e.g.\&
1826 .Ic {}
1828 .Ic {foo}
1829 are not expanded).
1830 Brace expansion is carried out after parameter substitution
1831 and before file name generation.
1832 .Ss File name patterns
1833 A file name pattern is a word containing one or more unquoted
1834 .Ql \&? ,
1835 .Ql * ,
1836 .Ql + ,
1837 .Ql @ ,
1839 .Ql \&!
1840 characters or
1841 .Dq [..]
1842 sequences.
1843 Once brace expansion has been performed, the shell replaces file
1844 name patterns with the sorted names of all the files that match the pattern
1845 (if no files match, the word is left unchanged).
1846 The pattern elements have the following meaning:
1847 .Bl -tag -width Ds
1848 .It \&?
1849 Matches any single character.
1850 .It \&*
1851 Matches any sequence of characters.
1852 .It [..]
1853 Matches any of the characters inside the brackets.
1854 Ranges of characters can be
1855 specified by separating two characters by a
1856 .Ql -
1857 (e.g.\&
1858 .Dq [a0-9]
1859 matches the letter
1860 .Sq a
1861 or any digit).
1862 In order to represent itself, a
1863 .Ql -
1864 must either be quoted or the first or last character in the character list.
1865 Similarly, a
1866 .Ql \&]
1867 must be quoted or the first character in the list if it is to represent itself
1868 instead of the end of the list.
1869 Also, a
1870 .Ql \&!
1871 appearing at the start of the list has special meaning (see below), so to
1872 represent itself it must be quoted or appear later in the list.
1874 Within a bracket expression, the name of a
1875 .Em character class
1876 enclosed in
1877 .Sq [:
1879 .Sq :]
1880 stands for the list of all characters belonging to that class.
1881 Supported character classes:
1882 .Bd -literal -offset indent
1883 alnum   cntrl   lower   space
1884 alpha   digit   print   upper
1885 blank   graph   punct   xdigit
1888 These match characters using the macros specified in
1889 .Xr isalnum 3 ,
1890 .Xr isalpha 3 ,
1891 and so on.
1892 A character class may not be used as an endpoint of a range.
1893 .It [!..]
1894 Like [..],
1895 except it matches any character not inside the brackets.
1896 .Sm off
1897 .It *( Ar pattern Ns | No ...| Ar pattern )
1898 .Sm on
1899 Matches any string of characters that matches zero or more occurrences of the
1900 specified patterns.
1901 Example: The pattern
1902 .Ic *(foo|bar)
1903 matches the strings
1904 .Dq ,
1905 .Dq foo ,
1906 .Dq bar ,
1907 .Dq foobarfoo ,
1908 etc.
1909 .Sm off
1910 .It +( Ar pattern Ns | No ...| Ar pattern )
1911 .Sm on
1912 Matches any string of characters that matches one or more occurrences of the
1913 specified patterns.
1914 Example: The pattern
1915 .Ic +(foo|bar)
1916 matches the strings
1917 .Dq foo ,
1918 .Dq bar ,
1919 .Dq foobar ,
1920 etc.
1921 .Sm off
1922 .It ?( Ar pattern Ns | No ...| Ar pattern )
1923 .Sm on
1924 Matches the empty string or a string that matches one of the specified
1925 patterns.
1926 Example: The pattern
1927 .Ic ?(foo|bar)
1928 only matches the strings
1929 .Dq ,
1930 .Dq foo ,
1932 .Dq bar .
1933 .Sm off
1934 .It @( Ar pattern Ns | No ...| Ar pattern )
1935 .Sm on
1936 Matches a string that matches one of the specified patterns.
1937 Example: The pattern
1938 .Ic @(foo|bar)
1939 only matches the strings
1940 .Dq foo
1942 .Dq bar .
1943 .Sm off
1944 .It !( Ar pattern Ns | No ...| Ar pattern )
1945 .Sm on
1946 Matches any string that does not match one of the specified patterns.
1947 Examples: The pattern
1948 .Ic !(foo|bar)
1949 matches all strings except
1950 .Dq foo
1952 .Dq bar ;
1953 the pattern
1954 .Ic !(*)
1955 matches no strings; the pattern
1956 .Ic !(?)*\&
1957 matches all strings (think about it).
1960 Unlike most shells,
1961 .Nm ksh
1962 never matches
1963 .Sq \&.
1965 .Sq .. .
1967 Note that none of the above pattern elements match either a period
1968 .Pq Sq \&.
1969 at the start of a file name or a slash
1970 .Pq Sq / ,
1971 even if they are explicitly used in a [..] sequence; also, the names
1972 .Sq \&.
1974 .Sq ..
1975 are never matched, even by the pattern
1976 .Sq .* .
1978 If the
1979 .Ic markdirs
1980 option is set, any directories that result from file name generation are marked
1981 with a trailing
1982 .Ql / .
1983 .Ss Input/output redirection
1984 When a command is executed, its standard input, standard output, and standard
1985 error (file descriptors 0, 1, and 2, respectively) are normally inherited from
1986 the shell.
1987 Three exceptions to this are commands in pipelines, for which
1988 standard input and/or standard output are those set up by the pipeline,
1989 asynchronous commands created when job control is disabled, for which standard
1990 input is initially set to be from
1991 .Pa /dev/null ,
1992 and commands for which any of the following redirections have been specified:
1993 .Bl -tag -width Ds
1994 .It Cm > Ar file
1995 Standard output is redirected to
1996 .Ar file .
1998 .Ar file
1999 does not exist, it is created; if it does exist, is a regular file, and the
2000 .Ic noclobber
2001 option is set, an error occurs; otherwise, the file is truncated.
2002 Note that this means the command
2003 .Ic cmd < foo > foo
2004 will open
2005 .Ar foo
2006 for reading and then truncate it when it opens it for writing, before
2007 .Ar cmd
2008 gets a chance to actually read
2009 .Ar foo .
2010 .It Cm >| Ar file
2011 Same as
2012 .Cm > ,
2013 except the file is truncated, even if the
2014 .Ic noclobber
2015 option is set.
2016 .It Cm >> Ar file
2017 Same as
2018 .Cm > ,
2019 except if
2020 .Ar file
2021 exists it is appended to instead of being truncated.
2022 Also, the file is opened
2023 in append mode, so writes always go to the end of the file (see
2024 .Xr open 2 ) .
2025 .It Cm < Ar file
2026 Standard input is redirected from
2027 .Ar file ,
2028 which is opened for reading.
2029 .It Cm <> Ar file
2030 Same as
2031 .Cm < ,
2032 except the file is opened for reading and writing.
2033 .It Cm << Ar marker
2034 After reading the command line containing this kind of redirection (called a
2035 .Dq here document ) ,
2036 the shell copies lines from the command source into a temporary file until a
2037 line matching
2038 .Ar marker
2039 is read.
2040 When the command is executed, standard input is redirected from the
2041 temporary file.
2043 .Ar marker
2044 contains no quoted characters, the contents of the temporary file are processed
2045 as if enclosed in double quotes each time the command is executed, so
2046 parameter, command, and arithmetic substitutions are performed, along with
2047 backslash
2048 .Pq Sq \e
2049 escapes for
2050 .Ql $ ,
2051 .Ql ` ,
2052 .Ql \e ,
2054 .Ql \enewline .
2055 If multiple here documents are used on the same command line, they are saved in
2056 order.
2057 .It Cm <<- Ar marker
2058 Same as
2059 .Cm << ,
2060 except leading tabs are stripped from lines in the here document.
2061 .It Cm <& Ar fd
2062 Standard input is duplicated from file descriptor
2063 .Ar fd .
2064 .Ar fd
2065 can be a single digit, indicating the number of an existing file descriptor;
2066 the letter
2067 .Ql p ,
2068 indicating the file descriptor associated with the output of the current
2069 co-process; or the character
2070 .Ql - ,
2071 indicating standard input is to be closed.
2072 .It Cm >& Ar fd
2073 Same as
2074 .Cm <& ,
2075 except the operation is done on standard output.
2078 In any of the above redirections, the file descriptor that is redirected
2079 (i.e. standard input or standard output)
2080 can be explicitly given by preceding the
2081 redirection with a single digit.
2082 Parameter, command, and arithmetic
2083 substitutions, tilde substitutions, and (if the shell is interactive)
2084 file name generation are all performed on the
2085 .Ar file ,
2086 .Ar marker ,
2088 .Ar fd
2089 arguments of redirections.
2090 Note, however, that the results of any file name
2091 generation are only used if a single file is matched; if multiple files match,
2092 the word with the expanded file name generation characters is used.
2093 Note
2094 that in restricted shells, redirections which can create files cannot be used.
2096 For simple-commands, redirections may appear anywhere in the command; for
2097 compound-commands
2099 .Ic if
2100 statements, etc.
2101 .Pc ,
2102 any redirections must appear at the end.
2103 Redirections are processed after
2104 pipelines are created and in the order they are given, so the following
2105 will print an error with a line number prepended to it:
2107 .D1 $ cat /foo/bar 2>&1 > /dev/null | cat -n
2108 .Ss Arithmetic expressions
2109 Integer arithmetic expressions can be used with the
2110 .Ic let
2111 command, inside $((..)) expressions, inside array references (e.g.\&
2112 .Ar name Ns Bq Ar expr ) ,
2113 as numeric arguments to the
2114 .Ic test
2115 command, and as the value of an assignment to an integer parameter.
2117 Expressions may contain alpha-numeric parameter identifiers, array references,
2118 and integer constants and may be combined with the following C operators
2119 (listed and grouped in increasing order of precedence):
2121 Unary operators:
2122 .Bd -literal -offset indent
2123 + - ! ~ ++ --
2126 Binary operators:
2127 .Bd -literal -offset indent
2129 = *= /= %= += -= <<= >>= &= ^= |=
2135 == !=
2136 < <= >= >
2137 << >>
2138 + -
2139 * / %
2142 Ternary operators:
2143 .Bd -literal -offset indent
2144 ?: (precedence is immediately higher than assignment)
2147 Grouping operators:
2148 .Bd -literal -offset indent
2149 ( )
2152 A parameter that is NULL or unset evaluates to 0.
2153 Integer constants may be specified with arbitrary bases using the notation
2154 .Ar base Ns # Ns Ar number ,
2155 where
2156 .Ar base
2157 is a decimal integer specifying the base, and
2158 .Ar number
2159 is a number in the specified base.
2160 Additionally,
2161 integers may be prefixed with
2162 .Sq 0X
2164 .Sq 0x
2165 (specifying base 16)
2167 .Sq 0
2168 (base 8)
2169 in all forms of arithmetic expressions,
2170 except as numeric arguments to the
2171 .Ic test
2172 command.
2174 The operators are evaluated as follows:
2175 .Bl -tag -width Ds -offset indent
2176 .It unary +
2177 Result is the argument (included for completeness).
2178 .It unary -
2179 Negation.
2180 .It \&!
2181 Logical NOT;
2182 the result is 1 if argument is zero, 0 if not.
2183 .It ~
2184 Arithmetic (bit-wise) NOT.
2185 .It ++
2186 Increment; must be applied to a parameter (not a literal or other expression).
2187 The parameter is incremented by 1.
2188 When used as a prefix operator, the result
2189 is the incremented value of the parameter; when used as a postfix operator, the
2190 result is the original value of the parameter.
2191 .It --
2192 Similar to
2193 .Ic ++ ,
2194 except the parameter is decremented by 1.
2195 .It \&,
2196 Separates two arithmetic expressions; the left-hand side is evaluated first,
2197 then the right.
2198 The result is the value of the expression on the right-hand side.
2199 .It =
2200 Assignment; the variable on the left is set to the value on the right.
2201 .It Xo
2202 .No *= /= += -= <<=
2203 .No >>= &= ^= |=
2205 Assignment operators.
2206 .Sm off
2207 .Ao Ar var Ac Xo
2208 .Aq Ar op
2209 .No = Aq Ar expr
2211 .Sm on
2212 is the same as
2213 .Sm off
2214 .Ao Ar var Ac Xo
2215 .No = Aq Ar var
2216 .Aq Ar op
2217 .Aq Ar expr ,
2219 .Sm on
2220 with any operator precedence in
2221 .Aq Ar expr
2222 preserved.
2223 For example,
2224 .Dq var1 *= 5 + 3
2225 is the same as specifying
2226 .Dq var1 = var1 * (5 + 3) .
2227 .It ||
2228 Logical OR;
2229 the result is 1 if either argument is non-zero, 0 if not.
2230 The right argument is evaluated only if the left argument is zero.
2231 .It &&
2232 Logical AND;
2233 the result is 1 if both arguments are non-zero, 0 if not.
2234 The right argument is evaluated only if the left argument is non-zero.
2235 .It |
2236 Arithmetic (bit-wise) OR.
2237 .It ^
2238 Arithmetic (bit-wise) XOR
2239 (exclusive-OR).
2240 .It &
2241 Arithmetic (bit-wise) AND.
2242 .It ==
2243 Equal; the result is 1 if both arguments are equal, 0 if not.
2244 .It !=
2245 Not equal; the result is 0 if both arguments are equal, 1 if not.
2246 .It <
2247 Less than; the result is 1 if the left argument is less than the right, 0 if
2248 not.
2249 .It <= >= >
2250 Less than or equal, greater than or equal, greater than.
2252 .Ic < .
2253 .It << >>
2254 Shift left (right); the result is the left argument with its bits shifted left
2255 (right) by the amount given in the right argument.
2256 .It + - * /
2257 Addition, subtraction, multiplication, and division.
2258 .It %
2259 Remainder; the result is the remainder of the division of the left argument by
2260 the right.
2261 The sign of the result is unspecified if either argument is negative.
2262 .It Xo
2263 .Sm off
2264 .Aq Ar arg1 ?
2265 .Aq Ar arg2 :
2266 .Aq Ar arg3
2267 .Sm on
2270 .Aq Ar arg1
2271 is non-zero, the result is
2272 .Aq Ar arg2 ;
2273 otherwise the result is
2274 .Aq Ar arg3 .
2276 .Ss Co-processes
2277 A co-process, which is a pipeline created with the
2278 .Sq |&
2279 operator, is an asynchronous process that the shell can both write to (using
2280 .Ic print -p )
2281 and read from (using
2282 .Ic read -p ) .
2283 The input and output of the co-process can also be manipulated using
2284 .Cm >&p
2286 .Cm <&p
2287 redirections, respectively.
2288 Once a co-process has been started, another can't
2289 be started until the co-process exits, or until the co-process's input has been
2290 redirected using an
2291 .Ic exec Ar n Ns Cm >&p
2292 redirection.
2293 If a co-process's input is redirected in this way, the next
2294 co-process to be started will share the output with the first co-process,
2295 unless the output of the initial co-process has been redirected using an
2296 .Ic exec Ar n Ns Cm <&p
2297 redirection.
2299 Some notes concerning co-processes:
2300 .Bl -bullet
2302 The only way to close the co-process's input (so the co-process reads an
2303 end-of-file) is to redirect the input to a numbered file descriptor and then
2304 close that file descriptor e.g.\&
2305 .Ic exec 3>&p; exec 3>&- .
2307 In order for co-processes to share a common output, the shell must keep the
2308 write portion of the output pipe open.
2309 This means that end-of-file will not be
2310 detected until all co-processes sharing the co-process's output have exited
2311 (when they all exit, the shell closes its copy of the pipe).
2312 This can be
2313 avoided by redirecting the output to a numbered file descriptor (as this also
2314 causes the shell to close its copy).
2315 Note that this behaviour is slightly
2316 different from the original Korn shell which closes its copy of the write
2317 portion of the co-process output when the most recently started co-process
2318 (instead of when all sharing co-processes) exits.
2320 .Ic print -p
2321 will ignore
2322 .Dv SIGPIPE
2323 signals during writes if the signal is not being trapped or ignored; the same
2324 is true if the co-process input has been duplicated to another file descriptor
2326 .Ic print -u Ns Ar n
2327 is used.
2329 .Ss Functions
2330 Functions are defined using either Korn shell
2331 .Ic function Ar function-name
2332 syntax or the Bourne/POSIX shell
2333 .Ar function-name Ns ()
2334 syntax (see below for the difference between the two forms).
2335 Functions are like
2336 .Li .-scripts
2337 (i.e. scripts sourced using the
2338 .Sq \&.
2339 built-in)
2340 in that they are executed in the current environment.
2341 However, unlike
2342 .Li .-scripts ,
2343 shell arguments (i.e. positional parameters $1, $2, etc.)\&
2344 are never visible inside them.
2345 When the shell is determining the location of a command, functions
2346 are searched after special built-in commands, before regular and
2347 non-regular built-ins, and before the
2348 .Ev PATH
2349 is searched.
2351 An existing function may be deleted using
2352 .Ic unset Fl f Ar function-name .
2353 A list of functions can be obtained using
2354 .Ic typeset +f
2355 and the function definitions can be listed using
2356 .Ic typeset -f .
2358 .Ic autoload
2359 command (which is an alias for
2360 .Ic typeset -fu )
2361 may be used to create undefined functions: when an undefined function is
2362 executed, the shell searches the path specified in the
2363 .Ev FPATH
2364 parameter for a file with the same name as the function, which, if found, is
2365 read and executed.
2366 If after executing the file the named function is found to
2367 be defined, the function is executed; otherwise, the normal command search is
2368 continued (i.e. the shell searches the regular built-in command table and
2369 .Ev PATH ) .
2370 Note that if a command is not found using
2371 .Ev PATH ,
2372 an attempt is made to autoload a function using
2373 .Ev FPATH
2374 (this is an undocumented feature of the original Korn shell).
2376 Functions can have two attributes,
2377 .Dq trace
2379 .Dq export ,
2380 which can be set with
2381 .Ic typeset -ft
2383 .Ic typeset -fx ,
2384 respectively.
2385 When a traced function is executed, the shell's
2386 .Ic xtrace
2387 option is turned on for the function's duration; otherwise, the
2388 .Ic xtrace
2389 option is turned off.
2391 .Dq export
2392 attribute of functions is currently not used.
2393 In the original Korn shell,
2394 exported functions are visible to shell scripts that are executed.
2396 Since functions are executed in the current shell environment, parameter
2397 assignments made inside functions are visible after the function completes.
2398 If this is not the desired effect, the
2399 .Ic typeset
2400 command can be used inside a function to create a local parameter.
2401 Note that special parameters (e.g.\&
2402 .Ic \&$$ , $! )
2403 can't be scoped in this way.
2405 The exit status of a function is that of the last command executed in the
2406 function.
2407 A function can be made to finish immediately using the
2408 .Ic return
2409 command; this may also be used to explicitly specify the exit status.
2411 Functions defined with the
2412 .Ic function
2413 reserved word are treated differently in the following ways from functions
2414 defined with the
2415 .Ic ()
2416 notation:
2417 .Bl -bullet
2419 The $0 parameter is set to the name of the function
2420 (Bourne-style functions leave $0 untouched).
2422 Parameter assignments preceding function calls are not kept in the shell
2423 environment (executing Bourne-style functions will keep assignments).
2425 .Ev OPTIND
2426 is saved/reset and restored on entry and exit from the function so
2427 .Ic getopts
2428 can be used properly both inside and outside the function (Bourne-style
2429 functions leave
2430 .Ev OPTIND
2431 untouched, so using
2432 .Ic getopts
2433 inside a function interferes with using
2434 .Ic getopts
2435 outside the function).
2437 .Ss POSIX mode
2438 The shell is intended to be POSIX compliant;
2439 however, in some cases, POSIX behaviour is contrary either to
2440 the original Korn shell behaviour or to user convenience.
2441 How the shell behaves in these cases is determined by the state of the
2442 .Ic posix
2443 option
2444 .Pq Ic set -o posix .
2445 If it is on, the POSIX behaviour is followed; otherwise, it is not.
2447 .Ic posix
2448 option is set automatically when the shell starts up if the environment
2449 contains the
2450 .Ev POSIXLY_CORRECT
2451 parameter.
2452 The shell can also be compiled so that it is in POSIX mode by default;
2453 however, this is usually not desirable.
2455 The following is a list of things that are affected by the state of the
2456 .Ic posix
2457 option:
2458 .Bl -bullet
2460 .Ic kill -l
2461 output.
2462 In POSIX mode, only signal names are listed (in a single line);
2463 in non-POSIX mode,
2464 signal numbers, names, and descriptions are printed (in columns).
2466 .Ic echo
2467 options.
2468 In POSIX mode,
2469 .Fl e
2471 .Fl E
2472 are not treated as options, but printed like other arguments;
2473 in non-POSIX mode, these options control the interpretation
2474 of backslash sequences.
2476 .Ic fg
2477 exit status.
2478 In POSIX mode, the exit status is 0 if no errors occur;
2479 in non-POSIX mode, the exit status is that of the last foregrounded job.
2481 .Ic eval
2482 exit status.
2484 .Ic eval
2485 gets to see an empty command (i.e.\&
2486 .Ic eval `false` ) ,
2487 its exit status in POSIX mode will be 0.
2488 In non-POSIX mode,
2489 it will be the exit status of the last command substitution that was
2490 done in the processing of the arguments to
2491 .Ic eval
2492 (or 0 if there were no command substitutions).
2494 .Ic getopts .
2495 In POSIX mode, options must start with a
2496 .Ql - ;
2497 in non-POSIX mode, options can start with either
2498 .Ql -
2500 .Ql + .
2502 Brace expansion (also known as alternation).
2503 In POSIX mode, brace expansion is disabled;
2504 in non-POSIX mode, brace expansion is enabled.
2505 Note that
2506 .Ic set -o posix
2507 (or setting the
2508 .Ev POSIXLY_CORRECT
2509 parameter) automatically turns the
2510 .Ic braceexpand
2511 option off; however, it can be explicitly turned on later.
2513 .Ic set - .
2514 In POSIX mode, this does not clear the
2515 .Ic verbose
2517 .Ic xtrace
2518 options; in non-POSIX mode, it does.
2520 .Ic set
2521 exit status.
2522 In POSIX mode, the exit status of
2523 .Ic set
2524 is 0 if there are no errors;
2525 in non-POSIX mode, the exit status is that of any
2526 command substitutions performed in generating the
2527 .Ic set
2528 command.
2529 For example,
2530 .Ic set -- `false`; echo $?\&
2531 prints 0 in POSIX mode, 1 in non-POSIX mode.
2532 This construct is used in most shell scripts that use the old
2533 .Xr getopt 1
2534 command.
2536 Argument expansion of the
2537 .Ic alias ,
2538 .Ic export ,
2539 .Ic readonly ,
2541 .Ic typeset
2542 commands.
2543 In POSIX mode, normal argument expansion is done; in non-POSIX mode,
2544 field splitting, file globbing, brace expansion, and (normal) tilde expansion
2545 are turned off, while assignment tilde expansion is turned on.
2547 Signal specification.
2548 In POSIX mode, signals can be specified as digits, only
2549 if signal numbers match POSIX values
2550 (i.e. HUP=1, INT=2, QUIT=3, ABRT=6, KILL=9, ALRM=14, and TERM=15);
2551 in non-POSIX mode, signals can always be digits.
2553 Alias expansion.
2554 In POSIX mode, alias expansion is only carried out when reading command words;
2555 in non-POSIX mode, alias expansion is carried out on any
2556 word following an alias that ended in a space.
2557 For example, the following
2558 .Ic for
2559 loop uses parameter
2560 .Sq i
2561 in POSIX mode and
2562 .Sq j
2563 in non-POSIX mode:
2564 .Bd -literal -offset indent
2565 alias a='for ' i='j'
2566 a i in 1 2; do echo i=$i j=$j; done
2569 .Ic test .
2570 In POSIX mode, the expression
2571 .Sq Fl t
2572 (preceded by some number of
2573 .Sq \&!
2574 arguments) is always true as it is a non-zero length string;
2575 in non-POSIX mode, it tests if file descriptor 1 is a
2576 .Xr tty 4
2577 (i.e. the
2578 .Ar fd
2579 argument to the
2580 .Fl t
2581 test may be left out and defaults to 1).
2583 .Ss Strict Bourne shell mode
2584 When the
2585 .Ic sh
2586 option is enabled (see the
2587 .Ic set
2588 command),
2590 will behave like
2591 .Xr sh 1
2592 in the following ways:
2593 .Bl -bullet
2595 The parameter
2596 .Ic $_
2597 is not set to:
2599 .Bl -dash -compact
2601 the expanded alias' full program path after entering commands
2602 that are tracked aliases
2604 the last argument on the command line after entering external
2605 commands
2607 the file that changed when
2608 .Ev MAILPATH
2609 is set to monitor a mailbox
2612 File descriptors are left untouched when executing
2613 .Ic exec
2614 with no arguments.
2616 Backslash-escaped special characters are not substituted in
2617 .Ev PS1 .
2619 Sequences of
2620 .Sq ((...))
2621 are not interpreted as arithmetic expressions.
2623 .Ss Command execution
2624 After evaluation of command-line arguments, redirections, and parameter
2625 assignments, the type of command is determined: a special built-in, a
2626 function, a regular built-in, or the name of a file to execute found using the
2627 .Ev PATH
2628 parameter.
2629 The checks are made in the above order.
2630 Special built-in commands differ from other commands in that the
2631 .Ev PATH
2632 parameter is not used to find them, an error during their execution can
2633 cause a non-interactive shell to exit, and parameter assignments that are
2634 specified before the command are kept after the command completes.
2635 Just to confuse things, if the
2636 .Ic posix
2637 option is turned off (see the
2638 .Ic set
2639 command below), some special commands are very special in that no field
2640 splitting, file globbing, brace expansion, nor tilde expansion is performed
2641 on arguments that look like assignments.
2642 Regular built-in commands are different only in that the
2643 .Ev PATH
2644 parameter is not used to find them.
2646 The original
2647 .Nm ksh
2648 and POSIX differ somewhat in which commands are considered
2649 special or regular:
2651 POSIX special commands
2653 .Ic \&. , \&: , break , continue ,
2654 .Ic eval , exec , exit , export ,
2655 .Ic readonly , return , set , shift ,
2656 .Ic times , trap , unset
2658 Additional
2660 special commands
2662 .Ic builtin , typeset
2664 Very special commands
2665 .Pq when POSIX mode is off
2667 .Ic alias , readonly , set , typeset
2669 POSIX regular commands
2671 .Ic alias , bg , cd , command ,
2672 .Ic false , fc , fg , getopts ,
2673 .Ic jobs , kill , pwd , read ,
2674 .Ic true , umask , unalias , wait
2676 Additional
2678 regular commands
2680 .Ic \&[ , echo , let ,
2681 .Ic print , suspend , test ,
2682 .Ic ulimit , whence
2684 Once the type of command has been determined, any command-line parameter
2685 assignments are performed and exported for the duration of the command.
2687 The following describes the special and regular built-in commands:
2689 .Bl -tag -width Ds -compact
2690 .It Ic \&. Ar file Op Ar arg ...
2691 Execute the commands in
2692 .Ar file
2693 in the current environment.
2694 The file is searched for in the directories of
2695 .Ev PATH .
2696 If arguments are given, the positional parameters may be used to access them
2697 while
2698 .Ar file
2699 is being executed.
2700 If no arguments are given, the positional parameters are
2701 those of the environment the command is used in.
2703 .It Ic \&: Op Ar ...
2704 The null command.
2705 Exit status is set to zero.
2707 .It Xo Ic alias
2708 .Oo Fl d | t Oo Fl r Oc |
2709 .Cm +-x Oc
2710 .Op Fl p
2711 .Op Cm +
2712 .Oo Ar name
2713 .Op Ns = Ns Ar value
2714 .Ar ... Oc
2716 Without arguments,
2717 .Ic alias
2718 lists all aliases.
2719 For any name without a value, the existing alias is listed.
2720 Any name with a value defines an alias (see
2721 .Sx Aliases
2722 above).
2724 When listing aliases, one of two formats is used.
2725 Normally, aliases are listed as
2726 .Ar name Ns = Ns Ar value ,
2727 where
2728 .Ar value
2729 is quoted.
2730 If options were preceded with
2731 .Ql + ,
2732 or a lone
2733 .Ql +
2734 is given on the command line, only
2735 .Ar name
2736 is printed.
2739 .Fl d
2740 option causes directory aliases, which are used in tilde expansion, to be
2741 listed or set (see
2742 .Sx Tilde expansion
2743 above).
2745 If the
2746 .Fl p
2747 option is used, each alias is prefixed with the string
2748 .Dq alias\ \& .
2751 .Fl t
2752 option indicates that tracked aliases are to be listed/set (values specified on
2753 the command line are ignored for tracked aliases).
2755 .Fl r
2756 option indicates that all tracked aliases are to be reset.
2759 .Fl x
2760 option sets
2761 .Pq Ic +x No clears
2762 the export attribute of an alias or, if no names are given, lists the aliases
2763 with the export attribute (exporting an alias has no effect).
2765 .It Ic bg Op Ar job ...
2766 Resume the specified stopped job(s) in the background.
2767 If no jobs are specified,
2768 .Ic %+
2769 is assumed.
2771 .Sx Job control
2772 below for more information.
2774 .It Ic bind Op Fl l
2775 The current bindings are listed.
2776 If the
2777 .Fl l
2778 flag is given,
2779 .Ic bind
2780 instead lists the names of the functions to which keys may be bound.
2782 .Sx Emacs editing mode
2783 for more information.
2785 .It Xo Ic bind Op Fl m
2786 .Ar string Ns = Ns Op Ar substitute
2787 .Ar ...
2789 .It Xo Ic bind
2790 .Ar string Ns = Ns Op Ar editing-command
2791 .Ar ...
2794 .Sx Emacs editing mode ,
2795 the specified editing command is bound to the given
2796 .Ar string .
2797 Future input of the
2798 .Ar string
2799 will cause the editing command to be immediately invoked.
2800 Bindings have no effect in
2801 .Sx Vi editing mode .
2803 If the
2804 .Fl m
2805 flag is given, the specified input
2806 .Ar string
2807 will afterwards be immediately replaced by the given
2808 .Ar substitute
2809 string, which may contain editing commands.
2810 Control characters may be written using caret notation.
2811 For example, ^X represents Control-X.
2813 If a certain character occurs as the first character of any bound
2814 multi-character
2815 .Ar string
2816 sequence, that character becomes a command prefix character.
2817 Any character sequence that starts with a command prefix character
2818 but that is not bound to a command or substitute
2819 is implicitly considered as bound to the
2820 .Sq error
2821 command.
2822 By default, two command prefix characters exist:
2823 Escape
2824 .Pq ^[
2825 and Control-X
2826 .Pq ^X .
2828 The following default bindings show how the arrow keys
2829 on an ANSI terminal or xterm are bound
2830 (of course some escape sequences won't work out quite this nicely):
2831 .Bd -literal -offset indent
2832 bind '^[[A'=up-history
2833 bind '^[[B'=down-history
2834 bind '^[[C'=forward-char
2835 bind '^[[D'=backward-char
2838 .It Ic break Op Ar level
2839 Exit the
2840 .Ar level Ns th
2841 inner-most
2842 .Ic for ,
2843 .Ic select ,
2844 .Ic until ,
2846 .Ic while
2847 loop.
2848 .Ar level
2849 defaults to 1.
2851 .It Ic builtin Ar command Op Ar arg ...
2852 Execute the built-in command
2853 .Ar command .
2855 .It Xo
2856 .Ic cd
2857 .Op Fl LP
2858 .Op Ar dir
2860 Set the working directory to
2861 .Ar dir .
2862 If the parameter
2863 .Ev CDPATH
2864 is set, it lists the search path for the directory containing
2865 .Ar dir .
2867 .Dv NULL
2868 path means the current directory.
2870 .Ar dir
2871 is found in any component of the
2872 .Ev CDPATH
2873 search path other than the
2874 .Dv NULL
2875 path, the name of the new working directory will be written to standard output.
2877 .Ar dir
2878 is missing, the home directory
2879 .Ev HOME
2880 is used.
2882 .Ar dir
2884 .Ql - ,
2885 the previous working directory is used (see the
2886 .Ev OLDPWD
2887 parameter).
2889 If the
2890 .Fl L
2891 option (logical path) is used or if the
2892 .Ic physical
2893 option isn't set (see the
2894 .Ic set
2895 command below), references to
2896 .Sq ..
2898 .Ar dir
2899 are relative to the path used to get to the directory.
2900 If the
2901 .Fl P
2902 option (physical path) is used or if the
2903 .Ic physical
2904 option is set,
2905 .Sq ..
2906 is relative to the filesystem directory tree.
2908 .Ev PWD
2910 .Ev OLDPWD
2911 parameters are updated to reflect the current and old working directory,
2912 respectively.
2914 .It Xo
2915 .Ic cd
2916 .Op Fl LP
2917 .Ar old new
2919 The string
2920 .Ar new
2921 is substituted for
2922 .Ar old
2923 in the current directory, and the shell attempts to change to the new
2924 directory.
2926 .It Xo
2927 .Ic command
2928 .Op Fl pVv
2929 .Ar cmd
2930 .Op Ar arg ...
2932 If neither the
2933 .Fl v
2935 .Fl V
2936 option is given,
2937 .Ar cmd
2938 is executed exactly as if
2939 .Ic command
2940 had not been specified, with two exceptions:
2941 firstly,
2942 .Ar cmd
2943 cannot be an alias or a shell function;
2944 and secondly, special built-in commands lose their specialness
2945 (i.e. redirection and utility errors do not cause the shell to
2946 exit, and command assignments are not permanent).
2948 If the
2949 .Fl p
2950 option is given, a default search path is used instead of the current value of
2951 .Ev PATH
2952 (the actual value of the default path is system dependent: on
2953 POSIX-ish systems, it is the value returned by
2954 .Ic getconf PATH ) .
2955 Nevertheless, reserved words, aliases, shell functions, and
2956 builtin commands are still found before external commands.
2958 If the
2959 .Fl v
2960 option is given, instead of executing
2961 .Ar cmd ,
2962 information about what would be executed is given (and the same is done for
2963 .Ar arg ... ) .
2964 For special and regular built-in commands and functions, their names are simply
2965 printed; for aliases, a command that defines them is printed; and for commands
2966 found by searching the
2967 .Ev PATH
2968 parameter, the full path of the command is printed.
2969 If no command is found
2970 (i.e. the path search fails), nothing is printed and
2971 .Ic command
2972 exits with a non-zero status.
2974 .Fl V
2975 option is like the
2976 .Fl v
2977 option, except it is more verbose.
2979 .It Ic continue Op Ar level
2980 Jumps to the beginning of the
2981 .Ar level Ns th
2982 inner-most
2983 .Ic for ,
2984 .Ic select ,
2985 .Ic until ,
2987 .Ic while
2988 loop.
2989 .Ar level
2990 defaults to 1.
2992 .It Xo
2993 .Ic echo
2994 .Op Fl Een
2995 .Op Ar arg ...
2997 Prints its arguments (separated by spaces) followed by a newline, to the
2998 standard output.
2999 The newline is suppressed if any of the arguments contain the
3000 backslash sequence
3001 .Ql \ec .
3002 See the
3003 .Ic print
3004 command below for a list of other backslash sequences that are recognized.
3006 The options are provided for compatibility with
3008 shell scripts.
3010 .Fl n
3011 option suppresses the trailing newline,
3012 .Fl e
3013 enables backslash interpretation (a no-op, since this is normally done), and
3014 .Fl E
3015 suppresses backslash interpretation.
3016 If the
3017 .Ic posix
3018 option is set, only the first argument is treated as an option, and only
3019 if it is exactly
3020 .Dq -n .
3022 .It Ic eval Ar command ...
3023 The arguments are concatenated (with spaces between them) to form a single
3024 string which the shell then parses and executes in the current environment.
3026 .It Xo
3027 .Ic exec
3028 .Op Ar command Op Ar arg ...
3030 The command is executed without forking, replacing the shell process.
3032 If no command is given except for I/O redirection, the I/O redirection is
3033 permanent and the shell is
3034 not replaced.
3035 Any file descriptors greater than 2 which are opened or
3036 .Xr dup 2 Ns 'd
3037 in this way are not made available to other executed commands (i.e. commands
3038 that are not built-in to the shell).
3039 Note that the Bourne shell differs here;
3040 it does pass these file descriptors on.
3042 .It Ic exit Op Ar status
3043 The shell exits with the specified exit status.
3045 .Ar status
3046 is not specified, the exit status is the current value of the
3047 .Ic $?\&
3048 parameter.
3050 .It Xo
3051 .Ic export
3052 .Op Fl p
3053 .Op Ar parameter Ns Op = Ns Ar value
3055 Sets the export attribute of the named parameters.
3056 Exported parameters are passed in the environment to executed commands.
3057 If values are specified, the named parameters are also assigned.
3059 If no parameters are specified, the names of all parameters with the export
3060 attribute are printed one per line, unless the
3061 .Fl p
3062 option is used, in which case
3063 .Ic export
3064 commands defining all exported parameters, including their values, are printed.
3066 .It Ic false
3067 A command that exits with a non-zero status.
3069 .It Xo
3070 .Ic fc
3072 .Fl e Ar editor |
3073 .Fl l Op Fl n
3075 .Op Fl r
3076 .Op Ar first Op Ar last
3078 Fix command.
3079 .Ar first
3081 .Ar last
3082 select commands from the history.
3083 Commands can be selected by history number
3084 or a string specifying the most recent command starting with that string.
3086 .Fl l
3087 option lists the command on standard output, and
3088 .Fl n
3089 inhibits the default command numbers.
3091 .Fl r
3092 option reverses the order of the list.
3093 Without
3094 .Fl l ,
3095 the selected commands are edited by the editor specified with the
3096 .Fl e
3097 option, or if no
3098 .Fl e
3099 is specified, the editor specified by the
3100 .Ev FCEDIT
3101 parameter (if this parameter is not set,
3102 .Pa /bin/ed
3103 is used), and then executed by the shell.
3105 .It Xo
3106 .Ic fc Fl s
3107 .Op Fl g
3108 .Op Ar old Ns = Ns Ar new
3109 .Op Ar prefix
3111 Re-execute the most recent command beginning with
3112 .Ar prefix ,
3113 or the previous command if no
3114 .Ar prefix
3115 is specified,
3116 performing the optional substitution of
3117 .Ar old
3118 with
3119 .Ar new .
3121 .Fl g
3122 is specified, all occurrences of
3123 .Ar old
3124 are replaced with
3125 .Ar new .
3126 The editor is not invoked when the
3127 .Fl s
3128 flag is used.
3129 The obsolescent equivalent
3130 .Dq Fl e No -
3131 is also accepted.
3132 This command is usually accessed with the predefined
3133 .Ic alias r='fc -s' .
3135 .It Ic fg Op Ar job ...
3136 Resume the specified job(s) in the foreground.
3137 If no jobs are specified,
3138 .Ic %+
3139 is assumed.
3141 .Sx Job control
3142 below for more information.
3144 .It Xo
3145 .Ic getopts
3146 .Ar optstring name
3147 .Op Ar arg ...
3149 Used by shell procedures to parse the specified arguments (or positional
3150 parameters, if no arguments are given) and to check for legal options.
3151 .Ar optstring
3152 contains the option letters that
3153 .Ic getopts
3154 is to recognize.
3155 If a letter is followed by a colon, the option is expected to
3156 have an argument.
3157 Options that do not take arguments may be grouped in a single argument.
3158 If an option takes an argument and the option character is not the
3159 last character of the argument it is found in, the remainder of the argument is
3160 taken to be the option's argument; otherwise, the next argument is the option's
3161 argument.
3163 Each time
3164 .Ic getopts
3165 is invoked, it places the next option in the shell parameter
3166 .Ar name
3167 and the index of the argument to be processed by the next call to
3168 .Ic getopts
3169 in the shell parameter
3170 .Ev OPTIND .
3171 If the option was introduced with a
3172 .Ql + ,
3173 the option placed in
3174 .Ar name
3175 is prefixed with a
3176 .Ql + .
3177 When an option requires an argument,
3178 .Ic getopts
3179 places it in the shell parameter
3180 .Ev OPTARG .
3182 When an illegal option or a missing option argument is encountered, a question
3183 mark or a colon is placed in
3184 .Ar name
3185 (indicating an illegal option or missing argument, respectively) and
3186 .Ev OPTARG
3187 is set to the option character that caused the problem.
3188 Furthermore, if
3189 .Ar optstring
3190 does not begin with a colon, a question mark is placed in
3191 .Ar name ,
3192 .Ev OPTARG
3193 is unset, and an error message is printed to standard error.
3195 When the end of the options is encountered,
3196 .Ic getopts
3197 exits with a non-zero exit status.
3198 Options end at the first (non-option
3199 argument) argument that does not start with a
3200 .Ql - ,
3201 or when a
3202 .Ql --
3203 argument is encountered.
3205 Option parsing can be reset by setting
3206 .Ev OPTIND
3207 to 1 (this is done automatically whenever the shell or a shell procedure is
3208 invoked).
3210 Warning: Changing the value of the shell parameter
3211 .Ev OPTIND
3212 to a value other than 1, or parsing different sets of arguments without
3213 resetting
3214 .Ev OPTIND ,
3215 may lead to unexpected results.
3217 .It Xo
3218 .Ic hash
3219 .Op Fl r
3220 .Op Ar name ...
3222 Without arguments, any hashed executable command pathnames are listed.
3224 .Fl r
3225 option causes all hashed commands to be removed from the hash table.
3226 Each
3227 .Ar name
3228 is searched as if it were a command name and added to the hash table if it is
3229 an executable command.
3231 .It Xo
3232 .Ic jobs
3233 .Op Fl lnp
3234 .Op Ar job ...
3236 Display information about the specified job(s); if no jobs are specified, all
3237 jobs are displayed.
3239 .Fl n
3240 option causes information to be displayed only for jobs that have changed
3241 state since the last notification.
3242 If the
3243 .Fl l
3244 option is used, the process ID of each process in a job is also listed.
3246 .Fl p
3247 option causes only the process group of each job to be printed.
3249 .Sx Job control
3250 below for the format of
3251 .Ar job
3252 and the displayed job.
3254 .It Xo
3255 .Ic kill
3256 .Oo Fl s Ar signame |
3257 .No - Ns Ar signum |
3258 .No - Ns Ar signame Oc
3259 .No { Ar job | pid | pgrp No }
3260 .Ar ...
3262 Send the specified signal to the specified jobs, process IDs, or process
3263 groups.
3264 If no signal is specified, the
3265 .Dv TERM
3266 signal is sent.
3267 If a job is specified, the signal is sent to the job's process group.
3269 .Sx Job control
3270 below for the format of
3271 .Ar job .
3273 .It Xo
3274 .Ic kill
3275 .Fl l
3276 .Op Ar exit-status ...
3278 Print the signal name corresponding to
3279 .Ar exit-status .
3280 If no arguments are specified, a list of all the signals, their numbers, and
3281 a short description of them are printed.
3283 .It Ic let Op Ar expression ...
3284 Each expression is evaluated (see
3285 .Sx Arithmetic expressions
3286 above).
3287 If all expressions are successfully evaluated, the exit status is 0 (1)
3288 if the last expression evaluated to non-zero (zero).
3289 If an error occurs during
3290 the parsing or evaluation of an expression, the exit status is greater than 1.
3291 Since expressions may need to be quoted,
3292 .No (( Ar expr No ))
3293 is syntactic sugar for
3294 .No let \&" Ns Ar expr Ns \&" .
3296 .It Xo
3297 .Ic print
3299 .Fl nprsu Ns Oo Ar n Oc |
3300 .Fl R Op Fl en
3302 .Op Ar argument ...
3304 .Ic print
3305 prints its arguments on the standard output, separated by spaces and
3306 terminated with a newline.
3308 .Fl n
3309 option suppresses the newline.
3310 By default, certain C escapes are translated.
3311 These include
3312 .Ql \eb ,
3313 .Ql \ef ,
3314 .Ql \en ,
3315 .Ql \er ,
3316 .Ql \et ,
3317 .Ql \ev ,
3319 .Ql \e0###
3321 .Ql #
3322 is an octal digit, of which there may be 0 to 3
3323 .Pc .
3324 .Ql \ec
3325 is equivalent to using the
3326 .Fl n
3327 option.
3328 .Ql \e
3329 expansion may be inhibited with the
3330 .Fl r
3331 option.
3333 .Fl s
3334 option prints to the history file instead of standard output; the
3335 .Fl u
3336 option prints to file descriptor
3337 .Ar n
3339 .Ar n
3340 defaults to 1 if omitted
3341 .Pc ;
3342 and the
3343 .Fl p
3344 option prints to the co-process (see
3345 .Sx Co-processes
3346 above).
3349 .Fl R
3350 option is used to emulate, to some degree, the
3352 .Xr echo 1
3353 command, which does not process
3354 .Ql \e
3355 sequences unless the
3356 .Fl e
3357 option is given.
3358 As above, the
3359 .Fl n
3360 option suppresses the trailing newline.
3362 .It Ic pwd Op Fl LP
3363 Print the present working directory.
3364 If the
3365 .Fl L
3366 option is used or if the
3367 .Ic physical
3368 option isn't set (see the
3369 .Ic set
3370 command below), the logical path is printed (i.e. the path used to
3371 .Ic cd
3372 to the current directory).
3373 If the
3374 .Fl P
3375 option (physical path) is used or if the
3376 .Ic physical
3377 option is set, the path determined from the filesystem (by following
3378 .Sq ..
3379 directories to the root directory) is printed.
3381 .It Xo
3382 .Ic read
3383 .Op Fl prsu Ns Op Ar n
3384 .Op Ar parameter ...
3386 Reads a line of input from the standard input, separates the line into fields
3387 using the
3388 .Ev IFS
3389 parameter (see
3390 .Sx Substitution
3391 above), and assigns each field to the specified parameters.
3392 If there are more parameters than fields, the extra parameters are set to
3393 .Dv NULL ,
3394 or alternatively, if there are more fields than parameters, the last parameter
3395 is assigned the remaining fields (inclusive of any separating spaces).
3396 If no parameters are specified, the
3397 .Ev REPLY
3398 parameter is used.
3399 If the input line ends in a backslash and the
3400 .Fl r
3401 option was not used, the backslash and the newline are stripped and more input
3402 is read.
3403 If no input is read,
3404 .Ic read
3405 exits with a non-zero status.
3407 The first parameter may have a question mark and a string appended to it, in
3408 which case the string is used as a prompt (printed to standard error before
3409 any input is read) if the input is a
3410 .Xr tty 4
3411 (e.g.\&
3412 .Ic read nfoo?'number of foos: ' ) .
3415 .Fl u Ns Ar n
3417 .Fl p
3418 options cause input to be read from file descriptor
3419 .Ar n
3420 .Pf ( Ar n
3421 defaults to 0 if omitted)
3422 or the current co-process (see
3423 .Sx Co-processes
3424 above for comments on this), respectively.
3425 If the
3426 .Fl s
3427 option is used, input is saved to the history file.
3429 .It Xo
3430 .Ic readonly
3431 .Op Fl p
3432 .Oo Ar parameter
3433 .Op Ns = Ns Ar value
3434 .Ar ... Oc
3436 Sets the read-only attribute of the named parameters.
3437 If values are given,
3438 parameters are set to them before setting the attribute.
3439 Once a parameter is
3440 made read-only, it cannot be unset and its value cannot be changed.
3442 If no parameters are specified, the names of all parameters with the read-only
3443 attribute are printed one per line, unless the
3444 .Fl p
3445 option is used, in which case
3446 .Ic readonly
3447 commands defining all read-only parameters, including their values, are
3448 printed.
3450 .It Ic return Op Ar status
3451 Returns from a function or
3452 .Ic .\&
3453 script, with exit status
3454 .Ar status .
3455 If no
3456 .Ar status
3457 is given, the exit status of the last executed command is used.
3458 If used outside of a function or
3459 .Ic .\&
3460 script, it has the same effect as
3461 .Ic exit .
3462 Note that
3463 .Nm ksh
3464 treats both profile and
3465 .Ev ENV
3466 files as
3467 .Ic .\&
3468 scripts, while the original Korn shell only treats profiles as
3469 .Ic .\&
3470 scripts.
3472 .It Xo
3473 .Ic set Op Ic +-abCefhkmnpsuvXx
3474 .Op Ic +-o Ar option
3475 .Op Ic +-A Ar name
3476 .Op Fl -
3477 .Op Ar arg ...
3480 .Ic set
3481 command can be used to set
3482 .Pq Ic -
3483 or clear
3484 .Pq Ic +
3485 shell options, set the positional parameters, or set an array parameter.
3486 Options can be changed using the
3487 .Cm +-o Ar option
3488 syntax, where
3489 .Ar option
3490 is the long name of an option, or using the
3491 .Cm +- Ns Ar letter
3492 syntax, where
3493 .Ar letter
3494 is the option's single letter name (not all options have a single letter name).
3495 The following table lists both option letters (if they exist) and long names
3496 along with a description of what the option does:
3497 .Bl -tag -width 15n
3498 .It Fl A Ar name
3499 Sets the elements of the array parameter
3500 .Ar name
3502 .Ar arg ...
3504 .Fl A
3505 is used, the array is reset (i.e. emptied) first; if
3506 .Ic +A
3507 is used, the first N elements are set (where N is the number of arguments);
3508 the rest are left untouched.
3509 .It Fl a | Ic allexport
3510 All new parameters are created with the export attribute.
3511 .It Fl b | Ic notify
3512 Print job notification messages asynchronously, instead of just before the
3513 prompt.
3514 Only used if job control is enabled
3515 .Pq Fl m .
3516 .It Fl C | Ic noclobber
3517 Prevent
3518 .Cm >
3519 redirection from overwriting existing files.
3520 Instead,
3521 .Cm >|
3522 must be used to force an overwrite.
3523 .It Fl e | Ic errexit
3524 Exit (after executing the
3525 .Dv ERR
3526 trap) as soon as an error occurs or a command fails (i.e. exits with a
3527 non-zero status).
3528 This does not apply to commands whose exit status is
3529 explicitly tested by a shell construct such as
3530 .Ic if ,
3531 .Ic until ,
3532 .Ic while ,
3534 .Ic !\&
3535 statements.
3537 .Ic &&
3539 .Ic || ,
3540 only the status of the last command is tested.
3541 .It Fl f | Ic noglob
3542 Do not expand file name patterns.
3543 .It Fl h | Ic trackall
3544 Create tracked aliases for all executed commands (see
3545 .Sx Aliases
3546 above).
3547 Enabled by default for non-interactive shells.
3548 .It Fl k | Ic keyword
3549 Parameter assignments are recognized anywhere in a command.
3550 .It Fl m | Ic monitor
3551 Enable job control (default for interactive shells).
3552 .It Fl n | Ic noexec
3553 Do not execute any commands.
3554 Useful for checking the syntax of scripts
3555 (ignored if interactive).
3556 .It Fl p | Ic privileged
3557 The shell is a privileged shell.
3558 It is set automatically if, when the shell starts,
3559 the real UID or GID does not match
3560 the effective UID (EUID) or GID (EGID), respectively.
3561 See above for a description of what this means.
3562 .It Fl s | Ic stdin
3563 If used when the shell is invoked, commands are read from standard input.
3564 Set automatically if the shell is invoked with no arguments.
3566 When
3567 .Fl s
3568 is used with the
3569 .Ic set
3570 command it causes the specified arguments to be sorted before assigning them to
3571 the positional parameters (or to array
3572 .Ar name ,
3574 .Fl A
3575 is used).
3576 .It Fl u | Ic nounset
3577 Referencing of an unset parameter is treated as an error, unless one of the
3578 .Ql - ,
3579 .Ql + ,
3581 .Ql =
3582 modifiers is used.
3583 .It Fl v | Ic verbose
3584 Write shell input to standard error as it is read.
3585 .It Fl X | Ic markdirs
3586 Mark directories with a trailing
3587 .Ql /
3588 during file name generation.
3589 .It Fl x | Ic xtrace
3590 Print commands and parameter assignments when they are executed, preceded by
3591 the value of
3592 .Ev PS4 .
3593 .It Ic bgnice
3594 Background jobs are run with lower priority.
3595 .It Ic braceexpand
3596 Enable brace expansion (a.k.a. alternation).
3597 .It Ic csh-history
3598 Enables a subset of
3599 .Xr csh 1 Ns -style
3600 history editing using the
3601 .Ql !\&
3602 character.
3603 .It Ic emacs
3604 Enable BRL emacs-like command-line editing (interactive shells only); see
3605 .Sx Emacs editing mode .
3606 .It Ic gmacs
3607 Enable gmacs-like command-line editing (interactive shells only).
3608 Currently identical to emacs editing except that transpose (^T) acts slightly
3609 differently.
3610 .It Ic ignoreeof
3611 The shell will not (easily) exit when end-of-file is read;
3612 .Ic exit
3613 must be used.
3614 To avoid infinite loops, the shell will exit if
3615 .Dv EOF
3616 is read 13 times in a row.
3617 .It Ic interactive
3618 The shell is an interactive shell.
3619 This option can only be used when the shell is invoked.
3620 See above for a description of what this means.
3621 .It Ic login
3622 The shell is a login shell.
3623 This option can only be used when the shell is invoked.
3624 See above for a description of what this means.
3625 .It Ic nohup
3626 Do not kill running jobs with a
3627 .Dv SIGHUP
3628 signal when a login shell exits.
3629 Currently set by default;
3630 this is different from the original Korn shell (which
3631 doesn't have this option, but does send the
3632 .Dv SIGHUP
3633 signal).
3634 .It Ic nolog
3635 No effect.
3636 In the original Korn shell, this prevents function definitions from
3637 being stored in the history file.
3638 .It Ic physical
3639 Causes the
3640 .Ic cd
3642 .Ic pwd
3643 commands to use
3644 .Dq physical
3645 (i.e. the filesystem's)
3646 .Sq ..
3647 directories instead of
3648 .Dq logical
3649 directories (i.e. the shell handles
3650 .Sq .. ,
3651 which allows the user to be oblivious of symbolic links to directories).
3652 Clear by default.
3653 Note that setting this option does not affect the current value of the
3654 .Ev PWD
3655 parameter; only the
3656 .Ic cd
3657 command changes
3658 .Ev PWD .
3659 See the
3660 .Ic cd
3662 .Ic pwd
3663 commands above for more details.
3664 .It Ic posix
3665 Enable POSIX mode.
3667 .Sx POSIX mode
3668 above.
3669 .It Ic restricted
3670 The shell is a restricted shell.
3671 This option can only be used when the shell is invoked.
3672 See above for a description of what this means.
3673 .It Ic sh
3674 Enable strict Bourne shell mode (see
3675 .Sx Strict Bourne shell mode
3676 above).
3677 .It Ic vi
3678 Enable
3679 .Xr vi 1 Ns -like
3680 command-line editing (interactive shells only).
3681 .It Ic vi-esccomplete
3682 In vi command-line editing, do command and file name completion when escape
3683 (^[) is entered in command mode.
3684 .It Ic vi-show8
3685 Prefix characters with the eighth bit set with
3686 .Sq M- .
3687 If this option is not set, characters in the range 128\-160 are printed as is,
3688 which may cause problems.
3689 .It Ic vi-tabcomplete
3690 In vi command-line editing, do command and file name completion when tab (^I)
3691 is entered in insert mode.
3692 This is the default.
3693 .It Ic viraw
3694 No effect.
3695 In the original Korn shell, unless
3696 .Ic viraw
3697 was set, the vi command-line mode would let the
3698 .Xr tty 4
3699 driver do the work until ESC (^[) was entered.
3700 .Nm ksh
3701 is always in viraw mode.
3704 These options can also be used upon invocation of the shell.
3705 The current set of
3706 options (with single letter names) can be found in the parameter
3707 .Sq $- .
3708 .Ic set Fl o
3709 with no option name will list all the options and whether each is on or off;
3710 .Ic set +o
3711 will print the current shell options in a form that
3712 can be reinput to the shell to achieve the same option settings.
3714 Remaining arguments, if any, are positional parameters and are assigned, in
3715 order, to the positional parameters (i.e. $1, $2, etc.).
3716 If options end with
3717 .Ql --
3718 and there are no remaining arguments, all positional parameters are cleared.
3719 If no options or arguments are given, the values of all names are printed.
3720 For unknown historical reasons, a lone
3721 .Ql -
3722 option is treated specially \- it clears both the
3723 .Fl x
3725 .Fl v
3726 options.
3728 .It Ic shift Op Ar number
3729 The positional parameters
3730 .Ar number Ns +1 ,
3731 .Ar number Ns +2 ,
3732 etc. are renamed to
3733 .Sq 1 ,
3734 .Sq 2 ,
3735 etc.
3736 .Ar number
3737 defaults to 1.
3739 .It Ic suspend
3740 Stops the shell as if it had received the suspend character from
3741 the terminal.
3742 It is not possible to suspend a login shell unless the parent process
3743 is a member of the same terminal session but is a member of a different
3744 process group.
3745 As a general rule, if the shell was started by another shell or via
3746 .Xr su 1 ,
3747 it can be suspended.
3749 .It Ic test Ar expression
3750 .It Ic \&[ Ar expression Ic \&]
3751 .Ic test
3752 evaluates the
3753 .Ar expression
3754 and returns zero status if true, 1 if false, or greater than 1 if there
3755 was an error.
3756 It is normally used as the condition command of
3757 .Ic if
3759 .Ic while
3760 statements.
3761 Symbolic links are followed for all
3762 .Ar file
3763 expressions except
3764 .Fl h
3766 .Fl L .
3768 The following basic expressions are available:
3769 .Bl -tag -width 17n
3770 .It Fl a Ar file
3771 .Ar file
3772 exists.
3773 .It Fl b Ar file
3774 .Ar file
3775 is a block special device.
3776 .It Fl c Ar file
3777 .Ar file
3778 is a character special device.
3779 .It Fl d Ar file
3780 .Ar file
3781 is a directory.
3782 .It Fl e Ar file
3783 .Ar file
3784 exists.
3785 .It Fl f Ar file
3786 .Ar file
3787 is a regular file.
3788 .It Fl G Ar file
3789 .Ar file Ns 's
3790 group is the shell's effective group ID.
3791 .It Fl g Ar file
3792 .Ar file Ns 's
3793 mode has the setgid bit set.
3794 .It Fl h Ar file
3795 .Ar file
3796 is a symbolic link.
3797 .It Fl k Ar file
3798 .Ar file Ns 's
3799 mode has the
3800 .Xr sticky 8
3801 bit set.
3802 .It Fl L Ar file
3803 .Ar file
3804 is a symbolic link.
3805 .It Fl O Ar file
3806 .Ar file Ns 's
3807 owner is the shell's effective user ID.
3808 .It Fl o Ar option
3809 Shell
3810 .Ar option
3811 is set (see the
3812 .Ic set
3813 command above for a list of options).
3814 As a non-standard extension, if the option starts with a
3815 .Ql \&! ,
3816 the test is negated; the test always fails if
3817 .Ar option
3818 doesn't exist (so [ -o foo -o -o !foo ] returns true if and only if option
3819 .Ar foo
3820 exists).
3821 .It Fl p Ar file
3822 .Ar file
3823 is a named pipe.
3824 .It Fl r Ar file
3825 .Ar file
3826 exists and is readable.
3827 .It Fl S Ar file
3828 .Ar file
3829 is a
3830 .Xr unix 4 Ns -domain
3831 socket.
3832 .It Fl s Ar file
3833 .Ar file
3834 is not empty.
3835 .It Fl t Op Ar fd
3836 File descriptor
3837 .Ar fd
3838 is a
3839 .Xr tty 4
3840 device.
3841 If the
3842 .Ic posix
3843 option is not set,
3844 .Ar fd
3845 may be left out, in which case it is taken to be 1 (the behaviour differs due
3846 to the special POSIX rules described above).
3847 .It Fl u Ar file
3848 .Ar file Ns 's
3849 mode has the setuid bit set.
3850 .It Fl w Ar file
3851 .Ar file
3852 exists and is writable.
3853 .It Fl x Ar file
3854 .Ar file
3855 exists and is executable.
3856 .It Ar file1 Fl nt Ar file2
3857 .Ar file1
3858 is newer than
3859 .Ar file2
3861 .Ar file1
3862 exists and
3863 .Ar file2
3864 does not.
3865 .It Ar file1 Fl ot Ar file2
3866 .Ar file1
3867 is older than
3868 .Ar file2
3870 .Ar file2
3871 exists and
3872 .Ar file1
3873 does not.
3874 .It Ar file1 Fl ef Ar file2
3875 .Ar file1
3876 is the same file as
3877 .Ar file2 .
3878 .It Ar string
3879 .Ar string
3880 has non-zero length.
3881 .It Fl n Ar string
3882 .Ar string
3883 is not empty.
3884 .It Fl z Ar string
3885 .Ar string
3886 is empty.
3887 .It Ar string No = Ar string
3888 Strings are equal.
3889 .It Ar string No == Ar string
3890 Strings are equal.
3891 .It Ar string No != Ar string
3892 Strings are not equal.
3893 .It Ar number Fl eq Ar number
3894 Numbers compare equal.
3895 .It Ar number Fl ne Ar number
3896 Numbers compare not equal.
3897 .It Ar number Fl ge Ar number
3898 Numbers compare greater than or equal.
3899 .It Ar number Fl gt Ar number
3900 Numbers compare greater than.
3901 .It Ar number Fl le Ar number
3902 Numbers compare less than or equal.
3903 .It Ar number Fl \&lt Ar number
3904 Numbers compare less than.
3907 The above basic expressions, in which unary operators have precedence over
3908 binary operators, may be combined with the following operators (listed in
3909 increasing order of precedence):
3910 .Bd -literal -offset indent
3911 expr -o expr            Logical OR.
3912 expr -a expr            Logical AND.
3913 ! expr                  Logical NOT.
3914 ( expr )                Grouping.
3917 On operating systems not supporting
3918 .Pa /dev/fd/ Ns Ar n
3919 devices (where
3920 .Ar n
3921 is a file descriptor number), the
3922 .Ic test
3923 command will attempt to fake it for all tests that operate on files (except the
3924 .Fl e
3925 test).
3926 For example,
3927 [ -w /dev/fd/2 ] tests if file descriptor 2 is writable.
3929 Note that some special rules are applied (courtesy of POSIX)
3930 if the number of
3931 arguments to
3932 .Ic test
3934 .Ic \&[ ... \&]
3935 is less than five: if leading
3936 .Ql \&!
3937 arguments can be stripped such that only one argument remains then a string
3938 length test is performed (again, even if the argument is a unary operator); if
3939 leading
3940 .Ql \&!
3941 arguments can be stripped such that three arguments remain and the second
3942 argument is a binary operator, then the binary operation is performed (even
3943 if the first argument is a unary operator, including an unstripped
3944 .Ql \&! ) .
3946 .Sy Note :
3947 A common mistake is to use
3948 .Dq if \&[ $foo = bar \&]
3949 which fails if parameter
3950 .Dq foo
3952 .Dv NULL
3953 or unset, if it has embedded spaces (i.e.\&
3954 .Ev IFS
3955 characters), or if it is a unary operator like
3956 .Sq \&!
3958 .Sq Fl n .
3959 Use tests like
3960 .Dq if \&[ \&"X$foo\&" = Xbar \&]
3961 instead.
3963 .It Xo
3964 .Ic time
3965 .Op Fl p
3966 .Op Ar pipeline
3968 If a
3969 .Ar pipeline
3970 is given, the times used to execute the pipeline are reported.
3971 If no pipeline
3972 is given, then the user and system time used by the shell itself, and all the
3973 commands it has run since it was started, are reported.
3974 The times reported are the real time (elapsed time from start to finish),
3975 the user CPU time (time spent running in user mode), and the system CPU time
3976 (time spent running in kernel mode).
3977 Times are reported to standard error; the format of the output is:
3979 .Dl "0m0.00s real     0m0.00s user     0m0.00s system"
3981 If the
3982 .Fl p
3983 option is given the output is slightly longer:
3984 .Bd -literal -offset indent
3985 real     0.00
3986 user     0.00
3987 sys      0.00
3990 It is an error to specify the
3991 .Fl p
3992 option unless
3993 .Ar pipeline
3994 is a simple command.
3996 Simple redirections of standard error do not affect the output of the
3997 .Ic time
3998 command:
4000 .Dl $ time sleep 1 2> afile
4001 .Dl $ { time sleep 1; } 2> afile
4003 Times for the first command do not go to
4004 .Dq afile ,
4005 but those of the second command do.
4007 .It Ic times
4008 Print the accumulated user and system times used both by the shell
4009 and by processes that the shell started which have exited.
4010 The format of the output is:
4011 .Bd -literal -offset indent
4012 0m0.00s 0m0.00s
4013 0m0.00s 0m0.00s
4016 .It Ic trap Op Ar handler signal ...
4017 Sets a trap handler that is to be executed when any of the specified signals are
4018 received.
4019 .Ar handler
4020 is either a
4021 .Dv NULL
4022 string, indicating the signals are to be ignored, a minus sign
4023 .Pq Sq - ,
4024 indicating that the default action is to be taken for the signals (see
4025 .Xr signal 3 ) ,
4026 or a string containing shell commands to be evaluated and executed at the first
4027 opportunity (i.e. when the current command completes, or before printing the
4028 next
4029 .Ev PS1
4030 prompt) after receipt of one of the signals.
4031 .Ar signal
4032 is the name of a signal (e.g.\&
4033 .Dv PIPE
4035 .Dv ALRM )
4036 or the number of the signal (see the
4037 .Ic kill -l
4038 command above).
4040 There are two special signals:
4041 .Dv EXIT
4042 (also known as 0), which is executed when the shell is about to exit, and
4043 .Dv ERR ,
4044 which is executed after an error occurs (an error is something that would cause
4045 the shell to exit if the
4046 .Fl e
4048 .Ic errexit
4049 option were set \- see the
4050 .Ic set
4051 command above).
4052 .Dv EXIT
4053 handlers are executed in the environment of the last executed command.
4054 Note
4055 that for non-interactive shells, the trap handler cannot be changed for signals
4056 that were ignored when the shell started.
4058 With no arguments,
4059 .Ic trap
4060 lists, as a series of
4061 .Ic trap
4062 commands, the current state of the traps that have been set since the shell
4063 started.
4064 Note that the output of
4065 .Ic trap
4066 cannot be usefully piped to another process (an artifact of the fact that
4067 traps are cleared when subprocesses are created).
4069 The original Korn shell's
4070 .Dv DEBUG
4071 trap and the handling of
4072 .Dv ERR
4074 .Dv EXIT
4075 traps in functions are not yet implemented.
4077 .It Ic true
4078 A command that exits with a zero value.
4080 .It Ic type
4081 Short form of
4082 .Ic command Fl V
4083 (see above).
4085 .It Xo
4086 .Ic typeset
4088 .Op Ic +-lprtUux
4089 .Op Fl L Ns Op Ar n
4090 .Op Fl R Ns Op Ar n
4091 .Op Fl Z Ns Op Ar n
4092 .Op Fl i Ns Op Ar n
4093 .No \&| Fl f Op Fl tux
4096 .Ar name
4097 .Op Ns = Ns Ar value
4098 .Ar ...
4101 Display or set parameter attributes.
4102 With no
4103 .Ar name
4104 arguments, parameter attributes are displayed; if no options are used, the
4105 current attributes of all parameters are printed as
4106 .Ic typeset
4107 commands; if an option is given (or
4108 .Ql -
4109 with no option letter), all parameters and their values with the specified
4110 attributes are printed; if options are introduced with
4111 .Ql + ,
4112 parameter values are not printed.
4115 .Ar name
4116 arguments are given, the attributes of the named parameters are set
4117 .Pq Ic -
4118 or cleared
4119 .Pq Ic + .
4120 Values for parameters may optionally be specified.
4122 .Ic typeset
4123 is used inside a function, any newly created parameters are local to the
4124 function.
4126 When
4127 .Fl f
4128 is used,
4129 .Ic typeset
4130 operates on the attributes of functions.
4131 As with parameters, if no
4132 .Ar name
4133 arguments are given,
4134 functions are listed with their values (i.e. definitions) unless
4135 options are introduced with
4136 .Ql + ,
4137 in which case only the function names are reported.
4138 .Bl -tag -width Ds
4139 .It Fl f
4140 Function mode.
4141 Display or set functions and their attributes, instead of parameters.
4142 .It Fl i Ns Op Ar n
4143 Integer attribute.
4144 .Ar n
4145 specifies the base to use when displaying the integer (if not specified, the
4146 base given in the first assignment is used).
4147 Parameters with this attribute may
4148 be assigned values containing arithmetic expressions.
4149 .It Fl L Ns Op Ar n
4150 Left justify attribute.
4151 .Ar n
4152 specifies the field width.
4154 .Ar n
4155 is not specified, the current width of a parameter (or the width of its first
4156 assigned value) is used.
4157 Leading whitespace (and zeros, if used with the
4158 .Fl Z
4159 option) is stripped.
4160 If necessary, values are either truncated or space padded
4161 to fit the field width.
4162 .It Fl l
4163 Lower case attribute.
4164 All upper case characters in values are converted to lower case.
4165 (In the original Korn shell, this parameter meant
4166 .Dq long integer
4167 when used with the
4168 .Fl i
4169 option.)
4170 .It Fl p
4171 Print complete
4172 .Ic typeset
4173 commands that can be used to re-create the attributes (but not the values) of
4174 parameters.
4175 This is the default action (option exists for ksh93 compatibility).
4176 .It Fl R Ns Op Ar n
4177 Right justify attribute.
4178 .Ar n
4179 specifies the field width.
4181 .Ar n
4182 is not specified, the current width of a parameter (or the width of its first
4183 assigned value) is used.
4184 Trailing whitespace is stripped.
4185 If necessary, values are either stripped of leading characters or space
4186 padded to make them fit the field width.
4187 .It Fl r
4188 Read-only attribute.
4189 Parameters with this attribute may not be assigned to or unset.
4190 Once this attribute is set, it cannot be turned off.
4191 .It Fl t
4192 Tag attribute.
4193 Has no meaning to the shell; provided for application use.
4195 For functions,
4196 .Fl t
4197 is the trace attribute.
4198 When functions with the trace attribute are executed, the
4199 .Ic xtrace
4200 .Pq Fl x
4201 shell option is temporarily turned on.
4202 .It Fl U
4203 Unsigned integer attribute.
4204 Integers are printed as unsigned values (only
4205 useful when combined with the
4206 .Fl i
4207 option).
4208 This option is not in the original Korn shell.
4209 .It Fl u
4210 Upper case attribute.
4211 All lower case characters in values are converted to upper case.
4212 (In the original Korn shell, this parameter meant
4213 .Dq unsigned integer
4214 when used with the
4215 .Fl i
4216 option, which meant upper case letters would never be used for bases greater
4217 than 10.
4218 See the
4219 .Fl U
4220 option.)
4222 For functions,
4223 .Fl u
4224 is the undefined attribute.
4226 .Sx Functions
4227 above for the implications of this.
4228 .It Fl x
4229 Export attribute.
4230 Parameters (or functions) are placed in the environment of
4231 any executed commands.
4232 Exported functions are not yet implemented.
4233 .It Fl Z Ns Op Ar n
4234 Zero fill attribute.
4235 If not combined with
4236 .Fl L ,
4237 this is the same as
4238 .Fl R ,
4239 except zero padding is used instead of space padding.
4242 .It Xo
4243 .Ic ulimit
4244 .Op Fl acdfHlmnpSst Op Ar value
4245 .Ar ...
4247 Display or set process limits.
4248 If no options are used, the file size limit
4249 .Pq Fl f
4250 is assumed.
4251 .Ar value ,
4252 if specified, may be either an arithmetic expression starting with a
4253 number or the word
4254 .Dq unlimited .
4255 The limits affect the shell and any processes created by the shell after a
4256 limit is imposed; limits may not be increased once they are set.
4257 .Bl -tag -width 5n
4258 .It Fl a
4259 Display all limits; unless
4260 .Fl H
4261 is used, soft limits are displayed.
4262 .It Fl c Ar n
4263 Impose a size limit of
4264 .Ar n
4265 blocks on the size of core dumps.
4266 .It Fl d Ar n
4267 Impose a size limit of
4268 .Ar n
4269 kilobytes on the size of the data area.
4270 .It Fl f Ar n
4271 Impose a size limit of
4272 .Ar n
4273 blocks on files written by the shell and its child processes (files of any
4274 size may be read).
4275 .It Fl H
4276 Set the hard limit only (the default is to set both hard and soft limits).
4277 .It Fl l Ar n
4278 Impose a limit of
4279 .Ar n
4280 kilobytes on the amount of locked (wired) physical memory.
4281 .It Fl m Ar n
4282 Impose a limit of
4283 .Ar n
4284 kilobytes on the amount of physical memory used.
4285 This limit is not enforced.
4286 .It Fl n Ar n
4287 Impose a limit of
4288 .Ar n
4289 file descriptors that can be open at once.
4290 .It Fl p Ar n
4291 Impose a limit of
4292 .Ar n
4293 processes that can be run by the user at any one time.
4294 .It Fl S
4295 Set the soft limit only (the default is to set both hard and soft limits).
4296 .It Fl s Ar n
4297 Impose a size limit of
4298 .Ar n
4299 kilobytes on the size of the stack area.
4300 .It Fl t Ar n
4301 Impose a time limit of
4302 .Ar n
4303 CPU seconds spent in user mode to be used by each process.
4304 .\".It Fl v Ar n
4305 .\"Impose a limit of
4306 .\"Ar n
4307 .\"kilobytes on the amount of virtual memory used.
4310 As far as
4311 .Ic ulimit
4312 is concerned, a block is 512 bytes.
4314 .It Xo
4315 .Ic umask
4316 .Op Fl S
4317 .Op Ar mask
4319 Display or set the file permission creation mask, or umask (see
4320 .Xr umask 2 ) .
4321 If the
4322 .Fl S
4323 option is used, the mask displayed or set is symbolic; otherwise, it is an
4324 octal number.
4326 Symbolic masks are like those used by
4327 .Xr chmod 1 .
4328 When used, they describe what permissions may be made available (as opposed to
4329 octal masks in which a set bit means the corresponding bit is to be cleared).
4330 For example,
4331 .Dq ug=rwx,o=
4332 sets the mask so files will not be readable, writable, or executable by
4333 .Dq others ,
4334 and is equivalent (on most systems) to the octal mask
4335 .Dq 007 .
4337 .It Xo
4338 .Ic unalias
4339 .Op Fl adt
4340 .Op Ar name ...
4342 The aliases for the given names are removed.
4343 If the
4344 .Fl a
4345 option is used, all aliases are removed.
4346 If the
4347 .Fl t
4349 .Fl d
4350 options are used, the indicated operations are carried out on tracked or
4351 directory aliases, respectively.
4353 .It Xo
4354 .Ic unset
4355 .Op Fl fv
4356 .Ar parameter ...
4358 Unset the named parameters
4360 .Fl v ,
4361 the default
4363 or functions
4364 .Pq Fl f .
4365 The exit status is non-zero if any of the parameters have the read-only
4366 attribute set, zero otherwise.
4368 .It Ic wait Op Ar job ...
4369 Wait for the specified job(s) to finish.
4370 The exit status of
4371 .Ic wait
4372 is that of the last specified job; if the last job is killed by a signal, the
4373 exit status is 128 + the number of the signal (see
4374 .Ic kill -l Ar exit-status
4375 above); if the last specified job can't be found (because it never existed, or
4376 had already finished), the exit status of
4377 .Ic wait
4378 is 127.
4380 .Sx Job control
4381 below for the format of
4382 .Ar job .
4383 .Ic wait
4384 will return if a signal for which a trap has been set is received, or if a
4385 .Dv SIGHUP ,
4386 .Dv SIGINT ,
4388 .Dv SIGQUIT
4389 signal is received.
4391 If no jobs are specified,
4392 .Ic wait
4393 waits for all currently running jobs (if any) to finish and exits with a zero
4394 status.
4395 If job monitoring is enabled, the completion status of jobs is printed
4396 (this is not the case when jobs are explicitly specified).
4398 .It Xo
4399 .Ic whence
4400 .Op Fl pv
4401 .Op Ar name ...
4403 For each
4404 .Ar name ,
4405 the type of command is listed (reserved word, built-in, alias,
4406 function, tracked alias, or executable).
4407 If the
4408 .Fl p
4409 option is used, a path search is performed even if
4410 .Ar name
4411 is a reserved word, alias, etc.
4412 Without the
4413 .Fl v
4414 option,
4415 .Ic whence
4416 is similar to
4417 .Ic command Fl v
4418 except that
4419 .Ic whence
4420 won't print aliases as alias commands.
4421 With the
4422 .Fl v
4423 option,
4424 .Ic whence
4425 is the same as
4426 .Ic command Fl V .
4427 Note that for
4428 .Ic whence ,
4430 .Fl p
4431 option does not affect the search path used, as it does for
4432 .Ic command .
4433 If the type of one or more of the names could not be determined, the exit
4434 status is non-zero.
4436 .Ss Job control
4437 Job control refers to the shell's ability to monitor and control jobs, which
4438 are processes or groups of processes created for commands or pipelines.
4439 At a minimum, the shell keeps track of the status of the background (i.e.\&
4440 asynchronous) jobs that currently exist; this information can be displayed
4441 using the
4442 .Ic jobs
4443 commands.
4444 If job control is fully enabled (using
4445 .Ic set -m
4447 .Ic set -o monitor ) ,
4448 as it is for interactive shells, the processes of a job are placed in their
4449 own process group.
4450 Foreground jobs can be stopped by typing the suspend
4451 character from the terminal (normally ^Z), jobs can be restarted in either the
4452 foreground or background using the
4453 .Ic fg
4455 .Ic bg
4456 commands, and the state of the terminal is saved or restored when a foreground
4457 job is stopped or restarted, respectively.
4459 Note that only commands that create processes (e.g. asynchronous commands,
4460 subshell commands, and non-built-in, non-function commands) can be stopped;
4461 commands like
4462 .Ic read
4463 cannot be.
4465 When a job is created, it is assigned a job number.
4466 For interactive shells, this number is printed inside
4467 .Dq [..] ,
4468 followed by the process IDs of the processes in the job when an asynchronous
4469 command is run.
4470 A job may be referred to in the
4471 .Ic bg ,
4472 .Ic fg ,
4473 .Ic jobs ,
4474 .Ic kill ,
4476 .Ic wait
4477 commands either by the process ID of the last process in the command pipeline
4478 (as stored in the
4479 .Ic $!\&
4480 parameter) or by prefixing the job number with a percent
4481 sign
4482 .Pq Sq % .
4483 Other percent sequences can also be used to refer to jobs:
4484 .Bl -tag -width "%+ | %% | %XX"
4485 .It %+ | %% | %
4486 The most recently stopped job or, if there are no stopped jobs, the oldest
4487 running job.
4488 .It %-
4489 The job that would be the
4490 .Ic %+
4491 job if the latter did not exist.
4492 .It % Ns Ar n
4493 The job with job number
4494 .Ar n .
4495 .It %? Ns Ar string
4496 The job with its command containing the string
4497 .Ar string
4498 (an error occurs if multiple jobs are matched).
4499 .It % Ns Ar string
4500 The job with its command starting with the string
4501 .Ar string
4502 (an error occurs if multiple jobs are matched).
4505 When a job changes state (e.g. a background job finishes or foreground job is
4506 stopped), the shell prints the following status information:
4508 .D1 [ Ns Ar number ] Ar flag status command
4510 where...
4511 .Bl -tag -width "command"
4512 .It Ar number
4513 is the job number of the job;
4514 .It Ar flag
4515 is the
4516 .Ql +
4518 .Ql -
4519 character if the job is the
4520 .Ic %+
4522 .Ic %-
4523 job, respectively, or space if it is neither;
4524 .It Ar status
4525 indicates the current state of the job and can be:
4526 .Bl -tag -width "RunningXX"
4527 .It Done Op Ar number
4528 The job exited.
4529 .Ar number
4530 is the exit status of the job, which is omitted if the status is zero.
4531 .It Running
4532 The job has neither stopped nor exited (note that running does not necessarily
4533 mean consuming CPU time \-
4534 the process could be blocked waiting for some event).
4535 .It Stopped Op Ar signal
4536 The job was stopped by the indicated
4537 .Ar signal
4538 (if no signal is given, the job was stopped by
4539 .Dv SIGTSTP ) .
4540 .It Ar signal-description Op Dq core dumped
4541 The job was killed by a signal (e.g. memory fault, hangup); use
4542 .Ic kill -l
4543 for a list of signal descriptions.
4545 .Dq core dumped
4546 message indicates the process created a core file.
4548 .It Ar command
4549 is the command that created the process.
4550 If there are multiple processes in
4551 the job, each process will have a line showing its
4552 .Ar command
4553 and possibly its
4554 .Ar status ,
4555 if it is different from the status of the previous process.
4558 When an attempt is made to exit the shell while there are jobs in the stopped
4559 state, the shell warns the user that there are stopped jobs and does not exit.
4560 If another attempt is immediately made to exit the shell, the stopped jobs are
4561 sent a
4562 .Dv SIGHUP
4563 signal and the shell exits.
4564 Similarly, if the
4565 .Ic nohup
4566 option is not set and there are running jobs when an attempt is made to exit
4567 a login shell, the shell warns the user and does not exit.
4568 If another attempt
4569 is immediately made to exit the shell, the running jobs are sent a
4570 .Dv SIGHUP
4571 signal and the shell exits.
4572 .Ss Interactive input line editing
4573 The shell supports three modes of reading command lines from a
4574 .Xr tty 4
4575 in an interactive session, controlled by the
4576 .Ic emacs ,
4577 .Ic gmacs ,
4579 .Ic vi
4580 options (at most one of these can be set at once).
4581 The default is
4582 .Ic emacs .
4583 Editing modes can be set explicitly using the
4584 .Ic set
4585 built-in, or implicitly via the
4586 .Ev EDITOR
4588 .Ev VISUAL
4589 environment variables.
4590 If none of these options are enabled,
4591 the shell simply reads lines using the normal
4592 .Xr tty 4
4593 driver.
4594 If the
4595 .Ic emacs
4597 .Ic gmacs
4598 option is set, the shell allows emacs-like editing of the command; similarly,
4599 if the
4600 .Ic vi
4601 option is set, the shell allows vi-like editing of the command.
4602 These modes are described in detail in the following sections.
4604 In these editing modes, if a line is longer than the screen width (see the
4605 .Ev COLUMNS
4606 parameter),
4608 .Ql > ,
4609 .Ql + ,
4611 .Ql <
4612 character is displayed in the last column indicating that there are more
4613 characters after, before and after, or before the current position,
4614 respectively.
4615 The line is scrolled horizontally as necessary.
4616 .Ss Emacs editing mode
4617 When the
4618 .Ic emacs
4619 option is set, interactive input line editing is enabled.
4620 Warning: This mode is
4621 slightly different from the emacs mode in the original Korn shell.
4622 In this mode, various editing commands
4623 (typically bound to one or more control characters) cause immediate actions
4624 without waiting for a newline.
4625 Several editing commands are bound to particular
4626 control characters when the shell is invoked; these bindings can be changed
4627 using the
4628 .Ic bind
4629 command.
4631 The following is a list of available editing commands.
4632 Each description starts with the name of the command,
4633 suffixed with a colon;
4635 .Op Ar n
4636 (if the command can be prefixed with a count); and any keys the command is
4637 bound to by default, written using caret notation
4638 e.g. the ASCII ESC character is written as ^[.
4639 ^[A-Z] sequences are not case sensitive.
4640 A count prefix for a command is entered using the sequence
4641 .Pf ^[ Ar n ,
4642 where
4643 .Ar n
4644 is a sequence of 1 or more digits.
4645 Unless otherwise specified, if a count is
4646 omitted, it defaults to 1.
4648 Note that editing command names are used only with the
4649 .Ic bind
4650 command.
4651 Furthermore, many editing commands are useful only on terminals with
4652 a visible cursor.
4653 The default bindings were chosen to resemble corresponding
4654 Emacs key bindings.
4655 The user's
4656 .Xr tty 4
4657 characters (e.g.\&
4658 .Dv ERASE )
4659 are bound to
4660 reasonable substitutes and override the default bindings.
4661 .Bl -tag -width Ds
4662 .It abort: ^C, ^G
4663 Useful as a response to a request for a
4664 .Ic search-history
4665 pattern in order to abort the search.
4666 .It auto-insert: Op Ar n
4667 Simply causes the character to appear as literal input.
4668 Most ordinary characters are bound to this.
4669 .It Xo backward-char:
4670 .Op Ar n
4671 .No ^B , ^X^D
4673 Moves the cursor backward
4674 .Ar n
4675 characters.
4676 .It Xo backward-word:
4677 .Op Ar n
4678 .No ^[b
4680 Moves the cursor backward to the beginning of the word; words consist of
4681 alphanumerics, underscore
4682 .Pq Sq _ ,
4683 and dollar sign
4684 .Pq Sq $
4685 characters.
4686 .It beginning-of-history: ^[<
4687 Moves to the beginning of the history.
4688 .It beginning-of-line: ^A
4689 Moves the cursor to the beginning of the edited input line.
4690 .It Xo capitalize-word:
4691 .Op Ar n
4692 .No ^[C , ^[c
4694 Uppercase the first character in the next
4695 .Ar n
4696 words, leaving the cursor past the end of the last word.
4697 .It clear-screen:
4698 Clears the screen if the
4699 .Ev TERM
4700 parameter is set and the terminal supports clearing the screen, then
4701 reprints the prompt string and the current input line.
4702 .It comment: ^[#
4703 If the current line does not begin with a comment character, one is added at
4704 the beginning of the line and the line is entered (as if return had been
4705 pressed); otherwise, the existing comment characters are removed and the cursor
4706 is placed at the beginning of the line.
4707 .It complete: ^[^[
4708 Automatically completes as much as is unique of the command name or the file
4709 name containing the cursor.
4710 If the entire remaining command or file name is
4711 unique, a space is printed after its completion, unless it is a directory name
4712 in which case
4713 .Ql /
4714 is appended.
4715 If there is no command or file name with the current partial word
4716 as its prefix, a bell character is output (usually causing a beep to be
4717 sounded).
4719 Custom completions may be configured by creating an array named
4720 .Ql complete_command ,
4721 optionally suffixed with an argument number to complete only for a single
4722 argument.
4723 So defining an array named
4724 .Ql complete_kill
4725 provides possible completions for any argument to the
4726 .Xr kill 1
4727 command, but
4728 .Ql complete_kill_1
4729 only completes the first argument.
4730 For example, the following command makes
4732 offer a selection of signal names for the first argument to
4733 .Xr kill 1 :
4735 .Dl set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
4736 .It complete-command: ^X^[
4737 Automatically completes as much as is unique of the command name having the
4738 partial word up to the cursor as its prefix, as in the
4739 .Ic complete
4740 command above.
4741 .It complete-file: ^[^X
4742 Automatically completes as much as is unique of the file name having the
4743 partial word up to the cursor as its prefix, as in the
4744 .Ic complete
4745 command described above.
4746 .It complete-list: ^I, ^[=
4747 Complete as much as is possible of the current word,
4748 and list the possible completions for it.
4749 If only one completion is possible,
4750 match as in the
4751 .Ic complete
4752 command above.
4753 .It Xo delete-char-backward:
4754 .Op Ar n
4755 .No ERASE , ^? , ^H
4757 Deletes
4758 .Ar n
4759 characters before the cursor.
4760 .It Xo delete-char-forward:
4761 .Op Ar n
4762 .No Delete
4764 Deletes
4765 .Ar n
4766 characters after the cursor.
4767 .It Xo delete-word-backward:
4768 .Op Ar n
4769 .No WERASE , ^[ERASE , ^W, ^[^? , ^[^H , ^[h
4771 Deletes
4772 .Ar n
4773 words before the cursor.
4774 .It Xo delete-word-forward:
4775 .Op Ar n
4776 .No ^[d
4778 Deletes
4779 .Ar n
4780 words after the cursor.
4781 .It Xo down-history:
4782 .Op Ar n
4783 .No ^N , ^XB
4785 Scrolls the history buffer forward
4786 .Ar n
4787 lines (later).
4788 Each input line originally starts just after the last entry
4789 in the history buffer, so
4790 .Ic down-history
4791 is not useful until either
4792 .Ic search-history
4794 .Ic up-history
4795 has been performed.
4796 .It Xo downcase-word:
4797 .Op Ar n
4798 .No ^[L , ^[l
4800 Lowercases the next
4801 .Ar n
4802 words.
4803 .It end-of-history: ^[>
4804 Moves to the end of the history.
4805 .It end-of-line: ^E
4806 Moves the cursor to the end of the input line.
4807 .It eot: ^_
4808 Acts as an end-of-file; this is useful because edit-mode input disables
4809 normal terminal input canonicalization.
4810 .It Xo eot-or-delete:
4811 .Op Ar n
4812 .No ^D
4814 Acts as
4815 .Ic eot
4816 if alone on a line; otherwise acts as
4817 .Ic delete-char-forward .
4818 .It error:
4819 Error (ring the bell).
4820 .It exchange-point-and-mark: ^X^X
4821 Places the cursor where the mark is and sets the mark to where the cursor was.
4822 .It expand-file: ^[*
4823 Appends a
4824 .Ql *
4825 to the current word and replaces the word with the result of performing file
4826 globbing on the word.
4827 If no files match the pattern, the bell is rung.
4828 .It Xo forward-char:
4829 .Op Ar n
4830 .No ^F , ^XC
4832 Moves the cursor forward
4833 .Ar n
4834 characters.
4835 .It Xo forward-word:
4836 .Op Ar n
4837 .No ^[f
4839 Moves the cursor forward to the end of the
4840 .Ar n Ns th
4841 word.
4842 .It Xo goto-history:
4843 .Op Ar n
4844 .No ^[g
4846 Goes to history number
4847 .Ar n .
4848 .It kill-line: KILL
4849 Deletes the entire input line.
4850 .It Xo kill-to-eol:
4851 .Op Ar n
4852 .No ^K
4854 Deletes the input from the cursor to the end of the line if
4855 .Ar n
4856 is not specified; otherwise deletes characters between the cursor and column
4857 .Ar n .
4858 .It list: ^[?
4859 Prints a sorted, columnated list of command names or file names (if any) that
4860 can complete the partial word containing the cursor.
4861 Directory names have
4862 .Ql /
4863 appended to them.
4864 .It list-command: ^X?
4865 Prints a sorted, columnated list of command names (if any) that can complete
4866 the partial word containing the cursor.
4867 .It list-file: ^X^Y
4868 Prints a sorted, columnated list of file names (if any) that can complete the
4869 partial word containing the cursor.
4870 File type indicators are appended as described under
4871 .Ic list
4872 above.
4873 .It newline: ^J , ^M
4874 Causes the current input line to be processed by the shell.
4875 The current cursor position may be anywhere on the line.
4876 .It newline-and-next: ^O
4877 Causes the current input line to be processed by the shell, and the next line
4878 from history becomes the current line.
4879 This is only useful after an
4880 .Ic up-history
4882 .Ic search-history .
4883 .It no-op: QUIT
4884 This does nothing.
4885 .It Xo prev-hist-word:
4886 .Op Ar n
4887 .No ^[. , ^[_
4889 The last
4890 .Pq Ar n Ns th
4891 word of the previous command is inserted at the cursor.
4892 .It quote: ^^
4893 The following character is taken literally rather than as an editing command.
4894 .It redraw: ^L
4895 Reprints the prompt string and the current input line.
4896 .It Xo search-character-backward:
4897 .Op Ar n
4898 .No ^[^]
4900 Search backward in the current line for the
4901 .Ar n Ns th
4902 occurrence of the next character typed.
4903 .It Xo search-character-forward:
4904 .Op Ar n
4905 .No ^]
4907 Search forward in the current line for the
4908 .Ar n Ns th
4909 occurrence of the next character typed.
4910 .It search-history: ^R
4911 Enter incremental search mode.
4912 The internal history list is searched
4913 backwards for commands matching the input.
4914 An initial
4915 .Ql ^
4916 in the search string anchors the search.
4917 The abort key will leave search mode.
4918 Other commands will be executed after leaving search mode.
4919 Successive
4920 .Ic search-history
4921 commands continue searching backward to the next previous occurrence of the
4922 pattern.
4923 The history buffer retains only a finite number of lines; the oldest
4924 are discarded as necessary.
4925 .It set-mark-command: ^[ Ns Aq space
4926 Set the mark at the cursor position.
4927 .It transpose-chars: ^T
4928 If at the end of line, or if the
4929 .Ic gmacs
4930 option is set, this exchanges the two previous characters; otherwise, it
4931 exchanges the previous and current characters and moves the cursor one
4932 character to the right.
4933 .It Xo up-history:
4934 .Op Ar n
4935 .No ^P , ^XA
4937 Scrolls the history buffer backward
4938 .Ar n
4939 lines (earlier).
4940 .It Xo upcase-word:
4941 .Op Ar n
4942 .No ^[U , ^[u
4944 Uppercase the next
4945 .Ar n
4946 words.
4947 .It quote: ^V
4948 Synonym for ^^.
4949 .It yank: ^Y
4950 Inserts the most recently killed text string at the current cursor position.
4951 .It yank-pop: ^[y
4952 Immediately after a
4953 .Ic yank ,
4954 replaces the inserted text string with the next previously killed text string.
4957 The following editing commands lack default bindings but can be used with the
4958 .Ic bind
4959 command:
4960 .Bl -tag -width Ds
4961 .It kill-region
4962 Deletes the input between the cursor and the mark.
4964 .Ss Vi editing mode
4965 The vi command-line editor in
4967 has basically the same commands as the
4968 .Xr vi 1
4969 editor with the following exceptions:
4970 .Bl -bullet
4972 You start out in insert mode.
4974 There are file name and command completion commands:
4975 =, \e, *, ^X, ^E, ^F, and, optionally,
4976 .Aq tab
4978 .Aq esc .
4981 .Ic _
4982 command is different (in
4984 it is the last argument command; in
4985 .Xr vi 1
4986 it goes to the start of the current line).
4989 .Ic /
4991 .Ic G
4992 commands move in the opposite direction to the
4993 .Ic j
4994 command.
4996 Commands which don't make sense in a single line editor are not available
4997 (e.g. screen movement commands and
4998 .Xr ex 1 Ns -style
4999 colon
5000 .Pq Ic \&:
5001 commands).
5004 Note that the ^X stands for control-X; also
5005 .Aq esc ,
5006 .Aq space ,
5008 .Aq tab
5009 are used for escape, space, and tab, respectively (no kidding).
5011 Like
5012 .Xr vi 1 ,
5013 there are two modes:
5014 .Dq insert
5015 mode and
5016 .Dq command
5017 mode.
5018 In insert mode, most characters are simply put in the buffer at the
5019 current cursor position as they are typed; however, some characters are
5020 treated specially.
5021 In particular, the following characters are taken from current
5022 .Xr tty 4
5023 settings
5024 (see
5025 .Xr stty 1 )
5026 and have their usual meaning (normal values are in parentheses): kill (^U),
5027 erase (^?), werase (^W), eof (^D), intr (^C), and quit (^\e).
5028 In addition to
5029 the above, the following characters are also treated specially in insert mode:
5030 .Bl -tag -width 10n
5031 .It ^E
5032 Command and file name enumeration (see below).
5033 .It ^F
5034 Command and file name completion (see below).
5035 If used twice in a row, the
5036 list of possible completions is displayed; if used a third time, the completion
5037 is undone.
5038 .It ^H
5039 Erases previous character.
5040 .It ^J | ^M
5041 End of line.
5042 The current line is read, parsed, and executed by the shell.
5043 .It ^V
5044 Literal next.
5045 The next character typed is not treated specially (can be used
5046 to insert the characters being described here).
5047 .It ^X
5048 Command and file name expansion (see below).
5049 .It Aq esc
5050 Puts the editor in command mode (see below).
5051 .It Aq tab
5052 Optional file name and command completion (see
5053 .Ic ^F
5054 above), enabled with
5055 .Ic set -o vi-tabcomplete .
5058 In command mode, each character is interpreted as a command.
5059 Characters that
5060 don't correspond to commands, are illegal combinations of commands, or are
5061 commands that can't be carried out, all cause beeps.
5062 In the following command descriptions, an
5063 .Op Ar n
5064 indicates the command may be prefixed by a number (e.g.\&
5065 .Ic 10l
5066 moves right 10 characters); if no number prefix is used,
5067 .Ar n
5068 is assumed to be 1 unless otherwise specified.
5069 The term
5070 .Dq current position
5071 refers to the position between the cursor and the character preceding the
5072 cursor.
5074 .Dq word
5075 is a sequence of letters, digits, and underscore characters or a sequence of
5076 non-letter, non-digit, non-underscore, and non-whitespace characters (e.g.\&
5077 .Dq ab2*&^
5078 contains two words) and a
5079 .Dq big-word
5080 is a sequence of non-whitespace characters.
5082 Special
5084 vi commands:
5086 The following commands are not in, or are different from, the normal vi file
5087 editor:
5088 .Bl -tag -width 10n
5089 .It Xo
5090 .Oo Ar n Oc Ns _
5092 Insert a space followed by the
5093 .Ar n Ns th
5094 big-word from the last command in the history at the current position and enter
5095 insert mode; if
5096 .Ar n
5097 is not specified, the last word is inserted.
5098 .It #
5099 Insert the comment character
5100 .Pq Sq #
5101 at the start of the current line and return the line to the shell (equivalent
5103 .Ic I#^J ) .
5104 .It Xo
5105 .Oo Ar n Oc Ns g
5107 Like
5108 .Ic G ,
5109 except if
5110 .Ar n
5111 is not specified, it goes to the most recent remembered line.
5112 .It Xo
5113 .Oo Ar n Oc Ns v
5115 Edit line
5116 .Ar n
5117 using the
5118 .Xr vi 1
5119 editor; if
5120 .Ar n
5121 is not specified, the current line is edited.
5122 The actual command executed is
5123 .Ic fc -e ${VISUAL:-${EDITOR:-vi}} Ar n .
5124 .It * and ^X
5125 Command or file name expansion is applied to the current big-word (with an
5126 appended
5127 .Ql *
5128 if the word contains no file globbing characters) \- the big-word is replaced
5129 with the resulting words.
5130 If the current big-word is the first on the line
5131 or follows one of the characters
5132 .Ql \&; ,
5133 .Ql | ,
5134 .Ql & ,
5135 .Ql \&( ,
5137 .Ql \&) ,
5138 and does not contain a slash
5139 .Pq Sq / ,
5140 then command expansion is done; otherwise file name expansion is done.
5141 Command expansion will match the big-word against all aliases, functions, and
5142 built-in commands as well as any executable files found by searching the
5143 directories in the
5144 .Ev PATH
5145 parameter.
5146 File name expansion matches the big-word against the files in the
5147 current directory.
5148 After expansion, the cursor is placed just past the last
5149 word and the editor is in insert mode.
5150 .It Xo
5151 .Oo Ar n Oc Ns \e ,
5152 .Oo Ar n Oc Ns ^F ,
5153 .Oo Ar n Oc Ns Aq tab ,
5154 .No and
5155 .Oo Ar n Oc Ns Aq esc
5157 Command/file name completion.
5158 Replace the current big-word with the
5159 longest unique match obtained after performing command and file name expansion.
5160 .Aq tab
5161 is only recognized if the
5162 .Ic vi-tabcomplete
5163 option is set, while
5164 .Aq esc
5165 is only recognized if the
5166 .Ic vi-esccomplete
5167 option is set (see
5168 .Ic set -o ) .
5170 .Ar n
5171 is specified, the
5172 .Ar n Ns th
5173 possible completion is selected (as reported by the command/file name
5174 enumeration command).
5175 .It = and ^E
5176 Command/file name enumeration.
5177 List all the commands or files that match the current big-word.
5178 .It @ Ns Ar c
5179 Macro expansion.
5180 Execute the commands found in the alias
5181 .No _ Ns Ar c .
5184 Intra-line movement commands:
5185 .Bl -tag -width Ds
5186 .It Xo
5187 .Oo Ar n Oc Ns h and
5188 .Oo Ar n Oc Ns ^H
5190 Move left
5191 .Ar n
5192 characters.
5193 .It Xo
5194 .Oo Ar n Oc Ns l and
5195 .Oo Ar n Oc Ns Aq space
5197 Move right
5198 .Ar n
5199 characters.
5200 .It 0
5201 Move to column 0.
5202 .It ^
5203 Move to the first non-whitespace character.
5204 .It Xo
5205 .Oo Ar n Oc Ns |
5207 Move to column
5208 .Ar n .
5209 .It $
5210 Move to the last character.
5211 .It Xo
5212 .Oo Ar n Oc Ns b
5214 Move back
5215 .Ar n
5216 words.
5217 .It Xo
5218 .Oo Ar n Oc Ns B
5220 Move back
5221 .Ar n
5222 big-words.
5223 .It Xo
5224 .Oo Ar n Oc Ns e
5226 Move forward to the end of the word,
5227 .Ar n
5228 times.
5229 .It Xo
5230 .Oo Ar n Oc Ns E
5232 Move forward to the end of the big-word,
5233 .Ar n
5234 times.
5235 .It Xo
5236 .Oo Ar n Oc Ns w
5238 Move forward
5239 .Ar n
5240 words.
5241 .It Xo
5242 .Oo Ar n Oc Ns W
5244 Move forward
5245 .Ar n
5246 big-words.
5247 .It %
5248 Find match.
5249 The editor looks forward for the nearest parenthesis, bracket, or
5250 brace and then moves the cursor to the matching parenthesis, bracket, or brace.
5251 .It Xo
5252 .Oo Ar n Oc Ns f Ns Ar c
5254 Move forward to the
5255 .Ar n Ns th
5256 occurrence of the character
5257 .Ar c .
5258 .It Xo
5259 .Oo Ar n Oc Ns F Ns Ar c
5261 Move backward to the
5262 .Ar n Ns th
5263 occurrence of the character
5264 .Ar c .
5265 .It Xo
5266 .Oo Ar n Oc Ns t Ns Ar c
5268 Move forward to just before the
5269 .Ar n Ns th
5270 occurrence of the character
5271 .Ar c .
5272 .It Xo
5273 .Oo Ar n Oc Ns T Ns Ar c
5275 Move backward to just before the
5276 .Ar n Ns th
5277 occurrence of the character
5278 .Ar c .
5279 .It Xo
5280 .Oo Ar n Oc Ns \&;
5282 Repeats the last
5283 .Ic f , F , t ,
5285 .Ic T
5286 command.
5287 .It Xo
5288 .Oo Ar n Oc Ns \&,
5290 Repeats the last
5291 .Ic f , F , t ,
5293 .Ic T
5294 command, but moves in the opposite direction.
5297 Inter-line movement commands:
5298 .Bl -tag -width Ds
5299 .It Xo
5300 .Oo Ar n Oc Ns j ,
5301 .Oo Ar n Oc Ns + ,
5302 .No and
5303 .Oo Ar n Oc Ns ^N
5305 Move to the
5306 .Ar n Ns th
5307 next line in the history.
5308 .It Xo
5309 .Oo Ar n Oc Ns k ,
5310 .Oo Ar n Oc Ns - ,
5311 .No and
5312 .Oo Ar n Oc Ns ^P
5314 Move to the
5315 .Ar n Ns th
5316 previous line in the history.
5317 .It Xo
5318 .Oo Ar n Oc Ns G
5320 Move to line
5321 .Ar n
5322 in the history; if
5323 .Ar n
5324 is not specified, the number of the first remembered line is used.
5325 .It Xo
5326 .Oo Ar n Oc Ns g
5328 Like
5329 .Ic G ,
5330 except if
5331 .Ar n
5332 is not specified, it goes to the most recent remembered line.
5333 .It Xo
5334 .Oo Ar n Oc Ns / Ns Ar string
5336 Search backward through the history for the
5337 .Ar n Ns th
5338 line containing
5339 .Ar string ;
5341 .Ar string
5342 starts with
5343 .Ql ^ ,
5344 the remainder of the string must appear at the start of the history line for
5345 it to match.
5346 .It Xo
5347 .Oo Ar n Oc Ns \&? Ns Ar string
5349 Same as
5350 .Ic / ,
5351 except it searches forward through the history.
5352 .It Xo
5353 .Oo Ar n Oc Ns n
5355 Search for the
5356 .Ar n Ns th
5357 occurrence of the last search string;
5358 the direction of the search is the same as the last search.
5359 .It Xo
5360 .Oo Ar n Oc Ns N
5362 Search for the
5363 .Ar n Ns th
5364 occurrence of the last search string;
5365 the direction of the search is the opposite of the last search.
5368 Edit commands
5369 .Bl -tag -width Ds
5370 .It Xo
5371 .Oo Ar n Oc Ns a
5373 Append text
5374 .Ar n
5375 times; goes into insert mode just after the current position.
5376 The append is
5377 only replicated if command mode is re-entered i.e.\&
5378 .Aq esc
5379 is used.
5380 .It Xo
5381 .Oo Ar n Oc Ns A
5383 Same as
5384 .Ic a ,
5385 except it appends at the end of the line.
5386 .It Xo
5387 .Oo Ar n Oc Ns i
5389 Insert text
5390 .Ar n
5391 times; goes into insert mode at the current position.
5392 The insertion is only
5393 replicated if command mode is re-entered i.e.\&
5394 .Aq esc
5395 is used.
5396 .It Xo
5397 .Oo Ar n Oc Ns I
5399 Same as
5400 .Ic i ,
5401 except the insertion is done just before the first non-blank character.
5402 .It Xo
5403 .Oo Ar n Oc Ns s
5405 Substitute the next
5406 .Ar n
5407 characters (i.e. delete the characters and go into insert mode).
5408 .It S
5409 Substitute whole line.
5410 All characters from the first non-blank character to the
5411 end of the line are deleted and insert mode is entered.
5412 .It Xo
5413 .Oo Ar n Oc Ns c Ns Ar move-cmd
5415 Change from the current position to the position resulting from
5416 .Ar n move-cmd Ns s
5417 (i.e. delete the indicated region and go into insert mode); if
5418 .Ar move-cmd
5420 .Ic c ,
5421 the line starting from the first non-blank character is changed.
5422 .It C
5423 Change from the current position to the end of the line (i.e. delete to the
5424 end of the line and go into insert mode).
5425 .It Xo
5426 .Oo Ar n Oc Ns x
5428 Delete the next
5429 .Ar n
5430 characters.
5431 .It Xo
5432 .Oo Ar n Oc Ns X
5434 Delete the previous
5435 .Ar n
5436 characters.
5437 .It D
5438 Delete to the end of the line.
5439 .It Xo
5440 .Oo Ar n Oc Ns d Ns Ar move-cmd
5442 Delete from the current position to the position resulting from
5443 .Ar n move-cmd Ns s ;
5444 .Ar move-cmd
5445 is a movement command (see above) or
5446 .Ic d ,
5447 in which case the current line is deleted.
5448 .It Xo
5449 .Oo Ar n Oc Ns r Ns Ar c
5451 Replace the next
5452 .Ar n
5453 characters with the character
5454 .Ar c .
5455 .It Xo
5456 .Oo Ar n Oc Ns R
5458 Replace.
5459 Enter insert mode but overwrite existing characters instead of
5460 inserting before existing characters.
5461 The replacement is repeated
5462 .Ar n
5463 times.
5464 .It Xo
5465 .Oo Ar n Oc Ns ~
5467 Change the case of the next
5468 .Ar n
5469 characters.
5470 .It Xo
5471 .Oo Ar n Oc Ns y Ns Ar move-cmd
5473 Yank from the current position to the position resulting from
5474 .Ar n move-cmd Ns s
5475 into the yank buffer; if
5476 .Ar move-cmd
5478 .Ic y ,
5479 the whole line is yanked.
5480 .It Y
5481 Yank from the current position to the end of the line.
5482 .It Xo
5483 .Oo Ar n Oc Ns p
5485 Paste the contents of the yank buffer just after the current position,
5486 .Ar n
5487 times.
5488 .It Xo
5489 .Oo Ar n Oc Ns P
5491 Same as
5492 .Ic p ,
5493 except the buffer is pasted at the current position.
5496 Miscellaneous vi commands
5497 .Bl -tag -width Ds
5498 .It ^J and ^M
5499 The current line is read, parsed, and executed by the shell.
5500 .It ^L and ^R
5501 Redraw the current line.
5502 .It Xo
5503 .Oo Ar n Oc Ns \&.
5505 Redo the last edit command
5506 .Ar n
5507 times.
5508 .It u
5509 Undo the last edit command.
5510 .It U
5511 Undo all changes that have been made to the current line.
5512 .It Ar intr No and Ar quit
5513 The interrupt and quit terminal characters cause the current line to be
5514 deleted and a new prompt to be printed.
5516 .Sh FILES
5517 .Bl -tag -width "/etc/suid_profileXX" -compact
5518 .It Pa ~/.profile
5519 User's login profile.
5520 .It Pa /etc/ksh.kshrc
5521 Global configuration file.
5522 Not sourced by default.
5523 .It Pa /etc/profile
5524 System login profile.
5525 .It Pa /etc/shells
5526 Shell database.
5527 .It Pa /etc/suid_profile
5528 Privileged shell profile.
5530 .Sh SEE ALSO
5531 .Xr csh 1 ,
5532 .Xr ed 1 ,
5533 .Xr mg 1 ,
5534 .Xr sh 1 ,
5535 .Xr stty 1 ,
5536 .Xr vi 1 ,
5537 .Xr shells 5 ,
5538 .Xr environ 7 ,
5539 .Xr script 7
5541 .%A Morris Bolsky
5542 .%A David Korn
5543 .%B The KornShell Command and Programming Language, 2nd Edition
5544 .%D 1995
5545 .%I Prentice Hall
5546 .%O ISBN 0131827006
5549 .%A Stephen G. Kochan
5550 .%A Patrick H. Wood
5551 .%B UNIX Shell Programming, 3rd Edition
5552 .%D 2003
5553 .%I Sams
5554 .%O ISBN 0672324903
5557 .%A IEEE Inc.
5558 .%D 1993
5559 .%O ISBN 1-55937-266-9
5560 .%T IEEE Standard for Information Technology \- Portable Operating \
5561     System Interface (POSIX) \- Part 2: Shell and Utilities
5563 .Sh VERSION
5564 This page documents version @(#)PD KSH v5.2.14 99/07/13.2 of the public
5565 domain Korn shell.
5566 .Sh AUTHORS
5567 .An -nosplit
5568 This shell is based on the public domain 7th edition Bourne shell clone by
5569 .An Charles Forsyth
5570 and parts of the BRL shell by
5571 .An Doug A. Gwyn ,
5572 .An Doug Kingston ,
5573 .An Ron Natalie ,
5574 .An Arnold Robbins ,
5575 .An Lou Salkind ,
5576 and others.
5577 The first release of
5578 .Nm pdksh
5579 was created by
5580 .An Eric Gisin ,
5581 and it was subsequently maintained by
5582 .An John R. MacMillan Aq Mt change!john@sq.sq.com ,
5583 .An Simon J. Gerraty Aq Mt sjg@zen.void.oz.au ,
5585 .An Michael Rendell Aq Mt michael@cs.mun.ca .
5587 .Pa CONTRIBUTORS
5588 file in the source distribution contains a more complete list of people and
5589 their part in the shell's development.
5590 .Sh BUGS
5591 .Pf $( Ar command )
5592 expressions are currently parsed by finding the closest matching (unquoted)
5593 parenthesis.
5594 Thus constructs inside
5595 .Pf $( Ar command )
5596 may produce an error.
5597 For example, the parenthesis in
5598 .Ql x);;
5599 is interpreted as the closing parenthesis in
5600 .Ql $(case x in x);; *);; esac) .