OSDN Git Service

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