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