OSDN Git Service

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