OSDN Git Service

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