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