OSDN Git Service

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