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