loksh-noxz

[fork] a Linux port of OpenBSD's ksh
git clone https://noxz.tech/git/loksh-noxz.git
Log | Files | README

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