• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.\" $MirOS: src/bin/mksh/mksh.1,v 1.344 2014/10/07 15:30:12 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: October 7 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.
1878Do not set this value to insanely high values such as 1000000000 because
1879.Nm
1880can then not allocate enough memory for the history and will not start.
1881.It Ev HOME
1882The default directory for the
1883.Ic cd
1884command and the value substituted for an unqualified
1885.Ic \*(TI
1886(see
1887.Sx Tilde expansion
1888below).
1889.It Ev IFS
1890Internal field separator, used during substitution and by the
1891.Ic read
1892command, to split values into distinct arguments; normally set to space, tab,
1893and newline.
1894See
1895.Sx Substitution
1896above for details.
1897.Pp
1898.Sy Note :
1899This parameter is not imported from the environment when the shell is
1900started.
1901.It Ev KSHEGID
1902The effective group id of the shell.
1903.It Ev KSHGID
1904The real group id of the shell.
1905.It Ev KSHUID
1906The real user id of the shell.
1907.It Ev KSH_VERSION
1908The name and version of the shell (read-only).
1909See also the version commands in
1910.Sx Emacs editing mode
1911and
1912.Sx Vi editing mode
1913sections, below.
1914.It Ev LINENO
1915The line number of the function or shell script that is currently being
1916executed.
1917.It Ev LINES
1918Set to the number of lines on the terminal or window.
1919Always set, defaults to 24.
1920See
1921.Ev COLUMNS .
1922.It Ev EPOCHREALTIME
1923Time since the epoch, as returned by
1924.Xr gettimeofday 2 ,
1925formatted as decimal
1926.Va tv_sec
1927followed by a dot
1928.Pq Sq .\&
1929and
1930.Va tv_usec
1931padded to exactly six decimal digits.
1932.It Ev OLDPWD
1933The previous working directory.
1934Unset if
1935.Ic cd
1936has not successfully changed directories since the shell started, or if the
1937shell doesn't know where it is.
1938.It Ev OPTARG
1939When using
1940.Ic getopts ,
1941it contains the argument for a parsed option, if it requires one.
1942.It Ev OPTIND
1943The index of the next argument to be processed when using
1944.Ic getopts .
1945Assigning 1 to this parameter causes
1946.Ic getopts
1947to process arguments from the beginning the next time it is invoked.
1948.It Ev PATH
1949A colon separated list of directories that are searched when looking for
1950commands and files sourced using the
1951.Sq \&.
1952command (see below).
1953An empty string resulting from a leading or trailing
1954colon, or two adjacent colons, is treated as a
1955.Sq \&.
1956(the current directory).
1957.It Ev PGRP
1958The process ID of the shell's process group leader.
1959.It Ev PIPESTATUS
1960An array containing the errorlevel (exit status) codes,
1961one by one, of the last pipeline run in the foreground.
1962.It Ev PPID
1963The process ID of the shell's parent.
1964.It Ev PS1
1965The primary prompt for interactive shells.
1966Parameter, command, and arithmetic
1967substitutions are performed, and
1968.Ql \&!
1969is replaced with the current command number (see the
1970.Ic fc
1971command below).
1972A literal
1973.Ql \&!
1974can be put in the prompt by placing
1975.Ql !!
1976in
1977.Ev PS1 .
1978.Pp
1979The default prompt is
1980.Sq $\ \&
1981for non-root users,
1982.Sq #\ \&
1983for root.
1984If
1985.Nm
1986is invoked by root and
1987.Ev PS1
1988does not contain a
1989.Sq #
1990character, the default value will be used even if
1991.Ev PS1
1992already exists in the environment.
1993.Pp
1994The
1995.Nm
1996distribution comes with a sample
1997.Pa dot.mkshrc
1998containing a sophisticated example, but you might like the following one
1999(note that ${HOSTNAME:=$(hostname)} and the
2000root-vs-user distinguishing clause are (in this example) executed at
2001.Ev PS1
2002assignment time, while the $USER and $PWD are escaped
2003and thus will be evaluated each time a prompt is displayed):
2004.Bd -literal
2005PS1=\*(aq${USER:=$(id \-un)}\*(aq"@${HOSTNAME:=$(hostname)}:\e$PWD $(
2006	if (( USER_ID )); then print \e$; else print \e#; fi) "
2007.Ed
2008.Pp
2009Note that since the command-line editors try to figure out how long the prompt
2010is (so they know how far it is to the edge of the screen), escape codes in
2011the prompt tend to mess things up.
2012You can tell the shell not to count certain
2013sequences (such as escape codes) by prefixing your prompt with a
2014character (such as Ctrl-A) followed by a carriage return and then delimiting
2015the escape codes with this character.
2016Any occurences of that character in the prompt are not printed.
2017By the way, don't blame me for
2018this hack; it's derived from the original
2019.Xr ksh88 1 ,
2020which did print the delimiter character so you were out of luck
2021if you did not have any non-printing characters.
2022.Pp
2023Since Backslashes and other special characters may be
2024interpreted by the shell, to set
2025.Ev PS1
2026either escape the backslash itself,
2027or use double quotes.
2028The latter is more practical.
2029This is a more complex example,
2030avoiding to directly enter special characters (for example with
2031.Ic \*(haV
2032in the emacs editing mode),
2033which embeds the current working directory,
2034in reverse video
2035.Pq colour would work, too ,
2036in the prompt string:
2037.Bd -literal -offset indent
2038x=$(print \e\e001)
2039PS1="$x$(print \e\er)$x$(tput so)$x\e$PWD$x$(tput se)$x\*(Gt "
2040.Ed
2041.Pp
2042Due to a strong suggestion from David G. Korn,
2043.Nm
2044now also supports the following form:
2045.Bd -literal -offset indent
2046PS1=$'\e1\er\e1\ee[7m\e1$PWD\e1\ee[0m\e1\*(Gt '
2047.Ed
2048.It Ev PS2
2049Secondary prompt string, by default
2050.Sq \*(Gt\ \& ,
2051used when more input is needed to complete a command.
2052.It Ev PS3
2053Prompt used by the
2054.Ic select
2055statement when reading a menu selection.
2056The default is
2057.Sq #?\ \& .
2058.It Ev PS4
2059Used to prefix commands that are printed during execution tracing (see the
2060.Ic set Fl x
2061command below).
2062Parameter, command, and arithmetic substitutions are performed
2063before it is printed.
2064The default is
2065.Sq +\ \& .
2066You may want to set it to
2067.Sq \&[$EPOCHREALTIME]\ \&
2068instead, to include timestamps.
2069.It Ev PWD
2070The current working directory.
2071May be unset or
2072.Dv NULL
2073if the shell doesn't know where it is.
2074.It Ev RANDOM
2075Each time
2076.Ev RANDOM
2077is referenced, it is assigned a number between 0 and 32767 from
2078a Linear Congruential PRNG first.
2079.It Ev REPLY
2080Default parameter for the
2081.Ic read
2082command if no names are given.
2083Also used in
2084.Ic select
2085loops to store the value that is read from standard input.
2086.It Ev SECONDS
2087The number of seconds since the shell started or, if the parameter has been
2088assigned an integer value, the number of seconds since the assignment plus the
2089value that was assigned.
2090.It Ev TMOUT
2091If set to a positive integer in an interactive shell, it specifies the maximum
2092number of seconds the shell will wait for input after printing the primary
2093prompt
2094.Pq Ev PS1 .
2095If the time is exceeded, the shell exits.
2096.It Ev TMPDIR
2097The directory temporary shell files are created in.
2098If this parameter is not
2099set, or does not contain the absolute path of a writable directory, temporary
2100files are created in
2101.Pa /tmp .
2102.It Ev USER_ID
2103The effective user id of the shell.
2104.El
2105.Ss Tilde expansion
2106Tilde expansion which is done in parallel with parameter substitution, is done
2107on words starting with an unquoted
2108.Ql \*(TI .
2109The characters following the tilde, up to the first
2110.Ql / ,
2111if any, are assumed to be a login name.
2112If the login name is empty,
2113.Ql + ,
2114or
2115.Ql \- ,
2116the value of the
2117.Ev HOME ,
2118.Ev PWD ,
2119or
2120.Ev OLDPWD
2121parameter is substituted, respectively.
2122Otherwise, the password file is
2123searched for the login name, and the tilde expression is substituted with the
2124user's home directory.
2125If the login name is not found in the password file or
2126if any quoting or parameter substitution occurs in the login name, no
2127substitution is performed.
2128.Pp
2129In parameter assignments
2130(such as those preceding a simple-command or those occurring
2131in the arguments of
2132.Ic alias ,
2133.Ic export ,
2134.Ic global ,
2135.Ic readonly ,
2136and
2137.Ic typeset ) ,
2138tilde expansion is done after any assignment
2139(i.e. after the equals sign)
2140or after an unquoted colon
2141.Pq Sq \&: ;
2142login names are also delimited by colons.
2143.Pp
2144The home directory of previously expanded login names are cached and re-used.
2145The
2146.Ic alias \-d
2147command may be used to list, change, and add to this cache (e.g.\&
2148.Ic alias \-d fac=/usr/local/facilities; cd \*(TIfac/bin ) .
2149.Ss Brace expansion (alteration)
2150Brace expressions take the following form:
2151.Bd -unfilled -offset indent
2152.Sm off
2153.Xo
2154.Ar prefix No { Ar str1 No ,...,
2155.Ar strN No } Ar suffix
2156.Xc
2157.Sm on
2158.Ed
2159.Pp
2160The expressions are expanded to
2161.Ar N
2162words, each of which is the concatenation of
2163.Ar prefix ,
2164.Ar str Ns i ,
2165and
2166.Ar suffix
2167(e.g.\&
2168.Dq a{c,b{X,Y},d}e
2169expands to four words:
2170.Dq ace ,
2171.Dq abXe ,
2172.Dq abYe ,
2173and
2174.Dq ade ) .
2175As noted in the example, brace expressions can be nested and the resulting
2176words are not sorted.
2177Brace expressions must contain an unquoted comma
2178.Pq Sq \&,
2179for expansion to occur (e.g.\&
2180.Ic {}
2181and
2182.Ic {foo}
2183are not expanded).
2184Brace expansion is carried out after parameter substitution
2185and before file name generation.
2186.Ss File name patterns
2187A file name pattern is a word containing one or more unquoted
2188.Ql \&? ,
2189.Ql * ,
2190.Ql + ,
2191.Ql @ ,
2192or
2193.Ql \&!
2194characters or
2195.Dq \&[..]
2196sequences.
2197Once brace expansion has been performed, the shell replaces file
2198name patterns with the sorted names of all the files that match the pattern
2199(if no files match, the word is left unchanged).
2200The pattern elements have the following meaning:
2201.Bl -tag -width Ds
2202.It \&?
2203Matches any single character.
2204.It \&*
2205Matches any sequence of octets.
2206.It \&[..]
2207Matches any of the octets inside the brackets.
2208Ranges of octets can be specified by separating two octets by a
2209.Ql \-
2210(e.g.\&
2211.Dq \&[a0\-9]
2212matches the letter
2213.Sq a
2214or any digit).
2215In order to represent itself, a
2216.Ql \-
2217must either be quoted or the first or last octet in the octet list.
2218Similarly, a
2219.Ql \&]
2220must be quoted or the first octet in the list if it is to represent itself
2221instead of the end of the list.
2222Also, a
2223.Ql \&!
2224appearing at the start of the list has special meaning (see below), so to
2225represent itself it must be quoted or appear later in the list.
2226.It \&[!..]
2227Like [..],
2228except it matches any octet not inside the brackets.
2229.Sm off
2230.It *( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2231.Sm on
2232Matches any string of octets that matches zero or more occurrences of the
2233specified patterns.
2234Example: The pattern
2235.Ic *(foo\*(Babar)
2236matches the strings
2237.Dq ,
2238.Dq foo ,
2239.Dq bar ,
2240.Dq foobarfoo ,
2241etc.
2242.Sm off
2243.It +( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2244.Sm on
2245Matches any string of octets that matches one or more occurrences of the
2246specified patterns.
2247Example: The pattern
2248.Ic +(foo\*(Babar)
2249matches the strings
2250.Dq foo ,
2251.Dq bar ,
2252.Dq foobar ,
2253etc.
2254.Sm off
2255.It ?( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2256.Sm on
2257Matches the empty string or a string that matches one of the specified
2258patterns.
2259Example: The pattern
2260.Ic ?(foo\*(Babar)
2261only matches the strings
2262.Dq ,
2263.Dq foo ,
2264and
2265.Dq bar .
2266.Sm off
2267.It @( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2268.Sm on
2269Matches a string that matches one of the specified patterns.
2270Example: The pattern
2271.Ic @(foo\*(Babar)
2272only matches the strings
2273.Dq foo
2274and
2275.Dq bar .
2276.Sm off
2277.It !( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2278.Sm on
2279Matches any string that does not match one of the specified patterns.
2280Examples: The pattern
2281.Ic !(foo\*(Babar)
2282matches all strings except
2283.Dq foo
2284and
2285.Dq bar ;
2286the pattern
2287.Ic !(*)
2288matches no strings; the pattern
2289.Ic !(?)*\&
2290matches all strings (think about it).
2291.El
2292.Pp
2293Note that complicated globbing, especially with alternatives,
2294is slow; using separate comparisons may (or may not) be faster.
2295.Pp
2296Note that
2297.Nm mksh
2298.Po and Nm pdksh Pc
2299never matches
2300.Sq \&.
2301and
2302.Sq .. ,
2303but
2304.At
2305.Nm ksh ,
2306Bourne
2307.Nm sh ,
2308and GNU
2309.Nm bash
2310do.
2311.Pp
2312Note that none of the above pattern elements match either a period
2313.Pq Sq \&.
2314at the start of a file name or a slash
2315.Pq Sq / ,
2316even if they are explicitly used in a [..] sequence; also, the names
2317.Sq \&.
2318and
2319.Sq ..
2320are never matched, even by the pattern
2321.Sq .* .
2322.Pp
2323If the
2324.Ic markdirs
2325option is set, any directories that result from file name generation are marked
2326with a trailing
2327.Ql / .
2328.Ss Input/output redirection
2329When a command is executed, its standard input, standard output, and standard
2330error (file descriptors 0, 1, and 2, respectively) are normally inherited from
2331the shell.
2332Three exceptions to this are commands in pipelines, for which
2333standard input and/or standard output are those set up by the pipeline,
2334asynchronous commands created when job control is disabled, for which standard
2335input is initially set to be from
2336.Pa /dev/null ,
2337and commands for which any of the following redirections have been specified:
2338.Bl -tag -width XXxxmarker
2339.It \*(Gt Ar file
2340Standard output is redirected to
2341.Ar file .
2342If
2343.Ar file
2344does not exist, it is created; if it does exist, is a regular file, and the
2345.Ic noclobber
2346option is set, an error occurs; otherwise, the file is truncated.
2347Note that this means the command
2348.Ic cmd \*(Ltfoo \*(Gtfoo
2349will open
2350.Ar foo
2351for reading and then truncate it when it opens it for writing, before
2352.Ar cmd
2353gets a chance to actually read
2354.Ar foo .
2355.It \*(Gt\*(Ba Ar file
2356Same as
2357.Ic \*(Gt ,
2358except the file is truncated, even if the
2359.Ic noclobber
2360option is set.
2361.It \*(Gt\*(Gt Ar file
2362Same as
2363.Ic \*(Gt ,
2364except if
2365.Ar file
2366exists it is appended to instead of being truncated.
2367Also, the file is opened
2368in append mode, so writes always go to the end of the file (see
2369.Xr open 2 ) .
2370.It \*(Lt Ar file
2371Standard input is redirected from
2372.Ar file ,
2373which is opened for reading.
2374.It \*(Lt\*(Gt Ar file
2375Same as
2376.Ic \*(Lt ,
2377except the file is opened for reading and writing.
2378.It \*(Lt\*(Lt Ar marker
2379After reading the command line containing this kind of redirection (called a
2380.Dq here document ) ,
2381the shell copies lines from the command source into a temporary file until a
2382line matching
2383.Ar marker
2384is read.
2385When the command is executed, standard input is redirected from the
2386temporary file.
2387If
2388.Ar marker
2389contains no quoted characters, the contents of the temporary file are processed
2390as if enclosed in double quotes each time the command is executed, so
2391parameter, command, and arithmetic substitutions are performed, along with
2392backslash
2393.Pq Sq \e
2394escapes for
2395.Ql $ ,
2396.Ql \` ,
2397.Ql \e ,
2398and
2399.Ql \enewline ,
2400but not for
2401.Ql \&" .
2402If multiple here documents are used on the same command line, they are saved in
2403order.
2404.Pp
2405If no
2406.Ar marker
2407is given, the here document ends at the next
2408.Ic \*(Lt\*(Lt
2409and substitution will be performed.
2410If
2411.Ar marker
2412is only a set of either single
2413.Dq \*(aq\*(aq
2414or double
2415.Sq \&""
2416quotes with nothing in between, the here document ends at the next empty line
2417and substitution will not be performed.
2418.It \*(Lt\*(Lt\- Ar marker
2419Same as
2420.Ic \*(Lt\*(Lt ,
2421except leading tabs are stripped from lines in the here document.
2422.It \*(Lt\*(Lt\*(Lt Ar word
2423Same as
2424.Ic \*(Lt\*(Lt ,
2425except that
2426.Ar word
2427.Em is
2428the here document.
2429This is called a here string.
2430.It \*(Lt& Ar fd
2431Standard input is duplicated from file descriptor
2432.Ar fd .
2433.Ar fd
2434can be a number, indicating the number of an existing file descriptor;
2435the letter
2436.Ql p ,
2437indicating the file descriptor associated with the output of the current
2438co-process; or the character
2439.Ql \- ,
2440indicating standard input is to be closed.
2441Note that
2442.Ar fd
2443is limited to a single digit in most shell implementations.
2444.It \*(Gt& Ar fd
2445Same as
2446.Ic \*(Lt& ,
2447except the operation is done on standard output.
2448.It &\*(Gt Ar file
2449Same as
2450.Ic \*(Gt Ar file 2\*(Gt&1 .
2451This is a GNU
2452.Nm bash
2453extension supported by
2454.Nm
2455which also supports the preceding explicit fd number, for example,
2456.Ic 3&\*(Gt Ar file
2457is the same as
2458.Ic 3\*(Gt Ar file 2\*(Gt&3
2459in
2460.Nm
2461but a syntax error in GNU
2462.Nm bash .
2463Setting the
2464.Fl o Ar posix
2465or
2466.Fl o Ar sh
2467shell options disable parsing of this redirection;
2468it's a compatibility feature to legacy scripts, to
2469not be used when writing new shell code.
2470.It Xo
2471.No &\*(Gt\*(Ba Ar file ,
2472.No &\*(Gt\*(Gt Ar file ,
2473.No &\*(Gt& Ar fd
2474.Xc
2475Same as
2476.Ic \*(Gt\*(Ba Ar file ,
2477.Ic \*(Gt\*(Gt Ar file ,
2478or
2479.Ic \*(Gt& Ar fd ,
2480followed by
2481.Ic 2\*(Gt&1 ,
2482as above.
2483These are
2484.Nm
2485extensions.
2486.El
2487.Pp
2488In any of the above redirections, the file descriptor that is redirected
2489(i.e. standard input or standard output)
2490can be explicitly given by preceding the
2491redirection with a number (portably, only a single digit).
2492Parameter, command, and arithmetic
2493substitutions, tilde substitutions, and (if the shell is interactive)
2494file name generation are all performed on the
2495.Ar file ,
2496.Ar marker ,
2497and
2498.Ar fd
2499arguments of redirections.
2500Note, however, that the results of any file name
2501generation are only used if a single file is matched; if multiple files match,
2502the word with the expanded file name generation characters is used.
2503Note
2504that in restricted shells, redirections which can create files cannot be used.
2505.Pp
2506For simple-commands, redirections may appear anywhere in the command; for
2507compound-commands
2508.Po
2509.Ic if
2510statements, etc.
2511.Pc ,
2512any redirections must appear at the end.
2513Redirections are processed after
2514pipelines are created and in the order they are given, so the following
2515will print an error with a line number prepended to it:
2516.Pp
2517.D1 $ cat /foo/bar 2\*(Gt&1 \*(Gt/dev/null \*(Ba pr \-n \-t
2518.Pp
2519File descriptors created by input/output redirections are private to the
2520Korn shell, but passed to sub-processes if
2521.Fl o Ic posix
2522or
2523.Fl o Ic sh
2524is set.
2525.Ss Arithmetic expressions
2526Integer arithmetic expressions can be used with the
2527.Ic let
2528command, inside $((..)) expressions, inside array references (e.g.\&
2529.Ar name Ns Bq Ar expr ) ,
2530as numeric arguments to the
2531.Ic test
2532command, and as the value of an assignment to an integer parameter.
2533.Em Warning :
2534This also affects implicit conversion to integer, for example as done by the
2535.Ic let
2536command.
2537.Em Never
2538use unchecked user input, e.g. from the environment, in arithmetics!
2539.Pp
2540Expressions are calculated using signed arithmetic and the
2541.Vt mksh_ari_t
2542type (a 32-bit signed integer), unless they begin with a sole
2543.Sq #
2544character, in which case they use
2545.Vt mksh_uari_t
2546.Po a 32-bit unsigned integer Pc .
2547.Pp
2548Expressions may contain alpha-numeric parameter identifiers, array references,
2549and integer constants and may be combined with the following C operators
2550(listed and grouped in increasing order of precedence):
2551.Pp
2552Unary operators:
2553.Bd -literal -offset indent
2554+ \- ! \*(TI ++ \-\-
2555.Ed
2556.Pp
2557Binary operators:
2558.Bd -literal -offset indent
2559,
2560= += \-= *= /= %= \*(Lt\*(Lt\*(Lt= \*(Gt\*(Gt\*(Gt= \*(Lt\*(Lt= \*(Gt\*(Gt= &= \*(ha= \*(Ba=
2561\*(Ba\*(Ba
2562&&
2563\*(Ba
2564\*(ha
2565&
2566== !=
2567\*(Lt \*(Lt= \*(Gt \*(Gt=
2568\*(Lt\*(Lt\*(Lt \*(Gt\*(Gt\*(Gt \*(Lt\*(Lt \*(Gt\*(Gt
2569+ \-
2570* / %
2571.Ed
2572.Pp
2573Ternary operators:
2574.Bd -literal -offset indent
2575?: (precedence is immediately higher than assignment)
2576.Ed
2577.Pp
2578Grouping operators:
2579.Bd -literal -offset indent
2580( )
2581.Ed
2582.Pp
2583Integer constants and expressions are calculated using an exactly 32-bit
2584wide, signed or unsigned, type with silent wraparound on integer overflow.
2585Integer constants may be specified with arbitrary bases using the notation
2586.Ar base Ns # Ns Ar number ,
2587where
2588.Ar base
2589is a decimal integer specifying the base, and
2590.Ar number
2591is a number in the specified base.
2592Additionally, base-16 integers may be specified by prefixing them with
2593.Sq 0x
2594.Pq case-insensitive
2595in all forms of arithmetic expressions, except as numeric arguments to the
2596.Ic test
2597built-in command.
2598Prefixing numbers with a sole digit zero
2599.Pq Sq 0
2600leads to the shell interpreting it as base-8 (octal) integer in
2601.Ic posix
2602mode
2603.Em only ;
2604historically, (pd)ksh has never done so either anyway,
2605and it's unsafe to do that, but POSIX demands it nowadays.
2606As a special
2607.Nm mksh
2608extension, numbers to the base of one are treated as either (8-bit
2609transparent) ASCII or Unicode codepoints, depending on the shell's
2610.Ic utf8\-mode
2611flag (current setting).
2612The
2613.At
2614.Nm ksh93
2615syntax of
2616.Dq \*(aqx\*(aq
2617instead of
2618.Dq 1#x
2619is also supported.
2620Note that NUL bytes (integral value of zero) cannot be used.
2621An unset or empty parameter evaluates to 0 in integer context.
2622In Unicode mode, raw octets are mapped into the range EF80..EFFF as in
2623OPTU-8, which is in the PUA and has been assigned by CSUR for this use.
2624If more than one octet in ASCII mode, or a sequence of more than one
2625octet not forming a valid and minimal CESU-8 sequence is passed, the
2626behaviour is undefined (usually, the shell aborts with a parse error,
2627but rarely, it succeeds, e.g. on the sequence C2 20).
2628That's why you should always use ASCII mode unless you know that the
2629input is well-formed UTF-8 in the range of 0000..FFFD.
2630.Pp
2631The operators are evaluated as follows:
2632.Bl -tag -width Ds -offset indent
2633.It unary +
2634Result is the argument (included for completeness).
2635.It unary \-
2636Negation.
2637.It \&!
2638Logical NOT;
2639the result is 1 if argument is zero, 0 if not.
2640.It \*(TI
2641Arithmetic (bit-wise) NOT.
2642.It ++
2643Increment; must be applied to a parameter (not a literal or other expression).
2644The parameter is incremented by 1.
2645When used as a prefix operator, the result
2646is the incremented value of the parameter; when used as a postfix operator, the
2647result is the original value of the parameter.
2648.It \-\-
2649Similar to
2650.Ic ++ ,
2651except the parameter is decremented by 1.
2652.It \&,
2653Separates two arithmetic expressions; the left-hand side is evaluated first,
2654then the right.
2655The result is the value of the expression on the right-hand side.
2656.It =
2657Assignment; the variable on the left is set to the value on the right.
2658.It Xo
2659.No += \-= *= /= %= \*(Lt\*(Lt\*(Lt= \*(Gt\*(Gt\*(Gt=
2660.No \*(Lt\*(Lt= \*(Gt\*(Gt= &= \*(ha= \*(Ba=
2661.Xc
2662Assignment operators.
2663.Sm off
2664.Ao Ar var Ac Xo
2665.Aq Ar op
2666.No = Aq Ar expr
2667.Xc
2668.Sm on
2669is the same as
2670.Sm off
2671.Ao Ar var Ac Xo
2672.No = Aq Ar var
2673.Aq Ar op
2674.Aq Ar expr ,
2675.Xc
2676.Sm on
2677with any operator precedence in
2678.Aq Ar expr
2679preserved.
2680For example,
2681.Dq var1 *= 5 + 3
2682is the same as specifying
2683.Dq var1 = var1 * (5 + 3) .
2684.It \*(Ba\*(Ba
2685Logical OR;
2686the result is 1 if either argument is non-zero, 0 if not.
2687The right argument is evaluated only if the left argument is zero.
2688.It &&
2689Logical AND;
2690the result is 1 if both arguments are non-zero, 0 if not.
2691The right argument is evaluated only if the left argument is non-zero.
2692.It \*(Ba
2693Arithmetic (bit-wise) OR.
2694.It \*(ha
2695Arithmetic (bit-wise) XOR
2696(exclusive-OR).
2697.It &
2698Arithmetic (bit-wise) AND.
2699.It ==
2700Equal; the result is 1 if both arguments are equal, 0 if not.
2701.It !=
2702Not equal; the result is 0 if both arguments are equal, 1 if not.
2703.It \*(Lt
2704Less than; the result is 1 if the left argument is less than the right, 0 if
2705not.
2706.It \*(Lt= \*(Gt \*(Gt=
2707Less than or equal, greater than, greater than or equal.
2708See
2709.Ic \*(Lt .
2710.It \*(Lt\*(Lt\*(Lt \*(Gt\*(Gt\*(Gt
2711Rotate left (right); the result is similar to shift (see
2712.Ic \*(Lt\*(Lt )
2713except that the bits shifted out at one end are shifted in
2714at the other end, instead of zero or sign bits.
2715.It \*(Lt\*(Lt \*(Gt\*(Gt
2716Shift left (right); the result is the left argument with its bits shifted left
2717(right) by the amount given in the right argument.
2718.It + \- * /
2719Addition, subtraction, multiplication, and division.
2720.It %
2721Remainder; the result is the symmetric remainder of the division of the left
2722argument by the right.
2723To get the mathematical modulus of
2724.Dq a Ic mod No b ,
2725use the formula
2726.Do
2727.Pq a % b + b
2728.No % b
2729.Dc .
2730.It Xo
2731.Sm off
2732.Aq Ar arg1 ?
2733.Aq Ar arg2 :
2734.Aq Ar arg3
2735.Sm on
2736.Xc
2737If
2738.Aq Ar arg1
2739is non-zero, the result is
2740.Aq Ar arg2 ;
2741otherwise the result is
2742.Aq Ar arg3 .
2743The non-result argument is not evaluated.
2744.El
2745.Ss Co-processes
2746A co-process (which is a pipeline created with the
2747.Sq \*(Ba&
2748operator) is an asynchronous process that the shell can both write to (using
2749.Ic print \-p )
2750and read from (using
2751.Ic read \-p ) .
2752The input and output of the co-process can also be manipulated using
2753.Ic \*(Gt&p
2754and
2755.Ic \*(Lt&p
2756redirections, respectively.
2757Once a co-process has been started, another can't
2758be started until the co-process exits, or until the co-process's input has been
2759redirected using an
2760.Ic exec Ar n Ns Ic \*(Gt&p
2761redirection.
2762If a co-process's input is redirected in this way, the next
2763co-process to be started will share the output with the first co-process,
2764unless the output of the initial co-process has been redirected using an
2765.Ic exec Ar n Ns Ic \*(Lt&p
2766redirection.
2767.Pp
2768Some notes concerning co-processes:
2769.Bl -bullet
2770.It
2771The only way to close the co-process's input (so the co-process reads an
2772end-of-file) is to redirect the input to a numbered file descriptor and then
2773close that file descriptor:
2774.Ic exec 3\*(Gt&p; exec 3\*(Gt&\-
2775.It
2776In order for co-processes to share a common output, the shell must keep the
2777write portion of the output pipe open.
2778This means that end-of-file will not be
2779detected until all co-processes sharing the co-process's output have exited
2780(when they all exit, the shell closes its copy of the pipe).
2781This can be
2782avoided by redirecting the output to a numbered file descriptor (as this also
2783causes the shell to close its copy).
2784Note that this behaviour is slightly
2785different from the original Korn shell which closes its copy of the write
2786portion of the co-process output when the most recently started co-process
2787(instead of when all sharing co-processes) exits.
2788.It
2789.Ic print \-p
2790will ignore
2791.Dv SIGPIPE
2792signals during writes if the signal is not being trapped or ignored; the same
2793is true if the co-process input has been duplicated to another file descriptor
2794and
2795.Ic print \-u Ns Ar n
2796is used.
2797.El
2798.Ss Functions
2799Functions are defined using either Korn shell
2800.Ic function Ar function-name
2801syntax or the Bourne/POSIX shell
2802.Ar function-name Ns \&()
2803syntax (see below for the difference between the two forms).
2804Functions are like
2805.Li .\(hyscripts
2806(i.e. scripts sourced using the
2807.Sq \&.
2808built-in)
2809in that they are executed in the current environment.
2810However, unlike
2811.Li .\(hyscripts ,
2812shell arguments (i.e. positional parameters $1, $2, etc.)\&
2813are never visible inside them.
2814When the shell is determining the location of a command, functions
2815are searched after special built-in commands, before builtins and the
2816.Ev PATH
2817is searched.
2818.Pp
2819An existing function may be deleted using
2820.Ic unset Fl f Ar function-name .
2821A list of functions can be obtained using
2822.Ic typeset +f
2823and the function definitions can be listed using
2824.Ic typeset \-f .
2825The
2826.Ic autoload
2827command (which is an alias for
2828.Ic typeset \-fu )
2829may be used to create undefined functions: when an undefined function is
2830executed, the shell searches the path specified in the
2831.Ev FPATH
2832parameter for a file with the same name as the function which, if found, is
2833read and executed.
2834If after executing the file the named function is found to
2835be defined, the function is executed; otherwise, the normal command search is
2836continued (i.e. the shell searches the regular built-in command table and
2837.Ev PATH ) .
2838Note that if a command is not found using
2839.Ev PATH ,
2840an attempt is made to autoload a function using
2841.Ev FPATH
2842(this is an undocumented feature of the original Korn shell).
2843.Pp
2844Functions can have two attributes,
2845.Dq trace
2846and
2847.Dq export ,
2848which can be set with
2849.Ic typeset \-ft
2850and
2851.Ic typeset \-fx ,
2852respectively.
2853When a traced function is executed, the shell's
2854.Ic xtrace
2855option is turned on for the function's duration.
2856The
2857.Dq export
2858attribute of functions is currently not used.
2859In the original Korn shell,
2860exported functions are visible to shell scripts that are executed.
2861.Pp
2862Since functions are executed in the current shell environment, parameter
2863assignments made inside functions are visible after the function completes.
2864If this is not the desired effect, the
2865.Ic typeset
2866command can be used inside a function to create a local parameter.
2867Note that
2868.At
2869.Nm ksh93
2870uses static scoping (one global scope, one local scope per function)
2871and allows local variables only on Korn style functions, whereas
2872.Nm mksh
2873uses dynamic scoping (nested scopes of varying locality).
2874Note that special parameters (e.g.\&
2875.Ic \&$$ , $! )
2876can't be scoped in this way.
2877.Pp
2878The exit status of a function is that of the last command executed in the
2879function.
2880A function can be made to finish immediately using the
2881.Ic return
2882command; this may also be used to explicitly specify the exit status.
2883.Pp
2884Functions defined with the
2885.Ic function
2886reserved word are treated differently in the following ways from functions
2887defined with the
2888.Ic \&()
2889notation:
2890.Bl -bullet
2891.It
2892The $0 parameter is set to the name of the function
2893(Bourne-style functions leave $0 untouched).
2894.It
2895Parameter assignments preceding function calls are not kept in the shell
2896environment (executing Bourne-style functions will keep assignments).
2897.It
2898.Ev OPTIND
2899is saved/reset and restored on entry and exit from the function so
2900.Ic getopts
2901can be used properly both inside and outside the function (Bourne-style
2902functions leave
2903.Ev OPTIND
2904untouched, so using
2905.Ic getopts
2906inside a function interferes with using
2907.Ic getopts
2908outside the function).
2909.It
2910Bourne-style function definitions take precedence over alias dereferences
2911and remove alias definitions upon encounter, while aliases take precedence
2912over Korn-style functions.
2913.El
2914.Pp
2915In the future, the following differences may also be added:
2916.Bl -bullet
2917.It
2918A separate trap/signal environment will be used during the execution of
2919functions.
2920This will mean that traps set inside a function will not affect the
2921shell's traps and signals that are not ignored in the shell (but may be
2922trapped) will have their default effect in a function.
2923.It
2924The EXIT trap, if set in a function, will be executed after the function
2925returns.
2926.El
2927.Ss Command execution
2928After evaluation of command-line arguments, redirections, and parameter
2929assignments, the type of command is determined: a special built-in command,
2930a function, a normal builtin, or the name of a file to execute found using the
2931.Ev PATH
2932parameter.
2933The checks are made in the above order.
2934Special built-in commands differ from other commands in that the
2935.Ev PATH
2936parameter is not used to find them, an error during their execution can
2937cause a non-interactive shell to exit, and parameter assignments that are
2938specified before the command are kept after the command completes.
2939Regular built-in commands are different only in that the
2940.Ev PATH
2941parameter is not used to find them.
2942.Pp
2943The original
2944.Nm ksh
2945and POSIX differ somewhat in which commands are considered
2946special or regular.
2947.Pp
2948POSIX special built-in utilities:
2949.Pp
2950.Ic \&. , \&: , break , continue ,
2951.Ic eval , exec , exit , export ,
2952.Ic readonly , return , set , shift ,
2953.Ic times , trap , unset
2954.Pp
2955Additional
2956.Nm
2957commands keeping assignments:
2958.Pp
2959.Ic builtin , global , typeset , wait
2960.Pp
2961Builtins that are not special:
2962.Pp
2963.Ic [ , alias , bg , bind ,
2964.Ic cat , cd , command , echo ,
2965.Ic false , fc , fg , getopts ,
2966.Ic jobs , kill , let , mknod ,
2967.Ic print , pwd , read , realpath ,
2968.Ic rename , sleep , suspend , test ,
2969.Ic true , ulimit , umask , unalias ,
2970.Ic whence
2971.Pp
2972Once the type of command has been determined, any command-line parameter
2973assignments are performed and exported for the duration of the command.
2974.Pp
2975The following describes the special and regular built-in commands:
2976.Pp
2977.Bl -tag -width false -compact
2978.It Ic \&. Ar file Op Ar arg ...
2979This is called the
2980.Dq dot
2981command.
2982Execute the commands in
2983.Ar file
2984in the current environment.
2985The file is searched for in the directories of
2986.Ev PATH .
2987If arguments are given, the positional parameters may be used to access them
2988while
2989.Ar file
2990is being executed.
2991If no arguments are given, the positional parameters are
2992those of the environment the command is used in.
2993.Pp
2994.It Ic \&: Op Ar ...
2995The null command.
2996Exit status is set to zero.
2997.Pp
2998.It Ic \&[ Ar expression Ic \&]
2999See
3000.Ic test .
3001.Pp
3002.It Xo Ic alias
3003.Oo Fl d \*(Ba t Oo Fl r Oc \*(Ba
3004.Cm +\-x Oc
3005.Op Fl p
3006.Op Cm +
3007.Oo Ar name
3008.Op Ns = Ns Ar value
3009.Ar ... Oc
3010.Xc
3011Without arguments,
3012.Ic alias
3013lists all aliases.
3014For any name without a value, the existing alias is listed.
3015Any name with a value defines an alias (see
3016.Sx Aliases
3017above).
3018.Pp
3019When listing aliases, one of two formats is used.
3020Normally, aliases are listed as
3021.Ar name Ns = Ns Ar value ,
3022where
3023.Ar value
3024is quoted.
3025If options were preceded with
3026.Ql + ,
3027or a lone
3028.Ql +
3029is given on the command line, only
3030.Ar name
3031is printed.
3032.Pp
3033The
3034.Fl d
3035option causes directory aliases which are used in tilde expansion to be
3036listed or set (see
3037.Sx Tilde expansion
3038above).
3039.Pp
3040If the
3041.Fl p
3042option is used, each alias is prefixed with the string
3043.Dq alias\ \& .
3044.Pp
3045The
3046.Fl t
3047option indicates that tracked aliases are to be listed/set (values specified on
3048the command line are ignored for tracked aliases).
3049The
3050.Fl r
3051option indicates that all tracked aliases are to be reset.
3052.Pp
3053The
3054.Fl x
3055option sets
3056.Pq Ic +x No clears
3057the export attribute of an alias, or, if no names are given, lists the aliases
3058with the export attribute (exporting an alias has no effect).
3059.Pp
3060.It Ic bg Op Ar job ...
3061Resume the specified stopped job(s) in the background.
3062If no jobs are specified,
3063.Ic %+
3064is assumed.
3065See
3066.Sx Job control
3067below for more information.
3068.Pp
3069.It Ic bind Op Fl l
3070The current bindings are listed.
3071If the
3072.Fl l
3073flag is given,
3074.Ic bind
3075instead lists the names of the functions to which keys may be bound.
3076See
3077.Sx Emacs editing mode
3078for more information.
3079.Pp
3080.It Xo Ic bind Op Fl m
3081.Ar string Ns = Ns Op Ar substitute
3082.Ar ...
3083.Xc
3084.It Xo Ic bind
3085.Ar string Ns = Ns Op Ar editing-command
3086.Ar ...
3087.Xc
3088The specified editing command is bound to the given
3089.Ar string ,
3090which should consist of a control character
3091optionally preceded by one of the two prefix characters
3092and optionally succeded by a tilde character.
3093Future input of the
3094.Ar string
3095will cause the editing command to be immediately invoked.
3096If the
3097.Fl m
3098flag is given, the specified input
3099.Ar string
3100will afterwards be immediately replaced by the given
3101.Ar substitute
3102string which may contain editing commands but not other macros.
3103If a tilde postfix is given, a tilde trailing the one or
3104two prefices and the control character is ignored, any
3105other trailing character will be processed afterwards.
3106.Pp
3107Control characters may be written using caret notation
3108i.e. \*(haX represents Ctrl-X.
3109Note that although only two prefix characters (usually ESC and \*(haX)
3110are supported, some multi-character sequences can be supported.
3111.Pp
3112The following default bindings show how the arrow keys, the home, end and
3113delete key on a BSD wsvt25, xterm\-xfree86 or GNU screen terminal are bound
3114(of course some escape sequences won't work out quite this nicely):
3115.Bd -literal -offset indent
3116bind \*(aq\*(haX\*(aq=prefix\-2
3117bind \*(aq\*(ha[[\*(aq=prefix\-2
3118bind \*(aq\*(haXA\*(aq=up\-history
3119bind \*(aq\*(haXB\*(aq=down\-history
3120bind \*(aq\*(haXC\*(aq=forward\-char
3121bind \*(aq\*(haXD\*(aq=backward\-char
3122bind \*(aq\*(haX1\*(TI\*(aq=beginning\-of\-line
3123bind \*(aq\*(haX7\*(TI\*(aq=beginning\-of\-line
3124bind \*(aq\*(haXH\*(aq=beginning\-of\-line
3125bind \*(aq\*(haX4\*(TI\*(aq=end\-of\-line
3126bind \*(aq\*(haX8\*(TI\*(aq=end\-of\-line
3127bind \*(aq\*(haXF\*(aq=end\-of\-line
3128bind \*(aq\*(haX3\*(TI\*(aq=delete\-char\-forward
3129.Ed
3130.Pp
3131.It Ic break Op Ar level
3132Exit the
3133.Ar level Ns th
3134inner-most
3135.Ic for ,
3136.Ic select ,
3137.Ic until ,
3138or
3139.Ic while
3140loop.
3141.Ar level
3142defaults to 1.
3143.Pp
3144.It Xo
3145.Ic builtin
3146.Op Fl \-
3147.Ar command Op Ar arg ...
3148.Xc
3149Execute the built-in command
3150.Ar command .
3151.Pp
3152.It Xo
3153.Ic cat
3154.Op Fl u
3155.Op Ar
3156.Xc
3157Read files sequentially, in command line order, and write them to
3158standard output.
3159If a
3160.Ar file
3161is a single dash
3162.Pq Sq -
3163or absent, read from standard input.
3164Unless compiled with
3165.Dv MKSH_NO_EXTERNAL_CAT ,
3166if any options are given, an external
3167.Xr cat 1
3168utility is invoked instead if called from the shell.
3169For direct builtin calls, the
3170.Tn POSIX
3171.Fl u
3172option is supported as a no-op.
3173.Pp
3174.It Xo
3175.Ic cd
3176.Op Fl L
3177.Op Ar dir
3178.Xc
3179.It Xo
3180.Ic cd
3181.Fl P Op Fl e
3182.Op Ar dir
3183.Xc
3184.It Xo
3185.Ic chdir
3186.Op Fl eLP
3187.Op Ar dir
3188.Xc
3189Set the working directory to
3190.Ar dir .
3191If the parameter
3192.Ev CDPATH
3193is set, it lists the search path for the directory containing
3194.Ar dir .
3195A
3196.Dv NULL
3197path means the current directory.
3198If
3199.Ar dir
3200is found in any component of the
3201.Ev CDPATH
3202search path other than the
3203.Dv NULL
3204path, the name of the new working directory will be written to standard output.
3205If
3206.Ar dir
3207is missing, the home directory
3208.Ev HOME
3209is used.
3210If
3211.Ar dir
3212is
3213.Ql \- ,
3214the previous working directory is used (see the
3215.Ev OLDPWD
3216parameter).
3217.Pp
3218If the
3219.Fl L
3220option (logical path) is used or if the
3221.Ic physical
3222option isn't set (see the
3223.Ic set
3224command below), references to
3225.Sq ..
3226in
3227.Ar dir
3228are relative to the path used to get to the directory.
3229If the
3230.Fl P
3231option (physical path) is used or if the
3232.Ic physical
3233option is set,
3234.Sq ..
3235is relative to the filesystem directory tree.
3236The
3237.Ev PWD
3238and
3239.Ev OLDPWD
3240parameters are updated to reflect the current and old working directory,
3241respectively.
3242If the
3243.Fl e
3244option is set for physical filesystem traversal, and
3245.Ev PWD
3246could not be set, the exit code is 1; greater than 1 if an
3247error occurred, 0 otherwise.
3248.Pp
3249.It Xo
3250.Ic cd
3251.Op Fl eLP
3252.Ar old new
3253.Xc
3254.It Xo
3255.Ic chdir
3256.Op Fl eLP
3257.Ar old new
3258.Xc
3259The string
3260.Ar new
3261is substituted for
3262.Ar old
3263in the current directory, and the shell attempts to change to the new
3264directory.
3265.Pp
3266.It Xo
3267.Ic command
3268.Op Fl pVv
3269.Ar cmd
3270.Op Ar arg ...
3271.Xc
3272If neither the
3273.Fl v
3274nor
3275.Fl V
3276option is given,
3277.Ar cmd
3278is executed exactly as if
3279.Ic command
3280had not been specified, with two exceptions:
3281firstly,
3282.Ar cmd
3283cannot be a shell function;
3284and secondly, special built-in commands lose their specialness
3285(i.e. redirection and utility errors do not cause the shell to
3286exit, and command assignments are not permanent).
3287.Pp
3288If the
3289.Fl p
3290option is given, a default search path is used instead of the current value of
3291.Ev PATH ,
3292the actual value of which is system dependent.
3293.Pp
3294If the
3295.Fl v
3296option is given, instead of executing
3297.Ar cmd ,
3298information about what would be executed is given (and the same is done for
3299.Ar arg ... ) .
3300For special and regular built-in commands and functions, their names are simply
3301printed; for aliases, a command that defines them is printed; and for commands
3302found by searching the
3303.Ev PATH
3304parameter, the full path of the command is printed.
3305If no command is found
3306(i.e. the path search fails), nothing is printed and
3307.Ic command
3308exits with a non-zero status.
3309The
3310.Fl V
3311option is like the
3312.Fl v
3313option, except it is more verbose.
3314.Pp
3315.It Ic continue Op Ar level
3316Jumps to the beginning of the
3317.Ar level Ns th
3318inner-most
3319.Ic for ,
3320.Ic select ,
3321.Ic until ,
3322or
3323.Ic while
3324loop.
3325.Ar level
3326defaults to 1.
3327.Pp
3328.It Xo
3329.Ic echo
3330.Op Fl Een
3331.Op Ar arg ...
3332.Xc
3333.Em Warning:
3334this utility is not portable; use the Korn shell builtin
3335.Ic print
3336instead.
3337.Pp
3338Prints its arguments (separated by spaces) followed by a newline, to the
3339standard output.
3340The newline is suppressed if any of the arguments contain the
3341backslash sequence
3342.Ql \ec .
3343See the
3344.Ic print
3345command below for a list of other backslash sequences that are recognised.
3346.Pp
3347The options are provided for compatibility with
3348.Bx
3349shell scripts.
3350The
3351.Fl n
3352option suppresses the trailing newline,
3353.Fl e
3354enables backslash interpretation (a no-op, since this is normally done), and
3355.Fl E
3356suppresses backslash interpretation.
3357.Pp
3358If the
3359.Ic posix
3360or
3361.Ic sh
3362option is set or this is a direct builtin call, only the first argument
3363is treated as an option, and only if it is exactly
3364.Dq Fl n .
3365Backslash interpretation is disabled.
3366.Pp
3367.It Ic eval Ar command ...
3368The arguments are concatenated (with spaces between them) to form a single
3369string which the shell then parses and executes in the current environment.
3370.Pp
3371.It Xo
3372.Ic exec
3373.Op Ar command Op Ar arg ...
3374.Xc
3375The command is executed without forking, replacing the shell process.
3376.Pp
3377If no command is given except for I/O redirection, the I/O redirection is
3378permanent and the shell is
3379not replaced.
3380Any file descriptors greater than 2 which are opened or
3381.Xr dup 2 Ns 'd
3382in this way are not made available to other executed commands (i.e. commands
3383that are not built-in to the shell).
3384Note that the Bourne shell differs here;
3385it does pass these file descriptors on.
3386.Pp
3387.It Ic exit Op Ar status
3388The shell exits with the specified exit status.
3389If
3390.Ar status
3391is not specified, the exit status is the current value of the
3392.Ic $?\&
3393parameter.
3394.Pp
3395.It Xo
3396.Ic export
3397.Op Fl p
3398.Op Ar parameter Ns Op = Ns Ar value
3399.Xc
3400Sets the export attribute of the named parameters.
3401Exported parameters are passed in the environment to executed commands.
3402If values are specified, the named parameters are also assigned.
3403.Pp
3404If no parameters are specified, all parameters with the export attribute
3405set are printed one per line; either their names, or, if a
3406.Ql \-
3407with no option letter is specified, name=value pairs, or, with
3408.Fl p ,
3409.Ic export
3410commands suitable for re-entry.
3411.Pp
3412.It Ic false
3413A command that exits with a non-zero status.
3414.Pp
3415.It Xo
3416.Ic fc
3417.Oo Fl e Ar editor \*(Ba
3418.Fl l Op Fl n Oc
3419.Op Fl r
3420.Op Ar first Op Ar last
3421.Xc
3422.Ar first
3423and
3424.Ar last
3425select commands from the history.
3426Commands can be selected by history number
3427(negative numbers go backwards from the current, most recent, line)
3428or a string specifying the most recent command starting with that string.
3429The
3430.Fl l
3431option lists the command on standard output, and
3432.Fl n
3433inhibits the default command numbers.
3434The
3435.Fl r
3436option reverses the order of the list.
3437Without
3438.Fl l ,
3439the selected commands are edited by the editor specified with the
3440.Fl e
3441option, or if no
3442.Fl e
3443is specified, the editor specified by the
3444.Ev FCEDIT
3445parameter (if this parameter is not set,
3446.Pa /bin/ed
3447is used), and then executed by the shell.
3448.Pp
3449.It Xo
3450.Ic fc
3451.Cm \-e \- \*(Ba Fl s
3452.Op Fl g
3453.Op Ar old Ns = Ns Ar new
3454.Op Ar prefix
3455.Xc
3456Re-execute the selected command (the previous command by default) after
3457performing the optional substitution of
3458.Ar old
3459with
3460.Ar new .
3461If
3462.Fl g
3463is specified, all occurrences of
3464.Ar old
3465are replaced with
3466.Ar new .
3467The meaning of
3468.Cm \-e \-
3469and
3470.Fl s
3471is identical: re-execute the selected command without invoking an editor.
3472This command is usually accessed with the predefined:
3473.Ic alias r=\*(aqfc \-e \-\*(aq
3474.Pp
3475.It Ic fg Op Ar job ...
3476Resume the specified job(s) in the foreground.
3477If no jobs are specified,
3478.Ic %+
3479is assumed.
3480See
3481.Sx Job control
3482below for more information.
3483.Pp
3484.It Xo
3485.Ic getopts
3486.Ar optstring name
3487.Op Ar arg ...
3488.Xc
3489Used by shell procedures to parse the specified arguments (or positional
3490parameters, if no arguments are given) and to check for legal options.
3491.Ar optstring
3492contains the option letters that
3493.Ic getopts
3494is to recognise.
3495If a letter is followed by a colon, the option is expected to
3496have an argument.
3497Options that do not take arguments may be grouped in a single argument.
3498If an option takes an argument and the option character is not the
3499last character of the argument it is found in, the remainder of the argument is
3500taken to be the option's argument; otherwise, the next argument is the option's
3501argument.
3502.Pp
3503Each time
3504.Ic getopts
3505is invoked, it places the next option in the shell parameter
3506.Ar name
3507and the index of the argument to be processed by the next call to
3508.Ic getopts
3509in the shell parameter
3510.Ev OPTIND .
3511If the option was introduced with a
3512.Ql + ,
3513the option placed in
3514.Ar name
3515is prefixed with a
3516.Ql + .
3517When an option requires an argument,
3518.Ic getopts
3519places it in the shell parameter
3520.Ev OPTARG .
3521.Pp
3522When an illegal option or a missing option argument is encountered, a question
3523mark or a colon is placed in
3524.Ar name
3525(indicating an illegal option or missing argument, respectively) and
3526.Ev OPTARG
3527is set to the option character that caused the problem.
3528Furthermore, if
3529.Ar optstring
3530does not begin with a colon, a question mark is placed in
3531.Ar name ,
3532.Ev OPTARG
3533is unset, and an error message is printed to standard error.
3534.Pp
3535When the end of the options is encountered,
3536.Ic getopts
3537exits with a non-zero exit status.
3538Options end at the first (non-option
3539argument) argument that does not start with a
3540.Ql \- ,
3541or when a
3542.Ql \-\-
3543argument is encountered.
3544.Pp
3545Option parsing can be reset by setting
3546.Ev OPTIND
3547to 1 (this is done automatically whenever the shell or a shell procedure is
3548invoked).
3549.Pp
3550Warning: Changing the value of the shell parameter
3551.Ev OPTIND
3552to a value other than 1, or parsing different sets of arguments without
3553resetting
3554.Ev OPTIND ,
3555may lead to unexpected results.
3556.Pp
3557.It global Ar ...
3558See
3559.Ic typeset .
3560.Pp
3561.It Xo
3562.Ic hash
3563.Op Fl r
3564.Op Ar name ...
3565.Xc
3566Without arguments, any hashed executable command pathnames are listed.
3567The
3568.Fl r
3569option causes all hashed commands to be removed from the hash table.
3570Each
3571.Ar name
3572is searched as if it were a command name and added to the hash table if it is
3573an executable command.
3574.Pp
3575.It Xo
3576.Ic jobs
3577.Op Fl lnp
3578.Op Ar job ...
3579.Xc
3580Display information about the specified job(s); if no jobs are specified, all
3581jobs are displayed.
3582The
3583.Fl n
3584option causes information to be displayed only for jobs that have changed
3585state since the last notification.
3586If the
3587.Fl l
3588option is used, the process ID of each process in a job is also listed.
3589The
3590.Fl p
3591option causes only the process group of each job to be printed.
3592See
3593.Sx Job control
3594below for the format of
3595.Ar job
3596and the displayed job.
3597.Pp
3598.It Xo
3599.Ic kill
3600.Oo Fl s Ar signame \*(Ba
3601.No \- Ns Ar signum \*(Ba
3602.No \- Ns Ar signame Oc
3603.No { Ar job \*(Ba pid \*(Ba pgrp No }
3604.Ar ...
3605.Xc
3606Send the specified signal to the specified jobs, process IDs, or process
3607groups.
3608If no signal is specified, the
3609.Dv TERM
3610signal is sent.
3611If a job is specified, the signal is sent to the job's process group.
3612See
3613.Sx Job control
3614below for the format of
3615.Ar job .
3616.Pp
3617.It Xo
3618.Ic kill
3619.Fl l
3620.Op Ar exit-status ...
3621.Xc
3622Print the signal name corresponding to
3623.Ar exit-status .
3624If no arguments are specified, a list of all the signals, their numbers, and
3625a short description of them are printed.
3626.Pp
3627.It Ic let Op Ar expression ...
3628Each expression is evaluated (see
3629.Sx Arithmetic expressions
3630above).
3631If all expressions are successfully evaluated, the exit status is 0 (1)
3632if the last expression evaluated to non-zero (zero).
3633If an error occurs during
3634the parsing or evaluation of an expression, the exit status is greater than 1.
3635Since expressions may need to be quoted,
3636.No \&(( Ar expr No ))
3637is syntactic sugar for
3638.No let \&" Ns Ar expr Ns \&" .
3639.Pp
3640.It Ic let]
3641Internally used alias for
3642.Ic let .
3643.Pp
3644.It Xo
3645.Ic mknod
3646.Op Fl m Ar mode
3647.Ar name
3648.Cm b\*(Bac
3649.Ar major minor
3650.Xc
3651.It Xo
3652.Ic mknod
3653.Op Fl m Ar mode
3654.Ar name
3655.Cm p
3656.Xc
3657Create a device special file.
3658The file type may be
3659.Cm b
3660(block type device),
3661.Cm c
3662(character type device),
3663or
3664.Cm p
3665.Pq named pipe , Tn FIFO .
3666The file created may be modified according to its
3667.Ar mode
3668(via the
3669.Fl m
3670option),
3671.Ar major
3672(major device number),
3673and
3674.Ar minor
3675(minor device number).
3676.Pp
3677See
3678.Xr mknod 8
3679for further information.
3680.Pp
3681.It Xo
3682.Ic print
3683.Oo Fl nprsu Ns Oo Ar n Oc \*(Ba
3684.Fl R Op Fl en Oc
3685.Op Ar argument ...
3686.Xc
3687.Ic print
3688prints its arguments on the standard output, separated by spaces and
3689terminated with a newline.
3690The
3691.Fl n
3692option suppresses the newline.
3693By default, certain C escapes are translated.
3694These include these mentioned in
3695.Sx Backslash expansion
3696above, as well as
3697.Ql \ec ,
3698which is equivalent to using the
3699.Fl n
3700option.
3701Backslash expansion may be inhibited with the
3702.Fl r
3703option.
3704The
3705.Fl s
3706option prints to the history file instead of standard output; the
3707.Fl u
3708option prints to file descriptor
3709.Ar n
3710.Po
3711.Ar n
3712defaults to 1 if omitted
3713.Pc ;
3714and the
3715.Fl p
3716option prints to the co-process (see
3717.Sx Co-processes
3718above).
3719.Pp
3720The
3721.Fl R
3722option is used to emulate, to some degree, the
3723.Bx
3724.Xr echo 1
3725command which does not process
3726.Ql \e
3727sequences unless the
3728.Fl e
3729option is given.
3730As above, the
3731.Fl n
3732option suppresses the trailing newline.
3733.Pp
3734.It Ic printf Ar format Op Ar arguments ...
3735Formatted output.
3736Approximately the same as the
3737.Xr printf 1 ,
3738utility, except it uses the same
3739.Sx Backslash expansion
3740and I/O code and does hot handle floating point as the rest of
3741.Nm mksh .
3742This is not normally part of
3743.Nm mksh ;
3744however, distributors may have added this as builtin as a speed hack.
3745Do not use in new code.
3746.Pp
3747.It Ic pwd Op Fl LP
3748Print the present working directory.
3749If the
3750.Fl L
3751option is used or if the
3752.Ic physical
3753option isn't set (see the
3754.Ic set
3755command below), the logical path is printed (i.e. the path used to
3756.Ic cd
3757to the current directory).
3758If the
3759.Fl P
3760option (physical path) is used or if the
3761.Ic physical
3762option is set, the path determined from the filesystem (by following
3763.Sq ..
3764directories to the root directory) is printed.
3765.Pp
3766.It Xo
3767.Ic read
3768.Op Fl A \*(Ba Fl a
3769.Op Fl d Ar x
3770.Oo Fl N Ar z \*(Ba
3771.Fl n Ar z Oc
3772.Oo Fl p \*(Ba
3773.Fl u Ns Op Ar n
3774.Oc Op Fl t Ar n
3775.Op Fl rs
3776.Op Ar p ...
3777.Xc
3778Reads a line of input, separates the input into fields using the
3779.Ev IFS
3780parameter (see
3781.Sx Substitution
3782above), and assigns each field to the specified parameters
3783.Ar p .
3784If no parameters are specified, the
3785.Ev REPLY
3786parameter is used to store the result.
3787With the
3788.Fl A
3789and
3790.Fl a
3791options, only no or one parameter is accepted.
3792If there are more parameters than fields, the extra parameters are set to
3793the empty string or 0; if there are more fields than parameters, the last
3794parameter is assigned the remaining fields (including the word separators).
3795.Pp
3796The options are as follows:
3797.Bl -tag -width XuXnX
3798.It Fl A
3799Store the result into the parameter
3800.Ar p
3801(or
3802.Ev REPLY )
3803as array of words.
3804.It Fl a
3805Store the result without word splitting into the parameter
3806.Ar p
3807(or
3808.Ev REPLY )
3809as array of characters (wide characters if the
3810.Ic utf8\-mode
3811option is enacted, octets otherwise).
3812.It Fl d Ar x
3813Use the first byte of
3814.Ar x ,
3815.Dv NUL
3816if empty, instead of the ASCII newline character as input line delimiter.
3817.It Fl N Ar z
3818Instead of reading till end-of-line, read exactly
3819.Ar z
3820bytes; less if EOF or a timeout occurs.
3821.It Fl n Ar z
3822Instead of reading till end-of-line, read up to
3823.Ar z
3824bytes but return as soon as any bytes are read, e.g.\& from a
3825slow terminal device, or if EOF or a timeout occurs.
3826.It Fl p
3827Read from the currently active co-process, see
3828.Sx Co-processes
3829above for details on this.
3830.It Fl u Ns Op Ar n
3831Read from the file descriptor
3832.Ar n
3833(defaults to 0, i.e.\& standard input).
3834The argument must immediately follow the option character.
3835.It Fl t Ar n
3836Interrupt reading after
3837.Ar n
3838seconds (specified as positive decimal value with an optional fractional part).
3839.It Fl r
3840Normally, the ASCII backslash character escapes the special
3841meaning of the following character and is stripped from the input;
3842.Ic read
3843does not stop when encountering a backslash-newline sequence and
3844does not store that newline in the result.
3845This option enables raw mode, in which backslashes are not processed.
3846.It Fl s
3847The input line is saved to the history.
3848.El
3849.Pp
3850If the input is a terminal, both the
3851.Fl N
3852and
3853.Fl n
3854options set it into raw mode;
3855they read an entire file if \-1 is passed as
3856.Ar z
3857argument.
3858.Pp
3859The first parameter may have a question mark and a string appended to it, in
3860which case the string is used as a prompt (printed to standard error before
3861any input is read) if the input is a
3862.Xr tty 4
3863(e.g.\&
3864.Ic read nfoo?\*(aqnumber of foos: \*(aq ) .
3865.Pp
3866If no input is read or a timeout occurred,
3867.Ic read
3868exits with a non-zero status.
3869.Pp
3870Another handy set of tricks:
3871If
3872.Ic read
3873is run in a loop such as
3874.Ic while read foo; do ...; done
3875then leading whitespace will be removed (IFS) and backslashes processed.
3876You might want to use
3877.Ic while IFS= read \-r foo; do ...; done
3878for pristine I/O.
3879Similarily, when using the
3880.Fl a
3881option, use of the
3882.Fl r
3883option might be prudent; the same applies for:
3884.Bd -literal -offset indent
3885find . \-type f \-print0 \*(Ba \e
3886    while IFS= read \-d \*(aq\*(aq \-r filename; do
3887	print \-r \-\- "found \*(Lt${filename#./}\*(Gt"
3888done
3889.Ed
3890.Pp
3891The inner loop will be executed in a subshell and variable changes
3892cannot be propagated if executed in a pipeline:
3893.Bd -literal -offset indent
3894bar \*(Ba baz \*(Ba while read foo; do ...; done
3895.Ed
3896.Pp
3897Use co-processes instead:
3898.Bd -literal -offset indent
3899bar \*(Ba baz \*(Ba&
3900while read \-p foo; do ...; done
3901exec 3\*(Gt&p; exec 3\*(Gt&\-
3902.Ed
3903.Pp
3904.It Xo
3905.Ic readonly
3906.Op Fl p
3907.Oo Ar parameter
3908.Op Ns = Ns Ar value
3909.Ar ... Oc
3910.Xc
3911Sets the read-only attribute of the named parameters.
3912If values are given,
3913parameters are set to them before setting the attribute.
3914Once a parameter is
3915made read-only, it cannot be unset and its value cannot be changed.
3916.Pp
3917If no parameters are specified, the names of all parameters with the read-only
3918attribute are printed one per line, unless the
3919.Fl p
3920option is used, in which case
3921.Ic readonly
3922commands defining all read-only parameters, including their values, are
3923printed.
3924.Pp
3925.It Xo
3926.Ic realpath
3927.Op Fl \-
3928.Ar name
3929.Xc
3930Prints the resolved absolute pathname corresponding to
3931.Ar name .
3932If
3933.Ar name
3934ends with a slash
3935.Pq Sq / ,
3936it's also checked for existence and whether it is a directory; otherwise,
3937.Ic realpath
3938returns 0 if the pathname either exists or can be created immediately,
3939i.e. all but the last component exist and are directories.
3940.Pp
3941.It Xo
3942.Ic rename
3943.Op Fl \-
3944.Ar from to
3945.Xc
3946Renames the file
3947.Ar from
3948to
3949.Ar to .
3950Both must be complete pathnames and on the same device.
3951This builtin is intended for emergency situations where
3952.Pa /bin/mv
3953becomes unusable, and directly calls
3954.Xr rename 2 .
3955.Pp
3956.It Ic return Op Ar status
3957Returns from a function or
3958.Ic .\&
3959script, with exit status
3960.Ar status .
3961If no
3962.Ar status
3963is given, the exit status of the last executed command is used.
3964If used outside of a function or
3965.Ic .\&
3966script, it has the same effect as
3967.Ic exit .
3968Note that
3969.Nm
3970treats both profile and
3971.Ev ENV
3972files as
3973.Ic .\&
3974scripts, while the original Korn shell only treats profiles as
3975.Ic .\&
3976scripts.
3977.Pp
3978.It Xo
3979.Ic set Op Ic +\-abCefhiklmnprsUuvXx
3980.Op Ic +\-o Ar option
3981.Op Ic +\-A Ar name
3982.Op Fl \-
3983.Op Ar arg ...
3984.Xc
3985The
3986.Ic set
3987command can be used to set
3988.Pq Ic \-
3989or clear
3990.Pq Ic +
3991shell options, set the positional parameters, or set an array parameter.
3992Options can be changed using the
3993.Cm +\-o Ar option
3994syntax, where
3995.Ar option
3996is the long name of an option, or using the
3997.Cm +\- Ns Ar letter
3998syntax, where
3999.Ar letter
4000is the option's single letter name (not all options have a single letter name).
4001The following table lists both option letters (if they exist) and long names
4002along with a description of what the option does:
4003.Bl -tag -width 3n
4004.It Fl A Ar name
4005Sets the elements of the array parameter
4006.Ar name
4007to
4008.Ar arg ...
4009If
4010.Fl A
4011is used, the array is reset (i.e. emptied) first; if
4012.Ic +A
4013is used, the first N elements are set (where N is the number of arguments);
4014the rest are left untouched.
4015.Pp
4016An alternative syntax for the command
4017.Ic set \-A foo \-\- a b c
4018which is compatible to
4019.Tn GNU
4020.Nm bash
4021and also supported by
4022.At
4023.Nm ksh93
4024is:
4025.Ic foo=(a b c); foo+=(d e)
4026.Pp
4027Another
4028.At
4029.Nm ksh93
4030and
4031.Tn GNU
4032.Nm bash
4033extension allows specifying the indices used for
4034.Ar arg ...
4035.Pq from the above example, Ic a b c
4036like this:
4037.Ic set \-A foo \-\- [0]=a [1]=b [2]=c
4038or
4039.Ic foo=([0]=a [1]=b [2]=c)
4040which can also be written
4041.Ic foo=([0]=a b c)
4042because indices are incremented automatically.
4043.It Fl a \*(Ba Fl o Ic allexport
4044All new parameters are created with the export attribute.
4045.It Fl b \*(Ba Fl o Ic notify
4046Print job notification messages asynchronously, instead of just before the
4047prompt.
4048Only used if job control is enabled
4049.Pq Fl m .
4050.It Fl C \*(Ba Fl o Ic noclobber
4051Prevent \*(Gt redirection from overwriting existing files.
4052Instead, \*(Gt\*(Ba must be used to force an overwrite.
4053Note that this is not safe to use for creation of temporary files or
4054lockfiles due to a TOCTOU in a check allowing one to redirect output to
4055.Pa /dev/null
4056or other device files even in
4057.Ic noclobber
4058mode.
4059.It Fl e \*(Ba Fl o Ic errexit
4060Exit (after executing the
4061.Dv ERR
4062trap) as soon as an error occurs or a command fails (i.e. exits with a
4063non-zero status).
4064This does not apply to commands whose exit status is
4065explicitly tested by a shell construct such as
4066.Ic if ,
4067.Ic until ,
4068.Ic while ,
4069or
4070.Ic !\&
4071statements.
4072For
4073.Ic &&
4074or
4075.Ic \*(Ba\*(Ba ,
4076only the status of the last command is tested.
4077.It Fl f \*(Ba Fl o Ic noglob
4078Do not expand file name patterns.
4079.It Fl h \*(Ba Fl o Ic trackall
4080Create tracked aliases for all executed commands (see
4081.Sx Aliases
4082above).
4083Enabled by default for non-interactive shells.
4084.It Fl i \*(Ba Fl o Ic interactive
4085The shell is an interactive shell.
4086This option can only be used when the shell is invoked.
4087See above for a description of what this means.
4088.It Fl k \*(Ba Fl o Ic keyword
4089Parameter assignments are recognised anywhere in a command.
4090.It Fl l \*(Ba Fl o Ic login
4091The shell is a login shell.
4092This option can only be used when the shell is invoked.
4093See above for a description of what this means.
4094.It Fl m \*(Ba Fl o Ic monitor
4095Enable job control (default for interactive shells).
4096.It Fl n \*(Ba Fl o Ic noexec
4097Do not execute any commands.
4098Useful for checking the syntax of scripts
4099(ignored if interactive).
4100.It Fl p \*(Ba Fl o Ic privileged
4101The shell is a privileged shell.
4102It is set automatically if, when the shell starts,
4103the real UID or GID does not match
4104the effective UID (EUID) or GID (EGID), respectively.
4105See above for a description of what this means.
4106.It Fl r \*(Ba Fl o Ic restricted
4107The shell is a restricted shell.
4108This option can only be used when the shell is invoked.
4109See above for a description of what this means.
4110.It Fl s \*(Ba Fl o Ic stdin
4111If used when the shell is invoked, commands are read from standard input.
4112Set automatically if the shell is invoked with no arguments.
4113.Pp
4114When
4115.Fl s
4116is used with the
4117.Ic set
4118command it causes the specified arguments to be sorted before assigning them to
4119the positional parameters (or to array
4120.Ar name ,
4121if
4122.Fl A
4123is used).
4124.It Fl U \*(Ba Fl o Ic utf8\-mode
4125Enable UTF-8 support in the
4126.Sx Emacs editing mode
4127and internal string handling functions.
4128This flag is disabled by default, but can be enabled by setting it on the
4129shell command line; is enabled automatically for interactive shells if
4130requested at compile time, your system supports
4131.Fn setlocale LC_CTYPE \&""
4132and optionally
4133.Fn nl_langinfo CODESET ,
4134or the
4135.Ev LC_ALL ,
4136.Ev LC_CTYPE ,
4137or
4138.Ev LANG
4139environment variables,
4140and at least one of these returns something that matches
4141.Dq UTF\-8
4142or
4143.Dq utf8
4144case-insensitively; for direct builtin calls depending on the
4145aforementioned environment variables; or for stdin or scripts,
4146if the input begins with a UTF-8 Byte Order Mark.
4147.It Fl u \*(Ba Fl o Ic nounset
4148Referencing of an unset parameter, other than
4149.Dq $@
4150or
4151.Dq $* ,
4152is treated as an error, unless one of the
4153.Ql \- ,
4154.Ql + ,
4155or
4156.Ql =
4157modifiers is used.
4158.It Fl v \*(Ba Fl o Ic verbose
4159Write shell input to standard error as it is read.
4160.It Fl X \*(Ba Fl o Ic markdirs
4161Mark directories with a trailing
4162.Ql /
4163during file name generation.
4164.It Fl x \*(Ba Fl o Ic xtrace
4165Print command trees when they are executed, preceded by
4166the value of
4167.Ev PS4 .
4168.It Fl o Ic bgnice
4169Background jobs are run with lower priority.
4170.It Fl o Ic braceexpand
4171Enable brace expansion (a.k.a. alternation).
4172This is enabled by default.
4173If disabled, tilde expansion after an equals sign is disabled as a side effect.
4174.It Fl o Ic emacs
4175Enable BRL emacs-like command-line editing (interactive shells only); see
4176.Sx Emacs editing mode .
4177.It Fl o Ic gmacs
4178Enable gmacs-like command-line editing (interactive shells only).
4179Currently identical to emacs editing except that transpose\-chars (\*(haT) acts
4180slightly differently.
4181.It Fl o Ic ignoreeof
4182The shell will not (easily) exit when end-of-file is read;
4183.Ic exit
4184must be used.
4185To avoid infinite loops, the shell will exit if
4186.Dv EOF
4187is read 13 times in a row.
4188.It Fl o Ic inherit\-xtrace
4189Do not reset
4190.Fl o Ic xtrace
4191upon entering functions.
4192This is enabled by default.
4193.It Fl o Ic nohup
4194Do not kill running jobs with a
4195.Dv SIGHUP
4196signal when a login shell exits.
4197Currently set by default, but this may
4198change in the future to be compatible with
4199.At
4200.Nm ksh ,
4201which
4202doesn't have this option, but does send the
4203.Dv SIGHUP
4204signal.
4205.It Fl o Ic nolog
4206No effect.
4207In the original Korn shell, this prevents function definitions from
4208being stored in the history file.
4209.It Fl o Ic physical
4210Causes the
4211.Ic cd
4212and
4213.Ic pwd
4214commands to use
4215.Dq physical
4216(i.e. the filesystem's)
4217.Sq ..
4218directories instead of
4219.Dq logical
4220directories (i.e. the shell handles
4221.Sq .. ,
4222which allows the user to be oblivious of symbolic links to directories).
4223Clear by default.
4224Note that setting this option does not affect the current value of the
4225.Ev PWD
4226parameter; only the
4227.Ic cd
4228command changes
4229.Ev PWD .
4230See the
4231.Ic cd
4232and
4233.Ic pwd
4234commands above for more details.
4235.It Fl o Ic pipefail
4236Make the exit status of a pipeline (before logically complementing) the
4237rightmost non-zero errorlevel, or zero if all commands exited with zero.
4238.It Fl o Ic posix
4239Enable a somewhat more
4240.Px
4241ish mode.
4242As a side effect, setting this flag turns off
4243.Ic braceexpand
4244mode, which can be turned back on manually, and
4245.Ic sh
4246mode.
4247.It Fl o Ic sh
4248Enable
4249.Pa /bin/sh
4250.Pq kludge
4251mode.
4252Automatically enabled if the basename of the shell invocation begins with
4253.Dq sh
4254and this autodetection feature is compiled in
4255.Pq not in MirBSD .
4256As a side effect, setting this flag turns off
4257.Ic braceexpand
4258mode, which can be turned back on manually, and
4259.Ic posix
4260mode.
4261.It Fl o Ic vi
4262Enable
4263.Xr vi 1 Ns -like
4264command-line editing (interactive shells only).
4265See
4266.Sx Vi editing mode
4267for documentation and limitations.
4268.It Fl o Ic vi\-esccomplete
4269In vi command-line editing, do command and file name completion when escape
4270(\*(ha[) is entered in command mode.
4271.It Fl o Ic vi\-tabcomplete
4272In vi command-line editing, do command and file name completion when tab (\*(haI)
4273is entered in insert mode.
4274This is the default.
4275.It Fl o Ic viraw
4276No effect.
4277In the original Korn shell, unless
4278.Ic viraw
4279was set, the vi command-line mode would let the
4280.Xr tty 4
4281driver do the work until ESC (\*(ha[) was entered.
4282.Nm
4283is always in viraw mode.
4284.El
4285.Pp
4286These options can also be used upon invocation of the shell.
4287The current set of
4288options (with single letter names) can be found in the parameter
4289.Sq $\- .
4290.Ic set Fl o
4291with no option name will list all the options and whether each is on or off;
4292.Ic set +o
4293will print the long names of all options that are currently on.
4294.Pp
4295Remaining arguments, if any, are positional parameters and are assigned, in
4296order, to the positional parameters (i.e. $1, $2, etc.).
4297If options end with
4298.Ql \-\-
4299and there are no remaining arguments, all positional parameters are cleared.
4300If no options or arguments are given, the values of all names are printed.
4301For unknown historical reasons, a lone
4302.Ql \-
4303option is treated specially \*(en it clears both the
4304.Fl v
4305and
4306.Fl x
4307options.
4308.Pp
4309.It Ic shift Op Ar number
4310The positional parameters
4311.Ar number Ns +1 ,
4312.Ar number Ns +2 ,
4313etc. are renamed to
4314.Sq 1 ,
4315.Sq 2 ,
4316etc.
4317.Ar number
4318defaults to 1.
4319.Pp
4320.It Ic sleep Ar seconds
4321Suspends execution for a minimum of the
4322.Ar seconds
4323specified as positive decimal value with an optional fractional part.
4324Signal delivery may continue execution earlier.
4325.Pp
4326.It Ic source Ar file Op Ar arg ...
4327Like
4328.Ic \&. Po Do dot Dc Pc ,
4329except that the current working directory is appended to the
4330.Ev PATH
4331in GNU
4332.Nm bash
4333and
4334.Nm mksh .
4335In
4336.Nm ksh93
4337and
4338.Nm mksh ,
4339this is implemented as a shell alias instead of a builtin.
4340.Pp
4341.It Ic suspend
4342Stops the shell as if it had received the suspend character from
4343the terminal.
4344It is not possible to suspend a login shell unless the parent process
4345is a member of the same terminal session but is a member of a different
4346process group.
4347As a general rule, if the shell was started by another shell or via
4348.Xr su 1 ,
4349it can be suspended.
4350.Pp
4351.It Ic test Ar expression
4352.It Ic \&[ Ar expression Ic \&]
4353.Ic test
4354evaluates the
4355.Ar expression
4356and returns zero status if true, 1 if false, or greater than 1 if there
4357was an error.
4358It is normally used as the condition command of
4359.Ic if
4360and
4361.Ic while
4362statements.
4363Symbolic links are followed for all
4364.Ar file
4365expressions except
4366.Fl h
4367and
4368.Fl L .
4369.Pp
4370The following basic expressions are available:
4371.Bl -tag -width 17n
4372.It Fl a Ar file
4373.Ar file
4374exists.
4375.It Fl b Ar file
4376.Ar file
4377is a block special device.
4378.It Fl c Ar file
4379.Ar file
4380is a character special device.
4381.It Fl d Ar file
4382.Ar file
4383is a directory.
4384.It Fl e Ar file
4385.Ar file
4386exists.
4387.It Fl f Ar file
4388.Ar file
4389is a regular file.
4390.It Fl G Ar file
4391.Ar file Ns 's
4392group is the shell's effective group ID.
4393.It Fl g Ar file
4394.Ar file Ns 's
4395mode has the setgid bit set.
4396.It Fl H Ar file
4397.Ar file
4398is a context dependent directory (only useful on HP-UX).
4399.It Fl h Ar file
4400.Ar file
4401is a symbolic link.
4402.It Fl k Ar file
4403.Ar file Ns 's
4404mode has the
4405.Xr sticky 8
4406bit set.
4407.It Fl L Ar file
4408.Ar file
4409is a symbolic link.
4410.It Fl O Ar file
4411.Ar file Ns 's
4412owner is the shell's effective user ID.
4413.It Fl o Ar option
4414Shell
4415.Ar option
4416is set (see the
4417.Ic set
4418command above for a list of options).
4419As a non-standard extension, if the option starts with a
4420.Ql \&! ,
4421the test is negated; the test always fails if
4422.Ar option
4423doesn't exist (so [ \-o foo \-o \-o !foo ] returns true if and only if option
4424.Ar foo
4425exists).
4426The same can be achieved with [ \-o ?foo ] like in
4427.At
4428.Nm ksh93 .
4429.Ar option
4430can also be the short flag led by either
4431.Ql \-
4432or
4433.Ql +
4434.Pq no logical negation ,
4435for example
4436.Ql \-x
4437or
4438.Ql +x
4439instead of
4440.Ql xtrace .
4441.It Fl p Ar file
4442.Ar file
4443is a named pipe
4444.Pq Tn FIFO .
4445.It Fl r Ar file
4446.Ar file
4447exists and is readable.
4448.It Fl S Ar file
4449.Ar file
4450is a
4451.Xr unix 4 Ns -domain
4452socket.
4453.It Fl s Ar file
4454.Ar file
4455is not empty.
4456.It Fl t Ar fd
4457File descriptor
4458.Ar fd
4459is a
4460.Xr tty 4
4461device.
4462.It Fl u Ar file
4463.Ar file Ns 's
4464mode has the setuid bit set.
4465.It Fl w Ar file
4466.Ar file
4467exists and is writable.
4468.It Fl x Ar file
4469.Ar file
4470exists and is executable.
4471.It Ar file1 Fl nt Ar file2
4472.Ar file1
4473is newer than
4474.Ar file2
4475or
4476.Ar file1
4477exists and
4478.Ar file2
4479does not.
4480.It Ar file1 Fl ot Ar file2
4481.Ar file1
4482is older than
4483.Ar file2
4484or
4485.Ar file2
4486exists and
4487.Ar file1
4488does not.
4489.It Ar file1 Fl ef Ar file2
4490.Ar file1
4491is the same file as
4492.Ar file2 .
4493.It Ar string
4494.Ar string
4495has non-zero length.
4496.It Fl n Ar string
4497.Ar string
4498is not empty.
4499.It Fl z Ar string
4500.Ar string
4501is empty.
4502.It Ar string No = Ar string
4503Strings are equal.
4504.It Ar string No == Ar string
4505Strings are equal.
4506.It Ar string No \*(Gt Ar string
4507First string operand is greater than second string operand.
4508.It Ar string No \*(Lt Ar string
4509First string operand is less than second string operand.
4510.It Ar string No != Ar string
4511Strings are not equal.
4512.It Ar number Fl eq Ar number
4513Numbers compare equal.
4514.It Ar number Fl ne Ar number
4515Numbers compare not equal.
4516.It Ar number Fl ge Ar number
4517Numbers compare greater than or equal.
4518.It Ar number Fl gt Ar number
4519Numbers compare greater than.
4520.It Ar number Fl le Ar number
4521Numbers compare less than or equal.
4522.It Ar number Fl \&lt Ar number
4523Numbers compare less than.
4524.El
4525.Pp
4526The above basic expressions, in which unary operators have precedence over
4527binary operators, may be combined with the following operators (listed in
4528increasing order of precedence):
4529.Bd -literal -offset indent
4530expr \-o expr		Logical OR.
4531expr \-a expr		Logical AND.
4532! expr			Logical NOT.
4533( expr )		Grouping.
4534.Ed
4535.Pp
4536Note that a number actually may be an arithmetic expression, such as
4537a mathematical term or the name of an integer variable:
4538.Bd -literal -offset indent
4539x=1; [ "x" \-eq 1 ]	evaluates to true
4540.Ed
4541.Pp
4542Note that some special rules are applied (courtesy of
4543.Px
4544) if the number of arguments to
4545.Ic test
4546or inside the brackets
4547.Ic \&[ ... \&]
4548is less than five: if leading
4549.Ql \&!
4550arguments can be stripped such that only one to three arguments remain,
4551then the lowered comparison is executed; (thanks to XSI) parentheses
4552.Ic \e( ... \e)
4553lower four- and three-argument forms to two- and one-argument forms,
4554respectively; three-argument forms ultimately prefer binary operations,
4555followed by negation and parenthesis lowering; two- and four-argument forms
4556prefer negation followed by parenthesis; the one-argument form always implies
4557.Fl n .
4558.Pp
4559.Sy Note :
4560A common mistake is to use
4561.Dq if \&[ $foo = bar \&]
4562which fails if parameter
4563.Dq foo
4564is
4565.Dv NULL
4566or unset, if it has embedded spaces (i.e.\&
4567.Ev IFS
4568octets), or if it is a unary operator like
4569.Sq \&!
4570or
4571.Sq Fl n .
4572Use tests like
4573.Dq if \&[ x\&"$foo\&" = x"bar" \&]
4574instead, or the double-bracket operator
4575.Dq if \&[[ $foo = bar \&]]
4576or, to avoid pattern matching (see
4577.Ic \&[[
4578above):
4579.Dq if \&[[ $foo = \&"$bar" \&]]
4580.Pp
4581The
4582.Ic \&[[ ... ]]
4583construct is not only more secure to use but also often faster.
4584.Pp
4585.It Xo
4586.Ic time
4587.Op Fl p
4588.Op Ar pipeline
4589.Xc
4590If a
4591.Ar pipeline
4592is given, the times used to execute the pipeline are reported.
4593If no pipeline
4594is given, then the user and system time used by the shell itself, and all the
4595commands it has run since it was started, are reported.
4596The times reported are the real time (elapsed time from start to finish),
4597the user CPU time (time spent running in user mode), and the system CPU time
4598(time spent running in kernel mode).
4599Times are reported to standard error; the format of the output is:
4600.Pp
4601.Dl "0m0.00s real     0m0.00s user     0m0.00s system"
4602.Pp
4603If the
4604.Fl p
4605option is given the output is slightly longer:
4606.Bd -literal -offset indent
4607real     0.00
4608user     0.00
4609sys      0.00
4610.Ed
4611.Pp
4612It is an error to specify the
4613.Fl p
4614option unless
4615.Ar pipeline
4616is a simple command.
4617.Pp
4618Simple redirections of standard error do not affect the output of the
4619.Ic time
4620command:
4621.Pp
4622.Dl $ time sleep 1 2\*(Gtafile
4623.Dl $ { time sleep 1; } 2\*(Gtafile
4624.Pp
4625Times for the first command do not go to
4626.Dq afile ,
4627but those of the second command do.
4628.Pp
4629.It Ic times
4630Print the accumulated user and system times used both by the shell
4631and by processes that the shell started which have exited.
4632The format of the output is:
4633.Bd -literal -offset indent
46340m0.00s 0m0.00s
46350m0.00s 0m0.00s
4636.Ed
4637.Pp
4638.It Ic trap Op Ar handler signal ...
4639Sets a trap handler that is to be executed when any of the specified signals are
4640received.
4641.Ar handler
4642is either a
4643.Dv NULL
4644string, indicating the signals are to be ignored, a minus sign
4645.Pq Sq \- ,
4646indicating that the default action is to be taken for the signals (see
4647.Xr signal 3 ) ,
4648or a string containing shell commands to be evaluated and executed at the first
4649opportunity (i.e. when the current command completes, or before printing the
4650next
4651.Ev PS1
4652prompt) after receipt of one of the signals.
4653.Ar signal
4654is the name of a signal (e.g.\&
4655.Dv PIPE
4656or
4657.Dv ALRM )
4658or the number of the signal (see the
4659.Ic kill \-l
4660command above).
4661.Pp
4662There are two special signals:
4663.Dv EXIT
4664(also known as 0) which is executed when the shell is about to exit, and
4665.Dv ERR ,
4666which is executed after an error occurs (an error is something that would cause
4667the shell to exit if the
4668.Fl e
4669or
4670.Ic errexit
4671option were set \*(en see the
4672.Ic set
4673command above).
4674.Dv EXIT
4675handlers are executed in the environment of the last executed command.
4676Note
4677that for non-interactive shells, the trap handler cannot be changed for signals
4678that were ignored when the shell started.
4679.Pp
4680With no arguments,
4681.Ic trap
4682lists, as a series of
4683.Ic trap
4684commands, the current state of the traps that have been set since the shell
4685started.
4686Note that the output of
4687.Ic trap
4688cannot be usefully piped to another process (an artifact of the fact that
4689traps are cleared when subprocesses are created).
4690.Pp
4691The original Korn shell's
4692.Dv DEBUG
4693trap and the handling of
4694.Dv ERR
4695and
4696.Dv EXIT
4697traps in functions are not yet implemented.
4698.Pp
4699.It Ic true
4700A command that exits with a zero value.
4701.Pp
4702.It Xo
4703.Ic global
4704.Oo Op Ic +\-alpnrtUux
4705.Op Fl L Ns Op Ar n
4706.Op Fl R Ns Op Ar n
4707.Op Fl Z Ns Op Ar n
4708.Op Fl i Ns Op Ar n
4709.No \*(Ba Fl f Op Fl tux Oc
4710.Oo Ar name
4711.Op Ns = Ns Ar value
4712.Ar ... Oc
4713.Xc
4714.It Xo
4715.Ic typeset
4716.Oo Op Ic +\-alpnrtUux
4717.Op Fl LRZ Ns Op Ar n
4718.Op Fl i Ns Op Ar n
4719.No \*(Ba Fl f Op Fl tux Oc
4720.Oo Ar name
4721.Op Ns = Ns Ar value
4722.Ar ... Oc
4723.Xc
4724Display or set parameter attributes.
4725With no
4726.Ar name
4727arguments, parameter attributes are displayed; if no options are used, the
4728current attributes of all parameters are printed as
4729.Ic typeset
4730commands; if an option is given (or
4731.Ql \-
4732with no option letter), all parameters and their values with the specified
4733attributes are printed; if options are introduced with
4734.Ql + ,
4735parameter values are not printed.
4736.Pp
4737If
4738.Ar name
4739arguments are given, the attributes of the named parameters are set
4740.Pq Ic \-
4741or cleared
4742.Pq Ic + .
4743Values for parameters may optionally be specified.
4744For
4745.Ar name Ns \&[*] ,
4746the change affects the entire array, and no value may be specified.
4747.Pp
4748If
4749.Ic typeset
4750is used inside a function, any parameters specified are localised.
4751This is not done by the otherwise identical
4752.Ic global .
4753.Em Note :
4754This means that
4755.Nm No 's Ic global
4756command is
4757.Em not
4758equivalent to other programming languages' as it does not allow a
4759function called from another function to access a parameter at truly
4760global scope, but only prevents putting an accessed one into local scope.
4761.Pp
4762When
4763.Fl f
4764is used,
4765.Ic typeset
4766operates on the attributes of functions.
4767As with parameters, if no
4768.Ar name
4769arguments are given,
4770functions are listed with their values (i.e. definitions) unless
4771options are introduced with
4772.Ql + ,
4773in which case only the function names are reported.
4774.Bl -tag -width Ds
4775.It Fl a
4776Indexed array attribute.
4777.It Fl f
4778Function mode.
4779Display or set functions and their attributes, instead of parameters.
4780.It Fl i Ns Op Ar n
4781Integer attribute.
4782.Ar n
4783specifies the base to use when displaying the integer (if not specified, the
4784base given in the first assignment is used).
4785Parameters with this attribute may
4786be assigned values containing arithmetic expressions.
4787.It Fl L Ns Op Ar n
4788Left justify attribute.
4789.Ar n
4790specifies the field width.
4791If
4792.Ar n
4793is not specified, the current width of a parameter (or the width of its first
4794assigned value) is used.
4795Leading whitespace (and zeros, if used with the
4796.Fl Z
4797option) is stripped.
4798If necessary, values are either truncated or space padded
4799to fit the field width.
4800.It Fl l
4801Lower case attribute.
4802All upper case characters in values are converted to lower case.
4803(In the original Korn shell, this parameter meant
4804.Dq long integer
4805when used with the
4806.Fl i
4807option.)
4808.It Fl n
4809Create a bound variable (name reference): any access to the variable
4810.Ar name
4811will access the variable
4812.Ar value
4813in the current scope (this is different from
4814.At
4815.Nm ksh93 ! )
4816instead.
4817Also different from
4818.At
4819.Nm ksh93
4820is that
4821.Ar value
4822is lazily evaluated at the time
4823.Ar name
4824is accessed.
4825This can be used by functions to access variables whose names are
4826passed as parametres, instead of using
4827.Ic eval .
4828.It Fl p
4829Print complete
4830.Ic typeset
4831commands that can be used to re-create the attributes and values of
4832parameters.
4833.It Fl R Ns Op Ar n
4834Right justify attribute.
4835.Ar n
4836specifies the field width.
4837If
4838.Ar n
4839is not specified, the current width of a parameter (or the width of its first
4840assigned value) is used.
4841Trailing whitespace is stripped.
4842If necessary, values are either stripped of leading characters or space
4843padded to make them fit the field width.
4844.It Fl r
4845Read-only attribute.
4846Parameters with this attribute may not be assigned to or unset.
4847Once this attribute is set, it cannot be turned off.
4848.It Fl t
4849Tag attribute.
4850Has no meaning to the shell; provided for application use.
4851.Pp
4852For functions,
4853.Fl t
4854is the trace attribute.
4855When functions with the trace attribute are executed, the
4856.Ic xtrace
4857.Pq Fl x
4858shell option is temporarily turned on.
4859.It Fl U
4860Unsigned integer attribute.
4861Integers are printed as unsigned values (combine with the
4862.Fl i
4863option).
4864This option is not in the original Korn shell.
4865.It Fl u
4866Upper case attribute.
4867All lower case characters in values are converted to upper case.
4868(In the original Korn shell, this parameter meant
4869.Dq unsigned integer
4870when used with the
4871.Fl i
4872option which meant upper case letters would never be used for bases greater
4873than 10.
4874See the
4875.Fl U
4876option.)
4877.Pp
4878For functions,
4879.Fl u
4880is the undefined attribute.
4881See
4882.Sx Functions
4883above for the implications of this.
4884.It Fl x
4885Export attribute.
4886Parameters (or functions) are placed in the environment of
4887any executed commands.
4888Exported functions are not yet implemented.
4889.It Fl Z Ns Op Ar n
4890Zero fill attribute.
4891If not combined with
4892.Fl L ,
4893this is the same as
4894.Fl R ,
4895except zero padding is used instead of space padding.
4896For integers, the number instead of the base is padded.
4897.El
4898.Pp
4899If any of the
4900.\" long integer ,
4901.Fl i ,
4902.Fl L ,
4903.Fl l ,
4904.Fl R ,
4905.Fl U ,
4906.Fl u ,
4907or
4908.Fl Z
4909options are changed, all others from this set are cleared,
4910unless they are also given on the same command line.
4911.Pp
4912.It Xo
4913.Ic ulimit
4914.Op Fl aBCcdefHilMmnOPpqrSsTtVvw
4915.Op Ar value
4916.Xc
4917Display or set process limits.
4918If no options are used, the file size limit
4919.Pq Fl f
4920is assumed.
4921.Ar value ,
4922if specified, may be either an arithmetic expression or the word
4923.Dq unlimited .
4924The limits affect the shell and any processes created by the shell after a
4925limit is imposed.
4926Note that some systems may not allow limits to be increased
4927once they are set.
4928Also note that the types of limits available are system
4929dependent \*(en some systems have only the
4930.Fl f
4931limit.
4932.Bl -tag -width 5n
4933.It Fl a
4934Display all limits; unless
4935.Fl H
4936is used, soft limits are displayed.
4937.It Fl B Ar n
4938Set the socket buffer size to
4939.Ar n
4940kibibytes.
4941.It Fl C Ar n
4942Set the number of cached threads to
4943.Ar n .
4944.It Fl c Ar n
4945Impose a size limit of
4946.Ar n
4947blocks on the size of core dumps.
4948.It Fl d Ar n
4949Impose a size limit of
4950.Ar n
4951kibibytes on the size of the data area.
4952.It Fl e Ar n
4953Set the maximum niceness to
4954.Ar n .
4955.It Fl f Ar n
4956Impose a size limit of
4957.Ar n
4958blocks on files written by the shell and its child processes (files of any
4959size may be read).
4960.It Fl H
4961Set the hard limit only (the default is to set both hard and soft limits).
4962.It Fl i Ar n
4963Set the number of pending signals to
4964.Ar n .
4965.It Fl l Ar n
4966Impose a limit of
4967.Ar n
4968kibibytes on the amount of locked (wired) physical memory.
4969.It Fl M Ar n
4970Set the AIO locked memory to
4971.Ar n
4972kibibytes.
4973.It Fl m Ar n
4974Impose a limit of
4975.Ar n
4976kibibytes on the amount of physical memory used.
4977.It Fl n Ar n
4978Impose a limit of
4979.Ar n
4980file descriptors that can be open at once.
4981.It Fl O Ar n
4982Set the number of AIO operations to
4983.Ar n .
4984.It Fl P Ar n
4985Limit the number of threads per process to
4986.Ar n .
4987.It Fl p Ar n
4988Impose a limit of
4989.Ar n
4990processes that can be run by the user at any one time.
4991.It Fl q Ar n
4992Limit the size of
4993.Tn POSIX
4994message queues to
4995.Ar n
4996bytes.
4997.It Fl r Ar n
4998Set the maximum real-time priority to
4999.Ar n .
5000.It Fl S
5001Set the soft limit only (the default is to set both hard and soft limits).
5002.It Fl s Ar n
5003Impose a size limit of
5004.Ar n
5005kibibytes on the size of the stack area.
5006.It Fl T Ar n
5007Impose a time limit of
5008.Ar n
5009real seconds to be used by each process.
5010.It Fl t Ar n
5011Impose a time limit of
5012.Ar n
5013CPU seconds spent in user mode to be used by each process.
5014.It Fl V Ar n
5015Set the number of vnode monitors on Haiku to
5016.Ar n .
5017.It Fl v Ar n
5018Impose a limit of
5019.Ar n
5020kibibytes on the amount of virtual memory (address space) used.
5021.It Fl w Ar n
5022Impose a limit of
5023.Ar n
5024kibibytes on the amount of swap space used.
5025.El
5026.Pp
5027As far as
5028.Ic ulimit
5029is concerned, a block is 512 bytes.
5030.Pp
5031.It Xo
5032.Ic umask
5033.Op Fl S
5034.Op Ar mask
5035.Xc
5036Display or set the file permission creation mask, or umask (see
5037.Xr umask 2 ) .
5038If the
5039.Fl S
5040option is used, the mask displayed or set is symbolic; otherwise, it is an
5041octal number.
5042.Pp
5043Symbolic masks are like those used by
5044.Xr chmod 1 .
5045When used, they describe what permissions may be made available (as opposed to
5046octal masks in which a set bit means the corresponding bit is to be cleared).
5047For example,
5048.Dq ug=rwx,o=
5049sets the mask so files will not be readable, writable, or executable by
5050.Dq others ,
5051and is equivalent (on most systems) to the octal mask
5052.Dq 007 .
5053.Pp
5054.It Xo
5055.Ic unalias
5056.Op Fl adt
5057.Op Ar name ...
5058.Xc
5059The aliases for the given names are removed.
5060If the
5061.Fl a
5062option is used, all aliases are removed.
5063If the
5064.Fl t
5065or
5066.Fl d
5067options are used, the indicated operations are carried out on tracked or
5068directory aliases, respectively.
5069.Pp
5070.It Xo
5071.Ic unset
5072.Op Fl fv
5073.Ar parameter ...
5074.Xc
5075Unset the named parameters
5076.Po
5077.Fl v ,
5078the default
5079.Pc
5080or functions
5081.Pq Fl f .
5082With
5083.Ar parameter Ns \&[*] ,
5084attributes are kept, only values are unset.
5085.Pp
5086The exit status is non-zero if any of the parameters have the read-only
5087attribute set, zero otherwise.
5088.Pp
5089.It Ic wait Op Ar job ...
5090Wait for the specified job(s) to finish.
5091The exit status of
5092.Ic wait
5093is that of the last specified job; if the last job is killed by a signal, the
5094exit status is 128 + the number of the signal (see
5095.Ic kill \-l Ar exit-status
5096above); if the last specified job can't be found (because it never existed, or
5097had already finished), the exit status of
5098.Ic wait
5099is 127.
5100See
5101.Sx Job control
5102below for the format of
5103.Ar job .
5104.Ic wait
5105will return if a signal for which a trap has been set is received, or if a
5106.Dv SIGHUP ,
5107.Dv SIGINT ,
5108or
5109.Dv SIGQUIT
5110signal is received.
5111.Pp
5112If no jobs are specified,
5113.Ic wait
5114waits for all currently running jobs (if any) to finish and exits with a zero
5115status.
5116If job monitoring is enabled, the completion status of jobs is printed
5117(this is not the case when jobs are explicitly specified).
5118.Pp
5119.It Xo
5120.Ic whence
5121.Op Fl pv
5122.Op Ar name ...
5123.Xc
5124For each
5125.Ar name ,
5126the type of command is listed (reserved word, built-in, alias,
5127function, tracked alias, or executable).
5128If the
5129.Fl p
5130option is used, a path search is performed even if
5131.Ar name
5132is a reserved word, alias, etc.
5133Without the
5134.Fl v
5135option,
5136.Ic whence
5137is similar to
5138.Ic command Fl v
5139except that
5140.Ic whence
5141will find reserved words and won't print aliases as alias commands.
5142With the
5143.Fl v
5144option,
5145.Ic whence
5146is the same as
5147.Ic command Fl V .
5148Note that for
5149.Ic whence ,
5150the
5151.Fl p
5152option does not affect the search path used, as it does for
5153.Ic command .
5154If the type of one or more of the names could not be determined, the exit
5155status is non-zero.
5156.El
5157.Ss Job control
5158Job control refers to the shell's ability to monitor and control jobs which
5159are processes or groups of processes created for commands or pipelines.
5160At a minimum, the shell keeps track of the status of the background (i.e.\&
5161asynchronous) jobs that currently exist; this information can be displayed
5162using the
5163.Ic jobs
5164commands.
5165If job control is fully enabled (using
5166.Ic set \-m
5167or
5168.Ic set \-o monitor ) ,
5169as it is for interactive shells, the processes of a job are placed in their
5170own process group.
5171Foreground jobs can be stopped by typing the suspend
5172character from the terminal (normally \*(haZ), jobs can be restarted in either the
5173foreground or background using the
5174.Ic fg
5175and
5176.Ic bg
5177commands, and the state of the terminal is saved or restored when a foreground
5178job is stopped or restarted, respectively.
5179.Pp
5180Note that only commands that create processes (e.g. asynchronous commands,
5181subshell commands, and non-built-in, non-function commands) can be stopped;
5182commands like
5183.Ic read
5184cannot be.
5185.Pp
5186When a job is created, it is assigned a job number.
5187For interactive shells, this number is printed inside
5188.Dq \&[..] ,
5189followed by the process IDs of the processes in the job when an asynchronous
5190command is run.
5191A job may be referred to in the
5192.Ic bg ,
5193.Ic fg ,
5194.Ic jobs ,
5195.Ic kill ,
5196and
5197.Ic wait
5198commands either by the process ID of the last process in the command pipeline
5199(as stored in the
5200.Ic $!\&
5201parameter) or by prefixing the job number with a percent
5202sign
5203.Pq Sq % .
5204Other percent sequences can also be used to refer to jobs:
5205.Bl -tag -width "%+ x %% x %XX"
5206.It %+ \*(Ba %% \*(Ba %
5207The most recently stopped job, or, if there are no stopped jobs, the oldest
5208running job.
5209.It %\-
5210The job that would be the
5211.Ic %+
5212job if the latter did not exist.
5213.It % Ns Ar n
5214The job with job number
5215.Ar n .
5216.It %? Ns Ar string
5217The job with its command containing the string
5218.Ar string
5219(an error occurs if multiple jobs are matched).
5220.It % Ns Ar string
5221The job with its command starting with the string
5222.Ar string
5223(an error occurs if multiple jobs are matched).
5224.El
5225.Pp
5226When a job changes state (e.g. a background job finishes or foreground job is
5227stopped), the shell prints the following status information:
5228.Pp
5229.D1 [ Ns Ar number ] Ar flag status command
5230.Pp
5231where...
5232.Bl -tag -width "command"
5233.It Ar number
5234is the job number of the job;
5235.It Ar flag
5236is the
5237.Ql +
5238or
5239.Ql \-
5240character if the job is the
5241.Ic %+
5242or
5243.Ic %\-
5244job, respectively, or space if it is neither;
5245.It Ar status
5246indicates the current state of the job and can be:
5247.Bl -tag -width "RunningXX"
5248.It Done Op Ar number
5249The job exited.
5250.Ar number
5251is the exit status of the job which is omitted if the status is zero.
5252.It Running
5253The job has neither stopped nor exited (note that running does not necessarily
5254mean consuming CPU time \*(en
5255the process could be blocked waiting for some event).
5256.It Stopped Op Ar signal
5257The job was stopped by the indicated
5258.Ar signal
5259(if no signal is given, the job was stopped by
5260.Dv SIGTSTP ) .
5261.It Ar signal-description Op Dq core dumped
5262The job was killed by a signal (e.g. memory fault, hangup); use
5263.Ic kill \-l
5264for a list of signal descriptions.
5265The
5266.Dq core dumped
5267message indicates the process created a core file.
5268.El
5269.It Ar command
5270is the command that created the process.
5271If there are multiple processes in
5272the job, each process will have a line showing its
5273.Ar command
5274and possibly its
5275.Ar status ,
5276if it is different from the status of the previous process.
5277.El
5278.Pp
5279When an attempt is made to exit the shell while there are jobs in the stopped
5280state, the shell warns the user that there are stopped jobs and does not exit.
5281If another attempt is immediately made to exit the shell, the stopped jobs are
5282sent a
5283.Dv SIGHUP
5284signal and the shell exits.
5285Similarly, if the
5286.Ic nohup
5287option is not set and there are running jobs when an attempt is made to exit
5288a login shell, the shell warns the user and does not exit.
5289If another attempt
5290is immediately made to exit the shell, the running jobs are sent a
5291.Dv SIGHUP
5292signal and the shell exits.
5293.Ss Interactive input line editing
5294The shell supports three modes of reading command lines from a
5295.Xr tty 4
5296in an interactive session, controlled by the
5297.Ic emacs ,
5298.Ic gmacs ,
5299and
5300.Ic vi
5301options (at most one of these can be set at once).
5302The default is
5303.Ic emacs .
5304Editing modes can be set explicitly using the
5305.Ic set
5306built-in.
5307If none of these options are enabled,
5308the shell simply reads lines using the normal
5309.Xr tty 4
5310driver.
5311If the
5312.Ic emacs
5313or
5314.Ic gmacs
5315option is set, the shell allows emacs-like editing of the command; similarly,
5316if the
5317.Ic vi
5318option is set, the shell allows vi-like editing of the command.
5319These modes are described in detail in the following sections.
5320.Pp
5321In these editing modes, if a line is longer than the screen width (see the
5322.Ev COLUMNS
5323parameter),
5324a
5325.Ql \*(Gt ,
5326.Ql + ,
5327or
5328.Ql \*(Lt
5329character is displayed in the last column indicating that there are more
5330characters after, before and after, or before the current position,
5331respectively.
5332The line is scrolled horizontally as necessary.
5333.Pp
5334Completed lines are pushed into the history, unless they begin with an
5335IFS octet or IFS white space, or are the same as the previous line.
5336.Ss Emacs editing mode
5337When the
5338.Ic emacs
5339option is set, interactive input line editing is enabled.
5340Warning: This mode is
5341slightly different from the emacs mode in the original Korn shell.
5342In this mode, various editing commands
5343(typically bound to one or more control characters) cause immediate actions
5344without waiting for a newline.
5345Several editing commands are bound to particular
5346control characters when the shell is invoked; these bindings can be changed
5347using the
5348.Ic bind
5349command.
5350.Pp
5351The following is a list of available editing commands.
5352Each description starts with the name of the command,
5353suffixed with a colon;
5354an
5355.Op Ar n
5356(if the command can be prefixed with a count); and any keys the command is
5357bound to by default, written using caret notation
5358e.g. the ASCII ESC character is written as \*(ha[.
5359These control sequences are not case sensitive.
5360A count prefix for a command is entered using the sequence
5361.Pf \*(ha[ Ns Ar n ,
5362where
5363.Ar n
5364is a sequence of 1 or more digits.
5365Unless otherwise specified, if a count is
5366omitted, it defaults to 1.
5367.Pp
5368Note that editing command names are used only with the
5369.Ic bind
5370command.
5371Furthermore, many editing commands are useful only on terminals with
5372a visible cursor.
5373The default bindings were chosen to resemble corresponding
5374Emacs key bindings.
5375The user's
5376.Xr tty 4
5377characters (e.g.\&
5378.Dv ERASE )
5379are bound to
5380reasonable substitutes and override the default bindings.
5381.Bl -tag -width Ds
5382.It abort: \*(haC, \*(haG
5383Abort the current command, empty the line buffer and
5384set the exit state to interrupted.
5385.It auto\-insert: Op Ar n
5386Simply causes the character to appear as literal input.
5387Most ordinary characters are bound to this.
5388.It Xo backward\-char:
5389.Op Ar n
5390.No \*(haB , \*(haXD , ANSI-CurLeft
5391.Xc
5392Moves the cursor backward
5393.Ar n
5394characters.
5395.It Xo backward\-word:
5396.Op Ar n
5397.No \*(ha[b , ANSI-Ctrl-CurLeft , ANSI-Alt-CurLeft
5398.Xc
5399Moves the cursor backward to the beginning of the word; words consist of
5400alphanumerics, underscore
5401.Pq Sq _ ,
5402and dollar sign
5403.Pq Sq $
5404characters.
5405.It beginning\-of\-history: \*(ha[\*(Lt
5406Moves to the beginning of the history.
5407.It beginning\-of\-line: \*(haA, ANSI-Home
5408Moves the cursor to the beginning of the edited input line.
5409.It Xo capitalise\-word:
5410.Op Ar n
5411.No \*(ha[C , \*(ha[c
5412.Xc
5413Uppercase the first character in the next
5414.Ar n
5415words, leaving the cursor past the end of the last word.
5416.It clear\-screen: \*(ha[\*(haL
5417Prints a compile-time configurable sequence to clear the screen and home
5418the cursor, redraws the entire prompt and the currently edited input line.
5419The default sequence works for almost all standard terminals.
5420.It comment: \*(ha[#
5421If the current line does not begin with a comment character, one is added at
5422the beginning of the line and the line is entered (as if return had been
5423pressed); otherwise, the existing comment characters are removed and the cursor
5424is placed at the beginning of the line.
5425.It complete: \*(ha[\*(ha[
5426Automatically completes as much as is unique of the command name or the file
5427name containing the cursor.
5428If the entire remaining command or file name is
5429unique, a space is printed after its completion, unless it is a directory name
5430in which case
5431.Ql /
5432is appended.
5433If there is no command or file name with the current partial word
5434as its prefix, a bell character is output (usually causing a beep to be
5435sounded).
5436.It complete\-command: \*(haX\*(ha[
5437Automatically completes as much as is unique of the command name having the
5438partial word up to the cursor as its prefix, as in the
5439.Ic complete
5440command above.
5441.It complete\-file: \*(ha[\*(haX
5442Automatically completes as much as is unique of the file name having the
5443partial word up to the cursor as its prefix, as in the
5444.Ic complete
5445command described above.
5446.It complete\-list: \*(haI, \*(ha[=
5447Complete as much as is possible of the current word,
5448and list the possible completions for it.
5449If only one completion is possible,
5450match as in the
5451.Ic complete
5452command above.
5453Note that \*(haI is usually generated by the TAB (tabulator) key.
5454.It Xo delete\-char\-backward:
5455.Op Ar n
5456.No ERASE , \*(ha? , \*(haH
5457.Xc
5458Deletes
5459.Ar n
5460characters before the cursor.
5461.It Xo delete\-char\-forward:
5462.Op Ar n
5463.No ANSI-Del
5464.Xc
5465Deletes
5466.Ar n
5467characters after the cursor.
5468.It Xo delete\-word\-backward:
5469.Op Ar n
5470.No WERASE , \*(ha[\*(ha? , \*(ha[\*(haH , \*(ha[h
5471.Xc
5472Deletes
5473.Ar n
5474words before the cursor.
5475.It Xo delete\-word\-forward:
5476.Op Ar n
5477.No \*(ha[d
5478.Xc
5479Deletes characters after the cursor up to the end of
5480.Ar n
5481words.
5482.It Xo down\-history:
5483.Op Ar n
5484.No \*(haN , \*(haXB , ANSI-CurDown
5485.Xc
5486Scrolls the history buffer forward
5487.Ar n
5488lines (later).
5489Each input line originally starts just after the last entry
5490in the history buffer, so
5491.Ic down\-history
5492is not useful until either
5493.Ic search\-history ,
5494.Ic search\-history\-up
5495or
5496.Ic up\-history
5497has been performed.
5498.It Xo downcase\-word:
5499.Op Ar n
5500.No \*(ha[L , \*(ha[l
5501.Xc
5502Lowercases the next
5503.Ar n
5504words.
5505.It Xo edit\-line:
5506.Op Ar n
5507.No \*(haXe
5508.Xc
5509Edit line
5510.Ar n
5511or the current line, if not specified, interactively.
5512The actual command executed is
5513.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
5514.It end\-of\-history: \*(ha[\*(Gt
5515Moves to the end of the history.
5516.It end\-of\-line: \*(haE, ANSI-End
5517Moves the cursor to the end of the input line.
5518.It eot: \*(ha_
5519Acts as an end-of-file; this is useful because edit-mode input disables
5520normal terminal input canonicalization.
5521.It Xo eot\-or\-delete:
5522.Op Ar n
5523.No \*(haD
5524.Xc
5525Acts as
5526.Ic eot
5527if alone on a line; otherwise acts as
5528.Ic delete\-char\-forward .
5529.It error: (not bound)
5530Error (ring the bell).
5531.It exchange\-point\-and\-mark: \*(haX\*(haX
5532Places the cursor where the mark is and sets the mark to where the cursor was.
5533.It expand\-file: \*(ha[*
5534Appends a
5535.Ql *
5536to the current word and replaces the word with the result of performing file
5537globbing on the word.
5538If no files match the pattern, the bell is rung.
5539.It Xo forward\-char:
5540.Op Ar n
5541.No \*(haF , \*(haXC , ANSI-CurRight
5542.Xc
5543Moves the cursor forward
5544.Ar n
5545characters.
5546.It Xo forward\-word:
5547.Op Ar n
5548.No \*(ha[f , ANSI-Ctrl-CurRight , ANSI-Alt-CurRight
5549.Xc
5550Moves the cursor forward to the end of the
5551.Ar n Ns th
5552word.
5553.It Xo goto\-history:
5554.Op Ar n
5555.No \*(ha[g
5556.Xc
5557Goes to history number
5558.Ar n .
5559.It kill\-line: KILL
5560Deletes the entire input line.
5561.It kill\-region: \*(haW
5562Deletes the input between the cursor and the mark.
5563.It Xo kill\-to\-eol:
5564.Op Ar n
5565.No \*(haK
5566.Xc
5567Deletes the input from the cursor to the end of the line if
5568.Ar n
5569is not specified; otherwise deletes characters between the cursor and column
5570.Ar n .
5571.It list: \*(ha[?
5572Prints a sorted, columnated list of command names or file names (if any) that
5573can complete the partial word containing the cursor.
5574Directory names have
5575.Ql /
5576appended to them.
5577.It list\-command: \*(haX?
5578Prints a sorted, columnated list of command names (if any) that can complete
5579the partial word containing the cursor.
5580.It list\-file: \*(haX\*(haY
5581Prints a sorted, columnated list of file names (if any) that can complete the
5582partial word containing the cursor.
5583File type indicators are appended as described under
5584.Ic list
5585above.
5586.It newline: \*(haJ , \*(haM
5587Causes the current input line to be processed by the shell.
5588The current cursor position may be anywhere on the line.
5589.It newline\-and\-next: \*(haO
5590Causes the current input line to be processed by the shell, and the next line
5591from history becomes the current line.
5592This is only useful after an
5593.Ic up\-history ,
5594.Ic search\-history
5595or
5596.Ic search\-history\-up .
5597.It no\-op: QUIT
5598This does nothing.
5599.It prefix\-1: \*(ha[
5600Introduces a 2-character command sequence.
5601.It prefix\-2: \*(haX , \*(ha[[ , \*(ha[O
5602Introduces a 2-character command sequence.
5603.It Xo prev\-hist\-word:
5604.Op Ar n
5605.No \*(ha[. , \*(ha[_
5606.Xc
5607The last word, or, if given, the
5608.Ar n Ns th
5609word (zero-based) of the previous (on repeated execution, second-last,
5610third-last, etc.) command is inserted at the cursor.
5611Use of this editing command trashes the mark.
5612.It quote: \*(ha\*(ha , \*(haV
5613The following character is taken literally rather than as an editing command.
5614.It redraw: \*(haL
5615Reprints the last line of the prompt string and the current input line
5616on a new line.
5617.It Xo search\-character\-backward:
5618.Op Ar n
5619.No \*(ha[\*(ha]
5620.Xc
5621Search backward in the current line for the
5622.Ar n Ns th
5623occurrence of the next character typed.
5624.It Xo search\-character\-forward:
5625.Op Ar n
5626.No \*(ha]
5627.Xc
5628Search forward in the current line for the
5629.Ar n Ns th
5630occurrence of the next character typed.
5631.It search\-history: \*(haR
5632Enter incremental search mode.
5633The internal history list is searched
5634backwards for commands matching the input.
5635An initial
5636.Ql \*(ha
5637in the search string anchors the search.
5638The escape key will leave search mode.
5639Other commands, including sequences of escape as
5640.Ic prefix\-1
5641followed by a
5642.Ic prefix\-1
5643or
5644.Ic prefix\-2
5645key will be executed after leaving search mode.
5646The
5647.Ic abort Pq \*(haG
5648command will restore the input line before search started.
5649Successive
5650.Ic search\-history
5651commands continue searching backward to the next previous occurrence of the
5652pattern.
5653The history buffer retains only a finite number of lines; the oldest
5654are discarded as necessary.
5655.It search\-history\-up: ANSI-PgUp
5656Search backwards through the history buffer for commands whose beginning match
5657the portion of the input line before the cursor.
5658When used on an empty line, this has the same effect as
5659.Ic up\-history .
5660.It search\-history\-down: ANSI-PgDn
5661Search forwards through the history buffer for commands whose beginning match
5662the portion of the input line before the cursor.
5663When used on an empty line, this has the same effect as
5664.Ic down\-history .
5665This is only useful after an
5666.Ic up\-history ,
5667.Ic search\-history
5668or
5669.Ic search\-history\-up .
5670.It set\-mark\-command: \*(ha[ Ns Aq space
5671Set the mark at the cursor position.
5672.It transpose\-chars: \*(haT
5673If at the end of line, or if the
5674.Ic gmacs
5675option is set, this exchanges the two previous characters; otherwise, it
5676exchanges the previous and current characters and moves the cursor one
5677character to the right.
5678.It Xo up\-history:
5679.Op Ar n
5680.No \*(haP , \*(haXA , ANSI-CurUp
5681.Xc
5682Scrolls the history buffer backward
5683.Ar n
5684lines (earlier).
5685.It Xo upcase\-word:
5686.Op Ar n
5687.No \*(ha[U , \*(ha[u
5688.Xc
5689Uppercase the next
5690.Ar n
5691words.
5692.It version: \*(ha[\*(haV
5693Display the version of
5694.Nm mksh .
5695The current edit buffer is restored as soon as a key is pressed.
5696The restoring keypress is processed, unless it is a space.
5697.It yank: \*(haY
5698Inserts the most recently killed text string at the current cursor position.
5699.It yank\-pop: \*(ha[y
5700Immediately after a
5701.Ic yank ,
5702replaces the inserted text string with the next previously killed text string.
5703.El
5704.Ss Vi editing mode
5705.Em Note:
5706The vi command-line editing mode is orphaned, yet still functional.
5707It is 8-bit clean but specifically does not support UTF-8 or MBCS.
5708.Pp
5709The vi command-line editor in
5710.Nm
5711has basically the same commands as the
5712.Xr vi 1
5713editor with the following exceptions:
5714.Bl -bullet
5715.It
5716You start out in insert mode.
5717.It
5718There are file name and command completion commands:
5719=, \e, *, \*(haX, \*(haE, \*(haF, and, optionally,
5720.Aq tab
5721and
5722.Aq esc .
5723.It
5724The
5725.Ic _
5726command is different (in
5727.Nm mksh ,
5728it is the last argument command; in
5729.Xr vi 1
5730it goes to the start of the current line).
5731.It
5732The
5733.Ic /
5734and
5735.Ic G
5736commands move in the opposite direction to the
5737.Ic j
5738command.
5739.It
5740Commands which don't make sense in a single line editor are not available
5741(e.g. screen movement commands and
5742.Xr ex 1 Ns -style
5743colon
5744.Pq Ic \&:
5745commands).
5746.El
5747.Pp
5748Like
5749.Xr vi 1 ,
5750there are two modes:
5751.Dq insert
5752mode and
5753.Dq command
5754mode.
5755In insert mode, most characters are simply put in the buffer at the
5756current cursor position as they are typed; however, some characters are
5757treated specially.
5758In particular, the following characters are taken from current
5759.Xr tty 4
5760settings
5761(see
5762.Xr stty 1 )
5763and have their usual meaning (normal values are in parentheses): kill (\*(haU),
5764erase (\*(ha?), werase (\*(haW), eof (\*(haD), intr (\*(haC), and quit (\*(ha\e).
5765In addition to
5766the above, the following characters are also treated specially in insert mode:
5767.Bl -tag -width XJXXXXM
5768.It \*(haE
5769Command and file name enumeration (see below).
5770.It \*(haF
5771Command and file name completion (see below).
5772If used twice in a row, the
5773list of possible completions is displayed; if used a third time, the completion
5774is undone.
5775.It \*(haH
5776Erases previous character.
5777.It \*(haJ \*(Ba \*(haM
5778End of line.
5779The current line is read, parsed, and executed by the shell.
5780.It \*(haV
5781Literal next.
5782The next character typed is not treated specially (can be used
5783to insert the characters being described here).
5784.It \*(haX
5785Command and file name expansion (see below).
5786.It Aq esc
5787Puts the editor in command mode (see below).
5788.It Aq tab
5789Optional file name and command completion (see
5790.Ic \*(haF
5791above), enabled with
5792.Ic set \-o vi\-tabcomplete .
5793.El
5794.Pp
5795In command mode, each character is interpreted as a command.
5796Characters that
5797don't correspond to commands, are illegal combinations of commands, or are
5798commands that can't be carried out, all cause beeps.
5799In the following command descriptions, an
5800.Op Ar n
5801indicates the command may be prefixed by a number (e.g.\&
5802.Ic 10l
5803moves right 10 characters); if no number prefix is used,
5804.Ar n
5805is assumed to be 1 unless otherwise specified.
5806The term
5807.Dq current position
5808refers to the position between the cursor and the character preceding the
5809cursor.
5810A
5811.Dq word
5812is a sequence of letters, digits, and underscore characters or a sequence of
5813non-letter, non-digit, non-underscore, and non-whitespace characters (e.g.\&
5814.Dq ab2*&\*(ha
5815contains two words) and a
5816.Dq big-word
5817is a sequence of non-whitespace characters.
5818.Pp
5819Special
5820.Nm
5821vi commands:
5822.Pp
5823The following commands are not in, or are different from, the normal vi file
5824editor:
5825.Bl -tag -width 10n
5826.It Xo
5827.Oo Ar n Oc Ns _
5828.Xc
5829Insert a space followed by the
5830.Ar n Ns th
5831big-word from the last command in the history at the current position and enter
5832insert mode; if
5833.Ar n
5834is not specified, the last word is inserted.
5835.It #
5836Insert the comment character
5837.Pq Sq #
5838at the start of the current line and return the line to the shell (equivalent
5839to
5840.Ic I#\*(haJ ) .
5841.It Xo
5842.Oo Ar n Oc Ns g
5843.Xc
5844Like
5845.Ic G ,
5846except if
5847.Ar n
5848is not specified, it goes to the most recent remembered line.
5849.It Xo
5850.Oo Ar n Oc Ns v
5851.Xc
5852Edit line
5853.Ar n
5854using the
5855.Xr vi 1
5856editor; if
5857.Ar n
5858is not specified, the current line is edited.
5859The actual command executed is
5860.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
5861.It * and \*(haX
5862Command or file name expansion is applied to the current big-word (with an
5863appended
5864.Ql *
5865if the word contains no file globbing characters) \*(en the big-word is replaced
5866with the resulting words.
5867If the current big-word is the first on the line
5868or follows one of the characters
5869.Ql \&; ,
5870.Ql \*(Ba ,
5871.Ql & ,
5872.Ql \&( ,
5873or
5874.Ql \&) ,
5875and does not contain a slash
5876.Pq Sq / ,
5877then command expansion is done; otherwise file name expansion is done.
5878Command expansion will match the big-word against all aliases, functions, and
5879built-in commands as well as any executable files found by searching the
5880directories in the
5881.Ev PATH
5882parameter.
5883File name expansion matches the big-word against the files in the
5884current directory.
5885After expansion, the cursor is placed just past the last
5886word and the editor is in insert mode.
5887.It Xo
5888.Oo Ar n Oc Ns \e ,
5889.Oo Ar n Oc Ns \*(haF ,
5890.Oo Ar n Oc Ns Aq tab ,
5891.No and
5892.Oo Ar n Oc Ns Aq esc
5893.Xc
5894Command/file name completion.
5895Replace the current big-word with the
5896longest unique match obtained after performing command and file name expansion.
5897.Aq tab
5898is only recognised if the
5899.Ic vi\-tabcomplete
5900option is set, while
5901.Aq esc
5902is only recognised if the
5903.Ic vi\-esccomplete
5904option is set (see
5905.Ic set \-o ) .
5906If
5907.Ar n
5908is specified, the
5909.Ar n Ns th
5910possible completion is selected (as reported by the command/file name
5911enumeration command).
5912.It = and \*(haE
5913Command/file name enumeration.
5914List all the commands or files that match the current big-word.
5915.It \*(haV
5916Display the version of
5917.Nm mksh .
5918The current edit buffer is restored as soon as a key is pressed.
5919The restoring keypress is ignored.
5920.It @ Ns Ar c
5921Macro expansion.
5922Execute the commands found in the alias
5923.Ar c .
5924.El
5925.Pp
5926Intra-line movement commands:
5927.Bl -tag -width Ds
5928.It Xo
5929.Oo Ar n Oc Ns h and
5930.Oo Ar n Oc Ns \*(haH
5931.Xc
5932Move left
5933.Ar n
5934characters.
5935.It Xo
5936.Oo Ar n Oc Ns l and
5937.Oo Ar n Oc Ns Aq space
5938.Xc
5939Move right
5940.Ar n
5941characters.
5942.It 0
5943Move to column 0.
5944.It \*(ha
5945Move to the first non-whitespace character.
5946.It Xo
5947.Oo Ar n Oc Ns \*(Ba
5948.Xc
5949Move to column
5950.Ar n .
5951.It $
5952Move to the last character.
5953.It Xo
5954.Oo Ar n Oc Ns b
5955.Xc
5956Move back
5957.Ar n
5958words.
5959.It Xo
5960.Oo Ar n Oc Ns B
5961.Xc
5962Move back
5963.Ar n
5964big-words.
5965.It Xo
5966.Oo Ar n Oc Ns e
5967.Xc
5968Move forward to the end of the word,
5969.Ar n
5970times.
5971.It Xo
5972.Oo Ar n Oc Ns E
5973.Xc
5974Move forward to the end of the big-word,
5975.Ar n
5976times.
5977.It Xo
5978.Oo Ar n Oc Ns w
5979.Xc
5980Move forward
5981.Ar n
5982words.
5983.It Xo
5984.Oo Ar n Oc Ns W
5985.Xc
5986Move forward
5987.Ar n
5988big-words.
5989.It %
5990Find match.
5991The editor looks forward for the nearest parenthesis, bracket, or
5992brace and then moves the cursor to the matching parenthesis, bracket, or brace.
5993.It Xo
5994.Oo Ar n Oc Ns f Ns Ar c
5995.Xc
5996Move forward to the
5997.Ar n Ns th
5998occurrence of the character
5999.Ar c .
6000.It Xo
6001.Oo Ar n Oc Ns F Ns Ar c
6002.Xc
6003Move backward to the
6004.Ar n Ns th
6005occurrence of the character
6006.Ar c .
6007.It Xo
6008.Oo Ar n Oc Ns t Ns Ar c
6009.Xc
6010Move forward to just before the
6011.Ar n Ns th
6012occurrence of the character
6013.Ar c .
6014.It Xo
6015.Oo Ar n Oc Ns T Ns Ar c
6016.Xc
6017Move backward to just before the
6018.Ar n Ns th
6019occurrence of the character
6020.Ar c .
6021.It Xo
6022.Oo Ar n Oc Ns \&;
6023.Xc
6024Repeats the last
6025.Ic f , F , t ,
6026or
6027.Ic T
6028command.
6029.It Xo
6030.Oo Ar n Oc Ns \&,
6031.Xc
6032Repeats the last
6033.Ic f , F , t ,
6034or
6035.Ic T
6036command, but moves in the opposite direction.
6037.El
6038.Pp
6039Inter-line movement commands:
6040.Bl -tag -width Ds
6041.It Xo
6042.Oo Ar n Oc Ns j ,
6043.Oo Ar n Oc Ns + ,
6044.No and
6045.Oo Ar n Oc Ns \*(haN
6046.Xc
6047Move to the
6048.Ar n Ns th
6049next line in the history.
6050.It Xo
6051.Oo Ar n Oc Ns k ,
6052.Oo Ar n Oc Ns \- ,
6053.No and
6054.Oo Ar n Oc Ns \*(haP
6055.Xc
6056Move to the
6057.Ar n Ns th
6058previous line in the history.
6059.It Xo
6060.Oo Ar n Oc Ns G
6061.Xc
6062Move to line
6063.Ar n
6064in the history; if
6065.Ar n
6066is not specified, the number of the first remembered line is used.
6067.It Xo
6068.Oo Ar n Oc Ns g
6069.Xc
6070Like
6071.Ic G ,
6072except if
6073.Ar n
6074is not specified, it goes to the most recent remembered line.
6075.It Xo
6076.Oo Ar n Oc Ns / Ns Ar string
6077.Xc
6078Search backward through the history for the
6079.Ar n Ns th
6080line containing
6081.Ar string ;
6082if
6083.Ar string
6084starts with
6085.Ql \*(ha ,
6086the remainder of the string must appear at the start of the history line for
6087it to match.
6088.It Xo
6089.Oo Ar n Oc Ns \&? Ns Ar string
6090.Xc
6091Same as
6092.Ic / ,
6093except it searches forward through the history.
6094.It Xo
6095.Oo Ar n Oc Ns n
6096.Xc
6097Search for the
6098.Ar n Ns th
6099occurrence of the last search string;
6100the direction of the search is the same as the last search.
6101.It Xo
6102.Oo Ar n Oc Ns N
6103.Xc
6104Search for the
6105.Ar n Ns th
6106occurrence of the last search string;
6107the direction of the search is the opposite of the last search.
6108.It Ar ANSI-CurUp
6109Take the characters from the beginning of the line to the current
6110cursor position as search string and do a backwards history search
6111for lines beginning with this string; keep the cursor position.
6112This works only in insert mode and keeps it enabled.
6113.El
6114.Pp
6115Edit commands
6116.Bl -tag -width Ds
6117.It Xo
6118.Oo Ar n Oc Ns a
6119.Xc
6120Append text
6121.Ar n
6122times; goes into insert mode just after the current position.
6123The append is
6124only replicated if command mode is re-entered i.e.\&
6125.Aq esc
6126is used.
6127.It Xo
6128.Oo Ar n Oc Ns A
6129.Xc
6130Same as
6131.Ic a ,
6132except it appends at the end of the line.
6133.It Xo
6134.Oo Ar n Oc Ns i
6135.Xc
6136Insert text
6137.Ar n
6138times; goes into insert mode at the current position.
6139The insertion is only
6140replicated if command mode is re-entered i.e.\&
6141.Aq esc
6142is used.
6143.It Xo
6144.Oo Ar n Oc Ns I
6145.Xc
6146Same as
6147.Ic i ,
6148except the insertion is done just before the first non-blank character.
6149.It Xo
6150.Oo Ar n Oc Ns s
6151.Xc
6152Substitute the next
6153.Ar n
6154characters (i.e. delete the characters and go into insert mode).
6155.It S
6156Substitute whole line.
6157All characters from the first non-blank character to the
6158end of the line are deleted and insert mode is entered.
6159.It Xo
6160.Oo Ar n Oc Ns c Ns Ar move-cmd
6161.Xc
6162Change from the current position to the position resulting from
6163.Ar n move-cmd Ns s
6164(i.e. delete the indicated region and go into insert mode); if
6165.Ar move-cmd
6166is
6167.Ic c ,
6168the line starting from the first non-blank character is changed.
6169.It C
6170Change from the current position to the end of the line (i.e. delete to the
6171end of the line and go into insert mode).
6172.It Xo
6173.Oo Ar n Oc Ns x
6174.Xc
6175Delete the next
6176.Ar n
6177characters.
6178.It Xo
6179.Oo Ar n Oc Ns X
6180.Xc
6181Delete the previous
6182.Ar n
6183characters.
6184.It D
6185Delete to the end of the line.
6186.It Xo
6187.Oo Ar n Oc Ns d Ns Ar move-cmd
6188.Xc
6189Delete from the current position to the position resulting from
6190.Ar n move-cmd Ns s ;
6191.Ar move-cmd
6192is a movement command (see above) or
6193.Ic d ,
6194in which case the current line is deleted.
6195.It Xo
6196.Oo Ar n Oc Ns r Ns Ar c
6197.Xc
6198Replace the next
6199.Ar n
6200characters with the character
6201.Ar c .
6202.It Xo
6203.Oo Ar n Oc Ns R
6204.Xc
6205Replace.
6206Enter insert mode but overwrite existing characters instead of
6207inserting before existing characters.
6208The replacement is repeated
6209.Ar n
6210times.
6211.It Xo
6212.Oo Ar n Oc Ns \*(TI
6213.Xc
6214Change the case of the next
6215.Ar n
6216characters.
6217.It Xo
6218.Oo Ar n Oc Ns y Ns Ar move-cmd
6219.Xc
6220Yank from the current position to the position resulting from
6221.Ar n move-cmd Ns s
6222into the yank buffer; if
6223.Ar move-cmd
6224is
6225.Ic y ,
6226the whole line is yanked.
6227.It Y
6228Yank from the current position to the end of the line.
6229.It Xo
6230.Oo Ar n Oc Ns p
6231.Xc
6232Paste the contents of the yank buffer just after the current position,
6233.Ar n
6234times.
6235.It Xo
6236.Oo Ar n Oc Ns P
6237.Xc
6238Same as
6239.Ic p ,
6240except the buffer is pasted at the current position.
6241.El
6242.Pp
6243Miscellaneous vi commands
6244.Bl -tag -width Ds
6245.It \*(haJ and \*(haM
6246The current line is read, parsed, and executed by the shell.
6247.It \*(haL and \*(haR
6248Redraw the current line.
6249.It Xo
6250.Oo Ar n Oc Ns \&.
6251.Xc
6252Redo the last edit command
6253.Ar n
6254times.
6255.It u
6256Undo the last edit command.
6257.It U
6258Undo all changes that have been made to the current line.
6259.It Ar intr No and Ar quit
6260The interrupt and quit terminal characters cause the current line to be
6261deleted and a new prompt to be printed.
6262.El
6263.Sh FILES
6264.Bl -tag -width XetcXsuid_profile -compact
6265.It Pa \*(TI/.mkshrc
6266User mkshrc profile (non-privileged interactive shells); see
6267.Sx Startup files.
6268The location can be changed at compile time (for embedded systems);
6269AOSP Android builds use
6270.Pa /system/etc/mkshrc .
6271.It Pa \*(TI/.profile
6272User profile (non-privileged login shells); see
6273.Sx Startup files
6274near the top of this manual.
6275.It Pa /etc/profile
6276System profile (login shells); see
6277.Sx Startup files.
6278.It Pa /etc/shells
6279Shell database.
6280.It Pa /etc/suid_profile
6281Suid profile (privileged shells); see
6282.Sx Startup files.
6283.El
6284.Pp
6285Note: On Android,
6286.Pa /system/etc/
6287contains the system and suid profile.
6288.Sh SEE ALSO
6289.Xr awk 1 ,
6290.Xr cat 1 ,
6291.Xr ed 1 ,
6292.Xr getopt 1 ,
6293.Xr sed 1 ,
6294.Xr sh 1 ,
6295.Xr stty 1 ,
6296.Xr dup 2 ,
6297.Xr execve 2 ,
6298.Xr getgid 2 ,
6299.Xr getuid 2 ,
6300.Xr mknod 2 ,
6301.Xr mkfifo 2 ,
6302.Xr open 2 ,
6303.Xr pipe 2 ,
6304.Xr rename 2 ,
6305.Xr wait 2 ,
6306.Xr getopt 3 ,
6307.Xr nl_langinfo 3 ,
6308.Xr setlocale 3 ,
6309.Xr signal 3 ,
6310.Xr system 3 ,
6311.Xr tty 4 ,
6312.Xr shells 5 ,
6313.Xr environ 7 ,
6314.Xr script 7 ,
6315.Xr utf\-8 7 ,
6316.Xr mknod 8
6317.Pp
6318.Pa http://docsrv.sco.com:507/en/man/html.C/sh.C.html
6319.Pp
6320.Pa https://www.mirbsd.org/ksh\-chan.htm
6321.Rs
6322.%A Morris Bolsky
6323.%B "The KornShell Command and Programming Language"
6324.%D 1989
6325.%I "Prentice Hall PTR"
6326.%P "xvi\ +\ 356 pages"
6327.%O "ISBN 978\-0\-13\-516972\-8 (0\-13\-516972\-0)"
6328.Re
6329.Rs
6330.%A Morris I. Bolsky
6331.%A David G. Korn
6332.%B "The New KornShell Command and Programming Language (2nd Edition)"
6333.%D 1995
6334.%I "Prentice Hall PTR"
6335.%P "xvi\ +\ 400 pages"
6336.%O "ISBN 978\-0\-13\-182700\-4 (0\-13\-182700\-6)"
6337.Re
6338.Rs
6339.%A Stephen G. Kochan
6340.%A Patrick H. Wood
6341.%B "\\*(tNUNIX\\*(sP Shell Programming"
6342.%V "3rd Edition"
6343.%D 2003
6344.%I "Sams"
6345.%P "xiii\ +\ 437 pages"
6346.%O "ISBN 978\-0\-672\-32490\-1 (0\-672\-32490\-3)"
6347.Re
6348.Rs
6349.%A "IEEE Inc."
6350.%T "\\*(tNIEEE\\*(sP Standard for Information Technology \*(en Portable Operating System Interface (POSIX)"
6351.%V "Part 2: Shell and Utilities"
6352.%D 1993
6353.%I "IEEE Press"
6354.%P "xvii\ +\ 1195 pages"
6355.%O "ISBN 978\-1\-55937\-255\-8 (1\-55937\-255\-9)"
6356.Re
6357.Rs
6358.%A Bill Rosenblatt
6359.%B "Learning the Korn Shell"
6360.%D 1993
6361.%I "O'Reilly"
6362.%P "360 pages"
6363.%O "ISBN 978\-1\-56592\-054\-5 (1\-56592\-054\-6)"
6364.Re
6365.Rs
6366.%A Bill Rosenblatt
6367.%A Arnold Robbins
6368.%B "Learning the Korn Shell, Second Edition"
6369.%D 2002
6370.%I "O'Reilly"
6371.%P "432 pages"
6372.%O "ISBN 978\-0\-596\-00195\-7 (0\-596\-00195\-9)"
6373.Re
6374.Rs
6375.%A Barry Rosenberg
6376.%B "KornShell Programming Tutorial"
6377.%D 1991
6378.%I "Addison-Wesley Professional"
6379.%P "xxi\ +\ 324 pages"
6380.%O "ISBN 978\-0\-201\-56324\-5 (0\-201\-56324\-X)"
6381.Re
6382.Sh AUTHORS
6383.An -nosplit
6384.Nm "The MirBSD Korn Shell"
6385is developed by
6386.An Thorsten Glaser Aq tg@mirbsd.org
6387and currently maintained as part of The MirOS Project.
6388This shell is based on the public domain 7th edition Bourne shell clone by
6389.An Charles Forsyth ,
6390who kindly agreed to, in countries where the Public Domain status of the work
6391may not be valid, grant a copyright licence to the general public to deal in
6392the work without restriction and permission to sublicence derivates under the
6393terms of any (OSI approved) Open Source licence,
6394and parts of the BRL shell by
6395.An Doug A. Gwyn ,
6396.An Doug Kingston ,
6397.An Ron Natalie ,
6398.An Arnold Robbins ,
6399.An Lou Salkind ,
6400and others.
6401The first release of
6402.Nm pdksh
6403was created by
6404.An Eric Gisin ,
6405and it was subsequently maintained by
6406.An John R. MacMillan Aq Mt change!john@sq.sq.com ,
6407.An Simon J. Gerraty Aq Mt sjg@zen.void.oz.au ,
6408and
6409.An Michael Rendell Aq Mt michael@cs.mun.ca .
6410The effort of several projects, such as Debian and OpenBSD, and other
6411contributors including our users, to improve the shell is appreciated.
6412See the documentation, CVS, and web site for details.
6413.Pp
6414The BSD daemon is Copyright \(co Marshall Kirk McKusick.
6415The complete legalese is at:
6416.Pa https://www.mirbsd.org/TaC\-mksh.txt
6417.\"
6418.\" This boils down to: feel free to use mksh.ico as application icon
6419.\" or shortcut for mksh or mksh/Win32; distro patches are ok (but we
6420.\" request they amend $KSH_VERSION when modifying mksh). Authors are
6421.\" Marshall Kirk McKusick (UCB), Rick Collette (ekkoBSD), Thorsten
6422.\" Glaser, Benny Siegert (MirBSD), Michael Langguth (mksh/Win32).
6423.\"
6424.\" As far as MirBSD is concerned, the files themselves are free
6425.\" to modification and distribution under BSD/MirOS Licence, the
6426.\" restriction on use stems only from trademark law's requirement
6427.\" to protect it or lose it, which McKusick almost did.
6428.\"
6429.Sh CAVEATS
6430.Nm
6431only supports the Unicode BMP (Basic Multilingual Plane).
6432.Pp
6433.Nm
6434has a different scope model from
6435.At
6436.Nm ksh ,
6437which leads to subtile differences in semantics for identical builtins.
6438This can cause issues with a
6439.Ic nameref
6440to suddenly point to a local variable by accident; fixing this is hard.
6441.Pp
6442The parts of a pipeline, like below, are executed in subshells.
6443Thus, variable assignments inside them are not visible in the
6444surrounding execution environment.
6445Use co-processes instead.
6446.Bd -literal -offset indent
6447foo \*(Ba bar \*(Ba read baz            # will not change $baz
6448foo \*(Ba bar \*(Ba& read \-p baz        # will, however, do so
6449.Ed
6450.Pp
6451.Nm mksh
6452provides a consistent set of 32-bit integer arithmetics, both signed
6453and unsigned, with defined wraparound and sign of the result of a
6454remainder operation, even (defying POSIX) on 64-bit systems.
6455If you require 64-bit integer arithmetics, use
6456.Nm lksh Pq legacy mksh
6457instead, but be aware that, in POSIX, it's legal for the OS to make
6458.Li print $((2147483647 + 1))
6459delete all files on your system, as it's Undefined Behaviour.
6460.Sh BUGS
6461Suspending (using \*(haZ) pipelines like the one below will only suspend
6462the currently running part of the pipeline; in this example,
6463.Dq fubar
6464is immediately printed on suspension (but not later after an
6465.Ic fg ) .
6466.Bd -literal -offset indent
6467$ /bin/sleep 666 && echo fubar
6468.Ed
6469.Pp
6470This document attempts to describe
6471.Nm mksh\ R50
6472and up,
6473compiled without any options impacting functionality, such as
6474.Dv MKSH_SMALL ,
6475when not called as
6476.Pa /bin/sh
6477which, on some systems only, enables
6478.Ic set \-o sh
6479automatically (whose behaviour differs across targets),
6480for an operating environment supporting all of its advanced needs.
6481Please report bugs in
6482.Nm
6483to the
6484.Mx
6485mailing list at
6486.Aq miros\-mksh@mirbsd.org
6487or in the
6488.Li \&#\&!/bin/mksh
6489.Pq or Li \&#ksh
6490IRC channel at
6491.Pa irc.freenode.net
6492.Pq Port 6697 SSL, 6667 unencrypted ,
6493or at:
6494.Pa https://launchpad.net/mksh
6495