OSDN Git Service

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