1.\" $MirOS: src/bin/mksh/mksh.1,v 1.463 2019/03/01 16:17:31 tg Exp $
2.\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $
3.\"-
4.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5.\"		2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
6.\"		2018, 2019
7.\"	mirabilos <m@mirbsd.org>
8.\"
9.\" Provided that these terms and disclaimer and all copyright notices
10.\" are retained or reproduced in an accompanying document, permission
11.\" is granted to deal in this work without restriction, including un‐
12.\" limited rights to use, publicly perform, distribute, sell, modify,
13.\" merge, give away, or sublicence.
14.\"
15.\" This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
16.\" the utmost extent permitted by applicable law, neither express nor
17.\" implied; without malicious intent or gross negligence. In no event
18.\" may a licensor, author or contributor be held liable for indirect,
19.\" direct, other damage, loss, or other issues arising in any way out
20.\" of dealing in the work, even if advised of the possibility of such
21.\" damage or existence of a defect, except proven that it results out
22.\" of said person’s immediate fault when using the work as intended.
23.\"-
24.\" Try to make GNU groff and AT&T nroff more compatible
25.\" * ` generates ‘ in gnroff, so use \`
26.\" * ' generates ’ in gnroff, \' generates ´, so use \*(aq
27.\" * - generates ‐ in gnroff, \- generates −, so .tr it to -
28.\"   thus use - for hyphens and \- for minus signs and option dashes
29.\" * ~ is size-reduced and placed atop in groff, so use \*(TI
30.\" * ^ is size-reduced and placed atop in groff, so use \*(ha
31.\" * \(en does not work in nroff, so use \*(en
32.\" * <>| are problematic, so redefine and use \*(Lt\*(Gt\*(Ba
33.\" Also make sure to use \& *before* a punctuation char that is to not
34.\" be interpreted as punctuation, and especially with two-letter words
35.\" but also (after) a period that does not end a sentence (“e.g.\&”).
36.\" The section after the "doc" macropackage has been loaded contains
37.\" additional code to convene between the UCB mdoc macropackage (and
38.\" its variant as BSD mdoc in groff) and the GNU mdoc macropackage.
39.\"
40.ie \n(.g \{\
41.	if \*[.T]ascii .tr \-\N'45'
42.	if \*[.T]latin1 .tr \-\N'45'
43.	if \*[.T]utf8 .tr \-\N'45'
44.	ds <= \[<=]
45.	ds >= \[>=]
46.	ds Rq \[rq]
47.	ds Lq \[lq]
48.	ds sL \(aq
49.	ds sR \(aq
50.	if \*[.T]utf8 .ds sL `
51.	if \*[.T]ps .ds sL `
52.	if \*[.T]utf8 .ds sR '
53.	if \*[.T]ps .ds sR '
54.	ds aq \(aq
55.	ds TI \(ti
56.	ds ha \(ha
57.	ds en \(en
58.\}
59.el \{\
60.	ds aq '
61.	ds TI ~
62.	ds ha ^
63.	ds en \(em
64.\}
65.\"
66.\" Implement .Dd with the Mdocdate RCS keyword
67.\"
68.rn Dd xD
69.de Dd
70.ie \\$1$Mdocdate: \{\
71.	xD \\$2 \\$3, \\$4
72.\}
73.el .xD \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8
74..
75.\"
76.\" .Dd must come before definition of .Mx, because when called
77.\" with -mandoc, it might implement .Mx itself, but we want to
78.\" use our own definition. And .Dd must come *first*, always.
79.\"
80.Dd $Mdocdate: March 1 2019 $
81.\"
82.\" Check which macro package we use, and do other -mdoc setup.
83.\"
84.ie \n(.g \{\
85.	if \*[.T]utf8 .tr \[la]\*(Lt
86.	if \*[.T]utf8 .tr \[ra]\*(Gt
87.	ie d volume-ds-1 .ds tT gnu
88.	el .ie d doc-volume-ds-1 .ds tT gnp
89.	el .ds tT bsd
90.\}
91.el .ds tT ucb
92.\"
93.\" Implement .Mx (MirBSD)
94.\"
95.ie "\*(tT"gnu" \{\
96.	eo
97.	de Mx
98.	nr curr-font \n[.f]
99.	nr curr-size \n[.ps]
100.	ds str-Mx \f[\n[curr-font]]\s[\n[curr-size]u]
101.	ds str-Mx1 \*[Tn-font-size]\%MirBSD\*[str-Mx]
102.	if !\n[arg-limit] \
103.	if \n[.$] \{\
104.	ds macro-name Mx
105.	parse-args \$@
106.	\}
107.	if (\n[arg-limit] > \n[arg-ptr]) \{\
108.	nr arg-ptr +1
109.	ie (\n[type\n[arg-ptr]] == 2) \
110.	as str-Mx1 \~\*[arg\n[arg-ptr]]
111.	el \
112.	nr arg-ptr -1
113.	\}
114.	ds arg\n[arg-ptr] "\*[str-Mx1]
115.	nr type\n[arg-ptr] 2
116.	ds space\n[arg-ptr] "\*[space]
117.	nr num-args (\n[arg-limit] - \n[arg-ptr])
118.	nr arg-limit \n[arg-ptr]
119.	if \n[num-args] \
120.	parse-space-vector
121.	print-recursive
122..
123.	ec
124.	ds sP \s0
125.	ds tN \*[Tn-font-size]
126.\}
127.el .ie "\*(tT"gnp" \{\
128.	eo
129.	de Mx
130.	nr doc-curr-font \n[.f]
131.	nr doc-curr-size \n[.ps]
132.	ds doc-str-Mx \f[\n[doc-curr-font]]\s[\n[doc-curr-size]u]
133.	ds doc-str-Mx1 \*[doc-Tn-font-size]\%MirBSD\*[doc-str-Mx]
134.	if !\n[doc-arg-limit] \
135.	if \n[.$] \{\
136.	ds doc-macro-name Mx
137.	doc-parse-args \$@
138.	\}
139.	if (\n[doc-arg-limit] > \n[doc-arg-ptr]) \{\
140.	nr doc-arg-ptr +1
141.	ie (\n[doc-type\n[doc-arg-ptr]] == 2) \
142.	as doc-str-Mx1 \~\*[doc-arg\n[doc-arg-ptr]]
143.	el \
144.	nr doc-arg-ptr -1
145.	\}
146.	ds doc-arg\n[doc-arg-ptr] "\*[doc-str-Mx1]
147.	nr doc-type\n[doc-arg-ptr] 2
148.	ds doc-space\n[doc-arg-ptr] "\*[doc-space]
149.	nr doc-num-args (\n[doc-arg-limit] - \n[doc-arg-ptr])
150.	nr doc-arg-limit \n[doc-arg-ptr]
151.	if \n[doc-num-args] \
152.	doc-parse-space-vector
153.	doc-print-recursive
154..
155.	ec
156.	ds sP \s0
157.	ds tN \*[doc-Tn-font-size]
158.\}
159.el \{\
160.	de Mx
161.	nr cF \\n(.f
162.	nr cZ \\n(.s
163.	ds aa \&\f\\n(cF\s\\n(cZ
164.	if \\n(aC==0 \{\
165.		ie \\n(.$==0 \&MirBSD\\*(aa
166.		el .aV \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
167.	\}
168.	if \\n(aC>\\n(aP \{\
169.		nr aP \\n(aP+1
170.		ie \\n(C\\n(aP==2 \{\
171.			as b1 \&MirBSD\ #\&\\*(A\\n(aP\\*(aa
172.			ie \\n(aC>\\n(aP \{\
173.				nr aP \\n(aP+1
174.				nR
175.			\}
176.			el .aZ
177.		\}
178.		el \{\
179.			as b1 \&MirBSD\\*(aa
180.			nR
181.		\}
182.	\}
183..
184.\}
185.\"-
186.Dt MKSH 1
187.Os MirBSD
188.Sh NAME
189.Nm mksh ,
190.Nm sh
191.Nd MirBSD Korn shell
192.Sh SYNOPSIS
193.Nm
194.Bk -words
195.Op Fl +abCefhiklmnprUuvXx
196.Oo
197.Fl T Oo Ar \&! Oc Ns Ar tty
198\*(Ba
199.Ar \&\-
200.Oc
201.Op Fl +o Ar option
202.Oo
203.Fl c Ar string \*(Ba
204.Fl s \*(Ba
205.Ar file
206.Op Ar argument ...
207.Oc
208.Ek
209.Nm builtin-name
210.Op Ar argument ...
211.Sh DESCRIPTION
212.Nm
213is a command interpreter intended for both interactive and shell
214script use.
215Its command language is a superset of the
216.Xr sh C
217shell language and largely compatible to the original Korn shell.
218At times, this manual page may give scripting advice; while it
219sometimes does take portable shell scripting or various standards
220into account all information is first and foremost presented with
221.Nm
222in mind and should be taken as such.
223.Ss I use Android, OS/2, etc. so what...?
224Please see the FAQ at the end of this document.
225.Ss Invocation
226Most builtins can be called directly, for example if a link points from its
227name to the shell; not all make sense, have been tested or work at all though.
228.Pp
229The options are as follows:
230.Bl -tag -width XcXstring
231.It Fl c Ar string
232.Nm
233will execute the command(s) contained in
234.Ar string .
235.It Fl i
236Interactive shell.
237A shell that reads commands from standard input is
238.Dq interactive
239if this
240option is used or if both standard input and standard error are attached
241to a
242.Xr tty 4 .
243An interactive shell has job control enabled, ignores the
244.Dv SIGINT ,
245.Dv SIGQUIT
246and
247.Dv SIGTERM
248signals, and prints prompts before reading input (see the
249.Ev PS1
250and
251.Ev PS2
252parameters).
253It also processes the
254.Ev ENV
255parameter or the
256.Pa mkshrc
257file (see below).
258For non-interactive shells, the
259.Ic trackall
260option is on by default (see the
261.Ic set
262command below).
263.It Fl l
264Login shell.
265If the basename the shell is called with (i.e. argv[0])
266starts with
267.Ql \-
268or if this option is used,
269the shell is assumed to be a login shell; see
270.Sx Startup files
271below.
272.It Fl p
273Privileged shell.
274A shell is
275.Dq privileged
276if the real user ID or group ID does not match the
277effective user ID or group ID (see
278.Xr getuid 2
279and
280.Xr getgid 2 ) .
281Clearing the privileged option causes the shell to set
282its effective user ID (group ID) to its real user ID (group ID).
283For further implications, see
284.Sx Startup files .
285If the shell is privileged and this flag is not explicitly set, the
286.Dq privileged
287option is cleared automatically after processing the startup files.
288.It Fl r
289Restricted shell.
290A shell is
291.Dq restricted
292if this
293option is used.
294The following restrictions come into effect after the shell processes any
295profile and
296.Ev ENV
297files:
298.Pp
299.Bl -bullet -compact
300.It
301The
302.Ic cd
303.Po and Ic chdir Pc
304command is disabled.
305.It
306The
307.Ev SHELL ,
308.Ev ENV
309and
310.Ev PATH
311parameters cannot be changed.
312.It
313Command names can't be specified with absolute or relative paths.
314.It
315The
316.Fl p
317option of the built-in command
318.Ic command
319can't be used.
320.It
321Redirections that create files can't be used (i.e.\&
322.Dq Li \*(Gt ,
323.Dq Li \*(Gt\*(Ba ,
324.Dq Li \*(Gt\*(Gt ,
325.Dq Li \*(Lt\*(Gt ) .
326.El
327.It Fl s
328The shell reads commands from standard input; all non-option arguments
329are positional parameters.
330.It Fl T Ar name
331Spawn
332.Nm
333on the
334.Xr tty 4
335device given.
336The paths
337.Ar name ,
338.Pa /dev/ttyC Ns Ar name
339and
340.Pa /dev/tty Ns Ar name
341are attempted in order.
342Unless
343.Ar name
344begins with an exclamation mark
345.Pq Ql \&! ,
346this is done in a subshell and returns immediately.
347If
348.Ar name
349is a dash
350.Pq Ql \&\- ,
351detach from controlling terminal (daemonise) instead.
352.El
353.Pp
354In addition to the above, the options described in the
355.Ic set
356built-in command can also be used on the command line:
357both
358.Op Fl +abCefhkmnuvXx
359and
360.Op Fl +o Ar option
361can be used for single letter or long options, respectively.
362.Pp
363If neither the
364.Fl c
365nor the
366.Fl s
367option is specified, the first non-option argument specifies the name
368of a file the shell reads commands from.
369If there are no non-option
370arguments, the shell reads commands from the standard input.
371The name of the shell (i.e. the contents of $0)
372is determined as follows: if the
373.Fl c
374option is used and there is a non-option argument, it is used as the name;
375if commands are being read from a file, the file is used as the name;
376otherwise, the basename the shell was called with (i.e. argv[0]) is used.
377.Pp
378The exit status of the shell is 127 if the command file specified on the
379command line could not be opened, or non-zero if a fatal syntax error
380occurred during the execution of a script.
381In the absence of fatal errors,
382the exit status is that of the last command executed, or zero if no
383command is executed.
384.Ss Startup files
385For the actual location of these files, see
386.Sx FILES .
387A login shell processes the system profile first.
388A privileged shell then processes the suid profile.
389A non-privileged login shell processes the user profile next.
390A non-privileged interactive shell checks the value of the
391.Ev ENV
392parameter after subjecting it to parameter, command, arithmetic and tilde
393.Pq Ql \*(TI
394substitution; if unset or empty, the user mkshrc profile is processed;
395otherwise, if a file whose name is the substitution result exists,
396it is processed; non-existence is silently ignored.
397A privileged shell then drops privileges if neither was the
398.Fl p
399option given on the command line nor set during execution of the startup files.
400.Ss Command syntax
401The shell begins parsing its input by removing any backslash-newline
402combinations, then breaking it into
403.Em words .
404Words (which are sequences of characters) are delimited by unquoted whitespace
405characters (space, tab and newline) or meta-characters
406.Po
407.Ql \*(Lt ,
408.Ql \*(Gt ,
409.Ql \*(Ba ,
410.Ql \&; ,
411.Ql \&( ,
412.Ql \&)
413and
414.Ql &
415.Pc .
416Aside from delimiting words, spaces and tabs are ignored, while newlines
417usually delimit commands.
418The meta-characters are used in building the following
419.Em tokens :
420.Dq Li \*(Lt ,
421.Dq Li \*(Lt& ,
422.Dq Li \*(Lt\*(Lt ,
423.Dq Li \*(Lt\*(Lt\*(Lt ,
424.Dq Li \*(Gt ,
425.Dq Li \*(Gt& ,
426.Dq Li \*(Gt\*(Gt ,
427.Dq Li &\*(Gt ,
428etc. are used to specify redirections (see
429.Sx Input/output redirection
430below);
431.Dq Li \*(Ba
432is used to create pipelines;
433.Dq Li \*(Ba&
434is used to create co-processes (see
435.Sx Co-processes
436below);
437.Dq Li \&;
438is used to separate commands;
439.Dq Li &
440is used to create asynchronous pipelines;
441.Dq Li &&
442and
443.Dq Li \*(Ba\*(Ba
444are used to specify conditional execution;
445.Dq Li \&;; ,
446.Dq Li \&;&
447and
448.Dq Li \&;\*(Ba
449are used in
450.Ic case
451statements;
452.Dq Li \&(( ... \&))
453is used in arithmetic expressions;
454and lastly,
455.Dq Li \&( ... \&)
456is used to create subshells.
457.Pp
458Whitespace and meta-characters can be quoted individually using a backslash
459.Pq Ql \e ,
460or in groups using double
461.Pq Ql \&"
462or single
463.Pq Dq Li \*(aq
464quotes.
465Note that the following characters are also treated specially by the
466shell and must be quoted if they are to represent themselves:
467.Ql \e ,
468.Ql \&" ,
469.Dq Li \*(aq ,
470.Ql # ,
471.Ql $ ,
472.Ql \` ,
473.Ql \*(TI ,
474.Ql { ,
475.Ql } ,
476.Ql * ,
477.Ql \&?
478and
479.Ql \&[ .
480The first three of these are the above mentioned quoting characters (see
481.Sx Quoting
482below);
483.Ql # ,
484if used at the beginning of a word, introduces a comment \*(en everything after
485the
486.Ql #
487up to the nearest newline is ignored;
488.Ql $
489is used to introduce parameter, command and arithmetic substitutions (see
490.Sx Substitution
491below);
492.Ql \`
493introduces an old-style command substitution (see
494.Sx Substitution
495below);
496.Ql \*(TI
497begins a directory expansion (see
498.Sx Tilde expansion
499below);
500.Ql {
501and
502.Ql }
503delimit
504.Xr csh 1 Ns -style
505alternations (see
506.Sx Brace expansion
507below);
508and finally,
509.Ql * ,
510.Ql \&?
511and
512.Ql \&[
513are used in file name generation (see
514.Sx File name patterns
515below).
516.Pp
517As words and tokens are parsed, the shell builds commands, of which there
518are two basic types:
519.Em simple-commands ,
520typically programmes that are executed, and
521.Em compound-commands ,
522such as
523.Ic for
524and
525.Ic if
526statements, grouping constructs and function definitions.
527.Pp
528A simple-command consists of some combination of parameter assignments
529(see
530.Sx Parameters
531below),
532input/output redirections (see
533.Sx Input/output redirections
534below)
535and command words; the only restriction is that parameter assignments come
536before any command words.
537The command words, if any, define the command
538that is to be executed and its arguments.
539The command may be a shell built-in command, a function
540or an external command
541(i.e. a separate executable file that is located using the
542.Ev PATH
543parameter; see
544.Sx Command execution
545below).
546Note that all command constructs have an exit status: for external commands,
547this is related to the status returned by
548.Xr wait 2
549(if the command could not be found, the exit status is 127; if it could not
550be executed, the exit status is 126); the exit status of other command
551constructs (built-in commands, functions, compound-commands, pipelines, lists,
552etc.) are all well-defined and are described where the construct is
553described.
554The exit status of a command consisting only of parameter
555assignments is that of the last command substitution performed during the
556parameter assignment or 0 if there were no command substitutions.
557.Pp
558Commands can be chained together using the
559.Dq Li \*(Ba
560token to form pipelines, in which the standard output of each command but the
561last is piped (see
562.Xr pipe 2 )
563to the standard input of the following command.
564The exit status of a pipeline is that of its last command, unless the
565.Ic pipefail
566option is set (see there).
567All commands of a pipeline are executed in separate subshells;
568this is allowed by POSIX but differs from both variants of
569.At
570.Nm ksh ,
571where all but the last command were executed in subshells; see the
572.Ic read
573builtin's description for implications and workarounds.
574A pipeline may be prefixed by the
575.Dq Li \&!
576reserved word which causes the exit status of the pipeline to be logically
577complemented: if the original status was 0, the complemented status will be 1;
578if the original status was not 0, the complemented status will be 0.
579.Pp
580.Em Lists
581of commands can be created by separating pipelines by any of the following
582tokens:
583.Dq Li && ,
584.Dq Li \*(Ba\*(Ba ,
585.Dq Li & ,
586.Dq Li \*(Ba&
587and
588.Dq Li \&; .
589The first two are for conditional execution:
590.Dq Ar cmd1 No && Ar cmd2
591executes
592.Ar cmd2
593only if the exit status of
594.Ar cmd1
595is zero;
596.Dq Li \*(Ba\*(Ba
597is the opposite \*(en
598.Ar cmd2
599is executed only if the exit status of
600.Ar cmd1
601is non-zero.
602.Dq Li &&
603and
604.Dq Li \*(Ba\*(Ba
605have equal precedence which is higher than that of
606.Dq Li & ,
607.Dq Li \*(Ba&
608and
609.Dq Li \&; ,
610which also have equal precedence.
611Note that the
612.Dq Li &&
613and
614.Dq Li \*(Ba\*(Ba
615operators are
616.Qq left-associative .
617For example, both of these commands will print only
618.Qq bar :
619.Bd -literal -offset indent
620$ false && echo foo \*(Ba\*(Ba echo bar
621$ true \*(Ba\*(Ba echo foo && echo bar
622.Ed
623.Pp
624The
625.Dq Li &
626token causes the preceding command to be executed asynchronously; that is,
627the shell starts the command but does not wait for it to complete (the shell
628does keep track of the status of asynchronous commands; see
629.Sx Job control
630below).
631When an asynchronous command is started when job control is disabled
632(i.e. in most scripts), the command is started with signals
633.Dv SIGINT
634and
635.Dv SIGQUIT
636ignored and with input redirected from
637.Pa /dev/null
638(however, redirections specified in the asynchronous command have precedence).
639The
640.Dq Li \*(Ba&
641operator starts a co-process which is a special kind of asynchronous process
642(see
643.Sx Co-processes
644below).
645Note that a command must follow the
646.Dq Li &&
647and
648.Dq Li \*(Ba\*(Ba
649operators, while it need not follow
650.Dq Li & ,
651.Dq Li \*(Ba&
652or
653.Dq Li \&; .
654The exit status of a list is that of the last command executed, with the
655exception of asynchronous lists, for which the exit status is 0.
656.Pp
657Compound commands are created using the following reserved words.
658These words
659are only recognised if they are unquoted and if they are used as the first
660word of a command (i.e. they can't be preceded by parameter assignments or
661redirections):
662.Bd -literal -offset indent
663case     else     function     then      !       (
664do       esac     if           time      [[      ((
665done     fi       in           until     {
666elif     for      select       while     }
667.Ed
668.Pp
669In the following compound command descriptions, command lists (denoted as
670.Em list )
671that are followed by reserved words must end with a semicolon, a newline or
672a (syntactically correct) reserved word.
673For example, the following are all valid:
674.Bd -literal -offset indent
675$ { echo foo; echo bar; }
676$ { echo foo; echo bar\*(Ltnewline\*(Gt}
677$ { { echo foo; echo bar; } }
678.Ed
679.Pp
680This is not valid:
681.Pp
682.Dl $ { echo foo; echo bar }
683.Bl -tag -width 4n
684.It Pq Ar list
685Execute
686.Ar list
687in a subshell.
688There is no implicit way to pass environment changes from a
689subshell back to its parent.
690.It { Ar list ; No }
691Compound construct;
692.Ar list
693is executed, but not in a subshell.
694Note that
695.Dq Li {
696and
697.Dq Li }
698are reserved words, not meta-characters.
699.It Xo case Ar word No in
700.Oo Op \&(
701.Ar pattern
702.Op \*(Ba Ar pattern
703.No ... Ns )
704.Ar list
705.Ic terminator
706.Oc No ... esac
707.Xc
708The
709.Ic case
710statement attempts to match
711.Ar word
712against a specified
713.Ar pattern ;
714the
715.Ar list
716associated with the first successfully matched pattern is executed.
717Patterns used in
718.Ic case
719statements are the same as those used for file name patterns except that the
720restrictions regarding
721.Ql \&.
722and
723.Ql /
724are dropped.
725Note that any unquoted space before and after a pattern is
726stripped; any space within a pattern must be quoted.
727Both the word and the
728patterns are subject to parameter, command and arithmetic substitution, as
729well as tilde substitution.
730.Pp
731For historical reasons, open and close braces may be used instead of
732.Ic in
733and
734.Ic esac
735e.g.\&
736.Ic case $foo { *) echo bar ;; } .
737.Pp
738The list
739.Ic terminator Ns s
740are:
741.Bl -tag -width 4n
742.It Dq Li ;;
743Terminate after the list.
744.It Dq Li \&;&
745Fall through into the next list.
746.It Dq Li \&;\*(Ba
747Evaluate the remaining pattern-list tuples.
748.El
749.Pp
750The exit status of a
751.Ic case
752statement is that of the executed
753.Ar list ;
754if no
755.Ar list
756is executed, the exit status is zero.
757.It Xo for Ar name
758.Oo in Ar word No ... Oc ;
759.No do Ar list ; No done
760.Xc
761For each
762.Ar word
763in the specified word list, the parameter
764.Ar name
765is set to the word and
766.Ar list
767is executed.
768If
769.Ic in
770is not used to specify a word list, the positional parameters ($1, $2,
771etc.) are used instead.
772For historical reasons, open and close braces may be used instead of
773.Ic do
774and
775.Ic done
776e.g.\&
777.Ic for i; { echo $i; } .
778The exit status of a
779.Ic for
780statement is the last exit status of
781.Ar list ;
782if
783.Ar list
784is never executed, the exit status is zero.
785.It Xo if Ar list ;
786.No then Ar list ;
787.Oo elif Ar list ;
788.No then Ar list ; Oc
789.No ...
790.Oo else Ar list ; Oc
791.No fi
792.Xc
793If the exit status of the first
794.Ar list
795is zero, the second
796.Ar list
797is executed; otherwise, the
798.Ar list
799following the
800.Ic elif ,
801if any, is executed with similar consequences.
802If all the lists following the
803.Ic if
804and
805.Ic elif Ns s
806fail (i.e. exit with non-zero status), the
807.Ar list
808following the
809.Ic else
810is executed.
811The exit status of an
812.Ic if
813statement is that of non-conditional
814.Ar list
815that is executed; if no non-conditional
816.Ar list
817is executed, the exit status is zero.
818.It Xo select Ar name
819.Oo in Ar word No ... Oc ;
820.No do Ar list ; No done
821.Xc
822The
823.Ic select
824statement provides an automatic method of presenting the user with a menu and
825selecting from it.
826An enumerated list of the specified
827.Ar word Ns (s)
828is printed on standard error, followed by a prompt
829.Po
830.Ev PS3 :
831normally
832.Dq Li #?\ \&
833.Pc .
834A number corresponding to one of the enumerated words is then read from
835standard input,
836.Ar name
837is set to the selected word (or unset if the selection is not valid),
838.Ev REPLY
839is set to what was read (leading/trailing space is stripped), and
840.Ar list
841is executed.
842If a blank line (i.e. zero or more
843.Ev IFS
844octets) is entered, the menu is reprinted without executing
845.Ar list .
846.Pp
847When
848.Ar list
849completes, the enumerated list is printed if
850.Ev REPLY
851is empty, the prompt is printed, and so on.
852This process continues until an end-of-file
853is read, an interrupt is received, or a
854.Ic break
855statement is executed inside the loop.
856If
857.Dq in Ar word ...
858is omitted, the positional parameters are used
859(i.e. $1, $2, etc.).
860For historical reasons, open and close braces may be used instead of
861.Ic do
862and
863.Ic done
864e.g.\&
865.Ic select i; { echo $i; } .
866The exit status of a
867.Ic select
868statement is zero if a
869.Ic break
870statement is used to exit the loop, non-zero otherwise.
871.It Xo until Ar list ;
872.No do Ar list ;
873.No done
874.Xc
875This works like
876.Ic while ,
877except that the body is executed only while the exit status of the first
878.Ar list
879is non-zero.
880.It Xo while Ar list ;
881.No do Ar list ;
882.No done
883.Xc
884A
885.Ic while
886is a pre-checked loop.
887Its body is executed as often as the exit status of the first
888.Ar list
889is zero.
890The exit status of a
891.Ic while
892statement is the last exit status of the
893.Ar list
894in the body of the loop; if the body is not executed, the exit status is zero.
895.It Xo function Ar name
896.No { Ar list ; No }
897.Xc
898Defines the function
899.Ar name
900(see
901.Sx Functions
902below).
903Note that redirections specified after a function definition are
904performed whenever the function is executed, not when the function definition
905is executed.
906.It Ar name Ns \&() Ar command
907Mostly the same as
908.Ic function
909(see
910.Sx Functions
911below).
912Whitespace (space or tab) after
913.Ar name
914will be ignored most of the time.
915.It Xo function Ar name Ns \&()
916.No { Ar list ; No }
917.Xc
918The same as
919.Ar name Ns \&()
920.Pq Nm bash Ns ism .
921The
922.Ic function
923keyword is ignored.
924.It Xo Ic time Op Fl p
925.Op Ar pipeline
926.Xc
927The
928.Sx Command execution
929section describes the
930.Ic time
931reserved word.
932.It \&(( Ar expression No ))
933The arithmetic expression
934.Ar expression
935is evaluated; equivalent to
936.Dq Li let \&" Ns Ar expression Ns \&"
937(see
938.Sx Arithmetic expressions
939and the
940.Ic let
941command, below) in a compound construct.
942.It Bq Bq Ar \ \&expression\ \&
943Similar to the
944.Ic test
945and
946.Ic \&[ ... \&]
947commands (described later), with the following exceptions:
948.Bl -bullet
949.It
950Field splitting and file name generation are not performed on arguments.
951.It
952The
953.Fl a
954.Pq AND
955and
956.Fl o
957.Pq OR
958operators are replaced with
959.Dq Li &&
960and
961.Dq Li \*(Ba\*(Ba ,
962respectively.
963.It
964Operators (e.g.\&
965.Dq Li \-f ,
966.Dq Li = ,
967.Dq Li \&! )
968must be unquoted.
969.It
970Parameter, command and arithmetic substitutions are performed as expressions
971are evaluated and lazy expression evaluation is used for the
972.Dq Li &&
973and
974.Dq Li \*(Ba\*(Ba
975operators.
976This means that in the following statement,
977.Ic $(\*(Ltfoo)
978is evaluated if and only if the file
979.Pa foo
980exists and is readable:
981.Bd -literal -offset indent
982$ [[ \-r foo && $(\*(Ltfoo) = b*r ]]
983.Ed
984.It
985The second operand of the
986.Dq Li !=
987and
988.Dq Li =
989expressions are a subset of patterns (e.g. the comparison
990.Ic \&[[ foobar = f*r ]]
991succeeds).
992This even works indirectly:
993.Bd -literal -offset indent
994$ bar=foobar; baz=\*(aqf*r\*(aq
995$ [[ $bar = $baz ]]; echo $?
996$ [[ $bar = \&"$baz" ]]; echo $?
997.Ed
998.Pp
999Perhaps surprisingly, the first comparison succeeds,
1000whereas the second doesn't.
1001This does not apply to all extglob metacharacters, currently.
1002.El
1003.El
1004.Ss Quoting
1005Quoting is used to prevent the shell from treating characters or words
1006specially.
1007There are three methods of quoting.
1008First,
1009.Ql \e
1010quotes the following character, unless it is at the end of a line, in which
1011case both the
1012.Ql \e
1013and the newline are stripped.
1014Second, a single quote
1015.Pq Dq Li \*(aq
1016quotes everything up to the next single quote (this may span lines).
1017Third, a double quote
1018.Pq Ql \&"
1019quotes all characters, except
1020.Ql $ ,
1021.Ql \e
1022and
1023.Ql \` ,
1024up to the next unescaped double quote.
1025.Ql $
1026and
1027.Ql \`
1028inside double quotes have their usual meaning (i.e. parameter, arithmetic
1029or command substitution) except no field splitting is carried out on the
1030results of double-quoted substitutions, and the old-style form of command
1031substitution has backslash-quoting for double quotes enabled.
1032If a
1033.Ql \e
1034inside a double-quoted string is followed by
1035.Ql \&" ,
1036.Ql $ ,
1037.Ql \e
1038or
1039.Ql \` ,
1040only the
1041.Ql \e
1042is removed, i.e. the combination is replaced by the second character;
1043if it is followed by a newline, both the
1044.Ql \e
1045and the newline are stripped; otherwise, both the
1046.Ql \e
1047and the character following are unchanged.
1048.Pp
1049If a single-quoted string is preceded by an unquoted
1050.Ql $ ,
1051C style backslash expansion (see below) is applied (even single quote
1052characters inside can be escaped and do not terminate the string then);
1053the expanded result is treated as any other single-quoted string.
1054If a double-quoted string is preceded by an unquoted
1055.Ql $ ,
1056the
1057.Ql $
1058is simply ignored.
1059.Ss Backslash expansion
1060In places where backslashes are expanded, certain C and
1061.At
1062.Nm ksh
1063or GNU
1064.Nm bash
1065style escapes are translated.
1066These include
1067.Dq Li \ea ,
1068.Dq Li \eb ,
1069.Dq Li \ef ,
1070.Dq Li \en ,
1071.Dq Li \er ,
1072.Dq Li \et ,
1073.Dq Li \eU######## ,
1074.Dq Li \eu####
1075and
1076.Dq Li \ev .
1077For
1078.Dq Li \eU########
1079and
1080.Dq Li \eu#### ,
1081.Dq #
1082means a hexadecimal digit, of which there may be none up to four or eight;
1083these escapes translate a Universal Coded Character Set codepoint to UTF-8.
1084Furthermore,
1085.Dq Li \eE
1086and
1087.Dq Li \ee
1088expand to the escape character.
1089.Pp
1090In the
1091.Ic print
1092builtin mode,
1093.Dq Li \e" ,
1094.Dq Li \e\*(aq
1095and
1096.Dq Li \e?
1097are explicitly excluded;
1098octal sequences must have the none up to three octal digits
1099.Dq #
1100prefixed with the digit zero
1101.Pq Dq Li \e0### ;
1102hexadecimal sequences
1103.Dq Li \ex##
1104are limited to none up to two hexadecimal digits
1105.Dq # ;
1106both octal and hexadecimal sequences convert to raw octets;
1107.Dq Li \e# ,
1108where # is none of the above, translates to \e# (backslashes are retained).
1109.Pp
1110Backslash expansion in the C style mode slightly differs: octal sequences
1111.Dq Li \e###
1112must have no digit zero prefixing the one up to three octal digits
1113.Dq #
1114and yield raw octets; hexadecimal sequences
1115.Dq Li \ex#*
1116greedily eat up as many hexadecimal digits
1117.Dq #
1118as they can and terminate with the first non-hexadecimal digit;
1119these translate a Universal Coded Character Set codepoint to UTF-8.
1120The sequence
1121.Dq Li \ec# ,
1122where
1123.Dq #
1124is any octet, translates to Ctrl-# (which basically means,
1125.Dq Li \ec?
1126becomes DEL, everything else is bitwise ANDed with 0x1F).
1127Finally,
1128.Dq Li \e# ,
1129where # is none of the above, translates to # (has the backslash trimmed),
1130even if it is a newline.
1131.Ss Aliases
1132There are two types of aliases: normal command aliases and tracked aliases.
1133Command aliases are normally used as a short hand for a long or often used
1134command.
1135The shell expands command aliases (i.e. substitutes the alias name
1136for its value) when it reads the first word of a command.
1137An expanded alias is re-processed to check for more aliases.
1138If a command alias ends in a
1139space or tab, the following word is also checked for alias expansion.
1140The alias expansion process stops when a word that is not an alias is found,
1141when a quoted word is found, or when an alias word that is currently being
1142expanded is found.
1143Aliases are specifically an interactive feature: while they do happen
1144to work in scripts and on the command line in some cases, aliases are
1145expanded during lexing, so their use must be in a separate command tree
1146from their definition; otherwise, the alias will not be found.
1147Noticeably, command lists (separated by semicolon, in command substitutions
1148also by newline) may be one same parse tree.
1149.Pp
1150The following command aliases are defined automatically by the shell:
1151.Bd -literal -offset indent
1152autoload=\*(aq\e\ebuiltin typeset \-fu\*(aq
1153functions=\*(aq\e\ebuiltin typeset \-f\*(aq
1154hash=\*(aq\e\ebuiltin alias \-t\*(aq
1155history=\*(aq\e\ebuiltin fc \-l\*(aq
1156integer=\*(aq\e\ebuiltin typeset \-i\*(aq
1157local=\*(aq\e\ebuiltin typeset\*(aq
1158login=\*(aq\e\ebuiltin exec login\*(aq
1159nameref=\*(aq\e\ebuiltin typeset \-n\*(aq
1160nohup=\*(aqnohup \*(aq
1161r=\*(aq\e\ebuiltin fc \-e \-\*(aq
1162type=\*(aq\e\ebuiltin whence \-v\*(aq
1163.Ed
1164.Pp
1165Tracked aliases allow the shell to remember where it found a particular
1166command.
1167The first time the shell does a path search for a command that is
1168marked as a tracked alias, it saves the full path of the command.
1169The next
1170time the command is executed, the shell checks the saved path to see that it
1171is still valid, and if so, avoids repeating the path search.
1172Tracked aliases can be listed and created using
1173.Ic alias Fl t .
1174Note that changing the
1175.Ev PATH
1176parameter clears the saved paths for all tracked aliases.
1177If the
1178.Ic trackall
1179option is set (i.e.\&
1180.Ic set Fl o Ic trackall
1181or
1182.Ic set Fl h ) ,
1183the shell tracks all commands.
1184This option is set automatically for non-interactive shells.
1185For interactive shells, only the following commands are
1186automatically tracked:
1187.Xr cat 1 ,
1188.Xr cc 1 ,
1189.Xr chmod 1 ,
1190.Xr cp 1 ,
1191.Xr date 1 ,
1192.Xr ed 1 ,
1193.Xr emacs 1 ,
1194.Xr grep 1 ,
1195.Xr ls 1 ,
1196.Xr make 1 ,
1197.Xr mv 1 ,
1198.Xr pr 1 ,
1199.Xr rm 1 ,
1200.Xr sed 1 ,
1201.Xr sh 1 ,
1202.Xr vi 1
1203and
1204.Xr who 1 .
1205.Ss Substitution
1206The first step the shell takes in executing a simple-command is to perform
1207substitutions on the words of the command.
1208There are three kinds of
1209substitution: parameter, command and arithmetic.
1210Parameter substitutions,
1211which are described in detail in the next section, take the form
1212.Pf $ Ns Ar name
1213or
1214.Pf ${ Ns Ar ... Ns } ;
1215command substitutions take the form
1216.Pf $( Ns Ar command Ns \&)
1217or (deprecated)
1218.Pf \` Ns Ar command Ns \`
1219or (executed in the current environment)
1220.Pf ${\ \& Ar command Ns \&;}
1221and strip trailing newlines;
1222and arithmetic substitutions take the form
1223.Pf $(( Ns Ar expression Ns )) .
1224Parsing the current-environment command substitution requires a space,
1225tab or newline after the opening brace and that the closing brace be
1226recognised as a keyword (i.e. is preceded by a newline or semicolon).
1227They are also called funsubs (function substitutions) and behave like
1228functions in that
1229.Ic local
1230and
1231.Ic return
1232work, and in that
1233.Ic exit
1234terminates the parent shell; shell options are shared.
1235.Pp
1236Another variant of substitution are the valsubs (value substitutions)
1237.Pf ${\*(Ba\& Ns Ar command Ns \&;}
1238which are also executed in the current environment, like funsubs, but
1239share their I/O with the parent; instead, they evaluate to whatever
1240the, initially empty, expression-local variable
1241.Ev REPLY
1242is set to within the
1243.Ar command Ns s .
1244.Pp
1245If a substitution appears outside of double quotes, the results of the
1246substitution are generally subject to word or field splitting according to
1247the current value of the
1248.Ev IFS
1249parameter.
1250The
1251.Ev IFS
1252parameter specifies a list of octets which are used to break a string up
1253into several words; any octets from the set space, tab and newline that
1254appear in the
1255.Ev IFS
1256octets are called
1257.Dq IFS whitespace .
1258Sequences of one or more
1259.Ev IFS
1260whitespace octets, in combination with zero or one
1261.Pf non- Ev IFS
1262whitespace octets, delimit a field.
1263As a special case, leading and trailing
1264.Ev IFS
1265whitespace is stripped (i.e. no leading or trailing empty field
1266is created by it); leading or trailing
1267.Pf non- Ev IFS
1268whitespace does create an empty field.
1269.Pp
1270Example: If
1271.Ev IFS
1272is set to
1273.Dq Li \*(Ltspace\*(Gt:
1274and VAR is set to
1275.Dq Li \*(Ltspace\*(GtA\*(Ltspace\*(Gt:\*(Ltspace\*(Gt\*(Ltspace\*(GtB::D ,
1276the substitution for $VAR results in four fields:
1277.Dq Li A ,
1278.Dq Li B ,
1279.Dq
1280(an empty field) and
1281.Dq Li D .
1282Note that if the
1283.Ev IFS
1284parameter is set to the empty string, no field splitting is done;
1285if it is unset, the default value of space, tab and newline is used.
1286.Pp
1287Also, note that the field splitting applies only to the immediate result of
1288the substitution.
1289Using the previous example, the substitution for $VAR:E
1290results in the fields:
1291.Dq Li A ,
1292.Dq Li B ,
1293.Dq
1294and
1295.Dq Li D:E ,
1296not
1297.Dq Li A ,
1298.Dq Li B ,
1299.Dq ,
1300.Dq Li D
1301and
1302.Dq Li E .
1303This behavior is POSIX compliant, but incompatible with some other shell
1304implementations which do field splitting on the word which contained the
1305substitution or use
1306.Dv IFS
1307as a general whitespace delimiter.
1308.Pp
1309The results of substitution are, unless otherwise specified, also subject to
1310brace expansion and file name expansion (see the relevant sections below).
1311.Pp
1312A command substitution is replaced by the output generated by the specified
1313command which is run in a subshell.
1314For
1315.Pf $( Ns Ar command Ns \&)
1316and
1317.Pf ${\*(Ba\& Ns Ar command Ns \&;}
1318and
1319.Pf ${\ \& Ar command Ns \&;}
1320substitutions, normal quoting rules are used when
1321.Ar command
1322is parsed; however, for the deprecated
1323.Pf \` Ns Ar command Ns \`
1324form, a
1325.Ql \e
1326followed by any of
1327.Ql $ ,
1328.Ql \`
1329or
1330.Ql \e
1331is stripped (as is
1332.Ql \&"
1333when the substitution is part of a double-quoted string); a backslash
1334.Ql \e
1335followed by any other character is unchanged.
1336As a special case in command substitutions, a command of the form
1337.Pf \*(Lt Ar file
1338is interpreted to mean substitute the contents of
1339.Ar file .
1340Note that
1341.Ic $(\*(Ltfoo)
1342has the same effect as
1343.Ic $(cat foo) .
1344.Pp
1345Note that some shells do not use a recursive parser for command substitutions,
1346leading to failure for certain constructs; to be portable, use as workaround
1347.Dq Li x=$(cat) \*(Lt\*(Lt\eEOF
1348(or the newline-keeping
1349.Dq Li x=\*(Lt\*(Lt\eEOF
1350extension) instead to merely slurp the string.
1351.St -p1003.1
1352recommends using case statements of the form
1353.Li "x=$(case $foo in (bar) echo $bar ;; (*) echo $baz ;; esac)"
1354instead, which would work but not serve as example for this portability issue.
1355.Bd -literal -offset indent
1356x=$(case $foo in bar) echo $bar ;; *) echo $baz ;; esac)
1357# above fails to parse on old shells; below is the workaround
1358x=$(eval $(cat)) \*(Lt\*(Lt\eEOF
1359case $foo in bar) echo $bar ;; *) echo $baz ;; esac
1360EOF
1361.Ed
1362.Pp
1363Arithmetic substitutions are replaced by the value of the specified expression.
1364For example, the command
1365.Ic print $((2+3*4))
1366displays 14.
1367See
1368.Sx Arithmetic expressions
1369for a description of an expression.
1370.Ss Parameters
1371Parameters are shell variables; they can be assigned values and their values
1372can be accessed using a parameter substitution.
1373A parameter name is either one
1374of the special single punctuation or digit character parameters described
1375below, or a letter followed by zero or more letters or digits
1376.Po
1377.Ql _
1378counts as a letter
1379.Pc .
1380The latter form can be treated as arrays by appending an array index of the
1381form
1382.Op Ar expr
1383where
1384.Ar expr
1385is an arithmetic expression.
1386Array indices in
1387.Nm
1388are limited to the range 0 through 4294967295, inclusive.
1389That is, they are a 32-bit unsigned integer.
1390.Pp
1391Parameter substitutions take the form
1392.Pf $ Ns Ar name ,
1393.Pf ${ Ns Ar name Ns }
1394or
1395.Sm off
1396.Pf ${ Ar name Oo Ar expr Oc }
1397.Sm on
1398where
1399.Ar name
1400is a parameter name.
1401Substitutions of an an array in scalar context, i.e. without an
1402.Ar expr
1403in the latter form mentioned above, expand the element with the key
1404.Dq 0 .
1405Substitution of all array elements with
1406.Pf ${ Ns Ar name Ns \&[*]}
1407and
1408.Pf ${ Ns Ar name Ns \&[@]}
1409works equivalent to $* and $@ for positional parameters.
1410If substitution is performed on a parameter
1411(or an array parameter element)
1412that is not set, an empty string is substituted unless the
1413.Ic nounset
1414option
1415.Pq Ic set Fl u
1416is set, in which case an error occurs.
1417.Pp
1418Parameters can be assigned values in a number of ways.
1419First, the shell implicitly sets some parameters like
1420.Dq Li # ,
1421.Dq Li PWD
1422and
1423.Dq Li $ ;
1424this is the only way the special single character parameters are set.
1425Second, parameters are imported from the shell's environment at startup.
1426Third, parameters can be assigned values on the command line: for example,
1427.Ic FOO=bar
1428sets the parameter
1429.Dq Li FOO
1430to
1431.Dq Li bar ;
1432multiple parameter assignments can be given on a single command line and they
1433can be followed by a simple-command, in which case the assignments are in
1434effect only for the duration of the command (such assignments are also
1435exported; see below for the implications of this).
1436Note that both the parameter name and the
1437.Ql =
1438must be unquoted for the shell to recognise a parameter assignment.
1439The construct
1440.Ic FOO+=baz
1441is also recognised; the old and new values are immediately concatenated.
1442The fourth way of setting a parameter is with the
1443.Ic export ,
1444.Ic global ,
1445.Ic readonly
1446and
1447.Ic typeset
1448commands; see their descriptions in the
1449.Sx Command execution
1450section.
1451Fifth,
1452.Ic for
1453and
1454.Ic select
1455loops set parameters as well as the
1456.Ic getopts ,
1457.Ic read
1458and
1459.Ic set Fl A
1460commands.
1461Lastly, parameters can be assigned values using assignment operators
1462inside arithmetic expressions (see
1463.Sx Arithmetic expressions
1464below) or using the
1465.Sm off
1466.Pf ${ Ar name No = Ar value No }
1467.Sm on
1468form of the parameter substitution (see below).
1469.Pp
1470Parameters with the export attribute (set using the
1471.Ic export
1472or
1473.Ic typeset Fl x
1474commands, or by parameter assignments followed by simple commands) are put in
1475the environment (see
1476.Xr environ 7 )
1477of commands run by the shell as
1478.Ar name Ns = Ns Ar value
1479pairs.
1480The order in which parameters appear in the environment of a command is
1481unspecified.
1482When the shell starts up, it extracts parameters and their values
1483from its environment and automatically sets the export attribute for those
1484parameters.
1485.Pp
1486Modifiers can be applied to the
1487.Pf ${ Ns Ar name Ns }
1488form of parameter substitution:
1489.Bl -tag -width Ds
1490.Sm off
1491.It ${ Ar name No :\- Ar word No }
1492.Sm on
1493If
1494.Ar name
1495is set and not empty,
1496it is substituted; otherwise,
1497.Ar word
1498is substituted.
1499.Sm off
1500.It ${ Ar name No :+ Ar word No }
1501.Sm on
1502If
1503.Ar name
1504is set and not empty,
1505.Ar word
1506is substituted; otherwise, nothing is substituted.
1507.Sm off
1508.It ${ Ar name No := Ar word No }
1509.Sm on
1510If
1511.Ar name
1512is set and not empty,
1513it is substituted; otherwise, it is assigned
1514.Ar word
1515and the resulting value of
1516.Ar name
1517is substituted.
1518.Sm off
1519.It ${ Ar name No :? Ar word No }
1520.Sm on
1521If
1522.Ar name
1523is set and not empty,
1524it is substituted; otherwise,
1525.Ar word
1526is printed on standard error (preceded by
1527.Ar name : )
1528and an error occurs (normally causing termination of a shell script, function,
1529or a script sourced using the
1530.Dq Li \&.
1531built-in).
1532If
1533.Ar word
1534is omitted, the string
1535.Dq Li parameter null or not set
1536is used instead.
1537.El
1538.Pp
1539Note that, for all of the above,
1540.Ar word
1541is actually considered quoted, and special parsing rules apply.
1542The parsing rules also differ on whether the expression is double-quoted:
1543.Ar word
1544then uses double-quoting rules, except for the double quote itself
1545.Pq Ql \&"
1546and the closing brace, which, if backslash escaped, gets quote removal applied.
1547.Pp
1548In the above modifiers, the
1549.Ql \&:
1550can be omitted, in which case the conditions only depend on
1551.Ar name
1552being set (as opposed to set and not empty).
1553If
1554.Ar word
1555is needed, parameter, command, arithmetic and tilde substitution are performed
1556on it; if
1557.Ar word
1558is not needed, it is not evaluated.
1559.Pp
1560The following forms of parameter substitution can also be used:
1561.Pp
1562.Bl -tag -width Ds -compact
1563.It Pf ${# Ns Ar name Ns \&}
1564The number of positional parameters if
1565.Ar name
1566is
1567.Dq Li * ,
1568.Dq Li @
1569or not specified; otherwise the length
1570.Pq in characters
1571of the string value of parameter
1572.Ar name .
1573.Pp
1574.It Pf ${# Ns Ar name Ns \&[*]}
1575.It Pf ${# Ns Ar name Ns \&[@]}
1576The number of elements in the array
1577.Ar name .
1578.Pp
1579.It Pf ${% Ns Ar name Ns \&}
1580The width
1581.Pq in screen columns
1582of the string value of parameter
1583.Ar name ,
1584or \-1 if
1585.Pf ${ Ns Ar name Ns }
1586contains a control character.
1587.Pp
1588.It Pf ${! Ns Ar name Ns }
1589The name of the variable referred to by
1590.Ar name .
1591This will be
1592.Ar name
1593except when
1594.Ar name
1595is a name reference (bound variable), created by the
1596.Ic nameref
1597command (which is an alias for
1598.Ic typeset Fl n ) .
1599.Ar name
1600cannot be one of most special parameters (see below).
1601.Pp
1602.It Pf ${! Ns Ar name Ns \&[*]}
1603.It Pf ${! Ns Ar name Ns \&[@]}
1604The names of indices (keys) in the array
1605.Ar name .
1606.Pp
1607.Sm off
1608.It Xo
1609.Pf ${ Ar name
1610.Pf # Ar pattern No }
1611.Xc
1612.It Xo
1613.Pf ${ Ar name
1614.Pf ## Ar pattern No }
1615.Xc
1616.Sm on
1617If
1618.Ar pattern
1619matches the beginning of the value of parameter
1620.Ar name ,
1621the matched text is deleted from the result of substitution.
1622A single
1623.Ql #
1624results in the shortest match, and two
1625of them result in the longest match.
1626Cannot be applied to a vector
1627.Pq ${*} or ${@} or ${array[*]} or ${array[@]} .
1628.Pp
1629.Sm off
1630.It Xo
1631.Pf ${ Ar name
1632.Pf % Ar pattern No }
1633.Xc
1634.It Xo
1635.Pf ${ Ar name
1636.Pf %% Ar pattern No }
1637.Xc
1638.Sm on
1639Like ${...#...} substitution, but it deletes from the end of the value.
1640Cannot be applied to a vector.
1641.Pp
1642.Sm off
1643.It Xo
1644.Pf ${ Ar name
1645.Pf / Ar pattern / Ar string No }
1646.Xc
1647.It Xo
1648.Pf ${ Ar name
1649.Pf /# Ar pattern / Ar string No }
1650.Xc
1651.It Xo
1652.Pf ${ Ar name
1653.Pf /% Ar pattern / Ar string No }
1654.Xc
1655.It Xo
1656.Pf ${ Ar name
1657.Pf // Ar pattern / Ar string No }
1658.Xc
1659.Sm on
1660The longest match of
1661.Ar pattern
1662in the value of parameter
1663.Ar name
1664is replaced with
1665.Ar string
1666(deleted if
1667.Ar string
1668is empty; the trailing slash
1669.Pq Ql /
1670may be omitted in that case).
1671A leading slash followed by
1672.Ql #
1673or
1674.Ql %
1675causes the pattern to be anchored at the beginning or end of
1676the value, respectively; empty unanchored
1677.Ar pattern Ns s
1678cause no replacement; a single leading slash or use of a
1679.Ar pattern
1680that matches the empty string causes the replacement to
1681happen only once; two leading slashes cause all occurrences
1682of matches in the value to be replaced.
1683Cannot be applied to a vector.
1684Inefficiently implemented, may be slow.
1685.Pp
1686.Sm off
1687.It Xo
1688.Pf ${ Ar name
1689.Pf @/ Ar pattern / Ar string No }
1690.Xc
1691.Sm on
1692The same as
1693.Sm off
1694.Xo
1695.Pf ${ Ar name
1696.Pf // Ar pattern / Ar string No } ,
1697.Xc
1698.Sm on
1699except that both
1700.Ar pattern
1701and
1702.Ar string
1703are expanded anew for each iteration.
1704.Pp
1705.Sm off
1706.It Xo
1707.Pf ${ Ar name : Ns Ar pos
1708.Pf : Ns Ar len Ns }
1709.Xc
1710.Sm on
1711The first
1712.Ar len
1713characters of
1714.Ar name ,
1715starting at position
1716.Ar pos ,
1717are substituted.
1718Both
1719.Ar pos
1720and
1721.Pf : Ns Ar len
1722are optional.
1723If
1724.Ar pos
1725is negative, counting starts at the end of the string; if it
1726is omitted, it defaults to 0.
1727If
1728.Ar len
1729is omitted or greater than the length of the remaining string,
1730all of it is substituted.
1731Both
1732.Ar pos
1733and
1734.Ar len
1735are evaluated as arithmetic expressions.
1736Currently,
1737.Ar pos
1738must start with a space, opening parenthesis or digit to be recognised.
1739Cannot be applied to a vector.
1740.Pp
1741.It Pf ${ Ns Ar name Ns @#}
1742The hash (using the BAFH algorithm) of the expansion of
1743.Ar name .
1744This is also used internally for the shell's hashtables.
1745.Pp
1746.It Pf ${ Ns Ar name Ns @Q}
1747A quoted expression safe for re-entry, whose value is the value of the
1748.Ar name
1749parameter, is substituted.
1750.El
1751.Pp
1752Note that
1753.Ar pattern
1754may need extended globbing pattern
1755.Pq @(...) ,
1756single
1757.Pq \&\*(aq...\&\*(aq
1758or double
1759.Pq \&"...\&"
1760quote escaping unless
1761.Fl o Ic sh
1762is set.
1763.Pp
1764The following special parameters are implicitly set by the shell and cannot be
1765set directly using assignments:
1766.Bl -tag -width "1 .. 9"
1767.It Ev \&!
1768Process ID of the last background process started.
1769If no background processes have been started, the parameter is not set.
1770.It Ev \&#
1771The number of positional parameters ($1, $2, etc.).
1772.It Ev \&$
1773The PID of the shell or, if it is a subshell, the PID of the original shell.
1774Do
1775.Em NOT
1776use this mechanism for generating temporary file names; see
1777.Xr mktemp 1
1778instead.
1779.It Ev \-
1780The concatenation of the current single letter options (see the
1781.Ic set
1782command below for a list of options).
1783.It Ev \&?
1784The exit status of the last non-asynchronous command executed.
1785If the last command was killed by a signal,
1786.Ic \&$?
1787is set to 128 plus the signal number, but at most 255.
1788.It Ev 0
1789The name of the shell, determined as follows:
1790the first argument to
1791.Nm
1792if it was invoked with the
1793.Fl c
1794option and arguments were given; otherwise the
1795.Ar file
1796argument, if it was supplied;
1797or else the basename the shell was invoked with (i.e.\&
1798.Li argv[0] ) .
1799.Ev $0
1800is also set to the name of the current script or
1801the name of the current function, if it was defined with the
1802.Ic function
1803keyword (i.e. a Korn shell style function).
1804.It Ev 1 No .. Ev 9
1805The first nine positional parameters that were supplied to the shell, function,
1806or script sourced using the
1807.Dq Li \&.
1808built-in.
1809Further positional parameters may be accessed using
1810.Pf ${ Ar number Ns } .
1811.It Ev *
1812All positional parameters (except 0), i.e. $1, $2, $3, ...
1813.br
1814If used
1815outside of double quotes, parameters are separate words (which are subjected
1816to word splitting); if used within double quotes, parameters are separated
1817by the first character of the
1818.Ev IFS
1819parameter (or the empty string if
1820.Ev IFS
1821is unset.
1822.It Ev @
1823Same as
1824.Ic $* ,
1825unless it is used inside double quotes, in which case a separate word is
1826generated for each positional parameter.
1827If there are no positional parameters, no word is generated.
1828.Ic \&"$@"
1829can be used to access arguments, verbatim, without losing
1830empty arguments or splitting arguments with spaces (IFS, actually).
1831.El
1832.Pp
1833The following parameters are set and/or used by the shell:
1834.Bl -tag -width "KSH_VERSION"
1835.It Ev _
1836.Pq underscore
1837When an external command is executed by the shell, this parameter is set in the
1838environment of the new process to the path of the executed command.
1839In interactive use, this parameter is also set in the parent shell to the last
1840word of the previous command.
1841.It Ev BASHPID
1842The PID of the shell or subshell.
1843.It Ev CDPATH
1844Like
1845.Ev PATH ,
1846but used to resolve the argument to the
1847.Ic cd
1848built-in command.
1849Note that if
1850.Ev CDPATH
1851is set and does not contain
1852.Dq Li \&.
1853or an empty string element, the current directory is not searched.
1854Also, the
1855.Ic cd
1856built-in command will display the resulting directory when a match is found
1857in any search path other than the empty path.
1858.It Ev COLUMNS
1859Set to the number of columns on the terminal or window.
1860If never unset and not imported, always set dynamically;
1861unless the value as reported by
1862.Xr stty 1
1863is non-zero and sane enough (minimum is 12x3), defaults to 80; similar for
1864.Ev LINES .
1865This parameter is used by the interactive line editing modes and by the
1866.Ic select ,
1867.Ic set Fl o
1868and
1869.Ic kill Fl l
1870commands to format information columns.
1871Importing from the environment or unsetting this parameter removes the
1872binding to the actual terminal size in favour of the provided value.
1873.It Ev ENV
1874If this parameter is found to be set after any profile files are executed, the
1875expanded value is used as a shell startup file.
1876It typically contains function and alias definitions.
1877.It Ev EPOCHREALTIME
1878Time since the epoch, as returned by
1879.Xr gettimeofday 2 ,
1880formatted as decimal
1881.Va tv_sec
1882followed by a dot
1883.Pq Ql \&.
1884and
1885.Va tv_usec
1886padded to exactly six decimal digits.
1887.It Ev EXECSHELL
1888If set, this parameter is assumed to contain the shell that is to be used to
1889execute commands that
1890.Xr execve 2
1891fails to execute and which do not start with a
1892.Dq Li #! Ns Ar shell
1893sequence.
1894.It Ev FCEDIT
1895The editor used by the
1896.Ic fc
1897command (see below).
1898.It Ev FPATH
1899Like
1900.Ev PATH ,
1901but used when an undefined function is executed to locate the file defining the
1902function.
1903It is also searched when a command can't be found using
1904.Ev PATH .
1905See
1906.Sx Functions
1907below for more information.
1908.It Ev HISTFILE
1909The name of the file used to store command history.
1910When assigned to or unset, the file is opened, history is truncated
1911then loaded from the file; subsequent new commands (possibly consisting
1912of several lines) are appended once they successfully compiled.
1913Also, several invocations of the shell will share history if their
1914.Ev HISTFILE
1915parameters all point to the same file.
1916.Pp
1917.Sy Note :
1918If
1919.Ev HISTFILE
1920is unset or empty, no history file is used.
1921This is different from
1922.At
1923.Nm ksh .
1924.It Ev HISTSIZE
1925The number of commands normally stored for history.
1926The default is 2047.
1927Do not set this value to insanely high values such as 1000000000 because
1928.Nm
1929can then not allocate enough memory for the history and will not start.
1930.It Ev HOME
1931The default directory for the
1932.Ic cd
1933command and the value substituted for an unqualified
1934.Ic \*(TI
1935(see
1936.Sx Tilde expansion
1937below).
1938.It Ev IFS
1939Internal field separator, used during substitution and by the
1940.Ic read
1941command, to split values into distinct arguments; normally set to space, tab
1942and newline.
1943See
1944.Sx Substitution
1945above for details.
1946.Pp
1947.Sy Note :
1948This parameter is not imported from the environment when the shell is
1949started.
1950.It Ev KSHEGID
1951The effective group id of the shell.
1952.It Ev KSHGID
1953The real group id of the shell.
1954.It Ev KSHUID
1955The real user id of the shell.
1956.It Ev KSH_MATCH
1957The last matched string.
1958In a future version, this will be an indexed array,
1959with indexes 1 and up capturing matching groups.
1960Set by string comparisons (== and !=) in double-bracket test
1961expressions when a match is found (when != returns false), by
1962.Ic case
1963when a match is encountered, and by the substitution operations
1964.Sm off
1965.Xo
1966.Pf ${ Ar x
1967.Pf # Ar pat No } ,
1968.Sm on
1969.Xc
1970.Sm off
1971.Xo
1972.Pf ${ Ar x
1973.Pf ## Ar pat No } ,
1974.Sm on
1975.Xc
1976.Sm off
1977.Xo
1978.Pf ${ Ar x
1979.Pf % Ar pat No } ,
1980.Sm on
1981.Xc
1982.Sm off
1983.Xo
1984.Pf ${ Ar x
1985.Pf %% Ar pat No } ,
1986.Sm on
1987.Xc
1988.Sm off
1989.Xo
1990.Pf ${ Ar x
1991.Pf / Ar pat / Ar rpl No } ,
1992.Sm on
1993.Xc
1994.Sm off
1995.Xo
1996.Pf ${ Ar x
1997.Pf /# Ar pat / Ar rpl No } ,
1998.Sm on
1999.Xc
2000.Sm off
2001.Xo
2002.Pf ${ Ar x
2003.Pf /% Ar pat / Ar rpl No } ,
2004.Sm on
2005.Xc
2006.Sm off
2007.Xo
2008.Pf ${ Ar x
2009.Pf // Ar pat / Ar rpl No } ,
2010.Sm on
2011.Xc
2012and
2013.Sm off
2014.Xo
2015.Pf ${ Ar x
2016.Pf @/ Ar pat / Ar rpl No } .
2017.Sm on
2018.Xc
2019See the end of the Emacs editing mode documentation for an example.
2020.It Ev KSH_VERSION
2021The name and version of the shell (read-only).
2022See also the version commands in
2023.Sx Emacs editing mode
2024and
2025.Sx Vi editing mode
2026sections, below.
2027.It Ev LINENO
2028The line number of the function or shell script that is currently being
2029executed.
2030.It Ev LINES
2031Set to the number of lines on the terminal or window.
2032Defaults to 24; always set, unless imported or unset.
2033See
2034.Ev COLUMNS .
2035.It Ev OLDPWD
2036The previous working directory.
2037Unset if
2038.Ic cd
2039has not successfully changed directories since the shell started or if the
2040shell doesn't know where it is.
2041.It Ev OPTARG
2042When using
2043.Ic getopts ,
2044it contains the argument for a parsed option, if it requires one.
2045.It Ev OPTIND
2046The index of the next argument to be processed when using
2047.Ic getopts .
2048Assigning 1 to this parameter causes
2049.Ic getopts
2050to process arguments from the beginning the next time it is invoked.
2051.It Ev PATH
2052A colon (semicolon on OS/2) separated list of directories that are
2053searched when looking for commands and files sourced using the
2054.Dq Li \&.
2055command (see below).
2056An empty string resulting from a leading or trailing
2057(semi)colon, or two adjacent ones, is treated as a
2058.Dq Li \&.
2059(the current directory).
2060.It Ev PATHSEP
2061A colon (semicolon on OS/2), for the user's convenience.
2062.It Ev PGRP
2063The process ID of the shell's process group leader.
2064.It Ev PIPESTATUS
2065An array containing the errorlevel (exit status) codes,
2066one by one, of the last pipeline run in the foreground.
2067.It Ev PPID
2068The process ID of the shell's parent.
2069.It Ev PS1
2070The primary prompt for interactive shells.
2071Parameter, command and arithmetic
2072substitutions are performed, and
2073.Ql \&!
2074is replaced with the current command number (see the
2075.Ic fc
2076command below).
2077A literal
2078.Ql \&!
2079can be put in the prompt by placing
2080.Dq Li !!
2081in
2082.Ev PS1 .
2083.Pp
2084The default prompt is
2085.Dq Li $\ \&
2086for non-root users,
2087.Dq Li #\ \&
2088for root.
2089If
2090.Nm
2091is invoked by root and
2092.Ev PS1
2093does not contain a
2094.Ql #
2095character, the default value will be used even if
2096.Ev PS1
2097already exists in the environment.
2098.Pp
2099The
2100.Nm
2101distribution comes with a sample
2102.Pa dot.mkshrc
2103containing a sophisticated example, but you might like the following one
2104(note that ${HOSTNAME:=$(hostname)} and the
2105root-vs-user distinguishing clause are (in this example) executed at
2106.Ev PS1
2107assignment time, while the $USER and $PWD are escaped
2108and thus will be evaluated each time a prompt is displayed):
2109.Bd -literal
2110PS1=\*(aq${USER:=$(id \-un)}\*(aq"@${HOSTNAME:=$(hostname)}:\e$PWD $(
2111	if (( USER_ID )); then print \e$; else print \e#; fi) "
2112.Ed
2113.Pp
2114Note that since the command-line editors try to figure out how long the prompt
2115is (so they know how far it is to the edge of the screen), escape codes in
2116the prompt tend to mess things up.
2117You can tell the shell not to count certain
2118sequences (such as escape codes) by prefixing your prompt with a
2119character (such as Ctrl-A) followed by a carriage return and then delimiting
2120the escape codes with this character.
2121Any occurrences of that character in the prompt are not printed.
2122By the way, don't blame me for
2123this hack; it's derived from the original
2124.Xr ksh88 1 ,
2125which did print the delimiter character so you were out of luck
2126if you did not have any non-printing characters.
2127.Pp
2128Since backslashes and other special characters may be
2129interpreted by the shell, to set
2130.Ev PS1
2131either escape the backslash itself
2132or use double quotes.
2133The latter is more practical.
2134This is a more complex example,
2135avoiding to directly enter special characters (for example with
2136.Ic \*(haV
2137in the emacs editing mode),
2138which embeds the current working directory,
2139in reverse video
2140.Pq colour would work, too ,
2141in the prompt string:
2142.Bd -literal -offset indent
2143x=$(print \e\e001) # otherwise unused char
2144PS1="$x$(print \e\er)$x$(tput so)$x\e$PWD$x$(tput se)$x\*(Gt "
2145.Ed
2146.Pp
2147Due to a strong suggestion from David G. Korn,
2148.Nm
2149now also supports the following form:
2150.Bd -literal -offset indent
2151PS1=$\*(aq\e1\er\e1\ee[7m\e1$PWD\e1\ee[0m\e1\*(Gt \*(aq
2152.Ed
2153.It Ev PS2
2154Secondary prompt string, by default
2155.Dq Li \*(Gt\ \& ,
2156used when more input is needed to complete a command.
2157.It Ev PS3
2158Prompt used by the
2159.Ic select
2160statement when reading a menu selection.
2161The default is
2162.Dq Li #?\ \& .
2163.It Ev PS4
2164Used to prefix commands that are printed during execution tracing (see the
2165.Ic set Fl x
2166command below).
2167Parameter, command and arithmetic substitutions are performed
2168before it is printed.
2169The default is
2170.Dq Li +\ \& .
2171You may want to set it to
2172.Dq Li \&[$EPOCHREALTIME]\ \&
2173instead, to include timestamps.
2174.It Ev PWD
2175The current working directory.
2176May be unset or empty if the shell doesn't know where it is.
2177.It Ev RANDOM
2178Each time
2179.Ev RANDOM
2180is referenced, it is assigned a number between 0 and 32767 from
2181a Linear Congruential PRNG first.
2182.It Ev REPLY
2183Default parameter for the
2184.Ic read
2185command if no names are given.
2186Also used in
2187.Ic select
2188loops to store the value that is read from standard input.
2189.It Ev SECONDS
2190The number of seconds since the shell started or, if the parameter has been
2191assigned an integer value, the number of seconds since the assignment plus the
2192value that was assigned.
2193.It Ev TMOUT
2194If set to a positive integer in an interactive shell, it specifies the maximum
2195number of seconds the shell will wait for input after printing the primary
2196prompt
2197.Pq Ev PS1 .
2198If the time is exceeded, the shell exits.
2199.It Ev TMPDIR
2200The directory temporary shell files are created in.
2201If this parameter is not
2202set or does not contain the absolute path of a writable directory, temporary
2203files are created in
2204.Pa /tmp .
2205.It Ev USER_ID
2206The effective user id of the shell.
2207.El
2208.Ss Tilde expansion
2209Tilde expansion, which is done in parallel with parameter substitution,
2210is applied to words starting with an unquoted
2211.Ql \*(TI .
2212In parameter assignments (such as those preceding a simple-command or those
2213occurring in the arguments of a declaration utility), tilde expansion is done
2214after any assignment (i.e. after the equals sign) or after an unquoted colon
2215.Pq Ql \&: ;
2216login names are also delimited by colons.
2217The Korn shell, except in POSIX mode, always expands tildes after unquoted
2218equals signs, not just in assignment context (see below), and enables tab
2219completion for tildes after all unquoted colons during command line editing.
2220.Pp
2221The characters following the tilde, up to the first
2222.Ql / ,
2223if any, are assumed to be a login name.
2224If the login name is empty,
2225.Ql +
2226or
2227.Ql \- ,
2228the simplified value of the
2229.Ev HOME ,
2230.Ev PWD
2231or
2232.Ev OLDPWD
2233parameter is substituted, respectively.
2234Otherwise, the password file is
2235searched for the login name, and the tilde expression is substituted with the
2236user's home directory.
2237If the login name is not found in the password file or
2238if any quoting or parameter substitution occurs in the login name, no
2239substitution is performed.
2240.Pp
2241The home directory of previously expanded login names are cached and re-used.
2242The
2243.Ic alias Fl d
2244command may be used to list, change and add to this cache (e.g.\&
2245.Ic alias \-d fac=/usr/local/facilities; cd \*(TIfac/bin ) .
2246.Ss Brace expansion (alternation)
2247Brace expressions take the following form:
2248.Bd -unfilled -offset indent
2249.Sm off
2250.Xo
2251.Ar prefix No { Ar str1 No ,...,
2252.Ar strN No } Ar suffix
2253.Xc
2254.Sm on
2255.Ed
2256.Pp
2257The expressions are expanded to
2258.Ar N
2259words, each of which is the concatenation of
2260.Ar prefix ,
2261.Ar str Ns i
2262and
2263.Ar suffix
2264(e.g.\&
2265.Dq Li a{c,b{X,Y},d}e
2266expands to four words:
2267.Dq Li ace ,
2268.Dq Li abXe ,
2269.Dq Li abYe
2270and
2271.Dq Li ade ) .
2272As noted in the example, brace expressions can be nested and the resulting
2273words are not sorted.
2274Brace expressions must contain an unquoted comma
2275.Pq Ql \&,
2276for expansion to occur (e.g.\&
2277.Ic {}
2278and
2279.Ic {foo}
2280are not expanded).
2281Brace expansion is carried out after parameter substitution
2282and before file name generation.
2283.Ss File name patterns
2284A file name pattern is a word containing one or more unquoted
2285.Ql \&? ,
2286.Ql * ,
2287.Ql + ,
2288.Ql @
2289or
2290.Ql \&!
2291characters or
2292.Dq Li \&[...]
2293sequences.
2294Once brace expansion has been performed, the shell replaces file
2295name patterns with the sorted names of all the files that match the pattern
2296(if no files match, the word is left unchanged).
2297The pattern elements have the following meaning:
2298.Bl -tag -width Ds
2299.It \&?
2300Matches any single character.
2301.It \&*
2302Matches any sequence of octets.
2303.It \&[...]
2304Matches any of the octets inside the brackets.
2305Ranges of octets can be specified by separating two octets by a
2306.Ql \-
2307(e.g.\&
2308.Dq Li \&[a0\-9]
2309matches the letter
2310.Ql a
2311or any digit).
2312In order to represent itself, a
2313.Ql \-
2314must either be quoted or the first or last octet in the octet list.
2315Similarly, a
2316.Ql \&]
2317must be quoted or the first octet in the list if it is to represent itself
2318instead of the end of the list.
2319Also, a
2320.Ql \&!
2321appearing at the start of the list has special meaning (see below), so to
2322represent itself it must be quoted or appear later in the list.
2323.It \&[!...]
2324Like [...],
2325except it matches any octet not inside the brackets.
2326.Sm off
2327.It *( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2328.Sm on
2329Matches any string of octets that matches zero or more occurrences of the
2330specified patterns.
2331Example: The pattern
2332.Ic *(foo\*(Babar)
2333matches the strings
2334.Dq ,
2335.Dq Li foo ,
2336.Dq Li bar ,
2337.Dq Li foobarfoo ,
2338etc.
2339.Sm off
2340.It +( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2341.Sm on
2342Matches any string of octets that matches one or more occurrences of the
2343specified patterns.
2344Example: The pattern
2345.Ic +(foo\*(Babar)
2346matches the strings
2347.Dq Li foo ,
2348.Dq Li bar ,
2349.Dq Li foobar ,
2350etc.
2351.Sm off
2352.It ?( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2353.Sm on
2354Matches the empty string or a string that matches one of the specified
2355patterns.
2356Example: The pattern
2357.Ic ?(foo\*(Babar)
2358only matches the strings
2359.Dq ,
2360.Dq Li foo
2361and
2362.Dq Li bar .
2363.Sm off
2364.It @( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2365.Sm on
2366Matches a string that matches one of the specified patterns.
2367Example: The pattern
2368.Ic @(foo\*(Babar)
2369only matches the strings
2370.Dq Li foo
2371and
2372.Dq Li bar .
2373.Sm off
2374.It !( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2375.Sm on
2376Matches any string that does not match one of the specified patterns.
2377Examples: The pattern
2378.Ic !(foo\*(Babar)
2379matches all strings except
2380.Dq Li foo
2381and
2382.Dq Li bar ;
2383the pattern
2384.Ic \&!(*)
2385matches no strings; the pattern
2386.Ic \&!(?)*
2387matches all strings (think about it).
2388.El
2389.Pp
2390Note that complicated globbing, especially with alternatives,
2391is slow; using separate comparisons may (or may not) be faster.
2392.Pp
2393Note that
2394.Nm mksh
2395.Po and Nm pdksh Pc
2396never matches
2397.Dq Li \&.
2398and
2399.Dq Li .. ,
2400but
2401.At
2402.Nm ksh ,
2403Bourne
2404.Nm sh
2405and GNU
2406.Nm bash
2407do.
2408.Pp
2409Note that none of the above pattern elements match either a period
2410.Pq Ql \&.
2411at the start of a file name or a slash
2412.Pq Ql / ,
2413even if they are explicitly used in a [...] sequence; also, the names
2414.Dq Li \&.
2415and
2416.Dq Li ..
2417are never matched, even by the pattern
2418.Dq Li .* .
2419.Pp
2420If the
2421.Ic markdirs
2422option is set, any directories that result from file name generation are marked
2423with a trailing
2424.Ql / .
2425.Ss Input/output redirection
2426When a command is executed, its standard input, standard output and standard
2427error (file descriptors 0, 1 and 2, respectively) are normally inherited from
2428the shell.
2429Three exceptions to this are commands in pipelines, for which
2430standard input and/or standard output are those set up by the pipeline,
2431asynchronous commands created when job control is disabled, for which standard
2432input is initially set to
2433.Pa /dev/null ,
2434and commands for which any of the following redirections have been specified:
2435.Bl -tag -width XXxxmarker
2436.It \*(Gt Ns Ar file
2437Standard output is redirected to
2438.Ar file .
2439If
2440.Ar file
2441does not exist, it is created; if it does exist, is a regular file, and the
2442.Ic noclobber
2443option is set, an error occurs; otherwise, the file is truncated.
2444Note that this means the command
2445.Ic cmd \*(Ltfoo \*(Gtfoo
2446will open
2447.Ar foo
2448for reading and then truncate it when it opens it for writing, before
2449.Ar cmd
2450gets a chance to actually read
2451.Ar foo .
2452.It \*(Gt\*(Ba Ns Ar file
2453Same as
2454.Ic \*(Gt ,
2455except the file is truncated, even if the
2456.Ic noclobber
2457option is set.
2458.It \*(Gt\*(Gt Ns Ar file
2459Same as
2460.Ic \*(Gt ,
2461except if
2462.Ar file
2463exists it is appended to instead of being truncated.
2464Also, the file is opened
2465in append mode, so writes always go to the end of the file (see
2466.Xr open 2 ) .
2467.It \*(Lt Ns Ar file
2468Standard input is redirected from
2469.Ar file ,
2470which is opened for reading.
2471.It \*(Lt\*(Gt Ns Ar file
2472Same as
2473.Ic \*(Lt ,
2474except the file is opened for reading and writing.
2475.It \*(Lt\*(Lt Ns Ar marker
2476After reading the command line containing this kind of redirection (called a
2477.Dq here document ) ,
2478the shell copies lines from the command source into a temporary file until a
2479line matching
2480.Ar marker
2481is read.
2482When the command is executed, standard input is redirected from the
2483temporary file.
2484If
2485.Ar marker
2486contains no quoted characters, the contents of the temporary file are processed
2487as if enclosed in double quotes each time the command is executed, so
2488parameter, command and arithmetic substitutions are performed, along with
2489backslash
2490.Pq Ql \e
2491escapes for
2492.Ql $ ,
2493.Ql \` ,
2494.Ql \e
2495and
2496.Dq Li \enewline ,
2497but not for
2498.Ql \&" .
2499If multiple here documents are used on the same command line, they are saved in
2500order.
2501.Pp
2502If no
2503.Ar marker
2504is given, the here document ends at the next
2505.Ic \*(Lt\*(Lt
2506and substitution will be performed.
2507If
2508.Ar marker
2509is only a set of either single
2510.Dq Li \*(aq\*(aq
2511or double
2512.Ql \&""
2513quotes with nothing in between, the here document ends at the next empty line
2514and substitution will not be performed.
2515.It \*(Lt\*(Lt\- Ns Ar marker
2516Same as
2517.Ic \*(Lt\*(Lt ,
2518except leading tabs are stripped from lines in the here document.
2519.It \*(Lt\*(Lt\*(Lt Ns Ar word
2520Same as
2521.Ic \*(Lt\*(Lt ,
2522except that
2523.Ar word
2524.Em is
2525the here document.
2526This is called a here string.
2527.It \*(Lt& Ns Ar fd
2528Standard input is duplicated from file descriptor
2529.Ar fd .
2530.Ar fd
2531can be a single digit, indicating the number of an existing file descriptor;
2532the letter
2533.Ql p ,
2534indicating the file descriptor associated with the output of the current
2535co-process; or the character
2536.Ql \- ,
2537indicating standard input is to be closed.
2538.It \*(Gt& Ns Ar fd
2539Same as
2540.Ic \*(Lt& ,
2541except the operation is done on standard output.
2542.It &\*(Gt Ns Ar file
2543Same as
2544.Ic \*(Gt Ns Ar file 2\*(Gt&1 .
2545This is a deprecated (legacy) GNU
2546.Nm bash
2547extension supported by
2548.Nm
2549which also supports the preceding explicit fd digit, for example,
2550.Ic 3&\*(Gt Ns Ar file
2551is the same as
2552.Ic 3\*(Gt Ns Ar file 2\*(Gt&3
2553in
2554.Nm
2555but a syntax error in GNU
2556.Nm bash .
2557.It Xo
2558.No &\*(Gt\*(Ba Ns Ar file ,
2559.No &\*(Gt\*(Gt Ns Ar file ,
2560.No &\*(Gt& Ns Ar fd
2561.Xc
2562Same as
2563.Ic \*(Gt\*(Ba Ns Ar file ,
2564.Ic \*(Gt\*(Gt Ns Ar file
2565or
2566.Ic \*(Gt& Ns Ar fd ,
2567followed by
2568.Ic 2\*(Gt&1 ,
2569as above.
2570These are
2571.Nm
2572extensions.
2573.El
2574.Pp
2575In any of the above redirections, the file descriptor that is redirected
2576(i.e. standard input or standard output)
2577can be explicitly given by preceding the
2578redirection with a single digit.
2579Parameter, command and arithmetic
2580substitutions, tilde substitutions, and, if the shell is interactive,
2581file name generation are all performed on the
2582.Ar file ,
2583.Ar marker
2584and
2585.Ar fd
2586arguments of redirections.
2587Note, however, that the results of any file name
2588generation are only used if a single file is matched; if multiple files match,
2589the word with the expanded file name generation characters is used.
2590Note
2591that in restricted shells, redirections which can create files cannot be used.
2592.Pp
2593For simple-commands, redirections may appear anywhere in the command; for
2594compound-commands
2595.Po
2596.Ic if
2597statements, etc.
2598.Pc ,
2599any redirections must appear at the end.
2600Redirections are processed after
2601pipelines are created and in the order they are given, so the following
2602will print an error with a line number prepended to it:
2603.Pp
2604.Dl $ cat /foo/bar 2\*(Gt&1 \*(Gt/dev/null \*(Ba pr \-n \-t
2605.Pp
2606File descriptors created by I/O redirections are private to the shell.
2607.Ss Arithmetic expressions
2608Integer arithmetic expressions can be used with the
2609.Ic let
2610command, inside $((...)) expressions, inside array references (e.g.\&
2611.Ar name Ns Bq Ar expr ) ,
2612as numeric arguments to the
2613.Ic test
2614command, and as the value of an assignment to an integer parameter.
2615.Em Warning :
2616This also affects implicit conversion to integer, for example as done by the
2617.Ic let
2618command.
2619.Em Never
2620use unchecked user input, e.g. from the environment, in an arithmetic context!
2621.Pp
2622Expressions are calculated using signed arithmetic and the
2623.Vt mksh_ari_t
2624type (a 32-bit signed integer), unless they begin with a sole
2625.Ql #
2626character, in which case they use
2627.Vt mksh_uari_t
2628.Po a 32-bit unsigned integer Pc .
2629.Pp
2630Expressions may contain alpha-numeric parameter identifiers, array references
2631and integer constants and may be combined with the following C operators
2632(listed and grouped in increasing order of precedence):
2633.Pp
2634Unary operators:
2635.Bd -literal -offset indent
2636+ \- ! \*(TI ++ \-\-
2637.Ed
2638.Pp
2639Binary operators:
2640.Bd -literal -offset indent
2641,
2642= += \-= *= /= %= \*(Lt\*(Lt= \*(Gt\*(Gt= \*(ha\*(Lt= \*(ha\*(Gt= &= \*(ha= \*(Ba=
2643\*(Ba\*(Ba
2644&&
2645\*(Ba
2646\*(ha
2647&
2648== !=
2649\*(Lt \*(Lt= \*(Gt \*(Gt=
2650\*(Lt\*(Lt \*(Gt\*(Gt \*(ha\*(Lt \*(ha\*(Gt
2651+ \-
2652* / %
2653.Ed
2654.Pp
2655Ternary operators:
2656.Bd -literal -offset indent
2657?: (precedence is immediately higher than assignment)
2658.Ed
2659.Pp
2660Grouping operators:
2661.Bd -literal -offset indent
2662( )
2663.Ed
2664.Pp
2665Integer constants and expressions are calculated using an exactly 32-bit
2666wide, signed or unsigned, type with silent wraparound on integer overflow.
2667Integer constants may be specified with arbitrary bases using the notation
2668.Ar base Ns # Ns Ar number ,
2669where
2670.Ar base
2671is a decimal integer specifying the base (up to 36), and
2672.Ar number
2673is a number in the specified base.
2674Additionally, base-16 integers may be specified by prefixing them with
2675.Dq Li 0x
2676.Pq case-insensitive
2677in all forms of arithmetic expressions, except as numeric arguments to the
2678.Ic test
2679built-in utility.
2680Prefixing numbers with a sole digit zero
2681.Pq Dq Li 0
2682does not cause interpretation as octal (except in POSIX mode,
2683as required by the standard), as that's unsafe to do.
2684.Pp
2685As a special
2686.Nm mksh
2687extension, numbers to the base of one are treated as either (8-bit
2688transparent) ASCII or Universal Coded Character Set codepoints,
2689depending on the shell's
2690.Ic utf8\-mode
2691flag (current setting).
2692The
2693.At
2694.Nm ksh93
2695syntax of
2696.Dq Li \*(aqx\*(aq
2697instead of
2698.Dq Li 1#x
2699is also supported.
2700Note that NUL bytes (integral value of zero) cannot be used.
2701An unset or empty parameter evaluates to 0 in integer context.
2702In UTF-8 mode, raw octets are mapped into the range EF80..EFFF as in
2703OPTU-8, which is in the PUA and has been assigned by CSUR for this use.
2704If more than one octet in ASCII mode, or a sequence of more than one
2705octet not forming a valid and minimal CESU-8 sequence is passed, the
2706behaviour is undefined (usually, the shell aborts with a parse error,
2707but rarely, it succeeds, e.g. on the sequence C2 20).
2708That's why you should always use ASCII mode unless you know that the
2709input is well-formed UTF-8 in the range of 0000..FFFD if you use this
2710feature, as opposed to
2711.Ic read Fl a .
2712.Pp
2713The operators are evaluated as follows:
2714.Bl -tag -width Ds -offset indent
2715.It unary +
2716Result is the argument (included for completeness).
2717.It unary \-
2718Negation.
2719.It \&!
2720Logical NOT;
2721the result is 1 if argument is zero, 0 if not.
2722.It \*(TI
2723Arithmetic (bit-wise) NOT.
2724.It ++
2725Increment; must be applied to a parameter (not a literal or other expression).
2726The parameter is incremented by 1.
2727When used as a prefix operator, the result
2728is the incremented value of the parameter; when used as a postfix operator, the
2729result is the original value of the parameter.
2730.It \-\-
2731Similar to
2732.Ic ++ ,
2733except the parameter is decremented by 1.
2734.It \&,
2735Separates two arithmetic expressions; the left-hand side is evaluated first,
2736then the right.
2737The result is the value of the expression on the right-hand side.
2738.It =
2739Assignment; the variable on the left is set to the value on the right.
2740.It Xo
2741.No += \-= *= /= %= \*(Lt\*(Lt= \*(Gt\*(Gt=
2742.No \*(ha\*(Lt= \*(ha\*(Gt= &= \*(ha= \*(Ba=
2743.Xc
2744Assignment operators.
2745.Sm off
2746.Ao Ar var Ac Xo
2747.Aq Ar op
2748.No = Aq Ar expr
2749.Xc
2750.Sm on
2751is the same as
2752.Sm off
2753.Ao Ar var Ac Xo
2754.No = Aq Ar var
2755.Aq Ar op
2756.Aq Ar expr ,
2757.Xc
2758.Sm on
2759with any operator precedence in
2760.Aq Ar expr
2761preserved.
2762For example,
2763.Dq Li var1 *= 5 + 3
2764is the same as specifying
2765.Dq Li var1 = var1 * (5 + 3) .
2766.It \*(Ba\*(Ba
2767Logical OR;
2768the result is 1 if either argument is non-zero, 0 if not.
2769The right argument is evaluated only if the left argument is zero.
2770.It &&
2771Logical AND;
2772the result is 1 if both arguments are non-zero, 0 if not.
2773The right argument is evaluated only if the left argument is non-zero.
2774.It \*(Ba
2775Arithmetic (bit-wise) OR.
2776.It \*(ha
2777Arithmetic (bit-wise) XOR
2778(exclusive-OR).
2779.It &
2780Arithmetic (bit-wise) AND.
2781.It ==
2782Equal; the result is 1 if both arguments are equal, 0 if not.
2783.It !=
2784Not equal; the result is 0 if both arguments are equal, 1 if not.
2785.It \*(Lt
2786Less than; the result is 1 if the left argument is less than the right, 0 if
2787not.
2788.It \*(Lt= \*(Gt \*(Gt=
2789Less than or equal, greater than, greater than or equal.
2790See
2791.Ic \*(Lt .
2792.It \*(Lt\*(Lt \*(Gt\*(Gt
2793Shift left (right); the result is the left argument with its bits
2794arithmetically (signed operation) or logically (unsigned expression)
2795shifted left (right) by the amount given in the right argument.
2796.It \*(ha\*(Lt \*(ha\*(Gt
2797Rotate left (right); the result is similar to shift,
2798except that the bits shifted out at one end are shifted in
2799at the other end, instead of zero or sign bits.
2800.It + \- * /
2801Addition, subtraction, multiplication and division.
2802.It %
2803Remainder; the result is the symmetric remainder of the division of the left
2804argument by the right.
2805To get the mathematical modulus of
2806.Dq a Ic mod No b ,
2807use the formula
2808.Do
2809.Pq a % b + b
2810.No % b
2811.Dc .
2812.It Xo
2813.Sm off
2814.Aq Ar arg1 ?
2815.Aq Ar arg2 :
2816.Aq Ar arg3
2817.Sm on
2818.Xc
2819If
2820.Aq Ar arg1
2821is non-zero, the result is
2822.Aq Ar arg2 ;
2823otherwise the result is
2824.Aq Ar arg3 .
2825The non-result argument is not evaluated.
2826.El
2827.Ss Co-processes
2828A co-process (which is a pipeline created with the
2829.Dq Li \*(Ba&
2830operator) is an asynchronous process that the shell can both write to (using
2831.Ic print Fl p )
2832and read from (using
2833.Ic read Fl p ) .
2834The input and output of the co-process can also be manipulated using
2835.Ic \*(Gt&p
2836and
2837.Ic \*(Lt&p
2838redirections, respectively.
2839Once a co-process has been started, another can't
2840be started until the co-process exits, or until the co-process's input has been
2841redirected using an
2842.Ic exec Ar n Ns Ic \*(Gt&p
2843redirection.
2844If a co-process's input is redirected in this way, the next
2845co-process to be started will share the output with the first co-process,
2846unless the output of the initial co-process has been redirected using an
2847.Ic exec Ar n Ns Ic \*(Lt&p
2848redirection.
2849.Pp
2850Some notes concerning co-processes:
2851.Bl -bullet
2852.It
2853The only way to close the co-process's input (so the co-process reads an
2854end-of-file) is to redirect the input to a numbered file descriptor and then
2855close that file descriptor:
2856.Ic exec 3\*(Gt&p; exec 3\*(Gt&\-
2857.It
2858In order for co-processes to share a common output, the shell must keep the
2859write portion of the output pipe open.
2860This means that end-of-file will not be
2861detected until all co-processes sharing the co-process's output have exited
2862(when they all exit, the shell closes its copy of the pipe).
2863This can be
2864avoided by redirecting the output to a numbered file descriptor (as this also
2865causes the shell to close its copy).
2866Note that this behaviour is slightly
2867different from the original Korn shell which closes its copy of the write
2868portion of the co-process output when the most recently started co-process
2869(instead of when all sharing co-processes) exits.
2870.It
2871.Ic print Fl p
2872will ignore
2873.Dv SIGPIPE
2874signals during writes if the signal is not being trapped or ignored; the same
2875is true if the co-process input has been duplicated to another file descriptor
2876and
2877.Ic print Fl u Ns Ar n
2878is used.
2879.El
2880.Ss Functions
2881Functions are defined using either Korn shell
2882.Ic function Ar function-name
2883syntax or the Bourne/POSIX shell
2884.Ar function-name Ns \&()
2885syntax (see below for the difference between the two forms).
2886Functions are like
2887.Li .\(hyscripts
2888(i.e. scripts sourced using the
2889.Dq Li \&.
2890built-in)
2891in that they are executed in the current environment.
2892However, unlike
2893.Li .\(hyscripts ,
2894shell arguments (i.e. positional parameters $1, $2, etc.)\&
2895are never visible inside them.
2896When the shell is determining the location of a command, functions
2897are searched after special built-in commands, before builtins and the
2898.Ev PATH
2899is searched.
2900.Pp
2901An existing function may be deleted using
2902.Ic unset Fl f Ar function-name .
2903A list of functions can be obtained using
2904.Ic typeset +f
2905and the function definitions can be listed using
2906.Ic typeset Fl f .
2907The
2908.Ic autoload
2909command (which is an alias for
2910.Ic typeset Fl fu )
2911may be used to create undefined functions: when an undefined function is
2912executed, the shell searches the path specified in the
2913.Ev FPATH
2914parameter for a file with the same name as the function which, if found, is
2915read and executed.
2916If after executing the file the named function is found to
2917be defined, the function is executed; otherwise, the normal command search is
2918continued (i.e. the shell searches the regular built-in command table and
2919.Ev PATH ) .
2920Note that if a command is not found using
2921.Ev PATH ,
2922an attempt is made to autoload a function using
2923.Ev FPATH
2924(this is an undocumented feature of the original Korn shell).
2925.Pp
2926Functions can have two attributes,
2927.Dq trace
2928and
2929.Dq export ,
2930which can be set with
2931.Ic typeset Fl ft
2932and
2933.Ic typeset Fl fx ,
2934respectively.
2935When a traced function is executed, the shell's
2936.Ic xtrace
2937option is turned on for the function's duration.
2938The
2939.Dq export
2940attribute of functions is currently not used.
2941.Pp
2942Since functions are executed in the current shell environment, parameter
2943assignments made inside functions are visible after the function completes.
2944If this is not the desired effect, the
2945.Ic typeset
2946command can be used inside a function to create a local parameter.
2947Note that
2948.At
2949.Nm ksh93
2950uses static scoping (one global scope, one local scope per function)
2951and allows local variables only on Korn style functions, whereas
2952.Nm mksh
2953uses dynamic scoping (nested scopes of varying locality).
2954Note that special parameters (e.g.\&
2955.Ic \&$$ , $! )
2956can't be scoped in this way.
2957.Pp
2958The exit status of a function is that of the last command executed in the
2959function.
2960A function can be made to finish immediately using the
2961.Ic return
2962command; this may also be used to explicitly specify the exit status.
2963Note that when called in a subshell,
2964.Ic return
2965will only exit that subshell and will not cause the original shell to exit
2966a running function (see the
2967.Ic while Ns Li \&... Ns Ic read
2968loop FAQ below).
2969.Pp
2970Functions defined with the
2971.Ic function
2972reserved word are treated differently in the following ways from functions
2973defined with the
2974.Ic \&()
2975notation:
2976.Bl -bullet
2977.It
2978The $0 parameter is set to the name of the function
2979(Bourne-style functions leave $0 untouched).
2980.It
2981Parameter assignments preceding function calls are not kept in the shell
2982environment (executing Bourne-style functions will keep assignments).
2983.It
2984.Ev OPTIND
2985is saved/reset and restored on entry and exit from the function so
2986.Ic getopts
2987can be used properly both inside and outside the function (Bourne-style
2988functions leave
2989.Ev OPTIND
2990untouched, so using
2991.Ic getopts
2992inside a function interferes with using
2993.Ic getopts
2994outside the function).
2995.It
2996Shell options
2997.Pq Ic set Fl o
2998have local scope, i.e. changes inside a function are reset upon its exit.
2999.El
3000.Pp
3001In the future, the following differences may also be added:
3002.Bl -bullet
3003.It
3004A separate trap/signal environment will be used during the execution of
3005functions.
3006This will mean that traps set inside a function will not affect the
3007shell's traps and signals that are not ignored in the shell (but may be
3008trapped) will have their default effect in a function.
3009.It
3010The EXIT trap, if set in a function, will be executed after the function
3011returns.
3012.El
3013.Ss Command execution
3014After evaluation of command-line arguments, redirections and parameter
3015assignments, the type of command is determined: a special built-in command,
3016a function, a normal builtin or the name of a file to execute found using the
3017.Ev PATH
3018parameter.
3019The checks are made in the above order.
3020Special built-in commands differ from other commands in that the
3021.Ev PATH
3022parameter is not used to find them, an error during their execution can
3023cause a non-interactive shell to exit, and parameter assignments that are
3024specified before the command are kept after the command completes.
3025Regular built-in commands are different only in that the
3026.Ev PATH
3027parameter is not used to find them.
3028.Pp
3029The original
3030.Nm ksh
3031and POSIX differ somewhat in which commands are considered
3032special or regular.
3033.Pp
3034POSIX special built-in utilities:
3035.Pp
3036.Ic \&. , \&: , break , continue ,
3037.Ic eval , exec , exit , export ,
3038.Ic readonly , return , set , shift ,
3039.Ic times , trap , unset
3040.Pp
3041Additional
3042.Nm
3043commands keeping assignments:
3044.Pp
3045.Ic global , source , typeset
3046.Pp
3047Builtins that are not special:
3048.Pp
3049.Ic [ , alias , bg , bind ,
3050.Ic builtin , cat , cd , command ,
3051.Ic echo , false , fc , fg ,
3052.Ic getopts , jobs , kill , let ,
3053.Ic print , pwd , read , realpath ,
3054.Ic rename , sleep , suspend , test ,
3055.Ic true , ulimit , umask , unalias ,
3056.Ic wait , whence
3057.Pp
3058Once the type of command has been determined, any command-line parameter
3059assignments are performed and exported for the duration of the command.
3060.Pp
3061The following describes the special and regular built-in commands and
3062builtin-like reserved words:
3063.Pp
3064.Bl -tag -width false -compact
3065.It Ic \&. Ar file Op Ar arg ...
3066This is called the
3067.Dq dot
3068command.
3069Execute the commands in
3070.Ar file
3071in the current environment.
3072The file is searched for in the directories of
3073.Ev PATH .
3074If arguments are given, the positional parameters may be used to access them
3075while
3076.Ar file
3077is being executed.
3078If no arguments are given, the positional parameters are
3079those of the environment the command is used in.
3080.Pp
3081.It Ic \&: Op Ar ...
3082The null command.
3083Exit status is set to zero.
3084.Pp
3085.It Ic \&[ Ar expression Ic \&]
3086See
3087.Ic test .
3088.Pp
3089.It Xo Ic alias
3090.Oo Fl d \*(Ba t Oo Fl r Oc \*(Ba
3091.Cm +\-x Oc
3092.Op Fl p
3093.Op Cm +
3094.Oo Ar name
3095.Op Ns = Ns Ar value
3096.Ar ... Oc
3097.Xc
3098Without arguments,
3099.Ic alias
3100lists all aliases.
3101For any name without a value, the existing alias is listed.
3102Any name with a value defines an alias; see
3103.Sx Aliases
3104above.
3105.Li \&[][A\-Za\-z0\-9_!%,.@:\-]
3106are valid in names, except they may not begin with a hyphen-minus, and
3107.Ic \&[[
3108is not a valid alias name.
3109.Pp
3110When listing aliases, one of two formats is used.
3111Normally, aliases are listed as
3112.Ar name Ns = Ns Ar value ,
3113where
3114.Ar value
3115is quoted.
3116If options were preceded with
3117.Ql + ,
3118or a lone
3119.Ql +
3120is given on the command line, only
3121.Ar name
3122is printed.
3123.Pp
3124The
3125.Fl d
3126option causes directory aliases which are used in tilde expansion to be
3127listed or set (see
3128.Sx Tilde expansion
3129above).
3130.Pp
3131If the
3132.Fl p
3133option is used, each alias is prefixed with the string
3134.Dq Li alias\ \& .
3135.Pp
3136The
3137.Fl t
3138option indicates that tracked aliases are to be listed/set (values specified on
3139the command line are ignored for tracked aliases).
3140The
3141.Fl r
3142option indicates that all tracked aliases are to be reset.
3143.Pp
3144The
3145.Fl x
3146option sets
3147.Pq Ic +x No clears
3148the export attribute of an alias, or, if no names are given, lists the aliases
3149with the export attribute (exporting an alias has no effect).
3150.Pp
3151.It Ic bg Op Ar job ...
3152Resume the specified stopped job(s) in the background.
3153If no jobs are specified,
3154.Ic %+
3155is assumed.
3156See
3157.Sx Job control
3158below for more information.
3159.Pp
3160.It Ic bind Op Fl l
3161The current bindings are listed.
3162If the
3163.Fl l
3164flag is given,
3165.Ic bind
3166instead lists the names of the functions to which keys may be bound.
3167See
3168.Sx Emacs editing mode
3169for more information.
3170.Pp
3171.It Xo Ic bind Op Fl m
3172.Ar string Ns = Ns Op Ar substitute
3173.Ar ...
3174.Xc
3175.It Xo Ic bind
3176.Ar string Ns = Ns Op Ar editing-command
3177.Ar ...
3178.Xc
3179The specified editing command is bound to the given
3180.Ar string ,
3181which should consist of a control character
3182optionally preceded by one of the two prefix characters
3183and optionally succeeded by a tilde character.
3184Future input of the
3185.Ar string
3186will cause the editing command to be immediately invoked.
3187If the
3188.Fl m
3189flag is given, the specified input
3190.Ar string
3191will afterwards be immediately replaced by the given
3192.Ar substitute
3193string which may contain editing commands but not other macros.
3194If a tilde postfix is given, a tilde trailing the one or
3195two prefices and the control character is ignored, any
3196other trailing character will be processed afterwards.
3197.Pp
3198Control characters may be written using caret notation
3199i.e. \*(haX represents Ctrl-X.
3200The caret itself can be escaped by a backslash, which also escapes itself.
3201Note that although only three prefix characters (usually ESC, \*(haX and NUL)
3202are supported, some multi-character sequences can be supported.
3203.Pp
3204The following default bindings show how the arrow keys, the home, end and
3205delete key on a BSD wsvt25, xterm\-xfree86 or GNU screen terminal are bound
3206(of course some escape sequences won't work out quite this nicely):
3207.Bd -literal -offset indent
3208bind \*(aq\*(haX\*(aq=prefix\-2
3209bind \*(aq\*(ha[[\*(aq=prefix\-2
3210bind \*(aq\*(haXA\*(aq=up\-history
3211bind \*(aq\*(haXB\*(aq=down\-history
3212bind \*(aq\*(haXC\*(aq=forward\-char
3213bind \*(aq\*(haXD\*(aq=backward\-char
3214bind \*(aq\*(haX1\*(TI\*(aq=beginning\-of\-line
3215bind \*(aq\*(haX7\*(TI\*(aq=beginning\-of\-line
3216bind \*(aq\*(haXH\*(aq=beginning\-of\-line
3217bind \*(aq\*(haX4\*(TI\*(aq=end\-of\-line
3218bind \*(aq\*(haX8\*(TI\*(aq=end\-of\-line
3219bind \*(aq\*(haXF\*(aq=end\-of\-line
3220bind \*(aq\*(haX3\*(TI\*(aq=delete\-char\-forward
3221.Ed
3222.Pp
3223.It Ic break Op Ar level
3224Exit the
3225.Ar level Ns th
3226inner-most
3227.Ic for ,
3228.Ic select ,
3229.Ic until
3230or
3231.Ic while
3232loop.
3233.Ar level
3234defaults to 1.
3235.Pp
3236.It Xo
3237.Ic builtin
3238.Op Fl \-
3239.Ar command Op Ar arg ...
3240.Xc
3241Execute the built-in command
3242.Ar command .
3243.Pp
3244.It Xo
3245.Ic \ebuiltin
3246.Ar command Op Ar arg ...
3247.Xc
3248Same as
3249.Ic builtin .
3250Additionally acts as declaration utility forwarder, i.e. this is a
3251declaration utility (see
3252.Sx Tilde expansion )
3253.No iff Ar command
3254is a declaration utility.
3255.Pp
3256.It Xo
3257.Ic cat
3258.Op Fl u
3259.Op Ar
3260.Xc
3261Read files sequentially, in command line order, and write them to
3262standard output.
3263If a
3264.Ar file
3265is a single dash
3266.Pq Dq Li \-
3267or absent, read from standard input.
3268For direct builtin calls, the
3269.Tn POSIX
3270.Fl u
3271option is supported as a no-op.
3272For calls from shell, if any options are given, an external
3273.Xr cat 1
3274utility is preferred over the builtin.
3275.Pp
3276.It Xo
3277.Ic cd
3278.Op Fl L
3279.Op Ar dir
3280.Xc
3281.It Xo
3282.Ic cd
3283.Fl P Op Fl e
3284.Op Ar dir
3285.Xc
3286.It Xo
3287.Ic chdir
3288.Op Fl eLP
3289.Op Ar dir
3290.Xc
3291Set the working directory to
3292.Ar dir .
3293If the parameter
3294.Ev CDPATH
3295is set, it lists the search path for the directory containing
3296.Ar dir .
3297An unset or empty path means the current directory.
3298If
3299.Ar dir
3300is found in any component of the
3301.Ev CDPATH
3302search path other than an unset or empty path,
3303the name of the new working directory will be written to standard output.
3304If
3305.Ar dir
3306is missing, the home directory
3307.Ev HOME
3308is used.
3309If
3310.Ar dir
3311is
3312.Dq Li \- ,
3313the previous working directory is used (see the
3314.Ev OLDPWD
3315parameter).
3316.Pp
3317If the
3318.Fl L
3319option (logical path) is used or if the
3320.Ic physical
3321option isn't set (see the
3322.Ic set
3323command below), references to
3324.Dq Li ..
3325in
3326.Ar dir
3327are relative to the path used to get to the directory.
3328If the
3329.Fl P
3330option (physical path) is used or if the
3331.Ic physical
3332option is set,
3333.Dq Li ..
3334is relative to the filesystem directory tree.
3335The
3336.Ev PWD
3337and
3338.Ev OLDPWD
3339parameters are updated to reflect the current and old working directory,
3340respectively.
3341If the
3342.Fl e
3343option is set for physical filesystem traversal and
3344.Ev PWD
3345could not be set, the exit code is 1; greater than 1 if an
3346error occurred, 0 otherwise.
3347.Pp
3348.It Xo
3349.Ic cd
3350.Op Fl eLP
3351.Ar old new
3352.Xc
3353.It Xo
3354.Ic chdir
3355.Op Fl eLP
3356.Ar old new
3357.Xc
3358The string
3359.Ar new
3360is substituted for
3361.Ar old
3362in the current directory, and the shell attempts to change to the new
3363directory.
3364.Pp
3365.It Xo
3366.Ic command
3367.Op Fl pVv
3368.Ar cmd
3369.Op Ar arg ...
3370.Xc
3371If neither the
3372.Fl v
3373nor
3374.Fl V
3375option is given,
3376.Ar cmd
3377is executed exactly as if
3378.Ic command
3379had not been specified, with two exceptions:
3380firstly,
3381.Ar cmd
3382cannot be a shell function;
3383and secondly, special built-in commands lose their specialness
3384(i.e. redirection and utility errors do not cause the shell to
3385exit, and command assignments are not permanent).
3386The declaration utility property is not reset.
3387.Pp
3388If the
3389.Fl p
3390option is given, a default search path is used instead of the current value of
3391.Ev PATH ,
3392the actual value of which is system dependent.
3393.Pp
3394If the
3395.Fl v
3396option is given, instead of executing
3397.Ar cmd ,
3398information about what would be executed is given (and the same is done for
3399.Ar arg ... ) .
3400For builtins, functions and keywords, their names are simply printed;
3401for aliases, a command that defines them is printed;
3402for utilities found by searching the
3403.Ev PATH
3404parameter, the full path of the command is printed.
3405If no command is found
3406(i.e. the path search fails), nothing is printed and
3407.Ic command
3408exits with a non-zero status.
3409The
3410.Fl V
3411option is like the
3412.Fl v
3413option, except it is more verbose.
3414.Pp
3415.It Ic continue Op Ar level
3416Jumps to the beginning of the
3417.Ar level Ns th
3418inner-most
3419.Ic for ,
3420.Ic select ,
3421.Ic until
3422or
3423.Ic while
3424loop.
3425.Ar level
3426defaults to 1.
3427.Pp
3428.It Xo
3429.Ic echo
3430.Op Fl Een
3431.Op Ar arg ...
3432.Xc
3433.Em Warning:
3434this utility is not portable; use the Korn shell builtin
3435.Ic print
3436instead.
3437.Pp
3438Prints its arguments (separated by spaces) followed by a newline, to the
3439standard output.
3440The newline is suppressed if any of the arguments contain the
3441backslash sequence
3442.Dq Li \ec .
3443See the
3444.Ic print
3445command below for a list of other backslash sequences that are recognised.
3446.Pp
3447The options are provided for compatibility with
3448.Bx
3449shell scripts.
3450The
3451.Fl n
3452option suppresses the trailing newline,
3453.Fl e
3454enables backslash interpretation (a no-op, since this is normally done), and
3455.Fl E
3456suppresses backslash interpretation.
3457.Pp
3458If the
3459.Ic posix
3460or
3461.Ic sh
3462option is set or this is a direct builtin call or
3463.Ic print
3464.Fl R ,
3465only the first argument is treated as an option, and only if it is exactly
3466.Dq Li \-n .
3467Backslash interpretation is disabled.
3468.Pp
3469.It Ic eval Ar command ...
3470The arguments are concatenated (with spaces between them) to form a single
3471string which the shell then parses and executes in the current environment.
3472.Pp
3473.It Xo
3474.Ic exec
3475.Op Fl a Ar argv0
3476.Op Fl c
3477.Op Ar command Op Ar arg ...
3478.Xc
3479The command is executed without forking, replacing the shell process.
3480This is currently absolute, i.e.\&
3481.Ic exec
3482never returns, even if the
3483.Ar command
3484is not found.
3485The
3486.Fl a
3487option permits setting a different
3488.Li argv[0]
3489value, and
3490.Fl c
3491clears the environment before executing the child process, except for the
3492.Ev _
3493variable and direct assignments.
3494.Pp
3495If no command is given except for I/O redirection, the I/O redirection is
3496permanent and the shell is
3497not replaced.
3498Any file descriptors greater than 2 which are opened or
3499.Xr dup 2 Ns 'd
3500in this way are not made available to other executed commands (i.e. commands
3501that are not built-in to the shell).
3502Note that the Bourne shell differs here;
3503it does pass these file descriptors on.
3504.Pp
3505.It Ic exit Op Ar status
3506The shell or subshell exits with the specified exit status.
3507If
3508.Ar status
3509is not specified, the exit status is the current value of the
3510.Ic \&$?
3511parameter.
3512.Pp
3513.It Xo
3514.Ic export
3515.Op Fl p
3516.Op Ar parameter Ns Op = Ns Ar value
3517.Xc
3518Sets the export attribute of the named parameters.
3519Exported parameters are passed in the environment to executed commands.
3520If values are specified, the named parameters are also assigned.
3521This is a declaration utility.
3522.Pp
3523If no parameters are specified, all parameters with the export attribute
3524set are printed one per line; either their names, or, if a
3525.Dq Li \-
3526with no option letter is specified, name=value pairs, or, with
3527.Fl p ,
3528.Ic export
3529commands suitable for re-entry.
3530.Pp
3531.It Ic false
3532A command that exits with a non-zero status.
3533.Pp
3534.It Xo
3535.Ic fc
3536.Oo Fl e Ar editor \*(Ba
3537.Fl l Op Fl n Oc
3538.Op Fl r
3539.Op Ar first Op Ar last
3540.Xc
3541.Ar first
3542and
3543.Ar last
3544select commands from the history.
3545Commands can be selected by history number
3546(negative numbers go backwards from the current, most recent, line)
3547or a string specifying the most recent command starting with that string.
3548The
3549.Fl l
3550option lists the command on standard output, and
3551.Fl n
3552inhibits the default command numbers.
3553The
3554.Fl r
3555option reverses the order of the list.
3556Without
3557.Fl l ,
3558the selected commands are edited by the editor specified with the
3559.Fl e
3560option or, if no
3561.Fl e
3562is specified, the editor specified by the
3563.Ev FCEDIT
3564parameter (if this parameter is not set,
3565.Pa /bin/ed
3566is used), and then executed by the shell.
3567.Pp
3568.It Xo
3569.Ic fc
3570.Cm \-e \- \*(Ba Fl s
3571.Op Fl g
3572.Op Ar old Ns = Ns Ar new
3573.Op Ar prefix
3574.Xc
3575Re-execute the selected command (the previous command by default) after
3576performing the optional substitution of
3577.Ar old
3578with
3579.Ar new .
3580If
3581.Fl g
3582is specified, all occurrences of
3583.Ar old
3584are replaced with
3585.Ar new .
3586The meaning of
3587.Cm \-e \-
3588and
3589.Fl s
3590is identical: re-execute the selected command without invoking an editor.
3591This command is usually accessed with the predefined:
3592.Ic alias r=\*(aqfc \-e \-\*(aq
3593.Pp
3594.It Ic fg Op Ar job ...
3595Resume the specified job(s) in the foreground.
3596If no jobs are specified,
3597.Ic %+
3598is assumed.
3599See
3600.Sx Job control
3601below for more information.
3602.Pp
3603.It Xo
3604.Ic getopts
3605.Ar optstring name
3606.Op Ar arg ...
3607.Xc
3608Used by shell procedures to parse the specified arguments (or positional
3609parameters, if no arguments are given) and to check for legal options.
3610.Ar optstring
3611contains the option letters that
3612.Ic getopts
3613is to recognise.
3614If a letter is followed by a colon, the option is expected to
3615have an argument.
3616Options that do not take arguments may be grouped in a single argument.
3617If an option takes an argument and the option character is not the
3618last character of the argument it is found in, the remainder of the argument is
3619taken to be the option's argument; otherwise, the next argument is the option's
3620argument.
3621.Pp
3622Each time
3623.Ic getopts
3624is invoked, it places the next option in the shell parameter
3625.Ar name
3626and the index of the argument to be processed by the next call to
3627.Ic getopts
3628in the shell parameter
3629.Ev OPTIND .
3630If the option was introduced with a
3631.Ql + ,
3632the option placed in
3633.Ar name
3634is prefixed with a
3635.Ql + .
3636When an option requires an argument,
3637.Ic getopts
3638places it in the shell parameter
3639.Ev OPTARG .
3640.Pp
3641When an illegal option or a missing option argument is encountered, a question
3642mark or a colon is placed in
3643.Ar name
3644(indicating an illegal option or missing argument, respectively) and
3645.Ev OPTARG
3646is set to the option character that caused the problem.
3647Furthermore, if
3648.Ar optstring
3649does not begin with a colon, a question mark is placed in
3650.Ar name ,
3651.Ev OPTARG
3652is unset, and an error message is printed to standard error.
3653.Pp
3654When the end of the options is encountered,
3655.Ic getopts
3656exits with a non-zero exit status.
3657Options end at the first (non-option
3658argument) argument that does not start with a
3659.Ql \- ,
3660or when a
3661.Dq Li \-\-
3662argument is encountered.
3663.Pp
3664Option parsing can be reset by setting
3665.Ev OPTIND
3666to 1 (this is done automatically whenever the shell or a shell procedure is
3667invoked).
3668.Pp
3669Warning: Changing the value of the shell parameter
3670.Ev OPTIND
3671to a value other than 1 or parsing different sets of arguments without
3672resetting
3673.Ev OPTIND
3674may lead to unexpected results.
3675.Pp
3676.It Xo
3677.Ic global
3678.Op Ic +\-aglpnrtUux
3679.Oo Fl L Ns Op Ar n
3680.No \*(Ba Fl R Ns Op Ar n
3681.No \*(Ba Fl Z Ns Op Ar n Oc
3682.Op Fl i Ns Op Ar n
3683.Oo Ar name
3684.Op Ns = Ns Ar value
3685.Ar ... Oc
3686.Xc
3687See
3688.Ic typeset Fl g .
3689.No Deprecated , Em will
3690be removed from a future version of
3691.Nm .
3692.Pp
3693.It Xo
3694.Ic hash
3695.Op Fl r
3696.Op Ar name ...
3697.Xc
3698Without arguments, any hashed executable command pathnames are listed.
3699The
3700.Fl r
3701option causes all hashed commands to be removed from the hash table.
3702Each
3703.Ar name
3704is searched as if it were a command name and added to the hash table if it is
3705an executable command.
3706.Pp
3707.It Xo
3708.Ic jobs
3709.Op Fl lnp
3710.Op Ar job ...
3711.Xc
3712Display information about the specified job(s); if no jobs are specified, all
3713jobs are displayed.
3714The
3715.Fl n
3716option causes information to be displayed only for jobs that have changed
3717state since the last notification.
3718If the
3719.Fl l
3720option is used, the process ID of each process in a job is also listed.
3721The
3722.Fl p
3723option causes only the process group of each job to be printed.
3724See
3725.Sx Job control
3726below for the format of
3727.Ar job
3728and the displayed job.
3729.Pp
3730.It Xo
3731.Ic kill
3732.Oo Fl s Ar signame \*(Ba
3733.No \- Ns Ar signum \*(Ba
3734.No \- Ns Ar signame Oc
3735.No { Ar job \*(Ba pid \*(Ba pgrp No }
3736.Ar ...
3737.Xc
3738Send the specified signal to the specified jobs, process IDs or process
3739groups.
3740If no signal is specified, the
3741.Dv TERM
3742signal is sent.
3743If a job is specified, the signal is sent to the job's process group.
3744See
3745.Sx Job control
3746below for the format of
3747.Ar job .
3748.Pp
3749.It Xo
3750.Ic kill
3751.Fl l
3752.Op Ar exit-status ...
3753.Xc
3754Print the signal name corresponding to
3755.Ar exit-status .
3756If no arguments are specified, a list of all the signals with their numbers
3757and a short description of each are printed.
3758.Pp
3759.It Ic let Op Ar expression ...
3760Each expression is evaluated (see
3761.Sx Arithmetic expressions
3762above).
3763If all expressions are successfully evaluated, the exit status is 0 (1)
3764if the last expression evaluated to non-zero (zero).
3765If an error occurs during
3766the parsing or evaluation of an expression, the exit status is greater than 1.
3767Since expressions may need to be quoted,
3768.No \&(( Ar expr No ))
3769is syntactic sugar for:
3770.Dl "{ \e\ebuiltin let \*(aq" Ns Ar expr Ns "\*(aq; }"
3771.Pp
3772.It Xo
3773.Ic mknod
3774.Op Fl m Ar mode
3775.Ar name
3776.Cm b\*(Bac
3777.Ar major minor
3778.Xc
3779.It Xo
3780.Ic mknod
3781.Op Fl m Ar mode
3782.Ar name
3783.Cm p
3784.Xc
3785Create a device special file.
3786The file type may be
3787.Cm b
3788(block type device),
3789.Cm c
3790(character type device)
3791or
3792.Cm p
3793.Pq named pipe , Tn FIFO .
3794The file created may be modified according to its
3795.Ar mode
3796(via the
3797.Fl m
3798option),
3799.Ar major
3800(major device number),
3801and
3802.Ar minor
3803(minor device number).
3804This is not normally part of
3805.Nm mksh ;
3806however, distributors may have added this as builtin as a speed hack.
3807.Pp
3808.It Xo
3809.Ic print
3810.Oo Fl AcelNnprsu Ns Oo Ar n Oc \*(Ba
3811.Fl R Op Fl n Oc
3812.Op Ar argument ...
3813.Xc
3814Print the specified argument(s) on the standard output,
3815separated by spaces, terminated with a newline.
3816The escapes mentioned in
3817.Sx Backslash expansion
3818above, as well as
3819.Dq Li \ec ,
3820which is equivalent to using the
3821.Fl n
3822option, are interpreted.
3823.Pp
3824The options are as follows:
3825.Bl -tag -width Ds
3826.It Fl A
3827Each
3828.Ar argument
3829is arithmetically evaluated; the character corresponding to the
3830resulting value is printed.
3831Empty
3832.Ar argument Ns s
3833separate input words.
3834.It Fl c
3835The output is printed columnised, line by line, similar to how the
3836.Xr rs 1
3837utility, tab completion, the
3838.Ic kill Fl l
3839built-in utility and the
3840.Ic select
3841statement do.
3842.It Fl e
3843Restore backslash expansion after a previous
3844.Fl r .
3845.It Fl l
3846Change the output word separator to newline.
3847.It Fl N
3848Change the output word and line separator to ASCII NUL.
3849.It Fl n
3850Do not print the trailing line separator.
3851.It Fl p
3852Print to the co-process (see
3853.Sx Co-processes
3854above).
3855.It Fl r
3856Inhibit backslash expansion.
3857.It Fl s
3858Print to the history file instead of standard output.
3859.It Fl u Ns Op Ar n
3860Print to the file descriptor
3861.Ar n Pq defaults to 1 if omitted
3862instead of standard output.
3863.El
3864.Pp
3865The
3866.Fl R
3867option mostly emulates the
3868.Bx
3869.Xr echo 1
3870command which does not expand backslashes and interprets
3871its first argument as option only if it is exactly
3872.Dq Li \-n
3873.Pq to suppress the trailing newline .
3874.Pp
3875.It Ic pwd Op Fl LP
3876Print the present working directory.
3877If the
3878.Fl L
3879option is used or if the
3880.Ic physical
3881option isn't set (see the
3882.Ic set
3883command below), the logical path is printed (i.e. the path used to
3884.Ic cd
3885to the current directory).
3886If the
3887.Fl P
3888option (physical path) is used or if the
3889.Ic physical
3890option is set, the path determined from the filesystem (by following
3891.Dq Li ..
3892directories to the root directory) is printed.
3893.Pp
3894.It Xo
3895.Ic read
3896.Op Fl A \*(Ba Fl a
3897.Op Fl d Ar x
3898.Oo Fl N Ar z \*(Ba
3899.Fl n Ar z Oc
3900.Oo Fl p \*(Ba
3901.Fl u Ns Op Ar n
3902.Oc Op Fl t Ar n
3903.Op Fl rs
3904.Op Ar p ...
3905.Xc
3906Reads a line of input, separates the input into fields using the
3907.Ev IFS
3908parameter (see
3909.Sx Substitution
3910above), and assigns each field to the specified parameters
3911.Ar p .
3912If no parameters are specified, the
3913.Ev REPLY
3914parameter is used to store the result.
3915With the
3916.Fl A
3917and
3918.Fl a
3919options, only no or one parameter is accepted.
3920If there are more parameters than fields, the extra parameters are set to
3921the empty string or 0; if there are more fields than parameters, the last
3922parameter is assigned the remaining fields (including the word separators).
3923.Pp
3924The options are as follows:
3925.Bl -tag -width XuXnX
3926.It Fl A
3927Store the result into the parameter
3928.Ar p
3929(or
3930.Ev REPLY )
3931as array of words.
3932.It Fl a
3933Store the result without word splitting into the parameter
3934.Ar p
3935(or
3936.Ev REPLY )
3937as array of characters (wide characters if the
3938.Ic utf8\-mode
3939option is enacted, octets otherwise); the codepoints are
3940encoded as decimal numbers by default.
3941.It Fl d Ar x
3942Use the first byte of
3943.Ar x ,
3944.Dv NUL
3945if empty, instead of the ASCII newline character as input line delimiter.
3946.It Fl N Ar z
3947Instead of reading till end-of-line, read exactly
3948.Ar z
3949bytes.
3950Upon EOF, a partial read is returned with exit status 1.
3951After timeout, a partial read is returned with an exit status as if
3952.Dv SIGALRM
3953were caught.
3954.It Fl n Ar z
3955Instead of reading till end-of-line, read up to
3956.Ar z
3957bytes but return as soon as any bytes are read, e.g.\& from a
3958slow terminal device, or if EOF or a timeout occurs.
3959.It Fl p
3960Read from the currently active co-process, see
3961.Sx Co-processes
3962above for details on this.
3963.It Fl u Ns Op Ar n
3964Read from the file descriptor
3965.Ar n
3966(defaults to 0, i.e.\& standard input).
3967The argument must immediately follow the option character.
3968.It Fl t Ar n
3969Interrupt reading after
3970.Ar n
3971seconds (specified as positive decimal value with an optional fractional part).
3972The exit status of
3973.Nm read
3974is the same as if
3975.Dv SIGALRM
3976were caught if the timeout occurred, but partial reads may still be returned.
3977.It Fl r
3978Normally, the ASCII backslash character escapes the special
3979meaning of the following character and is stripped from the input;
3980.Ic read
3981does not stop when encountering a backslash-newline sequence and
3982does not store that newline in the result.
3983This option enables raw mode, in which backslashes are not processed.
3984.It Fl s
3985The input line is saved to the history.
3986.El
3987.Pp
3988If the input is a terminal, both the
3989.Fl N
3990and
3991.Fl n
3992options set it into raw mode;
3993they read an entire file if \-1 is passed as
3994.Ar z
3995argument.
3996.Pp
3997The first parameter may have a question mark and a string appended to it, in
3998which case the string is used as a prompt (printed to standard error before
3999any input is read) if the input is a
4000.Xr tty 4
4001(e.g.\&
4002.Ic read nfoo?\*(aqnumber of foos: \*(aq ) .
4003.Pp
4004If no input is read or a timeout occurred,
4005.Ic read
4006exits with a non-zero status.
4007.Pp
4008.It Xo
4009.Ic readonly
4010.Op Fl p
4011.Oo Ar parameter
4012.Op Ns = Ns Ar value
4013.Ar ... Oc
4014.Xc
4015Sets the read-only attribute of the named parameters.
4016This is a declaration utility.
4017If values are given,
4018parameters are set to them before setting the attribute.
4019Once a parameter is
4020made read-only, it cannot be unset and its value cannot be changed.
4021.Pp
4022If no parameters are specified, the names of all parameters with the read-only
4023attribute are printed one per line, unless the
4024.Fl p
4025option is used, in which case
4026.Ic readonly
4027commands defining all read-only parameters, including their values, are
4028printed.
4029.Pp
4030.It Xo
4031.Ic realpath
4032.Op Fl \-
4033.Ar name
4034.Xc
4035Prints the resolved absolute pathname corresponding to
4036.Ar name .
4037If
4038.Ar name
4039ends with a slash
4040.Pq Ql / ,
4041it's also checked for existence and whether it is a directory; otherwise,
4042.Ic realpath
4043returns 0 if the pathname either exists or can be created immediately,
4044i.e. all but the last component exist and are directories.
4045For calls from the shell, if any options are given, an external
4046.Xr realpath 1
4047utility is preferred over the builtin.
4048.Pp
4049.It Xo
4050.Ic rename
4051.Op Fl \-
4052.Ar from to
4053.Xc
4054Renames the file
4055.Ar from
4056to
4057.Ar to .
4058Both must be complete pathnames and on the same device.
4059An external utility is preferred over this builtin,
4060which is intended for emergency situations
4061.Pq where Pa /bin/mv No becomes unusable
4062and directly calls
4063.Xr rename 2 .
4064.Pp
4065.It Ic return Op Ar status
4066Returns from a function or
4067.Ic \&.
4068script, with exit status
4069.Ar status .
4070If no
4071.Ar status
4072is given, the exit status of the last executed command is used.
4073If used outside of a function or
4074.Ic \&.
4075script, it has the same effect as
4076.Ic exit .
4077Note that
4078.Nm
4079treats both profile and
4080.Ev ENV
4081files as
4082.Ic \&.
4083scripts, while the original Korn shell only treats profiles as
4084.Ic \&.
4085scripts.
4086.Pp
4087.It Xo
4088.Ic set Op Ic +\-abCefhiklmnprsUuvXx
4089.Op Ic +\-o Ar option
4090.Op Ic +\-A Ar name
4091.Op Fl \-
4092.Op Ar arg ...
4093.Xc
4094The
4095.Ic set
4096command can be used to set
4097.Pq Ic \-
4098or clear
4099.Pq Ic +
4100shell options, set the positional parameters, or set an array parameter.
4101Options can be changed using the
4102.Cm +\-o Ar option
4103syntax, where
4104.Ar option
4105is the long name of an option, or using the
4106.Cm +\- Ns Ar letter
4107syntax, where
4108.Ar letter
4109is the option's single letter name (not all options have a single letter name).
4110The following table lists both option letters (if they exist) and long names
4111along with a description of what the option does:
4112.Bl -tag -width 3n
4113.It Fl A Ar name
4114Sets the elements of the array parameter
4115.Ar name
4116to
4117.Ar arg ...
4118If
4119.Fl A
4120is used, the array is reset (i.e. emptied) first; if
4121.Ic +A
4122is used, the first N elements are set (where N is the number of arguments);
4123the rest are left untouched.
4124.Pp
4125An alternative syntax for the command
4126.Ic set \-A foo \-\- a b c
4127which is compatible to
4128.Tn GNU
4129.Nm bash
4130and also supported by
4131.At
4132.Nm ksh93
4133is:
4134.Ic foo=(a b c); foo+=(d e)
4135.It Fl a \*(Ba Fl o Ic allexport
4136All new parameters are created with the export attribute.
4137.It Fl b \*(Ba Fl o Ic notify
4138Print job notification messages asynchronously, instead of just before the
4139prompt.
4140Only used if job control is enabled
4141.Pq Fl m .
4142.It Fl C \*(Ba Fl o Ic noclobber
4143Prevent \*(Gt redirection from overwriting existing files.
4144Instead, \*(Gt\*(Ba must be used to force an overwrite.
4145Note that this is not safe to use for creation of temporary files or
4146lockfiles due to a TOCTOU in a check allowing one to redirect output to
4147.Pa /dev/null
4148or other device files even in
4149.Ic noclobber
4150mode.
4151.It Fl e \*(Ba Fl o Ic errexit
4152Exit (after executing the
4153.Dv ERR
4154trap) as soon as an error occurs or a command fails (i.e. exits with a
4155non-zero status).
4156This does not apply to commands whose exit status is
4157explicitly tested by a shell construct such as
4158.Ic if ,
4159.Ic until ,
4160.Ic while
4161or
4162.Ic \&!
4163statements.
4164For
4165.Ic &&
4166or
4167.Ic \*(Ba\*(Ba ,
4168only the status of the last command is tested.
4169.It Fl f \*(Ba Fl o Ic noglob
4170Do not expand file name patterns.
4171.It Fl h \*(Ba Fl o Ic trackall
4172Create tracked aliases for all executed commands (see
4173.Sx Aliases
4174above).
4175Enabled by default for non-interactive shells.
4176.It Fl i \*(Ba Fl o Ic interactive
4177The shell is an interactive shell.
4178This option can only be used when the shell is invoked.
4179See above for a description of what this means.
4180.It Fl k \*(Ba Fl o Ic keyword
4181Parameter assignments are recognised anywhere in a command.
4182.It Fl l \*(Ba Fl o Ic login
4183The shell is a login shell.
4184This option can only be used when the shell is invoked.
4185See above for a description of what this means.
4186.It Fl m \*(Ba Fl o Ic monitor
4187Enable job control (default for interactive shells).
4188.It Fl n \*(Ba Fl o Ic noexec
4189Do not execute any commands.
4190Useful for checking the syntax of scripts
4191(ignored if interactive).
4192.It Fl p \*(Ba Fl o Ic privileged
4193The shell is a privileged shell.
4194It is set automatically if, when the shell starts,
4195the real UID or GID does not match
4196the effective UID (EUID) or GID (EGID), respectively.
4197See above for a description of what this means.
4198.It Fl r \*(Ba Fl o Ic restricted
4199The shell is a restricted shell.
4200This option can only be used when the shell is invoked.
4201See above for a description of what this means.
4202.It Fl s \*(Ba Fl o Ic stdin
4203If used when the shell is invoked, commands are read from standard input.
4204Set automatically if the shell is invoked with no arguments.
4205.Pp
4206When
4207.Fl s
4208is used with the
4209.Ic set
4210command it causes the specified arguments to be sorted before assigning them to
4211the positional parameters (or to array
4212.Ar name ,
4213if
4214.Fl A
4215is used).
4216.It Fl U \*(Ba Fl o Ic utf8\-mode
4217Enable UTF-8 support in the
4218.Sx Emacs editing mode
4219and internal string handling functions.
4220This flag is disabled by default, but can be enabled by setting it on the
4221shell command line; is enabled automatically for interactive shells if
4222requested at compile time, your system supports
4223.Fn setlocale LC_CTYPE \&""
4224and optionally
4225.Fn nl_langinfo CODESET ,
4226or the
4227.Ev LC_ALL ,
4228.Ev LC_CTYPE
4229or
4230.Ev LANG
4231environment variables,
4232and at least one of these returns something that matches
4233.Dq UTF\-8
4234or
4235.Dq utf8
4236case-insensitively; for direct builtin calls depending on the
4237aforementioned environment variables; or for stdin or scripts,
4238if the input begins with a UTF-8 Byte Order Mark.
4239.Pp
4240In near future, locale tracking will be implemented, which means that
4241.Ic set Fl +U
4242is changed whenever one of the
4243.Tn POSIX
4244locale-related environment variables changes.
4245.It Fl u \*(Ba Fl o Ic nounset
4246Referencing of an unset parameter, other than
4247.Dq Li $@
4248or
4249.Dq Li $* ,
4250is treated as an error, unless one of the
4251.Ql \- ,
4252.Ql +
4253or
4254.Ql =
4255modifiers is used.
4256.It Fl v \*(Ba Fl o Ic verbose
4257Write shell input to standard error as it is read.
4258.It Fl X \*(Ba Fl o Ic markdirs
4259Mark directories with a trailing
4260.Ql /
4261during file name generation.
4262.It Fl x \*(Ba Fl o Ic xtrace
4263Print command trees when they are executed, preceded by
4264the value of
4265.Ev PS4 .
4266.It Fl o Ic bgnice
4267Background jobs are run with lower priority.
4268.It Fl o Ic braceexpand
4269Enable brace expansion (a.k.a. alternation).
4270This is enabled by default.
4271.It Fl o Ic emacs
4272Enable BRL emacs-like command-line editing (interactive shells only); see
4273.Sx Emacs editing mode .
4274.It Fl o Ic gmacs
4275Enable gmacs-like command-line editing (interactive shells only).
4276Currently identical to emacs editing except that transpose\-chars (\*(haT) acts
4277slightly differently.
4278.It Fl o Ic ignoreeof
4279The shell will not (easily) exit when end-of-file is read;
4280.Ic exit
4281must be used.
4282To avoid infinite loops, the shell will exit if
4283.Dv EOF
4284is read 13 times in a row.
4285.It Fl o Ic inherit\-xtrace
4286Do not reset
4287.Fl o Ic xtrace
4288upon entering functions.
4289This is enabled by default.
4290.It Fl o Ic nohup
4291Do not kill running jobs with a
4292.Dv SIGHUP
4293signal when a login shell exits.
4294Currently set by default, but this may
4295change in the future to be compatible with
4296.At
4297.Nm ksh ,
4298which
4299doesn't have this option, but does send the
4300.Dv SIGHUP
4301signal.
4302.It Fl o Ic nolog
4303No effect.
4304In the original Korn shell, this prevents function definitions from
4305being stored in the history file.
4306.It Fl o Ic physical
4307Causes the
4308.Ic cd
4309and
4310.Ic pwd
4311commands to use
4312.Dq physical
4313(i.e. the filesystem's)
4314.Dq Li ..
4315directories instead of
4316.Dq logical
4317directories (i.e. the shell handles
4318.Dq Li .. ,
4319which allows the user to be oblivious of symbolic links to directories).
4320Clear by default.
4321Note that setting this option does not affect the current value of the
4322.Ev PWD
4323parameter; only the
4324.Ic cd
4325command changes
4326.Ev PWD .
4327See the
4328.Ic cd
4329and
4330.Ic pwd
4331commands above for more details.
4332.It Fl o Ic pipefail
4333Make the exit status of a pipeline (before logically complementing) the
4334rightmost non-zero errorlevel, or zero if all commands exited with zero.
4335.It Fl o Ic posix
4336Behave closer to the standards
4337(see
4338.Sx POSIX mode
4339for details).
4340Automatically enabled if the basename of the shell invocation begins with
4341.Dq sh
4342and this autodetection feature is compiled in
4343.Pq not in MirBSD .
4344As a side effect, setting this flag turns off the
4345.Ic braceexpand
4346and
4347.Ic utf8\-mode
4348flags, which can be turned back on manually, and
4349.Ic sh
4350mode (unless both are enabled at the same time).
4351.It Fl o Ic sh
4352Enable
4353.Pa /bin/sh
4354.Pq kludge
4355mode (see
4356.Sx SH mode ) .
4357Automatically enabled if the basename of the shell invocation begins with
4358.Dq sh
4359and this autodetection feature is compiled in
4360.Pq not in MirBSD .
4361As a side effect, setting this flag turns off
4362.Ic braceexpand
4363mode, which can be turned back on manually, and
4364.Ic posix
4365mode (unless both are enabled at the same time).
4366.It Fl o Ic vi
4367Enable
4368.Xr vi 1 Ns -like
4369command-line editing (interactive shells only).
4370See
4371.Sx Vi editing mode
4372for documentation and limitations.
4373.It Fl o Ic vi\-esccomplete
4374In vi command-line editing, do command and file name completion when escape
4375(\*(ha[) is entered in command mode.
4376.It Fl o Ic vi\-tabcomplete
4377In vi command-line editing, do command and file name completion when tab (\*(haI)
4378is entered in insert mode.
4379This is the default.
4380.It Fl o Ic viraw
4381No effect.
4382In the original Korn shell, unless
4383.Ic viraw
4384was set, the vi command-line mode would let the
4385.Xr tty 4
4386driver do the work until ESC (\*(ha[) was entered.
4387.Nm
4388is always in viraw mode.
4389.El
4390.Pp
4391These options can also be used upon invocation of the shell.
4392The current set of
4393options (with single letter names) can be found in the parameter
4394.Dq Li $\- .
4395.Ic set Fl o
4396with no option name will list all the options and whether each is on or off;
4397.Ic set +o
4398will print the long names of all options that are currently on.
4399In a future version,
4400.Ic set +o
4401will behave
4402.Tn POSIX
4403compliant and print commands to restore the current options instead.
4404.Pp
4405Remaining arguments, if any, are positional parameters and are assigned, in
4406order, to the positional parameters (i.e. $1, $2, etc.).
4407If options end with
4408.Dq Li \-\-
4409and there are no remaining arguments, all positional parameters are cleared.
4410If no options or arguments are given, the values of all names are printed.
4411For unknown historical reasons, a lone
4412.Dq Li \-
4413option is treated specially \*(en it clears both the
4414.Fl v
4415and
4416.Fl x
4417options.
4418.Pp
4419.It Ic shift Op Ar number
4420The positional parameters
4421.Ar number Ns +1 ,
4422.Ar number Ns +2 ,
4423etc. are renamed to 1, 2, etc.
4424.Ar number
4425defaults to 1.
4426.Pp
4427.It Ic sleep Ar seconds
4428Suspends execution for a minimum of the
4429.Ar seconds
4430specified as positive decimal value with an optional fractional part.
4431Signal delivery may continue execution earlier.
4432.Pp
4433.It Ic source Ar file Op Ar arg ...
4434Like
4435.Ic \&. Po Do dot Dc Pc ,
4436except that the current working directory is appended to the
4437search path (GNU
4438.Nm bash
4439extension).
4440.Pp
4441.It Ic suspend
4442Stops the shell as if it had received the suspend character from
4443the terminal.
4444It is not possible to suspend a login shell unless the parent process
4445is a member of the same terminal session but is a member of a different
4446process group.
4447As a general rule, if the shell was started by another shell or via
4448.Xr su 1 ,
4449it can be suspended.
4450.Pp
4451.It Ic test Ar expression
4452.It Ic \&[ Ar expression Ic \&]
4453.Ic test
4454evaluates the
4455.Ar expression
4456and returns zero status if true, 1 if false, or greater than 1 if there
4457was an error.
4458It is normally used as the condition command of
4459.Ic if
4460and
4461.Ic while
4462statements.
4463Symbolic links are followed for all
4464.Ar file
4465expressions except
4466.Fl h
4467and
4468.Fl L .
4469.Pp
4470The following basic expressions are available:
4471.Bl -tag -width 17n
4472.It Fl a Ar file
4473.Ar file
4474exists.
4475.It Fl b Ar file
4476.Ar file
4477is a block special device.
4478.It Fl c Ar file
4479.Ar file
4480is a character special device.
4481.It Fl d Ar file
4482.Ar file
4483is a directory.
4484.It Fl e Ar file
4485.Ar file
4486exists.
4487.It Fl f Ar file
4488.Ar file
4489is a regular file.
4490.It Fl G Ar file
4491.Ar file Ns 's
4492group is the shell's effective group ID.
4493.It Fl g Ar file
4494.Ar file Ns 's
4495mode has the setgid bit set.
4496.It Fl H Ar file
4497.Ar file
4498is a context dependent directory (only useful on HP-UX).
4499.It Fl h Ar file
4500.Ar file
4501is a symbolic link.
4502.It Fl k Ar file
4503.Ar file Ns 's
4504mode has the
4505.Xr sticky 8
4506bit set.
4507.It Fl L Ar file
4508.Ar file
4509is a symbolic link.
4510.It Fl O Ar file
4511.Ar file Ns 's
4512owner is the shell's effective user ID.
4513.It Fl p Ar file
4514.Ar file
4515is a named pipe
4516.Pq Tn FIFO .
4517.It Fl r Ar file
4518.Ar file
4519exists and is readable.
4520.It Fl S Ar file
4521.Ar file
4522is a
4523.Xr unix 4 Ns -domain
4524socket.
4525.It Fl s Ar file
4526.Ar file
4527is not empty.
4528.It Fl t Ar fd
4529File descriptor
4530.Ar fd
4531is a
4532.Xr tty 4
4533device.
4534.It Fl u Ar file
4535.Ar file Ns 's
4536mode has the setuid bit set.
4537.It Fl w Ar file
4538.Ar file
4539exists and is writable.
4540.It Fl x Ar file
4541.Ar file
4542exists and is executable.
4543.It Ar file1 Fl nt Ar file2
4544.Ar file1
4545is newer than
4546.Ar file2
4547or
4548.Ar file1
4549exists and
4550.Ar file2
4551does not.
4552.It Ar file1 Fl ot Ar file2
4553.Ar file1
4554is older than
4555.Ar file2
4556or
4557.Ar file2
4558exists and
4559.Ar file1
4560does not.
4561.It Ar file1 Fl ef Ar file2
4562.Ar file1
4563is the same file as
4564.Ar file2 .
4565.It Ar string
4566.Ar string
4567has non-zero length.
4568.It Fl n Ar string
4569.Ar string
4570is not empty.
4571.It Fl z Ar string
4572.Ar string
4573is empty.
4574.It Fl v Ar name
4575The shell parameter
4576.Ar name
4577is set.
4578.It Fl o Ar option
4579Shell
4580.Ar option
4581is set (see the
4582.Ic set
4583command above for a list of options).
4584As a non-standard extension, if the option starts with a
4585.Ql \&! ,
4586the test is negated; the test always fails if
4587.Ar option
4588doesn't exist (so [ \-o foo \-o \-o !foo ] returns true if and only if option
4589.Ar foo
4590exists).
4591The same can be achieved with [ \-o ?foo ] like in
4592.At
4593.Nm ksh93 .
4594.Ar option
4595can also be the short flag led by either
4596.Ql \-
4597or
4598.Ql +
4599.Pq no logical negation ,
4600for example
4601.Dq Li \-x
4602or
4603.Dq Li +x
4604instead of
4605.Dq Li xtrace .
4606.It Ar string No = Ar string
4607Strings are equal.
4608.It Ar string No == Ar string
4609Strings are equal.
4610.It Ar string No \*(Gt Ar string
4611First string operand is greater than second string operand.
4612.It Ar string No \*(Lt Ar string
4613First string operand is less than second string operand.
4614.It Ar string No != Ar string
4615Strings are not equal.
4616.It Ar number Fl eq Ar number
4617Numbers compare equal.
4618.It Ar number Fl ne Ar number
4619Numbers compare not equal.
4620.It Ar number Fl ge Ar number
4621Numbers compare greater than or equal.
4622.It Ar number Fl gt Ar number
4623Numbers compare greater than.
4624.It Ar number Fl le Ar number
4625Numbers compare less than or equal.
4626.It Ar number Fl \&lt Ar number
4627Numbers compare less than.
4628.El
4629.Pp
4630The above basic expressions, in which unary operators have precedence over
4631binary operators, may be combined with the following operators (listed in
4632increasing order of precedence):
4633.Bd -literal -offset indent
4634expr \-o expr		Logical OR.
4635expr \-a expr		Logical AND.
4636! expr			Logical NOT.
4637( expr )		Grouping.
4638.Ed
4639.Pp
4640Note that a number actually may be an arithmetic expression, such as
4641a mathematical term or the name of an integer variable:
4642.Bd -literal -offset indent
4643x=1; [ "x" \-eq 1 ]	evaluates to true
4644.Ed
4645.Pp
4646Note that some special rules are applied (courtesy of
4647.Px
4648) if the number of arguments to
4649.Ic test
4650or inside the brackets
4651.Ic \&[ ... \&]
4652is less than five: if leading
4653.Dq Li \&!
4654arguments can be stripped such that only one to three arguments remain,
4655then the lowered comparison is executed; (thanks to XSI) parentheses
4656.Ic \e( ... \e)
4657lower four- and three-argument forms to two- and one-argument forms,
4658respectively; three-argument forms ultimately prefer binary operations,
4659followed by negation and parenthesis lowering; two- and four-argument forms
4660prefer negation followed by parenthesis; the one-argument form always implies
4661.Fl n .
4662.Pp
4663.Sy Note :
4664A common mistake is to use
4665.Dq Li if \&[ $foo = bar \&]
4666which fails if parameter
4667.Dq foo
4668is empty or unset, if it has embedded spaces (i.e.\&
4669.Ev IFS
4670octets) or if it is a unary operator like
4671.Dq Li \&!
4672or
4673.Dq Li \-n .
4674Use tests like
4675.Dq Li if \&[ x\&"$foo\&" = x"bar" \&]
4676instead, or the double-bracket operator
4677.Dq Li if \&[[ $foo = bar \&]]
4678or, to avoid pattern matching (see
4679.Ic \&[[
4680above):
4681.Dq Li if \&[[ $foo = \&"$bar" \&]]
4682.Pp
4683The
4684.Ic \&[[ ... \&]]
4685construct is not only more secure to use but also often faster.
4686.Pp
4687.It Xo
4688.Ic time
4689.Op Fl p
4690.Op Ar pipeline
4691.Xc
4692If a
4693.Ar pipeline
4694is given, the times used to execute the pipeline are reported.
4695If no pipeline
4696is given, then the user and system time used by the shell itself, and all the
4697commands it has run since it was started, are reported.
4698The times reported are the real time (elapsed time from start to finish),
4699the user CPU time (time spent running in user mode), and the system CPU time
4700(time spent running in kernel mode).
4701Times are reported to standard error; the format of the output is:
4702.Pp
4703.Dl "0m0.03s real     0m0.02s user     0m0.01s system"
4704.Pp
4705If the
4706.Fl p
4707option is given the output is slightly longer:
4708.Bd -literal -offset indent
4709real     0.03
4710user     0.02
4711sys      0.01
4712.Ed
4713.Pp
4714It is an error to specify the
4715.Fl p
4716option unless
4717.Ar pipeline
4718is a simple command.
4719.Pp
4720Simple redirections of standard error do not affect the output of the
4721.Ic time
4722command:
4723.Pp
4724.Dl $ time sleep 1 2\*(Gtafile
4725.Dl $ { time sleep 1; } 2\*(Gtafile
4726.Pp
4727Times for the first command do not go to
4728.Dq afile ,
4729but those of the second command do.
4730.Pp
4731.It Ic times
4732Print the accumulated user and system times used both by the shell
4733and by processes that the shell started which have exited.
4734The format of the output is:
4735.Bd -literal -offset indent
47360m0.01s 0m0.00s
47370m0.04s 0m0.02s
4738.Ed
4739.Pp
4740.It Ic trap Ar n Op Ar signal ...
4741If the first operand is a decimal unsigned integer, this resets all
4742specified signals to the default action, i.e. is the same as calling
4743.Ic trap
4744with a dash
4745.Pq Dq Li \-
4746as
4747.Ar handler ,
4748followed by the arguments
4749.Pq Ar n Op Ar signal ... ,
4750all of which are treated as signals.
4751.Pp
4752.It Ic trap Op Ar handler signal ...
4753Sets a trap handler that is to be executed when any of the specified
4754.Ar signal Ns s
4755are received.
4756.Ar handler
4757is either an empty string, indicating the signals are to be ignored, a dash
4758.Pq Dq Li \- ,
4759indicating that the default action is to be taken for the signals
4760.Pq see Xr signal 3 ,
4761or a string containing shell commands to be executed at the first opportunity
4762(i.e. when the current command completes or before printing the next
4763.Ev PS1
4764prompt) after receipt of one of the signals.
4765.Ar signal
4766is the name of a signal
4767.Pq e.g.\& Dv PIPE or Dv ALRM
4768or the number of the signal (see the
4769.Ic kill Fl l
4770command above).
4771.Pp
4772There are two special signals:
4773.Dv EXIT
4774.Pq also known as 0 ,
4775which is executed when the shell is about to exit, and
4776.Dv ERR ,
4777which is executed after an error occurs; an error is something
4778that would cause the shell to exit if the
4779.Ic set Fl e
4780or
4781.Ic set Fl o Ic errexit
4782option were set.
4783.Dv EXIT
4784handlers are executed in the environment of the last executed command.
4785.Pp
4786Note that, for non-interactive shells, the trap handler cannot be changed
4787for signals that were ignored when the shell started.
4788.Pp
4789With no arguments, the current state of the traps that have been set since
4790the shell started is shown as a series of
4791.Ic trap
4792commands.
4793Note that the output of
4794.Ic trap
4795cannot be usefully piped to another process (an artifact of the fact that
4796traps are cleared when subprocesses are created).
4797.Pp
4798The original Korn shell's
4799.Dv DEBUG
4800trap and the handling of
4801.Dv ERR
4802and
4803.Dv EXIT
4804traps in functions are not yet implemented.
4805.Pp
4806.It Ic true
4807A command that exits with a zero value.
4808.Pp
4809.It Xo
4810.Ic typeset
4811.Op Ic +\-aglpnrtUux
4812.Oo Fl L Ns Op Ar n
4813.No \*(Ba Fl R Ns Op Ar n
4814.No \*(Ba Fl Z Ns Op Ar n Oc
4815.Op Fl i Ns Op Ar n
4816.Oo Ar name
4817.Op Ns = Ns Ar value
4818.Ar ... Oc
4819.Xc
4820.It Xo
4821.Ic typeset
4822.Fl f Op Fl tux
4823.Op Ar name ...
4824.Xc
4825Display or set parameter attributes.
4826This is a declaration utility.
4827With no
4828.Ar name
4829arguments, parameter attributes are displayed; if no options are used, the
4830current attributes of all parameters are printed as
4831.Ic typeset
4832commands; if an option is given (or
4833.Dq Li \-
4834with no option letter), all parameters and their values with the specified
4835attributes are printed; if options are introduced with
4836.Ql + ,
4837parameter values are not printed.
4838.Pp
4839If
4840.Ar name
4841arguments are given, the attributes of the named parameters are set
4842.Pq Ic \&\-
4843or cleared
4844.Pq Ic \&+ ;
4845inside a function, this will cause the parameters to be created
4846(with no value) in the local scope (but see
4847.Fl g ) .
4848Values for parameters may optionally be specified.
4849For
4850.Ar name Ns \&[*] ,
4851the change affects all elements of the array, and no value may be specified.
4852.Pp
4853When
4854.Fl f
4855is used,
4856.Ic typeset
4857operates on the attributes of functions.
4858As with parameters, if no
4859.Ar name
4860arguments are given,
4861functions are listed with their values (i.e. definitions) unless
4862options are introduced with
4863.Ql + ,
4864in which case only the function names are reported.
4865.Bl -tag -width Ds
4866.It Fl a
4867Indexed array attribute.
4868.It Fl f
4869Function mode.
4870Display or set functions and their attributes, instead of parameters.
4871.It Fl g
4872Do not cause named parameters to be created in
4873the local scope when called inside a function.
4874.It Fl i Ns Op Ar n
4875Integer attribute.
4876.Ar n
4877specifies the base to use when displaying the integer (if not specified, the
4878base given in the first assignment is used).
4879Parameters with this attribute may
4880be assigned values containing arithmetic expressions.
4881.It Fl L Ns Op Ar n
4882Left justify attribute.
4883.Ar n
4884specifies the field width.
4885If
4886.Ar n
4887is not specified, the current width of a parameter (or the width of its first
4888assigned value) is used.
4889Leading whitespace (and zeros, if used with the
4890.Fl Z
4891option) is stripped.
4892If necessary, values are either truncated or space padded
4893to fit the field width.
4894.It Fl l
4895Lower case attribute.
4896All upper case ASCII characters in values are converted to lower case.
4897(In the original Korn shell, this parameter meant
4898.Dq long integer
4899when used with the
4900.Fl i
4901option.)
4902.It Fl n
4903Create a bound variable (name reference): any access to the variable
4904.Ar name
4905will access the variable
4906.Ar value
4907in the current scope (this is different from
4908.At
4909.Nm ksh93 ! )
4910instead.
4911Also different from
4912.At
4913.Nm ksh93
4914is that
4915.Ar value
4916is lazily evaluated at the time
4917.Ar name
4918is accessed.
4919This can be used by functions to access variables whose names are
4920passed as parameters, instead of using
4921.Ic eval .
4922.It Fl p
4923Print complete
4924.Ic typeset
4925commands that can be used to re-create the attributes and values of
4926parameters.
4927.It Fl R Ns Op Ar n
4928Right justify attribute.
4929.Ar n
4930specifies the field width.
4931If
4932.Ar n
4933is not specified, the current width of a parameter (or the width of its first
4934assigned value) is used.
4935Trailing whitespace is stripped.
4936If necessary, values are either stripped of leading characters or space
4937padded to make them fit the field width.
4938.It Fl r
4939Read-only attribute.
4940Parameters with this attribute may not be assigned to or unset.
4941Once this attribute is set, it cannot be turned off.
4942.It Fl t
4943Tag attribute.
4944Has no meaning to the shell; provided for application use.
4945.Pp
4946For functions,
4947.Fl t
4948is the trace attribute.
4949When functions with the trace attribute are executed, the
4950.Ic xtrace
4951.Pq Fl x
4952shell option is temporarily turned on.
4953.It Fl U
4954Unsigned integer attribute.
4955Integers are printed as unsigned values (combine with the
4956.Fl i
4957option).
4958This option is not in the original Korn shell.
4959.It Fl u
4960Upper case attribute.
4961All lower case ASCII characters in values are converted to upper case.
4962(In the original Korn shell, this parameter meant
4963.Dq unsigned integer
4964when used with the
4965.Fl i
4966option which meant upper case letters would never be used for bases greater
4967than 10.
4968See
4969.Fl U
4970above.)
4971.Pp
4972For functions,
4973.Fl u
4974is the undefined attribute.
4975See
4976.Sx Functions
4977above for the implications of this.
4978.It Fl x
4979Export attribute.
4980Parameters are placed in the environment of any executed commands.
4981Functions cannot be exported for security reasons
4982.Pq Dq shellshock .
4983.It Fl Z Ns Op Ar n
4984Zero fill attribute.
4985If not combined with
4986.Fl L ,
4987this is the same as
4988.Fl R ,
4989except zero padding is used instead of space padding.
4990For integers, the number is padded, not the base.
4991.El
4992.Pp
4993If any of the
4994.\" long integer ,
4995.Fl i ,
4996.Fl L ,
4997.Fl l ,
4998.Fl R ,
4999.Fl U ,
5000.Fl u
5001or
5002.Fl Z
5003options are changed, all others from this set are cleared,
5004unless they are also given on the same command line.
5005.Pp
5006.It Xo
5007.Ic ulimit
5008.Op Fl aBCcdefHilMmnOPpqrSsTtVvw
5009.Op Ar value
5010.Xc
5011Display or set process limits.
5012If no options are used, the file size limit
5013.Pq Fl f
5014is assumed.
5015.Ar value ,
5016if specified, may be either an arithmetic expression or the word
5017.Dq unlimited .
5018The limits affect the shell and any processes created by the shell after a
5019limit is imposed.
5020Note that some systems may not allow limits to be increased
5021once they are set.
5022Also note that the types of limits available are system
5023dependent \*(en some systems have only the
5024.Fl f
5025limit, or not even that, or can set only the soft limits
5026.Bl -tag -width 5n
5027.It Fl a
5028Display all limits; unless
5029.Fl H
5030is used, soft limits are displayed.
5031.It Fl B Ar n
5032Set the socket buffer size to
5033.Ar n
5034kibibytes.
5035.It Fl C Ar n
5036Set the number of cached threads to
5037.Ar n .
5038.It Fl c Ar n
5039Impose a size limit of
5040.Ar n
5041blocks on the size of core dumps.
5042.It Fl d Ar n
5043Impose a size limit of
5044.Ar n
5045kibibytes on the size of the data area.
5046.It Fl e Ar n
5047Set the maximum niceness to
5048.Ar n .
5049.It Fl f Ar n
5050Impose a size limit of
5051.Ar n
5052blocks on files written by the shell and its child processes (files of any
5053size may be read).
5054.It Fl H
5055Set the hard limit only (the default is to set both hard and soft limits).
5056.It Fl i Ar n
5057Set the number of pending signals to
5058.Ar n .
5059.It Fl l Ar n
5060Impose a limit of
5061.Ar n
5062kibibytes on the amount of locked (wired) physical memory.
5063.It Fl M Ar n
5064Set the AIO locked memory to
5065.Ar n
5066kibibytes.
5067.It Fl m Ar n
5068Impose a limit of
5069.Ar n
5070kibibytes on the amount of physical memory used.
5071.It Fl n Ar n
5072Impose a limit of
5073.Ar n
5074file descriptors that can be open at once.
5075.It Fl O Ar n
5076Set the number of AIO operations to
5077.Ar n .
5078.It Fl P Ar n
5079Limit the number of threads per process to
5080.Ar n .
5081.It Fl p Ar n
5082Impose a limit of
5083.Ar n
5084processes that can be run by the user at any one time.
5085.It Fl q Ar n
5086Limit the size of
5087.Tn POSIX
5088message queues to
5089.Ar n
5090bytes.
5091.It Fl r Ar n
5092Set the maximum real-time priority to
5093.Ar n .
5094.It Fl S
5095Set the soft limit only (the default is to set both hard and soft limits).
5096.It Fl s Ar n
5097Impose a size limit of
5098.Ar n
5099kibibytes on the size of the stack area.
5100.It Fl T Ar n
5101Impose a time limit of
5102.Ar n
5103real seconds to be used by each process.
5104.It Fl t Ar n
5105Impose a time limit of
5106.Ar n
5107CPU seconds spent in user mode to be used by each process.
5108.It Fl V Ar n
5109Set the number of vnode monitors on Haiku to
5110.Ar n .
5111.It Fl v Ar n
5112Impose a limit of
5113.Ar n
5114kibibytes on the amount of virtual memory (address space) used.
5115.It Fl w Ar n
5116Impose a limit of
5117.Ar n
5118kibibytes on the amount of swap space used.
5119.El
5120.Pp
5121As far as
5122.Ic ulimit
5123is concerned, a block is 512 bytes.
5124.Pp
5125.It Xo
5126.Ic umask
5127.Op Fl S
5128.Op Ar mask
5129.Xc
5130Display or set the file permission creation mask or umask (see
5131.Xr umask 2 ) .
5132If the
5133.Fl S
5134option is used, the mask displayed or set is symbolic; otherwise, it is an
5135octal number.
5136.Pp
5137Symbolic masks are like those used by
5138.Xr chmod 1 .
5139When used, they describe what permissions may be made available (as opposed to
5140octal masks in which a set bit means the corresponding bit is to be cleared).
5141For example,
5142.Dq Li ug=rwx,o=
5143sets the mask so files will not be readable, writable or executable by
5144.Dq others ,
5145and is equivalent (on most systems) to the octal mask
5146.Dq Li 007 .
5147.Pp
5148.It Xo
5149.Ic unalias
5150.Op Fl adt
5151.Op Ar name ...
5152.Xc
5153The aliases for the given names are removed.
5154If the
5155.Fl a
5156option is used, all aliases are removed.
5157If the
5158.Fl t
5159or
5160.Fl d
5161options are used, the indicated operations are carried out on tracked or
5162directory aliases, respectively.
5163.Pp
5164.It Xo
5165.Ic unset
5166.Op Fl fv
5167.Ar parameter ...
5168.Xc
5169Unset the named parameters
5170.Po
5171.Fl v ,
5172the default
5173.Pc
5174or functions
5175.Pq Fl f .
5176With
5177.Ar parameter Ns \&[*] ,
5178attributes are kept, only values are unset.
5179.Pp
5180The exit status is non-zero if any of the parameters have the read-only
5181attribute set, zero otherwise.
5182.Pp
5183.It Ic wait Op Ar job ...
5184Wait for the specified job(s) to finish.
5185The exit status of
5186.Ic wait
5187is that of the last specified job; if the last job is killed by a signal, the
5188exit status is 128 + the number of the signal (see
5189.Ic kill Fl l Ar exit-status
5190above); if the last specified job can't be found (because it never existed or
5191had already finished), the exit status of
5192.Ic wait
5193is 127.
5194See
5195.Sx Job control
5196below for the format of
5197.Ar job .
5198.Ic wait
5199will return if a signal for which a trap has been set is received or if a
5200.Dv SIGHUP ,
5201.Dv SIGINT
5202or
5203.Dv SIGQUIT
5204signal is received.
5205.Pp
5206If no jobs are specified,
5207.Ic wait
5208waits for all currently running jobs (if any) to finish and exits with a zero
5209status.
5210If job monitoring is enabled, the completion status of jobs is printed
5211(this is not the case when jobs are explicitly specified).
5212.Pp
5213.It Xo
5214.Ic whence
5215.Op Fl pv
5216.Op Ar name ...
5217.Xc
5218Without the
5219.Fl v
5220option, it is the same as
5221.Ic command Fl v ,
5222except aliases are not printed as alias command.
5223With the
5224.Fl v
5225option, it is exactly the same as
5226.Ic command Fl V .
5227In either case, the
5228.Fl p
5229option differs: the search path is not affected in
5230.Ic whence ,
5231but the search is restricted to the path.
5232.El
5233.Ss Job control
5234Job control refers to the shell's ability to monitor and control jobs which
5235are processes or groups of processes created for commands or pipelines.
5236At a minimum, the shell keeps track of the status of the background (i.e.\&
5237asynchronous) jobs that currently exist; this information can be displayed
5238using the
5239.Ic jobs
5240commands.
5241If job control is fully enabled (using
5242.Ic set Fl m
5243or
5244.Ic set Fl o Ic monitor ) ,
5245as it is for interactive shells, the processes of a job are placed in their
5246own process group.
5247Foreground jobs can be stopped by typing the suspend character from
5248the terminal (normally \*(haZ); jobs can be restarted in either the
5249foreground or background using the commands
5250.Ic fg
5251and
5252.Ic bg .
5253.Pp
5254Note that only commands that create processes (e.g. asynchronous commands,
5255subshell commands and non-built-in, non-function commands) can be stopped;
5256commands like
5257.Ic read
5258cannot be.
5259.Pp
5260When a job is created, it is assigned a job number.
5261For interactive shells, this number is printed inside
5262.Dq Li \&[...] ,
5263followed by the process IDs of the processes in the job when an asynchronous
5264command is run.
5265A job may be referred to in the
5266.Ic bg ,
5267.Ic fg ,
5268.Ic jobs ,
5269.Ic kill
5270and
5271.Ic wait
5272commands either by the process ID of the last process in the command pipeline
5273(as stored in the
5274.Ic \&$!
5275parameter) or by prefixing the job number with a percent sign
5276.Pq Ql % .
5277Other percent sequences can also be used to refer to jobs:
5278.Bl -tag -width "%+ x %% x %XX"
5279.It %+ \*(Ba %% \*(Ba %
5280The most recently stopped job or, if there are no stopped jobs, the oldest
5281running job.
5282.It %\-
5283The job that would be the
5284.Ic %+
5285job if the latter did not exist.
5286.It % Ns Ar n
5287The job with job number
5288.Ar n .
5289.It %? Ns Ar string
5290The job with its command containing the string
5291.Ar string
5292(an error occurs if multiple jobs are matched).
5293.It % Ns Ar string
5294The job with its command starting with the string
5295.Ar string
5296(an error occurs if multiple jobs are matched).
5297.El
5298.Pp
5299When a job changes state (e.g. a background job finishes or foreground job is
5300stopped), the shell prints the following status information:
5301.Pp
5302.D1 [ Ns Ar number ] Ar flag status command
5303.Pp
5304where...
5305.Bl -tag -width "command"
5306.It Ar number
5307is the job number of the job;
5308.It Ar flag
5309is the
5310.Ql +
5311or
5312.Ql \-
5313character if the job is the
5314.Ic %+
5315or
5316.Ic %\-
5317job, respectively, or space if it is neither;
5318.It Ar status
5319indicates the current state of the job and can be:
5320.Bl -tag -width "RunningXX"
5321.It Done Op Ar number
5322The job exited.
5323.Ar number
5324is the exit status of the job which is omitted if the status is zero.
5325.It Running
5326The job has neither stopped nor exited (note that running does not necessarily
5327mean consuming CPU time \*(en
5328the process could be blocked waiting for some event).
5329.It Stopped Op Ar signal
5330The job was stopped by the indicated
5331.Ar signal
5332(if no signal is given, the job was stopped by
5333.Dv SIGTSTP ) .
5334.It Ar signal-description Op Dq core dumped
5335The job was killed by a signal (e.g. memory fault, hangup); use
5336.Ic kill Fl l
5337for a list of signal descriptions.
5338The
5339.Dq Li core dumped
5340message indicates the process created a core file.
5341.El
5342.It Ar command
5343is the command that created the process.
5344If there are multiple processes in
5345the job, each process will have a line showing its
5346.Ar command
5347and possibly its
5348.Ar status ,
5349if it is different from the status of the previous process.
5350.El
5351.Pp
5352When an attempt is made to exit the shell while there are jobs in the stopped
5353state, the shell warns the user that there are stopped jobs and does not exit.
5354If another attempt is immediately made to exit the shell, the stopped jobs are
5355sent a
5356.Dv SIGHUP
5357signal and the shell exits.
5358Similarly, if the
5359.Ic nohup
5360option is not set and there are running jobs when an attempt is made to exit
5361a login shell, the shell warns the user and does not exit.
5362If another attempt
5363is immediately made to exit the shell, the running jobs are sent a
5364.Dv SIGHUP
5365signal and the shell exits.
5366.Ss Terminal state
5367The state of the controlling terminal can be modified by a command
5368executed in the foreground, whether or not job control is enabled, but
5369the modified terminal state is only kept past the job's lifetime and used
5370for later command invocations if the command exits successfully (i.e.\&
5371with an exit status of 0).
5372When such a job is momentarily stopped or restarted, the terminal state
5373is saved and restored, respectively, but it will not be kept afterwards.
5374In interactive mode, when line editing is enabled, the terminal state is
5375saved before being reconfigured by the shell for the line editor, then
5376restored before running a command.
5377.Ss POSIX mode
5378Entering
5379.Ic set Fl o Ic posix
5380mode will cause
5381.Nm
5382to behave even more
5383.Tn POSIX
5384compliant in places where the defaults or opinions differ.
5385Note that
5386.Nm mksh
5387will still operate with unsigned 32-bit arithmetic; use
5388.Nm lksh
5389if arithmetic on the host
5390.Vt long
5391data type, complete with ISO C Undefined Behaviour, is required;
5392refer to the
5393.Xr lksh 1
5394manual page for details.
5395Most other historic,
5396.At
5397.Nm ksh Ns -compatible
5398or opinionated differences can be disabled by using this mode; these are:
5399.Bl -bullet
5400.It
5401The incompatible GNU
5402.Nm bash
5403I/O redirection
5404.Ic &\*(Gt Ns Ar file
5405is not supported.
5406.It
5407File descriptors created by I/O redirections are inherited by
5408child processes.
5409.It
5410Numbers with a leading digit zero are interpreted as octal.
5411.It
5412The
5413.Nm echo
5414builtin does not interpret backslashes and only supports the exact option
5415.Fl n .
5416.It
5417Alias expansion with a trailing space only reruns on command words.
5418.It
5419Tilde expansion follows POSIX instead of Korn shell rules.
5420.It
5421The exit status of
5422.Ic fg
5423is always 0.
5424.It
5425.Ic kill
5426.Fl l
5427only lists signal names, all in one line.
5428.It
5429.Ic getopts
5430does not accept options with a leading
5431.Ql + .
5432.It
5433.Ic exec
5434skips builtins, functions and other commands and uses a
5435.Ev PATH
5436search to determine the utility to execute.
5437.El
5438.Ss SH mode
5439Compatibility mode; intended for use with legacy scripts that
5440cannot easily be fixed; the changes are as follows:
5441.Bl -bullet
5442.It
5443The incompatible GNU
5444.Nm bash
5445I/O redirection
5446.Ic &\*(Gt Ns Ar file
5447is not supported.
5448.It
5449File descriptors created by I/O redirections are inherited by
5450child processes.
5451.It
5452The
5453.Nm echo
5454builtin does not interpret backslashes and only supports the exact option
5455.Fl n ,
5456unless built with
5457.Ev \-DMKSH_MIDNIGHTBSD01ASH_COMPAT .
5458.It
5459The substitution operations
5460.Sm off
5461.Xo
5462.Pf ${ Ar x
5463.Pf # Ar pat No } ,
5464.Sm on
5465.Xc
5466.Sm off
5467.Xo
5468.Pf ${ Ar x
5469.Pf ## Ar pat No } ,
5470.Sm on
5471.Xc
5472.Sm off
5473.Xo
5474.Pf ${ Ar x
5475.Pf % Ar pat No } ,
5476.Sm on
5477.Xc
5478and
5479.Sm off
5480.Xo
5481.Pf ${ Ar x
5482.Pf %% Ar pat No }
5483.Sm on
5484.Xc
5485wrongly do not require a parenthesis to be escaped and do not parse extglobs.
5486.It
5487The getopt construct from
5488.Xr lksh 1
5489passes through the errorlevel.
5490.It
5491.Nm sh
5492.Fl c
5493eats a leading
5494.Fl \-
5495if built with
5496.Ev \-DMKSH_MIDNIGHTBSD01ASH_COMPAT .
5497.El
5498.Ss Interactive input line editing
5499The shell supports three modes of reading command lines from a
5500.Xr tty 4
5501in an interactive session, controlled by the
5502.Ic emacs ,
5503.Ic gmacs
5504and
5505.Ic vi
5506options (at most one of these can be set at once).
5507The default is
5508.Ic emacs .
5509Editing modes can be set explicitly using the
5510.Ic set
5511built-in.
5512If none of these options are enabled,
5513the shell simply reads lines using the normal
5514.Xr tty 4
5515driver.
5516If the
5517.Ic emacs
5518or
5519.Ic gmacs
5520option is set, the shell allows emacs-like editing of the command; similarly,
5521if the
5522.Ic vi
5523option is set, the shell allows vi-like editing of the command.
5524These modes are described in detail in the following sections.
5525.Pp
5526In these editing modes, if a line is longer than the screen width (see the
5527.Ev COLUMNS
5528parameter),
5529a
5530.Ql \*(Gt ,
5531.Ql +
5532or
5533.Ql \*(Lt
5534character is displayed in the last column indicating that there are more
5535characters after, before and after, or before the current position,
5536respectively.
5537The line is scrolled horizontally as necessary.
5538.Pp
5539Completed lines are pushed into the history, unless they begin with an
5540IFS octet or IFS white space or are the same as the previous line.
5541.Ss Emacs editing mode
5542When the
5543.Ic emacs
5544option is set, interactive input line editing is enabled.
5545Warning: This mode is
5546slightly different from the emacs mode in the original Korn shell.
5547In this mode, various editing commands
5548(typically bound to one or more control characters) cause immediate actions
5549without waiting for a newline.
5550Several editing commands are bound to particular
5551control characters when the shell is invoked; these bindings can be changed
5552using the
5553.Ic bind
5554command.
5555.Pp
5556The following is a list of available editing commands.
5557Each description starts with the name of the command,
5558suffixed with a colon;
5559an
5560.Op Ar n
5561(if the command can be prefixed with a count); and any keys the command is
5562bound to by default, written using caret notation
5563e.g. the ASCII ESC character is written as \*(ha[.
5564These control sequences are not case sensitive.
5565A count prefix for a command is entered using the sequence
5566.Pf \*(ha[ Ns Ar n ,
5567where
5568.Ar n
5569is a sequence of 1 or more digits.
5570Unless otherwise specified, if a count is
5571omitted, it defaults to 1.
5572.Pp
5573Note that editing command names are used only with the
5574.Ic bind
5575command.
5576Furthermore, many editing commands are useful only on terminals with
5577a visible cursor.
5578The user's
5579.Xr tty 4
5580characters (e.g.\&
5581.Dv ERASE )
5582are bound to
5583reasonable substitutes and override the default bindings;
5584their customary values are shown in parentheses below.
5585The default bindings were chosen to resemble corresponding
5586Emacs key bindings:
5587.Bl -tag -width Ds
5588.It Xo abort:
5589.No INTR Pq \*(haC ,
5590.No \*(haG
5591.Xc
5592Abort the current command, save it to the history, empty the line buffer and
5593set the exit state to interrupted.
5594.It auto\-insert: Op Ar n
5595Simply causes the character to appear as literal input.
5596Most ordinary characters are bound to this.
5597.It Xo backward\-char:
5598.Op Ar n
5599.No \*(haB , \*(haXD , ANSI-CurLeft , PC-CurLeft
5600.Xc
5601Moves the cursor backward
5602.Ar n
5603characters.
5604.It Xo backward\-word:
5605.Op Ar n
5606.No \*(ha[b , ANSI-Ctrl-CurLeft , ANSI-Alt-CurLeft
5607.Xc
5608Moves the cursor backward to the beginning of the word; words consist of
5609alphanumerics, underscore
5610.Pq Ql _
5611and dollar sign
5612.Pq Ql $
5613characters.
5614.It beginning\-of\-history: \*(ha[\*(Lt
5615Moves to the beginning of the history.
5616.It beginning\-of\-line: \*(haA, ANSI-Home, PC-Home
5617Moves the cursor to the beginning of the edited input line.
5618.It Xo capitalise\-word:
5619.Op Ar n
5620.No \*(ha[C , \*(ha[c
5621.Xc
5622Uppercase the first ASCII character in the next
5623.Ar n
5624words, leaving the cursor past the end of the last word.
5625.It clear\-screen: \*(ha[\*(haL
5626Prints a compile-time configurable sequence to clear the screen and home
5627the cursor, redraws the last line of the prompt string and the currently
5628edited input line.
5629The default sequence works for almost all standard terminals.
5630.It comment: \*(ha[#
5631If the current line does not begin with a comment character, one is added at
5632the beginning of the line and the line is entered (as if return had been
5633pressed); otherwise, the existing comment characters are removed and the cursor
5634is placed at the beginning of the line.
5635.It complete: \*(ha[\*(ha[
5636Automatically completes as much as is unique of the command name or the file
5637name containing the cursor.
5638If the entire remaining command or file name is
5639unique, a space is printed after its completion, unless it is a directory name
5640in which case
5641.Ql /
5642is appended.
5643If there is no command or file name with the current partial word
5644as its prefix, a bell character is output (usually causing a beep to be
5645sounded).
5646.It complete\-command: \*(haX\*(ha[
5647Automatically completes as much as is unique of the command name having the
5648partial word up to the cursor as its prefix, as in the
5649.Ic complete
5650command above.
5651.It complete\-file: \*(ha[\*(haX
5652Automatically completes as much as is unique of the file name having the
5653partial word up to the cursor as its prefix, as in the
5654.Ic complete
5655command described above.
5656.It complete\-list: \*(haI, \*(ha[=
5657Complete as much as is possible of the current word
5658and list the possible completions for it.
5659If only one completion is possible,
5660match as in the
5661.Ic complete
5662command above.
5663Note that \*(haI is usually generated by the TAB (tabulator) key.
5664.It Xo delete\-char\-backward:
5665.Op Ar n
5666.No ERASE Pq \*(haH ,
5667.No \*(ha? , \*(haH
5668.Xc
5669Deletes
5670.Ar n
5671characters before the cursor.
5672.It Xo delete\-char\-forward:
5673.Op Ar n
5674.No ANSI-Del , PC-Del
5675.Xc
5676Deletes
5677.Ar n
5678characters after the cursor.
5679.It Xo delete\-word\-backward:
5680.Op Ar n
5681.No Pfx1+ERASE Pq \*(ha[\*(haH ,
5682.No WERASE Pq \*(haW ,
5683.No \*(ha[\*(ha? , \*(ha[\*(haH , \*(ha[h
5684.Xc
5685Deletes
5686.Ar n
5687words before the cursor.
5688.It Xo delete\-word\-forward:
5689.Op Ar n
5690.No \*(ha[d
5691.Xc
5692Deletes characters after the cursor up to the end of
5693.Ar n
5694words.
5695.It Xo down\-history:
5696.Op Ar n
5697.No \*(haN , \*(haXB , ANSI-CurDown , PC-CurDown
5698.Xc
5699Scrolls the history buffer forward
5700.Ar n
5701lines (later).
5702Each input line originally starts just after the last entry
5703in the history buffer, so
5704.Ic down\-history
5705is not useful until either
5706.Ic search\-history ,
5707.Ic search\-history\-up
5708or
5709.Ic up\-history
5710has been performed.
5711.It Xo downcase\-word:
5712.Op Ar n
5713.No \*(ha[L , \*(ha[l
5714.Xc
5715Lowercases the next
5716.Ar n
5717words.
5718.It Xo edit\-line:
5719.Op Ar n
5720.No \*(haXe
5721.Xc
5722Edit line
5723.Ar n
5724or the current line, if not specified, interactively.
5725The actual command executed is
5726.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
5727.It end\-of\-history: \*(ha[\*(Gt
5728Moves to the end of the history.
5729.It end\-of\-line: \*(haE, ANSI-End, PC-End
5730Moves the cursor to the end of the input line.
5731.It eot: \*(ha_
5732Acts as an end-of-file; this is useful because edit-mode input disables
5733normal terminal input canonicalisation.
5734.It Xo eot\-or\-delete:
5735.Op Ar n
5736.No EOF Pq \*(haD
5737.Xc
5738If alone on a line, same as
5739.Ic eot ,
5740otherwise,
5741.Ic delete\-char\-forward .
5742.It error: (not bound)
5743Error (ring the bell).
5744.It evaluate\-region: \*(ha[\*(haE
5745Evaluates the text between the mark and the cursor position
5746.Pq the entire line if no mark is set
5747as function substitution (if it cannot be parsed, the editing state is
5748unchanged and the bell is rung to signal an error); $? is updated accordingly.
5749.It exchange\-point\-and\-mark: \*(haX\*(haX
5750Places the cursor where the mark is and sets the mark to where the cursor was.
5751.It expand\-file: \*(ha[*
5752Appends a
5753.Ql *
5754to the current word and replaces the word with the result of performing file
5755globbing on the word.
5756If no files match the pattern, the bell is rung.
5757.It Xo forward\-char:
5758.Op Ar n
5759.No \*(haF , \*(haXC , ANSI-CurRight , PC-CurRight
5760.Xc
5761Moves the cursor forward
5762.Ar n
5763characters.
5764.It Xo forward\-word:
5765.Op Ar n
5766.No \*(ha[f , ANSI-Ctrl-CurRight , ANSI-Alt-CurRight
5767.Xc
5768Moves the cursor forward to the end of the
5769.Ar n Ns th
5770word.
5771.It Xo goto\-history:
5772.Op Ar n
5773.No \*(ha[g
5774.Xc
5775Goes to history number
5776.Ar n .
5777.It Xo kill\-line:
5778.No KILL Pq \*(haU
5779.Xc
5780Deletes the entire input line.
5781.It kill\-region: \*(haW
5782Deletes the input between the cursor and the mark.
5783.It Xo kill\-to\-eol:
5784.Op Ar n
5785.No \*(haK
5786.Xc
5787Deletes the input from the cursor to the end of the line if
5788.Ar n
5789is not specified; otherwise deletes characters between the cursor and column
5790.Ar n .
5791.It list: \*(ha[?
5792Prints a sorted, columnated list of command names or file names (if any) that
5793can complete the partial word containing the cursor.
5794Directory names have
5795.Ql /
5796appended to them.
5797.It list\-command: \*(haX?
5798Prints a sorted, columnated list of command names (if any) that can complete
5799the partial word containing the cursor.
5800.It list\-file: \*(haX\*(haY
5801Prints a sorted, columnated list of file names (if any) that can complete the
5802partial word containing the cursor.
5803File type indicators are appended as described under
5804.Ic list
5805above.
5806.It newline: \*(haJ , \*(haM
5807Causes the current input line to be processed by the shell.
5808The current cursor position may be anywhere on the line.
5809.It newline\-and\-next: \*(haO
5810Causes the current input line to be processed by the shell, and the next line
5811from history becomes the current line.
5812This is only useful after an
5813.Ic up\-history ,
5814.Ic search\-history
5815or
5816.Ic search\-history\-up .
5817.It Xo no\-op:
5818.No QUIT Pq \*(ha\e
5819.Xc
5820This does nothing.
5821.It prefix\-1: \*(ha[
5822Introduces a 2-character command sequence.
5823.It prefix\-2: \*(haX , \*(ha[[ , \*(ha[O
5824Introduces a multi-character command sequence.
5825.It Xo prev\-hist\-word:
5826.Op Ar n
5827.No \*(ha[. , \*(ha[_
5828.Xc
5829The last word or, if given, the
5830.Ar n Ns th
5831word (zero-based) of the previous (on repeated execution, second-last,
5832third-last, etc.) command is inserted at the cursor.
5833Use of this editing command trashes the mark.
5834.It quote: \*(ha\*(ha , \*(haV
5835The following character is taken literally rather than as an editing command.
5836.It redraw: \*(haL
5837Reprints the last line of the prompt string and the current input line
5838on a new line.
5839.It Xo search\-character\-backward:
5840.Op Ar n
5841.No \*(ha[\*(ha]
5842.Xc
5843Search backward in the current line for the
5844.Ar n Ns th
5845occurrence of the next character typed.
5846.It Xo search\-character\-forward:
5847.Op Ar n
5848.No \*(ha]
5849.Xc
5850Search forward in the current line for the
5851.Ar n Ns th
5852occurrence of the next character typed.
5853.It search\-history: \*(haR
5854Enter incremental search mode.
5855The internal history list is searched
5856backwards for commands matching the input.
5857An initial
5858.Ql \*(ha
5859in the search string anchors the search.
5860The escape key will leave search mode.
5861Other commands, including sequences of escape as
5862.Ic prefix\-1
5863followed by a
5864.Ic prefix\-1
5865or
5866.Ic prefix\-2
5867key will be executed after leaving search mode.
5868The
5869.Ic abort Pq \*(haG
5870command will restore the input line before search started.
5871Successive
5872.Ic search\-history
5873commands continue searching backward to the next previous occurrence of the
5874pattern.
5875The history buffer retains only a finite number of lines; the oldest
5876are discarded as necessary.
5877.It search\-history\-up: ANSI-PgUp, PC-PgUp
5878Search backwards through the history buffer for commands whose beginning match
5879the portion of the input line before the cursor.
5880When used on an empty line, this has the same effect as
5881.Ic up\-history .
5882.It search\-history\-down: ANSI-PgDn, PC-PgDn
5883Search forwards through the history buffer for commands whose beginning match
5884the portion of the input line before the cursor.
5885When used on an empty line, this has the same effect as
5886.Ic down\-history .
5887This is only useful after an
5888.Ic up\-history ,
5889.Ic search\-history
5890or
5891.Ic search\-history\-up .
5892.It set\-mark\-command: \*(ha[ Ns Aq space
5893Set the mark at the cursor position.
5894.It transpose\-chars: \*(haT
5895If at the end of line or, if the
5896.Ic gmacs
5897option is set, this exchanges the two previous characters; otherwise, it
5898exchanges the previous and current characters and moves the cursor one
5899character to the right.
5900.It Xo up\-history:
5901.Op Ar n
5902.No \*(haP , \*(haXA , ANSI-CurUp , PC-CurUp
5903.Xc
5904Scrolls the history buffer backward
5905.Ar n
5906lines (earlier).
5907.It Xo upcase\-word:
5908.Op Ar n
5909.No \*(ha[U , \*(ha[u
5910.Xc
5911Uppercase the next
5912.Ar n
5913words.
5914.It version: \*(ha[\*(haV
5915Display the version of
5916.Nm mksh .
5917The current edit buffer is restored as soon as a key is pressed.
5918The restoring keypress is processed, unless it is a space.
5919.It yank: \*(haY
5920Inserts the most recently killed text string at the current cursor position.
5921.It yank\-pop: \*(ha[y
5922Immediately after a
5923.Ic yank ,
5924replaces the inserted text string with the next previously killed text string.
5925.El
5926.Pp
5927The tab completion escapes characters the same way as the following code:
5928.Bd -literal
5929print \-nr \-\- "${x@/[\e"\-\e$\e&\-*:\-?[\e\e\e\`{\-\e}${IFS\-$\*(aq \et\en\*(aq}]/\e\e$KSH_MATCH}"
5930.Ed
5931.Ss Vi editing mode
5932.Em Note:
5933The vi command-line editing mode is orphaned, yet still functional.
5934It is 8-bit clean but specifically does not support UTF-8 or MBCS.
5935.Pp
5936The vi command-line editor in
5937.Nm
5938has basically the same commands as the
5939.Xr vi 1
5940editor with the following exceptions:
5941.Bl -bullet
5942.It
5943You start out in insert mode.
5944.It
5945There are file name and command completion commands:
5946=, \e, *, \*(haX, \*(haE, \*(haF and, optionally,
5947.Aq tab
5948and
5949.Aq esc .
5950.It
5951The
5952.Ic _
5953command is different (in
5954.Nm mksh ,
5955it is the last argument command; in
5956.Xr vi 1
5957it goes to the start of the current line).
5958.It
5959The
5960.Ic /
5961and
5962.Ic G
5963commands move in the opposite direction to the
5964.Ic j
5965command.
5966.It
5967Commands which don't make sense in a single line editor are not available
5968(e.g. screen movement commands and
5969.Xr ex 1 Ns -style
5970colon
5971.Pq Ic \&:
5972commands).
5973.El
5974.Pp
5975Like
5976.Xr vi 1 ,
5977there are two modes:
5978.Dq insert
5979mode and
5980.Dq command
5981mode.
5982In insert mode, most characters are simply put in the buffer at the
5983current cursor position as they are typed; however, some characters are
5984treated specially.
5985In particular, the following characters are taken from current
5986.Xr tty 4
5987settings
5988(see
5989.Xr stty 1 )
5990and have their usual meaning (normal values are in parentheses): kill (\*(haU),
5991erase (\*(ha?), werase (\*(haW), eof (\*(haD), intr (\*(haC) and quit (\*(ha\e).
5992In addition to
5993the above, the following characters are also treated specially in insert mode:
5994.Bl -tag -width XJXXXXM
5995.It \*(haE
5996Command and file name enumeration (see below).
5997.It \*(haF
5998Command and file name completion (see below).
5999If used twice in a row, the
6000list of possible completions is displayed; if used a third time, the completion
6001is undone.
6002.It \*(haH
6003Erases previous character.
6004.It \*(haJ \*(Ba \*(haM
6005End of line.
6006The current line is read, parsed and executed by the shell.
6007.It \*(haV
6008Literal next.
6009The next character typed is not treated specially (can be used
6010to insert the characters being described here).
6011.It \*(haX
6012Command and file name expansion (see below).
6013.It Aq esc
6014Puts the editor in command mode (see below).
6015.It Aq tab
6016Optional file name and command completion (see
6017.Ic \*(haF
6018above), enabled with
6019.Ic set Fl o Ic vi\-tabcomplete .
6020.El
6021.Pp
6022In command mode, each character is interpreted as a command.
6023Characters that
6024don't correspond to commands, are illegal combinations of commands, or are
6025commands that can't be carried out, all cause beeps.
6026In the following command descriptions, an
6027.Op Ar n
6028indicates the command may be prefixed by a number (e.g.\&
6029.Ic 10l
6030moves right 10 characters); if no number prefix is used,
6031.Ar n
6032is assumed to be 1 unless otherwise specified.
6033The term
6034.Dq current position
6035refers to the position between the cursor and the character preceding the
6036cursor.
6037A
6038.Dq word
6039is a sequence of letters, digits and underscore characters or a sequence of
6040non-letter, non-digit, non-underscore and non-whitespace characters (e.g.\&
6041.Dq Li ab2*&\*(ha
6042contains two words) and a
6043.Dq big-word
6044is a sequence of non-whitespace characters.
6045.Pp
6046Special
6047.Nm
6048vi commands:
6049.Pp
6050The following commands are not in, or are different from, the normal vi file
6051editor:
6052.Bl -tag -width 10n
6053.It Xo
6054.Oo Ar n Oc Ns _
6055.Xc
6056Insert a space followed by the
6057.Ar n Ns th
6058big-word from the last command in the history at the current position and enter
6059insert mode; if
6060.Ar n
6061is not specified, the last word is inserted.
6062.It #
6063Insert the comment character
6064.Pq Ql #
6065at the start of the current line and return the line to the shell (equivalent
6066to
6067.Ic I#\*(haJ ) .
6068.It Xo
6069.Oo Ar n Oc Ns g
6070.Xc
6071Like
6072.Ic G ,
6073except if
6074.Ar n
6075is not specified, it goes to the most recent remembered line.
6076.It Xo
6077.Oo Ar n Oc Ns v
6078.Xc
6079Edit line
6080.Ar n
6081using the
6082.Xr vi 1
6083editor; if
6084.Ar n
6085is not specified, the current line is edited.
6086The actual command executed is
6087.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
6088.It * and \*(haX
6089Command or file name expansion is applied to the current big-word (with an
6090appended
6091.Ql *
6092if the word contains no file globbing characters) \*(en the big-word is replaced
6093with the resulting words.
6094If the current big-word is the first on the line
6095or follows one of the characters
6096.Ql \&; ,
6097.Ql \*(Ba ,
6098.Ql & ,
6099.Ql \&(
6100or
6101.Ql \&)
6102and does not contain a slash
6103.Pq Ql / ,
6104then command expansion is done; otherwise file name expansion is done.
6105Command expansion will match the big-word against all aliases, functions and
6106built-in commands as well as any executable files found by searching the
6107directories in the
6108.Ev PATH
6109parameter.
6110File name expansion matches the big-word against the files in the
6111current directory.
6112After expansion, the cursor is placed just past the last
6113word and the editor is in insert mode.
6114.It Xo
6115.Oo Ar n Oc Ns \e ,
6116.Oo Ar n Oc Ns \*(haF ,
6117.Oo Ar n Oc Ns Aq tab ,
6118.No and
6119.Oo Ar n Oc Ns Aq esc
6120.Xc
6121Command/file name completion.
6122Replace the current big-word with the
6123longest unique match obtained after performing command and file name expansion.
6124.Aq tab
6125is only recognised if the
6126.Ic vi\-tabcomplete
6127option is set, while
6128.Aq esc
6129is only recognised if the
6130.Ic vi\-esccomplete
6131option is set (see
6132.Ic set Fl o ) .
6133If
6134.Ar n
6135is specified, the
6136.Ar n Ns th
6137possible completion is selected (as reported by the command/file name
6138enumeration command).
6139.It = and \*(haE
6140Command/file name enumeration.
6141List all the commands or files that match the current big-word.
6142.It \*(haV
6143Display the version of
6144.Nm mksh .
6145The current edit buffer is restored as soon as a key is pressed.
6146The restoring keypress is ignored.
6147.It @ Ns Ar c
6148Macro expansion.
6149Execute the commands found in the alias
6150.Ar c .
6151.El
6152.Pp
6153Intra-line movement commands:
6154.Bl -tag -width Ds
6155.It Xo
6156.Oo Ar n Oc Ns h and
6157.Oo Ar n Oc Ns \*(haH
6158.Xc
6159Move left
6160.Ar n
6161characters.
6162.It Xo
6163.Oo Ar n Oc Ns l and
6164.Oo Ar n Oc Ns Aq space
6165.Xc
6166Move right
6167.Ar n
6168characters.
6169.It 0
6170Move to column 0.
6171.It \*(ha
6172Move to the first non-whitespace character.
6173.It Xo
6174.Oo Ar n Oc Ns \*(Ba
6175.Xc
6176Move to column
6177.Ar n .
6178.It $
6179Move to the last character.
6180.It Xo
6181.Oo Ar n Oc Ns b
6182.Xc
6183Move back
6184.Ar n
6185words.
6186.It Xo
6187.Oo Ar n Oc Ns B
6188.Xc
6189Move back
6190.Ar n
6191big-words.
6192.It Xo
6193.Oo Ar n Oc Ns e
6194.Xc
6195Move forward to the end of the word,
6196.Ar n
6197times.
6198.It Xo
6199.Oo Ar n Oc Ns E
6200.Xc
6201Move forward to the end of the big-word,
6202.Ar n
6203times.
6204.It Xo
6205.Oo Ar n Oc Ns w
6206.Xc
6207Move forward
6208.Ar n
6209words.
6210.It Xo
6211.Oo Ar n Oc Ns W
6212.Xc
6213Move forward
6214.Ar n
6215big-words.
6216.It %
6217Find match.
6218The editor looks forward for the nearest parenthesis, bracket or
6219brace and then moves the cursor to the matching parenthesis, bracket or brace.
6220.It Xo
6221.Oo Ar n Oc Ns f Ns Ar c
6222.Xc
6223Move forward to the
6224.Ar n Ns th
6225occurrence of the character
6226.Ar c .
6227.It Xo
6228.Oo Ar n Oc Ns F Ns Ar c
6229.Xc
6230Move backward to the
6231.Ar n Ns th
6232occurrence of the character
6233.Ar c .
6234.It Xo
6235.Oo Ar n Oc Ns t Ns Ar c
6236.Xc
6237Move forward to just before the
6238.Ar n Ns th
6239occurrence of the character
6240.Ar c .
6241.It Xo
6242.Oo Ar n Oc Ns T Ns Ar c
6243.Xc
6244Move backward to just before the
6245.Ar n Ns th
6246occurrence of the character
6247.Ar c .
6248.It Xo
6249.Oo Ar n Oc Ns \&;
6250.Xc
6251Repeats the last
6252.Ic f , F , t
6253or
6254.Ic T
6255command.
6256.It Xo
6257.Oo Ar n Oc Ns \&,
6258.Xc
6259Repeats the last
6260.Ic f , F , t
6261or
6262.Ic T
6263command, but moves in the opposite direction.
6264.El
6265.Pp
6266Inter-line movement commands:
6267.Bl -tag -width Ds
6268.It Xo
6269.Oo Ar n Oc Ns j ,
6270.Oo Ar n Oc Ns + ,
6271.No and
6272.Oo Ar n Oc Ns \*(haN
6273.Xc
6274Move to the
6275.Ar n Ns th
6276next line in the history.
6277.It Xo
6278.Oo Ar n Oc Ns k ,
6279.Oo Ar n Oc Ns \- ,
6280.No and
6281.Oo Ar n Oc Ns \*(haP
6282.Xc
6283Move to the
6284.Ar n Ns th
6285previous line in the history.
6286.It Xo
6287.Oo Ar n Oc Ns G
6288.Xc
6289Move to line
6290.Ar n
6291in the history; if
6292.Ar n
6293is not specified, the number of the first remembered line is used.
6294.It Xo
6295.Oo Ar n Oc Ns g
6296.Xc
6297Like
6298.Ic G ,
6299except if
6300.Ar n
6301is not specified, it goes to the most recent remembered line.
6302.It Xo
6303.Oo Ar n Oc Ns / Ns Ar string
6304.Xc
6305Search backward through the history for the
6306.Ar n Ns th
6307line containing
6308.Ar string ;
6309if
6310.Ar string
6311starts with
6312.Ql \*(ha ,
6313the remainder of the string must appear at the start of the history line for
6314it to match.
6315.It Xo
6316.Oo Ar n Oc Ns \&? Ns Ar string
6317.Xc
6318Same as
6319.Ic / ,
6320except it searches forward through the history.
6321.It Xo
6322.Oo Ar n Oc Ns n
6323.Xc
6324Search for the
6325.Ar n Ns th
6326occurrence of the last search string;
6327the direction of the search is the same as the last search.
6328.It Xo
6329.Oo Ar n Oc Ns N
6330.Xc
6331Search for the
6332.Ar n Ns th
6333occurrence of the last search string;
6334the direction of the search is the opposite of the last search.
6335.It Ar ANSI-CurUp , PC-PgUp
6336Take the characters from the beginning of the line to the current
6337cursor position as search string and do a backwards history search
6338for lines beginning with this string; keep the cursor position.
6339This works only in insert mode and keeps it enabled.
6340.El
6341.Pp
6342Edit commands
6343.Bl -tag -width Ds
6344.It Xo
6345.Oo Ar n Oc Ns a
6346.Xc
6347Append text
6348.Ar n
6349times; goes into insert mode just after the current position.
6350The append is
6351only replicated if command mode is re-entered i.e.\&
6352.Aq esc
6353is used.
6354.It Xo
6355.Oo Ar n Oc Ns A
6356.Xc
6357Same as
6358.Ic a ,
6359except it appends at the end of the line.
6360.It Xo
6361.Oo Ar n Oc Ns i
6362.Xc
6363Insert text
6364.Ar n
6365times; goes into insert mode at the current position.
6366The insertion is only
6367replicated if command mode is re-entered i.e.\&
6368.Aq esc
6369is used.
6370.It Xo
6371.Oo Ar n Oc Ns I
6372.Xc
6373Same as
6374.Ic i ,
6375except the insertion is done just before the first non-blank character.
6376.It Xo
6377.Oo Ar n Oc Ns s
6378.Xc
6379Substitute the next
6380.Ar n
6381characters (i.e. delete the characters and go into insert mode).
6382.It S
6383Substitute whole line.
6384All characters from the first non-blank character to the
6385end of the line are deleted and insert mode is entered.
6386.It Xo
6387.Oo Ar n Oc Ns c Ns Ar move-cmd
6388.Xc
6389Change from the current position to the position resulting from
6390.Ar n move-cmd Ns s
6391(i.e. delete the indicated region and go into insert mode); if
6392.Ar move-cmd
6393is
6394.Ic c ,
6395the line starting from the first non-blank character is changed.
6396.It C
6397Change from the current position to the end of the line (i.e. delete to the
6398end of the line and go into insert mode).
6399.It Xo
6400.Oo Ar n Oc Ns x
6401.Xc
6402Delete the next
6403.Ar n
6404characters.
6405.It Xo
6406.Oo Ar n Oc Ns X
6407.Xc
6408Delete the previous
6409.Ar n
6410characters.
6411.It D
6412Delete to the end of the line.
6413.It Xo
6414.Oo Ar n Oc Ns d Ns Ar move-cmd
6415.Xc
6416Delete from the current position to the position resulting from
6417.Ar n move-cmd Ns s ;
6418.Ar move-cmd
6419is a movement command (see above) or
6420.Ic d ,
6421in which case the current line is deleted.
6422.It Xo
6423.Oo Ar n Oc Ns r Ns Ar c
6424.Xc
6425Replace the next
6426.Ar n
6427characters with the character
6428.Ar c .
6429.It Xo
6430.Oo Ar n Oc Ns R
6431.Xc
6432Replace.
6433Enter insert mode but overwrite existing characters instead of
6434inserting before existing characters.
6435The replacement is repeated
6436.Ar n
6437times.
6438.It Xo
6439.Oo Ar n Oc Ns \*(TI
6440.Xc
6441Change the case of the next
6442.Ar n
6443characters.
6444.It Xo
6445.Oo Ar n Oc Ns y Ns Ar move-cmd
6446.Xc
6447Yank from the current position to the position resulting from
6448.Ar n move-cmd Ns s
6449into the yank buffer; if
6450.Ar move-cmd
6451is
6452.Ic y ,
6453the whole line is yanked.
6454.It Y
6455Yank from the current position to the end of the line.
6456.It Xo
6457.Oo Ar n Oc Ns p
6458.Xc
6459Paste the contents of the yank buffer just after the current position,
6460.Ar n
6461times.
6462.It Xo
6463.Oo Ar n Oc Ns P
6464.Xc
6465Same as
6466.Ic p ,
6467except the buffer is pasted at the current position.
6468.El
6469.Pp
6470Miscellaneous vi commands
6471.Bl -tag -width Ds
6472.It \*(haJ and \*(haM
6473The current line is read, parsed and executed by the shell.
6474.It \*(haL and \*(haR
6475Redraw the current line.
6476.It Xo
6477.Oo Ar n Oc Ns \&.
6478.Xc
6479Redo the last edit command
6480.Ar n
6481times.
6482.It u
6483Undo the last edit command.
6484.It U
6485Undo all changes that have been made to the current line.
6486.It PC Home, End, Del and cursor keys
6487They move as expected, both in insert and command mode.
6488.It Ar intr No and Ar quit
6489The interrupt and quit terminal characters cause the current line to be
6490removed to the history and a new prompt to be printed.
6491.El
6492.Sh FILES
6493.Bl -tag -width XetcXsuid_profile -compact
6494.It Pa \*(TI/.mkshrc
6495User mkshrc profile (non-privileged interactive shells); see
6496.Sx Startup files.
6497The location can be changed at compile time (for embedded systems);
6498AOSP Android builds use
6499.Pa /system/etc/mkshrc .
6500.It Pa \*(TI/.profile
6501User profile (non-privileged login shells); see
6502.Sx Startup files
6503near the top of this manual.
6504.It Pa /etc/profile
6505System profile (login shells); see
6506.Sx Startup files.
6507.It Pa /etc/shells
6508Shell database.
6509.It Pa /etc/suid_profile
6510Suid profile (privileged shells); see
6511.Sx Startup files.
6512.El
6513.Pp
6514Note: On Android,
6515.Pa /system/etc/
6516contains the system and suid profile.
6517.Sh SEE ALSO
6518.Xr awk 1 ,
6519.Xr cat 1 ,
6520.Xr ed 1 ,
6521.Xr getopt 1 ,
6522.Xr lksh 1 ,
6523.Xr sed 1 ,
6524.Xr sh 1 ,
6525.Xr stty 1 ,
6526.Xr dup 2 ,
6527.Xr execve 2 ,
6528.Xr getgid 2 ,
6529.Xr getuid 2 ,
6530.Xr mknod 2 ,
6531.Xr mkfifo 2 ,
6532.Xr open 2 ,
6533.Xr pipe 2 ,
6534.Xr rename 2 ,
6535.Xr wait 2 ,
6536.Xr getopt 3 ,
6537.Xr nl_langinfo 3 ,
6538.Xr setlocale 3 ,
6539.Xr signal 3 ,
6540.Xr system 3 ,
6541.Xr tty 4 ,
6542.Xr shells 5 ,
6543.Xr environ 7 ,
6544.Xr script 7 ,
6545.Xr utf\-8 7 ,
6546.Xr mknod 8
6547.Pp
6548.Pa http://www.mirbsd.org/ksh\-chan.htm
6549.Rs
6550.%A Morris Bolsky
6551.%B "The KornShell Command and Programming Language"
6552.%D 1989
6553.%I "Prentice Hall PTR"
6554.%P "xvi\ +\ 356 pages"
6555.%O "ISBN 978\-0\-13\-516972\-8 (0\-13\-516972\-0)"
6556.Re
6557.Rs
6558.%A Morris I. Bolsky
6559.%A David G. Korn
6560.%B "The New KornShell Command and Programming Language (2nd Edition)"
6561.%D 1995
6562.%I "Prentice Hall PTR"
6563.%P "xvi\ +\ 400 pages"
6564.%O "ISBN 978\-0\-13\-182700\-4 (0\-13\-182700\-6)"
6565.Re
6566.Rs
6567.%A Stephen G. Kochan
6568.%A Patrick H. Wood
6569.%B "\\*(tNUNIX\\*(sP Shell Programming"
6570.%V "3rd Edition"
6571.%D 2003
6572.%I "Sams"
6573.%P "xiii\ +\ 437 pages"
6574.%O "ISBN 978\-0\-672\-32490\-1 (0\-672\-32490\-3)"
6575.Re
6576.Rs
6577.%A "IEEE Inc."
6578.%T "\\*(tNIEEE\\*(sP Standard for Information Technology \*(en Portable Operating System Interface (POSIX)"
6579.%V "Part 2: Shell and Utilities"
6580.%D 1993
6581.%I "IEEE Press"
6582.%P "xvii\ +\ 1195 pages"
6583.%O "ISBN 978\-1\-55937\-255\-8 (1\-55937\-255\-9)"
6584.Re
6585.Rs
6586.%A Bill Rosenblatt
6587.%B "Learning the Korn Shell"
6588.%D 1993
6589.%I "O'Reilly"
6590.%P "360 pages"
6591.%O "ISBN 978\-1\-56592\-054\-5 (1\-56592\-054\-6)"
6592.Re
6593.Rs
6594.%A Bill Rosenblatt
6595.%A Arnold Robbins
6596.%B "Learning the Korn Shell, Second Edition"
6597.%D 2002
6598.%I "O'Reilly"
6599.%P "432 pages"
6600.%O "ISBN 978\-0\-596\-00195\-7 (0\-596\-00195\-9)"
6601.Re
6602.Rs
6603.%A Barry Rosenberg
6604.%B "KornShell Programming Tutorial"
6605.%D 1991
6606.%I "Addison-Wesley Professional"
6607.%P "xxi\ +\ 324 pages"
6608.%O "ISBN 978\-0\-201\-56324\-5 (0\-201\-56324\-X)"
6609.Re
6610.Sh AUTHORS
6611.An -nosplit
6612.Nm "The MirBSD Korn Shell"
6613is developed by
6614.An mirabilos Aq Mt m@mirbsd.org
6615as part of The MirOS Project.
6616This shell is based on the public domain 7th edition Bourne shell clone by
6617.An Charles Forsyth ,
6618who kindly agreed to, in countries where the Public Domain status of the work
6619may not be valid, grant a copyright licence to the general public to deal in
6620the work without restriction and permission to sublicence derivatives under
6621the terms of any (OSI approved) Open Source licence,
6622and parts of the BRL shell by
6623.An Doug A. Gwyn ,
6624.An Doug Kingston ,
6625.An Ron Natalie ,
6626.An Arnold Robbins ,
6627.An Lou Salkind
6628and others.
6629The first release of
6630.Nm pdksh
6631was created by
6632.An Eric Gisin ,
6633and it was subsequently maintained by
6634.An John R. MacMillan ,
6635.An Simon J. Gerraty
6636and
6637.An Michael Rendell .
6638The effort of several projects, such as Debian and OpenBSD, and other
6639contributors including our users, to improve the shell is appreciated.
6640See the documentation, website and source code (CVS) for details.
6641.Pp
6642.Nm mksh\-os2
6643is developed by
6644.An KO Myung-Hun Aq Mt komh@chollian.net .
6645.Pp
6646.Nm mksh\-w32
6647is developed by
6648.An Michael Langguth Aq Mt lan@scalaris.com .
6649.Pp
6650.Nm mksh Ns / Ns Tn z/OS
6651is contributed by
6652.An Daniel Richard G. Aq Mt skunk@iSKUNK.ORG .
6653.Pp
6654The BSD daemon is Copyright \(co Marshall Kirk McKusick.
6655The complete legalese is at:
6656.Pa http://www.mirbsd.org/TaC\-mksh.txt
6657.\"
6658.\" This boils down to: feel free to use mksh.ico as application icon
6659.\" or shortcut for mksh or mksh/Win32 or OS/2; distro patches are ok
6660.\" (but we request they amend $KSH_VERSION when modifying mksh).
6661.\" Authors are Marshall Kirk McKusick (UCB), Rick Collette (ekkoBSD),
6662.\" mirabilos, Benny Siegert (MirBSD), Michael Langguth (mksh/Win32),
6663.\" KO Myung-Hun (mksh for OS/2).
6664.\"
6665.\" As far as MirBSD is concerned, the files themselves are free
6666.\" to modification and distribution under BSD/MirOS Licence, the
6667.\" restriction on use stems only from trademark law's requirement
6668.\" to protect it or lose it, which McKusick almost did.
6669.\"
6670.Sh CAVEATS
6671.Nm mksh
6672provides a consistent 32-bit integer arithmetic implementation, both
6673signed and unsigned, with sign of the result of a remainder operation
6674and wraparound defined, even (defying POSIX) on 36-bit and 64-bit systems.
6675.Pp
6676.Nm mksh
6677provides a consistent, clear interface normally.
6678This may deviate from POSIX in historic or opinionated places.
6679.Ic set Fl o Ic posix
6680(see
6681.Sx POSIX mode
6682for details)
6683will cause the shell to behave more conformant.
6684.Pp
6685For the purpose of
6686.Tn POSIX ,
6687.Nm mksh
6688supports only the
6689.Dq C
6690locale.
6691.Nm mksh Ns 's
6692.Ic utf8\-mode
6693.Em must
6694be disabled in POSIX mode, and it
6695only supports the BMP (Basic Multilingual Plane) of UCS and maps
6696raw octets into the U+EF80..U+EFFF wide character range; compare
6697.Sx Arithmetic expressions .
6698The following
6699.Tn POSIX
6700.Nm sh Ns -compatible
6701code toggles the
6702.Ic utf8\-mode
6703option dependent on the current
6704.Tn POSIX
6705locale for mksh to allow using the UTF-8 mode, within the constraints
6706outlined above, in code portable across various shell implementations:
6707.Bd -literal -offset indent
6708case ${KSH_VERSION:\-} in
6709*MIRBSD\ KSH*\*(Ba*LEGACY\ KSH*)
6710	case ${LC_ALL:\-${LC_CTYPE:\-${LANG:\-}}} in
6711	*[Uu][Tt][Ff]8*\*(Ba*[Uu][Tt][Ff]\-8*) set \-U ;;
6712	*) set +U ;;
6713	esac ;;
6714esac
6715.Ed
6716In near future, (UTF-8) locale tracking will be implemented though.
6717.Pp
6718Using
6719.Ic set Fl o Ic pipefail
6720makes the following construct error out:
6721.Bd -literal -offset indent
6722set -e
6723for x in 1 2; do
6724	false && echo $x
6725done \*(Ba cat
6726.Ed
6727This is because, while the
6728.Dq Li &&\&
6729ensures that the inner command's failure is not taken, it sets
6730the entire for..done loop's errorlevel, which is passed on by
6731.Fl o Ic pipefail .
6732Invert the inner command:
6733.Li true \*(Ba\*(Ba echo $x
6734.Pp
6735See also the FAQ below.
6736.Sh BUGS
6737Suspending (using \*(haZ) pipelines like the one below will only suspend
6738the currently running part of the pipeline; in this example,
6739.Dq Li fubar
6740is immediately printed on suspension (but not later after an
6741.Ic fg ) .
6742.Bd -literal -offset indent
6743$ /bin/sleep 666 && echo fubar
6744.Ed
6745.Pp
6746The truncation process involved when changing
6747.Ev HISTFILE
6748does not free old history entries (leaks memory) and leaks
6749old entries into the new history if their line numbers are
6750not overwritten by same-number entries from the persistent
6751history file; truncating the on-disc file to
6752.Ev HISTSIZE
6753lines has always been broken and prone to history file corruption
6754when multiple shells are accessing the file; the rollover process
6755for the in-memory portion of the history is slow, should use
6756.Xr memmove 3 .
6757.Pp
6758This document attempts to describe
6759.Nm mksh\ R57
6760and up,
6761.\" with vendor patches from insert-your-name-here,
6762compiled without any options impacting functionality, such as
6763.Dv MKSH_SMALL ,
6764when not called as
6765.Pa /bin/sh
6766which, on some systems only, enables
6767.Ic set Fl o Ic posix
6768or
6769.Ic set Fl o Ic sh
6770automatically (whose behaviour differs across targets),
6771for an operating environment supporting all of its advanced needs.
6772.Pp
6773Please report bugs in
6774.Nm
6775to the public development mailing list at
6776.Aq Mt miros\-mksh@mirbsd.org
6777(please note the EU-DSGVO/GDPR notice on
6778.Pa http://www.mirbsd.org/rss.htm#lists
6779and in the SMTP banner!) or in the
6780.Li \&#\&!/bin/mksh
6781.Pq or Li \&#ksh
6782IRC channel at
6783.Pa irc.freenode.net
6784.Pq Port 6697 SSL, 6667 unencrypted ,
6785or at:
6786.Pa https://launchpad.net/mksh
6787.Sh FREQUENTLY ASKED QUESTIONS
6788This FAQ attempts to document some of the questions users of
6789.Nm
6790or readers of this manual page may encounter.
6791.Ss I'm an Android user, so what's mksh?
6792.Nm mksh
6793is a
6794.Ux
6795shell / command interpreter, similar to
6796.Nm COMMAND.COM
6797or
6798.Nm CMD.EXE ,
6799which has been included with
6800.Tn Android Open Source Project
6801for a while now.
6802Basically, it's a program that runs in a terminal (console window),
6803takes user input and runs commands or scripts, which it can also
6804be asked to do by other programs, even in the background.
6805Any privilege pop-ups you might be encountering are thus not
6806.Nm mksh
6807issues but questions by some other program utilising it.
6808.Ss "I'm an OS/2 user, what do I need to know?"
6809Unlike the native command prompt, the current working directory is,
6810for security reasons common on Unix systems which the shell is designed for,
6811not in the search path at all; if you really need this, run the command
6812.Li PATH=.$PATHSEP$PATH
6813or add that to a suitable initialisation file.
6814.Pp
6815There are two different newline modes for mksh-os2: standard (Unix) mode,
6816in which only LF (0A hex) is supported as line separator, and "textmode",
6817which also accepts ASCII newlines (CR+LF), like most other tools on OS/2,
6818but creating an incompatibility with standard
6819.Nm .
6820If you compiled mksh from source, you will get the standard Unix mode unless
6821.Fl T
6822is added during compilation; you will most likely have gotten this shell
6823through komh's port on Hobbes, or from his OS/2 Factory on eComStation
6824Korea, which uses "textmode", though.
6825Most OS/2 users will want to use "textmode" unless they need absolute
6826compatibility with Unix
6827.Nm .
6828.Ss "How do I start mksh on a specific terminal?"
6829Normally:
6830.Dl mksh \-T/dev/tty2
6831.Pp
6832However, if you want for it to return (e.g. for an embedded
6833system rescue shell), use this on your real console device instead:
6834.Dl mksh \-T!/dev/ttyACM0
6835.Pp
6836.Nm
6837can also daemonise (send to the background):
6838.Dl mksh \-T\- \-c \*(aqexec cdio lock\*(aq
6839.Ss "POSIX says..."
6840Run the shell in POSIX mode (and possibly
6841.Nm lksh
6842instead of
6843.Nm mksh ) :
6844.Dl set \-o posix
6845.Ss "I forbid stat(2) in my SELinux policy, and some things do not work!"
6846Don't break Unix.
6847Read up on the GIGO principle.
6848Duh.
6849.Ss "My prompt from <some other shell> does not work!"
6850Contact us on the mailing list or on IRC, we'll convert it for you.
6851.Ss "Something is going wrong with my while...read loop"
6852Most likely, you've encountered the problem in which the shell runs
6853all parts of a pipeline as subshell.
6854The inner loop will be executed in a subshell and variable changes
6855cannot be propagated if run in a pipeline:
6856.Bd -literal -offset indent
6857bar \*(Ba baz \*(Ba while read foo; do ...; done
6858.Ed
6859.Pp
6860Note that
6861.Ic exit
6862in the inner loop will only exit the subshell and not the original shell.
6863Likewise, if the code is inside a function,
6864.Ic return
6865in the inner loop will only exit the subshell and won't terminate the function.
6866.Pp
6867Use co-processes instead:
6868.Bd -literal -offset indent
6869bar \*(Ba baz \*(Ba&
6870while read \-p foo; do ...; done
6871exec 3\*(Gt&p; exec 3\*(Gt&\-
6872.Ed
6873.Pp
6874If
6875.Ic read
6876is run in a loop such as
6877.Ic while read foo; do ...; done
6878then leading whitespace will be removed (IFS) and backslashes processed.
6879You might want to use
6880.Ic while IFS= read \-r foo; do ...; done
6881for pristine I/O.
6882Similarly, when using the
6883.Fl a
6884option, use of the
6885.Fl r
6886option might be prudent
6887.Pq Dq Li read \-raN\-1 arr \*(Ltfile ;
6888the same applies for NUL-terminated lines:
6889.Bd -literal -offset indent
6890find . \-type f \-print0 \*(Ba& \e
6891    while IFS= read \-d \*(aq\*(aq \-pr filename; do
6892	print \-r \-\- "found \*(Lt${filename#./}\*(Gt"
6893done
6894.Ed
6895.Pp
6896.Ss "What differences in function-local scopes are there?"
6897.Nm
6898has a different scope model from
6899.At
6900.Nm ksh ,
6901which leads to subtle differences in semantics for identical builtins.
6902This can cause issues with a
6903.Ic nameref
6904to suddenly point to a local variable by accident.
6905.Pp
6906.Tn GNU
6907.Nm bash
6908allows unsetting local variables; in
6909.Nm ,
6910doing so in a function allows back access to the global variable
6911(actually the one in the next scope up) with the same name.
6912The following code, when run before the function definitions, changes
6913the behaviour of
6914.Ic unset
6915to behave like other shells (the alias can be removed after the definitions):
6916.Bd -literal -offset indent
6917case ${KSH_VERSION:\-} in
6918*MIRBSD\ KSH*\*(Ba*LEGACY\ KSH*)
6919	function unset_compat {
6920		\e\ebuiltin typeset unset_compat_x
6921
6922		for unset_compat_x in "$@"; do
6923			eval "\e\e\e\ebuiltin unset $unset_compat_x[*]"
6924		done
6925	}
6926	\e\ebuiltin alias unset=unset_compat
6927	;;
6928esac
6929.Ed
6930.Pp
6931When a local variable is created (e.g. using
6932.Ic local ,
6933.Ic typeset ,
6934.Ic integer ,
6935.Ic \e\ebuiltin typeset )
6936it does not, like in other shells, inherit the value from the global
6937(next scope up) variable with the same name; it is rather created
6938without any value (unset but defined).
6939.Ss "I get an error in this regex comparison"
6940Use extglobs instead of regexes:
6941.Dl "[[ foo =~ (foo\*(Babar).*baz ]]	# becomes"
6942.Dl "[[ foo = *@(foo\*(Babar)*baz* ]]	# instead"
6943.Ss "Are there any extensions to avoid?"
6944.Tn GNU
6945.Nm bash
6946supports
6947.Dq Li &\*(Gt
6948.Pq and Dq Li \*(Ba&
6949to redirect both stdout and stderr in one go, but this breaks POSIX
6950and Korn Shell syntax; use POSIX redirections instead:
6951.Dl "foo \*(Ba& bar \*(Ba& baz &\*(Gtlog			     # GNU bash"
6952.Dl "foo 2\*(Gt&1 \*(Ba bar 2\*(Gt&1 \*(Ba baz \*(Gtlog 2\*(Gt&1	# POSIX"
6953.Ss "\*(haL (Ctrl-L) does not clear the screen"
6954Use \*(ha[\*(haL (Escape+Ctrl-L) or rebind it:
6955.Dl bind \*(aq\*(haL=clear-screen\*(aq
6956.Ss "\*(haU (Ctrl-U) clears the entire line"
6957If it should only delete the line up to the cursor, use:
6958.Dl bind \-m \*(haU=\*(aq\*(ha[0\*(haK\*(aq
6959.Ss "Cursor Up behaves differently from zsh"
6960Some shells make Cursor Up search in the history only for
6961commands starting with what was already entered.
6962.Nm
6963separates the shortcuts: Cursor Up goes up one command
6964and PgUp searches the history as described above.
6965.Ss "My question is not answered here!"
6966Check
6967.Pa http://www.mirbsd.org/mksh\-faq.htm
6968which contains a collection of frequently asked questions about
6969.Nm
6970in general, for packagers, etc. while these above are in user scope.
6971