blob: 6a2609a962bc0cb0083aead81cef49103b798c3f [file] [log] [blame]
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001.\" $MirOS: src/bin/mksh/mksh.1,v 1.442 2017/04/12 18:30:58 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 Hughesa3c3f962017-04-12 16:52:30 -07005.\" 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006.\" 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
Elliott Hughes77740fc2016-08-12 15:06:53 -070032.\" Also make sure to use \& *before* a punctuation char that is to not
33.\" be interpreted as punctuation, and especially with two-letter words
34.\" but also (after) a period that does not end a sentence (“e.g.\&”).
Geremy Condra03ebf062011-10-12 18:17:24 -070035.\" The section after the "doc" macropackage has been loaded contains
36.\" additional code to convene between the UCB mdoc macropackage (and
37.\" its variant as BSD mdoc in groff) and the GNU mdoc macropackage.
38.\"
39.ie \n(.g \{\
40. if \*[.T]ascii .tr \-\N'45'
41. if \*[.T]latin1 .tr \-\N'45'
42. if \*[.T]utf8 .tr \-\N'45'
43. ds <= \[<=]
44. ds >= \[>=]
45. ds Rq \[rq]
46. ds Lq \[lq]
47. ds sL \(aq
48. ds sR \(aq
49. if \*[.T]utf8 .ds sL `
50. if \*[.T]ps .ds sL `
51. if \*[.T]utf8 .ds sR '
52. if \*[.T]ps .ds sR '
53. ds aq \(aq
54. ds TI \(ti
55. ds ha \(ha
56. ds en \(en
57.\}
58.el \{\
59. ds aq '
60. ds TI ~
61. ds ha ^
62. ds en \(em
63.\}
64.\"
65.\" Implement .Dd with the Mdocdate RCS keyword
66.\"
67.rn Dd xD
68.de Dd
69.ie \\$1$Mdocdate: \{\
70. xD \\$2 \\$3, \\$4
71.\}
72.el .xD \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8
73..
74.\"
75.\" .Dd must come before definition of .Mx, because when called
76.\" with -mandoc, it might implement .Mx itself, but we want to
77.\" use our own definition. And .Dd must come *first*, always.
78.\"
Elliott Hughesa3c3f962017-04-12 16:52:30 -070079.Dd $Mdocdate: April 12 2017 $
Geremy Condra03ebf062011-10-12 18:17:24 -070080.\"
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000081.\" Check which macro package we use, and do other -mdoc setup.
Geremy Condra03ebf062011-10-12 18:17:24 -070082.\"
83.ie \n(.g \{\
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000084. if \*[.T]utf8 .tr \[la]\*(Lt
85. if \*[.T]utf8 .tr \[ra]\*(Gt
Geremy Condra03ebf062011-10-12 18:17:24 -070086. ie d volume-ds-1 .ds tT gnu
87. el .ds tT bsd
88.\}
89.el .ds tT ucb
90.\"
91.\" Implement .Mx (MirBSD)
92.\"
93.ie "\*(tT"gnu" \{\
94. eo
95. de Mx
96. nr curr-font \n[.f]
97. nr curr-size \n[.ps]
98. ds str-Mx \f[\n[curr-font]]\s[\n[curr-size]u]
99. ds str-Mx1 \*[Tn-font-size]\%MirOS\*[str-Mx]
100. if !\n[arg-limit] \
101. if \n[.$] \{\
102. ds macro-name Mx
103. parse-args \$@
104. \}
105. if (\n[arg-limit] > \n[arg-ptr]) \{\
106. nr arg-ptr +1
107. ie (\n[type\n[arg-ptr]] == 2) \
108. as str-Mx1 \~\*[arg\n[arg-ptr]]
109. el \
110. nr arg-ptr -1
111. \}
112. ds arg\n[arg-ptr] "\*[str-Mx1]
113. nr type\n[arg-ptr] 2
114. ds space\n[arg-ptr] "\*[space]
115. nr num-args (\n[arg-limit] - \n[arg-ptr])
116. nr arg-limit \n[arg-ptr]
117. if \n[num-args] \
118. parse-space-vector
119. print-recursive
120..
121. ec
122. ds sP \s0
123. ds tN \*[Tn-font-size]
124.\}
125.el \{\
126. de Mx
127. nr cF \\n(.f
128. nr cZ \\n(.s
129. ds aa \&\f\\n(cF\s\\n(cZ
130. if \\n(aC==0 \{\
131. ie \\n(.$==0 \&MirOS\\*(aa
132. el .aV \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
133. \}
134. if \\n(aC>\\n(aP \{\
135. nr aP \\n(aP+1
136. ie \\n(C\\n(aP==2 \{\
137. as b1 \&MirOS\ #\&\\*(A\\n(aP\\*(aa
138. ie \\n(aC>\\n(aP \{\
139. nr aP \\n(aP+1
140. nR
141. \}
142. el .aZ
143. \}
144. el \{\
145. as b1 \&MirOS\\*(aa
146. nR
147. \}
148. \}
149..
150.\}
151.\"-
152.Dt MKSH 1
153.Os MirBSD
154.Sh NAME
155.Nm mksh ,
156.Nm sh
157.Nd MirBSD Korn shell
158.Sh SYNOPSIS
159.Nm
160.Bk -words
161.Op Fl +abCefhiklmnprUuvXx
Thorsten Glaser811a5752013-07-25 14:24:45 +0000162.Oo
163.Fl T Oo Ar \&! Oc Ns Ar tty
164\*(Ba
165.Ar \&\-
166.Oc
Geremy Condra03ebf062011-10-12 18:17:24 -0700167.Op Fl +o Ar option
168.Oo
169.Fl c Ar string \*(Ba
170.Fl s \*(Ba
171.Ar file
172.Op Ar argument ...
173.Oc
174.Ek
175.Nm builtin-name
176.Op Ar argument ...
177.Sh DESCRIPTION
178.Nm
179is a command interpreter intended for both interactive and shell
180script use.
181Its command language is a superset of the
182.Xr sh C
183shell language and largely compatible to the original Korn shell.
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800184At times, this manual page may give scripting advice; while it
185sometimes does take portable shell scripting or various standards
186into account all information is first and foremost presented with
187.Nm
188in mind and should be taken as such.
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700189.Ss I use Android, OS/2, etc. so what...?
190Please see the FAQ at the end of this document.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000191.Ss Invocation
Geremy Condra03ebf062011-10-12 18:17:24 -0700192Most builtins can be called directly, for example if a link points from its
193name to the shell; not all make sense, have been tested or work at all though.
194.Pp
195The options are as follows:
196.Bl -tag -width XcXstring
197.It Fl c Ar string
198.Nm
199will execute the command(s) contained in
200.Ar string .
201.It Fl i
202Interactive shell.
Elliott Hughes56b517d2014-10-06 11:30:44 -0700203A shell that reads commands from standard input is
Geremy Condra03ebf062011-10-12 18:17:24 -0700204.Dq interactive
205if this
206option is used or if both standard input and standard error are attached
207to a
208.Xr tty 4 .
209An interactive shell has job control enabled, ignores the
210.Dv SIGINT ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800211.Dv SIGQUIT
Geremy Condra03ebf062011-10-12 18:17:24 -0700212and
213.Dv SIGTERM
214signals, and prints prompts before reading input (see the
215.Ev PS1
216and
217.Ev PS2
218parameters).
219It also processes the
220.Ev ENV
221parameter or the
222.Pa mkshrc
223file (see below).
224For non-interactive shells, the
225.Ic trackall
226option is on by default (see the
227.Ic set
228command below).
229.It Fl l
230Login shell.
231If the basename the shell is called with (i.e. argv[0])
232starts with
233.Ql \-
234or if this option is used,
235the shell is assumed to be a login shell; see
236.Sx Startup files
237below.
238.It Fl p
239Privileged shell.
240A shell is
241.Dq privileged
Elliott Hughes737fdce2014-08-07 12:59:26 -0700242if the real user ID or group ID does not match the
Geremy Condra03ebf062011-10-12 18:17:24 -0700243effective user ID or group ID (see
244.Xr getuid 2
245and
246.Xr getgid 2 ) .
247Clearing the privileged option causes the shell to set
248its effective user ID (group ID) to its real user ID (group ID).
249For further implications, see
250.Sx Startup files .
Elliott Hughes737fdce2014-08-07 12:59:26 -0700251If the shell is privileged and this flag is not explicitly set, the
252.Dq privileged
253option is cleared automatically after processing the startup files.
Geremy Condra03ebf062011-10-12 18:17:24 -0700254.It Fl r
255Restricted shell.
256A shell is
257.Dq restricted
258if this
259option is used.
260The following restrictions come into effect after the shell processes any
261profile and
262.Ev ENV
263files:
264.Pp
265.Bl -bullet -compact
266.It
267The
268.Ic cd
269.Po and Ic chdir Pc
270command is disabled.
271.It
272The
273.Ev SHELL ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800274.Ev ENV
Geremy Condra03ebf062011-10-12 18:17:24 -0700275and
276.Ev PATH
277parameters cannot be changed.
278.It
279Command names can't be specified with absolute or relative paths.
280.It
281The
282.Fl p
283option of the built-in command
284.Ic command
285can't be used.
286.It
287Redirections that create files can't be used (i.e.\&
Elliott Hughes966dd552016-12-08 15:56:04 -0800288.Dq Li \*(Gt ,
289.Dq Li \*(Gt\*(Ba ,
290.Dq Li \*(Gt\*(Gt ,
291.Dq Li \*(Lt\*(Gt ) .
Geremy Condra03ebf062011-10-12 18:17:24 -0700292.El
293.It Fl s
294The shell reads commands from standard input; all non-option arguments
295are positional parameters.
Thorsten Glaser811a5752013-07-25 14:24:45 +0000296.It Fl T Ar name
Geremy Condra03ebf062011-10-12 18:17:24 -0700297Spawn
298.Nm
299on the
300.Xr tty 4
301device given.
Thorsten Glaser811a5752013-07-25 14:24:45 +0000302The paths
303.Ar name ,
304.Pa /dev/ttyC Ns Ar name
305and
306.Pa /dev/tty Ns Ar name
307are attempted in order.
308Unless
309.Ar name
310begins with an exclamation mark
Elliott Hughes966dd552016-12-08 15:56:04 -0800311.Pq Ql \&! ,
Thorsten Glaser811a5752013-07-25 14:24:45 +0000312this is done in a subshell and returns immediately.
Geremy Condra03ebf062011-10-12 18:17:24 -0700313If
Thorsten Glaser811a5752013-07-25 14:24:45 +0000314.Ar name
315is a dash
Elliott Hughes966dd552016-12-08 15:56:04 -0800316.Pq Ql \&\- ,
Thorsten Glaser811a5752013-07-25 14:24:45 +0000317detach from controlling terminal (daemonise) instead.
Geremy Condra03ebf062011-10-12 18:17:24 -0700318.El
319.Pp
320In addition to the above, the options described in the
321.Ic set
322built-in command can also be used on the command line:
323both
324.Op Fl +abCefhkmnuvXx
325and
326.Op Fl +o Ar option
327can be used for single letter or long options, respectively.
328.Pp
329If neither the
330.Fl c
331nor the
332.Fl s
333option is specified, the first non-option argument specifies the name
334of a file the shell reads commands from.
335If there are no non-option
336arguments, the shell reads commands from the standard input.
337The name of the shell (i.e. the contents of $0)
338is determined as follows: if the
339.Fl c
340option is used and there is a non-option argument, it is used as the name;
341if commands are being read from a file, the file is used as the name;
342otherwise, the basename the shell was called with (i.e. argv[0]) is used.
343.Pp
344The exit status of the shell is 127 if the command file specified on the
345command line could not be opened, or non-zero if a fatal syntax error
346occurred during the execution of a script.
347In the absence of fatal errors,
Elliott Hughes966dd552016-12-08 15:56:04 -0800348the exit status is that of the last command executed, or zero if no
Geremy Condra03ebf062011-10-12 18:17:24 -0700349command is executed.
350.Ss Startup files
351For the actual location of these files, see
352.Sx FILES .
353A login shell processes the system profile first.
354A privileged shell then processes the suid profile.
355A non-privileged login shell processes the user profile next.
356A non-privileged interactive shell checks the value of the
357.Ev ENV
358parameter after subjecting it to parameter, command, arithmetic and tilde
Elliott Hughes966dd552016-12-08 15:56:04 -0800359.Pq Ql \*(TI
Geremy Condra03ebf062011-10-12 18:17:24 -0700360substitution; if unset or empty, the user mkshrc profile is processed;
361otherwise, if a file whose name is the substitution result exists,
362it is processed; non-existence is silently ignored.
Elliott Hughes737fdce2014-08-07 12:59:26 -0700363A privileged shell then drops privileges if neither was the
364.Fl p
365option given on the command line nor set during execution of the startup files.
Geremy Condra03ebf062011-10-12 18:17:24 -0700366.Ss Command syntax
367The shell begins parsing its input by removing any backslash-newline
368combinations, then breaking it into
369.Em words .
370Words (which are sequences of characters) are delimited by unquoted whitespace
Elliott Hughes966dd552016-12-08 15:56:04 -0800371characters (space, tab and newline) or meta-characters
Geremy Condra03ebf062011-10-12 18:17:24 -0700372.Po
373.Ql \*(Lt ,
374.Ql \*(Gt ,
375.Ql \*(Ba ,
376.Ql \&; ,
377.Ql \&( ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800378.Ql \&)
Geremy Condra03ebf062011-10-12 18:17:24 -0700379and
380.Ql &
381.Pc .
382Aside from delimiting words, spaces and tabs are ignored, while newlines
383usually delimit commands.
384The meta-characters are used in building the following
385.Em tokens :
Elliott Hughes966dd552016-12-08 15:56:04 -0800386.Dq Li \*(Lt ,
387.Dq Li \*(Lt& ,
388.Dq Li \*(Lt\*(Lt ,
389.Dq Li \*(Lt\*(Lt\*(Lt ,
390.Dq Li \*(Gt ,
391.Dq Li \*(Gt& ,
392.Dq Li \*(Gt\*(Gt ,
393.Dq Li &\*(Gt ,
Geremy Condra03ebf062011-10-12 18:17:24 -0700394etc. are used to specify redirections (see
395.Sx Input/output redirection
396below);
Elliott Hughes966dd552016-12-08 15:56:04 -0800397.Dq Li \*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700398is used to create pipelines;
Elliott Hughes966dd552016-12-08 15:56:04 -0800399.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -0700400is used to create co-processes (see
401.Sx Co-processes
402below);
Elliott Hughes966dd552016-12-08 15:56:04 -0800403.Dq Li \&;
Geremy Condra03ebf062011-10-12 18:17:24 -0700404is used to separate commands;
Elliott Hughes966dd552016-12-08 15:56:04 -0800405.Dq Li &
Geremy Condra03ebf062011-10-12 18:17:24 -0700406is used to create asynchronous pipelines;
Elliott Hughes966dd552016-12-08 15:56:04 -0800407.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700408and
Elliott Hughes966dd552016-12-08 15:56:04 -0800409.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700410are used to specify conditional execution;
Elliott Hughes966dd552016-12-08 15:56:04 -0800411.Dq Li \&;; ,
412.Dq Li \&;&
Geremy Condra03ebf062011-10-12 18:17:24 -0700413and
Elliott Hughes966dd552016-12-08 15:56:04 -0800414.Dq Li \&;\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700415are used in
416.Ic case
417statements;
Elliott Hughes966dd552016-12-08 15:56:04 -0800418.Dq Li \&(( ... \&))
Geremy Condra03ebf062011-10-12 18:17:24 -0700419is used in arithmetic expressions;
420and lastly,
Elliott Hughes966dd552016-12-08 15:56:04 -0800421.Dq Li \&( ... \&)
Geremy Condra03ebf062011-10-12 18:17:24 -0700422is used to create subshells.
423.Pp
424Whitespace and meta-characters can be quoted individually using a backslash
Elliott Hughes966dd552016-12-08 15:56:04 -0800425.Pq Ql \e ,
Geremy Condra03ebf062011-10-12 18:17:24 -0700426or in groups using double
Elliott Hughes966dd552016-12-08 15:56:04 -0800427.Pq Ql \&"
Geremy Condra03ebf062011-10-12 18:17:24 -0700428or single
Elliott Hughes966dd552016-12-08 15:56:04 -0800429.Pq Dq Li \*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -0700430quotes.
431Note that the following characters are also treated specially by the
432shell and must be quoted if they are to represent themselves:
433.Ql \e ,
434.Ql \&" ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800435.Dq Li \*(aq ,
Geremy Condra03ebf062011-10-12 18:17:24 -0700436.Ql # ,
437.Ql $ ,
438.Ql \` ,
439.Ql \*(TI ,
440.Ql { ,
441.Ql } ,
442.Ql * ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800443.Ql \&?
Geremy Condra03ebf062011-10-12 18:17:24 -0700444and
445.Ql \&[ .
446The first three of these are the above mentioned quoting characters (see
447.Sx Quoting
448below);
449.Ql # ,
450if used at the beginning of a word, introduces a comment \*(en everything after
451the
452.Ql #
453up to the nearest newline is ignored;
454.Ql $
Elliott Hughes966dd552016-12-08 15:56:04 -0800455is used to introduce parameter, command and arithmetic substitutions (see
Geremy Condra03ebf062011-10-12 18:17:24 -0700456.Sx Substitution
457below);
458.Ql \`
459introduces an old-style command substitution (see
460.Sx Substitution
461below);
462.Ql \*(TI
463begins a directory expansion (see
464.Sx Tilde expansion
465below);
466.Ql {
467and
468.Ql }
469delimit
470.Xr csh 1 Ns -style
Elliott Hughes96b43632015-07-17 11:39:41 -0700471alternations (see
Geremy Condra03ebf062011-10-12 18:17:24 -0700472.Sx Brace expansion
473below);
474and finally,
475.Ql * ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800476.Ql \&?
Geremy Condra03ebf062011-10-12 18:17:24 -0700477and
478.Ql \&[
479are used in file name generation (see
480.Sx File name patterns
481below).
482.Pp
483As words and tokens are parsed, the shell builds commands, of which there
484are two basic types:
485.Em simple-commands ,
486typically programmes that are executed, and
487.Em compound-commands ,
488such as
489.Ic for
490and
491.Ic if
Elliott Hughes966dd552016-12-08 15:56:04 -0800492statements, grouping constructs and function definitions.
Geremy Condra03ebf062011-10-12 18:17:24 -0700493.Pp
494A simple-command consists of some combination of parameter assignments
495(see
496.Sx Parameters
497below),
498input/output redirections (see
499.Sx Input/output redirections
Elliott Hughes966dd552016-12-08 15:56:04 -0800500below)
Geremy Condra03ebf062011-10-12 18:17:24 -0700501and command words; the only restriction is that parameter assignments come
502before any command words.
503The command words, if any, define the command
504that is to be executed and its arguments.
Elliott Hughes966dd552016-12-08 15:56:04 -0800505The command may be a shell built-in command, a function
Geremy Condra03ebf062011-10-12 18:17:24 -0700506or an external command
507(i.e. a separate executable file that is located using the
508.Ev PATH
509parameter; see
510.Sx Command execution
511below).
512Note that all command constructs have an exit status: for external commands,
513this is related to the status returned by
514.Xr wait 2
515(if the command could not be found, the exit status is 127; if it could not
516be executed, the exit status is 126); the exit status of other command
517constructs (built-in commands, functions, compound-commands, pipelines, lists,
518etc.) are all well-defined and are described where the construct is
519described.
520The exit status of a command consisting only of parameter
521assignments is that of the last command substitution performed during the
522parameter assignment or 0 if there were no command substitutions.
523.Pp
524Commands can be chained together using the
Elliott Hughes966dd552016-12-08 15:56:04 -0800525.Dq Li \*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700526token to form pipelines, in which the standard output of each command but the
527last is piped (see
528.Xr pipe 2 )
529to the standard input of the following command.
Thorsten Glaser811a5752013-07-25 14:24:45 +0000530The exit status of a pipeline is that of its last command, unless the
531.Ic pipefail
532option is set (see there).
Geremy Condra03ebf062011-10-12 18:17:24 -0700533All commands of a pipeline are executed in separate subshells;
534this is allowed by POSIX but differs from both variants of
535.At
536.Nm ksh ,
537where all but the last command were executed in subshells; see the
538.Ic read
539builtin's description for implications and workarounds.
540A pipeline may be prefixed by the
Elliott Hughes966dd552016-12-08 15:56:04 -0800541.Dq Li \&!
Geremy Condra03ebf062011-10-12 18:17:24 -0700542reserved word which causes the exit status of the pipeline to be logically
543complemented: if the original status was 0, the complemented status will be 1;
544if the original status was not 0, the complemented status will be 0.
545.Pp
546.Em Lists
547of commands can be created by separating pipelines by any of the following
548tokens:
Elliott Hughes966dd552016-12-08 15:56:04 -0800549.Dq Li && ,
550.Dq Li \*(Ba\*(Ba ,
551.Dq Li & ,
552.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -0700553and
Elliott Hughes966dd552016-12-08 15:56:04 -0800554.Dq Li \&; .
Geremy Condra03ebf062011-10-12 18:17:24 -0700555The first two are for conditional execution:
556.Dq Ar cmd1 No && Ar cmd2
557executes
558.Ar cmd2
559only if the exit status of
560.Ar cmd1
561is zero;
Elliott Hughes966dd552016-12-08 15:56:04 -0800562.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700563is the opposite \*(en
564.Ar cmd2
565is executed only if the exit status of
566.Ar cmd1
567is non-zero.
Elliott Hughes966dd552016-12-08 15:56:04 -0800568.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700569and
Elliott Hughes966dd552016-12-08 15:56:04 -0800570.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700571have equal precedence which is higher than that of
Elliott Hughes966dd552016-12-08 15:56:04 -0800572.Dq Li & ,
573.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -0700574and
Elliott Hughes966dd552016-12-08 15:56:04 -0800575.Dq Li \&; ,
Geremy Condra03ebf062011-10-12 18:17:24 -0700576which also have equal precedence.
577Note that the
Elliott Hughes966dd552016-12-08 15:56:04 -0800578.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700579and
Elliott Hughes966dd552016-12-08 15:56:04 -0800580.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700581operators are
582.Qq left-associative .
583For example, both of these commands will print only
584.Qq bar :
585.Bd -literal -offset indent
586$ false && echo foo \*(Ba\*(Ba echo bar
587$ true \*(Ba\*(Ba echo foo && echo bar
588.Ed
589.Pp
590The
Elliott Hughes966dd552016-12-08 15:56:04 -0800591.Dq Li &
Geremy Condra03ebf062011-10-12 18:17:24 -0700592token causes the preceding command to be executed asynchronously; that is,
593the shell starts the command but does not wait for it to complete (the shell
594does keep track of the status of asynchronous commands; see
595.Sx Job control
596below).
597When an asynchronous command is started when job control is disabled
598(i.e. in most scripts), the command is started with signals
599.Dv SIGINT
600and
601.Dv SIGQUIT
602ignored and with input redirected from
603.Pa /dev/null
604(however, redirections specified in the asynchronous command have precedence).
605The
Elliott Hughes966dd552016-12-08 15:56:04 -0800606.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -0700607operator starts a co-process which is a special kind of asynchronous process
608(see
609.Sx Co-processes
610below).
611Note that a command must follow the
Elliott Hughes966dd552016-12-08 15:56:04 -0800612.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700613and
Elliott Hughes966dd552016-12-08 15:56:04 -0800614.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700615operators, while it need not follow
Elliott Hughes966dd552016-12-08 15:56:04 -0800616.Dq Li & ,
617.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -0700618or
Elliott Hughes966dd552016-12-08 15:56:04 -0800619.Dq Li \&; .
Geremy Condra03ebf062011-10-12 18:17:24 -0700620The exit status of a list is that of the last command executed, with the
621exception of asynchronous lists, for which the exit status is 0.
622.Pp
623Compound commands are created using the following reserved words.
624These words
625are only recognised if they are unquoted and if they are used as the first
626word of a command (i.e. they can't be preceded by parameter assignments or
627redirections):
628.Bd -literal -offset indent
629case else function then ! (
630do esac if time [[ ((
631done fi in until {
632elif for select while }
633.Ed
634.Pp
635In the following compound command descriptions, command lists (denoted as
636.Em list )
Elliott Hughes966dd552016-12-08 15:56:04 -0800637that are followed by reserved words must end with a semicolon, a newline or
Geremy Condra03ebf062011-10-12 18:17:24 -0700638a (syntactically correct) reserved word.
639For example, the following are all valid:
640.Bd -literal -offset indent
641$ { echo foo; echo bar; }
642$ { echo foo; echo bar\*(Ltnewline\*(Gt}
643$ { { echo foo; echo bar; } }
644.Ed
645.Pp
646This is not valid:
647.Pp
648.Dl $ { echo foo; echo bar }
649.Bl -tag -width 4n
650.It Pq Ar list
651Execute
652.Ar list
653in a subshell.
654There is no implicit way to pass environment changes from a
655subshell back to its parent.
656.It { Ar list ; No }
657Compound construct;
658.Ar list
659is executed, but not in a subshell.
660Note that
Elliott Hughes966dd552016-12-08 15:56:04 -0800661.Dq Li {
Geremy Condra03ebf062011-10-12 18:17:24 -0700662and
Elliott Hughes966dd552016-12-08 15:56:04 -0800663.Dq Li }
Geremy Condra03ebf062011-10-12 18:17:24 -0700664are reserved words, not meta-characters.
665.It Xo case Ar word No in
666.Oo Op \&(
667.Ar pattern
Elliott Hughes50012062015-03-10 22:22:24 -0700668.Op \*(Ba Ar pattern
Geremy Condra03ebf062011-10-12 18:17:24 -0700669.No ... Ns )
670.Ar list
Elliott Hughes50012062015-03-10 22:22:24 -0700671.Ic terminator
672.Oc No ... esac
Geremy Condra03ebf062011-10-12 18:17:24 -0700673.Xc
674The
675.Ic case
676statement attempts to match
677.Ar word
678against a specified
679.Ar pattern ;
680the
681.Ar list
682associated with the first successfully matched pattern is executed.
683Patterns used in
684.Ic case
685statements are the same as those used for file name patterns except that the
686restrictions regarding
687.Ql \&.
688and
689.Ql /
690are dropped.
691Note that any unquoted space before and after a pattern is
692stripped; any space within a pattern must be quoted.
693Both the word and the
Elliott Hughes966dd552016-12-08 15:56:04 -0800694patterns are subject to parameter, command and arithmetic substitution, as
Geremy Condra03ebf062011-10-12 18:17:24 -0700695well as tilde substitution.
696.Pp
697For historical reasons, open and close braces may be used instead of
698.Ic in
699and
700.Ic esac
701e.g.\&
Elliott Hughes50012062015-03-10 22:22:24 -0700702.Ic case $foo { *) echo bar ;; } .
Geremy Condra03ebf062011-10-12 18:17:24 -0700703.Pp
Elliott Hughes50012062015-03-10 22:22:24 -0700704The list
705.Ic terminator Ns s
706are:
Geremy Condra03ebf062011-10-12 18:17:24 -0700707.Bl -tag -width 4n
Elliott Hughes966dd552016-12-08 15:56:04 -0800708.It Dq Li ;;
Geremy Condra03ebf062011-10-12 18:17:24 -0700709Terminate after the list.
Elliott Hughes966dd552016-12-08 15:56:04 -0800710.It Dq Li \&;&
Geremy Condra03ebf062011-10-12 18:17:24 -0700711Fall through into the next list.
Elliott Hughes966dd552016-12-08 15:56:04 -0800712.It Dq Li \&;\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700713Evaluate the remaining pattern-list tuples.
714.El
715.Pp
716The exit status of a
717.Ic case
718statement is that of the executed
719.Ar list ;
720if no
721.Ar list
722is executed, the exit status is zero.
723.It Xo for Ar name
724.Oo in Ar word No ... Oc ;
725.No do Ar list ; No done
726.Xc
727For each
728.Ar word
729in the specified word list, the parameter
730.Ar name
731is set to the word and
732.Ar list
733is executed.
734If
735.Ic in
Elliott Hughes966dd552016-12-08 15:56:04 -0800736is not used to specify a word list, the positional parameters ($1, $2,
737etc.) are used instead.
Geremy Condra03ebf062011-10-12 18:17:24 -0700738For historical reasons, open and close braces may be used instead of
739.Ic do
740and
741.Ic done
742e.g.\&
743.Ic for i; { echo $i; } .
744The exit status of a
745.Ic for
746statement is the last exit status of
747.Ar list ;
748if
749.Ar list
750is never executed, the exit status is zero.
751.It Xo if Ar list ;
752.No then Ar list ;
753.Oo elif Ar list ;
754.No then Ar list ; Oc
755.No ...
756.Oo else Ar list ; Oc
757.No fi
758.Xc
759If the exit status of the first
760.Ar list
761is zero, the second
762.Ar list
763is executed; otherwise, the
764.Ar list
765following the
766.Ic elif ,
767if any, is executed with similar consequences.
768If all the lists following the
769.Ic if
770and
771.Ic elif Ns s
772fail (i.e. exit with non-zero status), the
773.Ar list
774following the
775.Ic else
776is executed.
777The exit status of an
778.Ic if
779statement is that of non-conditional
780.Ar list
781that is executed; if no non-conditional
782.Ar list
783is executed, the exit status is zero.
784.It Xo select Ar name
785.Oo in Ar word No ... Oc ;
786.No do Ar list ; No done
787.Xc
788The
789.Ic select
790statement provides an automatic method of presenting the user with a menu and
791selecting from it.
792An enumerated list of the specified
793.Ar word Ns (s)
794is printed on standard error, followed by a prompt
795.Po
Elliott Hughes966dd552016-12-08 15:56:04 -0800796.Ev PS3 :
797normally
798.Dq Li #?\ \&
Geremy Condra03ebf062011-10-12 18:17:24 -0700799.Pc .
800A number corresponding to one of the enumerated words is then read from
801standard input,
802.Ar name
803is set to the selected word (or unset if the selection is not valid),
804.Ev REPLY
805is set to what was read (leading/trailing space is stripped), and
806.Ar list
807is executed.
808If a blank line (i.e. zero or more
809.Ev IFS
810octets) is entered, the menu is reprinted without executing
811.Ar list .
812.Pp
813When
814.Ar list
815completes, the enumerated list is printed if
816.Ev REPLY
Elliott Hughes966dd552016-12-08 15:56:04 -0800817is empty, the prompt is printed, and so on.
Geremy Condra03ebf062011-10-12 18:17:24 -0700818This process continues until an end-of-file
819is read, an interrupt is received, or a
820.Ic break
821statement is executed inside the loop.
822If
Elliott Hughes966dd552016-12-08 15:56:04 -0800823.Dq in Ar word ...
Geremy Condra03ebf062011-10-12 18:17:24 -0700824is omitted, the positional parameters are used
825(i.e. $1, $2, etc.).
826For historical reasons, open and close braces may be used instead of
827.Ic do
828and
829.Ic done
830e.g.\&
831.Ic select i; { echo $i; } .
832The exit status of a
833.Ic select
834statement is zero if a
835.Ic break
836statement is used to exit the loop, non-zero otherwise.
837.It Xo until Ar list ;
838.No do Ar list ;
839.No done
840.Xc
841This works like
842.Ic while ,
843except that the body is executed only while the exit status of the first
844.Ar list
845is non-zero.
846.It Xo while Ar list ;
847.No do Ar list ;
848.No done
849.Xc
850A
851.Ic while
852is a pre-checked loop.
853Its body is executed as often as the exit status of the first
854.Ar list
855is zero.
856The exit status of a
857.Ic while
858statement is the last exit status of the
859.Ar list
860in the body of the loop; if the body is not executed, the exit status is zero.
861.It Xo function Ar name
862.No { Ar list ; No }
863.Xc
864Defines the function
865.Ar name
866(see
867.Sx Functions
868below).
869Note that redirections specified after a function definition are
870performed whenever the function is executed, not when the function definition
871is executed.
872.It Ar name Ns \&() Ar command
873Mostly the same as
874.Ic function
875(see
876.Sx Functions
877below).
878Whitespace (space or tab) after
879.Ar name
880will be ignored most of the time.
881.It Xo function Ar name Ns \&()
882.No { Ar list ; No }
883.Xc
884The same as
885.Ar name Ns \&()
886.Pq Nm bash Ns ism .
887The
888.Ic function
889keyword is ignored.
890.It Xo Ic time Op Fl p
891.Op Ar pipeline
892.Xc
893The
894.Sx Command execution
895section describes the
896.Ic time
897reserved word.
898.It \&(( Ar expression No ))
899The arithmetic expression
900.Ar expression
901is evaluated; equivalent to
Elliott Hughes966dd552016-12-08 15:56:04 -0800902.Dq Li let \&" Ns Ar expression Ns \&"
Geremy Condra03ebf062011-10-12 18:17:24 -0700903(see
904.Sx Arithmetic expressions
905and the
906.Ic let
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800907command, below) in a compound construct.
Geremy Condra03ebf062011-10-12 18:17:24 -0700908.It Bq Bq Ar \ \&expression\ \&
909Similar to the
910.Ic test
911and
912.Ic \&[ ... \&]
913commands (described later), with the following exceptions:
914.Bl -bullet
915.It
916Field splitting and file name generation are not performed on arguments.
917.It
918The
919.Fl a
920.Pq AND
921and
922.Fl o
923.Pq OR
924operators are replaced with
Elliott Hughes966dd552016-12-08 15:56:04 -0800925.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700926and
Elliott Hughes966dd552016-12-08 15:56:04 -0800927.Dq Li \*(Ba\*(Ba ,
Geremy Condra03ebf062011-10-12 18:17:24 -0700928respectively.
929.It
930Operators (e.g.\&
Elliott Hughes966dd552016-12-08 15:56:04 -0800931.Dq Li \-f ,
932.Dq Li = ,
933.Dq Li \&! )
Geremy Condra03ebf062011-10-12 18:17:24 -0700934must be unquoted.
935.It
Elliott Hughes966dd552016-12-08 15:56:04 -0800936Parameter, command and arithmetic substitutions are performed as expressions
Geremy Condra03ebf062011-10-12 18:17:24 -0700937are evaluated and lazy expression evaluation is used for the
Elliott Hughes966dd552016-12-08 15:56:04 -0800938.Dq Li &&
Geremy Condra03ebf062011-10-12 18:17:24 -0700939and
Elliott Hughes966dd552016-12-08 15:56:04 -0800940.Dq Li \*(Ba\*(Ba
Geremy Condra03ebf062011-10-12 18:17:24 -0700941operators.
942This means that in the following statement,
943.Ic $(\*(Ltfoo)
944is evaluated if and only if the file
945.Pa foo
946exists and is readable:
947.Bd -literal -offset indent
948$ [[ \-r foo && $(\*(Ltfoo) = b*r ]]
949.Ed
950.It
951The second operand of the
Elliott Hughes966dd552016-12-08 15:56:04 -0800952.Dq Li !=
Geremy Condra03ebf062011-10-12 18:17:24 -0700953and
Elliott Hughes966dd552016-12-08 15:56:04 -0800954.Dq Li =
Elliott Hughes737fdce2014-08-07 12:59:26 -0700955expressions are a subset of patterns (e.g. the comparison
Geremy Condra03ebf062011-10-12 18:17:24 -0700956.Ic \&[[ foobar = f*r ]]
957succeeds).
958This even works indirectly:
959.Bd -literal -offset indent
960$ bar=foobar; baz=\*(aqf*r\*(aq
961$ [[ $bar = $baz ]]; echo $?
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000962$ [[ $bar = \&"$baz" ]]; echo $?
Geremy Condra03ebf062011-10-12 18:17:24 -0700963.Ed
964.Pp
965Perhaps surprisingly, the first comparison succeeds,
966whereas the second doesn't.
Elliott Hughes737fdce2014-08-07 12:59:26 -0700967This does not apply to all extglob metacharacters, currently.
Geremy Condra03ebf062011-10-12 18:17:24 -0700968.El
969.El
970.Ss Quoting
971Quoting is used to prevent the shell from treating characters or words
972specially.
973There are three methods of quoting.
974First,
975.Ql \e
976quotes the following character, unless it is at the end of a line, in which
977case both the
978.Ql \e
979and the newline are stripped.
980Second, a single quote
Elliott Hughes966dd552016-12-08 15:56:04 -0800981.Pq Dq Li \*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -0700982quotes everything up to the next single quote (this may span lines).
983Third, a double quote
Elliott Hughes966dd552016-12-08 15:56:04 -0800984.Pq Ql \&"
Geremy Condra03ebf062011-10-12 18:17:24 -0700985quotes all characters, except
986.Ql $ ,
Elliott Hughes966dd552016-12-08 15:56:04 -0800987.Ql \e
Elliott Hughes77740fc2016-08-12 15:06:53 -0700988and
989.Ql \` ,
990up to the next unescaped double quote.
Geremy Condra03ebf062011-10-12 18:17:24 -0700991.Ql $
992and
993.Ql \`
Elliott Hughes966dd552016-12-08 15:56:04 -0800994inside double quotes have their usual meaning (i.e. parameter, arithmetic
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800995or command substitution) except no field splitting is carried out on the
Elliott Hughes77740fc2016-08-12 15:06:53 -0700996results of double-quoted substitutions, and the old-style form of command
997substitution has backslash-quoting for double quotes enabled.
Geremy Condra03ebf062011-10-12 18:17:24 -0700998If a
999.Ql \e
1000inside a double-quoted string is followed by
Geremy Condra03ebf062011-10-12 18:17:24 -07001001.Ql \&" ,
Elliott Hughes77740fc2016-08-12 15:06:53 -07001002.Ql $ ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001003.Ql \e
Elliott Hughes77740fc2016-08-12 15:06:53 -07001004or
1005.Ql \` ,
1006only the
1007.Ql \e
1008is removed, i.e. the combination is replaced by the second character;
1009if it is followed by a newline, both the
Geremy Condra03ebf062011-10-12 18:17:24 -07001010.Ql \e
1011and the newline are stripped; otherwise, both the
1012.Ql \e
1013and the character following are unchanged.
1014.Pp
1015If a single-quoted string is preceded by an unquoted
1016.Ql $ ,
1017C style backslash expansion (see below) is applied (even single quote
1018characters inside can be escaped and do not terminate the string then);
1019the expanded result is treated as any other single-quoted string.
1020If a double-quoted string is preceded by an unquoted
1021.Ql $ ,
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001022the
1023.Ql $
1024is simply ignored.
Geremy Condra03ebf062011-10-12 18:17:24 -07001025.Ss Backslash expansion
1026In places where backslashes are expanded, certain C and
1027.At
1028.Nm ksh
1029or GNU
1030.Nm bash
1031style escapes are translated.
1032These include
Elliott Hughes966dd552016-12-08 15:56:04 -08001033.Dq Li \ea ,
1034.Dq Li \eb ,
1035.Dq Li \ef ,
1036.Dq Li \en ,
1037.Dq Li \er ,
1038.Dq Li \et ,
1039.Dq Li \eU######## ,
1040.Dq Li \eu####
Geremy Condra03ebf062011-10-12 18:17:24 -07001041and
Elliott Hughes966dd552016-12-08 15:56:04 -08001042.Dq Li \ev .
Geremy Condra03ebf062011-10-12 18:17:24 -07001043For
Elliott Hughes966dd552016-12-08 15:56:04 -08001044.Dq Li \eU########
Geremy Condra03ebf062011-10-12 18:17:24 -07001045and
Elliott Hughes966dd552016-12-08 15:56:04 -08001046.Dq Li \eu#### ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001047.Dq #
Elliott Hughes966dd552016-12-08 15:56:04 -08001048means a hexadecimal digit, of which there may be none up to four or eight;
Geremy Condra03ebf062011-10-12 18:17:24 -07001049these escapes translate a Unicode codepoint to UTF-8.
1050Furthermore,
Elliott Hughes966dd552016-12-08 15:56:04 -08001051.Dq Li \eE
Geremy Condra03ebf062011-10-12 18:17:24 -07001052and
Elliott Hughes966dd552016-12-08 15:56:04 -08001053.Dq Li \ee
Geremy Condra03ebf062011-10-12 18:17:24 -07001054expand to the escape character.
1055.Pp
1056In the
1057.Ic print
1058builtin mode,
Elliott Hughes966dd552016-12-08 15:56:04 -08001059.Dq Li \e" ,
1060.Dq Li \e\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07001061and
Elliott Hughes966dd552016-12-08 15:56:04 -08001062.Dq Li \e?
Geremy Condra03ebf062011-10-12 18:17:24 -07001063are explicitly excluded;
1064octal sequences must have the none up to three octal digits
1065.Dq #
1066prefixed with the digit zero
Elliott Hughes966dd552016-12-08 15:56:04 -08001067.Pq Dq Li \e0### ;
Geremy Condra03ebf062011-10-12 18:17:24 -07001068hexadecimal sequences
Elliott Hughes966dd552016-12-08 15:56:04 -08001069.Dq Li \ex##
Geremy Condra03ebf062011-10-12 18:17:24 -07001070are limited to none up to two hexadecimal digits
1071.Dq # ;
1072both octal and hexadecimal sequences convert to raw octets;
Elliott Hughes966dd552016-12-08 15:56:04 -08001073.Dq Li \e# ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001074where # is none of the above, translates to \e# (backslashes are retained).
1075.Pp
1076Backslash expansion in the C style mode slightly differs: octal sequences
Elliott Hughes966dd552016-12-08 15:56:04 -08001077.Dq Li \e###
Geremy Condra03ebf062011-10-12 18:17:24 -07001078must have no digit zero prefixing the one up to three octal digits
1079.Dq #
1080and yield raw octets; hexadecimal sequences
Elliott Hughes966dd552016-12-08 15:56:04 -08001081.Dq Li \ex#*
Geremy Condra03ebf062011-10-12 18:17:24 -07001082greedily eat up as many hexadecimal digits
1083.Dq #
1084as they can and terminate with the first non-hexadecimal digit;
1085these translate a Unicode codepoint to UTF-8.
1086The sequence
Elliott Hughes966dd552016-12-08 15:56:04 -08001087.Dq Li \ec# ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001088where
1089.Dq #
1090is any octet, translates to Ctrl-# (which basically means,
Elliott Hughes966dd552016-12-08 15:56:04 -08001091.Dq Li \ec?
Geremy Condra03ebf062011-10-12 18:17:24 -07001092becomes DEL, everything else is bitwise ANDed with 0x1F).
1093Finally,
Elliott Hughes966dd552016-12-08 15:56:04 -08001094.Dq Li \e# ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001095where # is none of the above, translates to # (has the backslash trimmed),
1096even if it is a newline.
1097.Ss Aliases
1098There are two types of aliases: normal command aliases and tracked aliases.
1099Command aliases are normally used as a short hand for a long or often used
1100command.
1101The shell expands command aliases (i.e. substitutes the alias name
1102for its value) when it reads the first word of a command.
1103An expanded alias is re-processed to check for more aliases.
1104If a command alias ends in a
1105space or tab, the following word is also checked for alias expansion.
1106The alias expansion process stops when a word that is not an alias is found,
1107when a quoted word is found, or when an alias word that is currently being
1108expanded is found.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001109Aliases are specifically an interactive feature: while they do happen
1110to work in scripts and on the command line in some cases, aliases are
1111expanded during lexing, so their use must be in a separate command tree
1112from their definition; otherwise, the alias will not be found.
1113Noticeably, command lists (separated by semicolon, in command substitutions
1114also by newline) may be one same parse tree.
Geremy Condra03ebf062011-10-12 18:17:24 -07001115.Pp
1116The following command aliases are defined automatically by the shell:
1117.Bd -literal -offset indent
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001118autoload=\*(aq\e\ebuiltin typeset \-fu\*(aq
1119functions=\*(aq\e\ebuiltin typeset \-f\*(aq
1120hash=\*(aq\e\ebuiltin alias \-t\*(aq
1121history=\*(aq\e\ebuiltin fc \-l\*(aq
1122integer=\*(aq\e\ebuiltin typeset \-i\*(aq
1123local=\*(aq\e\ebuiltin typeset\*(aq
1124login=\*(aq\e\ebuiltin exec login\*(aq
1125nameref=\*(aq\e\ebuiltin typeset \-n\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07001126nohup=\*(aqnohup \*(aq
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001127r=\*(aq\e\ebuiltin fc \-e \-\*(aq
1128type=\*(aq\e\ebuiltin whence \-v\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07001129.Ed
1130.Pp
1131Tracked aliases allow the shell to remember where it found a particular
1132command.
1133The first time the shell does a path search for a command that is
1134marked as a tracked alias, it saves the full path of the command.
1135The next
1136time the command is executed, the shell checks the saved path to see that it
1137is still valid, and if so, avoids repeating the path search.
1138Tracked aliases can be listed and created using
Elliott Hughes96b43632015-07-17 11:39:41 -07001139.Ic alias Fl t .
Geremy Condra03ebf062011-10-12 18:17:24 -07001140Note that changing the
1141.Ev PATH
1142parameter clears the saved paths for all tracked aliases.
1143If the
1144.Ic trackall
1145option is set (i.e.\&
Elliott Hughes96b43632015-07-17 11:39:41 -07001146.Ic set Fl o Ic trackall
Geremy Condra03ebf062011-10-12 18:17:24 -07001147or
Elliott Hughes96b43632015-07-17 11:39:41 -07001148.Ic set Fl h ) ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001149the shell tracks all commands.
1150This option is set automatically for non-interactive shells.
1151For interactive shells, only the following commands are
1152automatically tracked:
1153.Xr cat 1 ,
1154.Xr cc 1 ,
1155.Xr chmod 1 ,
1156.Xr cp 1 ,
1157.Xr date 1 ,
1158.Xr ed 1 ,
1159.Xr emacs 1 ,
1160.Xr grep 1 ,
1161.Xr ls 1 ,
1162.Xr make 1 ,
1163.Xr mv 1 ,
1164.Xr pr 1 ,
1165.Xr rm 1 ,
1166.Xr sed 1 ,
1167.Xr sh 1 ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001168.Xr vi 1
Geremy Condra03ebf062011-10-12 18:17:24 -07001169and
1170.Xr who 1 .
1171.Ss Substitution
1172The first step the shell takes in executing a simple-command is to perform
1173substitutions on the words of the command.
1174There are three kinds of
Elliott Hughes966dd552016-12-08 15:56:04 -08001175substitution: parameter, command and arithmetic.
Geremy Condra03ebf062011-10-12 18:17:24 -07001176Parameter substitutions,
1177which are described in detail in the next section, take the form
1178.Pf $ Ns Ar name
1179or
1180.Pf ${ Ns Ar ... Ns } ;
1181command substitutions take the form
1182.Pf $( Ns Ar command Ns \&)
1183or (deprecated)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001184.Pf \` Ns Ar command Ns \`
1185or (executed in the current environment)
1186.Pf ${\ \& Ar command Ns \&;}
1187and strip trailing newlines;
Geremy Condra03ebf062011-10-12 18:17:24 -07001188and arithmetic substitutions take the form
1189.Pf $(( Ns Ar expression Ns )) .
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001190Parsing the current-environment command substitution requires a space,
1191tab or newline after the opening brace and that the closing brace be
1192recognised as a keyword (i.e. is preceded by a newline or semicolon).
1193They are also called funsubs (function substitutions) and behave like
1194functions in that
1195.Ic local
1196and
1197.Ic return
1198work, and in that
1199.Ic exit
Elliott Hughesb27ce952015-04-21 13:39:18 -07001200terminates the parent shell; shell options are shared.
Geremy Condra03ebf062011-10-12 18:17:24 -07001201.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00001202Another variant of substitution are the valsubs (value substitutions)
1203.Pf ${\*(Ba\& Ns Ar command Ns \&;}
1204which are also executed in the current environment, like funsubs, but
1205share their I/O with the parent; instead, they evaluate to whatever
1206the, initially empty, expression-local variable
1207.Ev REPLY
1208is set to within the
Elliott Hughes96b43632015-07-17 11:39:41 -07001209.Ar command Ns s .
Thorsten Glaser811a5752013-07-25 14:24:45 +00001210.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07001211If a substitution appears outside of double quotes, the results of the
1212substitution are generally subject to word or field splitting according to
1213the current value of the
1214.Ev IFS
1215parameter.
1216The
1217.Ev IFS
1218parameter specifies a list of octets which are used to break a string up
Elliott Hughes966dd552016-12-08 15:56:04 -08001219into several words; any octets from the set space, tab and newline that
Geremy Condra03ebf062011-10-12 18:17:24 -07001220appear in the
1221.Ev IFS
1222octets are called
1223.Dq IFS whitespace .
1224Sequences of one or more
1225.Ev IFS
1226whitespace octets, in combination with zero or one
1227.Pf non- Ev IFS
1228whitespace octets, delimit a field.
1229As a special case, leading and trailing
1230.Ev IFS
Elliott Hughes56b517d2014-10-06 11:30:44 -07001231whitespace is stripped (i.e. no leading or trailing empty field
1232is created by it); leading or trailing
Geremy Condra03ebf062011-10-12 18:17:24 -07001233.Pf non- Ev IFS
1234whitespace does create an empty field.
1235.Pp
1236Example: If
1237.Ev IFS
1238is set to
Elliott Hughes966dd552016-12-08 15:56:04 -08001239.Dq Li \*(Ltspace\*(Gt:
Geremy Condra03ebf062011-10-12 18:17:24 -07001240and VAR is set to
Elliott Hughes966dd552016-12-08 15:56:04 -08001241.Dq Li \*(Ltspace\*(GtA\*(Ltspace\*(Gt:\*(Ltspace\*(Gt\*(Ltspace\*(GtB::D ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001242the substitution for $VAR results in four fields:
Elliott Hughes966dd552016-12-08 15:56:04 -08001243.Dq Li A ,
1244.Dq Li B ,
1245.Dq
1246(an empty field) and
1247.Dq Li D .
Geremy Condra03ebf062011-10-12 18:17:24 -07001248Note that if the
1249.Ev IFS
Elliott Hughesb27ce952015-04-21 13:39:18 -07001250parameter is set to the empty string, no field splitting is done;
Elliott Hughes966dd552016-12-08 15:56:04 -08001251if it is unset, the default value of space, tab and newline is used.
Geremy Condra03ebf062011-10-12 18:17:24 -07001252.Pp
1253Also, note that the field splitting applies only to the immediate result of
1254the substitution.
1255Using the previous example, the substitution for $VAR:E
1256results in the fields:
Elliott Hughes966dd552016-12-08 15:56:04 -08001257.Dq Li A ,
1258.Dq Li B ,
1259.Dq
Geremy Condra03ebf062011-10-12 18:17:24 -07001260and
Elliott Hughes966dd552016-12-08 15:56:04 -08001261.Dq Li D:E ,
Geremy Condra03ebf062011-10-12 18:17:24 -07001262not
Elliott Hughes966dd552016-12-08 15:56:04 -08001263.Dq Li A ,
1264.Dq Li B ,
1265.Dq ,
1266.Dq Li D
Geremy Condra03ebf062011-10-12 18:17:24 -07001267and
Elliott Hughes966dd552016-12-08 15:56:04 -08001268.Dq Li E .
Geremy Condra03ebf062011-10-12 18:17:24 -07001269This behavior is POSIX compliant, but incompatible with some other shell
1270implementations which do field splitting on the word which contained the
1271substitution or use
1272.Dv IFS
1273as a general whitespace delimiter.
1274.Pp
1275The results of substitution are, unless otherwise specified, also subject to
1276brace expansion and file name expansion (see the relevant sections below).
1277.Pp
1278A command substitution is replaced by the output generated by the specified
1279command which is run in a subshell.
1280For
1281.Pf $( Ns Ar command Ns \&)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001282and
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001283.Pf ${\*(Ba\& Ns Ar command Ns \&;}
1284and
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001285.Pf ${\ \& Ar command Ns \&;}
Geremy Condra03ebf062011-10-12 18:17:24 -07001286substitutions, normal quoting rules are used when
1287.Ar command
1288is parsed; however, for the deprecated
1289.Pf \` Ns Ar command Ns \`
1290form, a
1291.Ql \e
1292followed by any of
1293.Ql $ ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001294.Ql \`
Geremy Condra03ebf062011-10-12 18:17:24 -07001295or
1296.Ql \e
Elliott Hughes77740fc2016-08-12 15:06:53 -07001297is stripped (as is
1298.Ql \&"
1299when the substitution is part of a double-quoted string); a backslash
Geremy Condra03ebf062011-10-12 18:17:24 -07001300.Ql \e
Elliott Hughes77740fc2016-08-12 15:06:53 -07001301followed by any other character is unchanged.
Geremy Condra03ebf062011-10-12 18:17:24 -07001302As a special case in command substitutions, a command of the form
1303.Pf \*(Lt Ar file
1304is interpreted to mean substitute the contents of
1305.Ar file .
1306Note that
1307.Ic $(\*(Ltfoo)
1308has the same effect as
1309.Ic $(cat foo) .
1310.Pp
1311Note that some shells do not use a recursive parser for command substitutions,
1312leading to failure for certain constructs; to be portable, use as workaround
Elliott Hughes966dd552016-12-08 15:56:04 -08001313.Dq Li x=$(cat) \*(Lt\*(Lt\eEOF
Geremy Condra03ebf062011-10-12 18:17:24 -07001314(or the newline-keeping
Elliott Hughes966dd552016-12-08 15:56:04 -08001315.Dq Li x=\*(Lt\*(Lt\eEOF
Geremy Condra03ebf062011-10-12 18:17:24 -07001316extension) instead to merely slurp the string.
1317.St -p1003.1
Elliott Hughes966dd552016-12-08 15:56:04 -08001318recommends using case statements of the form
1319.Li "x=$(case $foo in (bar) echo $bar ;; (*) echo $baz ;; esac)"
Geremy Condra03ebf062011-10-12 18:17:24 -07001320instead, which would work but not serve as example for this portability issue.
1321.Bd -literal -offset indent
1322x=$(case $foo in bar) echo $bar ;; *) echo $baz ;; esac)
1323# above fails to parse on old shells; below is the workaround
Elliott Hughes966dd552016-12-08 15:56:04 -08001324x=$(eval $(cat)) \*(Lt\*(Lt\eEOF
Geremy Condra03ebf062011-10-12 18:17:24 -07001325case $foo in bar) echo $bar ;; *) echo $baz ;; esac
1326EOF
1327.Ed
1328.Pp
1329Arithmetic substitutions are replaced by the value of the specified expression.
1330For example, the command
1331.Ic print $((2+3*4))
1332displays 14.
1333See
1334.Sx Arithmetic expressions
1335for a description of an expression.
1336.Ss Parameters
1337Parameters are shell variables; they can be assigned values and their values
1338can be accessed using a parameter substitution.
1339A parameter name is either one
1340of the special single punctuation or digit character parameters described
1341below, or a letter followed by zero or more letters or digits
1342.Po
1343.Ql _
1344counts as a letter
1345.Pc .
1346The latter form can be treated as arrays by appending an array index of the
1347form
1348.Op Ar expr
1349where
1350.Ar expr
1351is an arithmetic expression.
1352Array indices in
1353.Nm
1354are limited to the range 0 through 4294967295, inclusive.
1355That is, they are a 32-bit unsigned integer.
1356.Pp
1357Parameter substitutions take the form
1358.Pf $ Ns Ar name ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001359.Pf ${ Ns Ar name Ns }
Geremy Condra03ebf062011-10-12 18:17:24 -07001360or
1361.Sm off
1362.Pf ${ Ar name Oo Ar expr Oc }
1363.Sm on
1364where
1365.Ar name
1366is a parameter name.
1367Substitution of all array elements with
1368.Pf ${ Ns Ar name Ns \&[*]}
1369and
1370.Pf ${ Ns Ar name Ns \&[@]}
1371works equivalent to $* and $@ for positional parameters.
1372If substitution is performed on a parameter
1373(or an array parameter element)
Elliott Hughes966dd552016-12-08 15:56:04 -08001374that is not set, an empty string is substituted unless the
Geremy Condra03ebf062011-10-12 18:17:24 -07001375.Ic nounset
1376option
Elliott Hughes966dd552016-12-08 15:56:04 -08001377.Pq Ic set Fl u
Geremy Condra03ebf062011-10-12 18:17:24 -07001378is set, in which case an error occurs.
1379.Pp
1380Parameters can be assigned values in a number of ways.
1381First, the shell implicitly sets some parameters like
Elliott Hughes966dd552016-12-08 15:56:04 -08001382.Dq Li # ,
1383.Dq Li PWD
Geremy Condra03ebf062011-10-12 18:17:24 -07001384and
Elliott Hughes966dd552016-12-08 15:56:04 -08001385.Dq Li $ ;
Geremy Condra03ebf062011-10-12 18:17:24 -07001386this is the only way the special single character parameters are set.
1387Second, parameters are imported from the shell's environment at startup.
1388Third, parameters can be assigned values on the command line: for example,
1389.Ic FOO=bar
1390sets the parameter
Elliott Hughes966dd552016-12-08 15:56:04 -08001391.Dq Li FOO
Geremy Condra03ebf062011-10-12 18:17:24 -07001392to
Elliott Hughes966dd552016-12-08 15:56:04 -08001393.Dq Li bar ;
Geremy Condra03ebf062011-10-12 18:17:24 -07001394multiple parameter assignments can be given on a single command line and they
1395can be followed by a simple-command, in which case the assignments are in
1396effect only for the duration of the command (such assignments are also
1397exported; see below for the implications of this).
1398Note that both the parameter name and the
1399.Ql =
1400must be unquoted for the shell to recognise a parameter assignment.
1401The construct
1402.Ic FOO+=baz
1403is also recognised; the old and new values are immediately concatenated.
1404The fourth way of setting a parameter is with the
1405.Ic export ,
1406.Ic global ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001407.Ic readonly
Geremy Condra03ebf062011-10-12 18:17:24 -07001408and
1409.Ic typeset
1410commands; see their descriptions in the
1411.Sx Command execution
1412section.
1413Fifth,
1414.Ic for
1415and
1416.Ic select
1417loops set parameters as well as the
1418.Ic getopts ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001419.Ic read
Geremy Condra03ebf062011-10-12 18:17:24 -07001420and
Elliott Hughes96b43632015-07-17 11:39:41 -07001421.Ic set Fl A
Geremy Condra03ebf062011-10-12 18:17:24 -07001422commands.
1423Lastly, parameters can be assigned values using assignment operators
1424inside arithmetic expressions (see
1425.Sx Arithmetic expressions
1426below) or using the
1427.Sm off
1428.Pf ${ Ar name No = Ar value No }
1429.Sm on
1430form of the parameter substitution (see below).
1431.Pp
1432Parameters with the export attribute (set using the
1433.Ic export
1434or
1435.Ic typeset Fl x
1436commands, or by parameter assignments followed by simple commands) are put in
1437the environment (see
1438.Xr environ 7 )
1439of commands run by the shell as
1440.Ar name Ns = Ns Ar value
1441pairs.
1442The order in which parameters appear in the environment of a command is
1443unspecified.
1444When the shell starts up, it extracts parameters and their values
1445from its environment and automatically sets the export attribute for those
1446parameters.
1447.Pp
1448Modifiers can be applied to the
1449.Pf ${ Ns Ar name Ns }
1450form of parameter substitution:
1451.Bl -tag -width Ds
1452.Sm off
1453.It ${ Ar name No :\- Ar word No }
1454.Sm on
1455If
1456.Ar name
Elliott Hughes966dd552016-12-08 15:56:04 -08001457is set and not empty,
Geremy Condra03ebf062011-10-12 18:17:24 -07001458it is substituted; otherwise,
1459.Ar word
1460is substituted.
1461.Sm off
1462.It ${ Ar name No :+ Ar word No }
1463.Sm on
1464If
1465.Ar name
Elliott Hughes966dd552016-12-08 15:56:04 -08001466is set and not empty,
Geremy Condra03ebf062011-10-12 18:17:24 -07001467.Ar word
1468is substituted; otherwise, nothing is substituted.
1469.Sm off
1470.It ${ Ar name No := Ar word No }
1471.Sm on
1472If
1473.Ar name
Elliott Hughes966dd552016-12-08 15:56:04 -08001474is set and not empty,
Geremy Condra03ebf062011-10-12 18:17:24 -07001475it is substituted; otherwise, it is assigned
1476.Ar word
1477and the resulting value of
1478.Ar name
1479is substituted.
1480.Sm off
1481.It ${ Ar name No :? Ar word No }
1482.Sm on
1483If
1484.Ar name
Elliott Hughes966dd552016-12-08 15:56:04 -08001485is set and not empty,
Geremy Condra03ebf062011-10-12 18:17:24 -07001486it is substituted; otherwise,
1487.Ar word
1488is printed on standard error (preceded by
1489.Ar name : )
1490and an error occurs (normally causing termination of a shell script, function,
Elliott Hughes966dd552016-12-08 15:56:04 -08001491or a script sourced using the
1492.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07001493built-in).
1494If
1495.Ar word
1496is omitted, the string
Elliott Hughes966dd552016-12-08 15:56:04 -08001497.Dq Li parameter null or not set
Geremy Condra03ebf062011-10-12 18:17:24 -07001498is used instead.
Geremy Condra03ebf062011-10-12 18:17:24 -07001499.El
1500.Pp
1501Note that, for all of the above,
1502.Ar word
1503is actually considered quoted, and special parsing rules apply.
1504The parsing rules also differ on whether the expression is double-quoted:
1505.Ar word
1506then uses double-quoting rules, except for the double quote itself
Elliott Hughes966dd552016-12-08 15:56:04 -08001507.Pq Ql \&"
Geremy Condra03ebf062011-10-12 18:17:24 -07001508and the closing brace, which, if backslash escaped, gets quote removal applied.
1509.Pp
1510In the above modifiers, the
1511.Ql \&:
1512can be omitted, in which case the conditions only depend on
1513.Ar name
Elliott Hughes966dd552016-12-08 15:56:04 -08001514being set (as opposed to set and not empty).
Geremy Condra03ebf062011-10-12 18:17:24 -07001515If
1516.Ar word
Elliott Hughes966dd552016-12-08 15:56:04 -08001517is needed, parameter, command, arithmetic and tilde substitution are performed
Geremy Condra03ebf062011-10-12 18:17:24 -07001518on it; if
1519.Ar word
1520is not needed, it is not evaluated.
1521.Pp
1522The following forms of parameter substitution can also be used (if
1523.Ar name
Elliott Hughes96b43632015-07-17 11:39:41 -07001524is an array, the element with the key
1525.Dq 0
1526will be substituted in scalar context):
Geremy Condra03ebf062011-10-12 18:17:24 -07001527.Pp
1528.Bl -tag -width Ds -compact
1529.It Pf ${# Ns Ar name Ns \&}
1530The number of positional parameters if
1531.Ar name
1532is
Elliott Hughes966dd552016-12-08 15:56:04 -08001533.Dq Li * ,
1534.Dq Li @
Geremy Condra03ebf062011-10-12 18:17:24 -07001535or not specified; otherwise the length
1536.Pq in characters
1537of the string value of parameter
1538.Ar name .
1539.Pp
1540.It Pf ${# Ns Ar name Ns \&[*]}
1541.It Pf ${# Ns Ar name Ns \&[@]}
1542The number of elements in the array
1543.Ar name .
1544.Pp
1545.It Pf ${% Ns Ar name Ns \&}
1546The width
1547.Pq in screen columns
1548of the string value of parameter
1549.Ar name ,
1550or \-1 if
1551.Pf ${ Ns Ar name Ns }
1552contains a control character.
1553.Pp
1554.It Pf ${! Ns Ar name Ns }
1555The name of the variable referred to by
1556.Ar name .
1557This will be
1558.Ar name
1559except when
1560.Ar name
1561is a name reference (bound variable), created by the
1562.Ic nameref
1563command (which is an alias for
1564.Ic typeset Fl n ) .
Elliott Hughes77740fc2016-08-12 15:06:53 -07001565.Ar name
1566cannot be one of most special parameters (see below).
Geremy Condra03ebf062011-10-12 18:17:24 -07001567.Pp
1568.It Pf ${! Ns Ar name Ns \&[*]}
1569.It Pf ${! Ns Ar name Ns \&[@]}
1570The names of indices (keys) in the array
1571.Ar name .
1572.Pp
1573.Sm off
1574.It Xo
1575.Pf ${ Ar name
1576.Pf # Ar pattern No }
1577.Xc
1578.It Xo
1579.Pf ${ Ar name
1580.Pf ## Ar pattern No }
1581.Xc
1582.Sm on
1583If
1584.Ar pattern
1585matches the beginning of the value of parameter
1586.Ar name ,
1587the matched text is deleted from the result of substitution.
1588A single
1589.Ql #
1590results in the shortest match, and two
1591of them result in the longest match.
1592Cannot be applied to a vector
1593.Pq ${*} or ${@} or ${array[*]} or ${array[@]} .
1594.Pp
1595.Sm off
1596.It Xo
1597.Pf ${ Ar name
1598.Pf % Ar pattern No }
1599.Xc
1600.It Xo
1601.Pf ${ Ar name
1602.Pf %% Ar pattern No }
1603.Xc
1604.Sm on
Elliott Hughes966dd552016-12-08 15:56:04 -08001605Like ${...#...} substitution, but it deletes from the end of the value.
Geremy Condra03ebf062011-10-12 18:17:24 -07001606Cannot be applied to a vector.
1607.Pp
1608.Sm off
1609.It Xo
1610.Pf ${ Ar name
1611.Pf / Ar pattern / Ar string No }
1612.Xc
1613.It Xo
1614.Pf ${ Ar name
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001615.Pf /# Ar pattern / Ar string No }
1616.Xc
1617.It Xo
1618.Pf ${ Ar name
1619.Pf /% Ar pattern / Ar string No }
1620.Xc
1621.It Xo
1622.Pf ${ Ar name
Geremy Condra03ebf062011-10-12 18:17:24 -07001623.Pf // Ar pattern / Ar string No }
1624.Xc
1625.Sm on
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001626The longest match of
Geremy Condra03ebf062011-10-12 18:17:24 -07001627.Ar pattern
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001628in the value of parameter
1629.Ar name
1630is replaced with
1631.Ar string
1632(deleted if
1633.Ar string
1634is empty; the trailing slash
1635.Pq Ql /
1636may be omitted in that case).
1637A leading slash followed by
1638.Ql #
1639or
1640.Ql %
1641causes the pattern to be anchored at the beginning or end of
1642the value, respectively; empty unanchored
1643.Ar pattern Ns s
1644cause no replacement; a single leading slash or use of a
Geremy Condra03ebf062011-10-12 18:17:24 -07001645.Ar pattern
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001646that matches the empty string causes the replacement to
1647happen only once; two leading slashes cause all occurrences
1648of matches in the value to be replaced.
Geremy Condra03ebf062011-10-12 18:17:24 -07001649Cannot be applied to a vector.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001650Inefficiently implemented, may be slow.
Geremy Condra03ebf062011-10-12 18:17:24 -07001651.Pp
1652.Sm off
1653.It Xo
Elliott Hughes77740fc2016-08-12 15:06:53 -07001654.Pf ${ Ar name
1655.Pf @/ Ar pattern / Ar string No }
1656.Xc
1657.Sm on
1658The same as
1659.Sm off
1660.Xo
1661.Pf ${ Ar name
1662.Pf // Ar pattern / Ar string No } ,
1663.Xc
1664.Sm on
1665except that both
1666.Ar pattern
1667and
1668.Ar string
1669are expanded anew for each iteration.
1670.Pp
1671.Sm off
1672.It Xo
Geremy Condra03ebf062011-10-12 18:17:24 -07001673.Pf ${ Ar name : Ns Ar pos
1674.Pf : Ns Ar len Ns }
1675.Xc
1676.Sm on
1677The first
1678.Ar len
1679characters of
1680.Ar name ,
1681starting at position
1682.Ar pos ,
1683are substituted.
1684Both
1685.Ar pos
1686and
1687.Pf : Ns Ar len
1688are optional.
1689If
1690.Ar pos
1691is negative, counting starts at the end of the string; if it
1692is omitted, it defaults to 0.
1693If
1694.Ar len
1695is omitted or greater than the length of the remaining string,
1696all of it is substituted.
1697Both
1698.Ar pos
1699and
1700.Ar len
1701are evaluated as arithmetic expressions.
1702Currently,
1703.Ar pos
1704must start with a space, opening parenthesis or digit to be recognised.
1705Cannot be applied to a vector.
1706.Pp
Elliott Hughes737fdce2014-08-07 12:59:26 -07001707.It Pf ${ Ns Ar name Ns @#}
1708The hash (using the BAFH algorithm) of the expansion of
1709.Ar name .
1710This is also used internally for the shell's hashtables.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001711.Pp
1712.It Pf ${ Ns Ar name Ns @Q}
1713A quoted expression safe for re-entry, whose value is the value of the
1714.Ar name
1715parameter, is substituted.
Geremy Condra03ebf062011-10-12 18:17:24 -07001716.El
1717.Pp
1718Note that
1719.Ar pattern
1720may need extended globbing pattern
1721.Pq @(...) ,
1722single
1723.Pq \&\*(aq...\&\*(aq
1724or double
1725.Pq \&"...\&"
1726quote escaping unless
1727.Fl o Ic sh
1728is set.
1729.Pp
1730The following special parameters are implicitly set by the shell and cannot be
1731set directly using assignments:
1732.Bl -tag -width "1 .. 9"
1733.It Ev \&!
1734Process ID of the last background process started.
1735If no background processes have been started, the parameter is not set.
1736.It Ev \&#
1737The number of positional parameters ($1, $2, etc.).
1738.It Ev \&$
Elliott Hughes966dd552016-12-08 15:56:04 -08001739The PID of the shell or, if it is a subshell, the PID of the original shell.
Geremy Condra03ebf062011-10-12 18:17:24 -07001740Do
1741.Em NOT
1742use this mechanism for generating temporary file names; see
1743.Xr mktemp 1
1744instead.
1745.It Ev \-
1746The concatenation of the current single letter options (see the
1747.Ic set
1748command below for a list of options).
1749.It Ev \&?
1750The exit status of the last non-asynchronous command executed.
1751If the last command was killed by a signal,
Elliott Hughes966dd552016-12-08 15:56:04 -08001752.Ic \&$?
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001753is set to 128 plus the signal number, but at most 255.
Geremy Condra03ebf062011-10-12 18:17:24 -07001754.It Ev 0
1755The name of the shell, determined as follows:
1756the first argument to
1757.Nm
1758if it was invoked with the
1759.Fl c
1760option and arguments were given; otherwise the
1761.Ar file
1762argument, if it was supplied;
1763or else the basename the shell was invoked with (i.e.\&
1764.Li argv[0] ) .
1765.Ev $0
1766is also set to the name of the current script or
1767the name of the current function, if it was defined with the
1768.Ic function
1769keyword (i.e. a Korn shell style function).
1770.It Ev 1 No .. Ev 9
1771The first nine positional parameters that were supplied to the shell, function,
1772or script sourced using the
Elliott Hughes966dd552016-12-08 15:56:04 -08001773.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07001774built-in.
1775Further positional parameters may be accessed using
1776.Pf ${ Ar number Ns } .
1777.It Ev *
1778All positional parameters (except 0), i.e. $1, $2, $3, ...
1779.br
1780If used
1781outside of double quotes, parameters are separate words (which are subjected
1782to word splitting); if used within double quotes, parameters are separated
1783by the first character of the
1784.Ev IFS
1785parameter (or the empty string if
1786.Ev IFS
Elliott Hughes966dd552016-12-08 15:56:04 -08001787is unset.
Geremy Condra03ebf062011-10-12 18:17:24 -07001788.It Ev @
1789Same as
1790.Ic $* ,
1791unless it is used inside double quotes, in which case a separate word is
1792generated for each positional parameter.
1793If there are no positional parameters, no word is generated.
Elliott Hughes966dd552016-12-08 15:56:04 -08001794.Ic \&"$@"
Geremy Condra03ebf062011-10-12 18:17:24 -07001795can be used to access arguments, verbatim, without losing
Elliott Hughes966dd552016-12-08 15:56:04 -08001796empty arguments or splitting arguments with spaces (IFS, actually).
Geremy Condra03ebf062011-10-12 18:17:24 -07001797.El
1798.Pp
1799The following parameters are set and/or used by the shell:
1800.Bl -tag -width "KSH_VERSION"
1801.It Ev _
1802.Pq underscore
1803When an external command is executed by the shell, this parameter is set in the
1804environment of the new process to the path of the executed command.
1805In interactive use, this parameter is also set in the parent shell to the last
1806word of the previous command.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001807.It Ev BASHPID
1808The PID of the shell or subshell.
Geremy Condra03ebf062011-10-12 18:17:24 -07001809.It Ev CDPATH
Elliott Hughesb27ce952015-04-21 13:39:18 -07001810Like
1811.Ev PATH ,
1812but used to resolve the argument to the
Geremy Condra03ebf062011-10-12 18:17:24 -07001813.Ic cd
1814built-in command.
Geremy Condra03ebf062011-10-12 18:17:24 -07001815Note that if
1816.Ev CDPATH
1817is set and does not contain
Elliott Hughes966dd552016-12-08 15:56:04 -08001818.Dq Li \&.
Elliott Hughesb27ce952015-04-21 13:39:18 -07001819or an empty string element, the current directory is not searched.
Geremy Condra03ebf062011-10-12 18:17:24 -07001820Also, the
1821.Ic cd
1822built-in command will display the resulting directory when a match is found
1823in any search path other than the empty path.
1824.It Ev COLUMNS
1825Set to the number of columns on the terminal or window.
1826Always set, defaults to 80, unless the
1827value as reported by
1828.Xr stty 1
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001829is non-zero and sane enough (minimum is 12x3); similar for
Geremy Condra03ebf062011-10-12 18:17:24 -07001830.Ev LINES .
Elliott Hughes966dd552016-12-08 15:56:04 -08001831This parameter is used by the interactive line editing modes and by the
Geremy Condra03ebf062011-10-12 18:17:24 -07001832.Ic select ,
Elliott Hughes966dd552016-12-08 15:56:04 -08001833.Ic set Fl o
Geremy Condra03ebf062011-10-12 18:17:24 -07001834and
Elliott Hughes96b43632015-07-17 11:39:41 -07001835.Ic kill Fl l
Geremy Condra03ebf062011-10-12 18:17:24 -07001836commands to format information columns.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001837Importing from the environment or unsetting this parameter removes the
1838binding to the actual terminal size in favour of the provided value.
Geremy Condra03ebf062011-10-12 18:17:24 -07001839.It Ev ENV
1840If this parameter is found to be set after any profile files are executed, the
1841expanded value is used as a shell startup file.
1842It typically contains function and alias definitions.
Elliott Hughes77740fc2016-08-12 15:06:53 -07001843.It Ev EPOCHREALTIME
1844Time since the epoch, as returned by
1845.Xr gettimeofday 2 ,
1846formatted as decimal
1847.Va tv_sec
1848followed by a dot
Elliott Hughes966dd552016-12-08 15:56:04 -08001849.Pq Ql \&.
Elliott Hughes77740fc2016-08-12 15:06:53 -07001850and
1851.Va tv_usec
1852padded to exactly six decimal digits.
Geremy Condra03ebf062011-10-12 18:17:24 -07001853.It Ev EXECSHELL
1854If set, this parameter is assumed to contain the shell that is to be used to
1855execute commands that
1856.Xr execve 2
1857fails to execute and which do not start with a
Elliott Hughes966dd552016-12-08 15:56:04 -08001858.Dq Li #! Ns Ar shell
Geremy Condra03ebf062011-10-12 18:17:24 -07001859sequence.
1860.It Ev FCEDIT
1861The editor used by the
1862.Ic fc
1863command (see below).
1864.It Ev FPATH
1865Like
1866.Ev PATH ,
1867but used when an undefined function is executed to locate the file defining the
1868function.
1869It is also searched when a command can't be found using
1870.Ev PATH .
1871See
1872.Sx Functions
1873below for more information.
1874.It Ev HISTFILE
1875The name of the file used to store command history.
Elliott Hughesb27ce952015-04-21 13:39:18 -07001876When assigned to or unset, the file is opened, history is truncated
Elliott Hughes96b43632015-07-17 11:39:41 -07001877then loaded from the file; subsequent new commands (possibly consisting
1878of several lines) are appended once they successfully compiled.
Geremy Condra03ebf062011-10-12 18:17:24 -07001879Also, several invocations of the shell will share history if their
1880.Ev HISTFILE
1881parameters all point to the same file.
1882.Pp
1883.Sy Note :
1884If
1885.Ev HISTFILE
Elliott Hughesb27ce952015-04-21 13:39:18 -07001886is unset or empty, no history file is used.
Geremy Condra03ebf062011-10-12 18:17:24 -07001887This is different from
1888.At
1889.Nm ksh .
1890.It Ev HISTSIZE
1891The number of commands normally stored for history.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001892The default is 2047.
Elliott Hughes56b517d2014-10-06 11:30:44 -07001893Do not set this value to insanely high values such as 1000000000 because
1894.Nm
1895can then not allocate enough memory for the history and will not start.
Geremy Condra03ebf062011-10-12 18:17:24 -07001896.It Ev HOME
1897The default directory for the
1898.Ic cd
1899command and the value substituted for an unqualified
1900.Ic \*(TI
1901(see
1902.Sx Tilde expansion
1903below).
1904.It Ev IFS
1905Internal field separator, used during substitution and by the
1906.Ic read
Elliott Hughes966dd552016-12-08 15:56:04 -08001907command, to split values into distinct arguments; normally set to space, tab
Geremy Condra03ebf062011-10-12 18:17:24 -07001908and newline.
1909See
1910.Sx Substitution
1911above for details.
1912.Pp
1913.Sy Note :
1914This parameter is not imported from the environment when the shell is
1915started.
1916.It Ev KSHEGID
1917The effective group id of the shell.
1918.It Ev KSHGID
1919The real group id of the shell.
1920.It Ev KSHUID
1921The real user id of the shell.
Elliott Hughes77740fc2016-08-12 15:06:53 -07001922.It Ev KSH_MATCH
1923The last matched string.
1924In a future version, this will be an indexed array,
1925with indexes 1 and up capturing matching groups.
1926Set by string comparisons (== and !=) in double-bracket test
1927expressions when a match is found (when != returns false), by
1928.Ic case
1929when a match is encountered, and by the substitution operations
1930.Sm off
1931.Xo
1932.Pf ${ Ar x
1933.Pf # Ar pat No } ,
1934.Sm on
1935.Xc
1936.Sm off
1937.Xo
1938.Pf ${ Ar x
1939.Pf ## Ar pat No } ,
1940.Sm on
1941.Xc
1942.Sm off
1943.Xo
1944.Pf ${ Ar x
1945.Pf % Ar pat No } ,
1946.Sm on
1947.Xc
1948.Sm off
1949.Xo
1950.Pf ${ Ar x
1951.Pf %% Ar pat No } ,
1952.Sm on
1953.Xc
1954.Sm off
1955.Xo
1956.Pf ${ Ar x
1957.Pf / Ar pat / Ar rpl No } ,
1958.Sm on
1959.Xc
1960.Sm off
1961.Xo
1962.Pf ${ Ar x
1963.Pf /# Ar pat / Ar rpl No } ,
1964.Sm on
1965.Xc
1966.Sm off
1967.Xo
1968.Pf ${ Ar x
1969.Pf /% Ar pat / Ar rpl No } ,
1970.Sm on
1971.Xc
1972.Sm off
1973.Xo
1974.Pf ${ Ar x
1975.Pf // Ar pat / Ar rpl No } ,
1976.Sm on
1977.Xc
1978and
1979.Sm off
1980.Xo
1981.Pf ${ Ar x
1982.Pf @/ Ar pat / Ar rpl No } .
1983.Sm on
1984.Xc
1985See the end of the Emacs editing mode documentation for an example.
Geremy Condra03ebf062011-10-12 18:17:24 -07001986.It Ev KSH_VERSION
1987The name and version of the shell (read-only).
1988See also the version commands in
1989.Sx Emacs editing mode
1990and
1991.Sx Vi editing mode
1992sections, below.
1993.It Ev LINENO
1994The line number of the function or shell script that is currently being
1995executed.
1996.It Ev LINES
1997Set to the number of lines on the terminal or window.
1998Always set, defaults to 24.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001999See
2000.Ev COLUMNS .
Geremy Condra03ebf062011-10-12 18:17:24 -07002001.It Ev OLDPWD
2002The previous working directory.
2003Unset if
2004.Ic cd
Elliott Hughes966dd552016-12-08 15:56:04 -08002005has not successfully changed directories since the shell started or if the
Geremy Condra03ebf062011-10-12 18:17:24 -07002006shell doesn't know where it is.
2007.It Ev OPTARG
2008When using
2009.Ic getopts ,
2010it contains the argument for a parsed option, if it requires one.
2011.It Ev OPTIND
2012The index of the next argument to be processed when using
2013.Ic getopts .
2014Assigning 1 to this parameter causes
2015.Ic getopts
2016to process arguments from the beginning the next time it is invoked.
2017.It Ev PATH
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002018A colon (semicolon on OS/2) separated list of directories that are
2019searched when looking for commands and files sourced using the
Elliott Hughes966dd552016-12-08 15:56:04 -08002020.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002021command (see below).
2022An empty string resulting from a leading or trailing
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002023(semi)colon, or two adjacent ones, is treated as a
Elliott Hughes966dd552016-12-08 15:56:04 -08002024.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002025(the current directory).
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002026.It Ev PATHSEP
2027A colon (semicolon on OS/2), for the user's convenience.
Geremy Condra03ebf062011-10-12 18:17:24 -07002028.It Ev PGRP
2029The process ID of the shell's process group leader.
2030.It Ev PIPESTATUS
2031An array containing the errorlevel (exit status) codes,
2032one by one, of the last pipeline run in the foreground.
2033.It Ev PPID
2034The process ID of the shell's parent.
2035.It Ev PS1
2036The primary prompt for interactive shells.
Elliott Hughes966dd552016-12-08 15:56:04 -08002037Parameter, command and arithmetic
Geremy Condra03ebf062011-10-12 18:17:24 -07002038substitutions are performed, and
2039.Ql \&!
2040is replaced with the current command number (see the
2041.Ic fc
2042command below).
2043A literal
2044.Ql \&!
2045can be put in the prompt by placing
Elliott Hughes966dd552016-12-08 15:56:04 -08002046.Dq Li !!
Geremy Condra03ebf062011-10-12 18:17:24 -07002047in
2048.Ev PS1 .
2049.Pp
2050The default prompt is
Elliott Hughes966dd552016-12-08 15:56:04 -08002051.Dq Li $\ \&
Geremy Condra03ebf062011-10-12 18:17:24 -07002052for non-root users,
Elliott Hughes966dd552016-12-08 15:56:04 -08002053.Dq Li #\ \&
Geremy Condra03ebf062011-10-12 18:17:24 -07002054for root.
2055If
2056.Nm
2057is invoked by root and
2058.Ev PS1
2059does not contain a
Elliott Hughes966dd552016-12-08 15:56:04 -08002060.Ql #
Geremy Condra03ebf062011-10-12 18:17:24 -07002061character, the default value will be used even if
2062.Ev PS1
2063already exists in the environment.
2064.Pp
2065The
2066.Nm
2067distribution comes with a sample
2068.Pa dot.mkshrc
2069containing a sophisticated example, but you might like the following one
2070(note that ${HOSTNAME:=$(hostname)} and the
2071root-vs-user distinguishing clause are (in this example) executed at
2072.Ev PS1
2073assignment time, while the $USER and $PWD are escaped
2074and thus will be evaluated each time a prompt is displayed):
2075.Bd -literal
2076PS1=\*(aq${USER:=$(id \-un)}\*(aq"@${HOSTNAME:=$(hostname)}:\e$PWD $(
2077 if (( USER_ID )); then print \e$; else print \e#; fi) "
2078.Ed
2079.Pp
2080Note that since the command-line editors try to figure out how long the prompt
2081is (so they know how far it is to the edge of the screen), escape codes in
2082the prompt tend to mess things up.
2083You can tell the shell not to count certain
2084sequences (such as escape codes) by prefixing your prompt with a
2085character (such as Ctrl-A) followed by a carriage return and then delimiting
2086the escape codes with this character.
Elliott Hughes96b43632015-07-17 11:39:41 -07002087Any occurrences of that character in the prompt are not printed.
Geremy Condra03ebf062011-10-12 18:17:24 -07002088By the way, don't blame me for
2089this hack; it's derived from the original
2090.Xr ksh88 1 ,
2091which did print the delimiter character so you were out of luck
2092if you did not have any non-printing characters.
2093.Pp
2094Since Backslashes and other special characters may be
2095interpreted by the shell, to set
2096.Ev PS1
Elliott Hughes966dd552016-12-08 15:56:04 -08002097either escape the backslash itself
Geremy Condra03ebf062011-10-12 18:17:24 -07002098or use double quotes.
2099The latter is more practical.
2100This is a more complex example,
2101avoiding to directly enter special characters (for example with
2102.Ic \*(haV
2103in the emacs editing mode),
2104which embeds the current working directory,
2105in reverse video
2106.Pq colour would work, too ,
2107in the prompt string:
2108.Bd -literal -offset indent
2109x=$(print \e\e001)
Elliott Hughes737fdce2014-08-07 12:59:26 -07002110PS1="$x$(print \e\er)$x$(tput so)$x\e$PWD$x$(tput se)$x\*(Gt "
Geremy Condra03ebf062011-10-12 18:17:24 -07002111.Ed
2112.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002113Due to a strong suggestion from David G. Korn,
Geremy Condra03ebf062011-10-12 18:17:24 -07002114.Nm
2115now also supports the following form:
2116.Bd -literal -offset indent
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002117PS1=$\*(aq\e1\er\e1\ee[7m\e1$PWD\e1\ee[0m\e1\*(Gt \*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07002118.Ed
2119.It Ev PS2
2120Secondary prompt string, by default
Elliott Hughes966dd552016-12-08 15:56:04 -08002121.Dq Li \*(Gt\ \& ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002122used when more input is needed to complete a command.
2123.It Ev PS3
2124Prompt used by the
2125.Ic select
2126statement when reading a menu selection.
2127The default is
Elliott Hughes966dd552016-12-08 15:56:04 -08002128.Dq Li #?\ \& .
Geremy Condra03ebf062011-10-12 18:17:24 -07002129.It Ev PS4
2130Used to prefix commands that are printed during execution tracing (see the
2131.Ic set Fl x
2132command below).
Elliott Hughes966dd552016-12-08 15:56:04 -08002133Parameter, command and arithmetic substitutions are performed
Geremy Condra03ebf062011-10-12 18:17:24 -07002134before it is printed.
2135The default is
Elliott Hughes966dd552016-12-08 15:56:04 -08002136.Dq Li +\ \& .
Thorsten Glaser811a5752013-07-25 14:24:45 +00002137You may want to set it to
Elliott Hughes966dd552016-12-08 15:56:04 -08002138.Dq Li \&[$EPOCHREALTIME]\ \&
Thorsten Glaser811a5752013-07-25 14:24:45 +00002139instead, to include timestamps.
Geremy Condra03ebf062011-10-12 18:17:24 -07002140.It Ev PWD
2141The current working directory.
Elliott Hughes966dd552016-12-08 15:56:04 -08002142May be unset or empty if the shell doesn't know where it is.
Geremy Condra03ebf062011-10-12 18:17:24 -07002143.It Ev RANDOM
2144Each time
2145.Ev RANDOM
2146is referenced, it is assigned a number between 0 and 32767 from
2147a Linear Congruential PRNG first.
2148.It Ev REPLY
2149Default parameter for the
2150.Ic read
2151command if no names are given.
2152Also used in
2153.Ic select
2154loops to store the value that is read from standard input.
2155.It Ev SECONDS
2156The number of seconds since the shell started or, if the parameter has been
2157assigned an integer value, the number of seconds since the assignment plus the
2158value that was assigned.
2159.It Ev TMOUT
2160If set to a positive integer in an interactive shell, it specifies the maximum
2161number of seconds the shell will wait for input after printing the primary
2162prompt
2163.Pq Ev PS1 .
2164If the time is exceeded, the shell exits.
2165.It Ev TMPDIR
2166The directory temporary shell files are created in.
2167If this parameter is not
Elliott Hughes966dd552016-12-08 15:56:04 -08002168set or does not contain the absolute path of a writable directory, temporary
Geremy Condra03ebf062011-10-12 18:17:24 -07002169files are created in
2170.Pa /tmp .
2171.It Ev USER_ID
2172The effective user id of the shell.
2173.El
2174.Ss Tilde expansion
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002175Tilde expansion, which is done in parallel with parameter substitution,
2176is applied to words starting with an unquoted
Geremy Condra03ebf062011-10-12 18:17:24 -07002177.Ql \*(TI .
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002178In parameter assignments (such as those preceding a simple-command or those
2179occurring in the arguments of a declaration utility), tilde expansion is done
2180after any assignment (i.e. after the equals sign) or after an unquoted colon
2181.Pq Ql \&: ;
2182login names are also delimited by colons.
2183The Korn shell, except in POSIX mode, always expands tildes after unquoted
2184equals signs, not just in assignment context (see below), and enables tab
2185completion for tildes after all unquoted colons during command line editing.
2186.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07002187The characters following the tilde, up to the first
2188.Ql / ,
2189if any, are assumed to be a login name.
2190If the login name is empty,
Elliott Hughes966dd552016-12-08 15:56:04 -08002191.Ql +
Geremy Condra03ebf062011-10-12 18:17:24 -07002192or
2193.Ql \- ,
Elliott Hughes77740fc2016-08-12 15:06:53 -07002194the simplified value of the
Geremy Condra03ebf062011-10-12 18:17:24 -07002195.Ev HOME ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002196.Ev PWD
Geremy Condra03ebf062011-10-12 18:17:24 -07002197or
2198.Ev OLDPWD
2199parameter is substituted, respectively.
2200Otherwise, the password file is
2201searched for the login name, and the tilde expression is substituted with the
2202user's home directory.
2203If the login name is not found in the password file or
2204if any quoting or parameter substitution occurs in the login name, no
2205substitution is performed.
2206.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07002207The home directory of previously expanded login names are cached and re-used.
2208The
Elliott Hughes96b43632015-07-17 11:39:41 -07002209.Ic alias Fl d
Elliott Hughes966dd552016-12-08 15:56:04 -08002210command may be used to list, change and add to this cache (e.g.\&
Geremy Condra03ebf062011-10-12 18:17:24 -07002211.Ic alias \-d fac=/usr/local/facilities; cd \*(TIfac/bin ) .
Elliott Hughes96b43632015-07-17 11:39:41 -07002212.Ss Brace expansion (alternation)
Geremy Condra03ebf062011-10-12 18:17:24 -07002213Brace expressions take the following form:
2214.Bd -unfilled -offset indent
2215.Sm off
2216.Xo
2217.Ar prefix No { Ar str1 No ,...,
2218.Ar strN No } Ar suffix
2219.Xc
2220.Sm on
2221.Ed
2222.Pp
2223The expressions are expanded to
2224.Ar N
2225words, each of which is the concatenation of
2226.Ar prefix ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002227.Ar str Ns i
Geremy Condra03ebf062011-10-12 18:17:24 -07002228and
2229.Ar suffix
2230(e.g.\&
Elliott Hughes966dd552016-12-08 15:56:04 -08002231.Dq Li a{c,b{X,Y},d}e
Geremy Condra03ebf062011-10-12 18:17:24 -07002232expands to four words:
Elliott Hughes966dd552016-12-08 15:56:04 -08002233.Dq Li ace ,
2234.Dq Li abXe ,
2235.Dq Li abYe
Geremy Condra03ebf062011-10-12 18:17:24 -07002236and
Elliott Hughes966dd552016-12-08 15:56:04 -08002237.Dq Li ade ) .
Geremy Condra03ebf062011-10-12 18:17:24 -07002238As noted in the example, brace expressions can be nested and the resulting
2239words are not sorted.
2240Brace expressions must contain an unquoted comma
Elliott Hughes966dd552016-12-08 15:56:04 -08002241.Pq Ql \&,
Geremy Condra03ebf062011-10-12 18:17:24 -07002242for expansion to occur (e.g.\&
2243.Ic {}
2244and
2245.Ic {foo}
2246are not expanded).
2247Brace expansion is carried out after parameter substitution
2248and before file name generation.
2249.Ss File name patterns
2250A file name pattern is a word containing one or more unquoted
2251.Ql \&? ,
2252.Ql * ,
2253.Ql + ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002254.Ql @
Geremy Condra03ebf062011-10-12 18:17:24 -07002255or
2256.Ql \&!
2257characters or
Elliott Hughes966dd552016-12-08 15:56:04 -08002258.Dq Li \&[...]
Geremy Condra03ebf062011-10-12 18:17:24 -07002259sequences.
2260Once brace expansion has been performed, the shell replaces file
2261name patterns with the sorted names of all the files that match the pattern
2262(if no files match, the word is left unchanged).
2263The pattern elements have the following meaning:
2264.Bl -tag -width Ds
2265.It \&?
2266Matches any single character.
2267.It \&*
2268Matches any sequence of octets.
Elliott Hughes966dd552016-12-08 15:56:04 -08002269.It \&[...]
Geremy Condra03ebf062011-10-12 18:17:24 -07002270Matches any of the octets inside the brackets.
2271Ranges of octets can be specified by separating two octets by a
2272.Ql \-
2273(e.g.\&
Elliott Hughes966dd552016-12-08 15:56:04 -08002274.Dq Li \&[a0\-9]
Geremy Condra03ebf062011-10-12 18:17:24 -07002275matches the letter
Elliott Hughes966dd552016-12-08 15:56:04 -08002276.Ql a
Geremy Condra03ebf062011-10-12 18:17:24 -07002277or any digit).
2278In order to represent itself, a
2279.Ql \-
2280must either be quoted or the first or last octet in the octet list.
2281Similarly, a
2282.Ql \&]
2283must be quoted or the first octet in the list if it is to represent itself
2284instead of the end of the list.
2285Also, a
2286.Ql \&!
2287appearing at the start of the list has special meaning (see below), so to
2288represent itself it must be quoted or appear later in the list.
Elliott Hughes966dd552016-12-08 15:56:04 -08002289.It \&[!...]
2290Like [...],
Geremy Condra03ebf062011-10-12 18:17:24 -07002291except it matches any octet not inside the brackets.
2292.Sm off
2293.It *( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2294.Sm on
2295Matches any string of octets that matches zero or more occurrences of the
2296specified patterns.
2297Example: The pattern
2298.Ic *(foo\*(Babar)
2299matches the strings
2300.Dq ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002301.Dq Li foo ,
2302.Dq Li bar ,
2303.Dq Li foobarfoo ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002304etc.
2305.Sm off
2306.It +( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2307.Sm on
2308Matches any string of octets that matches one or more occurrences of the
2309specified patterns.
2310Example: The pattern
2311.Ic +(foo\*(Babar)
2312matches the strings
Elliott Hughes966dd552016-12-08 15:56:04 -08002313.Dq Li foo ,
2314.Dq Li bar ,
2315.Dq Li foobar ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002316etc.
2317.Sm off
2318.It ?( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2319.Sm on
2320Matches the empty string or a string that matches one of the specified
2321patterns.
2322Example: The pattern
2323.Ic ?(foo\*(Babar)
2324only matches the strings
2325.Dq ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002326.Dq Li foo
Geremy Condra03ebf062011-10-12 18:17:24 -07002327and
Elliott Hughes966dd552016-12-08 15:56:04 -08002328.Dq Li bar .
Geremy Condra03ebf062011-10-12 18:17:24 -07002329.Sm off
2330.It @( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2331.Sm on
2332Matches a string that matches one of the specified patterns.
2333Example: The pattern
2334.Ic @(foo\*(Babar)
2335only matches the strings
Elliott Hughes966dd552016-12-08 15:56:04 -08002336.Dq Li foo
Geremy Condra03ebf062011-10-12 18:17:24 -07002337and
Elliott Hughes966dd552016-12-08 15:56:04 -08002338.Dq Li bar .
Geremy Condra03ebf062011-10-12 18:17:24 -07002339.Sm off
2340.It !( Ar pattern\*(Ba No ...\*(Ba Ar pattern )
2341.Sm on
2342Matches any string that does not match one of the specified patterns.
2343Examples: The pattern
2344.Ic !(foo\*(Babar)
2345matches all strings except
Elliott Hughes966dd552016-12-08 15:56:04 -08002346.Dq Li foo
Geremy Condra03ebf062011-10-12 18:17:24 -07002347and
Elliott Hughes966dd552016-12-08 15:56:04 -08002348.Dq Li bar ;
Geremy Condra03ebf062011-10-12 18:17:24 -07002349the pattern
Elliott Hughes966dd552016-12-08 15:56:04 -08002350.Ic \&!(*)
Geremy Condra03ebf062011-10-12 18:17:24 -07002351matches no strings; the pattern
Elliott Hughes966dd552016-12-08 15:56:04 -08002352.Ic \&!(?)*
Geremy Condra03ebf062011-10-12 18:17:24 -07002353matches all strings (think about it).
2354.El
2355.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002356Note that complicated globbing, especially with alternatives,
2357is slow; using separate comparisons may (or may not) be faster.
2358.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07002359Note that
2360.Nm mksh
2361.Po and Nm pdksh Pc
2362never matches
Elliott Hughes966dd552016-12-08 15:56:04 -08002363.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002364and
Elliott Hughes966dd552016-12-08 15:56:04 -08002365.Dq Li .. ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002366but
2367.At
2368.Nm ksh ,
2369Bourne
Elliott Hughes966dd552016-12-08 15:56:04 -08002370.Nm sh
Geremy Condra03ebf062011-10-12 18:17:24 -07002371and GNU
2372.Nm bash
2373do.
2374.Pp
2375Note that none of the above pattern elements match either a period
Elliott Hughes966dd552016-12-08 15:56:04 -08002376.Pq Ql \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002377at the start of a file name or a slash
Elliott Hughes966dd552016-12-08 15:56:04 -08002378.Pq Ql / ,
2379even if they are explicitly used in a [...] sequence; also, the names
2380.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002381and
Elliott Hughes966dd552016-12-08 15:56:04 -08002382.Dq Li ..
Geremy Condra03ebf062011-10-12 18:17:24 -07002383are never matched, even by the pattern
Elliott Hughes966dd552016-12-08 15:56:04 -08002384.Dq Li .* .
Geremy Condra03ebf062011-10-12 18:17:24 -07002385.Pp
2386If the
2387.Ic markdirs
2388option is set, any directories that result from file name generation are marked
2389with a trailing
2390.Ql / .
2391.Ss Input/output redirection
Elliott Hughes966dd552016-12-08 15:56:04 -08002392When a command is executed, its standard input, standard output and standard
2393error (file descriptors 0, 1 and 2, respectively) are normally inherited from
Geremy Condra03ebf062011-10-12 18:17:24 -07002394the shell.
2395Three exceptions to this are commands in pipelines, for which
2396standard input and/or standard output are those set up by the pipeline,
2397asynchronous commands created when job control is disabled, for which standard
Elliott Hughes966dd552016-12-08 15:56:04 -08002398input is initially set to
Geremy Condra03ebf062011-10-12 18:17:24 -07002399.Pa /dev/null ,
2400and commands for which any of the following redirections have been specified:
2401.Bl -tag -width XXxxmarker
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002402.It \*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002403Standard output is redirected to
2404.Ar file .
2405If
2406.Ar file
2407does not exist, it is created; if it does exist, is a regular file, and the
2408.Ic noclobber
2409option is set, an error occurs; otherwise, the file is truncated.
2410Note that this means the command
2411.Ic cmd \*(Ltfoo \*(Gtfoo
2412will open
2413.Ar foo
2414for reading and then truncate it when it opens it for writing, before
2415.Ar cmd
2416gets a chance to actually read
2417.Ar foo .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002418.It \*(Gt\*(Ba Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002419Same as
2420.Ic \*(Gt ,
2421except the file is truncated, even if the
2422.Ic noclobber
2423option is set.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002424.It \*(Gt\*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002425Same as
2426.Ic \*(Gt ,
2427except if
2428.Ar file
2429exists it is appended to instead of being truncated.
2430Also, the file is opened
2431in append mode, so writes always go to the end of the file (see
2432.Xr open 2 ) .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002433.It \*(Lt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002434Standard input is redirected from
2435.Ar file ,
2436which is opened for reading.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002437.It \*(Lt\*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002438Same as
2439.Ic \*(Lt ,
2440except the file is opened for reading and writing.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002441.It \*(Lt\*(Lt Ns Ar marker
Geremy Condra03ebf062011-10-12 18:17:24 -07002442After reading the command line containing this kind of redirection (called a
2443.Dq here document ) ,
2444the shell copies lines from the command source into a temporary file until a
2445line matching
2446.Ar marker
2447is read.
2448When the command is executed, standard input is redirected from the
2449temporary file.
2450If
2451.Ar marker
2452contains no quoted characters, the contents of the temporary file are processed
2453as if enclosed in double quotes each time the command is executed, so
Elliott Hughes966dd552016-12-08 15:56:04 -08002454parameter, command and arithmetic substitutions are performed, along with
Geremy Condra03ebf062011-10-12 18:17:24 -07002455backslash
Elliott Hughes966dd552016-12-08 15:56:04 -08002456.Pq Ql \e
Geremy Condra03ebf062011-10-12 18:17:24 -07002457escapes for
2458.Ql $ ,
2459.Ql \` ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002460.Ql \e
Geremy Condra03ebf062011-10-12 18:17:24 -07002461and
Elliott Hughes966dd552016-12-08 15:56:04 -08002462.Dq Li \enewline ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002463but not for
2464.Ql \&" .
2465If multiple here documents are used on the same command line, they are saved in
2466order.
2467.Pp
2468If no
2469.Ar marker
2470is given, the here document ends at the next
2471.Ic \*(Lt\*(Lt
2472and substitution will be performed.
2473If
2474.Ar marker
2475is only a set of either single
Elliott Hughes966dd552016-12-08 15:56:04 -08002476.Dq Li \*(aq\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07002477or double
Elliott Hughes966dd552016-12-08 15:56:04 -08002478.Ql \&""
Geremy Condra03ebf062011-10-12 18:17:24 -07002479quotes with nothing in between, the here document ends at the next empty line
2480and substitution will not be performed.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002481.It \*(Lt\*(Lt\- Ns Ar marker
Geremy Condra03ebf062011-10-12 18:17:24 -07002482Same as
2483.Ic \*(Lt\*(Lt ,
2484except leading tabs are stripped from lines in the here document.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002485.It \*(Lt\*(Lt\*(Lt Ns Ar word
Geremy Condra03ebf062011-10-12 18:17:24 -07002486Same as
2487.Ic \*(Lt\*(Lt ,
2488except that
2489.Ar word
2490.Em is
2491the here document.
2492This is called a here string.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002493.It \*(Lt& Ns Ar fd
Geremy Condra03ebf062011-10-12 18:17:24 -07002494Standard input is duplicated from file descriptor
2495.Ar fd .
2496.Ar fd
Elliott Hughes77740fc2016-08-12 15:06:53 -07002497can be a single digit, indicating the number of an existing file descriptor;
Geremy Condra03ebf062011-10-12 18:17:24 -07002498the letter
2499.Ql p ,
2500indicating the file descriptor associated with the output of the current
2501co-process; or the character
2502.Ql \- ,
2503indicating standard input is to be closed.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002504.It \*(Gt& Ns Ar fd
Geremy Condra03ebf062011-10-12 18:17:24 -07002505Same as
2506.Ic \*(Lt& ,
2507except the operation is done on standard output.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002508.It &\*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002509Same as
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002510.Ic \*(Gt Ns Ar file 2\*(Gt&1 .
2511This is a deprecated (legacy) GNU
Geremy Condra03ebf062011-10-12 18:17:24 -07002512.Nm bash
2513extension supported by
2514.Nm
Elliott Hughes77740fc2016-08-12 15:06:53 -07002515which also supports the preceding explicit fd digit, for example,
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002516.Ic 3&\*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002517is the same as
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002518.Ic 3\*(Gt Ns Ar file 2\*(Gt&3
Geremy Condra03ebf062011-10-12 18:17:24 -07002519in
2520.Nm
2521but a syntax error in GNU
2522.Nm bash .
2523.It Xo
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002524.No &\*(Gt\*(Ba Ns Ar file ,
2525.No &\*(Gt\*(Gt Ns Ar file ,
2526.No &\*(Gt& Ns Ar fd
Geremy Condra03ebf062011-10-12 18:17:24 -07002527.Xc
2528Same as
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002529.Ic \*(Gt\*(Ba Ns Ar file ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002530.Ic \*(Gt\*(Gt Ns Ar file
Geremy Condra03ebf062011-10-12 18:17:24 -07002531or
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002532.Ic \*(Gt& Ns Ar fd ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002533followed by
2534.Ic 2\*(Gt&1 ,
2535as above.
2536These are
2537.Nm
2538extensions.
2539.El
2540.Pp
2541In any of the above redirections, the file descriptor that is redirected
2542(i.e. standard input or standard output)
2543can be explicitly given by preceding the
Elliott Hughes77740fc2016-08-12 15:06:53 -07002544redirection with a single digit.
Elliott Hughes966dd552016-12-08 15:56:04 -08002545Parameter, command and arithmetic
2546substitutions, tilde substitutions, and, if the shell is interactive,
Geremy Condra03ebf062011-10-12 18:17:24 -07002547file name generation are all performed on the
2548.Ar file ,
Elliott Hughes966dd552016-12-08 15:56:04 -08002549.Ar marker
Geremy Condra03ebf062011-10-12 18:17:24 -07002550and
2551.Ar fd
2552arguments of redirections.
2553Note, however, that the results of any file name
2554generation are only used if a single file is matched; if multiple files match,
2555the word with the expanded file name generation characters is used.
2556Note
2557that in restricted shells, redirections which can create files cannot be used.
2558.Pp
2559For simple-commands, redirections may appear anywhere in the command; for
2560compound-commands
2561.Po
2562.Ic if
2563statements, etc.
2564.Pc ,
2565any redirections must appear at the end.
2566Redirections are processed after
2567pipelines are created and in the order they are given, so the following
2568will print an error with a line number prepended to it:
2569.Pp
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002570.Dl $ cat /foo/bar 2\*(Gt&1 \*(Gt/dev/null \*(Ba pr \-n \-t
Geremy Condra03ebf062011-10-12 18:17:24 -07002571.Pp
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002572File descriptors created by I/O redirections are private to the shell.
Geremy Condra03ebf062011-10-12 18:17:24 -07002573.Ss Arithmetic expressions
2574Integer arithmetic expressions can be used with the
2575.Ic let
Elliott Hughes966dd552016-12-08 15:56:04 -08002576command, inside $((...)) expressions, inside array references (e.g.\&
Geremy Condra03ebf062011-10-12 18:17:24 -07002577.Ar name Ns Bq Ar expr ) ,
2578as numeric arguments to the
2579.Ic test
2580command, and as the value of an assignment to an integer parameter.
Elliott Hughesf7f79562014-10-07 15:04:14 -07002581.Em Warning :
2582This also affects implicit conversion to integer, for example as done by the
2583.Ic let
2584command.
2585.Em Never
Elliott Hughes966dd552016-12-08 15:56:04 -08002586use unchecked user input, e.g. from the environment, in an arithmetic context!
Geremy Condra03ebf062011-10-12 18:17:24 -07002587.Pp
2588Expressions are calculated using signed arithmetic and the
2589.Vt mksh_ari_t
2590type (a 32-bit signed integer), unless they begin with a sole
Elliott Hughes966dd552016-12-08 15:56:04 -08002591.Ql #
Geremy Condra03ebf062011-10-12 18:17:24 -07002592character, in which case they use
2593.Vt mksh_uari_t
2594.Po a 32-bit unsigned integer Pc .
2595.Pp
Elliott Hughes966dd552016-12-08 15:56:04 -08002596Expressions may contain alpha-numeric parameter identifiers, array references
Geremy Condra03ebf062011-10-12 18:17:24 -07002597and integer constants and may be combined with the following C operators
2598(listed and grouped in increasing order of precedence):
2599.Pp
2600Unary operators:
2601.Bd -literal -offset indent
2602+ \- ! \*(TI ++ \-\-
2603.Ed
2604.Pp
2605Binary operators:
2606.Bd -literal -offset indent
2607,
Elliott Hughes77740fc2016-08-12 15:06:53 -07002608= += \-= *= /= %= \*(Lt\*(Lt= \*(Gt\*(Gt= \*(ha\*(Lt= \*(ha\*(Gt= &= \*(ha= \*(Ba=
Geremy Condra03ebf062011-10-12 18:17:24 -07002609\*(Ba\*(Ba
2610&&
2611\*(Ba
2612\*(ha
2613&
2614== !=
Thorsten Glaser811a5752013-07-25 14:24:45 +00002615\*(Lt \*(Lt= \*(Gt \*(Gt=
Elliott Hughes77740fc2016-08-12 15:06:53 -07002616\*(Lt\*(Lt \*(Gt\*(Gt \*(ha\*(Lt \*(ha\*(Gt
Geremy Condra03ebf062011-10-12 18:17:24 -07002617+ \-
2618* / %
2619.Ed
2620.Pp
2621Ternary operators:
2622.Bd -literal -offset indent
2623?: (precedence is immediately higher than assignment)
2624.Ed
2625.Pp
2626Grouping operators:
2627.Bd -literal -offset indent
2628( )
2629.Ed
2630.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002631Integer constants and expressions are calculated using an exactly 32-bit
2632wide, signed or unsigned, type with silent wraparound on integer overflow.
Geremy Condra03ebf062011-10-12 18:17:24 -07002633Integer constants may be specified with arbitrary bases using the notation
2634.Ar base Ns # Ns Ar number ,
2635where
2636.Ar base
Elliott Hughes77740fc2016-08-12 15:06:53 -07002637is a decimal integer specifying the base (up to 36), and
Geremy Condra03ebf062011-10-12 18:17:24 -07002638.Ar number
2639is a number in the specified base.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002640Additionally, base-16 integers may be specified by prefixing them with
Elliott Hughes966dd552016-12-08 15:56:04 -08002641.Dq Li 0x
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002642.Pq case-insensitive
2643in all forms of arithmetic expressions, except as numeric arguments to the
Geremy Condra03ebf062011-10-12 18:17:24 -07002644.Ic test
Elliott Hughes77740fc2016-08-12 15:06:53 -07002645built-in utility.
Thorsten Glaser811a5752013-07-25 14:24:45 +00002646Prefixing numbers with a sole digit zero
Elliott Hughes966dd552016-12-08 15:56:04 -08002647.Pq Dq Li 0
Elliott Hughes77740fc2016-08-12 15:06:53 -07002648does not cause interpretation as octal (except in POSIX mode,
2649as required by the standard), as that's unsafe to do.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002650.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07002651As a special
2652.Nm mksh
2653extension, numbers to the base of one are treated as either (8-bit
2654transparent) ASCII or Unicode codepoints, depending on the shell's
2655.Ic utf8\-mode
2656flag (current setting).
2657The
2658.At
2659.Nm ksh93
2660syntax of
Elliott Hughes966dd552016-12-08 15:56:04 -08002661.Dq Li \*(aqx\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07002662instead of
Elliott Hughes966dd552016-12-08 15:56:04 -08002663.Dq Li 1#x
Geremy Condra03ebf062011-10-12 18:17:24 -07002664is also supported.
2665Note that NUL bytes (integral value of zero) cannot be used.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002666An unset or empty parameter evaluates to 0 in integer context.
Geremy Condra03ebf062011-10-12 18:17:24 -07002667In Unicode mode, raw octets are mapped into the range EF80..EFFF as in
2668OPTU-8, which is in the PUA and has been assigned by CSUR for this use.
2669If more than one octet in ASCII mode, or a sequence of more than one
2670octet not forming a valid and minimal CESU-8 sequence is passed, the
2671behaviour is undefined (usually, the shell aborts with a parse error,
2672but rarely, it succeeds, e.g. on the sequence C2 20).
2673That's why you should always use ASCII mode unless you know that the
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002674input is well-formed UTF-8 in the range of 0000..FFFD if you use this
2675feature, as opposed to
2676.Ic read Fl a .
Geremy Condra03ebf062011-10-12 18:17:24 -07002677.Pp
2678The operators are evaluated as follows:
2679.Bl -tag -width Ds -offset indent
2680.It unary +
2681Result is the argument (included for completeness).
2682.It unary \-
2683Negation.
2684.It \&!
2685Logical NOT;
2686the result is 1 if argument is zero, 0 if not.
2687.It \*(TI
2688Arithmetic (bit-wise) NOT.
2689.It ++
2690Increment; must be applied to a parameter (not a literal or other expression).
2691The parameter is incremented by 1.
2692When used as a prefix operator, the result
2693is the incremented value of the parameter; when used as a postfix operator, the
2694result is the original value of the parameter.
2695.It \-\-
2696Similar to
2697.Ic ++ ,
2698except the parameter is decremented by 1.
2699.It \&,
2700Separates two arithmetic expressions; the left-hand side is evaluated first,
2701then the right.
2702The result is the value of the expression on the right-hand side.
2703.It =
2704Assignment; the variable on the left is set to the value on the right.
2705.It Xo
Elliott Hughes77740fc2016-08-12 15:06:53 -07002706.No += \-= *= /= %= \*(Lt\*(Lt= \*(Gt\*(Gt=
2707.No \*(ha\*(Lt= \*(ha\*(Gt= &= \*(ha= \*(Ba=
Geremy Condra03ebf062011-10-12 18:17:24 -07002708.Xc
2709Assignment operators.
2710.Sm off
2711.Ao Ar var Ac Xo
2712.Aq Ar op
2713.No = Aq Ar expr
2714.Xc
2715.Sm on
2716is the same as
2717.Sm off
2718.Ao Ar var Ac Xo
2719.No = Aq Ar var
2720.Aq Ar op
2721.Aq Ar expr ,
2722.Xc
2723.Sm on
2724with any operator precedence in
2725.Aq Ar expr
2726preserved.
2727For example,
Elliott Hughes966dd552016-12-08 15:56:04 -08002728.Dq Li var1 *= 5 + 3
Geremy Condra03ebf062011-10-12 18:17:24 -07002729is the same as specifying
Elliott Hughes966dd552016-12-08 15:56:04 -08002730.Dq Li var1 = var1 * (5 + 3) .
Geremy Condra03ebf062011-10-12 18:17:24 -07002731.It \*(Ba\*(Ba
2732Logical OR;
2733the result is 1 if either argument is non-zero, 0 if not.
2734The right argument is evaluated only if the left argument is zero.
2735.It &&
2736Logical AND;
2737the result is 1 if both arguments are non-zero, 0 if not.
2738The right argument is evaluated only if the left argument is non-zero.
2739.It \*(Ba
2740Arithmetic (bit-wise) OR.
2741.It \*(ha
2742Arithmetic (bit-wise) XOR
2743(exclusive-OR).
2744.It &
2745Arithmetic (bit-wise) AND.
2746.It ==
2747Equal; the result is 1 if both arguments are equal, 0 if not.
2748.It !=
2749Not equal; the result is 0 if both arguments are equal, 1 if not.
2750.It \*(Lt
2751Less than; the result is 1 if the left argument is less than the right, 0 if
2752not.
Thorsten Glaser811a5752013-07-25 14:24:45 +00002753.It \*(Lt= \*(Gt \*(Gt=
Elliott Hughes56b517d2014-10-06 11:30:44 -07002754Less than or equal, greater than, greater than or equal.
Geremy Condra03ebf062011-10-12 18:17:24 -07002755See
2756.Ic \*(Lt .
Elliott Hughes77740fc2016-08-12 15:06:53 -07002757.It \*(Lt\*(Lt \*(Gt\*(Gt
2758Shift left (right); the result is the left argument with its bits
2759arithmetically (signed operation) or logically (unsigned expression)
2760shifted left (right) by the amount given in the right argument.
2761.It \*(ha\*(Lt \*(ha\*(Gt
2762Rotate left (right); the result is similar to shift,
Thorsten Glaser811a5752013-07-25 14:24:45 +00002763except that the bits shifted out at one end are shifted in
2764at the other end, instead of zero or sign bits.
Geremy Condra03ebf062011-10-12 18:17:24 -07002765.It + \- * /
Elliott Hughes966dd552016-12-08 15:56:04 -08002766Addition, subtraction, multiplication and division.
Geremy Condra03ebf062011-10-12 18:17:24 -07002767.It %
Elliott Hughes56b517d2014-10-06 11:30:44 -07002768Remainder; the result is the symmetric remainder of the division of the left
2769argument by the right.
2770To get the mathematical modulus of
2771.Dq a Ic mod No b ,
2772use the formula
2773.Do
2774.Pq a % b + b
2775.No % b
2776.Dc .
Geremy Condra03ebf062011-10-12 18:17:24 -07002777.It Xo
2778.Sm off
2779.Aq Ar arg1 ?
2780.Aq Ar arg2 :
2781.Aq Ar arg3
2782.Sm on
2783.Xc
2784If
2785.Aq Ar arg1
2786is non-zero, the result is
2787.Aq Ar arg2 ;
2788otherwise the result is
2789.Aq Ar arg3 .
Thorsten Glaser811a5752013-07-25 14:24:45 +00002790The non-result argument is not evaluated.
Geremy Condra03ebf062011-10-12 18:17:24 -07002791.El
2792.Ss Co-processes
2793A co-process (which is a pipeline created with the
Elliott Hughes966dd552016-12-08 15:56:04 -08002794.Dq Li \*(Ba&
Geremy Condra03ebf062011-10-12 18:17:24 -07002795operator) is an asynchronous process that the shell can both write to (using
Elliott Hughes96b43632015-07-17 11:39:41 -07002796.Ic print Fl p )
Geremy Condra03ebf062011-10-12 18:17:24 -07002797and read from (using
Elliott Hughes96b43632015-07-17 11:39:41 -07002798.Ic read Fl p ) .
Geremy Condra03ebf062011-10-12 18:17:24 -07002799The input and output of the co-process can also be manipulated using
2800.Ic \*(Gt&p
2801and
2802.Ic \*(Lt&p
2803redirections, respectively.
2804Once a co-process has been started, another can't
2805be started until the co-process exits, or until the co-process's input has been
2806redirected using an
2807.Ic exec Ar n Ns Ic \*(Gt&p
2808redirection.
2809If a co-process's input is redirected in this way, the next
2810co-process to be started will share the output with the first co-process,
2811unless the output of the initial co-process has been redirected using an
2812.Ic exec Ar n Ns Ic \*(Lt&p
2813redirection.
2814.Pp
2815Some notes concerning co-processes:
2816.Bl -bullet
2817.It
2818The only way to close the co-process's input (so the co-process reads an
2819end-of-file) is to redirect the input to a numbered file descriptor and then
2820close that file descriptor:
2821.Ic exec 3\*(Gt&p; exec 3\*(Gt&\-
2822.It
2823In order for co-processes to share a common output, the shell must keep the
2824write portion of the output pipe open.
2825This means that end-of-file will not be
2826detected until all co-processes sharing the co-process's output have exited
2827(when they all exit, the shell closes its copy of the pipe).
2828This can be
2829avoided by redirecting the output to a numbered file descriptor (as this also
2830causes the shell to close its copy).
2831Note that this behaviour is slightly
2832different from the original Korn shell which closes its copy of the write
2833portion of the co-process output when the most recently started co-process
2834(instead of when all sharing co-processes) exits.
2835.It
Elliott Hughes96b43632015-07-17 11:39:41 -07002836.Ic print Fl p
Geremy Condra03ebf062011-10-12 18:17:24 -07002837will ignore
2838.Dv SIGPIPE
2839signals during writes if the signal is not being trapped or ignored; the same
2840is true if the co-process input has been duplicated to another file descriptor
2841and
Elliott Hughes96b43632015-07-17 11:39:41 -07002842.Ic print Fl u Ns Ar n
Geremy Condra03ebf062011-10-12 18:17:24 -07002843is used.
2844.El
2845.Ss Functions
2846Functions are defined using either Korn shell
2847.Ic function Ar function-name
2848syntax or the Bourne/POSIX shell
2849.Ar function-name Ns \&()
2850syntax (see below for the difference between the two forms).
2851Functions are like
2852.Li .\(hyscripts
2853(i.e. scripts sourced using the
Elliott Hughes966dd552016-12-08 15:56:04 -08002854.Dq Li \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07002855built-in)
2856in that they are executed in the current environment.
2857However, unlike
2858.Li .\(hyscripts ,
2859shell arguments (i.e. positional parameters $1, $2, etc.)\&
2860are never visible inside them.
2861When the shell is determining the location of a command, functions
Thorsten Glaser811a5752013-07-25 14:24:45 +00002862are searched after special built-in commands, before builtins and the
Geremy Condra03ebf062011-10-12 18:17:24 -07002863.Ev PATH
2864is searched.
2865.Pp
2866An existing function may be deleted using
2867.Ic unset Fl f Ar function-name .
2868A list of functions can be obtained using
2869.Ic typeset +f
2870and the function definitions can be listed using
Elliott Hughes96b43632015-07-17 11:39:41 -07002871.Ic typeset Fl f .
Geremy Condra03ebf062011-10-12 18:17:24 -07002872The
2873.Ic autoload
2874command (which is an alias for
Elliott Hughes96b43632015-07-17 11:39:41 -07002875.Ic typeset Fl fu )
Geremy Condra03ebf062011-10-12 18:17:24 -07002876may be used to create undefined functions: when an undefined function is
2877executed, the shell searches the path specified in the
2878.Ev FPATH
2879parameter for a file with the same name as the function which, if found, is
2880read and executed.
2881If after executing the file the named function is found to
2882be defined, the function is executed; otherwise, the normal command search is
2883continued (i.e. the shell searches the regular built-in command table and
2884.Ev PATH ) .
2885Note that if a command is not found using
2886.Ev PATH ,
2887an attempt is made to autoload a function using
2888.Ev FPATH
2889(this is an undocumented feature of the original Korn shell).
2890.Pp
2891Functions can have two attributes,
2892.Dq trace
2893and
2894.Dq export ,
2895which can be set with
Elliott Hughes96b43632015-07-17 11:39:41 -07002896.Ic typeset Fl ft
Geremy Condra03ebf062011-10-12 18:17:24 -07002897and
Elliott Hughes96b43632015-07-17 11:39:41 -07002898.Ic typeset Fl fx ,
Geremy Condra03ebf062011-10-12 18:17:24 -07002899respectively.
2900When a traced function is executed, the shell's
2901.Ic xtrace
2902option is turned on for the function's duration.
2903The
2904.Dq export
2905attribute of functions is currently not used.
2906In the original Korn shell,
2907exported functions are visible to shell scripts that are executed.
2908.Pp
2909Since functions are executed in the current shell environment, parameter
2910assignments made inside functions are visible after the function completes.
2911If this is not the desired effect, the
2912.Ic typeset
2913command can be used inside a function to create a local parameter.
2914Note that
2915.At
2916.Nm ksh93
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002917uses static scoping (one global scope, one local scope per function)
2918and allows local variables only on Korn style functions, whereas
Geremy Condra03ebf062011-10-12 18:17:24 -07002919.Nm mksh
2920uses dynamic scoping (nested scopes of varying locality).
2921Note that special parameters (e.g.\&
2922.Ic \&$$ , $! )
2923can't be scoped in this way.
2924.Pp
2925The exit status of a function is that of the last command executed in the
2926function.
2927A function can be made to finish immediately using the
2928.Ic return
2929command; this may also be used to explicitly specify the exit status.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002930Note that when called in a subshell,
2931.Ic return
2932will only exit that subshell and will not cause the original shell to exit
2933a running function (see the
2934.Ic while Ns Li \&... Ns Ic read
2935loop FAQ below).
Geremy Condra03ebf062011-10-12 18:17:24 -07002936.Pp
2937Functions defined with the
2938.Ic function
2939reserved word are treated differently in the following ways from functions
2940defined with the
2941.Ic \&()
2942notation:
2943.Bl -bullet
2944.It
2945The $0 parameter is set to the name of the function
2946(Bourne-style functions leave $0 untouched).
2947.It
2948Parameter assignments preceding function calls are not kept in the shell
2949environment (executing Bourne-style functions will keep assignments).
2950.It
2951.Ev OPTIND
2952is saved/reset and restored on entry and exit from the function so
2953.Ic getopts
2954can be used properly both inside and outside the function (Bourne-style
2955functions leave
2956.Ev OPTIND
2957untouched, so using
2958.Ic getopts
2959inside a function interferes with using
2960.Ic getopts
2961outside the function).
2962.It
Elliott Hughes96b43632015-07-17 11:39:41 -07002963Shell options
2964.Pq Ic set Fl o
2965have local scope, i.e. changes inside a function are reset upon its exit.
Geremy Condra03ebf062011-10-12 18:17:24 -07002966.El
2967.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002968In the future, the following differences may also be added:
Geremy Condra03ebf062011-10-12 18:17:24 -07002969.Bl -bullet
2970.It
2971A separate trap/signal environment will be used during the execution of
2972functions.
2973This will mean that traps set inside a function will not affect the
2974shell's traps and signals that are not ignored in the shell (but may be
2975trapped) will have their default effect in a function.
2976.It
2977The EXIT trap, if set in a function, will be executed after the function
2978returns.
2979.El
2980.Ss Command execution
Elliott Hughes966dd552016-12-08 15:56:04 -08002981After evaluation of command-line arguments, redirections and parameter
Thorsten Glaser811a5752013-07-25 14:24:45 +00002982assignments, the type of command is determined: a special built-in command,
Elliott Hughes966dd552016-12-08 15:56:04 -08002983a function, a normal builtin or the name of a file to execute found using the
Geremy Condra03ebf062011-10-12 18:17:24 -07002984.Ev PATH
2985parameter.
2986The checks are made in the above order.
2987Special built-in commands differ from other commands in that the
2988.Ev PATH
2989parameter is not used to find them, an error during their execution can
2990cause a non-interactive shell to exit, and parameter assignments that are
2991specified before the command are kept after the command completes.
2992Regular built-in commands are different only in that the
2993.Ev PATH
2994parameter is not used to find them.
2995.Pp
2996The original
2997.Nm ksh
2998and POSIX differ somewhat in which commands are considered
Thorsten Glaser811a5752013-07-25 14:24:45 +00002999special or regular.
Geremy Condra03ebf062011-10-12 18:17:24 -07003000.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00003001POSIX special built-in utilities:
Geremy Condra03ebf062011-10-12 18:17:24 -07003002.Pp
3003.Ic \&. , \&: , break , continue ,
3004.Ic eval , exec , exit , export ,
3005.Ic readonly , return , set , shift ,
Thorsten Glaser811a5752013-07-25 14:24:45 +00003006.Ic times , trap , unset
Geremy Condra03ebf062011-10-12 18:17:24 -07003007.Pp
3008Additional
3009.Nm
Thorsten Glaser811a5752013-07-25 14:24:45 +00003010commands keeping assignments:
Geremy Condra03ebf062011-10-12 18:17:24 -07003011.Pp
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003012.Ic global , source , typeset
Geremy Condra03ebf062011-10-12 18:17:24 -07003013.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00003014Builtins that are not special:
Geremy Condra03ebf062011-10-12 18:17:24 -07003015.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00003016.Ic [ , alias , bg , bind ,
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003017.Ic builtin , cat , cd , command ,
3018.Ic echo , false , fc , fg ,
3019.Ic getopts , jobs , kill , let ,
3020.Ic print , pwd , read , realpath ,
3021.Ic rename , sleep , suspend , test ,
3022.Ic true , ulimit , umask , unalias ,
3023.Ic wait , whence
Geremy Condra03ebf062011-10-12 18:17:24 -07003024.Pp
3025Once the type of command has been determined, any command-line parameter
3026assignments are performed and exported for the duration of the command.
3027.Pp
Elliott Hughesfc0307d2016-02-02 15:26:47 -08003028The following describes the special and regular built-in commands and
3029builtin-like reserved words:
Geremy Condra03ebf062011-10-12 18:17:24 -07003030.Pp
3031.Bl -tag -width false -compact
3032.It Ic \&. Ar file Op Ar arg ...
3033This is called the
3034.Dq dot
3035command.
3036Execute the commands in
3037.Ar file
3038in the current environment.
3039The file is searched for in the directories of
3040.Ev PATH .
3041If arguments are given, the positional parameters may be used to access them
3042while
3043.Ar file
3044is being executed.
3045If no arguments are given, the positional parameters are
3046those of the environment the command is used in.
3047.Pp
3048.It Ic \&: Op Ar ...
3049The null command.
3050Exit status is set to zero.
3051.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00003052.It Ic \&[ Ar expression Ic \&]
3053See
3054.Ic test .
3055.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07003056.It Xo Ic alias
3057.Oo Fl d \*(Ba t Oo Fl r Oc \*(Ba
3058.Cm +\-x Oc
3059.Op Fl p
3060.Op Cm +
3061.Oo Ar name
3062.Op Ns = Ns Ar value
3063.Ar ... Oc
3064.Xc
3065Without arguments,
3066.Ic alias
3067lists all aliases.
3068For any name without a value, the existing alias is listed.
3069Any name with a value defines an alias (see
3070.Sx Aliases
3071above).
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003072.Li \&[A\-Za\-z0\-9_!%,@\-]
3073are valid in names except they may not begin with a hyphen-minus.
Geremy Condra03ebf062011-10-12 18:17:24 -07003074.Pp
3075When listing aliases, one of two formats is used.
3076Normally, aliases are listed as
3077.Ar name Ns = Ns Ar value ,
3078where
3079.Ar value
3080is quoted.
3081If options were preceded with
3082.Ql + ,
3083or a lone
3084.Ql +
3085is given on the command line, only
3086.Ar name
3087is printed.
3088.Pp
3089The
3090.Fl d
3091option causes directory aliases which are used in tilde expansion to be
3092listed or set (see
3093.Sx Tilde expansion
3094above).
3095.Pp
3096If the
3097.Fl p
3098option is used, each alias is prefixed with the string
Elliott Hughes966dd552016-12-08 15:56:04 -08003099.Dq Li alias\ \& .
Geremy Condra03ebf062011-10-12 18:17:24 -07003100.Pp
3101The
3102.Fl t
3103option indicates that tracked aliases are to be listed/set (values specified on
3104the command line are ignored for tracked aliases).
3105The
3106.Fl r
3107option indicates that all tracked aliases are to be reset.
3108.Pp
3109The
3110.Fl x
3111option sets
3112.Pq Ic +x No clears
3113the export attribute of an alias, or, if no names are given, lists the aliases
3114with the export attribute (exporting an alias has no effect).
3115.Pp
3116.It Ic bg Op Ar job ...
3117Resume the specified stopped job(s) in the background.
3118If no jobs are specified,
3119.Ic %+
3120is assumed.
3121See
3122.Sx Job control
3123below for more information.
3124.Pp
3125.It Ic bind Op Fl l
3126The current bindings are listed.
3127If the
3128.Fl l
3129flag is given,
3130.Ic bind
3131instead lists the names of the functions to which keys may be bound.
3132See
3133.Sx Emacs editing mode
3134for more information.
3135.Pp
3136.It Xo Ic bind Op Fl m
3137.Ar string Ns = Ns Op Ar substitute
3138.Ar ...
3139.Xc
3140.It Xo Ic bind
3141.Ar string Ns = Ns Op Ar editing-command
3142.Ar ...
3143.Xc
3144The specified editing command is bound to the given
3145.Ar string ,
3146which should consist of a control character
3147optionally preceded by one of the two prefix characters
Elliott Hughes77740fc2016-08-12 15:06:53 -07003148and optionally succeeded by a tilde character.
Geremy Condra03ebf062011-10-12 18:17:24 -07003149Future input of the
3150.Ar string
3151will cause the editing command to be immediately invoked.
3152If the
3153.Fl m
3154flag is given, the specified input
3155.Ar string
3156will afterwards be immediately replaced by the given
3157.Ar substitute
3158string which may contain editing commands but not other macros.
3159If a tilde postfix is given, a tilde trailing the one or
3160two prefices and the control character is ignored, any
3161other trailing character will be processed afterwards.
3162.Pp
3163Control characters may be written using caret notation
3164i.e. \*(haX represents Ctrl-X.
3165Note that although only two prefix characters (usually ESC and \*(haX)
3166are supported, some multi-character sequences can be supported.
3167.Pp
3168The following default bindings show how the arrow keys, the home, end and
3169delete key on a BSD wsvt25, xterm\-xfree86 or GNU screen terminal are bound
3170(of course some escape sequences won't work out quite this nicely):
3171.Bd -literal -offset indent
3172bind \*(aq\*(haX\*(aq=prefix\-2
3173bind \*(aq\*(ha[[\*(aq=prefix\-2
3174bind \*(aq\*(haXA\*(aq=up\-history
3175bind \*(aq\*(haXB\*(aq=down\-history
3176bind \*(aq\*(haXC\*(aq=forward\-char
3177bind \*(aq\*(haXD\*(aq=backward\-char
3178bind \*(aq\*(haX1\*(TI\*(aq=beginning\-of\-line
3179bind \*(aq\*(haX7\*(TI\*(aq=beginning\-of\-line
3180bind \*(aq\*(haXH\*(aq=beginning\-of\-line
3181bind \*(aq\*(haX4\*(TI\*(aq=end\-of\-line
3182bind \*(aq\*(haX8\*(TI\*(aq=end\-of\-line
3183bind \*(aq\*(haXF\*(aq=end\-of\-line
3184bind \*(aq\*(haX3\*(TI\*(aq=delete\-char\-forward
3185.Ed
3186.Pp
3187.It Ic break Op Ar level
3188Exit the
3189.Ar level Ns th
3190inner-most
3191.Ic for ,
3192.Ic select ,
Elliott Hughes966dd552016-12-08 15:56:04 -08003193.Ic until
Geremy Condra03ebf062011-10-12 18:17:24 -07003194or
3195.Ic while
3196loop.
3197.Ar level
3198defaults to 1.
3199.Pp
3200.It Xo
3201.Ic builtin
3202.Op Fl \-
3203.Ar command Op Ar arg ...
3204.Xc
3205Execute the built-in command
3206.Ar command .
3207.Pp
3208.It Xo
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003209.Ic \ebuiltin
3210.Ar command Op Ar arg ...
3211.Xc
3212Same as
3213.Ic builtin .
3214Additionally acts as declaration utility forwarder, i.e. this is a
3215declaration utility (see
3216.Sx Tilde expansion )
3217.No iff Ar command
3218is a declaration utility.
3219.Pp
3220.It Xo
Geremy Condra03ebf062011-10-12 18:17:24 -07003221.Ic cat
3222.Op Fl u
3223.Op Ar
3224.Xc
3225Read files sequentially, in command line order, and write them to
3226standard output.
3227If a
3228.Ar file
3229is a single dash
Elliott Hughes966dd552016-12-08 15:56:04 -08003230.Pq Dq Li \-
Geremy Condra03ebf062011-10-12 18:17:24 -07003231or absent, read from standard input.
Geremy Condra03ebf062011-10-12 18:17:24 -07003232For direct builtin calls, the
3233.Tn POSIX
3234.Fl u
3235option is supported as a no-op.
Elliott Hughes96b43632015-07-17 11:39:41 -07003236For calls from shell, if any options are given, an external
3237.Xr cat 1
3238utility is preferred over the builtin.
Geremy Condra03ebf062011-10-12 18:17:24 -07003239.Pp
3240.It Xo
3241.Ic cd
3242.Op Fl L
3243.Op Ar dir
3244.Xc
3245.It Xo
3246.Ic cd
3247.Fl P Op Fl e
3248.Op Ar dir
3249.Xc
3250.It Xo
3251.Ic chdir
3252.Op Fl eLP
3253.Op Ar dir
3254.Xc
3255Set the working directory to
3256.Ar dir .
3257If the parameter
3258.Ev CDPATH
3259is set, it lists the search path for the directory containing
3260.Ar dir .
Elliott Hughes966dd552016-12-08 15:56:04 -08003261An unset or empty path means the current directory.
Geremy Condra03ebf062011-10-12 18:17:24 -07003262If
3263.Ar dir
3264is found in any component of the
3265.Ev CDPATH
Elliott Hughes966dd552016-12-08 15:56:04 -08003266search path other than an unset or empty path,
3267the name of the new working directory will be written to standard output.
Geremy Condra03ebf062011-10-12 18:17:24 -07003268If
3269.Ar dir
3270is missing, the home directory
3271.Ev HOME
3272is used.
3273If
3274.Ar dir
3275is
Elliott Hughes966dd552016-12-08 15:56:04 -08003276.Dq Li \- ,
Geremy Condra03ebf062011-10-12 18:17:24 -07003277the previous working directory is used (see the
3278.Ev OLDPWD
3279parameter).
3280.Pp
3281If the
3282.Fl L
3283option (logical path) is used or if the
3284.Ic physical
3285option isn't set (see the
3286.Ic set
3287command below), references to
Elliott Hughes966dd552016-12-08 15:56:04 -08003288.Dq Li ..
Geremy Condra03ebf062011-10-12 18:17:24 -07003289in
3290.Ar dir
3291are relative to the path used to get to the directory.
3292If the
3293.Fl P
3294option (physical path) is used or if the
3295.Ic physical
3296option is set,
Elliott Hughes966dd552016-12-08 15:56:04 -08003297.Dq Li ..
Geremy Condra03ebf062011-10-12 18:17:24 -07003298is relative to the filesystem directory tree.
3299The
3300.Ev PWD
3301and
3302.Ev OLDPWD
3303parameters are updated to reflect the current and old working directory,
3304respectively.
3305If the
3306.Fl e
Elliott Hughes966dd552016-12-08 15:56:04 -08003307option is set for physical filesystem traversal and
Geremy Condra03ebf062011-10-12 18:17:24 -07003308.Ev PWD
3309could not be set, the exit code is 1; greater than 1 if an
3310error occurred, 0 otherwise.
3311.Pp
3312.It Xo
3313.Ic cd
3314.Op Fl eLP
3315.Ar old new
3316.Xc
3317.It Xo
3318.Ic chdir
3319.Op Fl eLP
3320.Ar old new
3321.Xc
3322The string
3323.Ar new
3324is substituted for
3325.Ar old
3326in the current directory, and the shell attempts to change to the new
3327directory.
3328.Pp
3329.It Xo
3330.Ic command
3331.Op Fl pVv
3332.Ar cmd
3333.Op Ar arg ...
3334.Xc
3335If neither the
3336.Fl v
3337nor
3338.Fl V
3339option is given,
3340.Ar cmd
3341is executed exactly as if
3342.Ic command
3343had not been specified, with two exceptions:
3344firstly,
3345.Ar cmd
3346cannot be a shell function;
3347and secondly, special built-in commands lose their specialness
3348(i.e. redirection and utility errors do not cause the shell to
3349exit, and command assignments are not permanent).
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003350The declaration utility property is not reset.
Geremy Condra03ebf062011-10-12 18:17:24 -07003351.Pp
3352If the
3353.Fl p
3354option is given, a default search path is used instead of the current value of
3355.Ev PATH ,
3356the actual value of which is system dependent.
3357.Pp
3358If the
3359.Fl v
3360option is given, instead of executing
3361.Ar cmd ,
3362information about what would be executed is given (and the same is done for
3363.Ar arg ... ) .
Elliott Hughes96b43632015-07-17 11:39:41 -07003364For builtins, functions and keywords, their names are simply printed;
3365for aliases, a command that defines them is printed;
3366for utilities found by searching the
Geremy Condra03ebf062011-10-12 18:17:24 -07003367.Ev PATH
3368parameter, the full path of the command is printed.
3369If no command is found
3370(i.e. the path search fails), nothing is printed and
3371.Ic command
3372exits with a non-zero status.
3373The
3374.Fl V
3375option is like the
3376.Fl v
3377option, except it is more verbose.
3378.Pp
3379.It Ic continue Op Ar level
3380Jumps to the beginning of the
3381.Ar level Ns th
3382inner-most
3383.Ic for ,
3384.Ic select ,
Elliott Hughes966dd552016-12-08 15:56:04 -08003385.Ic until
Geremy Condra03ebf062011-10-12 18:17:24 -07003386or
3387.Ic while
3388loop.
3389.Ar level
3390defaults to 1.
3391.Pp
3392.It Xo
3393.Ic echo
3394.Op Fl Een
3395.Op Ar arg ...
3396.Xc
3397.Em Warning:
3398this utility is not portable; use the Korn shell builtin
3399.Ic print
Geremy Condra03ebf062011-10-12 18:17:24 -07003400instead.
3401.Pp
3402Prints its arguments (separated by spaces) followed by a newline, to the
3403standard output.
3404The newline is suppressed if any of the arguments contain the
3405backslash sequence
Elliott Hughes966dd552016-12-08 15:56:04 -08003406.Dq Li \ec .
Geremy Condra03ebf062011-10-12 18:17:24 -07003407See the
3408.Ic print
3409command below for a list of other backslash sequences that are recognised.
3410.Pp
3411The options are provided for compatibility with
3412.Bx
3413shell scripts.
3414The
3415.Fl n
3416option suppresses the trailing newline,
3417.Fl e
3418enables backslash interpretation (a no-op, since this is normally done), and
3419.Fl E
3420suppresses backslash interpretation.
3421.Pp
3422If the
3423.Ic posix
3424or
3425.Ic sh
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003426option is set or this is a direct builtin call or
3427.Ic print
3428.Fl R ,
3429only the first argument is treated as an option, and only if it is exactly
Elliott Hughes966dd552016-12-08 15:56:04 -08003430.Dq Li \-n .
Geremy Condra03ebf062011-10-12 18:17:24 -07003431Backslash interpretation is disabled.
3432.Pp
3433.It Ic eval Ar command ...
3434The arguments are concatenated (with spaces between them) to form a single
3435string which the shell then parses and executes in the current environment.
3436.Pp
3437.It Xo
3438.Ic exec
Elliott Hughes50012062015-03-10 22:22:24 -07003439.Op Fl a Ar argv0
3440.Op Fl c
Geremy Condra03ebf062011-10-12 18:17:24 -07003441.Op Ar command Op Ar arg ...
3442.Xc
3443The command is executed without forking, replacing the shell process.
Elliott Hughes50012062015-03-10 22:22:24 -07003444This is currently absolute, i.e.\&
3445.Ic exec
3446never returns, even if the
3447.Ar command
3448is not found.
3449The
3450.Fl a
3451option permits setting a different
3452.Li argv[0]
3453value, and
3454.Fl c
3455clears the environment before executing the child process, except for the
3456.Ev _
3457variable and direct assignments.
Geremy Condra03ebf062011-10-12 18:17:24 -07003458.Pp
3459If no command is given except for I/O redirection, the I/O redirection is
3460permanent and the shell is
3461not replaced.
3462Any file descriptors greater than 2 which are opened or
3463.Xr dup 2 Ns 'd
3464in this way are not made available to other executed commands (i.e. commands
3465that are not built-in to the shell).
3466Note that the Bourne shell differs here;
3467it does pass these file descriptors on.
3468.Pp
3469.It Ic exit Op Ar status
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003470The shell or subshell exits with the specified exit status.
Geremy Condra03ebf062011-10-12 18:17:24 -07003471If
3472.Ar status
3473is not specified, the exit status is the current value of the
Elliott Hughes966dd552016-12-08 15:56:04 -08003474.Ic \&$?
Geremy Condra03ebf062011-10-12 18:17:24 -07003475parameter.
3476.Pp
3477.It Xo
3478.Ic export
3479.Op Fl p
3480.Op Ar parameter Ns Op = Ns Ar value
3481.Xc
3482Sets the export attribute of the named parameters.
3483Exported parameters are passed in the environment to executed commands.
3484If values are specified, the named parameters are also assigned.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003485This is a declaration utility.
Geremy Condra03ebf062011-10-12 18:17:24 -07003486.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003487If no parameters are specified, all parameters with the export attribute
3488set are printed one per line; either their names, or, if a
Elliott Hughes966dd552016-12-08 15:56:04 -08003489.Dq Li \-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003490with no option letter is specified, name=value pairs, or, with
3491.Fl p ,
Geremy Condra03ebf062011-10-12 18:17:24 -07003492.Ic export
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003493commands suitable for re-entry.
Geremy Condra03ebf062011-10-12 18:17:24 -07003494.Pp
3495.It Ic false
3496A command that exits with a non-zero status.
3497.Pp
3498.It Xo
3499.Ic fc
3500.Oo Fl e Ar editor \*(Ba
3501.Fl l Op Fl n Oc
3502.Op Fl r
3503.Op Ar first Op Ar last
3504.Xc
3505.Ar first
3506and
3507.Ar last
3508select commands from the history.
3509Commands can be selected by history number
Elliott Hughes737fdce2014-08-07 12:59:26 -07003510(negative numbers go backwards from the current, most recent, line)
Geremy Condra03ebf062011-10-12 18:17:24 -07003511or a string specifying the most recent command starting with that string.
3512The
3513.Fl l
3514option lists the command on standard output, and
3515.Fl n
3516inhibits the default command numbers.
3517The
3518.Fl r
3519option reverses the order of the list.
3520Without
3521.Fl l ,
3522the selected commands are edited by the editor specified with the
3523.Fl e
Elliott Hughes966dd552016-12-08 15:56:04 -08003524option or, if no
Geremy Condra03ebf062011-10-12 18:17:24 -07003525.Fl e
3526is specified, the editor specified by the
3527.Ev FCEDIT
3528parameter (if this parameter is not set,
3529.Pa /bin/ed
3530is used), and then executed by the shell.
3531.Pp
3532.It Xo
3533.Ic fc
3534.Cm \-e \- \*(Ba Fl s
3535.Op Fl g
3536.Op Ar old Ns = Ns Ar new
3537.Op Ar prefix
3538.Xc
3539Re-execute the selected command (the previous command by default) after
3540performing the optional substitution of
3541.Ar old
3542with
3543.Ar new .
3544If
3545.Fl g
3546is specified, all occurrences of
3547.Ar old
3548are replaced with
3549.Ar new .
3550The meaning of
3551.Cm \-e \-
3552and
3553.Fl s
3554is identical: re-execute the selected command without invoking an editor.
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003555This command is usually accessed with the predefined:
Geremy Condra03ebf062011-10-12 18:17:24 -07003556.Ic alias r=\*(aqfc \-e \-\*(aq
Geremy Condra03ebf062011-10-12 18:17:24 -07003557.Pp
3558.It Ic fg Op Ar job ...
3559Resume the specified job(s) in the foreground.
3560If no jobs are specified,
3561.Ic %+
3562is assumed.
3563See
3564.Sx Job control
3565below for more information.
3566.Pp
3567.It Xo
3568.Ic getopts
3569.Ar optstring name
3570.Op Ar arg ...
3571.Xc
3572Used by shell procedures to parse the specified arguments (or positional
3573parameters, if no arguments are given) and to check for legal options.
3574.Ar optstring
3575contains the option letters that
3576.Ic getopts
3577is to recognise.
3578If a letter is followed by a colon, the option is expected to
3579have an argument.
3580Options that do not take arguments may be grouped in a single argument.
3581If an option takes an argument and the option character is not the
3582last character of the argument it is found in, the remainder of the argument is
3583taken to be the option's argument; otherwise, the next argument is the option's
3584argument.
3585.Pp
3586Each time
3587.Ic getopts
3588is invoked, it places the next option in the shell parameter
3589.Ar name
3590and the index of the argument to be processed by the next call to
3591.Ic getopts
3592in the shell parameter
3593.Ev OPTIND .
3594If the option was introduced with a
3595.Ql + ,
3596the option placed in
3597.Ar name
3598is prefixed with a
3599.Ql + .
3600When an option requires an argument,
3601.Ic getopts
3602places it in the shell parameter
3603.Ev OPTARG .
3604.Pp
3605When an illegal option or a missing option argument is encountered, a question
3606mark or a colon is placed in
3607.Ar name
3608(indicating an illegal option or missing argument, respectively) and
3609.Ev OPTARG
3610is set to the option character that caused the problem.
3611Furthermore, if
3612.Ar optstring
3613does not begin with a colon, a question mark is placed in
3614.Ar name ,
3615.Ev OPTARG
3616is unset, and an error message is printed to standard error.
3617.Pp
3618When the end of the options is encountered,
3619.Ic getopts
3620exits with a non-zero exit status.
3621Options end at the first (non-option
3622argument) argument that does not start with a
3623.Ql \- ,
3624or when a
Elliott Hughes966dd552016-12-08 15:56:04 -08003625.Dq Li \-\-
Geremy Condra03ebf062011-10-12 18:17:24 -07003626argument is encountered.
3627.Pp
3628Option parsing can be reset by setting
3629.Ev OPTIND
3630to 1 (this is done automatically whenever the shell or a shell procedure is
3631invoked).
3632.Pp
3633Warning: Changing the value of the shell parameter
3634.Ev OPTIND
Elliott Hughes966dd552016-12-08 15:56:04 -08003635to a value other than 1 or parsing different sets of arguments without
Geremy Condra03ebf062011-10-12 18:17:24 -07003636resetting
Elliott Hughes966dd552016-12-08 15:56:04 -08003637.Ev OPTIND
Geremy Condra03ebf062011-10-12 18:17:24 -07003638may lead to unexpected results.
3639.Pp
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003640.It Xo
3641.Ic global
3642.Op Ic +\-aglpnrtUux
3643.Oo Fl L Ns Op Ar n
3644.No \*(Ba Fl R Ns Op Ar n
3645.No \*(Ba Fl Z Ns Op Ar n Oc
3646.Op Fl i Ns Op Ar n
3647.Oo Ar name
3648.Op Ns = Ns Ar value
3649.Ar ... Oc
3650.Xc
Thorsten Glaser811a5752013-07-25 14:24:45 +00003651See
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003652.Ic typeset Fl g .
3653.No Deprecated , Em will
3654be removed from a future version of
3655.Nm .
Thorsten Glaser811a5752013-07-25 14:24:45 +00003656.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07003657.It Xo
3658.Ic hash
3659.Op Fl r
3660.Op Ar name ...
3661.Xc
3662Without arguments, any hashed executable command pathnames are listed.
3663The
3664.Fl r
3665option causes all hashed commands to be removed from the hash table.
3666Each
3667.Ar name
3668is searched as if it were a command name and added to the hash table if it is
3669an executable command.
3670.Pp
3671.It Xo
3672.Ic jobs
3673.Op Fl lnp
3674.Op Ar job ...
3675.Xc
3676Display information about the specified job(s); if no jobs are specified, all
3677jobs are displayed.
3678The
3679.Fl n
3680option causes information to be displayed only for jobs that have changed
3681state since the last notification.
3682If the
3683.Fl l
3684option is used, the process ID of each process in a job is also listed.
3685The
3686.Fl p
3687option causes only the process group of each job to be printed.
3688See
3689.Sx Job control
3690below for the format of
3691.Ar job
3692and the displayed job.
3693.Pp
3694.It Xo
3695.Ic kill
3696.Oo Fl s Ar signame \*(Ba
3697.No \- Ns Ar signum \*(Ba
3698.No \- Ns Ar signame Oc
3699.No { Ar job \*(Ba pid \*(Ba pgrp No }
3700.Ar ...
3701.Xc
Elliott Hughes966dd552016-12-08 15:56:04 -08003702Send the specified signal to the specified jobs, process IDs or process
Geremy Condra03ebf062011-10-12 18:17:24 -07003703groups.
3704If no signal is specified, the
3705.Dv TERM
3706signal is sent.
3707If a job is specified, the signal is sent to the job's process group.
3708See
3709.Sx Job control
3710below for the format of
3711.Ar job .
3712.Pp
3713.It Xo
3714.Ic kill
3715.Fl l
3716.Op Ar exit-status ...
3717.Xc
3718Print the signal name corresponding to
3719.Ar exit-status .
Elliott Hughes966dd552016-12-08 15:56:04 -08003720If no arguments are specified, a list of all the signals with their numbers
3721and a short description of each are printed.
Geremy Condra03ebf062011-10-12 18:17:24 -07003722.Pp
3723.It Ic let Op Ar expression ...
3724Each expression is evaluated (see
3725.Sx Arithmetic expressions
3726above).
3727If all expressions are successfully evaluated, the exit status is 0 (1)
3728if the last expression evaluated to non-zero (zero).
3729If an error occurs during
3730the parsing or evaluation of an expression, the exit status is greater than 1.
3731Since expressions may need to be quoted,
3732.No \&(( Ar expr No ))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003733is syntactic sugar for:
3734.Dl "{ \e\ebuiltin let \*(aq" Ns Ar expr Ns "\*(aq; }"
Thorsten Glaser811a5752013-07-25 14:24:45 +00003735.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07003736.It Xo
3737.Ic mknod
3738.Op Fl m Ar mode
3739.Ar name
3740.Cm b\*(Bac
3741.Ar major minor
3742.Xc
3743.It Xo
3744.Ic mknod
3745.Op Fl m Ar mode
3746.Ar name
3747.Cm p
3748.Xc
3749Create a device special file.
3750The file type may be
3751.Cm b
3752(block type device),
3753.Cm c
Elliott Hughes966dd552016-12-08 15:56:04 -08003754(character type device)
Geremy Condra03ebf062011-10-12 18:17:24 -07003755or
3756.Cm p
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003757.Pq named pipe , Tn FIFO .
Geremy Condra03ebf062011-10-12 18:17:24 -07003758The file created may be modified according to its
3759.Ar mode
3760(via the
3761.Fl m
3762option),
3763.Ar major
3764(major device number),
3765and
3766.Ar minor
3767(minor device number).
Elliott Hughes96b43632015-07-17 11:39:41 -07003768This is not normally part of
3769.Nm mksh ;
3770however, distributors may have added this as builtin as a speed hack.
Geremy Condra03ebf062011-10-12 18:17:24 -07003771.Pp
3772.It Xo
3773.Ic print
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003774.Oo Fl AcelNnprsu Ns Oo Ar n Oc \*(Ba
3775.Fl R Op Fl n Oc
Geremy Condra03ebf062011-10-12 18:17:24 -07003776.Op Ar argument ...
3777.Xc
Elliott Hughes966dd552016-12-08 15:56:04 -08003778Print the specified argument(s) on the standard output,
3779separated by spaces, terminated with a newline.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003780The escapes mentioned in
Geremy Condra03ebf062011-10-12 18:17:24 -07003781.Sx Backslash expansion
3782above, as well as
Elliott Hughes966dd552016-12-08 15:56:04 -08003783.Dq Li \ec ,
Geremy Condra03ebf062011-10-12 18:17:24 -07003784which is equivalent to using the
3785.Fl n
Elliott Hughes966dd552016-12-08 15:56:04 -08003786option, are interpreted.
3787.Pp
3788The options are as follows:
3789.Bl -tag -width Ds
3790.It Fl A
3791Each
3792.Ar argument
3793is arithmetically evaluated; the character corresponding to the
3794resulting value is printed.
3795Empty
3796.Ar argument Ns s
3797separate input words.
3798.It Fl c
3799The output is printed columnised, line by line, similar to how the
3800.Xr rs 1
3801utility, tab completion, the
3802.Ic kill Fl l
3803built-in utility and the
3804.Ic select
3805statement do.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003806.It Fl e
3807Restore backslash expansion after a previous
3808.Fl r .
Elliott Hughes966dd552016-12-08 15:56:04 -08003809.It Fl l
3810Change the output word separator to newline.
3811.It Fl N
3812Change the output word and line separator to ASCII NUL.
3813.It Fl n
3814Do not print the trailing line separator.
3815.It Fl p
3816Print to the co-process (see
Geremy Condra03ebf062011-10-12 18:17:24 -07003817.Sx Co-processes
3818above).
Elliott Hughes966dd552016-12-08 15:56:04 -08003819.It Fl r
3820Inhibit backslash expansion.
3821.It Fl s
3822Print to the history file instead of standard output.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003823.It Fl u Ns Op Ar n
Elliott Hughes966dd552016-12-08 15:56:04 -08003824Print to the file descriptor
3825.Ar n Pq defaults to 1 if omitted
3826instead of standard output.
3827.El
Geremy Condra03ebf062011-10-12 18:17:24 -07003828.Pp
3829The
3830.Fl R
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003831option mostly emulates the
Geremy Condra03ebf062011-10-12 18:17:24 -07003832.Bx
3833.Xr echo 1
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003834command which does not expand backslashes and interprets
3835its first argument as option only if it is exactly
3836.Dq Li \-n
3837.Pq to suppress the trailing newline .
Geremy Condra03ebf062011-10-12 18:17:24 -07003838.Pp
3839.It Ic pwd Op Fl LP
3840Print the present working directory.
3841If the
3842.Fl L
3843option is used or if the
3844.Ic physical
3845option isn't set (see the
3846.Ic set
3847command below), the logical path is printed (i.e. the path used to
3848.Ic cd
3849to the current directory).
3850If the
3851.Fl P
3852option (physical path) is used or if the
3853.Ic physical
3854option is set, the path determined from the filesystem (by following
Elliott Hughes966dd552016-12-08 15:56:04 -08003855.Dq Li ..
Geremy Condra03ebf062011-10-12 18:17:24 -07003856directories to the root directory) is printed.
3857.Pp
3858.It Xo
3859.Ic read
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003860.Op Fl A \*(Ba Fl a
Geremy Condra03ebf062011-10-12 18:17:24 -07003861.Op Fl d Ar x
3862.Oo Fl N Ar z \*(Ba
3863.Fl n Ar z Oc
3864.Oo Fl p \*(Ba
3865.Fl u Ns Op Ar n
3866.Oc Op Fl t Ar n
3867.Op Fl rs
3868.Op Ar p ...
3869.Xc
3870Reads a line of input, separates the input into fields using the
3871.Ev IFS
3872parameter (see
3873.Sx Substitution
3874above), and assigns each field to the specified parameters
3875.Ar p .
3876If no parameters are specified, the
3877.Ev REPLY
3878parameter is used to store the result.
3879With the
3880.Fl A
3881and
3882.Fl a
3883options, only no or one parameter is accepted.
3884If there are more parameters than fields, the extra parameters are set to
3885the empty string or 0; if there are more fields than parameters, the last
3886parameter is assigned the remaining fields (including the word separators).
3887.Pp
3888The options are as follows:
3889.Bl -tag -width XuXnX
3890.It Fl A
3891Store the result into the parameter
3892.Ar p
3893(or
3894.Ev REPLY )
3895as array of words.
3896.It Fl a
3897Store the result without word splitting into the parameter
3898.Ar p
3899(or
3900.Ev REPLY )
3901as array of characters (wide characters if the
3902.Ic utf8\-mode
Elliott Hughesfc0307d2016-02-02 15:26:47 -08003903option is enacted, octets otherwise); the codepoints are
3904encoded as decimal numbers by default.
Geremy Condra03ebf062011-10-12 18:17:24 -07003905.It Fl d Ar x
3906Use the first byte of
3907.Ar x ,
3908.Dv NUL
3909if empty, instead of the ASCII newline character as input line delimiter.
3910.It Fl N Ar z
3911Instead of reading till end-of-line, read exactly
3912.Ar z
Elliott Hughes96b43632015-07-17 11:39:41 -07003913bytes.
Elliott Hughes966dd552016-12-08 15:56:04 -08003914Upon EOF, a partial read is returned with exit status 1.
3915After timeout, a partial read is returned with an exit status as if
3916.Dv SIGALRM
3917were caught.
Geremy Condra03ebf062011-10-12 18:17:24 -07003918.It Fl n Ar z
3919Instead of reading till end-of-line, read up to
3920.Ar z
3921bytes but return as soon as any bytes are read, e.g.\& from a
3922slow terminal device, or if EOF or a timeout occurs.
3923.It Fl p
3924Read from the currently active co-process, see
3925.Sx Co-processes
3926above for details on this.
3927.It Fl u Ns Op Ar n
3928Read from the file descriptor
3929.Ar n
3930(defaults to 0, i.e.\& standard input).
3931The argument must immediately follow the option character.
3932.It Fl t Ar n
3933Interrupt reading after
3934.Ar n
3935seconds (specified as positive decimal value with an optional fractional part).
Elliott Hughes96b43632015-07-17 11:39:41 -07003936The exit status of
3937.Nm read
Elliott Hughes966dd552016-12-08 15:56:04 -08003938is the same as if
3939.Dv SIGALRM
3940were caught if the timeout occurred, but partial reads may still be returned.
Geremy Condra03ebf062011-10-12 18:17:24 -07003941.It Fl r
3942Normally, the ASCII backslash character escapes the special
3943meaning of the following character and is stripped from the input;
3944.Ic read
3945does not stop when encountering a backslash-newline sequence and
3946does not store that newline in the result.
3947This option enables raw mode, in which backslashes are not processed.
3948.It Fl s
3949The input line is saved to the history.
3950.El
3951.Pp
3952If the input is a terminal, both the
3953.Fl N
3954and
3955.Fl n
3956options set it into raw mode;
3957they read an entire file if \-1 is passed as
3958.Ar z
3959argument.
3960.Pp
3961The first parameter may have a question mark and a string appended to it, in
3962which case the string is used as a prompt (printed to standard error before
3963any input is read) if the input is a
3964.Xr tty 4
3965(e.g.\&
3966.Ic read nfoo?\*(aqnumber of foos: \*(aq ) .
3967.Pp
3968If no input is read or a timeout occurred,
3969.Ic read
3970exits with a non-zero status.
3971.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07003972.It Xo
3973.Ic readonly
3974.Op Fl p
3975.Oo Ar parameter
3976.Op Ns = Ns Ar value
3977.Ar ... Oc
3978.Xc
3979Sets the read-only attribute of the named parameters.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07003980This is a declaration utility.
Geremy Condra03ebf062011-10-12 18:17:24 -07003981If values are given,
3982parameters are set to them before setting the attribute.
3983Once a parameter is
3984made read-only, it cannot be unset and its value cannot be changed.
3985.Pp
3986If no parameters are specified, the names of all parameters with the read-only
3987attribute are printed one per line, unless the
3988.Fl p
3989option is used, in which case
3990.Ic readonly
3991commands defining all read-only parameters, including their values, are
3992printed.
3993.Pp
3994.It Xo
3995.Ic realpath
3996.Op Fl \-
3997.Ar name
3998.Xc
3999Prints the resolved absolute pathname corresponding to
4000.Ar name .
4001If
4002.Ar name
4003ends with a slash
Elliott Hughes966dd552016-12-08 15:56:04 -08004004.Pq Ql / ,
Geremy Condra03ebf062011-10-12 18:17:24 -07004005it's also checked for existence and whether it is a directory; otherwise,
4006.Ic realpath
4007returns 0 if the pathname either exists or can be created immediately,
4008i.e. all but the last component exist and are directories.
Elliott Hughes77740fc2016-08-12 15:06:53 -07004009For calls from the shell, if any options are given, an external
4010.Xr realpath 1
4011utility is preferred over the builtin.
Geremy Condra03ebf062011-10-12 18:17:24 -07004012.Pp
4013.It Xo
4014.Ic rename
4015.Op Fl \-
4016.Ar from to
4017.Xc
4018Renames the file
4019.Ar from
4020to
4021.Ar to .
4022Both must be complete pathnames and on the same device.
Elliott Hughes77740fc2016-08-12 15:06:53 -07004023An external utility is preferred over this builtin,
4024which is intended for emergency situations
4025.Pq where Pa /bin/mv No becomes unusable
4026and directly calls
Geremy Condra03ebf062011-10-12 18:17:24 -07004027.Xr rename 2 .
4028.Pp
4029.It Ic return Op Ar status
4030Returns from a function or
Elliott Hughes77740fc2016-08-12 15:06:53 -07004031.Ic \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07004032script, with exit status
4033.Ar status .
4034If no
4035.Ar status
4036is given, the exit status of the last executed command is used.
4037If used outside of a function or
Elliott Hughes77740fc2016-08-12 15:06:53 -07004038.Ic \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07004039script, it has the same effect as
4040.Ic exit .
4041Note that
4042.Nm
4043treats both profile and
4044.Ev ENV
4045files as
Elliott Hughes77740fc2016-08-12 15:06:53 -07004046.Ic \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07004047scripts, while the original Korn shell only treats profiles as
Elliott Hughes77740fc2016-08-12 15:06:53 -07004048.Ic \&.
Geremy Condra03ebf062011-10-12 18:17:24 -07004049scripts.
4050.Pp
4051.It Xo
4052.Ic set Op Ic +\-abCefhiklmnprsUuvXx
4053.Op Ic +\-o Ar option
4054.Op Ic +\-A Ar name
4055.Op Fl \-
4056.Op Ar arg ...
4057.Xc
4058The
4059.Ic set
4060command can be used to set
4061.Pq Ic \-
4062or clear
4063.Pq Ic +
4064shell options, set the positional parameters, or set an array parameter.
4065Options can be changed using the
4066.Cm +\-o Ar option
4067syntax, where
4068.Ar option
4069is the long name of an option, or using the
4070.Cm +\- Ns Ar letter
4071syntax, where
4072.Ar letter
4073is the option's single letter name (not all options have a single letter name).
4074The following table lists both option letters (if they exist) and long names
4075along with a description of what the option does:
4076.Bl -tag -width 3n
4077.It Fl A Ar name
4078Sets the elements of the array parameter
4079.Ar name
4080to
4081.Ar arg ...
4082If
4083.Fl A
4084is used, the array is reset (i.e. emptied) first; if
4085.Ic +A
4086is used, the first N elements are set (where N is the number of arguments);
4087the rest are left untouched.
4088.Pp
4089An alternative syntax for the command
4090.Ic set \-A foo \-\- a b c
4091which is compatible to
4092.Tn GNU
4093.Nm bash
4094and also supported by
4095.At
4096.Nm ksh93
4097is:
4098.Ic foo=(a b c); foo+=(d e)
Geremy Condra03ebf062011-10-12 18:17:24 -07004099.It Fl a \*(Ba Fl o Ic allexport
4100All new parameters are created with the export attribute.
4101.It Fl b \*(Ba Fl o Ic notify
4102Print job notification messages asynchronously, instead of just before the
4103prompt.
4104Only used if job control is enabled
4105.Pq Fl m .
4106.It Fl C \*(Ba Fl o Ic noclobber
4107Prevent \*(Gt redirection from overwriting existing files.
4108Instead, \*(Gt\*(Ba must be used to force an overwrite.
Elliott Hughes737fdce2014-08-07 12:59:26 -07004109Note that this is not safe to use for creation of temporary files or
4110lockfiles due to a TOCTOU in a check allowing one to redirect output to
4111.Pa /dev/null
4112or other device files even in
4113.Ic noclobber
4114mode.
Geremy Condra03ebf062011-10-12 18:17:24 -07004115.It Fl e \*(Ba Fl o Ic errexit
4116Exit (after executing the
4117.Dv ERR
4118trap) as soon as an error occurs or a command fails (i.e. exits with a
4119non-zero status).
4120This does not apply to commands whose exit status is
4121explicitly tested by a shell construct such as
4122.Ic if ,
4123.Ic until ,
Elliott Hughes966dd552016-12-08 15:56:04 -08004124.Ic while
Geremy Condra03ebf062011-10-12 18:17:24 -07004125or
Elliott Hughes77740fc2016-08-12 15:06:53 -07004126.Ic \&!
Geremy Condra03ebf062011-10-12 18:17:24 -07004127statements.
Thorsten Glaser811a5752013-07-25 14:24:45 +00004128For
4129.Ic &&
4130or
4131.Ic \*(Ba\*(Ba ,
4132only the status of the last command is tested.
Geremy Condra03ebf062011-10-12 18:17:24 -07004133.It Fl f \*(Ba Fl o Ic noglob
4134Do not expand file name patterns.
4135.It Fl h \*(Ba Fl o Ic trackall
4136Create tracked aliases for all executed commands (see
4137.Sx Aliases
4138above).
4139Enabled by default for non-interactive shells.
4140.It Fl i \*(Ba Fl o Ic interactive
4141The shell is an interactive shell.
4142This option can only be used when the shell is invoked.
4143See above for a description of what this means.
4144.It Fl k \*(Ba Fl o Ic keyword
4145Parameter assignments are recognised anywhere in a command.
4146.It Fl l \*(Ba Fl o Ic login
4147The shell is a login shell.
4148This option can only be used when the shell is invoked.
4149See above for a description of what this means.
4150.It Fl m \*(Ba Fl o Ic monitor
4151Enable job control (default for interactive shells).
4152.It Fl n \*(Ba Fl o Ic noexec
4153Do not execute any commands.
4154Useful for checking the syntax of scripts
4155(ignored if interactive).
4156.It Fl p \*(Ba Fl o Ic privileged
4157The shell is a privileged shell.
4158It is set automatically if, when the shell starts,
4159the real UID or GID does not match
4160the effective UID (EUID) or GID (EGID), respectively.
4161See above for a description of what this means.
4162.It Fl r \*(Ba Fl o Ic restricted
4163The shell is a restricted shell.
4164This option can only be used when the shell is invoked.
4165See above for a description of what this means.
4166.It Fl s \*(Ba Fl o Ic stdin
4167If used when the shell is invoked, commands are read from standard input.
4168Set automatically if the shell is invoked with no arguments.
4169.Pp
4170When
4171.Fl s
4172is used with the
4173.Ic set
4174command it causes the specified arguments to be sorted before assigning them to
4175the positional parameters (or to array
4176.Ar name ,
4177if
4178.Fl A
4179is used).
4180.It Fl U \*(Ba Fl o Ic utf8\-mode
4181Enable UTF-8 support in the
4182.Sx Emacs editing mode
4183and internal string handling functions.
4184This flag is disabled by default, but can be enabled by setting it on the
4185shell command line; is enabled automatically for interactive shells if
4186requested at compile time, your system supports
4187.Fn setlocale LC_CTYPE \&""
4188and optionally
4189.Fn nl_langinfo CODESET ,
4190or the
4191.Ev LC_ALL ,
Elliott Hughes966dd552016-12-08 15:56:04 -08004192.Ev LC_CTYPE
Geremy Condra03ebf062011-10-12 18:17:24 -07004193or
4194.Ev LANG
4195environment variables,
4196and at least one of these returns something that matches
4197.Dq UTF\-8
4198or
4199.Dq utf8
4200case-insensitively; for direct builtin calls depending on the
4201aforementioned environment variables; or for stdin or scripts,
4202if the input begins with a UTF-8 Byte Order Mark.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004203.Pp
4204In near future, locale tracking will be implemented, which means that
4205.Ic set Fl +U
4206is changed whenever one of the
4207.Tn POSIX
4208locale-related environment variables changes.
Geremy Condra03ebf062011-10-12 18:17:24 -07004209.It Fl u \*(Ba Fl o Ic nounset
4210Referencing of an unset parameter, other than
Elliott Hughes966dd552016-12-08 15:56:04 -08004211.Dq Li $@
Geremy Condra03ebf062011-10-12 18:17:24 -07004212or
Elliott Hughes966dd552016-12-08 15:56:04 -08004213.Dq Li $* ,
Geremy Condra03ebf062011-10-12 18:17:24 -07004214is treated as an error, unless one of the
4215.Ql \- ,
Elliott Hughes966dd552016-12-08 15:56:04 -08004216.Ql +
Geremy Condra03ebf062011-10-12 18:17:24 -07004217or
4218.Ql =
4219modifiers is used.
4220.It Fl v \*(Ba Fl o Ic verbose
4221Write shell input to standard error as it is read.
4222.It Fl X \*(Ba Fl o Ic markdirs
4223Mark directories with a trailing
4224.Ql /
4225during file name generation.
4226.It Fl x \*(Ba Fl o Ic xtrace
Thorsten Glaser811a5752013-07-25 14:24:45 +00004227Print command trees when they are executed, preceded by
Geremy Condra03ebf062011-10-12 18:17:24 -07004228the value of
4229.Ev PS4 .
4230.It Fl o Ic bgnice
4231Background jobs are run with lower priority.
4232.It Fl o Ic braceexpand
4233Enable brace expansion (a.k.a. alternation).
4234This is enabled by default.
Geremy Condra03ebf062011-10-12 18:17:24 -07004235.It Fl o Ic emacs
4236Enable BRL emacs-like command-line editing (interactive shells only); see
4237.Sx Emacs editing mode .
4238.It Fl o Ic gmacs
4239Enable gmacs-like command-line editing (interactive shells only).
4240Currently identical to emacs editing except that transpose\-chars (\*(haT) acts
4241slightly differently.
4242.It Fl o Ic ignoreeof
4243The shell will not (easily) exit when end-of-file is read;
4244.Ic exit
4245must be used.
4246To avoid infinite loops, the shell will exit if
4247.Dv EOF
4248is read 13 times in a row.
Thorsten Glaser811a5752013-07-25 14:24:45 +00004249.It Fl o Ic inherit\-xtrace
4250Do not reset
4251.Fl o Ic xtrace
4252upon entering functions.
4253This is enabled by default.
Geremy Condra03ebf062011-10-12 18:17:24 -07004254.It Fl o Ic nohup
4255Do not kill running jobs with a
4256.Dv SIGHUP
4257signal when a login shell exits.
4258Currently set by default, but this may
4259change in the future to be compatible with
4260.At
4261.Nm ksh ,
4262which
4263doesn't have this option, but does send the
4264.Dv SIGHUP
4265signal.
4266.It Fl o Ic nolog
4267No effect.
4268In the original Korn shell, this prevents function definitions from
4269being stored in the history file.
4270.It Fl o Ic physical
4271Causes the
4272.Ic cd
4273and
4274.Ic pwd
4275commands to use
4276.Dq physical
4277(i.e. the filesystem's)
Elliott Hughes966dd552016-12-08 15:56:04 -08004278.Dq Li ..
Geremy Condra03ebf062011-10-12 18:17:24 -07004279directories instead of
4280.Dq logical
4281directories (i.e. the shell handles
Elliott Hughes966dd552016-12-08 15:56:04 -08004282.Dq Li .. ,
Geremy Condra03ebf062011-10-12 18:17:24 -07004283which allows the user to be oblivious of symbolic links to directories).
4284Clear by default.
4285Note that setting this option does not affect the current value of the
4286.Ev PWD
4287parameter; only the
4288.Ic cd
4289command changes
4290.Ev PWD .
4291See the
4292.Ic cd
4293and
4294.Ic pwd
4295commands above for more details.
Thorsten Glaser811a5752013-07-25 14:24:45 +00004296.It Fl o Ic pipefail
4297Make the exit status of a pipeline (before logically complementing) the
4298rightmost non-zero errorlevel, or zero if all commands exited with zero.
Geremy Condra03ebf062011-10-12 18:17:24 -07004299.It Fl o Ic posix
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004300Behave closer to the standards
4301(see
4302.Sx POSIX mode
4303for details).
4304Automatically enabled if the basename of the shell invocation begins with
4305.Dq sh
4306and this autodetection feature is compiled in
4307.Pq not in MirBSD .
Geremy Condra03ebf062011-10-12 18:17:24 -07004308As a side effect, setting this flag turns off
4309.Ic braceexpand
4310mode, which can be turned back on manually, and
4311.Ic sh
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004312mode (unless both are enabled at the same time).
Geremy Condra03ebf062011-10-12 18:17:24 -07004313.It Fl o Ic sh
4314Enable
4315.Pa /bin/sh
4316.Pq kludge
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004317mode (see
4318.Sx SH mode ) .
Geremy Condra03ebf062011-10-12 18:17:24 -07004319Automatically enabled if the basename of the shell invocation begins with
4320.Dq sh
4321and this autodetection feature is compiled in
4322.Pq not in MirBSD .
4323As a side effect, setting this flag turns off
4324.Ic braceexpand
4325mode, which can be turned back on manually, and
4326.Ic posix
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004327mode (unless both are enabled at the same time).
Geremy Condra03ebf062011-10-12 18:17:24 -07004328.It Fl o Ic vi
4329Enable
4330.Xr vi 1 Ns -like
4331command-line editing (interactive shells only).
Elliott Hughes737fdce2014-08-07 12:59:26 -07004332See
4333.Sx Vi editing mode
4334for documentation and limitations.
Geremy Condra03ebf062011-10-12 18:17:24 -07004335.It Fl o Ic vi\-esccomplete
4336In vi command-line editing, do command and file name completion when escape
4337(\*(ha[) is entered in command mode.
4338.It Fl o Ic vi\-tabcomplete
4339In vi command-line editing, do command and file name completion when tab (\*(haI)
4340is entered in insert mode.
4341This is the default.
4342.It Fl o Ic viraw
4343No effect.
4344In the original Korn shell, unless
4345.Ic viraw
4346was set, the vi command-line mode would let the
4347.Xr tty 4
4348driver do the work until ESC (\*(ha[) was entered.
4349.Nm
4350is always in viraw mode.
4351.El
4352.Pp
4353These options can also be used upon invocation of the shell.
4354The current set of
4355options (with single letter names) can be found in the parameter
Elliott Hughes966dd552016-12-08 15:56:04 -08004356.Dq Li $\- .
Geremy Condra03ebf062011-10-12 18:17:24 -07004357.Ic set Fl o
4358with no option name will list all the options and whether each is on or off;
4359.Ic set +o
4360will print the long names of all options that are currently on.
Elliott Hughes77740fc2016-08-12 15:06:53 -07004361In a future version,
4362.Ic set +o
4363will behave
4364.Tn POSIX
4365compliant and print commands to restore the current options instead.
Geremy Condra03ebf062011-10-12 18:17:24 -07004366.Pp
4367Remaining arguments, if any, are positional parameters and are assigned, in
4368order, to the positional parameters (i.e. $1, $2, etc.).
4369If options end with
Elliott Hughes966dd552016-12-08 15:56:04 -08004370.Dq Li \-\-
Geremy Condra03ebf062011-10-12 18:17:24 -07004371and there are no remaining arguments, all positional parameters are cleared.
4372If no options or arguments are given, the values of all names are printed.
4373For unknown historical reasons, a lone
Elliott Hughes966dd552016-12-08 15:56:04 -08004374.Dq Li \-
Geremy Condra03ebf062011-10-12 18:17:24 -07004375option is treated specially \*(en it clears both the
4376.Fl v
4377and
4378.Fl x
4379options.
4380.Pp
4381.It Ic shift Op Ar number
4382The positional parameters
4383.Ar number Ns +1 ,
4384.Ar number Ns +2 ,
Elliott Hughes966dd552016-12-08 15:56:04 -08004385etc. are renamed to 1, 2, etc.
Geremy Condra03ebf062011-10-12 18:17:24 -07004386.Ar number
4387defaults to 1.
4388.Pp
4389.It Ic sleep Ar seconds
4390Suspends execution for a minimum of the
4391.Ar seconds
4392specified as positive decimal value with an optional fractional part.
4393Signal delivery may continue execution earlier.
4394.Pp
4395.It Ic source Ar file Op Ar arg ...
4396Like
4397.Ic \&. Po Do dot Dc Pc ,
4398except that the current working directory is appended to the
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004399search path (GNU
Geremy Condra03ebf062011-10-12 18:17:24 -07004400.Nm bash
Elliott Hughesfc0307d2016-02-02 15:26:47 -08004401extension).
Geremy Condra03ebf062011-10-12 18:17:24 -07004402.Pp
Elliott Hughes737fdce2014-08-07 12:59:26 -07004403.It Ic suspend
4404Stops the shell as if it had received the suspend character from
4405the terminal.
4406It is not possible to suspend a login shell unless the parent process
4407is a member of the same terminal session but is a member of a different
4408process group.
4409As a general rule, if the shell was started by another shell or via
4410.Xr su 1 ,
4411it can be suspended.
4412.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07004413.It Ic test Ar expression
4414.It Ic \&[ Ar expression Ic \&]
4415.Ic test
4416evaluates the
4417.Ar expression
4418and returns zero status if true, 1 if false, or greater than 1 if there
4419was an error.
4420It is normally used as the condition command of
4421.Ic if
4422and
4423.Ic while
4424statements.
4425Symbolic links are followed for all
4426.Ar file
4427expressions except
4428.Fl h
4429and
4430.Fl L .
4431.Pp
4432The following basic expressions are available:
4433.Bl -tag -width 17n
4434.It Fl a Ar file
4435.Ar file
4436exists.
4437.It Fl b Ar file
4438.Ar file
4439is a block special device.
4440.It Fl c Ar file
4441.Ar file
4442is a character special device.
4443.It Fl d Ar file
4444.Ar file
4445is a directory.
4446.It Fl e Ar file
4447.Ar file
4448exists.
4449.It Fl f Ar file
4450.Ar file
4451is a regular file.
4452.It Fl G Ar file
4453.Ar file Ns 's
4454group is the shell's effective group ID.
4455.It Fl g Ar file
4456.Ar file Ns 's
4457mode has the setgid bit set.
4458.It Fl H Ar file
4459.Ar file
4460is a context dependent directory (only useful on HP-UX).
4461.It Fl h Ar file
4462.Ar file
4463is a symbolic link.
4464.It Fl k Ar file
4465.Ar file Ns 's
4466mode has the
4467.Xr sticky 8
4468bit set.
4469.It Fl L Ar file
4470.Ar file
4471is a symbolic link.
4472.It Fl O Ar file
4473.Ar file Ns 's
4474owner is the shell's effective user ID.
Geremy Condra03ebf062011-10-12 18:17:24 -07004475.It Fl p Ar file
4476.Ar file
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004477is a named pipe
4478.Pq Tn FIFO .
Geremy Condra03ebf062011-10-12 18:17:24 -07004479.It Fl r Ar file
4480.Ar file
4481exists and is readable.
4482.It Fl S Ar file
4483.Ar file
4484is a
4485.Xr unix 4 Ns -domain
4486socket.
4487.It Fl s Ar file
4488.Ar file
4489is not empty.
4490.It Fl t Ar fd
4491File descriptor
4492.Ar fd
4493is a
4494.Xr tty 4
4495device.
4496.It Fl u Ar file
4497.Ar file Ns 's
4498mode has the setuid bit set.
4499.It Fl w Ar file
4500.Ar file
4501exists and is writable.
4502.It Fl x Ar file
4503.Ar file
4504exists and is executable.
4505.It Ar file1 Fl nt Ar file2
4506.Ar file1
4507is newer than
4508.Ar file2
4509or
4510.Ar file1
4511exists and
4512.Ar file2
4513does not.
4514.It Ar file1 Fl ot Ar file2
4515.Ar file1
4516is older than
4517.Ar file2
4518or
4519.Ar file2
4520exists and
4521.Ar file1
4522does not.
4523.It Ar file1 Fl ef Ar file2
4524.Ar file1
4525is the same file as
4526.Ar file2 .
4527.It Ar string
4528.Ar string
4529has non-zero length.
4530.It Fl n Ar string
4531.Ar string
4532is not empty.
4533.It Fl z Ar string
4534.Ar string
4535is empty.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004536.It Fl v Ar name
4537The shell parameter
4538.Ar name
4539is set.
4540.It Fl o Ar option
4541Shell
4542.Ar option
4543is set (see the
4544.Ic set
4545command above for a list of options).
4546As a non-standard extension, if the option starts with a
4547.Ql \&! ,
4548the test is negated; the test always fails if
4549.Ar option
4550doesn't exist (so [ \-o foo \-o \-o !foo ] returns true if and only if option
4551.Ar foo
4552exists).
4553The same can be achieved with [ \-o ?foo ] like in
4554.At
4555.Nm ksh93 .
4556.Ar option
4557can also be the short flag led by either
4558.Ql \-
4559or
4560.Ql +
4561.Pq no logical negation ,
4562for example
4563.Dq Li \-x
4564or
4565.Dq Li +x
4566instead of
4567.Dq Li xtrace .
Geremy Condra03ebf062011-10-12 18:17:24 -07004568.It Ar string No = Ar string
4569Strings are equal.
4570.It Ar string No == Ar string
4571Strings are equal.
4572.It Ar string No \*(Gt Ar string
4573First string operand is greater than second string operand.
4574.It Ar string No \*(Lt Ar string
4575First string operand is less than second string operand.
4576.It Ar string No != Ar string
4577Strings are not equal.
4578.It Ar number Fl eq Ar number
4579Numbers compare equal.
4580.It Ar number Fl ne Ar number
4581Numbers compare not equal.
4582.It Ar number Fl ge Ar number
4583Numbers compare greater than or equal.
4584.It Ar number Fl gt Ar number
4585Numbers compare greater than.
4586.It Ar number Fl le Ar number
4587Numbers compare less than or equal.
4588.It Ar number Fl \&lt Ar number
4589Numbers compare less than.
4590.El
4591.Pp
4592The above basic expressions, in which unary operators have precedence over
4593binary operators, may be combined with the following operators (listed in
4594increasing order of precedence):
4595.Bd -literal -offset indent
4596expr \-o expr Logical OR.
4597expr \-a expr Logical AND.
4598! expr Logical NOT.
4599( expr ) Grouping.
4600.Ed
4601.Pp
4602Note that a number actually may be an arithmetic expression, such as
4603a mathematical term or the name of an integer variable:
4604.Bd -literal -offset indent
4605x=1; [ "x" \-eq 1 ] evaluates to true
4606.Ed
4607.Pp
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004608Note that some special rules are applied (courtesy of
4609.Px
4610) if the number of arguments to
Geremy Condra03ebf062011-10-12 18:17:24 -07004611.Ic test
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004612or inside the brackets
Geremy Condra03ebf062011-10-12 18:17:24 -07004613.Ic \&[ ... \&]
4614is less than five: if leading
Elliott Hughes966dd552016-12-08 15:56:04 -08004615.Dq Li \&!
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004616arguments can be stripped such that only one to three arguments remain,
4617then the lowered comparison is executed; (thanks to XSI) parentheses
4618.Ic \e( ... \e)
4619lower four- and three-argument forms to two- and one-argument forms,
4620respectively; three-argument forms ultimately prefer binary operations,
4621followed by negation and parenthesis lowering; two- and four-argument forms
4622prefer negation followed by parenthesis; the one-argument form always implies
4623.Fl n .
Geremy Condra03ebf062011-10-12 18:17:24 -07004624.Pp
4625.Sy Note :
4626A common mistake is to use
Elliott Hughes966dd552016-12-08 15:56:04 -08004627.Dq Li if \&[ $foo = bar \&]
Geremy Condra03ebf062011-10-12 18:17:24 -07004628which fails if parameter
4629.Dq foo
Elliott Hughes966dd552016-12-08 15:56:04 -08004630is empty or unset, if it has embedded spaces (i.e.\&
Geremy Condra03ebf062011-10-12 18:17:24 -07004631.Ev IFS
Elliott Hughes966dd552016-12-08 15:56:04 -08004632octets) or if it is a unary operator like
4633.Dq Li \&!
Geremy Condra03ebf062011-10-12 18:17:24 -07004634or
Elliott Hughes966dd552016-12-08 15:56:04 -08004635.Dq Li \-n .
Geremy Condra03ebf062011-10-12 18:17:24 -07004636Use tests like
Elliott Hughes966dd552016-12-08 15:56:04 -08004637.Dq Li if \&[ x\&"$foo\&" = x"bar" \&]
Geremy Condra03ebf062011-10-12 18:17:24 -07004638instead, or the double-bracket operator
Elliott Hughes966dd552016-12-08 15:56:04 -08004639.Dq Li if \&[[ $foo = bar \&]]
Geremy Condra03ebf062011-10-12 18:17:24 -07004640or, to avoid pattern matching (see
4641.Ic \&[[
4642above):
Elliott Hughes966dd552016-12-08 15:56:04 -08004643.Dq Li if \&[[ $foo = \&"$bar" \&]]
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004644.Pp
4645The
Elliott Hughes966dd552016-12-08 15:56:04 -08004646.Ic \&[[ ... \&]]
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00004647construct is not only more secure to use but also often faster.
Geremy Condra03ebf062011-10-12 18:17:24 -07004648.Pp
4649.It Xo
4650.Ic time
4651.Op Fl p
4652.Op Ar pipeline
4653.Xc
4654If a
4655.Ar pipeline
4656is given, the times used to execute the pipeline are reported.
4657If no pipeline
4658is given, then the user and system time used by the shell itself, and all the
4659commands it has run since it was started, are reported.
4660The times reported are the real time (elapsed time from start to finish),
4661the user CPU time (time spent running in user mode), and the system CPU time
4662(time spent running in kernel mode).
4663Times are reported to standard error; the format of the output is:
4664.Pp
4665.Dl "0m0.00s real 0m0.00s user 0m0.00s system"
4666.Pp
4667If the
4668.Fl p
4669option is given the output is slightly longer:
4670.Bd -literal -offset indent
4671real 0.00
4672user 0.00
4673sys 0.00
4674.Ed
4675.Pp
4676It is an error to specify the
4677.Fl p
4678option unless
4679.Ar pipeline
4680is a simple command.
4681.Pp
4682Simple redirections of standard error do not affect the output of the
4683.Ic time
4684command:
4685.Pp
4686.Dl $ time sleep 1 2\*(Gtafile
4687.Dl $ { time sleep 1; } 2\*(Gtafile
4688.Pp
4689Times for the first command do not go to
4690.Dq afile ,
4691but those of the second command do.
4692.Pp
4693.It Ic times
4694Print the accumulated user and system times used both by the shell
4695and by processes that the shell started which have exited.
4696The format of the output is:
4697.Bd -literal -offset indent
46980m0.00s 0m0.00s
46990m0.00s 0m0.00s
4700.Ed
4701.Pp
Elliott Hughes96b43632015-07-17 11:39:41 -07004702.It Ic trap Ar n Op Ar signal ...
4703If the first operand is a decimal unsigned integer, this resets all
4704specified signals to the default action, i.e. is the same as calling
4705.Ic trap
Elliott Hughes966dd552016-12-08 15:56:04 -08004706with a dash
4707.Pq Dq Li \-
Elliott Hughes96b43632015-07-17 11:39:41 -07004708as
4709.Ar handler ,
4710followed by the arguments
4711.Pq Ar n Op Ar signal ... ,
4712all of which are treated as signals.
4713.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07004714.It Ic trap Op Ar handler signal ...
Elliott Hughes96b43632015-07-17 11:39:41 -07004715Sets a trap handler that is to be executed when any of the specified
4716.Ar signal Ns s
4717are received.
Geremy Condra03ebf062011-10-12 18:17:24 -07004718.Ar handler
Elliott Hughes966dd552016-12-08 15:56:04 -08004719is either an empty string, indicating the signals are to be ignored, a dash
4720.Pq Dq Li \- ,
Elliott Hughes96b43632015-07-17 11:39:41 -07004721indicating that the default action is to be taken for the signals
4722.Pq see Xr signal 3 ,
4723or a string containing shell commands to be executed at the first opportunity
Elliott Hughes966dd552016-12-08 15:56:04 -08004724(i.e. when the current command completes or before printing the next
Geremy Condra03ebf062011-10-12 18:17:24 -07004725.Ev PS1
4726prompt) after receipt of one of the signals.
4727.Ar signal
Elliott Hughes96b43632015-07-17 11:39:41 -07004728is the name of a signal
4729.Pq e.g.\& Dv PIPE or Dv ALRM
Geremy Condra03ebf062011-10-12 18:17:24 -07004730or the number of the signal (see the
Elliott Hughes96b43632015-07-17 11:39:41 -07004731.Ic kill Fl l
Geremy Condra03ebf062011-10-12 18:17:24 -07004732command above).
4733.Pp
4734There are two special signals:
4735.Dv EXIT
Elliott Hughes96b43632015-07-17 11:39:41 -07004736.Pq also known as 0 ,
4737which is executed when the shell is about to exit, and
Geremy Condra03ebf062011-10-12 18:17:24 -07004738.Dv ERR ,
Elliott Hughes96b43632015-07-17 11:39:41 -07004739which is executed after an error occurs; an error is something
4740that would cause the shell to exit if the
4741.Ic set Fl e
Geremy Condra03ebf062011-10-12 18:17:24 -07004742or
Elliott Hughes96b43632015-07-17 11:39:41 -07004743.Ic set Fl o Ic errexit
4744option were set.
Geremy Condra03ebf062011-10-12 18:17:24 -07004745.Dv EXIT
4746handlers are executed in the environment of the last executed command.
Geremy Condra03ebf062011-10-12 18:17:24 -07004747.Pp
Elliott Hughes96b43632015-07-17 11:39:41 -07004748Note that, for non-interactive shells, the trap handler cannot be changed
4749for signals that were ignored when the shell started.
4750.Pp
4751With no arguments, the current state of the traps that have been set since
4752the shell started is shown as a series of
Geremy Condra03ebf062011-10-12 18:17:24 -07004753.Ic trap
Elliott Hughes96b43632015-07-17 11:39:41 -07004754commands.
Geremy Condra03ebf062011-10-12 18:17:24 -07004755Note that the output of
4756.Ic trap
4757cannot be usefully piped to another process (an artifact of the fact that
4758traps are cleared when subprocesses are created).
4759.Pp
4760The original Korn shell's
4761.Dv DEBUG
4762trap and the handling of
4763.Dv ERR
4764and
4765.Dv EXIT
4766traps in functions are not yet implemented.
4767.Pp
4768.It Ic true
4769A command that exits with a zero value.
4770.Pp
4771.It Xo
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004772.Ic typeset
4773.Op Ic +\-aglpnrtUux
4774.Oo Fl L Ns Op Ar n
4775.No \*(Ba Fl R Ns Op Ar n
4776.No \*(Ba Fl Z Ns Op Ar n Oc
Geremy Condra03ebf062011-10-12 18:17:24 -07004777.Op Fl i Ns Op Ar n
Geremy Condra03ebf062011-10-12 18:17:24 -07004778.Oo Ar name
4779.Op Ns = Ns Ar value
4780.Ar ... Oc
4781.Xc
4782.It Xo
4783.Ic typeset
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004784.Fl f Op Fl tux
4785.Op Ar name ...
Geremy Condra03ebf062011-10-12 18:17:24 -07004786.Xc
4787Display or set parameter attributes.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004788This is a declaration utility.
Geremy Condra03ebf062011-10-12 18:17:24 -07004789With no
4790.Ar name
4791arguments, parameter attributes are displayed; if no options are used, the
4792current attributes of all parameters are printed as
4793.Ic typeset
4794commands; if an option is given (or
Elliott Hughes966dd552016-12-08 15:56:04 -08004795.Dq Li \-
Geremy Condra03ebf062011-10-12 18:17:24 -07004796with no option letter), all parameters and their values with the specified
4797attributes are printed; if options are introduced with
4798.Ql + ,
4799parameter values are not printed.
4800.Pp
4801If
4802.Ar name
4803arguments are given, the attributes of the named parameters are set
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004804.Pq Ic \&\-
Geremy Condra03ebf062011-10-12 18:17:24 -07004805or cleared
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004806.Pq Ic \&+ ;
4807inside a function, this will cause the parameters to be created
4808(with no value) in the local scope (but see
4809.Fl g ) .
Geremy Condra03ebf062011-10-12 18:17:24 -07004810Values for parameters may optionally be specified.
4811For
4812.Ar name Ns \&[*] ,
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004813the change affects all elements of the array, and no value may be specified.
Geremy Condra03ebf062011-10-12 18:17:24 -07004814.Pp
4815When
4816.Fl f
4817is used,
4818.Ic typeset
4819operates on the attributes of functions.
4820As with parameters, if no
4821.Ar name
4822arguments are given,
4823functions are listed with their values (i.e. definitions) unless
4824options are introduced with
4825.Ql + ,
4826in which case only the function names are reported.
4827.Bl -tag -width Ds
4828.It Fl a
4829Indexed array attribute.
4830.It Fl f
4831Function mode.
4832Display or set functions and their attributes, instead of parameters.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004833.It Fl g
4834Do not cause named parameters to be created in
4835the local scope when called inside a function.
Geremy Condra03ebf062011-10-12 18:17:24 -07004836.It Fl i Ns Op Ar n
4837Integer attribute.
4838.Ar n
4839specifies the base to use when displaying the integer (if not specified, the
4840base given in the first assignment is used).
4841Parameters with this attribute may
4842be assigned values containing arithmetic expressions.
4843.It Fl L Ns Op Ar n
4844Left justify attribute.
4845.Ar n
4846specifies the field width.
4847If
4848.Ar n
4849is not specified, the current width of a parameter (or the width of its first
4850assigned value) is used.
4851Leading whitespace (and zeros, if used with the
4852.Fl Z
4853option) is stripped.
4854If necessary, values are either truncated or space padded
4855to fit the field width.
4856.It Fl l
4857Lower case attribute.
Elliott Hughes966dd552016-12-08 15:56:04 -08004858All upper case ASCII characters in values are converted to lower case.
Geremy Condra03ebf062011-10-12 18:17:24 -07004859(In the original Korn shell, this parameter meant
4860.Dq long integer
4861when used with the
4862.Fl i
4863option.)
4864.It Fl n
4865Create a bound variable (name reference): any access to the variable
4866.Ar name
4867will access the variable
4868.Ar value
4869in the current scope (this is different from
4870.At
4871.Nm ksh93 ! )
4872instead.
4873Also different from
4874.At
4875.Nm ksh93
4876is that
4877.Ar value
4878is lazily evaluated at the time
4879.Ar name
4880is accessed.
4881This can be used by functions to access variables whose names are
Elliott Hughes966dd552016-12-08 15:56:04 -08004882passed as parameters, instead of using
Geremy Condra03ebf062011-10-12 18:17:24 -07004883.Ic eval .
4884.It Fl p
4885Print complete
4886.Ic typeset
4887commands that can be used to re-create the attributes and values of
4888parameters.
4889.It Fl R Ns Op Ar n
4890Right justify attribute.
4891.Ar n
4892specifies the field width.
4893If
4894.Ar n
4895is not specified, the current width of a parameter (or the width of its first
4896assigned value) is used.
4897Trailing whitespace is stripped.
4898If necessary, values are either stripped of leading characters or space
4899padded to make them fit the field width.
4900.It Fl r
4901Read-only attribute.
4902Parameters with this attribute may not be assigned to or unset.
4903Once this attribute is set, it cannot be turned off.
4904.It Fl t
4905Tag attribute.
4906Has no meaning to the shell; provided for application use.
4907.Pp
4908For functions,
4909.Fl t
4910is the trace attribute.
4911When functions with the trace attribute are executed, the
4912.Ic xtrace
4913.Pq Fl x
4914shell option is temporarily turned on.
4915.It Fl U
4916Unsigned integer attribute.
4917Integers are printed as unsigned values (combine with the
4918.Fl i
4919option).
4920This option is not in the original Korn shell.
4921.It Fl u
4922Upper case attribute.
Elliott Hughes966dd552016-12-08 15:56:04 -08004923All lower case ASCII characters in values are converted to upper case.
Geremy Condra03ebf062011-10-12 18:17:24 -07004924(In the original Korn shell, this parameter meant
4925.Dq unsigned integer
4926when used with the
4927.Fl i
4928option which meant upper case letters would never be used for bases greater
4929than 10.
4930See the
4931.Fl U
4932option.)
4933.Pp
4934For functions,
4935.Fl u
4936is the undefined attribute.
4937See
4938.Sx Functions
4939above for the implications of this.
4940.It Fl x
4941Export attribute.
4942Parameters (or functions) are placed in the environment of
4943any executed commands.
4944Exported functions are not yet implemented.
4945.It Fl Z Ns Op Ar n
4946Zero fill attribute.
4947If not combined with
4948.Fl L ,
4949this is the same as
4950.Fl R ,
4951except zero padding is used instead of space padding.
4952For integers, the number instead of the base is padded.
4953.El
4954.Pp
4955If any of the
4956.\" long integer ,
4957.Fl i ,
4958.Fl L ,
4959.Fl l ,
4960.Fl R ,
4961.Fl U ,
Elliott Hughes966dd552016-12-08 15:56:04 -08004962.Fl u
Geremy Condra03ebf062011-10-12 18:17:24 -07004963or
4964.Fl Z
4965options are changed, all others from this set are cleared,
4966unless they are also given on the same command line.
4967.Pp
4968.It Xo
4969.Ic ulimit
Elliott Hughes737fdce2014-08-07 12:59:26 -07004970.Op Fl aBCcdefHilMmnOPpqrSsTtVvw
Geremy Condra03ebf062011-10-12 18:17:24 -07004971.Op Ar value
4972.Xc
4973Display or set process limits.
4974If no options are used, the file size limit
4975.Pq Fl f
4976is assumed.
4977.Ar value ,
4978if specified, may be either an arithmetic expression or the word
4979.Dq unlimited .
4980The limits affect the shell and any processes created by the shell after a
4981limit is imposed.
4982Note that some systems may not allow limits to be increased
4983once they are set.
4984Also note that the types of limits available are system
4985dependent \*(en some systems have only the
4986.Fl f
Elliott Hughesa3c3f962017-04-12 16:52:30 -07004987limit, or not even that, or can set only the soft limits
Geremy Condra03ebf062011-10-12 18:17:24 -07004988.Bl -tag -width 5n
4989.It Fl a
4990Display all limits; unless
4991.Fl H
4992is used, soft limits are displayed.
4993.It Fl B Ar n
4994Set the socket buffer size to
4995.Ar n
4996kibibytes.
4997.It Fl C Ar n
4998Set the number of cached threads to
4999.Ar n .
5000.It Fl c Ar n
5001Impose a size limit of
5002.Ar n
5003blocks on the size of core dumps.
5004.It Fl d Ar n
5005Impose a size limit of
5006.Ar n
5007kibibytes on the size of the data area.
5008.It Fl e Ar n
5009Set the maximum niceness to
5010.Ar n .
5011.It Fl f Ar n
5012Impose a size limit of
5013.Ar n
5014blocks on files written by the shell and its child processes (files of any
5015size may be read).
5016.It Fl H
5017Set the hard limit only (the default is to set both hard and soft limits).
5018.It Fl i Ar n
5019Set the number of pending signals to
5020.Ar n .
Geremy Condra03ebf062011-10-12 18:17:24 -07005021.It Fl l Ar n
5022Impose a limit of
5023.Ar n
5024kibibytes on the amount of locked (wired) physical memory.
5025.It Fl M Ar n
5026Set the AIO locked memory to
5027.Ar n
5028kibibytes.
5029.It Fl m Ar n
5030Impose a limit of
5031.Ar n
5032kibibytes on the amount of physical memory used.
5033.It Fl n Ar n
5034Impose a limit of
5035.Ar n
5036file descriptors that can be open at once.
5037.It Fl O Ar n
5038Set the number of AIO operations to
5039.Ar n .
5040.It Fl P Ar n
5041Limit the number of threads per process to
5042.Ar n .
5043.It Fl p Ar n
5044Impose a limit of
5045.Ar n
5046processes that can be run by the user at any one time.
5047.It Fl q Ar n
5048Limit the size of
5049.Tn POSIX
5050message queues to
5051.Ar n
5052bytes.
5053.It Fl r Ar n
5054Set the maximum real-time priority to
5055.Ar n .
5056.It Fl S
5057Set the soft limit only (the default is to set both hard and soft limits).
5058.It Fl s Ar n
5059Impose a size limit of
5060.Ar n
5061kibibytes on the size of the stack area.
5062.It Fl T Ar n
5063Impose a time limit of
5064.Ar n
5065real seconds to be used by each process.
5066.It Fl t Ar n
5067Impose a time limit of
5068.Ar n
5069CPU seconds spent in user mode to be used by each process.
5070.It Fl V Ar n
5071Set the number of vnode monitors on Haiku to
5072.Ar n .
5073.It Fl v Ar n
5074Impose a limit of
5075.Ar n
5076kibibytes on the amount of virtual memory (address space) used.
5077.It Fl w Ar n
5078Impose a limit of
5079.Ar n
5080kibibytes on the amount of swap space used.
5081.El
5082.Pp
5083As far as
5084.Ic ulimit
5085is concerned, a block is 512 bytes.
5086.Pp
5087.It Xo
5088.Ic umask
5089.Op Fl S
5090.Op Ar mask
5091.Xc
Elliott Hughes966dd552016-12-08 15:56:04 -08005092Display or set the file permission creation mask or umask (see
Geremy Condra03ebf062011-10-12 18:17:24 -07005093.Xr umask 2 ) .
5094If the
5095.Fl S
5096option is used, the mask displayed or set is symbolic; otherwise, it is an
5097octal number.
5098.Pp
5099Symbolic masks are like those used by
5100.Xr chmod 1 .
5101When used, they describe what permissions may be made available (as opposed to
5102octal masks in which a set bit means the corresponding bit is to be cleared).
5103For example,
Elliott Hughes966dd552016-12-08 15:56:04 -08005104.Dq Li ug=rwx,o=
5105sets the mask so files will not be readable, writable or executable by
Geremy Condra03ebf062011-10-12 18:17:24 -07005106.Dq others ,
5107and is equivalent (on most systems) to the octal mask
Elliott Hughes966dd552016-12-08 15:56:04 -08005108.Dq Li 007 .
Geremy Condra03ebf062011-10-12 18:17:24 -07005109.Pp
5110.It Xo
5111.Ic unalias
5112.Op Fl adt
5113.Op Ar name ...
5114.Xc
5115The aliases for the given names are removed.
5116If the
5117.Fl a
5118option is used, all aliases are removed.
5119If the
5120.Fl t
5121or
5122.Fl d
5123options are used, the indicated operations are carried out on tracked or
5124directory aliases, respectively.
5125.Pp
5126.It Xo
5127.Ic unset
5128.Op Fl fv
5129.Ar parameter ...
5130.Xc
5131Unset the named parameters
5132.Po
5133.Fl v ,
5134the default
5135.Pc
5136or functions
5137.Pq Fl f .
5138With
5139.Ar parameter Ns \&[*] ,
5140attributes are kept, only values are unset.
5141.Pp
5142The exit status is non-zero if any of the parameters have the read-only
5143attribute set, zero otherwise.
5144.Pp
5145.It Ic wait Op Ar job ...
5146Wait for the specified job(s) to finish.
5147The exit status of
5148.Ic wait
5149is that of the last specified job; if the last job is killed by a signal, the
5150exit status is 128 + the number of the signal (see
Elliott Hughes96b43632015-07-17 11:39:41 -07005151.Ic kill Fl l Ar exit-status
Elliott Hughes966dd552016-12-08 15:56:04 -08005152above); if the last specified job can't be found (because it never existed or
Geremy Condra03ebf062011-10-12 18:17:24 -07005153had already finished), the exit status of
5154.Ic wait
5155is 127.
5156See
5157.Sx Job control
5158below for the format of
5159.Ar job .
5160.Ic wait
Elliott Hughes966dd552016-12-08 15:56:04 -08005161will return if a signal for which a trap has been set is received or if a
Geremy Condra03ebf062011-10-12 18:17:24 -07005162.Dv SIGHUP ,
Elliott Hughes966dd552016-12-08 15:56:04 -08005163.Dv SIGINT
Geremy Condra03ebf062011-10-12 18:17:24 -07005164or
5165.Dv SIGQUIT
5166signal is received.
5167.Pp
5168If no jobs are specified,
5169.Ic wait
5170waits for all currently running jobs (if any) to finish and exits with a zero
5171status.
5172If job monitoring is enabled, the completion status of jobs is printed
5173(this is not the case when jobs are explicitly specified).
5174.Pp
5175.It Xo
5176.Ic whence
5177.Op Fl pv
5178.Op Ar name ...
5179.Xc
Geremy Condra03ebf062011-10-12 18:17:24 -07005180Without the
5181.Fl v
Elliott Hughes77740fc2016-08-12 15:06:53 -07005182option, it is the same as
5183.Ic command Fl v ,
5184except aliases are not printed as alias command.
Geremy Condra03ebf062011-10-12 18:17:24 -07005185With the
5186.Fl v
Elliott Hughes77740fc2016-08-12 15:06:53 -07005187option, it is exactly the same as
Geremy Condra03ebf062011-10-12 18:17:24 -07005188.Ic command Fl V .
Elliott Hughes77740fc2016-08-12 15:06:53 -07005189In either case, the
Geremy Condra03ebf062011-10-12 18:17:24 -07005190.Fl p
Elliott Hughes77740fc2016-08-12 15:06:53 -07005191option differs: the search path is not affected in
5192.Ic whence ,
5193but the search is restricted to the path.
Geremy Condra03ebf062011-10-12 18:17:24 -07005194.El
5195.Ss Job control
5196Job control refers to the shell's ability to monitor and control jobs which
5197are processes or groups of processes created for commands or pipelines.
5198At a minimum, the shell keeps track of the status of the background (i.e.\&
5199asynchronous) jobs that currently exist; this information can be displayed
5200using the
5201.Ic jobs
5202commands.
5203If job control is fully enabled (using
Elliott Hughes96b43632015-07-17 11:39:41 -07005204.Ic set Fl m
Geremy Condra03ebf062011-10-12 18:17:24 -07005205or
Elliott Hughes96b43632015-07-17 11:39:41 -07005206.Ic set Fl o Ic monitor ) ,
Geremy Condra03ebf062011-10-12 18:17:24 -07005207as it is for interactive shells, the processes of a job are placed in their
5208own process group.
5209Foreground jobs can be stopped by typing the suspend
5210character from the terminal (normally \*(haZ), jobs can be restarted in either the
5211foreground or background using the
5212.Ic fg
5213and
5214.Ic bg
5215commands, and the state of the terminal is saved or restored when a foreground
5216job is stopped or restarted, respectively.
5217.Pp
5218Note that only commands that create processes (e.g. asynchronous commands,
Elliott Hughes966dd552016-12-08 15:56:04 -08005219subshell commands and non-built-in, non-function commands) can be stopped;
Geremy Condra03ebf062011-10-12 18:17:24 -07005220commands like
5221.Ic read
5222cannot be.
5223.Pp
5224When a job is created, it is assigned a job number.
5225For interactive shells, this number is printed inside
Elliott Hughes966dd552016-12-08 15:56:04 -08005226.Dq Li \&[...] ,
Geremy Condra03ebf062011-10-12 18:17:24 -07005227followed by the process IDs of the processes in the job when an asynchronous
5228command is run.
5229A job may be referred to in the
5230.Ic bg ,
5231.Ic fg ,
5232.Ic jobs ,
Elliott Hughes966dd552016-12-08 15:56:04 -08005233.Ic kill
Geremy Condra03ebf062011-10-12 18:17:24 -07005234and
5235.Ic wait
5236commands either by the process ID of the last process in the command pipeline
5237(as stored in the
Elliott Hughes966dd552016-12-08 15:56:04 -08005238.Ic \&$!
5239parameter) or by prefixing the job number with a percent sign
5240.Pq Ql % .
Geremy Condra03ebf062011-10-12 18:17:24 -07005241Other percent sequences can also be used to refer to jobs:
5242.Bl -tag -width "%+ x %% x %XX"
5243.It %+ \*(Ba %% \*(Ba %
Elliott Hughes966dd552016-12-08 15:56:04 -08005244The most recently stopped job or, if there are no stopped jobs, the oldest
Geremy Condra03ebf062011-10-12 18:17:24 -07005245running job.
5246.It %\-
5247The job that would be the
5248.Ic %+
5249job if the latter did not exist.
5250.It % Ns Ar n
5251The job with job number
5252.Ar n .
5253.It %? Ns Ar string
5254The job with its command containing the string
5255.Ar string
5256(an error occurs if multiple jobs are matched).
5257.It % Ns Ar string
5258The job with its command starting with the string
5259.Ar string
5260(an error occurs if multiple jobs are matched).
5261.El
5262.Pp
5263When a job changes state (e.g. a background job finishes or foreground job is
5264stopped), the shell prints the following status information:
5265.Pp
5266.D1 [ Ns Ar number ] Ar flag status command
5267.Pp
5268where...
5269.Bl -tag -width "command"
5270.It Ar number
5271is the job number of the job;
5272.It Ar flag
5273is the
5274.Ql +
5275or
5276.Ql \-
5277character if the job is the
5278.Ic %+
5279or
5280.Ic %\-
5281job, respectively, or space if it is neither;
5282.It Ar status
5283indicates the current state of the job and can be:
5284.Bl -tag -width "RunningXX"
5285.It Done Op Ar number
5286The job exited.
5287.Ar number
5288is the exit status of the job which is omitted if the status is zero.
5289.It Running
5290The job has neither stopped nor exited (note that running does not necessarily
5291mean consuming CPU time \*(en
5292the process could be blocked waiting for some event).
5293.It Stopped Op Ar signal
5294The job was stopped by the indicated
5295.Ar signal
5296(if no signal is given, the job was stopped by
5297.Dv SIGTSTP ) .
5298.It Ar signal-description Op Dq core dumped
5299The job was killed by a signal (e.g. memory fault, hangup); use
Elliott Hughes96b43632015-07-17 11:39:41 -07005300.Ic kill Fl l
Geremy Condra03ebf062011-10-12 18:17:24 -07005301for a list of signal descriptions.
5302The
Elliott Hughes966dd552016-12-08 15:56:04 -08005303.Dq Li core dumped
Geremy Condra03ebf062011-10-12 18:17:24 -07005304message indicates the process created a core file.
5305.El
5306.It Ar command
5307is the command that created the process.
5308If there are multiple processes in
5309the job, each process will have a line showing its
5310.Ar command
5311and possibly its
5312.Ar status ,
5313if it is different from the status of the previous process.
5314.El
5315.Pp
5316When an attempt is made to exit the shell while there are jobs in the stopped
5317state, the shell warns the user that there are stopped jobs and does not exit.
5318If another attempt is immediately made to exit the shell, the stopped jobs are
5319sent a
5320.Dv SIGHUP
5321signal and the shell exits.
5322Similarly, if the
5323.Ic nohup
5324option is not set and there are running jobs when an attempt is made to exit
5325a login shell, the shell warns the user and does not exit.
5326If another attempt
5327is immediately made to exit the shell, the running jobs are sent a
5328.Dv SIGHUP
5329signal and the shell exits.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005330.Ss POSIX mode
5331Entering
5332.Ic set Fl o Ic posix
5333mode will cause
5334.Nm
5335to behave even more
5336.Tn POSIX
5337compliant in places where the defaults or opinions differ.
5338Note that
5339.Nm mksh
Elliott Hughes966dd552016-12-08 15:56:04 -08005340will still operate with unsigned 32-bit arithmetic; use
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005341.Nm lksh
Elliott Hughes966dd552016-12-08 15:56:04 -08005342if arithmetic on the host
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005343.Vt long
Elliott Hughes966dd552016-12-08 15:56:04 -08005344data type, complete with ISO C Undefined Behaviour, is required;
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005345refer to the
5346.Xr lksh 1
5347manual page for details.
5348Most other historic,
5349.At
Elliott Hughes966dd552016-12-08 15:56:04 -08005350.Nm ksh Ns -compatible
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005351or opinionated differences can be disabled by using this mode; these are:
5352.Bl -bullet
5353.It
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005354The incompatible GNU
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005355.Nm bash
5356I/O redirection
5357.Ic &\*(Gt Ns Ar file
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005358is not supported.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005359.It
5360File descriptors created by I/O redirections are inherited by
5361child processes.
5362.It
5363Numbers with a leading digit zero are interpreted as octal.
5364.It
5365The
5366.Nm echo
5367builtin does not interpret backslashes and only supports the exact option
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005368.Fl n .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005369.It
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005370Alias expansion with a trailing space only reruns on command words.
5371.It
5372Tilde expansion follows POSIX instead of Korn shell rules.
5373.It
5374The exit status of
5375.Ic fg
5376is always 0.
5377.It
5378.Ic kill
5379.Fl l
5380only lists signal names, all in one line.
5381.It
5382.Ic getopts
5383does not accept options with a leading
5384.Ql + .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005385.El
5386.Ss SH mode
5387Compatibility mode; intended for use with legacy scripts that
5388cannot easily be fixed; the changes are as follows:
5389.Bl -bullet
5390.It
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005391The incompatible GNU
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005392.Nm bash
5393I/O redirection
5394.Ic &\*(Gt Ns Ar file
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005395is not supported.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005396.It
5397File descriptors created by I/O redirections are inherited by
5398child processes.
5399.It
5400The
5401.Nm echo
5402builtin does not interpret backslashes and only supports the exact option
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005403.Fl n ,
5404unless built with
5405.Ev \-DMKSH_MIDNIGHTBSD01ASH_COMPAT .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005406.It
Elliott Hughes77740fc2016-08-12 15:06:53 -07005407The substitution operations
5408.Sm off
5409.Xo
5410.Pf ${ Ar x
5411.Pf # Ar pat No } ,
5412.Sm on
5413.Xc
5414.Sm off
5415.Xo
5416.Pf ${ Ar x
5417.Pf ## Ar pat No } ,
5418.Sm on
5419.Xc
5420.Sm off
5421.Xo
5422.Pf ${ Ar x
5423.Pf % Ar pat No } ,
5424.Sm on
5425.Xc
5426and
5427.Sm off
5428.Xo
5429.Pf ${ Ar x
5430.Pf %% Ar pat No }
5431.Sm on
5432.Xc
5433wrongly do not require a parenthesis to be escaped and do not parse extglobs.
5434.It
Elliott Hughesa3c3f962017-04-12 16:52:30 -07005435The getopt construct from
5436.Xr lksh 1
5437passes through the errorlevel.
5438.It
5439.Nm sh
5440.Fl c
5441eats a leading
5442.Fl \-
5443if built with
5444.Ev \-DMKSH_MIDNIGHTBSD01ASH_COMPAT .
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005445.El
Geremy Condra03ebf062011-10-12 18:17:24 -07005446.Ss Interactive input line editing
5447The shell supports three modes of reading command lines from a
5448.Xr tty 4
5449in an interactive session, controlled by the
5450.Ic emacs ,
Elliott Hughes966dd552016-12-08 15:56:04 -08005451.Ic gmacs
Geremy Condra03ebf062011-10-12 18:17:24 -07005452and
5453.Ic vi
5454options (at most one of these can be set at once).
5455The default is
5456.Ic emacs .
5457Editing modes can be set explicitly using the
5458.Ic set
5459built-in.
5460If none of these options are enabled,
5461the shell simply reads lines using the normal
5462.Xr tty 4
5463driver.
5464If the
5465.Ic emacs
5466or
5467.Ic gmacs
5468option is set, the shell allows emacs-like editing of the command; similarly,
5469if the
5470.Ic vi
5471option is set, the shell allows vi-like editing of the command.
5472These modes are described in detail in the following sections.
5473.Pp
5474In these editing modes, if a line is longer than the screen width (see the
5475.Ev COLUMNS
5476parameter),
5477a
5478.Ql \*(Gt ,
Elliott Hughes966dd552016-12-08 15:56:04 -08005479.Ql +
Geremy Condra03ebf062011-10-12 18:17:24 -07005480or
5481.Ql \*(Lt
5482character is displayed in the last column indicating that there are more
5483characters after, before and after, or before the current position,
5484respectively.
5485The line is scrolled horizontally as necessary.
5486.Pp
5487Completed lines are pushed into the history, unless they begin with an
Elliott Hughes966dd552016-12-08 15:56:04 -08005488IFS octet or IFS white space or are the same as the previous line.
Geremy Condra03ebf062011-10-12 18:17:24 -07005489.Ss Emacs editing mode
5490When the
5491.Ic emacs
5492option is set, interactive input line editing is enabled.
5493Warning: This mode is
5494slightly different from the emacs mode in the original Korn shell.
5495In this mode, various editing commands
5496(typically bound to one or more control characters) cause immediate actions
5497without waiting for a newline.
5498Several editing commands are bound to particular
5499control characters when the shell is invoked; these bindings can be changed
5500using the
5501.Ic bind
5502command.
5503.Pp
5504The following is a list of available editing commands.
5505Each description starts with the name of the command,
5506suffixed with a colon;
5507an
5508.Op Ar n
5509(if the command can be prefixed with a count); and any keys the command is
5510bound to by default, written using caret notation
5511e.g. the ASCII ESC character is written as \*(ha[.
5512These control sequences are not case sensitive.
5513A count prefix for a command is entered using the sequence
5514.Pf \*(ha[ Ns Ar n ,
5515where
5516.Ar n
5517is a sequence of 1 or more digits.
5518Unless otherwise specified, if a count is
5519omitted, it defaults to 1.
5520.Pp
5521Note that editing command names are used only with the
5522.Ic bind
5523command.
5524Furthermore, many editing commands are useful only on terminals with
5525a visible cursor.
Geremy Condra03ebf062011-10-12 18:17:24 -07005526The user's
5527.Xr tty 4
5528characters (e.g.\&
5529.Dv ERASE )
5530are bound to
Elliott Hughes77740fc2016-08-12 15:06:53 -07005531reasonable substitutes and override the default bindings;
5532their customary values are shown in parentheses below.
5533The default bindings were chosen to resemble corresponding
5534Emacs key bindings:
Geremy Condra03ebf062011-10-12 18:17:24 -07005535.Bl -tag -width Ds
Elliott Hughes77740fc2016-08-12 15:06:53 -07005536.It Xo abort:
5537.No INTR Pq \*(haC ,
5538.No \*(haG
5539.Xc
Geremy Condra03ebf062011-10-12 18:17:24 -07005540Abort the current command, empty the line buffer and
5541set the exit state to interrupted.
5542.It auto\-insert: Op Ar n
5543Simply causes the character to appear as literal input.
5544Most ordinary characters are bound to this.
5545.It Xo backward\-char:
5546.Op Ar n
Elliott Hughes96b43632015-07-17 11:39:41 -07005547.No \*(haB , \*(haXD , ANSI-CurLeft , PC-CurLeft
Geremy Condra03ebf062011-10-12 18:17:24 -07005548.Xc
5549Moves the cursor backward
5550.Ar n
5551characters.
5552.It Xo backward\-word:
5553.Op Ar n
5554.No \*(ha[b , ANSI-Ctrl-CurLeft , ANSI-Alt-CurLeft
5555.Xc
5556Moves the cursor backward to the beginning of the word; words consist of
5557alphanumerics, underscore
Elliott Hughes966dd552016-12-08 15:56:04 -08005558.Pq Ql _
Geremy Condra03ebf062011-10-12 18:17:24 -07005559and dollar sign
Elliott Hughes966dd552016-12-08 15:56:04 -08005560.Pq Ql $
Geremy Condra03ebf062011-10-12 18:17:24 -07005561characters.
5562.It beginning\-of\-history: \*(ha[\*(Lt
5563Moves to the beginning of the history.
Elliott Hughes96b43632015-07-17 11:39:41 -07005564.It beginning\-of\-line: \*(haA, ANSI-Home, PC-Home
Geremy Condra03ebf062011-10-12 18:17:24 -07005565Moves the cursor to the beginning of the edited input line.
5566.It Xo capitalise\-word:
5567.Op Ar n
5568.No \*(ha[C , \*(ha[c
5569.Xc
Elliott Hughes966dd552016-12-08 15:56:04 -08005570Uppercase the first ASCII character in the next
Geremy Condra03ebf062011-10-12 18:17:24 -07005571.Ar n
5572words, leaving the cursor past the end of the last word.
5573.It clear\-screen: \*(ha[\*(haL
5574Prints a compile-time configurable sequence to clear the screen and home
5575the cursor, redraws the entire prompt and the currently edited input line.
5576The default sequence works for almost all standard terminals.
5577.It comment: \*(ha[#
5578If the current line does not begin with a comment character, one is added at
5579the beginning of the line and the line is entered (as if return had been
5580pressed); otherwise, the existing comment characters are removed and the cursor
5581is placed at the beginning of the line.
5582.It complete: \*(ha[\*(ha[
5583Automatically completes as much as is unique of the command name or the file
5584name containing the cursor.
5585If the entire remaining command or file name is
5586unique, a space is printed after its completion, unless it is a directory name
5587in which case
5588.Ql /
5589is appended.
5590If there is no command or file name with the current partial word
5591as its prefix, a bell character is output (usually causing a beep to be
5592sounded).
5593.It complete\-command: \*(haX\*(ha[
5594Automatically completes as much as is unique of the command name having the
5595partial word up to the cursor as its prefix, as in the
5596.Ic complete
5597command above.
5598.It complete\-file: \*(ha[\*(haX
5599Automatically completes as much as is unique of the file name having the
5600partial word up to the cursor as its prefix, as in the
5601.Ic complete
5602command described above.
5603.It complete\-list: \*(haI, \*(ha[=
Elliott Hughes966dd552016-12-08 15:56:04 -08005604Complete as much as is possible of the current word
Geremy Condra03ebf062011-10-12 18:17:24 -07005605and list the possible completions for it.
5606If only one completion is possible,
5607match as in the
5608.Ic complete
5609command above.
5610Note that \*(haI is usually generated by the TAB (tabulator) key.
5611.It Xo delete\-char\-backward:
5612.Op Ar n
Elliott Hughes77740fc2016-08-12 15:06:53 -07005613.No ERASE Pq \*(haH ,
5614.No \*(ha? , \*(haH
Geremy Condra03ebf062011-10-12 18:17:24 -07005615.Xc
5616Deletes
5617.Ar n
5618characters before the cursor.
5619.It Xo delete\-char\-forward:
5620.Op Ar n
Elliott Hughes96b43632015-07-17 11:39:41 -07005621.No ANSI-Del , PC-Del
Geremy Condra03ebf062011-10-12 18:17:24 -07005622.Xc
5623Deletes
5624.Ar n
5625characters after the cursor.
5626.It Xo delete\-word\-backward:
5627.Op Ar n
Elliott Hughes77740fc2016-08-12 15:06:53 -07005628.No Pfx1+ERASE Pq \*(ha[\*(haH ,
5629.No WERASE Pq \*(haW ,
5630.No \*(ha[\*(ha? , \*(ha[\*(haH , \*(ha[h
Geremy Condra03ebf062011-10-12 18:17:24 -07005631.Xc
5632Deletes
5633.Ar n
5634words before the cursor.
5635.It Xo delete\-word\-forward:
5636.Op Ar n
5637.No \*(ha[d
5638.Xc
5639Deletes characters after the cursor up to the end of
5640.Ar n
5641words.
5642.It Xo down\-history:
5643.Op Ar n
Elliott Hughes96b43632015-07-17 11:39:41 -07005644.No \*(haN , \*(haXB , ANSI-CurDown , PC-CurDown
Geremy Condra03ebf062011-10-12 18:17:24 -07005645.Xc
5646Scrolls the history buffer forward
5647.Ar n
5648lines (later).
5649Each input line originally starts just after the last entry
5650in the history buffer, so
5651.Ic down\-history
5652is not useful until either
5653.Ic search\-history ,
5654.Ic search\-history\-up
5655or
5656.Ic up\-history
5657has been performed.
5658.It Xo downcase\-word:
5659.Op Ar n
5660.No \*(ha[L , \*(ha[l
5661.Xc
5662Lowercases the next
5663.Ar n
5664words.
5665.It Xo edit\-line:
5666.Op Ar n
5667.No \*(haXe
5668.Xc
5669Edit line
5670.Ar n
5671or the current line, if not specified, interactively.
5672The actual command executed is
5673.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
5674.It end\-of\-history: \*(ha[\*(Gt
5675Moves to the end of the history.
Elliott Hughes96b43632015-07-17 11:39:41 -07005676.It end\-of\-line: \*(haE, ANSI-End, PC-End
Geremy Condra03ebf062011-10-12 18:17:24 -07005677Moves the cursor to the end of the input line.
5678.It eot: \*(ha_
5679Acts as an end-of-file; this is useful because edit-mode input disables
Elliott Hughes77740fc2016-08-12 15:06:53 -07005680normal terminal input canonicalisation.
Geremy Condra03ebf062011-10-12 18:17:24 -07005681.It Xo eot\-or\-delete:
5682.Op Ar n
Elliott Hughes77740fc2016-08-12 15:06:53 -07005683.No EOF Pq \*(haD
Geremy Condra03ebf062011-10-12 18:17:24 -07005684.Xc
Elliott Hughes77740fc2016-08-12 15:06:53 -07005685If alone on a line, same as
5686.Ic eot ,
5687otherwise,
Geremy Condra03ebf062011-10-12 18:17:24 -07005688.Ic delete\-char\-forward .
5689.It error: (not bound)
5690Error (ring the bell).
Elliott Hughes77740fc2016-08-12 15:06:53 -07005691.It evaluate\-region: \*(ha[\*(haE
5692Evaluates the text between the mark and the cursor position
5693.Pq the entire line if no mark is set
5694as function substitution (if it cannot be parsed, the editing state is
5695unchanged and the bell is rung to signal an error); $? is updated accordingly.
Geremy Condra03ebf062011-10-12 18:17:24 -07005696.It exchange\-point\-and\-mark: \*(haX\*(haX
5697Places the cursor where the mark is and sets the mark to where the cursor was.
5698.It expand\-file: \*(ha[*
5699Appends a
5700.Ql *
5701to the current word and replaces the word with the result of performing file
5702globbing on the word.
5703If no files match the pattern, the bell is rung.
5704.It Xo forward\-char:
5705.Op Ar n
Elliott Hughes96b43632015-07-17 11:39:41 -07005706.No \*(haF , \*(haXC , ANSI-CurRight , PC-CurRight
Geremy Condra03ebf062011-10-12 18:17:24 -07005707.Xc
5708Moves the cursor forward
5709.Ar n
5710characters.
5711.It Xo forward\-word:
5712.Op Ar n
5713.No \*(ha[f , ANSI-Ctrl-CurRight , ANSI-Alt-CurRight
5714.Xc
5715Moves the cursor forward to the end of the
5716.Ar n Ns th
5717word.
5718.It Xo goto\-history:
5719.Op Ar n
5720.No \*(ha[g
5721.Xc
5722Goes to history number
5723.Ar n .
Elliott Hughes77740fc2016-08-12 15:06:53 -07005724.It Xo kill\-line:
5725.No KILL Pq \*(haU
5726.Xc
Geremy Condra03ebf062011-10-12 18:17:24 -07005727Deletes the entire input line.
5728.It kill\-region: \*(haW
5729Deletes the input between the cursor and the mark.
5730.It Xo kill\-to\-eol:
5731.Op Ar n
5732.No \*(haK
5733.Xc
5734Deletes the input from the cursor to the end of the line if
5735.Ar n
5736is not specified; otherwise deletes characters between the cursor and column
5737.Ar n .
5738.It list: \*(ha[?
5739Prints a sorted, columnated list of command names or file names (if any) that
5740can complete the partial word containing the cursor.
5741Directory names have
5742.Ql /
5743appended to them.
5744.It list\-command: \*(haX?
5745Prints a sorted, columnated list of command names (if any) that can complete
5746the partial word containing the cursor.
5747.It list\-file: \*(haX\*(haY
5748Prints a sorted, columnated list of file names (if any) that can complete the
5749partial word containing the cursor.
5750File type indicators are appended as described under
5751.Ic list
5752above.
5753.It newline: \*(haJ , \*(haM
5754Causes the current input line to be processed by the shell.
5755The current cursor position may be anywhere on the line.
5756.It newline\-and\-next: \*(haO
5757Causes the current input line to be processed by the shell, and the next line
5758from history becomes the current line.
5759This is only useful after an
5760.Ic up\-history ,
5761.Ic search\-history
5762or
5763.Ic search\-history\-up .
Elliott Hughes77740fc2016-08-12 15:06:53 -07005764.It Xo no\-op:
5765.No QUIT Pq \*(ha\e
5766.Xc
Geremy Condra03ebf062011-10-12 18:17:24 -07005767This does nothing.
5768.It prefix\-1: \*(ha[
5769Introduces a 2-character command sequence.
5770.It prefix\-2: \*(haX , \*(ha[[ , \*(ha[O
Elliott Hughes77740fc2016-08-12 15:06:53 -07005771Introduces a multi-character command sequence.
Geremy Condra03ebf062011-10-12 18:17:24 -07005772.It Xo prev\-hist\-word:
5773.Op Ar n
5774.No \*(ha[. , \*(ha[_
5775.Xc
Elliott Hughes966dd552016-12-08 15:56:04 -08005776The last word or, if given, the
Geremy Condra03ebf062011-10-12 18:17:24 -07005777.Ar n Ns th
5778word (zero-based) of the previous (on repeated execution, second-last,
5779third-last, etc.) command is inserted at the cursor.
5780Use of this editing command trashes the mark.
5781.It quote: \*(ha\*(ha , \*(haV
5782The following character is taken literally rather than as an editing command.
5783.It redraw: \*(haL
5784Reprints the last line of the prompt string and the current input line
5785on a new line.
5786.It Xo search\-character\-backward:
5787.Op Ar n
5788.No \*(ha[\*(ha]
5789.Xc
5790Search backward in the current line for the
5791.Ar n Ns th
5792occurrence of the next character typed.
5793.It Xo search\-character\-forward:
5794.Op Ar n
5795.No \*(ha]
5796.Xc
5797Search forward in the current line for the
5798.Ar n Ns th
5799occurrence of the next character typed.
5800.It search\-history: \*(haR
5801Enter incremental search mode.
5802The internal history list is searched
5803backwards for commands matching the input.
5804An initial
5805.Ql \*(ha
5806in the search string anchors the search.
5807The escape key will leave search mode.
5808Other commands, including sequences of escape as
5809.Ic prefix\-1
5810followed by a
5811.Ic prefix\-1
5812or
5813.Ic prefix\-2
5814key will be executed after leaving search mode.
5815The
5816.Ic abort Pq \*(haG
5817command will restore the input line before search started.
5818Successive
5819.Ic search\-history
5820commands continue searching backward to the next previous occurrence of the
5821pattern.
5822The history buffer retains only a finite number of lines; the oldest
5823are discarded as necessary.
Elliott Hughes96b43632015-07-17 11:39:41 -07005824.It search\-history\-up: ANSI-PgUp, PC-PgUp
Geremy Condra03ebf062011-10-12 18:17:24 -07005825Search backwards through the history buffer for commands whose beginning match
5826the portion of the input line before the cursor.
5827When used on an empty line, this has the same effect as
5828.Ic up\-history .
Elliott Hughes96b43632015-07-17 11:39:41 -07005829.It search\-history\-down: ANSI-PgDn, PC-PgDn
Geremy Condra03ebf062011-10-12 18:17:24 -07005830Search forwards through the history buffer for commands whose beginning match
5831the portion of the input line before the cursor.
5832When used on an empty line, this has the same effect as
5833.Ic down\-history .
5834This is only useful after an
5835.Ic up\-history ,
5836.Ic search\-history
5837or
5838.Ic search\-history\-up .
5839.It set\-mark\-command: \*(ha[ Ns Aq space
5840Set the mark at the cursor position.
5841.It transpose\-chars: \*(haT
Elliott Hughes966dd552016-12-08 15:56:04 -08005842If at the end of line or, if the
Geremy Condra03ebf062011-10-12 18:17:24 -07005843.Ic gmacs
5844option is set, this exchanges the two previous characters; otherwise, it
5845exchanges the previous and current characters and moves the cursor one
5846character to the right.
5847.It Xo up\-history:
5848.Op Ar n
Elliott Hughes96b43632015-07-17 11:39:41 -07005849.No \*(haP , \*(haXA , ANSI-CurUp , PC-CurUp
Geremy Condra03ebf062011-10-12 18:17:24 -07005850.Xc
5851Scrolls the history buffer backward
5852.Ar n
5853lines (earlier).
5854.It Xo upcase\-word:
5855.Op Ar n
5856.No \*(ha[U , \*(ha[u
5857.Xc
5858Uppercase the next
5859.Ar n
5860words.
5861.It version: \*(ha[\*(haV
5862Display the version of
5863.Nm mksh .
5864The current edit buffer is restored as soon as a key is pressed.
5865The restoring keypress is processed, unless it is a space.
5866.It yank: \*(haY
5867Inserts the most recently killed text string at the current cursor position.
5868.It yank\-pop: \*(ha[y
5869Immediately after a
5870.Ic yank ,
5871replaces the inserted text string with the next previously killed text string.
5872.El
Elliott Hughes77740fc2016-08-12 15:06:53 -07005873.Pp
5874The tab completion escapes characters the same way as the following code:
5875.Bd -literal
5876print \-nr \-\- "${x@/[\e"\-\e$\e&\-*:\-?[\e\e\e\`{\-\e}${IFS\-$\*(aq \et\en\*(aq}]/\e\e$KSH_MATCH}"
5877.Ed
Geremy Condra03ebf062011-10-12 18:17:24 -07005878.Ss Vi editing mode
5879.Em Note:
5880The vi command-line editing mode is orphaned, yet still functional.
Elliott Hughes737fdce2014-08-07 12:59:26 -07005881It is 8-bit clean but specifically does not support UTF-8 or MBCS.
Geremy Condra03ebf062011-10-12 18:17:24 -07005882.Pp
5883The vi command-line editor in
5884.Nm
5885has basically the same commands as the
5886.Xr vi 1
5887editor with the following exceptions:
5888.Bl -bullet
5889.It
5890You start out in insert mode.
5891.It
5892There are file name and command completion commands:
Elliott Hughes966dd552016-12-08 15:56:04 -08005893=, \e, *, \*(haX, \*(haE, \*(haF and, optionally,
Geremy Condra03ebf062011-10-12 18:17:24 -07005894.Aq tab
5895and
5896.Aq esc .
5897.It
5898The
5899.Ic _
5900command is different (in
5901.Nm mksh ,
5902it is the last argument command; in
5903.Xr vi 1
5904it goes to the start of the current line).
5905.It
5906The
5907.Ic /
5908and
5909.Ic G
5910commands move in the opposite direction to the
5911.Ic j
5912command.
5913.It
5914Commands which don't make sense in a single line editor are not available
5915(e.g. screen movement commands and
5916.Xr ex 1 Ns -style
5917colon
5918.Pq Ic \&:
5919commands).
5920.El
5921.Pp
5922Like
5923.Xr vi 1 ,
5924there are two modes:
5925.Dq insert
5926mode and
5927.Dq command
5928mode.
5929In insert mode, most characters are simply put in the buffer at the
5930current cursor position as they are typed; however, some characters are
5931treated specially.
5932In particular, the following characters are taken from current
5933.Xr tty 4
5934settings
5935(see
5936.Xr stty 1 )
5937and have their usual meaning (normal values are in parentheses): kill (\*(haU),
Elliott Hughes966dd552016-12-08 15:56:04 -08005938erase (\*(ha?), werase (\*(haW), eof (\*(haD), intr (\*(haC) and quit (\*(ha\e).
Geremy Condra03ebf062011-10-12 18:17:24 -07005939In addition to
5940the above, the following characters are also treated specially in insert mode:
5941.Bl -tag -width XJXXXXM
5942.It \*(haE
5943Command and file name enumeration (see below).
5944.It \*(haF
5945Command and file name completion (see below).
5946If used twice in a row, the
5947list of possible completions is displayed; if used a third time, the completion
5948is undone.
5949.It \*(haH
5950Erases previous character.
5951.It \*(haJ \*(Ba \*(haM
5952End of line.
Elliott Hughes966dd552016-12-08 15:56:04 -08005953The current line is read, parsed and executed by the shell.
Geremy Condra03ebf062011-10-12 18:17:24 -07005954.It \*(haV
5955Literal next.
5956The next character typed is not treated specially (can be used
5957to insert the characters being described here).
5958.It \*(haX
5959Command and file name expansion (see below).
5960.It Aq esc
5961Puts the editor in command mode (see below).
5962.It Aq tab
5963Optional file name and command completion (see
5964.Ic \*(haF
5965above), enabled with
Elliott Hughes96b43632015-07-17 11:39:41 -07005966.Ic set Fl o Ic vi\-tabcomplete .
Geremy Condra03ebf062011-10-12 18:17:24 -07005967.El
5968.Pp
5969In command mode, each character is interpreted as a command.
5970Characters that
5971don't correspond to commands, are illegal combinations of commands, or are
5972commands that can't be carried out, all cause beeps.
5973In the following command descriptions, an
5974.Op Ar n
5975indicates the command may be prefixed by a number (e.g.\&
5976.Ic 10l
5977moves right 10 characters); if no number prefix is used,
5978.Ar n
5979is assumed to be 1 unless otherwise specified.
5980The term
5981.Dq current position
5982refers to the position between the cursor and the character preceding the
5983cursor.
5984A
5985.Dq word
Elliott Hughes966dd552016-12-08 15:56:04 -08005986is a sequence of letters, digits and underscore characters or a sequence of
5987non-letter, non-digit, non-underscore and non-whitespace characters (e.g.\&
5988.Dq Li ab2*&\*(ha
Geremy Condra03ebf062011-10-12 18:17:24 -07005989contains two words) and a
5990.Dq big-word
5991is a sequence of non-whitespace characters.
5992.Pp
5993Special
5994.Nm
5995vi commands:
5996.Pp
5997The following commands are not in, or are different from, the normal vi file
5998editor:
5999.Bl -tag -width 10n
6000.It Xo
6001.Oo Ar n Oc Ns _
6002.Xc
6003Insert a space followed by the
6004.Ar n Ns th
6005big-word from the last command in the history at the current position and enter
6006insert mode; if
6007.Ar n
6008is not specified, the last word is inserted.
6009.It #
6010Insert the comment character
Elliott Hughes966dd552016-12-08 15:56:04 -08006011.Pq Ql #
Geremy Condra03ebf062011-10-12 18:17:24 -07006012at the start of the current line and return the line to the shell (equivalent
6013to
6014.Ic I#\*(haJ ) .
6015.It Xo
6016.Oo Ar n Oc Ns g
6017.Xc
6018Like
6019.Ic G ,
6020except if
6021.Ar n
6022is not specified, it goes to the most recent remembered line.
6023.It Xo
6024.Oo Ar n Oc Ns v
6025.Xc
6026Edit line
6027.Ar n
6028using the
6029.Xr vi 1
6030editor; if
6031.Ar n
6032is not specified, the current line is edited.
6033The actual command executed is
6034.Ic fc \-e ${VISUAL:\-${EDITOR:\-vi}} Ar n .
6035.It * and \*(haX
6036Command or file name expansion is applied to the current big-word (with an
6037appended
6038.Ql *
6039if the word contains no file globbing characters) \*(en the big-word is replaced
6040with the resulting words.
6041If the current big-word is the first on the line
6042or follows one of the characters
6043.Ql \&; ,
6044.Ql \*(Ba ,
6045.Ql & ,
Elliott Hughes966dd552016-12-08 15:56:04 -08006046.Ql \&(
Geremy Condra03ebf062011-10-12 18:17:24 -07006047or
Elliott Hughes966dd552016-12-08 15:56:04 -08006048.Ql \&)
Geremy Condra03ebf062011-10-12 18:17:24 -07006049and does not contain a slash
Elliott Hughes966dd552016-12-08 15:56:04 -08006050.Pq Ql / ,
Geremy Condra03ebf062011-10-12 18:17:24 -07006051then command expansion is done; otherwise file name expansion is done.
Elliott Hughes966dd552016-12-08 15:56:04 -08006052Command expansion will match the big-word against all aliases, functions and
Geremy Condra03ebf062011-10-12 18:17:24 -07006053built-in commands as well as any executable files found by searching the
6054directories in the
6055.Ev PATH
6056parameter.
6057File name expansion matches the big-word against the files in the
6058current directory.
6059After expansion, the cursor is placed just past the last
6060word and the editor is in insert mode.
6061.It Xo
6062.Oo Ar n Oc Ns \e ,
6063.Oo Ar n Oc Ns \*(haF ,
6064.Oo Ar n Oc Ns Aq tab ,
6065.No and
6066.Oo Ar n Oc Ns Aq esc
6067.Xc
6068Command/file name completion.
6069Replace the current big-word with the
6070longest unique match obtained after performing command and file name expansion.
6071.Aq tab
6072is only recognised if the
6073.Ic vi\-tabcomplete
6074option is set, while
6075.Aq esc
6076is only recognised if the
6077.Ic vi\-esccomplete
6078option is set (see
Elliott Hughes96b43632015-07-17 11:39:41 -07006079.Ic set Fl o ) .
Geremy Condra03ebf062011-10-12 18:17:24 -07006080If
6081.Ar n
6082is specified, the
6083.Ar n Ns th
6084possible completion is selected (as reported by the command/file name
6085enumeration command).
6086.It = and \*(haE
6087Command/file name enumeration.
6088List all the commands or files that match the current big-word.
6089.It \*(haV
6090Display the version of
6091.Nm mksh .
6092The current edit buffer is restored as soon as a key is pressed.
6093The restoring keypress is ignored.
6094.It @ Ns Ar c
6095Macro expansion.
6096Execute the commands found in the alias
6097.Ar c .
6098.El
6099.Pp
6100Intra-line movement commands:
6101.Bl -tag -width Ds
6102.It Xo
6103.Oo Ar n Oc Ns h and
6104.Oo Ar n Oc Ns \*(haH
6105.Xc
6106Move left
6107.Ar n
6108characters.
6109.It Xo
6110.Oo Ar n Oc Ns l and
6111.Oo Ar n Oc Ns Aq space
6112.Xc
6113Move right
6114.Ar n
6115characters.
6116.It 0
6117Move to column 0.
6118.It \*(ha
6119Move to the first non-whitespace character.
6120.It Xo
6121.Oo Ar n Oc Ns \*(Ba
6122.Xc
6123Move to column
6124.Ar n .
6125.It $
6126Move to the last character.
6127.It Xo
6128.Oo Ar n Oc Ns b
6129.Xc
6130Move back
6131.Ar n
6132words.
6133.It Xo
6134.Oo Ar n Oc Ns B
6135.Xc
6136Move back
6137.Ar n
6138big-words.
6139.It Xo
6140.Oo Ar n Oc Ns e
6141.Xc
6142Move forward to the end of the word,
6143.Ar n
6144times.
6145.It Xo
6146.Oo Ar n Oc Ns E
6147.Xc
6148Move forward to the end of the big-word,
6149.Ar n
6150times.
6151.It Xo
6152.Oo Ar n Oc Ns w
6153.Xc
6154Move forward
6155.Ar n
6156words.
6157.It Xo
6158.Oo Ar n Oc Ns W
6159.Xc
6160Move forward
6161.Ar n
6162big-words.
6163.It %
6164Find match.
Elliott Hughes966dd552016-12-08 15:56:04 -08006165The editor looks forward for the nearest parenthesis, bracket or
6166brace and then moves the cursor to the matching parenthesis, bracket or brace.
Geremy Condra03ebf062011-10-12 18:17:24 -07006167.It Xo
6168.Oo Ar n Oc Ns f Ns Ar c
6169.Xc
6170Move forward to the
6171.Ar n Ns th
6172occurrence of the character
6173.Ar c .
6174.It Xo
6175.Oo Ar n Oc Ns F Ns Ar c
6176.Xc
6177Move backward to the
6178.Ar n Ns th
6179occurrence of the character
6180.Ar c .
6181.It Xo
6182.Oo Ar n Oc Ns t Ns Ar c
6183.Xc
6184Move forward to just before the
6185.Ar n Ns th
6186occurrence of the character
6187.Ar c .
6188.It Xo
6189.Oo Ar n Oc Ns T Ns Ar c
6190.Xc
6191Move backward to just before the
6192.Ar n Ns th
6193occurrence of the character
6194.Ar c .
6195.It Xo
6196.Oo Ar n Oc Ns \&;
6197.Xc
6198Repeats the last
Elliott Hughes966dd552016-12-08 15:56:04 -08006199.Ic f , F , t
Geremy Condra03ebf062011-10-12 18:17:24 -07006200or
6201.Ic T
6202command.
6203.It Xo
6204.Oo Ar n Oc Ns \&,
6205.Xc
6206Repeats the last
Elliott Hughes966dd552016-12-08 15:56:04 -08006207.Ic f , F , t
Geremy Condra03ebf062011-10-12 18:17:24 -07006208or
6209.Ic T
6210command, but moves in the opposite direction.
6211.El
6212.Pp
6213Inter-line movement commands:
6214.Bl -tag -width Ds
6215.It Xo
6216.Oo Ar n Oc Ns j ,
6217.Oo Ar n Oc Ns + ,
6218.No and
6219.Oo Ar n Oc Ns \*(haN
6220.Xc
6221Move to the
6222.Ar n Ns th
6223next line in the history.
6224.It Xo
6225.Oo Ar n Oc Ns k ,
6226.Oo Ar n Oc Ns \- ,
6227.No and
6228.Oo Ar n Oc Ns \*(haP
6229.Xc
6230Move to the
6231.Ar n Ns th
6232previous line in the history.
6233.It Xo
6234.Oo Ar n Oc Ns G
6235.Xc
6236Move to line
6237.Ar n
6238in the history; if
6239.Ar n
6240is not specified, the number of the first remembered line is used.
6241.It Xo
6242.Oo Ar n Oc Ns g
6243.Xc
6244Like
6245.Ic G ,
6246except if
6247.Ar n
6248is not specified, it goes to the most recent remembered line.
6249.It Xo
6250.Oo Ar n Oc Ns / Ns Ar string
6251.Xc
6252Search backward through the history for the
6253.Ar n Ns th
6254line containing
6255.Ar string ;
6256if
6257.Ar string
6258starts with
6259.Ql \*(ha ,
6260the remainder of the string must appear at the start of the history line for
6261it to match.
6262.It Xo
6263.Oo Ar n Oc Ns \&? Ns Ar string
6264.Xc
6265Same as
6266.Ic / ,
6267except it searches forward through the history.
6268.It Xo
6269.Oo Ar n Oc Ns n
6270.Xc
6271Search for the
6272.Ar n Ns th
6273occurrence of the last search string;
6274the direction of the search is the same as the last search.
6275.It Xo
6276.Oo Ar n Oc Ns N
6277.Xc
6278Search for the
6279.Ar n Ns th
6280occurrence of the last search string;
6281the direction of the search is the opposite of the last search.
Elliott Hughes96b43632015-07-17 11:39:41 -07006282.It Ar ANSI-CurUp , PC-PgUp
Thorsten Glaser811a5752013-07-25 14:24:45 +00006283Take the characters from the beginning of the line to the current
6284cursor position as search string and do a backwards history search
6285for lines beginning with this string; keep the cursor position.
6286This works only in insert mode and keeps it enabled.
Geremy Condra03ebf062011-10-12 18:17:24 -07006287.El
6288.Pp
6289Edit commands
6290.Bl -tag -width Ds
6291.It Xo
6292.Oo Ar n Oc Ns a
6293.Xc
6294Append text
6295.Ar n
6296times; goes into insert mode just after the current position.
6297The append is
6298only replicated if command mode is re-entered i.e.\&
6299.Aq esc
6300is used.
6301.It Xo
6302.Oo Ar n Oc Ns A
6303.Xc
6304Same as
6305.Ic a ,
6306except it appends at the end of the line.
6307.It Xo
6308.Oo Ar n Oc Ns i
6309.Xc
6310Insert text
6311.Ar n
6312times; goes into insert mode at the current position.
6313The insertion is only
6314replicated if command mode is re-entered i.e.\&
6315.Aq esc
6316is used.
6317.It Xo
6318.Oo Ar n Oc Ns I
6319.Xc
6320Same as
6321.Ic i ,
6322except the insertion is done just before the first non-blank character.
6323.It Xo
6324.Oo Ar n Oc Ns s
6325.Xc
6326Substitute the next
6327.Ar n
6328characters (i.e. delete the characters and go into insert mode).
6329.It S
6330Substitute whole line.
6331All characters from the first non-blank character to the
6332end of the line are deleted and insert mode is entered.
6333.It Xo
6334.Oo Ar n Oc Ns c Ns Ar move-cmd
6335.Xc
6336Change from the current position to the position resulting from
6337.Ar n move-cmd Ns s
6338(i.e. delete the indicated region and go into insert mode); if
6339.Ar move-cmd
6340is
6341.Ic c ,
6342the line starting from the first non-blank character is changed.
6343.It C
6344Change from the current position to the end of the line (i.e. delete to the
6345end of the line and go into insert mode).
6346.It Xo
6347.Oo Ar n Oc Ns x
6348.Xc
6349Delete the next
6350.Ar n
6351characters.
6352.It Xo
6353.Oo Ar n Oc Ns X
6354.Xc
6355Delete the previous
6356.Ar n
6357characters.
6358.It D
6359Delete to the end of the line.
6360.It Xo
6361.Oo Ar n Oc Ns d Ns Ar move-cmd
6362.Xc
6363Delete from the current position to the position resulting from
6364.Ar n move-cmd Ns s ;
6365.Ar move-cmd
6366is a movement command (see above) or
6367.Ic d ,
6368in which case the current line is deleted.
6369.It Xo
6370.Oo Ar n Oc Ns r Ns Ar c
6371.Xc
6372Replace the next
6373.Ar n
6374characters with the character
6375.Ar c .
6376.It Xo
6377.Oo Ar n Oc Ns R
6378.Xc
6379Replace.
6380Enter insert mode but overwrite existing characters instead of
6381inserting before existing characters.
6382The replacement is repeated
6383.Ar n
6384times.
6385.It Xo
6386.Oo Ar n Oc Ns \*(TI
6387.Xc
6388Change the case of the next
6389.Ar n
6390characters.
6391.It Xo
6392.Oo Ar n Oc Ns y Ns Ar move-cmd
6393.Xc
6394Yank from the current position to the position resulting from
6395.Ar n move-cmd Ns s
6396into the yank buffer; if
6397.Ar move-cmd
6398is
6399.Ic y ,
6400the whole line is yanked.
6401.It Y
6402Yank from the current position to the end of the line.
6403.It Xo
6404.Oo Ar n Oc Ns p
6405.Xc
6406Paste the contents of the yank buffer just after the current position,
6407.Ar n
6408times.
6409.It Xo
6410.Oo Ar n Oc Ns P
6411.Xc
6412Same as
6413.Ic p ,
6414except the buffer is pasted at the current position.
6415.El
6416.Pp
6417Miscellaneous vi commands
6418.Bl -tag -width Ds
6419.It \*(haJ and \*(haM
Elliott Hughes966dd552016-12-08 15:56:04 -08006420The current line is read, parsed and executed by the shell.
Geremy Condra03ebf062011-10-12 18:17:24 -07006421.It \*(haL and \*(haR
6422Redraw the current line.
6423.It Xo
6424.Oo Ar n Oc Ns \&.
6425.Xc
6426Redo the last edit command
6427.Ar n
6428times.
6429.It u
6430Undo the last edit command.
6431.It U
6432Undo all changes that have been made to the current line.
Elliott Hughes966dd552016-12-08 15:56:04 -08006433.It PC Home, End, Del and cursor keys
Elliott Hughes96b43632015-07-17 11:39:41 -07006434They move as expected, both in insert and command mode.
Geremy Condra03ebf062011-10-12 18:17:24 -07006435.It Ar intr No and Ar quit
6436The interrupt and quit terminal characters cause the current line to be
6437deleted and a new prompt to be printed.
6438.El
6439.Sh FILES
6440.Bl -tag -width XetcXsuid_profile -compact
6441.It Pa \*(TI/.mkshrc
6442User mkshrc profile (non-privileged interactive shells); see
6443.Sx Startup files.
6444The location can be changed at compile time (for embedded systems);
6445AOSP Android builds use
6446.Pa /system/etc/mkshrc .
6447.It Pa \*(TI/.profile
6448User profile (non-privileged login shells); see
6449.Sx Startup files
6450near the top of this manual.
6451.It Pa /etc/profile
6452System profile (login shells); see
6453.Sx Startup files.
6454.It Pa /etc/shells
6455Shell database.
6456.It Pa /etc/suid_profile
6457Suid profile (privileged shells); see
6458.Sx Startup files.
6459.El
6460.Pp
6461Note: On Android,
6462.Pa /system/etc/
6463contains the system and suid profile.
6464.Sh SEE ALSO
6465.Xr awk 1 ,
6466.Xr cat 1 ,
6467.Xr ed 1 ,
6468.Xr getopt 1 ,
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006469.Xr lksh 1 ,
Geremy Condra03ebf062011-10-12 18:17:24 -07006470.Xr sed 1 ,
6471.Xr sh 1 ,
6472.Xr stty 1 ,
6473.Xr dup 2 ,
6474.Xr execve 2 ,
6475.Xr getgid 2 ,
6476.Xr getuid 2 ,
6477.Xr mknod 2 ,
6478.Xr mkfifo 2 ,
6479.Xr open 2 ,
6480.Xr pipe 2 ,
6481.Xr rename 2 ,
6482.Xr wait 2 ,
6483.Xr getopt 3 ,
6484.Xr nl_langinfo 3 ,
6485.Xr setlocale 3 ,
6486.Xr signal 3 ,
6487.Xr system 3 ,
6488.Xr tty 4 ,
6489.Xr shells 5 ,
6490.Xr environ 7 ,
6491.Xr script 7 ,
6492.Xr utf\-8 7 ,
6493.Xr mknod 8
6494.Pp
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006495.Pa http://www.mirbsd.org/ksh\-chan.htm
Geremy Condra03ebf062011-10-12 18:17:24 -07006496.Rs
6497.%A Morris Bolsky
6498.%B "The KornShell Command and Programming Language"
6499.%D 1989
6500.%I "Prentice Hall PTR"
6501.%P "xvi\ +\ 356 pages"
6502.%O "ISBN 978\-0\-13\-516972\-8 (0\-13\-516972\-0)"
6503.Re
6504.Rs
6505.%A Morris I. Bolsky
6506.%A David G. Korn
6507.%B "The New KornShell Command and Programming Language (2nd Edition)"
6508.%D 1995
6509.%I "Prentice Hall PTR"
6510.%P "xvi\ +\ 400 pages"
6511.%O "ISBN 978\-0\-13\-182700\-4 (0\-13\-182700\-6)"
6512.Re
6513.Rs
6514.%A Stephen G. Kochan
6515.%A Patrick H. Wood
6516.%B "\\*(tNUNIX\\*(sP Shell Programming"
Elliott Hughes737fdce2014-08-07 12:59:26 -07006517.%V "3rd Edition"
6518.%D 2003
6519.%I "Sams"
6520.%P "xiii\ +\ 437 pages"
6521.%O "ISBN 978\-0\-672\-32490\-1 (0\-672\-32490\-3)"
Geremy Condra03ebf062011-10-12 18:17:24 -07006522.Re
6523.Rs
6524.%A "IEEE Inc."
Elliott Hughes737fdce2014-08-07 12:59:26 -07006525.%T "\\*(tNIEEE\\*(sP Standard for Information Technology \*(en Portable Operating System Interface (POSIX)"
Geremy Condra03ebf062011-10-12 18:17:24 -07006526.%V "Part 2: Shell and Utilities"
6527.%D 1993
6528.%I "IEEE Press"
6529.%P "xvii\ +\ 1195 pages"
6530.%O "ISBN 978\-1\-55937\-255\-8 (1\-55937\-255\-9)"
6531.Re
6532.Rs
6533.%A Bill Rosenblatt
6534.%B "Learning the Korn Shell"
6535.%D 1993
6536.%I "O'Reilly"
6537.%P "360 pages"
6538.%O "ISBN 978\-1\-56592\-054\-5 (1\-56592\-054\-6)"
6539.Re
6540.Rs
6541.%A Bill Rosenblatt
6542.%A Arnold Robbins
6543.%B "Learning the Korn Shell, Second Edition"
6544.%D 2002
6545.%I "O'Reilly"
6546.%P "432 pages"
6547.%O "ISBN 978\-0\-596\-00195\-7 (0\-596\-00195\-9)"
6548.Re
6549.Rs
6550.%A Barry Rosenberg
6551.%B "KornShell Programming Tutorial"
6552.%D 1991
6553.%I "Addison-Wesley Professional"
6554.%P "xxi\ +\ 324 pages"
6555.%O "ISBN 978\-0\-201\-56324\-5 (0\-201\-56324\-X)"
6556.Re
6557.Sh AUTHORS
Elliott Hughes737fdce2014-08-07 12:59:26 -07006558.An -nosplit
Geremy Condra03ebf062011-10-12 18:17:24 -07006559.Nm "The MirBSD Korn Shell"
6560is developed by
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006561.An mirabilos Aq Mt m@mirbsd.org
6562as part of The MirOS Project.
Elliott Hughes737fdce2014-08-07 12:59:26 -07006563This shell is based on the public domain 7th edition Bourne shell clone by
6564.An Charles Forsyth ,
6565who kindly agreed to, in countries where the Public Domain status of the work
6566may not be valid, grant a copyright licence to the general public to deal in
Elliott Hughes966dd552016-12-08 15:56:04 -08006567the work without restriction and permission to sublicence derivatives under
6568the terms of any (OSI approved) Open Source licence,
Elliott Hughes737fdce2014-08-07 12:59:26 -07006569and parts of the BRL shell by
6570.An Doug A. Gwyn ,
6571.An Doug Kingston ,
6572.An Ron Natalie ,
6573.An Arnold Robbins ,
Elliott Hughes966dd552016-12-08 15:56:04 -08006574.An Lou Salkind
Elliott Hughes737fdce2014-08-07 12:59:26 -07006575and others.
6576The first release of
6577.Nm pdksh
6578was created by
6579.An Eric Gisin ,
6580and it was subsequently maintained by
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006581.An John R. MacMillan ,
Elliott Hughes966dd552016-12-08 15:56:04 -08006582.An Simon J. Gerraty
Elliott Hughes737fdce2014-08-07 12:59:26 -07006583and
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006584.An Michael Rendell .
Elliott Hughes737fdce2014-08-07 12:59:26 -07006585The effort of several projects, such as Debian and OpenBSD, and other
6586contributors including our users, to improve the shell is appreciated.
Elliott Hughes966dd552016-12-08 15:56:04 -08006587See the documentation, web site and CVS for details.
Thorsten Glaser811a5752013-07-25 14:24:45 +00006588.Pp
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006589.Nm mksh\-os2
6590is developed by
6591.An KO Myung-Hun Aq Mt komh@chollian.net .
6592.Pp
6593.Nm mksh\-w32
6594is developed by
6595.An Michael Langguth Aq Mt lan@scalaris.com .
6596.Pp
Thorsten Glaser811a5752013-07-25 14:24:45 +00006597The BSD daemon is Copyright \(co Marshall Kirk McKusick.
6598The complete legalese is at:
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006599.Pa http://www.mirbsd.org/TaC\-mksh.txt
Thorsten Glaser811a5752013-07-25 14:24:45 +00006600.\"
6601.\" This boils down to: feel free to use mksh.ico as application icon
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006602.\" or shortcut for mksh or mksh/Win32 or OS/2; distro patches are ok
6603.\" (but we request they amend $KSH_VERSION when modifying mksh).
6604.\" Authors are Marshall Kirk McKusick (UCB), Rick Collette (ekkoBSD),
6605.\" mirabilos, Benny Siegert (MirBSD), Michael Langguth (mksh/Win32),
6606.\" KO Myung-Hun (mksh for OS/2).
Thorsten Glaser811a5752013-07-25 14:24:45 +00006607.\"
6608.\" As far as MirBSD is concerned, the files themselves are free
6609.\" to modification and distribution under BSD/MirOS Licence, the
6610.\" restriction on use stems only from trademark law's requirement
6611.\" to protect it or lose it, which McKusick almost did.
6612.\"
Geremy Condra03ebf062011-10-12 18:17:24 -07006613.Sh CAVEATS
Thorsten Glaser811a5752013-07-25 14:24:45 +00006614.Nm mksh
Elliott Hughes966dd552016-12-08 15:56:04 -08006615provides a consistent 32-bit integer arithmetic implementation, both
6616signed and unsigned, with sign of the result of a remainder operation
6617and wraparound defined, even (defying POSIX) on 36-bit and 64-bit systems.
Elliott Hughes50012062015-03-10 22:22:24 -07006618.Pp
6619.Nm mksh
6620provides a consistent, clear interface normally.
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006621This may deviate from POSIX in historic or opinionated places.
Elliott Hughes96b43632015-07-17 11:39:41 -07006622.Ic set Fl o Ic posix
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006623(see
6624.Sx POSIX mode
6625for details)
6626will cause the shell to behave more conformant.
Elliott Hughesb27ce952015-04-21 13:39:18 -07006627.Pp
6628For the purpose of
6629.Tn POSIX ,
6630.Nm mksh
6631supports only the
6632.Dq C
6633locale.
Elliott Hughes96b43632015-07-17 11:39:41 -07006634.Nm mksh Ns 's
6635.Ic utf8\-mode
6636only supports the Unicode BMP (Basic Multilingual Plane) and maps
6637raw octets into the U+EF80..U+EFFF wide character range; compare
6638.Sx Arithmetic expressions .
6639The following
6640.Tn POSIX
6641.Nm sh
6642code toggles the
6643.Ic utf8\-mode
6644option dependent on the current
6645.Tn POSIX
6646locale for mksh to allow using the UTF-8 mode, within the constraints
6647outlined above, in code portable across various shell implementations:
Elliott Hughesb27ce952015-04-21 13:39:18 -07006648.Bd -literal -offset indent
6649case ${KSH_VERSION:\-} in
6650*MIRBSD\ KSH*\*(Ba*LEGACY\ KSH*)
6651 case ${LC_ALL:\-${LC_CTYPE:\-${LANG:\-}}} in
6652 *[Uu][Tt][Ff]8*\*(Ba*[Uu][Tt][Ff]\-8*) set \-U ;;
6653 *) set +U ;;
6654 esac ;;
6655esac
6656.Ed
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006657In near future, (Unicode) locale tracking will be implemented though.
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006658.Pp
6659See also the FAQ below.
Geremy Condra03ebf062011-10-12 18:17:24 -07006660.Sh BUGS
6661Suspending (using \*(haZ) pipelines like the one below will only suspend
6662the currently running part of the pipeline; in this example,
Elliott Hughes966dd552016-12-08 15:56:04 -08006663.Dq Li fubar
Geremy Condra03ebf062011-10-12 18:17:24 -07006664is immediately printed on suspension (but not later after an
6665.Ic fg ) .
6666.Bd -literal -offset indent
6667$ /bin/sleep 666 && echo fubar
6668.Ed
6669.Pp
Elliott Hughesb27ce952015-04-21 13:39:18 -07006670The truncation process involved when changing
6671.Ev HISTFILE
6672does not free old history entries (leaks memory) and leaks
6673old entries into the new history if their line numbers are
Elliott Hughes966dd552016-12-08 15:56:04 -08006674not overwritten by same-number entries from the persistent
Elliott Hughesb27ce952015-04-21 13:39:18 -07006675history file; truncating the on-disc file to
6676.Ev HISTSIZE
6677lines has always been broken and prone to history file corruption
6678when multiple shells are accessing the file; the rollover process
6679for the in-memory portion of the history is slow, should use
6680.Xr memmove 3 .
6681.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07006682This document attempts to describe
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006683.Nm mksh\ R55
Geremy Condra03ebf062011-10-12 18:17:24 -07006684and up,
Elliott Hughesb27ce952015-04-21 13:39:18 -07006685.\" with vendor patches from insert-your-name-here,
Geremy Condra03ebf062011-10-12 18:17:24 -07006686compiled without any options impacting functionality, such as
6687.Dv MKSH_SMALL ,
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00006688when not called as
6689.Pa /bin/sh
6690which, on some systems only, enables
Elliott Hughes96b43632015-07-17 11:39:41 -07006691.Ic set Fl o Ic posix
Elliott Hughes50012062015-03-10 22:22:24 -07006692or
Elliott Hughes96b43632015-07-17 11:39:41 -07006693.Ic set Fl o Ic sh
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00006694automatically (whose behaviour differs across targets),
Geremy Condra03ebf062011-10-12 18:17:24 -07006695for an operating environment supporting all of its advanced needs.
Elliott Hughes50012062015-03-10 22:22:24 -07006696.Pp
Geremy Condra03ebf062011-10-12 18:17:24 -07006697Please report bugs in
6698.Nm
6699to the
Elliott Hughesfc0307d2016-02-02 15:26:47 -08006700.Aq Mt miros\-mksh@mirbsd.org
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006701mailing list
Geremy Condra03ebf062011-10-12 18:17:24 -07006702or in the
6703.Li \&#\&!/bin/mksh
6704.Pq or Li \&#ksh
6705IRC channel at
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00006706.Pa irc.freenode.net
6707.Pq Port 6697 SSL, 6667 unencrypted ,
6708or at:
6709.Pa https://launchpad.net/mksh
Elliott Hughesa3c3f962017-04-12 16:52:30 -07006710.Sh FREQUENTLY ASKED QUESTIONS
6711This FAQ attempts to document some of the questions users of
6712.Nm
6713or readers of this manual page may encounter.
6714.Ss I'm an Android user, so what's mksh?
6715.Nm mksh
6716is a
6717.Ux
6718shell / command interpreter, similar to
6719.Nm COMMAND.COM
6720or
6721.Nm CMD.EXE ,
6722which has been included with
6723.Tn Android Open Source Project
6724for a while now.
6725Basically, it's a program that runs in a terminal (console window),
6726takes user input and runs commands or scripts, which it can also
6727be asked to do by other programs, even in the background.
6728Any privilege pop-ups you might be encountering are thus not
6729.Nm mksh
6730issues but questions by some other program utilising it.
6731.Ss "I'm an OS/2 user, what do I need to know?"
6732Unlike the native command prompt, the current working directory is,
6733for security reasons common on Unix systems which the shell is designed for,
6734not in the search path at all; if you really need this, run the command
6735.Li PATH=.$PATHSEP$PATH
6736or add that to a suitable initialisation file.
6737.Pp
6738There are two different newline modes for mksh-os2: standard (Unix) mode,
6739in which only LF (0A hex) is supported as line separator, and "textmode",
6740which also accepts ASCII newlines (CR+LF), like most other tools on OS/2,
6741but creating an incompatibility with standard
6742.Nm .
6743If you compiled mksh from source, you will get the standard Unix mode unless
6744.Fl T
6745is added during compilation; you will most likely have gotten this shell
6746through komh's port on Hobbes, or from his OS/2 Factory on eComStation
6747Korea, which uses "textmode", though.
6748Most OS/2 users will want to use "textmode" unless they need absolute
6749compatibility with Unix
6750.Nm .
6751.Ss "How do I start mksh on a specific terminal?"
6752Normally:
6753.Dl mksh \-T/dev/tty2
6754.Pp
6755However, if you want for it to return (e.g. for an embedded
6756system rescue shell), use this on your real console device instead:
6757.Dl mksh \-T!/dev/ttyACM0
6758.Pp
6759.Nm
6760can also daemonise (send to the background):
6761.Dl mksh \-T\- \-c \*(aqexec cdio lock\*(aq
6762.Ss "POSIX says..."
6763Run the shell in POSIX mode (and possibly
6764.Nm lksh
6765instead of
6766.Nm mksh ) :
6767.Dl set \-o posix
6768.Ss "My prompt from <some other shell> does not work!"
6769Contact us on the mailing list or on IRC, we'll convert it for you.
6770.Ss "Something is going wrong with my while...read loop"
6771Most likely, you've encountered the problem in which the shell runs
6772all parts of a pipeline as subshell.
6773The inner loop will be executed in a subshell and variable changes
6774cannot be propagated if run in a pipeline:
6775.Bd -literal -offset indent
6776bar \*(Ba baz \*(Ba while read foo; do ...; done
6777.Ed
6778.Pp
6779Note that
6780.Ic exit
6781in the inner loop will only exit the subshell and not the original shell.
6782Likewise, if the code is inside a function,
6783.Ic return
6784in the inner loop will only exit the subshell and won't terminate the function.
6785.Pp
6786Use co-processes instead:
6787.Bd -literal -offset indent
6788bar \*(Ba baz \*(Ba&
6789while read \-p foo; do ...; done
6790exec 3\*(Gt&p; exec 3\*(Gt&\-
6791.Ed
6792.Pp
6793If
6794.Ic read
6795is run in a loop such as
6796.Ic while read foo; do ...; done
6797then leading whitespace will be removed (IFS) and backslashes processed.
6798You might want to use
6799.Ic while IFS= read \-r foo; do ...; done
6800for pristine I/O.
6801Similarly, when using the
6802.Fl a
6803option, use of the
6804.Fl r
6805option might be prudent
6806.Pq Dq Li read \-raN\-1 arr \*(Ltfile ;
6807the same applies for NUL-terminated lines:
6808.Bd -literal -offset indent
6809find . \-type f \-print0 \*(Ba& \e
6810 while IFS= read \-d \*(aq\*(aq \-pr filename; do
6811 print \-r \-\- "found \*(Lt${filename#./}\*(Gt"
6812done
6813.Ed
6814.Pp
6815.Ss "What differences in function-local scopes are there?"
6816.Nm
6817has a different scope model from
6818.At
6819.Nm ksh ,
6820which leads to subtle differences in semantics for identical builtins.
6821This can cause issues with a
6822.Ic nameref
6823to suddenly point to a local variable by accident.
6824.Pp
6825.Tn GNU
6826.Nm bash
6827allows unsetting local variables; in
6828.Nm ,
6829doing so in a function allows back access to the global variable
6830(actually the one in the next scope up) with the same name.
6831The following code, when run before the function definitions, changes
6832the behaviour of
6833.Ic unset
6834to behave like other shells (the alias can be removed after the definitions):
6835.Bd -literal -offset indent
6836case ${KSH_VERSION:\-} in
6837*MIRBSD\ KSH*\*(Ba*LEGACY\ KSH*)
6838 function unset_compat {
6839 \e\ebuiltin typeset unset_compat_x
6840
6841 for unset_compat_x in "$@"; do
6842 eval "\e\e\e\ebuiltin unset $unset_compat_x[*]"
6843 done
6844 }
6845 \e\ebuiltin alias unset=unset_compat
6846 ;;
6847esac
6848.Ed
6849.Pp
6850When a local variable is created (e.g. using
6851.Ic local ,
6852.Ic typeset ,
6853.Ic integer ,
6854.Ic \e\ebuiltin typeset )
6855it does not, like in other shells, inherit the value from the global
6856(next scope up) variable with the same name; it is rather created
6857without any value (unset but defined).
6858.Ss "I get an error in this regex comparison"
6859Use extglobs instead of regexes:
6860.Dl "[[ foo =~ (foo\*(Babar).*baz ]] # becomes"
6861.Dl "[[ foo = *@(foo\*(Babar)*baz* ]] # instead"
6862.Ss "Are there any extensions to avoid?"
6863.Tn GNU
6864.Nm bash
6865supports
6866.Dq Li &\*(Gt
6867.Pq and Dq Li \*(Ba&
6868to redirect both stdout and stderr in one go, but this breaks POSIX
6869and Korn Shell syntax; use POSIX redirections instead:
6870.Dl "foo \*(Ba& bar \*(Ba& baz &\*(Gtlog # GNU bash"
6871.Dl "foo 2\*(Gt&1 \*(Ba bar 2\*(Gt&1 \*(Ba baz \*(Gtlog 2\*(Gt&1 # POSIX"
6872.Ss "\*(haL (Ctrl-L) does not clear the screen"
6873Use \*(ha[\*(haL (Escape+Ctrl-L) or rebind it:
6874.Dl bind \*(aq\*(haL=clear-screen\*(aq
6875.Ss "\*(haU (Ctrl-U) clears the entire line"
6876If it should only delete the line up to the cursor, use:
6877.Dl bind \-m \*(haU=\*(aq\*(ha[0\*(haK\*(aq
6878.Ss "Cursor Up behaves differently from zsh"
6879Some shells make Cursor Up search in the history only for
6880commands starting with what was already entered.
6881.Nm
6882separates the shortcuts: Cursor Up goes up one command
6883and PgUp searches the history as described above.