ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/src/vendor/MirOS/mksh/R56c/mksh.1
Revision: 12139
Committed: Fri Jan 18 20:59:20 2019 UTC (5 years, 4 months ago) by laffer1
Original Path: vendor/MirOS/mksh/dist/mksh.1
File size: 175249 byte(s)
Log Message:
Import mksh r56c

File Contents

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