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