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