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