OSDN Git Service

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