OSDN Git Service

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