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