xref: /openbsd-src/bin/ksh/ksh.1 (revision e9d1bb1d46b9ea48990151f6d2f59af3a434cc77)
1.\"	$OpenBSD: ksh.1,v 1.221 2024/09/25 06:13:01 jmc Exp $
2.\"
3.\"	Public Domain
4.\"
5.Dd $Mdocdate: September 25 2024 $
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.Ar prefix No { Ar str1 No ,...,
1804.Ar strN No } Ar suffix
1805.Sm on
1806.Ed
1807.Pp
1808The expressions are expanded to
1809.Ar N
1810words, each of which is the concatenation of
1811.Ar prefix ,
1812.Ar str Ns i ,
1813and
1814.Ar suffix
1815(e.g.\&
1816.Dq a{c,b{X,Y},d}e
1817expands to four words:
1818.Dq ace ,
1819.Dq abXe ,
1820.Dq abYe ,
1821and
1822.Dq ade ) .
1823As noted in the example, brace expressions can be nested and the resulting
1824words are not sorted.
1825Brace expressions must contain an unquoted comma
1826.Pq Sq \&,
1827for expansion to occur (e.g.\&
1828.Ic {}
1829and
1830.Ic {foo}
1831are not expanded).
1832Brace expansion is carried out after parameter substitution
1833and before file name generation.
1834.Ss File name patterns
1835A file name pattern is a word containing one or more unquoted
1836.Ql \&? ,
1837.Ql * ,
1838.Ql + ,
1839.Ql @ ,
1840or
1841.Ql \&!
1842characters or
1843.Dq [..]
1844sequences.
1845Once brace expansion has been performed, the shell replaces file
1846name patterns with the sorted names of all the files that match the pattern
1847(if no files match, the word is left unchanged).
1848The pattern elements have the following meaning:
1849.Bl -tag -width Ds
1850.It \&?
1851Matches any single character.
1852.It \&*
1853Matches any sequence of characters.
1854.It [..]
1855Matches any of the characters inside the brackets.
1856Ranges of characters can be
1857specified by separating two characters by a
1858.Ql -
1859(e.g.\&
1860.Dq [a0-9]
1861matches the letter
1862.Sq a
1863or any digit).
1864In order to represent itself, a
1865.Ql -
1866must either be quoted or the first or last character in the character list.
1867Similarly, a
1868.Ql \&]
1869must be quoted or the first character in the list if it is to represent itself
1870instead of the end of the list.
1871Also, a
1872.Ql \&!
1873appearing at the start of the list has special meaning (see below), so to
1874represent itself it must be quoted or appear later in the list.
1875.Pp
1876Within a bracket expression, the name of a
1877.Em character class
1878enclosed in
1879.Sq [:
1880and
1881.Sq :]
1882stands for the list of all characters belonging to that class.
1883Supported character classes:
1884.Bd -literal -offset indent
1885alnum	cntrl	lower	space
1886alpha	digit	print	upper
1887blank	graph	punct	xdigit
1888.Ed
1889.Pp
1890These match characters using the macros specified in
1891.Xr isalnum 3 ,
1892.Xr isalpha 3 ,
1893and so on.
1894A character class may not be used as an endpoint of a range.
1895.It [!..]
1896Like [..],
1897except it matches any character not inside the brackets.
1898.Sm off
1899.It *( Ar pattern Ns | No ...| Ar pattern )
1900.Sm on
1901Matches any string of characters that matches zero or more occurrences of the
1902specified patterns.
1903Example: The pattern
1904.Ic *(foo|bar)
1905matches the strings
1906.Dq ,
1907.Dq foo ,
1908.Dq bar ,
1909.Dq foobarfoo ,
1910etc.
1911.Sm off
1912.It +( Ar pattern Ns | No ...| Ar pattern )
1913.Sm on
1914Matches any string of characters that matches one or more occurrences of the
1915specified patterns.
1916Example: The pattern
1917.Ic +(foo|bar)
1918matches the strings
1919.Dq foo ,
1920.Dq bar ,
1921.Dq foobar ,
1922etc.
1923.Sm off
1924.It ?( Ar pattern Ns | No ...| Ar pattern )
1925.Sm on
1926Matches the empty string or a string that matches one of the specified
1927patterns.
1928Example: The pattern
1929.Ic ?(foo|bar)
1930only matches the strings
1931.Dq ,
1932.Dq foo ,
1933and
1934.Dq bar .
1935.Sm off
1936.It @( Ar pattern Ns | No ...| Ar pattern )
1937.Sm on
1938Matches a string that matches one of the specified patterns.
1939Example: The pattern
1940.Ic @(foo|bar)
1941only matches the strings
1942.Dq foo
1943and
1944.Dq bar .
1945.Sm off
1946.It !( Ar pattern Ns | No ...| Ar pattern )
1947.Sm on
1948Matches any string that does not match one of the specified patterns.
1949Examples: The pattern
1950.Ic !(foo|bar)
1951matches all strings except
1952.Dq foo
1953and
1954.Dq bar ;
1955the pattern
1956.Ic !(*)
1957matches no strings; the pattern
1958.Ic !(?)*\&
1959matches all strings (think about it).
1960.El
1961.Pp
1962Unlike most shells,
1963.Nm ksh
1964never matches
1965.Sq \&.
1966and
1967.Sq .. .
1968.Pp
1969Note that none of the above pattern elements match either a period
1970.Pq Sq \&.
1971at the start of a file name or a slash
1972.Pq Sq / ,
1973even if they are explicitly used in a [..] sequence; also, the names
1974.Sq \&.
1975and
1976.Sq ..
1977are never matched, even by the pattern
1978.Sq .* .
1979.Pp
1980If the
1981.Ic markdirs
1982option is set, any directories that result from file name generation are marked
1983with a trailing
1984.Ql / .
1985.Ss Input/output redirection
1986When a command is executed, its standard input, standard output, and standard
1987error (file descriptors 0, 1, and 2, respectively) are normally inherited from
1988the shell.
1989Three exceptions to this are commands in pipelines, for which
1990standard input and/or standard output are those set up by the pipeline,
1991asynchronous commands created when job control is disabled, for which standard
1992input is initially set to be from
1993.Pa /dev/null ,
1994and commands for which any of the following redirections have been specified:
1995.Bl -tag -width Ds
1996.It Cm > Ar file
1997Standard output is redirected to
1998.Ar file .
1999If
2000.Ar file
2001does not exist, it is created; if it does exist, is a regular file, and the
2002.Ic noclobber
2003option is set, an error occurs; otherwise, the file is truncated.
2004Note that this means the command
2005.Ic cmd < foo > foo
2006will open
2007.Ar foo
2008for reading and then truncate it when it opens it for writing, before
2009.Ar cmd
2010gets a chance to actually read
2011.Ar foo .
2012.It Cm >| Ar file
2013Same as
2014.Cm > ,
2015except the file is truncated, even if the
2016.Ic noclobber
2017option is set.
2018.It Cm >> Ar file
2019Same as
2020.Cm > ,
2021except if
2022.Ar file
2023exists it is appended to instead of being truncated.
2024Also, the file is opened
2025in append mode, so writes always go to the end of the file (see
2026.Xr open 2 ) .
2027.It Cm < Ar file
2028Standard input is redirected from
2029.Ar file ,
2030which is opened for reading.
2031.It Cm <> Ar file
2032Same as
2033.Cm < ,
2034except the file is opened for reading and writing.
2035.It Cm << Ar marker
2036After reading the command line containing this kind of redirection (called a
2037.Dq here document ) ,
2038the shell copies lines from the command source into a temporary file until a
2039line matching
2040.Ar marker
2041is read.
2042When the command is executed, standard input is redirected from the
2043temporary file.
2044If
2045.Ar marker
2046contains no quoted characters, the contents of the temporary file are processed
2047as if enclosed in double quotes each time the command is executed, so
2048parameter, command, and arithmetic substitutions are performed, along with
2049backslash
2050.Pq Sq \e
2051escapes for
2052.Ql $ ,
2053.Ql ` ,
2054.Ql \e ,
2055and
2056.Ql \enewline .
2057If multiple here documents are used on the same command line, they are saved in
2058order.
2059.It Cm <<- Ar marker
2060Same as
2061.Cm << ,
2062except leading tabs are stripped from lines in the here document.
2063.It Cm <& Ar fd
2064Standard input is duplicated from file descriptor
2065.Ar fd .
2066.Ar fd
2067can be a single digit, indicating the number of an existing file descriptor;
2068the letter
2069.Ql p ,
2070indicating the file descriptor associated with the output of the current
2071co-process; or the character
2072.Ql - ,
2073indicating standard input is to be closed.
2074.It Cm >& Ar fd
2075Same as
2076.Cm <& ,
2077except the operation is done on standard output.
2078.El
2079.Pp
2080In any of the above redirections, the file descriptor that is redirected
2081(i.e. standard input or standard output)
2082can be explicitly given by preceding the
2083redirection with a single digit.
2084Parameter, command, and arithmetic
2085substitutions, tilde substitutions, and (if the shell is interactive)
2086file name generation are all performed on the
2087.Ar file ,
2088.Ar marker ,
2089and
2090.Ar fd
2091arguments of redirections.
2092Note, however, that the results of any file name
2093generation are only used if a single file is matched; if multiple files match,
2094the word with the expanded file name generation characters is used.
2095Note
2096that in restricted shells, redirections which can create files cannot be used.
2097.Pp
2098For simple-commands, redirections may appear anywhere in the command; for
2099compound-commands
2100.Po
2101.Ic if
2102statements, etc.
2103.Pc ,
2104any redirections must appear at the end.
2105Redirections are processed after
2106pipelines are created and in the order they are given, so the following
2107will print an error with a line number prepended to it:
2108.Pp
2109.D1 $ cat /foo/bar 2>&1 > /dev/null | cat -n
2110.Ss Arithmetic expressions
2111Integer arithmetic expressions can be used with the
2112.Ic let
2113command, inside $((..)) expressions, inside array references (e.g.\&
2114.Ar name Ns Bq Ar expr ) ,
2115as numeric arguments to the
2116.Ic test
2117command, and as the value of an assignment to an integer parameter.
2118.Pp
2119Expressions may contain alpha-numeric parameter identifiers, array references,
2120and integer constants and may be combined with the following C operators
2121(listed and grouped in increasing order of precedence):
2122.Pp
2123Unary operators:
2124.Bd -literal -offset indent
2125+ - ! ~ ++ --
2126.Ed
2127.Pp
2128Binary operators:
2129.Bd -literal -offset indent
2130,
2131= *= /= %= += -= <<= >>= &= ^= |=
2132||
2133&&
2134|
2135^
2136&
2137== !=
2138< <= >= >
2139<< >>
2140+ -
2141* / %
2142.Ed
2143.Pp
2144Ternary operators:
2145.Bd -literal -offset indent
2146?: (precedence is immediately higher than assignment)
2147.Ed
2148.Pp
2149Grouping operators:
2150.Bd -literal -offset indent
2151( )
2152.Ed
2153.Pp
2154A parameter that is NULL or unset evaluates to 0.
2155Integer constants may be specified with arbitrary bases using the notation
2156.Ar base Ns # Ns Ar number ,
2157where
2158.Ar base
2159is a decimal integer specifying the base, and
2160.Ar number
2161is a number in the specified base.
2162Additionally,
2163integers may be prefixed with
2164.Sq 0X
2165or
2166.Sq 0x
2167(specifying base 16)
2168or
2169.Sq 0
2170(base 8)
2171in all forms of arithmetic expressions,
2172except as numeric arguments to the
2173.Ic test
2174command.
2175.Pp
2176The operators are evaluated as follows:
2177.Bl -tag -width Ds -offset indent
2178.It unary +
2179Result is the argument (included for completeness).
2180.It unary -
2181Negation.
2182.It \&!
2183Logical NOT;
2184the result is 1 if argument is zero, 0 if not.
2185.It ~
2186Arithmetic (bit-wise) NOT.
2187.It ++
2188Increment; must be applied to a parameter (not a literal or other expression).
2189The parameter is incremented by 1.
2190When used as a prefix operator, the result
2191is the incremented value of the parameter; when used as a postfix operator, the
2192result is the original value of the parameter.
2193.It --
2194Similar to
2195.Ic ++ ,
2196except the parameter is decremented by 1.
2197.It \&,
2198Separates two arithmetic expressions; the left-hand side is evaluated first,
2199then the right.
2200The result is the value of the expression on the right-hand side.
2201.It =
2202Assignment; the variable on the left is set to the value on the right.
2203.It Xo
2204.No *= /= += -= <<=
2205.No >>= &= ^= |=
2206.Xc
2207Assignment operators.
2208.Sm off
2209.Ao Ar var Ac Xo
2210.Aq Ar op
2211.No = Aq Ar expr
2212.Xc
2213.Sm on
2214is the same as
2215.Sm off
2216.Ao Ar var Ac Xo
2217.No = Aq Ar var
2218.Aq Ar op
2219.Aq Ar expr ,
2220.Xc
2221.Sm on
2222with any operator precedence in
2223.Aq Ar expr
2224preserved.
2225For example,
2226.Dq var1 *= 5 + 3
2227is the same as specifying
2228.Dq var1 = var1 * (5 + 3) .
2229.It ||
2230Logical OR;
2231the result is 1 if either argument is non-zero, 0 if not.
2232The right argument is evaluated only if the left argument is zero.
2233.It &&
2234Logical AND;
2235the result is 1 if both arguments are non-zero, 0 if not.
2236The right argument is evaluated only if the left argument is non-zero.
2237.It |
2238Arithmetic (bit-wise) OR.
2239.It ^
2240Arithmetic (bit-wise) XOR
2241(exclusive-OR).
2242.It &
2243Arithmetic (bit-wise) AND.
2244.It ==
2245Equal; the result is 1 if both arguments are equal, 0 if not.
2246.It !=
2247Not equal; the result is 0 if both arguments are equal, 1 if not.
2248.It <
2249Less than; the result is 1 if the left argument is less than the right, 0 if
2250not.
2251.It <= >= >
2252Less than or equal, greater than or equal, greater than.
2253See
2254.Ic < .
2255.It << >>
2256Shift left (right); the result is the left argument with its bits shifted left
2257(right) by the amount given in the right argument.
2258.It + - * /
2259Addition, subtraction, multiplication, and division.
2260.It %
2261Remainder; the result is the remainder of the division of the left argument by
2262the right.
2263The sign of the result is unspecified if either argument is negative.
2264.It Xo
2265.Sm off
2266.Aq Ar arg1 ?
2267.Aq Ar arg2 :
2268.Aq Ar arg3
2269.Sm on
2270.Xc
2271If
2272.Aq Ar arg1
2273is non-zero, the result is
2274.Aq Ar arg2 ;
2275otherwise the result is
2276.Aq Ar arg3 .
2277.El
2278.Ss Co-processes
2279A co-process, which is a pipeline created with the
2280.Sq |&
2281operator, is an asynchronous process that the shell can both write to (using
2282.Ic print -p )
2283and read from (using
2284.Ic read -p ) .
2285The input and output of the co-process can also be manipulated using
2286.Cm >&p
2287and
2288.Cm <&p
2289redirections, respectively.
2290Once a co-process has been started, another can't
2291be started until the co-process exits, or until the co-process's input has been
2292redirected using an
2293.Ic exec Ar n Ns Cm >&p
2294redirection.
2295If a co-process's input is redirected in this way, the next
2296co-process to be started will share the output with the first co-process,
2297unless the output of the initial co-process has been redirected using an
2298.Ic exec Ar n Ns Cm <&p
2299redirection.
2300.Pp
2301Some notes concerning co-processes:
2302.Bl -bullet
2303.It
2304The only way to close the co-process's input (so the co-process reads an
2305end-of-file) is to redirect the input to a numbered file descriptor and then
2306close that file descriptor e.g.\&
2307.Ic exec 3>&p; exec 3>&- .
2308.It
2309In order for co-processes to share a common output, the shell must keep the
2310write portion of the output pipe open.
2311This means that end-of-file will not be
2312detected until all co-processes sharing the co-process's output have exited
2313(when they all exit, the shell closes its copy of the pipe).
2314This can be
2315avoided by redirecting the output to a numbered file descriptor (as this also
2316causes the shell to close its copy).
2317Note that this behaviour is slightly
2318different from the original Korn shell which closes its copy of the write
2319portion of the co-process output when the most recently started co-process
2320(instead of when all sharing co-processes) exits.
2321.It
2322.Ic print -p
2323will ignore
2324.Dv SIGPIPE
2325signals during writes if the signal is not being trapped or ignored; the same
2326is true if the co-process input has been duplicated to another file descriptor
2327and
2328.Ic print -u Ns Ar n
2329is used.
2330.El
2331.Ss Functions
2332Functions are defined using either Korn shell
2333.Ic function Ar function-name
2334syntax or the Bourne/POSIX shell
2335.Ar function-name Ns ()
2336syntax (see below for the difference between the two forms).
2337Functions are like
2338.Ic \&. Ns -scripts
2339(i.e. scripts sourced using the
2340.Sq Ic \&.
2341built-in command)
2342in that they are executed in the current environment.
2343However, unlike
2344.Ic \&. Ns -scripts,
2345shell arguments (i.e. positional parameters $1, $2, etc.)\&
2346are never visible inside them.
2347When the shell is determining the location of a command, functions
2348are searched after special built-in commands, before regular and
2349non-regular built-ins, and before the
2350.Ev PATH
2351is searched.
2352.Pp
2353An existing function may be deleted using
2354.Ic unset Fl f Ar function-name .
2355A list of functions can be obtained using
2356.Ic typeset +f
2357and the function definitions can be listed using
2358.Ic typeset -f .
2359The
2360.Ic autoload
2361command (which is an alias for
2362.Ic typeset -fu )
2363may be used to create undefined functions: when an undefined function is
2364executed, the shell searches the path specified in the
2365.Ev FPATH
2366parameter for a file with the same name as the function, which, if found, is
2367read and executed.
2368If after executing the file the named function is found to
2369be defined, the function is executed; otherwise, the normal command search is
2370continued (i.e. the shell searches the regular built-in command table and
2371.Ev PATH ) .
2372Note that if a command is not found using
2373.Ev PATH ,
2374an attempt is made to autoload a function using
2375.Ev FPATH
2376(this is an undocumented feature of the original Korn shell).
2377.Pp
2378Functions can have two attributes,
2379.Dq trace
2380and
2381.Dq export ,
2382which can be set with
2383.Ic typeset -ft
2384and
2385.Ic typeset -fx ,
2386respectively.
2387When a traced function is executed, the shell's
2388.Ic xtrace
2389option is turned on for the function's duration; otherwise, the
2390.Ic xtrace
2391option is turned off.
2392The
2393.Dq export
2394attribute of functions is currently not used.
2395In the original Korn shell,
2396exported functions are visible to shell scripts that are executed.
2397.Pp
2398Since functions are executed in the current shell environment, parameter
2399assignments made inside functions are visible after the function completes.
2400If this is not the desired effect, the
2401.Ic typeset
2402command can be used inside a function to create a local parameter.
2403Note that special parameters (e.g.\&
2404.Ic \&$$ , $! )
2405can't be scoped in this way.
2406.Pp
2407The exit status of a function is that of the last command executed in the
2408function.
2409A function can be made to finish immediately using the
2410.Ic return
2411command; this may also be used to explicitly specify the exit status.
2412.Pp
2413Functions defined with the
2414.Ic function
2415reserved word are treated differently in the following ways from functions
2416defined with the
2417.Ic ()
2418notation:
2419.Bl -bullet
2420.It
2421The $0 parameter is set to the name of the function
2422(Bourne-style functions leave $0 untouched).
2423.It
2424Parameter assignments preceding function calls are not kept in the shell
2425environment (executing Bourne-style functions will keep assignments).
2426.It
2427.Ev OPTIND
2428is saved/reset and restored on entry and exit from the function so
2429.Ic getopts
2430can be used properly both inside and outside the function (Bourne-style
2431functions leave
2432.Ev OPTIND
2433untouched, so using
2434.Ic getopts
2435inside a function interferes with using
2436.Ic getopts
2437outside the function).
2438.El
2439.Ss POSIX mode
2440The shell is intended to be POSIX compliant;
2441however, in some cases, POSIX behaviour is contrary either to
2442the original Korn shell behaviour or to user convenience.
2443How the shell behaves in these cases is determined by the state of the
2444.Ic posix
2445option
2446.Pq Ic set -o posix .
2447If it is on, the POSIX behaviour is followed; otherwise, it is not.
2448The
2449.Ic posix
2450option is set automatically when the shell starts up if the environment
2451contains the
2452.Ev POSIXLY_CORRECT
2453parameter.
2454The shell can also be compiled so that it is in POSIX mode by default;
2455however, this is usually not desirable.
2456.Pp
2457The following is a list of things that are affected by the state of the
2458.Ic posix
2459option:
2460.Bl -bullet
2461.It
2462.Ic kill -l
2463output.
2464In POSIX mode, only signal names are listed (in a single line);
2465in non-POSIX mode,
2466signal numbers, names, and descriptions are printed (in columns).
2467.It
2468.Ic echo
2469options.
2470In POSIX mode,
2471.Fl e
2472and
2473.Fl E
2474are not treated as options, but printed like other arguments;
2475in non-POSIX mode, these options control the interpretation
2476of backslash sequences.
2477.It
2478.Ic fg
2479exit status.
2480In POSIX mode, the exit status is 0 if no errors occur;
2481in non-POSIX mode, the exit status is that of the last foregrounded job.
2482.It
2483.Ic eval
2484exit status.
2485If
2486.Ic eval
2487gets to see an empty command (i.e.\&
2488.Ic eval `false` ) ,
2489its exit status in POSIX mode will be 0.
2490In non-POSIX mode,
2491it will be the exit status of the last command substitution that was
2492done in the processing of the arguments to
2493.Ic eval
2494(or 0 if there were no command substitutions).
2495.It
2496.Ic getopts .
2497In POSIX mode, options must start with a
2498.Ql - ;
2499in non-POSIX mode, options can start with either
2500.Ql -
2501or
2502.Ql + .
2503.It
2504Brace expansion (also known as alternation).
2505In POSIX mode, brace expansion is disabled;
2506in non-POSIX mode, brace expansion is enabled.
2507Note that
2508.Ic set -o posix
2509(or setting the
2510.Ev POSIXLY_CORRECT
2511parameter) automatically turns the
2512.Ic braceexpand
2513option off; however, it can be explicitly turned on later.
2514.It
2515.Ic set - .
2516In POSIX mode, this does not clear the
2517.Ic verbose
2518or
2519.Ic xtrace
2520options; in non-POSIX mode, it does.
2521.It
2522.Ic set
2523exit status.
2524In POSIX mode, the exit status of
2525.Ic set
2526is 0 if there are no errors;
2527in non-POSIX mode, the exit status is that of any
2528command substitutions performed in generating the
2529.Ic set
2530command.
2531For example,
2532.Ic set -- `false`; echo $?\&
2533prints 0 in POSIX mode, 1 in non-POSIX mode.
2534This construct is used in most shell scripts that use the old
2535.Xr getopt 1
2536command.
2537.It
2538Argument expansion of the
2539.Ic alias ,
2540.Ic export ,
2541.Ic readonly ,
2542and
2543.Ic typeset
2544commands.
2545In POSIX mode, normal argument expansion is done; in non-POSIX mode,
2546field splitting, file globbing, brace expansion, and (normal) tilde expansion
2547are turned off, while assignment tilde expansion is turned on.
2548.It
2549Signal specification.
2550In POSIX mode, signals can be specified as digits, only
2551if signal numbers match POSIX values
2552(i.e. HUP=1, INT=2, QUIT=3, ABRT=6, KILL=9, ALRM=14, and TERM=15);
2553in non-POSIX mode, signals can always be digits.
2554.It
2555Alias expansion.
2556In POSIX mode, alias expansion is only carried out when reading command words;
2557in non-POSIX mode, alias expansion is carried out on any
2558word following an alias that ended in a space.
2559For example, the following
2560.Ic for
2561loop uses parameter
2562.Sq i
2563in POSIX mode and
2564.Sq j
2565in non-POSIX mode:
2566.Bd -literal -offset indent
2567alias a='for ' i='j'
2568a i in 1 2; do echo i=$i j=$j; done
2569.Ed
2570.El
2571.Ss Strict Bourne shell mode
2572When the
2573.Ic sh
2574option is enabled (see the
2575.Ic set
2576command),
2577.Nm
2578will behave like
2579.Xr sh 1
2580in the following ways:
2581.Bl -bullet
2582.It
2583The parameter
2584.Ic $_
2585is not set to:
2586.Pp
2587.Bl -dash -compact
2588.It
2589the expanded alias' full program path after entering commands
2590that are tracked aliases
2591.It
2592the last argument on the command line after entering external
2593commands
2594.It
2595the file that changed when
2596.Ev MAILPATH
2597is set to monitor a mailbox
2598.El
2599.It
2600File descriptors are left untouched when executing
2601.Ic exec
2602with no arguments.
2603.It
2604Backslash-escaped special characters are not substituted in
2605.Ev PS1 .
2606.It
2607Sequences of
2608.Sq ((...))
2609are not interpreted as arithmetic expressions.
2610.El
2611.Ss Command execution
2612After evaluation of command-line arguments, redirections, and parameter
2613assignments, the type of command is determined: a special built-in, a
2614function, a regular built-in, or the name of a file to execute found using the
2615.Ev PATH
2616parameter.
2617The checks are made in the above order.
2618Special built-in commands differ from other commands in that the
2619.Ev PATH
2620parameter is not used to find them, an error during their execution can
2621cause a non-interactive shell to exit, and parameter assignments that are
2622specified before the command are kept after the command completes.
2623Just to confuse things, if the
2624.Ic posix
2625option is turned off (see the
2626.Ic set
2627command below), some special commands are very special in that no field
2628splitting, file globbing, brace expansion, nor tilde expansion is performed
2629on arguments that look like assignments.
2630Regular built-in commands are different only in that the
2631.Ev PATH
2632parameter is not used to find them.
2633.Pp
2634The original
2635.Nm ksh
2636and POSIX differ somewhat in which commands are considered
2637special or regular:
2638.Pp
2639POSIX special commands
2640.Pp
2641.Ic \&. , \&: , break , continue ,
2642.Ic eval , exec , exit , export ,
2643.Ic readonly , return , set , shift ,
2644.Ic times , trap , unset
2645.Pp
2646Additional
2647.Nm
2648special commands
2649.Pp
2650.Ic builtin , typeset
2651.Pp
2652Very special commands
2653.Pq when POSIX mode is off
2654.Pp
2655.Ic alias , readonly , set , typeset
2656.Pp
2657POSIX regular commands
2658.Pp
2659.Ic alias , bg , cd , command ,
2660.Ic false , fc , fg , getopts ,
2661.Ic jobs , kill , pwd , read ,
2662.Ic true , umask , unalias , wait
2663.Pp
2664Additional
2665.Nm
2666regular commands
2667.Pp
2668.Ic \&[ , echo , let ,
2669.Ic print , suspend , test ,
2670.Ic ulimit , whence
2671.Pp
2672Once the type of command has been determined, any command-line parameter
2673assignments are performed and exported for the duration of the command.
2674.Pp
2675The following describes the special and regular built-in commands:
2676.Pp
2677.Bl -tag -width Ds -compact
2678.It Ic \&. Ar file Op Ar arg ...
2679Execute the commands in
2680.Ar file
2681in the current environment.
2682The file is searched for in the directories of
2683.Ev PATH .
2684If arguments are given, the positional parameters may be used to access them
2685while
2686.Ar file
2687is being executed.
2688If no arguments are given, the positional parameters are
2689those of the environment the command is used in.
2690.Pp
2691.It Ic \&: Op Ar ...
2692The null command.
2693Exit status is set to zero.
2694.Pp
2695.It Xo Ic alias
2696.Oo Fl d | t Oo Fl r Oc |
2697.Cm +-x Oc
2698.Op Fl p
2699.Op Cm +
2700.Op Ar name Ns Oo = Ns Ar value Oc Ar ...
2701.Xc
2702Without arguments,
2703.Ic alias
2704lists all aliases.
2705For any name without a value, the existing alias is listed.
2706Any name with a value defines an alias (see
2707.Sx Aliases
2708above).
2709.Pp
2710When listing aliases, one of two formats is used.
2711Normally, aliases are listed as
2712.Ar name Ns = Ns Ar value ,
2713where
2714.Ar value
2715is quoted.
2716If options were preceded with
2717.Ql + ,
2718or a lone
2719.Ql +
2720is given on the command line, only
2721.Ar name
2722is printed.
2723.Pp
2724The
2725.Fl d
2726option causes directory aliases, which are used in tilde expansion, to be
2727listed or set (see
2728.Sx Tilde expansion
2729above).
2730.Pp
2731If the
2732.Fl p
2733option is used, each alias is prefixed with the string
2734.Dq alias\ \& .
2735.Pp
2736The
2737.Fl t
2738option indicates that tracked aliases are to be listed/set (values specified on
2739the command line are ignored for tracked aliases).
2740The
2741.Fl r
2742option indicates that all tracked aliases are to be reset.
2743.Pp
2744The
2745.Fl x
2746option sets
2747.Pq Ic +x No clears
2748the export attribute of an alias or, if no names are given, lists the aliases
2749with the export attribute (exporting an alias has no effect).
2750.Pp
2751.It Ic bg Op Ar job ...
2752Resume the specified stopped job(s) in the background.
2753If no jobs are specified,
2754.Ic %+
2755is assumed.
2756See
2757.Sx Job control
2758below for more information.
2759.Pp
2760.It Ic bind Op Fl l
2761The current bindings are listed.
2762If the
2763.Fl l
2764flag is given,
2765.Ic bind
2766instead lists the names of the functions to which keys may be bound.
2767See
2768.Sx Emacs editing mode
2769for more information.
2770.Pp
2771.It Xo Ic bind Op Fl m
2772.Ar string Ns = Ns Op Ar substitute
2773.Ar ...
2774.Xc
2775.It Xo Ic bind
2776.Ar string Ns = Ns Op Ar editing-command
2777.Ar ...
2778.Xc
2779In
2780.Sx Emacs editing mode ,
2781the specified editing command is bound to the given
2782.Ar string .
2783Future input of the
2784.Ar string
2785will cause the editing command to be immediately invoked.
2786Bindings have no effect in
2787.Sx Vi editing mode .
2788.Pp
2789If the
2790.Fl m
2791flag is given, the specified input
2792.Ar string
2793will afterwards be immediately replaced by the given
2794.Ar substitute
2795string, which may contain editing commands.
2796Control characters may be written using caret notation.
2797For example, ^X represents Control-X.
2798.Pp
2799If a certain character occurs as the first character of any bound
2800multi-character
2801.Ar string
2802sequence, that character becomes a command prefix character.
2803Any character sequence that starts with a command prefix character
2804but that is not bound to a command or substitute
2805is implicitly considered as bound to the
2806.Sq error
2807command.
2808By default, two command prefix characters exist:
2809Escape
2810.Pq ^[
2811and Control-X
2812.Pq ^X .
2813.Pp
2814The following default bindings show how the arrow keys
2815on an ANSI terminal or xterm are bound
2816(of course some escape sequences won't work out quite this nicely):
2817.Bd -literal -offset indent
2818bind '^[[A'=up-history
2819bind '^[[B'=down-history
2820bind '^[[C'=forward-char
2821bind '^[[D'=backward-char
2822.Ed
2823.Pp
2824.It Ic break Op Ar level
2825Exit the
2826.Ar level Ns th
2827inner-most
2828.Ic for ,
2829.Ic select ,
2830.Ic until ,
2831or
2832.Ic while
2833loop.
2834.Ar level
2835defaults to 1.
2836.Pp
2837.It Ic builtin Ar command Op Ar arg ...
2838Execute the built-in command
2839.Ar command .
2840.Pp
2841.It Xo
2842.Ic cd
2843.Op Fl LP
2844.Op Ar dir
2845.Xc
2846Set the working directory to
2847.Ar dir .
2848If the parameter
2849.Ev CDPATH
2850is set, it lists the search path for the directory containing
2851.Ar dir .
2852A
2853.Dv NULL
2854path or
2855.Ql .\&
2856means the current directory.
2857If
2858.Ar dir
2859is found in any component of the
2860.Ev CDPATH
2861search path other than the
2862.Dv NULL
2863path, the name of the new working directory will be written to standard output.
2864If
2865.Ar dir
2866is missing, the home directory
2867.Ev HOME
2868is used.
2869If
2870.Ar dir
2871is
2872.Ql - ,
2873the previous working directory is used (see the
2874.Ev OLDPWD
2875parameter).
2876.Pp
2877If the
2878.Fl L
2879option (logical path) is used or if the
2880.Ic physical
2881option isn't set (see the
2882.Ic set
2883command below), references to
2884.Sq ..
2885in
2886.Ar dir
2887are relative to the path used to get to the directory.
2888If the
2889.Fl P
2890option (physical path) is used or if the
2891.Ic physical
2892option is set,
2893.Sq ..
2894is relative to the filesystem directory tree.
2895The
2896.Ev PWD
2897and
2898.Ev OLDPWD
2899parameters are updated to reflect the current and old working directory,
2900respectively.
2901.Pp
2902.It Xo
2903.Ic cd
2904.Op Fl LP
2905.Ar old new
2906.Xc
2907The string
2908.Ar new
2909is substituted for
2910.Ar old
2911in the current directory, and the shell attempts to change to the new
2912directory.
2913.Pp
2914.It Xo
2915.Ic command
2916.Op Fl pVv
2917.Ar cmd
2918.Op Ar arg ...
2919.Xc
2920If neither the
2921.Fl v
2922nor
2923.Fl V
2924option is given,
2925.Ar cmd
2926is executed exactly as if
2927.Ic command
2928had not been specified, with two exceptions:
2929firstly,
2930.Ar cmd
2931cannot be an alias or a shell function;
2932and secondly, special built-in commands lose their specialness
2933(i.e. redirection and utility errors do not cause the shell to
2934exit, and command assignments are not permanent).
2935.Pp
2936If the
2937.Fl p
2938option is given, a default search path is used instead of the current value of
2939.Ev PATH
2940(the actual value of the default path is system dependent: on
2941POSIX-ish systems, it is the value returned by
2942.Ic getconf PATH ) .
2943Nevertheless, reserved words, aliases, shell functions, and
2944builtin commands are still found before external commands.
2945.Pp
2946If the
2947.Fl v
2948option is given, instead of executing
2949.Ar cmd ,
2950information about what would be executed is given (and the same is done for
2951.Ar arg ... ) .
2952For special and regular built-in commands and functions, their names are simply
2953printed; for aliases, a command that defines them is printed; and for commands
2954found by searching the
2955.Ev PATH
2956parameter, the full path of the command is printed.
2957If no command is found
2958(i.e. the path search fails), nothing is printed and
2959.Ic command
2960exits with a non-zero status.
2961The
2962.Fl V
2963option is like the
2964.Fl v
2965option, except it is more verbose.
2966.Pp
2967.It Ic continue Op Ar level
2968Jumps to the beginning of the
2969.Ar level Ns th
2970inner-most
2971.Ic for ,
2972.Ic select ,
2973.Ic until ,
2974or
2975.Ic while
2976loop.
2977.Ar level
2978defaults to 1.
2979.Pp
2980.It Xo
2981.Ic echo
2982.Op Fl Een
2983.Op Ar arg ...
2984.Xc
2985Prints its arguments (separated by spaces) followed by a newline, to the
2986standard output.
2987The newline is suppressed if any of the arguments contain the
2988backslash sequence
2989.Ql \ec .
2990See the
2991.Ic print
2992command below for a list of other backslash sequences that are recognized.
2993.Pp
2994The options are provided for compatibility with
2995.Bx
2996shell scripts.
2997The
2998.Fl n
2999option suppresses the trailing newline,
3000.Fl e
3001enables backslash interpretation (a no-op, since this is normally done), and
3002.Fl E
3003suppresses backslash interpretation.
3004If the
3005.Ic posix
3006option is set, only the first argument is treated as an option, and only
3007if it is exactly
3008.Dq -n .
3009.Pp
3010.It Ic eval Ar command ...
3011The arguments are concatenated (with spaces between them) to form a single
3012string which the shell then parses and executes in the current environment.
3013.Pp
3014.It Xo
3015.Ic exec
3016.Op Ar command Op Ar arg ...
3017.Xc
3018The command is executed without forking, replacing the shell process.
3019.Pp
3020If no command is given except for I/O redirection, the I/O redirection is
3021permanent and the shell is
3022not replaced.
3023Any file descriptors greater than 2 which are opened or
3024.Xr dup 2 Ns 'd
3025in this way are not made available to other executed commands (i.e. commands
3026that are not built-in to the shell).
3027Note that the Bourne shell differs here;
3028it does pass these file descriptors on.
3029.Pp
3030.It Ic exit Op Ar status
3031The shell exits with the specified exit status.
3032If
3033.Ar status
3034is not specified, the exit status is the current value of the
3035.Ic $?\&
3036parameter.
3037.Pp
3038.It Xo
3039.Ic export
3040.Op Fl p
3041.Op Ar parameter Ns Op = Ns Ar value
3042.Xc
3043Sets the export attribute of the named parameters.
3044Exported parameters are passed in the environment to executed commands.
3045If values are specified, the named parameters are also assigned.
3046.Pp
3047If no parameters are specified, the names of all parameters with the export
3048attribute are printed one per line, unless the
3049.Fl p
3050option is used, in which case
3051.Ic export
3052commands defining all exported parameters, including their values, are printed.
3053.Pp
3054.It Ic false
3055A command that exits with a non-zero status.
3056.Pp
3057.It Xo
3058.Ic fc
3059.Oo
3060.Fl e Ar editor |
3061.Fl l Op Fl n
3062.Oc
3063.Op Fl r
3064.Op Ar first Op Ar last
3065.Xc
3066Fix command.
3067.Ar first
3068and
3069.Ar last
3070select commands from the history.
3071Commands can be selected by history number
3072or a string specifying the most recent command starting with that string.
3073The
3074.Fl l
3075option lists the command on standard output, and
3076.Fl n
3077inhibits the default command numbers.
3078The
3079.Fl r
3080option reverses the order of the list.
3081Without
3082.Fl l ,
3083the selected commands are edited by the editor specified with the
3084.Fl e
3085option, or if no
3086.Fl e
3087is specified, the editor specified by the
3088.Ev FCEDIT
3089parameter (if this parameter is not set,
3090.Pa /bin/ed
3091is used), and then executed by the shell.
3092.Pp
3093.It Xo
3094.Ic fc Fl s
3095.Op Fl g
3096.Op Ar old Ns = Ns Ar new
3097.Op Ar prefix
3098.Xc
3099Re-execute the most recent command beginning with
3100.Ar prefix ,
3101or the previous command if no
3102.Ar prefix
3103is specified,
3104performing the optional substitution of
3105.Ar old
3106with
3107.Ar new .
3108If
3109.Fl g
3110is specified, all occurrences of
3111.Ar old
3112are replaced with
3113.Ar new .
3114The editor is not invoked when the
3115.Fl s
3116flag is used.
3117The obsolescent equivalent
3118.Dq Fl e No -
3119is also accepted.
3120This command is usually accessed with the predefined
3121.Ic alias r='fc -s' .
3122.Pp
3123.It Ic fg Op Ar job ...
3124Resume the specified job(s) in the foreground.
3125If no jobs are specified,
3126.Ic %+
3127is assumed.
3128See
3129.Sx Job control
3130below for more information.
3131.Pp
3132.It Xo
3133.Ic getopts
3134.Ar optstring name
3135.Op Ar arg ...
3136.Xc
3137Used by shell procedures to parse the specified arguments (or positional
3138parameters, if no arguments are given) and to check for legal options.
3139.Ar optstring
3140contains the option letters that
3141.Ic getopts
3142is to recognize.
3143If a letter is followed by a colon, the option is expected to
3144have an argument.
3145Options that do not take arguments may be grouped in a single argument.
3146If an option takes an argument and the option character is not the
3147last character of the argument it is found in, the remainder of the argument is
3148taken to be the option's argument; otherwise, the next argument is the option's
3149argument.
3150.Pp
3151Each time
3152.Ic getopts
3153is invoked, it places the next option in the shell parameter
3154.Ar name
3155and the index of the argument to be processed by the next call to
3156.Ic getopts
3157in the shell parameter
3158.Ev OPTIND .
3159If the option was introduced with a
3160.Ql + ,
3161the option placed in
3162.Ar name
3163is prefixed with a
3164.Ql + .
3165When an option requires an argument,
3166.Ic getopts
3167places it in the shell parameter
3168.Ev OPTARG .
3169.Pp
3170When an illegal option or a missing option argument is encountered, a question
3171mark or a colon is placed in
3172.Ar name
3173(indicating an illegal option or missing argument, respectively) and
3174.Ev OPTARG
3175is set to the option character that caused the problem.
3176Furthermore, if
3177.Ar optstring
3178does not begin with a colon, a question mark is placed in
3179.Ar name ,
3180.Ev OPTARG
3181is unset, and an error message is printed to standard error.
3182.Pp
3183When the end of the options is encountered,
3184.Ic getopts
3185exits with a non-zero exit status.
3186Options end at the first (non-option
3187argument) argument that does not start with a
3188.Ql - ,
3189or when a
3190.Ql --
3191argument is encountered.
3192.Pp
3193Option parsing can be reset by setting
3194.Ev OPTIND
3195to 1 (this is done automatically whenever the shell or a shell procedure is
3196invoked).
3197.Pp
3198Warning: Changing the value of the shell parameter
3199.Ev OPTIND
3200to a value other than 1, or parsing different sets of arguments without
3201resetting
3202.Ev OPTIND ,
3203may lead to unexpected results.
3204.Pp
3205The following code fragment shows how one might process the arguments
3206for a command that can take the option
3207.Fl a
3208and the option
3209.Fl o ,
3210which requires an argument.
3211.Bd -literal -offset indent
3212while getopts ao: name
3213do
3214	case $name in
3215	a)	flag=1 ;;
3216	o)	oarg=$OPTARG ;;
3217	?)	echo "Usage: ..."; exit 2 ;;
3218	esac
3219done
3220shift $(($OPTIND - 1))
3221echo "Non-option arguments: " "$@"
3222.Ed
3223.Pp
3224.It Xo
3225.Ic hash
3226.Op Fl r
3227.Op Ar name ...
3228.Xc
3229Without arguments, any hashed executable command pathnames are listed.
3230The
3231.Fl r
3232option causes all hashed commands to be removed from the hash table.
3233Each
3234.Ar name
3235is searched as if it were a command name and added to the hash table if it is
3236an executable command.
3237.Pp
3238.It Xo
3239.Ic jobs
3240.Op Fl lnp
3241.Op Ar job ...
3242.Xc
3243Display information about the specified job(s); if no jobs are specified, all
3244jobs are displayed.
3245The
3246.Fl n
3247option causes information to be displayed only for jobs that have changed
3248state since the last notification.
3249If the
3250.Fl l
3251option is used, the process ID of each process in a job is also listed.
3252The
3253.Fl p
3254option causes only the process group of each job to be printed.
3255See
3256.Sx Job control
3257below for the format of
3258.Ar job
3259and the displayed job.
3260.Pp
3261.It Xo
3262.Ic kill
3263.Oo Fl s Ar signame |
3264.No - Ns Ar signum |
3265.No - Ns Ar signame Oc
3266.No { Ar job | pid | pgrp No }
3267.Ar ...
3268.Xc
3269Send the specified signal to the specified jobs, process IDs, or process
3270groups.
3271If no signal is specified, the
3272.Dv TERM
3273signal is sent.
3274If a job is specified, the signal is sent to the job's process group.
3275See
3276.Sx Job control
3277below for the format of
3278.Ar job .
3279.Pp
3280.It Xo
3281.Ic kill
3282.Fl l
3283.Op Ar exit-status ...
3284.Xc
3285Print the signal name corresponding to
3286.Ar exit-status .
3287If no arguments are specified, a list of all the signals, their numbers, and
3288a short description of them are printed.
3289.Pp
3290.It Ic let Op Ar expression ...
3291Each expression is evaluated (see
3292.Sx Arithmetic expressions
3293above).
3294If all expressions are successfully evaluated, the exit status is 0 (1)
3295if the last expression evaluated to non-zero (zero).
3296If an error occurs during
3297the parsing or evaluation of an expression, the exit status is greater than 1.
3298Since expressions may need to be quoted,
3299.No (( Ar expr No ))
3300is syntactic sugar for
3301.No let \&" Ns Ar expr Ns \&" .
3302.Pp
3303.It Xo
3304.Ic print
3305.Oo
3306.Fl nprsu Ns Oo Ar n Oc |
3307.Fl R Op Fl en
3308.Oc
3309.Op Ar argument ...
3310.Xc
3311.Ic print
3312prints its arguments on the standard output, separated by spaces and
3313terminated with a newline.
3314The
3315.Fl n
3316option suppresses the newline.
3317By default, certain C escapes are translated.
3318These include
3319.Ql \eb ,
3320.Ql \ef ,
3321.Ql \en ,
3322.Ql \er ,
3323.Ql \et ,
3324.Ql \ev ,
3325and
3326.Ql \e0###
3327.Po
3328.Ql #
3329is an octal digit, of which there may be 0 to 3
3330.Pc .
3331.Ql \ec
3332is equivalent to using the
3333.Fl n
3334option.
3335.Ql \e
3336expansion may be inhibited with the
3337.Fl r
3338option.
3339The
3340.Fl s
3341option prints to the history file instead of standard output; the
3342.Fl u
3343option prints to file descriptor
3344.Ar n
3345.Po
3346.Ar n
3347defaults to 1 if omitted
3348.Pc ;
3349and the
3350.Fl p
3351option prints to the co-process (see
3352.Sx Co-processes
3353above).
3354.Pp
3355The
3356.Fl R
3357option is used to emulate, to some degree, the
3358.Bx
3359.Xr echo 1
3360command, which does not process
3361.Ql \e
3362sequences unless the
3363.Fl e
3364option is given.
3365As above, the
3366.Fl n
3367option suppresses the trailing newline.
3368.Pp
3369.It Ic pwd Op Fl LP
3370Print the present working directory.
3371If the
3372.Fl L
3373option is used or if the
3374.Ic physical
3375option isn't set (see the
3376.Ic set
3377command below), the logical path is printed (i.e. the path used to
3378.Ic cd
3379to the current directory).
3380If the
3381.Fl P
3382option (physical path) is used or if the
3383.Ic physical
3384option is set, the path determined from the filesystem (by following
3385.Sq ..
3386directories to the root directory) is printed.
3387.Pp
3388.It Xo
3389.Ic read
3390.Op Fl prsu Ns Op Ar n
3391.Op Ar parameter ...
3392.Xc
3393Reads a line of input from the standard input, separates the line into fields
3394using the
3395.Ev IFS
3396parameter (see
3397.Sx Substitution
3398above), and assigns each field to the specified parameters.
3399If there are more parameters than fields, the extra parameters are set to
3400.Dv NULL ,
3401or alternatively, if there are more fields than parameters, the last parameter
3402is assigned the remaining fields (inclusive of any separating spaces).
3403If no parameters are specified, the
3404.Ev REPLY
3405parameter is used.
3406If the input line ends in a backslash and the
3407.Fl r
3408option was not used, the backslash and the newline are stripped and more input
3409is read.
3410If no input is read,
3411.Ic read
3412exits with a non-zero status.
3413.Pp
3414The first parameter may have a question mark and a string appended to it, in
3415which case the string is used as a prompt (printed to standard error before
3416any input is read) if the input is a
3417.Xr tty 4
3418(e.g.\&
3419.Ic read nfoo?'number of foos: ' ) .
3420.Pp
3421The
3422.Fl u Ns Ar n
3423and
3424.Fl p
3425options cause input to be read from file descriptor
3426.Ar n
3427.Pf ( Ar n
3428defaults to 0 if omitted)
3429or the current co-process (see
3430.Sx Co-processes
3431above for comments on this), respectively.
3432If the
3433.Fl s
3434option is used, input is saved to the history file.
3435.Pp
3436.It Xo
3437.Ic readonly
3438.Op Fl p
3439.Op Ar parameter Ns Oo = Ns Ar value Oc Ar ...
3440.Xc
3441Sets the read-only attribute of the named parameters.
3442If values are given,
3443parameters are set to them before setting the attribute.
3444Once a parameter is
3445made read-only, it cannot be unset and its value cannot be changed.
3446.Pp
3447If no parameters are specified, the names of all parameters with the read-only
3448attribute are printed one per line, unless the
3449.Fl p
3450option is used, in which case
3451.Ic readonly
3452commands defining all read-only parameters, including their values, are
3453printed.
3454.Pp
3455.It Ic return Op Ar status
3456Returns from a function or
3457.Ic .\&
3458script, with exit status
3459.Ar status .
3460If no
3461.Ar status
3462is given, the exit status of the last executed command is used.
3463If used outside of a function or
3464.Ic .\&
3465script, it has the same effect as
3466.Ic exit .
3467Note that
3468.Nm ksh
3469treats both profile and
3470.Ev ENV
3471files as
3472.Ic .\&
3473scripts, while the original Korn shell only treats profiles as
3474.Ic .\&
3475scripts.
3476.Pp
3477.It Xo
3478.Ic set Op Ic +-abCefhkmnpsuvXx
3479.Op Ic +-o Ar option
3480.Op Ic +-A Ar name
3481.Op Fl -
3482.Op Ar arg ...
3483.Xc
3484The
3485.Ic set
3486command can be used to set
3487.Pq Ic -
3488or clear
3489.Pq Ic +
3490shell options, set the positional parameters, or set an array parameter.
3491Options can be changed using the
3492.Cm +-o Ar option
3493syntax, where
3494.Ar option
3495is the long name of an option, or using the
3496.Cm +- Ns Ar letter
3497syntax, where
3498.Ar letter
3499is the option's single letter name (not all options have a single letter name).
3500The following table lists both option letters (if they exist) and long names
3501along with a description of what the option does:
3502.Bl -tag -width 15n
3503.It Fl A Ar name
3504Sets the elements of the array parameter
3505.Ar name
3506to
3507.Ar arg ...
3508If
3509.Fl A
3510is used, the array is reset (i.e. emptied) first; if
3511.Ic +A
3512is used, the first N elements are set (where N is the number of arguments);
3513the rest are left untouched.
3514.It Fl a | Ic allexport
3515All new parameters are created with the export attribute.
3516.It Fl b | Ic notify
3517Print job notification messages asynchronously, instead of just before the
3518prompt.
3519Only used if job control is enabled
3520.Pq Fl m .
3521.It Fl C | Ic noclobber
3522Prevent
3523.Cm >
3524redirection from overwriting existing files.
3525Instead,
3526.Cm >|
3527must be used to force an overwrite.
3528.It Fl e | Ic errexit
3529Exit (after executing the
3530.Dv ERR
3531trap) as soon as an error occurs or a command fails (i.e. exits with a
3532non-zero status).
3533This does not apply to commands whose exit status is
3534explicitly tested by a shell construct such as
3535.Ic if ,
3536.Ic until ,
3537.Ic while ,
3538or
3539.Ic !\&
3540statements.
3541For
3542.Ic &&
3543or
3544.Ic || ,
3545only the status of the last command is tested.
3546.It Fl f | Ic noglob
3547Do not expand file name patterns.
3548.It Fl h | Ic trackall
3549Create tracked aliases for all executed commands (see
3550.Sx Aliases
3551above).
3552Enabled by default for non-interactive shells.
3553.It Fl k | Ic keyword
3554Parameter assignments are recognized anywhere in a command.
3555.It Fl m | Ic monitor
3556Enable job control (default for interactive shells).
3557.It Fl n | Ic noexec
3558Do not execute any commands.
3559Useful for checking the syntax of scripts
3560(ignored if interactive).
3561.It Fl p | Ic privileged
3562The shell is a privileged shell.
3563It is set automatically if, when the shell starts,
3564the real UID or GID does not match
3565the effective UID (EUID) or GID (EGID), respectively.
3566See above for a description of what this means.
3567.It Fl s | Ic stdin
3568If used when the shell is invoked, commands are read from standard input.
3569Set automatically if the shell is invoked with no arguments.
3570.Pp
3571When
3572.Fl s
3573is used with the
3574.Ic set
3575command it causes the specified arguments to be sorted before assigning them to
3576the positional parameters (or to array
3577.Ar name ,
3578if
3579.Fl A
3580is used).
3581.It Fl u | Ic nounset
3582Referencing of an unset parameter is treated as an error, unless one of the
3583.Ql - ,
3584.Ql + ,
3585or
3586.Ql =
3587modifiers is used.
3588.It Fl v | Ic verbose
3589Write shell input to standard error as it is read.
3590.It Fl X | Ic markdirs
3591Mark directories with a trailing
3592.Ql /
3593during file name generation.
3594.It Fl x | Ic xtrace
3595Print commands and parameter assignments when they are executed, preceded by
3596the value of
3597.Ev PS4 .
3598.It Ic bgnice
3599Background jobs are run with lower priority.
3600.It Ic braceexpand
3601Enable brace expansion (a.k.a. alternation).
3602.It Ic csh-history
3603Enables a subset of
3604.Xr csh 1 Ns -style
3605history editing using the
3606.Ql !\&
3607character.
3608.It Ic emacs
3609Enable BRL emacs-like command-line editing (interactive shells only); see
3610.Sx Emacs editing mode .
3611.It Ic gmacs
3612Enable gmacs-like command-line editing (interactive shells only).
3613Currently identical to emacs editing except that transpose (^T) acts slightly
3614differently.
3615.It Ic ignoreeof
3616The shell will not (easily) exit when end-of-file is read;
3617.Ic exit
3618must be used.
3619To avoid infinite loops, the shell will exit if
3620.Dv EOF
3621is read 13 times in a row.
3622.It Ic interactive
3623The shell is an interactive shell.
3624This option can only be used when the shell is invoked.
3625See above for a description of what this means.
3626.It Ic login
3627The shell is a login shell.
3628This option can only be used when the shell is invoked.
3629See above for a description of what this means.
3630.It Ic nohup
3631Do not kill running jobs with a
3632.Dv SIGHUP
3633signal when a login shell exits.
3634Currently set by default;
3635this is different from the original Korn shell (which
3636doesn't have this option, but does send the
3637.Dv SIGHUP
3638signal).
3639.It Ic nolog
3640No effect.
3641In the original Korn shell, this prevents function definitions from
3642being stored in the history file.
3643.It Ic physical
3644Causes the
3645.Ic cd
3646and
3647.Ic pwd
3648commands to use
3649.Dq physical
3650(i.e. the filesystem's)
3651.Sq ..
3652directories instead of
3653.Dq logical
3654directories (i.e. the shell handles
3655.Sq .. ,
3656which allows the user to be oblivious of symbolic links to directories).
3657Clear by default.
3658Note that setting this option does not affect the current value of the
3659.Ev PWD
3660parameter; only the
3661.Ic cd
3662command changes
3663.Ev PWD .
3664See the
3665.Ic cd
3666and
3667.Ic pwd
3668commands above for more details.
3669.It Ic pipefail
3670The exit status of a pipeline is the exit status of the rightmost
3671command in the pipeline that doesn't return 0, or 0 if all commands
3672returned a 0 exit status.
3673.It Ic posix
3674Enable POSIX mode.
3675See
3676.Sx POSIX mode
3677above.
3678.It Ic restricted
3679The shell is a restricted shell.
3680This option can only be used when the shell is invoked.
3681See above for a description of what this means.
3682.It Ic sh
3683Enable strict Bourne shell mode (see
3684.Sx Strict Bourne shell mode
3685above).
3686.It Ic vi
3687Enable
3688.Xr vi 1 Ns -like
3689command-line editing (interactive shells only).
3690.It Ic vi-esccomplete
3691In vi command-line editing, do command and file name completion when escape
3692(^[) is entered in command mode.
3693.It Ic vi-show8
3694Prefix characters with the eighth bit set with
3695.Sq M- .
3696If this option is not set, characters in the range 128\-160 are printed as is,
3697which may cause problems.
3698.It Ic vi-tabcomplete
3699In vi command-line editing, do command and file name completion when tab (^I)
3700is entered in insert mode.
3701This is the default.
3702.It Ic viraw
3703No effect.
3704In the original Korn shell, unless
3705.Ic viraw
3706was set, the vi command-line mode would let the
3707.Xr tty 4
3708driver do the work until ESC (^[) was entered.
3709.Nm ksh
3710is always in viraw mode.
3711.El
3712.Pp
3713These options can also be used upon invocation of the shell.
3714The current set of
3715options (with single letter names) can be found in the parameter
3716.Sq $- .
3717.Ic set Fl o
3718with no option name will list all the options and whether each is on or off;
3719.Ic set +o
3720will print the current shell options in a form that
3721can be reinput to the shell to achieve the same option settings.
3722.Pp
3723Remaining arguments, if any, are positional parameters and are assigned, in
3724order, to the positional parameters (i.e. $1, $2, etc.).
3725If options end with
3726.Ql --
3727and there are no remaining arguments, all positional parameters are cleared.
3728If no options or arguments are given, the values of all names are printed.
3729For unknown historical reasons, a lone
3730.Ql -
3731option is treated specially \- it clears both the
3732.Fl x
3733and
3734.Fl v
3735options.
3736.Pp
3737.It Ic shift Op Ar number
3738The positional parameters
3739.Ar number Ns +1 ,
3740.Ar number Ns +2 ,
3741etc. are renamed to
3742.Sq 1 ,
3743.Sq 2 ,
3744etc.
3745.Ar number
3746defaults to 1.
3747.Pp
3748.It Ic suspend
3749Stops the shell as if it had received the suspend character from
3750the terminal.
3751It is not possible to suspend a login shell unless the parent process
3752is a member of the same terminal session but is a member of a different
3753process group.
3754As a general rule, if the shell was started by another shell or via
3755.Xr su 1 ,
3756it can be suspended.
3757.Pp
3758.It Ic test Ar expression
3759.It Ic \&[ Ar expression Ic \&]
3760.Ic test
3761evaluates the
3762.Ar expression
3763and returns zero status if true, 1 if false, or greater than 1 if there
3764was an error.
3765It is normally used as the condition command of
3766.Ic if
3767and
3768.Ic while
3769statements.
3770Symbolic links are followed for all
3771.Ar file
3772expressions except
3773.Fl h
3774and
3775.Fl L .
3776.Pp
3777The following basic expressions are available:
3778.Bl -tag -width 17n
3779.It Fl a Ar file
3780.Ar file
3781exists.
3782.It Fl b Ar file
3783.Ar file
3784is a block special device.
3785.It Fl c Ar file
3786.Ar file
3787is a character special device.
3788.It Fl d Ar file
3789.Ar file
3790is a directory.
3791.It Fl e Ar file
3792.Ar file
3793exists.
3794.It Fl f Ar file
3795.Ar file
3796is a regular file.
3797.It Fl G Ar file
3798.Ar file Ns 's
3799group is the shell's effective group ID.
3800.It Fl g Ar file
3801.Ar file Ns 's
3802mode has the setgid bit set.
3803.It Fl h Ar file
3804.Ar file
3805is a symbolic link.
3806.It Fl k Ar file
3807.Ar file Ns 's
3808mode has the
3809.Xr sticky 8
3810bit set.
3811.It Fl L Ar file
3812.Ar file
3813is a symbolic link.
3814.It Fl O Ar file
3815.Ar file Ns 's
3816owner is the shell's effective user ID.
3817.It Fl o Ar option
3818Shell
3819.Ar option
3820is set (see the
3821.Ic set
3822command above for a list of options).
3823As a non-standard extension, if the option starts with a
3824.Ql \&! ,
3825the test is negated; the test always fails if
3826.Ar option
3827doesn't exist (so [ -o foo -o -o !foo ] returns true if and only if option
3828.Ar foo
3829exists).
3830.It Fl p Ar file
3831.Ar file
3832is a named pipe.
3833.It Fl r Ar file
3834.Ar file
3835exists and is readable.
3836.It Fl S Ar file
3837.Ar file
3838is a
3839.Xr unix 4 Ns -domain
3840socket.
3841.It Fl s Ar file
3842.Ar file
3843is not empty.
3844.It Fl t Ar fd
3845File descriptor
3846.Ar fd
3847is a
3848.Xr tty 4
3849device.
3850.It Fl u Ar file
3851.Ar file Ns 's
3852mode has the setuid bit set.
3853.It Fl w Ar file
3854.Ar file
3855exists and is writable.
3856.It Fl x Ar file
3857.Ar file
3858exists and is executable.
3859.It Ar file1 Fl nt Ar file2
3860.Ar file1
3861is newer than
3862.Ar file2
3863or
3864.Ar file1
3865exists and
3866.Ar file2
3867does not.
3868.It Ar file1 Fl ot Ar file2
3869.Ar file1
3870is older than
3871.Ar file2
3872or
3873.Ar file2
3874exists and
3875.Ar file1
3876does not.
3877.It Ar file1 Fl ef Ar file2
3878.Ar file1
3879is the same file as
3880.Ar file2 .
3881.It Ar string
3882.Ar string
3883has non-zero length.
3884.It Fl n Ar string
3885.Ar string
3886is not empty.
3887.It Fl z Ar string
3888.Ar string
3889is empty.
3890.It Ar string No = Ar string
3891Strings are equal.
3892.It Ar string No == Ar string
3893Strings are equal.
3894.It Ar string No != Ar string
3895Strings are not equal.
3896.It Ar string No > Ar string
3897Strings compare greater than based on the ASCII value of their characters.
3898.It Ar string No < Ar string
3899Strings compare less than based on the ASCII value of their characters.
3900.It Ar number Fl eq Ar number
3901Numbers compare equal.
3902.It Ar number Fl ne Ar number
3903Numbers compare not equal.
3904.It Ar number Fl ge Ar number
3905Numbers compare greater than or equal.
3906.It Ar number Fl gt Ar number
3907Numbers compare greater than.
3908.It Ar number Fl le Ar number
3909Numbers compare less than or equal.
3910.It Ar number Fl \&lt Ar number
3911Numbers compare less than.
3912.El
3913.Pp
3914The above basic expressions, in which unary operators have precedence over
3915binary operators, may be combined with the following operators (listed in
3916increasing order of precedence):
3917.Bd -literal -offset indent
3918expr -o expr		Logical OR.
3919expr -a expr		Logical AND.
3920! expr			Logical NOT.
3921( expr )		Grouping.
3922.Ed
3923.Pp
3924On operating systems not supporting
3925.Pa /dev/fd/ Ns Ar n
3926devices (where
3927.Ar n
3928is a file descriptor number), the
3929.Ic test
3930command will attempt to fake it for all tests that operate on files (except the
3931.Fl e
3932test).
3933For example,
3934[ -w /dev/fd/2 ] tests if file descriptor 2 is writable.
3935.Pp
3936Note that some special rules are applied (courtesy of POSIX)
3937if the number of
3938arguments to
3939.Ic test
3940or
3941.Ic \&[ ... \&]
3942is less than five: if leading
3943.Ql \&!
3944arguments can be stripped such that only one argument remains then a string
3945length test is performed (again, even if the argument is a unary operator); if
3946leading
3947.Ql \&!
3948arguments can be stripped such that three arguments remain and the second
3949argument is a binary operator, then the binary operation is performed (even
3950if the first argument is a unary operator, including an unstripped
3951.Ql \&! ) .
3952.Pp
3953.Sy Note :
3954A common mistake is to use
3955.Dq if \&[ $foo = bar \&]
3956which fails if parameter
3957.Dq foo
3958is
3959.Dv NULL
3960or unset, if it has embedded spaces (i.e.\&
3961.Ev IFS
3962characters), or if it is a unary operator like
3963.Sq \&!
3964or
3965.Sq Fl n .
3966Use tests like
3967.Dq if \&[ \&"X$foo\&" = Xbar \&]
3968instead.
3969.Pp
3970.It Xo
3971.Ic time
3972.Op Fl p
3973.Op Ar pipeline
3974.Xc
3975If a
3976.Ar pipeline
3977is given, the times used to execute the pipeline are reported.
3978If no pipeline
3979is given, then the user and system time used by the shell itself, and all the
3980commands it has run since it was started, are reported.
3981The times reported are the real time (elapsed time from start to finish),
3982the user CPU time (time spent running in user mode), and the system CPU time
3983(time spent running in kernel mode).
3984Times are reported to standard error; the format of the output is:
3985.Pp
3986.Dl "0m0.00s real     0m0.00s user     0m0.00s system"
3987.Pp
3988If the
3989.Fl p
3990option is given, the output is slightly longer:
3991.Bd -literal -offset indent
3992real     0.00
3993user     0.00
3994sys      0.00
3995.Ed
3996.Pp
3997It is an error to specify the
3998.Fl p
3999option unless
4000.Ar pipeline
4001is a simple command.
4002.Pp
4003Simple redirections of standard error do not affect the output of the
4004.Ic time
4005command:
4006.Pp
4007.Dl $ time sleep 1 2> afile
4008.Dl $ { time sleep 1; } 2> afile
4009.Pp
4010Times for the first command do not go to
4011.Dq afile ,
4012but those of the second command do.
4013.Pp
4014.It Ic times
4015Print the accumulated user and system times used both by the shell
4016and by processes that the shell started which have exited.
4017The format of the output is:
4018.Bd -literal -offset indent
40190m0.00s 0m0.00s
40200m0.00s 0m0.00s
4021.Ed
4022.Pp
4023.It Ic trap Op Ar handler signal ...
4024Sets a trap handler that is to be executed when any of the specified signals are
4025received.
4026.Ar handler
4027is either a
4028.Dv NULL
4029string, indicating the signals are to be ignored, a minus sign
4030.Pq Sq - ,
4031indicating that the default action is to be taken for the signals (see
4032.Xr signal 3 ) ,
4033or a string containing shell commands to be evaluated and executed at the first
4034opportunity (i.e. when the current command completes, or before printing the
4035next
4036.Ev PS1
4037prompt) after receipt of one of the signals.
4038.Ar signal
4039is the name of a signal (e.g.\&
4040.Dv PIPE
4041or
4042.Dv ALRM )
4043or the number of the signal (see the
4044.Ic kill -l
4045command above).
4046.Pp
4047There are two special signals:
4048.Dv EXIT
4049(also known as 0), which is executed when the shell is about to exit, and
4050.Dv ERR ,
4051which is executed after an error occurs (an error is something that would cause
4052the shell to exit if the
4053.Fl e
4054or
4055.Ic errexit
4056option were set \- see the
4057.Ic set
4058command above).
4059.Dv EXIT
4060handlers are executed in the environment of the last executed command.
4061Note
4062that for non-interactive shells, the trap handler cannot be changed for signals
4063that were ignored when the shell started.
4064.Pp
4065With no arguments,
4066.Ic trap
4067lists, as a series of
4068.Ic trap
4069commands, the current state of the traps that have been set since the shell
4070started.
4071Note that the output of
4072.Ic trap
4073cannot be usefully piped to another process (an artifact of the fact that
4074traps are cleared when subprocesses are created).
4075.Pp
4076The original Korn shell's
4077.Dv DEBUG
4078trap and the handling of
4079.Dv ERR
4080and
4081.Dv EXIT
4082traps in functions are not yet implemented.
4083.Pp
4084.It Ic true
4085A command that exits with a zero value.
4086.Pp
4087.It Ic type
4088Short form of
4089.Ic command Fl V
4090(see above).
4091.Pp
4092.It Xo
4093.Ic typeset
4094.Oo
4095.Op Ic +-lprtUux
4096.Op Fl L Ns Op Ar n
4097.Op Fl R Ns Op Ar n
4098.Op Fl Z Ns Op Ar n
4099.Op Fl i Ns Op Ar n
4100.No \&| Fl f Op Fl tux
4101.Oc
4102.Op Ar name Ns Oo = Ns Ar value Oc Ar ...
4103.Xc
4104Display or set parameter attributes.
4105With no
4106.Ar name
4107arguments, parameter attributes are displayed; if no options are used, the
4108current attributes of all parameters are printed as
4109.Ic typeset
4110commands; if an option is given (or
4111.Ql -
4112with no option letter), all parameters and their values with the specified
4113attributes are printed; if options are introduced with
4114.Ql + ,
4115parameter values are not printed.
4116.Pp
4117If
4118.Ar name
4119arguments are given, the attributes of the named parameters are set
4120.Pq Ic -
4121or cleared
4122.Pq Ic + .
4123Values for parameters may optionally be specified.
4124If
4125.Ic typeset
4126is used inside a function, any newly created parameters are local to the
4127function.
4128.Pp
4129When
4130.Fl f
4131is used,
4132.Ic typeset
4133operates on the attributes of functions.
4134As with parameters, if no
4135.Ar name
4136arguments are given,
4137functions are listed with their values (i.e. definitions) unless
4138options are introduced with
4139.Ql + ,
4140in which case only the function names are reported.
4141.Bl -tag -width Ds
4142.It Fl f
4143Function mode.
4144Display or set functions and their attributes, instead of parameters.
4145.It Fl i Ns Op Ar n
4146Integer attribute.
4147.Ar n
4148specifies the base to use when displaying the integer (if not specified, the
4149base given in the first assignment is used).
4150Parameters with this attribute may
4151be assigned values containing arithmetic expressions.
4152.It Fl L Ns Op Ar n
4153Left justify attribute.
4154.Ar n
4155specifies the field width.
4156If
4157.Ar n
4158is not specified, the current width of a parameter (or the width of its first
4159assigned value) is used.
4160Leading whitespace (and zeros, if used with the
4161.Fl Z
4162option) is stripped.
4163If necessary, values are either truncated or space padded
4164to fit the field width.
4165.It Fl l
4166Lower case attribute.
4167All upper case characters in values are converted to lower case.
4168(In the original Korn shell, this parameter meant
4169.Dq long integer
4170when used with the
4171.Fl i
4172option.)
4173.It Fl p
4174Print complete
4175.Ic typeset
4176commands that can be used to re-create the attributes (but not the values) of
4177parameters.
4178This is the default action (option exists for ksh93 compatibility).
4179.It Fl R Ns Op Ar n
4180Right justify attribute.
4181.Ar n
4182specifies the field width.
4183If
4184.Ar n
4185is not specified, the current width of a parameter (or the width of its first
4186assigned value) is used.
4187Trailing whitespace is stripped.
4188If necessary, values are either stripped of leading characters or space
4189padded to make them fit the field width.
4190.It Fl r
4191Read-only attribute.
4192Parameters with this attribute may not be assigned to or unset.
4193Once this attribute is set, it cannot be turned off.
4194.It Fl t
4195Tag attribute.
4196Has no meaning to the shell; provided for application use.
4197.Pp
4198For functions,
4199.Fl t
4200is the trace attribute.
4201When functions with the trace attribute are executed, the
4202.Ic xtrace
4203.Pq Fl x
4204shell option is temporarily turned on.
4205.It Fl U
4206Unsigned integer attribute.
4207Integers are printed as unsigned values (only
4208useful when combined with the
4209.Fl i
4210option).
4211This option is not in the original Korn shell.
4212.It Fl u
4213Upper case attribute.
4214All lower case characters in values are converted to upper case.
4215(In the original Korn shell, this parameter meant
4216.Dq unsigned integer
4217when used with the
4218.Fl i
4219option, which meant upper case letters would never be used for bases greater
4220than 10.
4221See the
4222.Fl U
4223option.)
4224.Pp
4225For functions,
4226.Fl u
4227is the undefined attribute.
4228See
4229.Sx Functions
4230above for the implications of this.
4231.It Fl x
4232Export attribute.
4233Parameters (or functions) are placed in the environment of
4234any executed commands.
4235Exported functions are not yet implemented.
4236.It Fl Z Ns Op Ar n
4237Zero fill attribute.
4238If not combined with
4239.Fl L ,
4240this is the same as
4241.Fl R ,
4242except zero padding is used instead of space padding.
4243.El
4244.Pp
4245.It Xo
4246.Ic ulimit
4247.Op Fl acdfHlmnpSst Op Ar value
4248.Ar ...
4249.Xc
4250Display or set process limits.
4251If no options are used, the file size limit
4252.Pq Fl f
4253is assumed.
4254.Ar value ,
4255if specified, may be either an arithmetic expression starting with a
4256number or the word
4257.Dq unlimited .
4258The limits affect the shell and any processes created by the shell after a
4259limit is imposed; limits may not be increased once they are set.
4260.Bl -tag -width 5n
4261.It Fl a
4262Display all limits; unless
4263.Fl H
4264is used, soft limits are displayed.
4265.It Fl c Ar n
4266Impose a size limit of
4267.Ar n
4268blocks on the size of core dumps.
4269.It Fl d Ar n
4270Impose a size limit of
4271.Ar n
4272kilobytes on the size of the data area.
4273.It Fl f Ar n
4274Impose a size limit of
4275.Ar n
4276blocks on files written by the shell and its child processes (files of any
4277size may be read).
4278.It Fl H
4279Set the hard limit only (the default is to set both hard and soft limits).
4280.It Fl l Ar n
4281Impose a limit of
4282.Ar n
4283kilobytes on the amount of locked (wired) physical memory.
4284.It Fl m Ar n
4285Impose a limit of
4286.Ar n
4287kilobytes on the amount of physical memory used.
4288This limit is not enforced.
4289.It Fl n Ar n
4290Impose a limit of
4291.Ar n
4292file descriptors that can be open at once.
4293.It Fl p Ar n
4294Impose a limit of
4295.Ar n
4296processes that can be run by the user at any one time.
4297.It Fl S
4298Set the soft limit only (the default is to set both hard and soft limits).
4299.It Fl s Ar n
4300Impose a size limit of
4301.Ar n
4302kilobytes on the size of the stack area.
4303.It Fl t Ar n
4304Impose a time limit of
4305.Ar n
4306CPU seconds spent in user mode to be used by each process.
4307.\".It Fl v Ar n
4308.\"Impose a limit of
4309.\"Ar n
4310.\"kilobytes on the amount of virtual memory used.
4311.El
4312.Pp
4313As far as
4314.Ic ulimit
4315is concerned, a block is 512 bytes.
4316.Pp
4317.It Xo
4318.Ic umask
4319.Op Fl S
4320.Op Ar mask
4321.Xc
4322Display or set the file permission creation mask, or umask (see
4323.Xr umask 2 ) .
4324If the
4325.Fl S
4326option is used, the mask displayed or set is symbolic; otherwise, it is an
4327octal number.
4328.Pp
4329Symbolic masks are like those used by
4330.Xr chmod 1 .
4331When used, they describe what permissions may be made available (as opposed to
4332octal masks in which a set bit means the corresponding bit is to be cleared).
4333For example,
4334.Dq ug=rwx,o=
4335sets the mask so files will not be readable, writable, or executable by
4336.Dq others ,
4337and is equivalent (on most systems) to the octal mask
4338.Dq 007 .
4339.Pp
4340.It Xo
4341.Ic unalias
4342.Op Fl adt
4343.Op Ar name ...
4344.Xc
4345The aliases for the given names are removed.
4346If the
4347.Fl a
4348option is used, all aliases are removed.
4349If the
4350.Fl t
4351or
4352.Fl d
4353options are used, the indicated operations are carried out on tracked or
4354directory aliases, respectively.
4355.Pp
4356.It Xo
4357.Ic unset
4358.Op Fl fv
4359.Ar parameter ...
4360.Xc
4361Unset the named parameters
4362.Po
4363.Fl v ,
4364the default
4365.Pc
4366or functions
4367.Pq Fl f .
4368The exit status is non-zero if any of the parameters have the read-only
4369attribute set, zero otherwise.
4370.Pp
4371.It Ic wait Op Ar job ...
4372Wait for the specified job(s) to finish.
4373The exit status of
4374.Ic wait
4375is that of the last specified job; if the last job is killed by a signal, the
4376exit status is 128 + the number of the signal (see
4377.Ic kill -l Ar exit-status
4378above); if the last specified job can't be found (because it never existed, or
4379had already finished), the exit status of
4380.Ic wait
4381is 127.
4382See
4383.Sx Job control
4384below for the format of
4385.Ar job .
4386.Ic wait
4387will return if a signal for which a trap has been set is received, or if a
4388.Dv SIGHUP ,
4389.Dv SIGINT ,
4390or
4391.Dv SIGQUIT
4392signal is received.
4393.Pp
4394If no jobs are specified,
4395.Ic wait
4396waits for all currently running jobs (if any) to finish and exits with a zero
4397status.
4398If job monitoring is enabled, the completion status of jobs is printed
4399(this is not the case when jobs are explicitly specified).
4400.Pp
4401.It Xo
4402.Ic whence
4403.Op Fl pv
4404.Op Ar name ...
4405.Xc
4406For each
4407.Ar name ,
4408the type of command is listed (reserved word, built-in, alias,
4409function, tracked alias, or executable).
4410If the
4411.Fl p
4412option is used, a path search is performed even if
4413.Ar name
4414is a reserved word, alias, etc.
4415Without the
4416.Fl v
4417option,
4418.Ic whence
4419is similar to
4420.Ic command Fl v
4421except that
4422.Ic whence
4423won't print aliases as alias commands.
4424With the
4425.Fl v
4426option,
4427.Ic whence
4428is the same as
4429.Ic command Fl V .
4430Note that for
4431.Ic whence ,
4432the
4433.Fl p
4434option does not affect the search path used, as it does for
4435.Ic command .
4436If the type of one or more of the names could not be determined, the exit
4437status is non-zero.
4438.El
4439.Ss Job control
4440Job control refers to the shell's ability to monitor and control jobs, which
4441are processes or groups of processes created for commands or pipelines.
4442At a minimum, the shell keeps track of the status of the background (i.e.\&
4443asynchronous) jobs that currently exist; this information can be displayed
4444using the
4445.Ic jobs
4446commands.
4447If job control is fully enabled (using
4448.Ic set -m
4449or
4450.Ic set -o monitor ) ,
4451as it is for interactive shells, the processes of a job are placed in their
4452own process group.
4453Foreground jobs can be stopped by typing the suspend
4454character from the terminal (normally ^Z), jobs can be restarted in either the
4455foreground or background using the
4456.Ic fg
4457and
4458.Ic bg
4459commands, and the state of the terminal is saved or restored when a foreground
4460job is stopped or restarted, respectively.
4461.Pp
4462Note that only commands that create processes (e.g. asynchronous commands,
4463subshell commands, and non-built-in, non-function commands) can be stopped;
4464commands like
4465.Ic read
4466cannot be.
4467.Pp
4468When a job is created, it is assigned a job number.
4469For interactive shells, this number is printed inside
4470.Dq [..] ,
4471followed by the process IDs of the processes in the job when an asynchronous
4472command is run.
4473A job may be referred to in the
4474.Ic bg ,
4475.Ic fg ,
4476.Ic jobs ,
4477.Ic kill ,
4478and
4479.Ic wait
4480commands either by the process ID of the last process in the command pipeline
4481(as stored in the
4482.Ic $!\&
4483parameter) or by prefixing the job number with a percent
4484sign
4485.Pq Sq % .
4486Other percent sequences can also be used to refer to jobs:
4487.Bl -tag -width "%+ | %% | %XX"
4488.It %+ | %% | %
4489The most recently stopped job or, if there are no stopped jobs, the oldest
4490running job.
4491.It %-
4492The job that would be the
4493.Ic %+
4494job if the latter did not exist.
4495.It % Ns Ar n
4496The job with job number
4497.Ar n .
4498.It %? Ns Ar string
4499The job with its command containing the string
4500.Ar string
4501(an error occurs if multiple jobs are matched).
4502.It % Ns Ar string
4503The job with its command starting with the string
4504.Ar string
4505(an error occurs if multiple jobs are matched).
4506.El
4507.Pp
4508When a job changes state (e.g. a background job finishes or foreground job is
4509stopped), the shell prints the following status information:
4510.Pp
4511.D1 [ Ns Ar number ] Ar flag status command
4512.Pp
4513where...
4514.Bl -tag -width "command"
4515.It Ar number
4516is the job number of the job;
4517.It Ar flag
4518is the
4519.Ql +
4520or
4521.Ql -
4522character if the job is the
4523.Ic %+
4524or
4525.Ic %-
4526job, respectively, or space if it is neither;
4527.It Ar status
4528indicates the current state of the job and can be:
4529.Bl -tag -width "RunningXX"
4530.It Done Op Ar number
4531The job exited.
4532.Ar number
4533is the exit status of the job, which is omitted if the status is zero.
4534.It Running
4535The job has neither stopped nor exited (note that running does not necessarily
4536mean consuming CPU time \-
4537the process could be blocked waiting for some event).
4538.It Stopped Op Ar signal
4539The job was stopped by the indicated
4540.Ar signal
4541(if no signal is given, the job was stopped by
4542.Dv SIGTSTP ) .
4543.It Ar signal-description Op Dq core dumped
4544The job was killed by a signal (e.g. memory fault, hangup); use
4545.Ic kill -l
4546for a list of signal descriptions.
4547The
4548.Dq core dumped
4549message indicates the process created a core file.
4550.El
4551.It Ar command
4552is the command that created the process.
4553If there are multiple processes in
4554the job, each process will have a line showing its
4555.Ar command
4556and possibly its
4557.Ar status ,
4558if it is different from the status of the previous process.
4559.El
4560.Pp
4561When an attempt is made to exit the shell while there are jobs in the stopped
4562state, the shell warns the user that there are stopped jobs and does not exit.
4563If another attempt is immediately made to exit the shell, the stopped jobs are
4564sent a
4565.Dv SIGHUP
4566signal and the shell exits.
4567Similarly, if the
4568.Ic nohup
4569option is not set and there are running jobs when an attempt is made to exit
4570a login shell, the shell warns the user and does not exit.
4571If another attempt
4572is immediately made to exit the shell, the running jobs are sent a
4573.Dv SIGHUP
4574signal and the shell exits.
4575.Ss Interactive input line editing
4576The shell supports three modes of reading command lines from a
4577.Xr tty 4
4578in an interactive session, controlled by the
4579.Ic emacs ,
4580.Ic gmacs ,
4581and
4582.Ic vi
4583options (at most one of these can be set at once).
4584The default is
4585.Ic emacs .
4586Editing modes can be set explicitly using the
4587.Ic set
4588built-in, or implicitly via the
4589.Ev EDITOR
4590and
4591.Ev VISUAL
4592environment variables.
4593If none of these options are enabled,
4594the shell simply reads lines using the normal
4595.Xr tty 4
4596driver.
4597If the
4598.Ic emacs
4599or
4600.Ic gmacs
4601option is set, the shell allows emacs-like editing of the command; similarly,
4602if the
4603.Ic vi
4604option is set, the shell allows vi-like editing of the command.
4605These modes are described in detail in the following sections.
4606.Pp
4607In these editing modes, if a line is longer than the screen width (see the
4608.Ev COLUMNS
4609parameter),
4610a
4611.Ql > ,
4612.Ql + ,
4613or
4614.Ql <
4615character is displayed in the last column indicating that there are more
4616characters after, before and after, or before the current position,
4617respectively.
4618The line is scrolled horizontally as necessary.
4619.Ss Emacs editing mode
4620When the
4621.Ic emacs
4622option is set, interactive input line editing is enabled.
4623Warning: This mode is
4624slightly different from the emacs mode in the original Korn shell.
4625In this mode, various editing commands
4626(typically bound to one or more control characters) cause immediate actions
4627without waiting for a newline.
4628Several editing commands are bound to particular
4629control characters when the shell is invoked; these bindings can be changed
4630using the
4631.Ic bind
4632command.
4633.Pp
4634The following is a list of available editing commands.
4635Each description starts with the name of the command,
4636suffixed with a colon;
4637an
4638.Op Ar n
4639(if the command can be prefixed with a count); and any keys the command is
4640bound to by default, written using caret notation
4641e.g. the ASCII ESC character is written as ^[.
4642^[A-Z] sequences are not case sensitive.
4643A count prefix for a command is entered using the sequence
4644.Pf ^[ Ar n ,
4645where
4646.Ar n
4647is a sequence of 1 or more digits.
4648Unless otherwise specified, if a count is
4649omitted, it defaults to 1.
4650.Pp
4651Note that editing command names are used only with the
4652.Ic bind
4653command.
4654Furthermore, many editing commands are useful only on terminals with
4655a visible cursor.
4656The default bindings were chosen to resemble corresponding
4657Emacs key bindings.
4658The user's
4659.Xr tty 4
4660characters (e.g.\&
4661.Dv ERASE )
4662are bound to
4663reasonable substitutes and override the default bindings.
4664.Bl -tag -width Ds
4665.It abort: ^C, ^G
4666Useful as a response to a request for a
4667.Ic search-history
4668pattern in order to abort the search.
4669.It auto-insert: Op Ar n
4670Simply causes the character to appear as literal input.
4671Most ordinary characters are bound to this.
4672.It Xo backward-char:
4673.Op Ar n
4674.No ^B , ^X^D
4675.Xc
4676Moves the cursor backward
4677.Ar n
4678characters.
4679.It Xo backward-word:
4680.Op Ar n
4681.No ^[b
4682.Xc
4683Moves the cursor backward to the beginning of the word; words consist of
4684alphanumerics, underscore
4685.Pq Sq _ ,
4686and dollar sign
4687.Pq Sq $
4688characters.
4689.It beginning-of-history: ^[<
4690Moves to the beginning of the history.
4691.It beginning-of-line: ^A
4692Moves the cursor to the beginning of the edited input line.
4693.It Xo capitalize-word:
4694.Op Ar n
4695.No ^[C , ^[c
4696.Xc
4697Uppercase the first character in the next
4698.Ar n
4699words, leaving the cursor past the end of the last word.
4700.It clear-screen: ^L
4701Clears the screen if the
4702.Ev TERM
4703parameter is set and the terminal supports clearing the screen, then
4704reprints the prompt string and the current input line.
4705.It comment: ^[#
4706If the current line does not begin with a comment character, one is added at
4707the beginning of the line and the line is entered (as if return had been
4708pressed); otherwise, the existing comment characters are removed and the cursor
4709is placed at the beginning of the line.
4710.It complete: ^[^[
4711Automatically completes as much as is unique of the command name or the file
4712name containing the cursor.
4713If the entire remaining command or file name is
4714unique, a space is printed after its completion, unless it is a directory name
4715in which case
4716.Ql /
4717is appended.
4718If there is no command or file name with the current partial word
4719as its prefix, a bell character is output (usually causing a beep to be
4720sounded).
4721.Pp
4722Custom completions may be configured by creating an array named
4723.Ql complete_command ,
4724optionally suffixed with an argument number to complete only for a single
4725argument.
4726So defining an array named
4727.Ql complete_kill
4728provides possible completions for any argument to the
4729.Xr kill 1
4730command, but
4731.Ql complete_kill_1
4732only completes the first argument.
4733For example, the following command makes
4734.Nm
4735offer a selection of signal names for the first argument to
4736.Xr kill 1 :
4737.Pp
4738.Dl set -A complete_kill_1 -- -9 -HUP -INFO -KILL -TERM
4739.It complete-command: ^X^[
4740Automatically completes as much as is unique of the command name having the
4741partial word up to the cursor as its prefix, as in the
4742.Ic complete
4743command above.
4744.It complete-file: ^[^X
4745Automatically completes as much as is unique of the file name having the
4746partial word up to the cursor as its prefix, as in the
4747.Ic complete
4748command described above.
4749.It complete-list: ^I, ^[=
4750Complete as much as is possible of the current word,
4751and list the possible completions for it.
4752If only one completion is possible,
4753match as in the
4754.Ic complete
4755command above.
4756.It Xo delete-char-backward:
4757.Op Ar n
4758.No ERASE , ^? , ^H
4759.Xc
4760Deletes
4761.Ar n
4762characters before the cursor.
4763.It Xo delete-char-forward:
4764.Op Ar n
4765.No Delete
4766.Xc
4767Deletes
4768.Ar n
4769characters after the cursor.
4770.It Xo delete-word-backward:
4771.Op Ar n
4772.No WERASE , ^[ERASE , ^W, ^[^? , ^[^H , ^[h
4773.Xc
4774Deletes
4775.Ar n
4776words before the cursor.
4777.It Xo delete-word-forward:
4778.Op Ar n
4779.No ^[d
4780.Xc
4781Deletes
4782.Ar n
4783words after the cursor.
4784.It Xo down-history:
4785.Op Ar n
4786.No ^N , ^XB
4787.Xc
4788Scrolls the history buffer forward
4789.Ar n
4790lines (later).
4791Each input line originally starts just after the last entry
4792in the history buffer, so
4793.Ic down-history
4794is not useful until either
4795.Ic search-history
4796or
4797.Ic up-history
4798has been performed.
4799.It Xo downcase-word:
4800.Op Ar n
4801.No ^[L , ^[l
4802.Xc
4803Lowercases the next
4804.Ar n
4805words.
4806.It end-of-history: ^[>
4807Moves to the end of the history.
4808.It end-of-line: ^E
4809Moves the cursor to the end of the input line.
4810.It eot: ^_
4811Acts as an end-of-file; this is useful because edit-mode input disables
4812normal terminal input canonicalization.
4813.It Xo eot-or-delete:
4814.Op Ar n
4815.No ^D
4816.Xc
4817Acts as
4818.Ic eot
4819if alone on a line; otherwise acts as
4820.Ic delete-char-forward .
4821.It error:
4822Error (ring the bell).
4823.It exchange-point-and-mark: ^X^X
4824Places the cursor where the mark is and sets the mark to where the cursor was.
4825.It expand-file: ^[*
4826Appends a
4827.Ql *
4828to the current word and replaces the word with the result of performing file
4829globbing on the word.
4830If no files match the pattern, the bell is rung.
4831.It Xo forward-char:
4832.Op Ar n
4833.No ^F , ^XC
4834.Xc
4835Moves the cursor forward
4836.Ar n
4837characters.
4838.It Xo forward-word:
4839.Op Ar n
4840.No ^[f
4841.Xc
4842Moves the cursor forward to the end of the
4843.Ar n Ns th
4844word.
4845.It Xo goto-history:
4846.Op Ar n
4847.No ^[g
4848.Xc
4849Goes to history number
4850.Ar n .
4851.It kill-line: KILL
4852Deletes the entire input line.
4853.It Xo kill-to-eol:
4854.Op Ar n
4855.No ^K
4856.Xc
4857Deletes the input from the cursor to the end of the line if
4858.Ar n
4859is not specified; otherwise deletes characters between the cursor and column
4860.Ar n .
4861.It list: ^[?
4862Prints a sorted, columnated list of command names or file names (if any) that
4863can complete the partial word containing the cursor.
4864Directory names have
4865.Ql /
4866appended to them.
4867.It list-command: ^X?
4868Prints a sorted, columnated list of command names (if any) that can complete
4869the partial word containing the cursor.
4870.It list-file: ^X^Y
4871Prints a sorted, columnated list of file names (if any) that can complete the
4872partial word containing the cursor.
4873File type indicators are appended as described under
4874.Ic list
4875above.
4876.It newline: ^J , ^M
4877Causes the current input line to be processed by the shell.
4878The current cursor position may be anywhere on the line.
4879.It newline-and-next: ^O
4880Causes the current input line to be processed by the shell, and the next line
4881from history becomes the current line.
4882This is only useful after an
4883.Ic up-history
4884or
4885.Ic search-history .
4886.It no-op: QUIT
4887This does nothing.
4888.It Xo prev-hist-word:
4889.Op Ar n
4890.No ^[. , ^[_
4891.Xc
4892The last
4893.Pq Ar n Ns th
4894word of the previous command is inserted at the cursor.
4895.It quote: ^^
4896The following character is taken literally rather than as an editing command.
4897.It redraw:
4898Reprints the prompt string and the current input line.
4899.It Xo search-character-backward:
4900.Op Ar n
4901.No ^[^]
4902.Xc
4903Search backward in the current line for the
4904.Ar n Ns th
4905occurrence of the next character typed.
4906.It Xo search-character-forward:
4907.Op Ar n
4908.No ^]
4909.Xc
4910Search forward in the current line for the
4911.Ar n Ns th
4912occurrence of the next character typed.
4913.It search-history: ^R
4914Enter incremental search mode.
4915The internal history list is searched
4916backwards for commands matching the input.
4917An initial
4918.Ql ^
4919in the search string anchors the search.
4920The abort key will leave search mode.
4921Other commands will be executed after leaving search mode.
4922Successive
4923.Ic search-history
4924commands continue searching backward to the next previous occurrence of the
4925pattern.
4926The history buffer retains only a finite number of lines; the oldest
4927are discarded as necessary.
4928.It set-mark-command: ^[ Ns Aq space
4929Set the mark at the cursor position.
4930.It transpose-chars: ^T
4931If at the end of line, or if the
4932.Ic gmacs
4933option is set, this exchanges the two previous characters; otherwise, it
4934exchanges the previous and current characters and moves the cursor one
4935character to the right.
4936.It Xo up-history:
4937.Op Ar n
4938.No ^P , ^XA
4939.Xc
4940Scrolls the history buffer backward
4941.Ar n
4942lines (earlier).
4943.It Xo upcase-word:
4944.Op Ar n
4945.No ^[U , ^[u
4946.Xc
4947Uppercase the next
4948.Ar n
4949words.
4950.It quote: ^V
4951Synonym for ^^.
4952.It yank: ^Y
4953Inserts the most recently killed text string at the current cursor position.
4954.It yank-pop: ^[y
4955Immediately after a
4956.Ic yank ,
4957replaces the inserted text string with the next previously killed text string.
4958.El
4959.Pp
4960The following editing commands lack default bindings but can be used with the
4961.Ic bind
4962command:
4963.Bl -tag -width Ds
4964.It kill-region
4965Deletes the input between the cursor and the mark.
4966.El
4967.Ss Vi editing mode
4968The vi command-line editor in
4969.Nm
4970has basically the same commands as the
4971.Xr vi 1
4972editor with the following exceptions:
4973.Bl -bullet
4974.It
4975You start out in insert mode.
4976.It
4977There are file name and command completion commands:
4978=, \e, *, ^X, ^E, ^F, and, optionally,
4979.Aq tab
4980and
4981.Aq esc .
4982.It
4983The
4984.Ic _
4985command is different (in
4986.Nm
4987it is the last argument command; in
4988.Xr vi 1
4989it goes to the start of the current line).
4990.It
4991The
4992.Ic /
4993and
4994.Ic G
4995commands move in the opposite direction to the
4996.Ic j
4997command.
4998.It
4999Commands which don't make sense in a single line editor are not available
5000(e.g. screen movement commands and
5001.Xr ex 1 Ns -style
5002colon
5003.Pq Ic \&:
5004commands).
5005.El
5006.Pp
5007Note that the ^X stands for control-X; also
5008.Aq esc ,
5009.Aq space ,
5010and
5011.Aq tab
5012are used for escape, space, and tab, respectively (no kidding).
5013.Pp
5014Like
5015.Xr vi 1 ,
5016there are two modes:
5017.Dq insert
5018mode and
5019.Dq command
5020mode.
5021In insert mode, most characters are simply put in the buffer at the
5022current cursor position as they are typed; however, some characters are
5023treated specially.
5024In particular, the following characters are taken from current
5025.Xr tty 4
5026settings
5027(see
5028.Xr stty 1 )
5029and have their usual meaning (normal values are in parentheses): kill (^U),
5030erase (^?), werase (^W), eof (^D), intr (^C), and quit (^\e).
5031In addition to
5032the above, the following characters are also treated specially in insert mode:
5033.Bl -tag -width 10n
5034.It ^E
5035Command and file name enumeration (see below).
5036.It ^F
5037Command and file name completion (see below).
5038If used twice in a row, the
5039list of possible completions is displayed; if used a third time, the completion
5040is undone.
5041.It ^H
5042Erases previous character.
5043.It ^J | ^M
5044End of line.
5045The current line is read, parsed, and executed by the shell.
5046.It ^L
5047Clear the screen (if possible) and redraw the current line.
5048See the
5049.Em clear-screen
5050command in
5051.Sx Emacs editing mode
5052for more information.
5053.It ^R
5054Redraw the current line.
5055.It ^V
5056Literal next.
5057The next character typed is not treated specially (can be used
5058to insert the characters being described here).
5059.It ^X
5060Command and file name expansion (see below).
5061.It Aq esc
5062Puts the editor in command mode (see below).
5063.It Aq tab
5064Optional file name and command completion (see
5065.Ic ^F
5066above), enabled with
5067.Ic set -o vi-tabcomplete .
5068.El
5069.Pp
5070In command mode, each character is interpreted as a command.
5071Characters that
5072don't correspond to commands, are illegal combinations of commands, or are
5073commands that can't be carried out, all cause beeps.
5074In the following command descriptions, an
5075.Op Ar n
5076indicates the command may be prefixed by a number (e.g.\&
5077.Ic 10l
5078moves right 10 characters); if no number prefix is used,
5079.Ar n
5080is assumed to be 1 unless otherwise specified.
5081The term
5082.Dq current position
5083refers to the position between the cursor and the character preceding the
5084cursor.
5085A
5086.Dq word
5087is a sequence of letters, digits, and underscore characters or a sequence of
5088non-letter, non-digit, non-underscore, and non-whitespace characters (e.g.\&
5089.Dq ab2*&^
5090contains two words) and a
5091.Dq big-word
5092is a sequence of non-whitespace characters.
5093.Pp
5094Special
5095.Nm
5096vi commands:
5097.Pp
5098The following commands are not in, or are different from, the normal vi file
5099editor:
5100.Bl -tag -width 10n
5101.It Xo
5102.Oo Ar n Oc Ns _
5103.Xc
5104Insert a space followed by the
5105.Ar n Ns th
5106big-word from the last command in the history at the current position and enter
5107insert mode; if
5108.Ar n
5109is not specified, the last word is inserted.
5110.It #
5111Insert the comment character
5112.Pq Sq #
5113at the start of the current line and return the line to the shell (equivalent
5114to
5115.Ic I#^J ) .
5116.It Xo
5117.Oo Ar n Oc Ns g
5118.Xc
5119Like
5120.Ic G ,
5121except if
5122.Ar n
5123is not specified, it goes to the most recent remembered line.
5124.It Xo
5125.Oo Ar n Oc Ns v
5126.Xc
5127Edit line
5128.Ar n
5129using the
5130.Xr vi 1
5131editor; if
5132.Ar n
5133is not specified, the current line is edited.
5134The actual command executed is
5135.Ic fc -e ${VISUAL:-${EDITOR:-vi}} Ar n .
5136.It * and ^X
5137Command or file name expansion is applied to the current big-word (with an
5138appended
5139.Ql *
5140if the word contains no file globbing characters) \- the big-word is replaced
5141with the resulting words.
5142If the current big-word is the first on the line
5143or follows one of the characters
5144.Ql \&; ,
5145.Ql | ,
5146.Ql & ,
5147.Ql \&( ,
5148or
5149.Ql \&) ,
5150and does not contain a slash
5151.Pq Sq / ,
5152then command expansion is done; otherwise file name expansion is done.
5153Command expansion will match the big-word against all aliases, functions, and
5154built-in commands as well as any executable files found by searching the
5155directories in the
5156.Ev PATH
5157parameter.
5158File name expansion matches the big-word against the files in the
5159current directory.
5160After expansion, the cursor is placed just past the last
5161word and the editor is in insert mode.
5162.It Xo
5163.Oo Ar n Oc Ns \e ,
5164.Oo Ar n Oc Ns ^F ,
5165.Oo Ar n Oc Ns Aq tab ,
5166.No and
5167.Oo Ar n Oc Ns Aq esc
5168.Xc
5169Command/file name completion.
5170Replace the current big-word with the
5171longest unique match obtained after performing command and file name expansion.
5172.Aq tab
5173is only recognized if the
5174.Ic vi-tabcomplete
5175option is set, while
5176.Aq esc
5177is only recognized if the
5178.Ic vi-esccomplete
5179option is set (see
5180.Ic set -o ) .
5181If
5182.Ar n
5183is specified, the
5184.Ar n Ns th
5185possible completion is selected (as reported by the command/file name
5186enumeration command).
5187.It = and ^E
5188Command/file name enumeration.
5189List all the commands or files that match the current big-word.
5190.It @ Ns Ar c
5191Macro expansion.
5192Execute the commands found in the alias
5193.No _ Ns Ar c .
5194.El
5195.Pp
5196Intra-line movement commands:
5197.Bl -tag -width Ds
5198.It Xo
5199.Oo Ar n Oc Ns h and
5200.Oo Ar n Oc Ns ^H
5201.Xc
5202Move left
5203.Ar n
5204characters.
5205.It Xo
5206.Oo Ar n Oc Ns l and
5207.Oo Ar n Oc Ns Aq space
5208.Xc
5209Move right
5210.Ar n
5211characters.
5212.It 0
5213Move to column 0.
5214.It ^
5215Move to the first non-whitespace character.
5216.It Xo
5217.Oo Ar n Oc Ns |
5218.Xc
5219Move to column
5220.Ar n .
5221.It $
5222Move to the last character.
5223.It Xo
5224.Oo Ar n Oc Ns b
5225.Xc
5226Move back
5227.Ar n
5228words.
5229.It Xo
5230.Oo Ar n Oc Ns B
5231.Xc
5232Move back
5233.Ar n
5234big-words.
5235.It Xo
5236.Oo Ar n Oc Ns e
5237.Xc
5238Move forward to the end of the word,
5239.Ar n
5240times.
5241.It Xo
5242.Oo Ar n Oc Ns E
5243.Xc
5244Move forward to the end of the big-word,
5245.Ar n
5246times.
5247.It Xo
5248.Oo Ar n Oc Ns w
5249.Xc
5250Move forward
5251.Ar n
5252words.
5253.It Xo
5254.Oo Ar n Oc Ns W
5255.Xc
5256Move forward
5257.Ar n
5258big-words.
5259.It %
5260Find match.
5261The editor looks forward for the nearest parenthesis, bracket, or
5262brace and then moves the cursor to the matching parenthesis, bracket, or brace.
5263.It Xo
5264.Oo Ar n Oc Ns f Ns Ar c
5265.Xc
5266Move forward to the
5267.Ar n Ns th
5268occurrence of the character
5269.Ar c .
5270.It Xo
5271.Oo Ar n Oc Ns F Ns Ar c
5272.Xc
5273Move backward to the
5274.Ar n Ns th
5275occurrence of the character
5276.Ar c .
5277.It Xo
5278.Oo Ar n Oc Ns t Ns Ar c
5279.Xc
5280Move forward to just before the
5281.Ar n Ns th
5282occurrence of the character
5283.Ar c .
5284.It Xo
5285.Oo Ar n Oc Ns T Ns Ar c
5286.Xc
5287Move backward to just before the
5288.Ar n Ns th
5289occurrence of the character
5290.Ar c .
5291.It Xo
5292.Oo Ar n Oc Ns \&;
5293.Xc
5294Repeats the last
5295.Ic f , F , t ,
5296or
5297.Ic T
5298command.
5299.It Xo
5300.Oo Ar n Oc Ns \&,
5301.Xc
5302Repeats the last
5303.Ic f , F , t ,
5304or
5305.Ic T
5306command, but moves in the opposite direction.
5307.El
5308.Pp
5309Inter-line movement commands:
5310.Bl -tag -width Ds
5311.It Xo
5312.Oo Ar n Oc Ns j ,
5313.Oo Ar n Oc Ns + ,
5314.No and
5315.Oo Ar n Oc Ns ^N
5316.Xc
5317Move to the
5318.Ar n Ns th
5319next line in the history.
5320.It Xo
5321.Oo Ar n Oc Ns k ,
5322.Oo Ar n Oc Ns - ,
5323.No and
5324.Oo Ar n Oc Ns ^P
5325.Xc
5326Move to the
5327.Ar n Ns th
5328previous line in the history.
5329.It Xo
5330.Oo Ar n Oc Ns G
5331.Xc
5332Move to line
5333.Ar n
5334in the history; if
5335.Ar n
5336is not specified, the number of the first remembered line is used.
5337.It Xo
5338.Oo Ar n Oc Ns g
5339.Xc
5340Like
5341.Ic G ,
5342except if
5343.Ar n
5344is not specified, it goes to the most recent remembered line.
5345.It Xo
5346.Oo Ar n Oc Ns / Ns Ar string
5347.Xc
5348Search backward through the history for the
5349.Ar n Ns th
5350line containing
5351.Ar string ;
5352if
5353.Ar string
5354starts with
5355.Ql ^ ,
5356the remainder of the string must appear at the start of the history line for
5357it to match.
5358.It Xo
5359.Oo Ar n Oc Ns \&? Ns Ar string
5360.Xc
5361Same as
5362.Ic / ,
5363except it searches forward through the history.
5364.It Xo
5365.Oo Ar n Oc Ns n
5366.Xc
5367Search for the
5368.Ar n Ns th
5369occurrence of the last search string;
5370the direction of the search is the same as the last search.
5371.It Xo
5372.Oo Ar n Oc Ns N
5373.Xc
5374Search for the
5375.Ar n Ns th
5376occurrence of the last search string;
5377the direction of the search is the opposite of the last search.
5378.El
5379.Pp
5380Edit commands
5381.Bl -tag -width Ds
5382.It Xo
5383.Oo Ar n Oc Ns a
5384.Xc
5385Append text
5386.Ar n
5387times; goes into insert mode just after the current position.
5388The append is
5389only replicated if command mode is re-entered i.e.\&
5390.Aq esc
5391is used.
5392.It Xo
5393.Oo Ar n Oc Ns A
5394.Xc
5395Same as
5396.Ic a ,
5397except it appends at the end of the line.
5398.It Xo
5399.Oo Ar n Oc Ns i
5400.Xc
5401Insert text
5402.Ar n
5403times; goes into insert mode at the current position.
5404The insertion is only
5405replicated if command mode is re-entered i.e.\&
5406.Aq esc
5407is used.
5408.It Xo
5409.Oo Ar n Oc Ns I
5410.Xc
5411Same as
5412.Ic i ,
5413except the insertion is done just before the first non-blank character.
5414.It Xo
5415.Oo Ar n Oc Ns s
5416.Xc
5417Substitute the next
5418.Ar n
5419characters (i.e. delete the characters and go into insert mode).
5420.It S
5421Substitute whole line.
5422All characters from the first non-blank character to the
5423end of the line are deleted and insert mode is entered.
5424.It Xo
5425.Oo Ar n Oc Ns c Ns Ar move-cmd
5426.Xc
5427Change from the current position to the position resulting from
5428.Ar n move-cmd Ns s
5429(i.e. delete the indicated region and go into insert mode); if
5430.Ar move-cmd
5431is
5432.Ic c ,
5433the line starting from the first non-blank character is changed.
5434.It C
5435Change from the current position to the end of the line (i.e. delete to the
5436end of the line and go into insert mode).
5437.It Xo
5438.Oo Ar n Oc Ns x
5439.Xc
5440Delete the next
5441.Ar n
5442characters.
5443.It Xo
5444.Oo Ar n Oc Ns X
5445.Xc
5446Delete the previous
5447.Ar n
5448characters.
5449.It D
5450Delete to the end of the line.
5451.It Xo
5452.Oo Ar n Oc Ns d Ns Ar move-cmd
5453.Xc
5454Delete from the current position to the position resulting from
5455.Ar n move-cmd Ns s ;
5456.Ar move-cmd
5457is a movement command (see above) or
5458.Ic d ,
5459in which case the current line is deleted.
5460.It Xo
5461.Oo Ar n Oc Ns r Ns Ar c
5462.Xc
5463Replace the next
5464.Ar n
5465characters with the character
5466.Ar c .
5467.It Xo
5468.Oo Ar n Oc Ns R
5469.Xc
5470Replace.
5471Enter insert mode but overwrite existing characters instead of
5472inserting before existing characters.
5473The replacement is repeated
5474.Ar n
5475times.
5476.It Xo
5477.Oo Ar n Oc Ns ~
5478.Xc
5479Change the case of the next
5480.Ar n
5481characters.
5482.It Xo
5483.Oo Ar n Oc Ns y Ns Ar move-cmd
5484.Xc
5485Yank from the current position to the position resulting from
5486.Ar n move-cmd Ns s
5487into the yank buffer; if
5488.Ar move-cmd
5489is
5490.Ic y ,
5491the whole line is yanked.
5492.It Y
5493Yank from the current position to the end of the line.
5494.It Xo
5495.Oo Ar n Oc Ns p
5496.Xc
5497Paste the contents of the yank buffer just after the current position,
5498.Ar n
5499times.
5500.It Xo
5501.Oo Ar n Oc Ns P
5502.Xc
5503Same as
5504.Ic p ,
5505except the buffer is pasted at the current position.
5506.El
5507.Pp
5508Miscellaneous vi commands
5509.Bl -tag -width Ds
5510.It ^J and ^M
5511The current line is read, parsed, and executed by the shell.
5512.It ^L
5513Clear the screen (if possible) and redraw the current line.
5514.It ^R
5515Redraw the current line.
5516.It Xo
5517.Oo Ar n Oc Ns \&.
5518.Xc
5519Redo the last edit command
5520.Ar n
5521times.
5522.It u
5523Undo the last edit command.
5524.It U
5525Undo all changes that have been made to the current line.
5526.It Ar intr No and Ar quit
5527The interrupt and quit terminal characters cause the current line to be
5528deleted and a new prompt to be printed.
5529.El
5530.Sh FILES
5531.Bl -tag -width "/etc/suid_profileXX" -compact
5532.It Pa ~/.profile
5533User's login profile.
5534.It Pa /etc/ksh.kshrc
5535Global configuration file.
5536Not sourced by default.
5537.It Pa /etc/profile
5538System login profile.
5539.It Pa /etc/shells
5540Shell database.
5541.It Pa /etc/suid_profile
5542Privileged shell profile.
5543.El
5544.Sh SEE ALSO
5545.Xr csh 1 ,
5546.Xr ed 1 ,
5547.Xr mg 1 ,
5548.Xr sh 1 ,
5549.Xr stty 1 ,
5550.Xr vi 1 ,
5551.Xr shells 5 ,
5552.Xr environ 7 ,
5553.Xr script 7
5554.Rs
5555.%A S. R. Bourne
5556.%T The UNIX Shell
5557.%J Bell System Technical Journal
5558.%V 57:6
5559.%P pp. 1971-1990
5560.%D 1978
5561.Re
5562.Rs
5563.\" 4.4BSD USD:3
5564.%A S. R. Bourne
5565.%T \&An Introduction to the UNIX Shell
5566.%I AT&T Bell Laboratories
5567.%R Computing Science Technical Report
5568.%N 70
5569.%D 1978
5570.Re
5571.Rs
5572.%A Morris Bolsky
5573.%A David Korn
5574.%B The KornShell Command and Programming Language
5575.%D First Edition 1989
5576.%I Prentice Hall
5577.%O ISBN 0135169720
5578.\" The second edition of the above book (1995) is about ksh93,
5579.\" but the OpenBSD ksh is a descendant from ksh88 via pdksh.
5580.Re
5581.Rs
5582.%A Stephen G. Kochan
5583.%A Patrick H. Wood
5584.%B UNIX Shell Programming, 3rd Edition
5585.%D 2003
5586.%I Sams
5587.%O ISBN 0672324903
5588.Re
5589.Rs
5590.%A IEEE Inc.
5591.%D 1993
5592.%O ISBN 1-55937-266-9
5593.%T IEEE Standard for Information Technology \- Portable Operating \
5594    System Interface (POSIX) \- Part 2: Shell and Utilities
5595.Re
5596.Sh VERSION
5597This page documents version @(#)PD KSH v5.2.14 99/07/13.2 of the public
5598domain Korn shell.
5599.Sh AUTHORS
5600.An -nosplit
5601This shell is based on the public domain 7th edition Bourne shell clone by
5602.An Charles Forsyth
5603and parts of the BRL shell by
5604.An Doug A. Gwyn ,
5605.An Doug Kingston ,
5606.An Ron Natalie ,
5607.An Arnold Robbins ,
5608.An Lou Salkind ,
5609and others.
5610The first release of
5611.Nm pdksh
5612was created by
5613.An Eric Gisin ,
5614and it was subsequently maintained by
5615.An John R. MacMillan Aq Mt change!john@sq.sq.com ,
5616.An Simon J. Gerraty Aq Mt sjg@zen.void.oz.au ,
5617and
5618.An Michael Rendell Aq Mt michael@cs.mun.ca .
5619The
5620.Pa CONTRIBUTORS
5621file in the source distribution contains a more complete list of people and
5622their part in the shell's development.
5623.Sh BUGS
5624.Pf $( Ar command )
5625expressions are currently parsed by finding the closest matching (unquoted)
5626parenthesis.
5627Thus constructs inside
5628.Pf $( Ar command )
5629may produce an error.
5630For example, the parenthesis in
5631.Ql x);;
5632is interpreted as the closing parenthesis in
5633.Ql $(case x in x);; *);; esac) .
5634