forked from MirBSD/mksh
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mksh.1
7390 lines (7390 loc) · 178 KB
/
mksh.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.\" $MirOS: src/bin/mksh/mksh.1,v 1.543 2024/02/02 02:33:07 tg Exp $
.\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
.\" 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
.\" 2018, 2019, 2020, 2021, 2022, 2023, 2024
.\" mirabilos <[email protected]>
.\"
.\" Provided that these terms and disclaimer and all copyright notices
.\" are retained or reproduced in an accompanying document, permission
.\" is granted to deal in this work without restriction, including un‐
.\" limited rights to use, publicly perform, distribute, sell, modify,
.\" merge, give away, or sublicence.
.\"
.\" This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
.\" the utmost extent permitted by applicable law, neither express nor
.\" implied; without malicious intent or gross negligence. In no event
.\" may a licensor, author or contributor be held liable for indirect,
.\" direct, other damage, loss, or other issues arising in any way out
.\" of dealing in the work, even if advised of the possibility of such
.\" damage or existence of a defect, except proven that it results out
.\" of said person’s immediate fault when using the work as intended.
.\"-
.\" $miros: contrib/samples/portmdoc,v 1.23 2024/01/04 22:52:50 tg Exp $
.\"-
.\" Copyright © 2008, 2009, 2010, 2016, 2018, 2020, 2023
.\" mirabilos <[email protected]>
.\"
.\" Glue GNU groff (BSD and GNU mdoc both) to AT&T nroff (UCB mdoc).
.\" * ` generates ‘ in gnroff, so use \`
.\" * ' generates ’ in gnroff, \' generates ´, so use \*(aq
.\" * - generates ‐ in gnroff, \- generates −, so .tr it to -
.\" thus use - for hyphens and \- for minus signs and option dashes
.\" * ~ is size-reduced and placed atop in groff, so use \*(TI
.\" * ^ is size-reduced and placed atop in groff, so use \*(ha
.\" * \(en does not work in nroff, so use \*(en for a solo en dash
.\" * and \*(EM for a correctly spaced em dash
.\" * <>| are problematic, so redefine and use \*(Lt\*(Gt\*(Ba
.\" Also make sure to use \& *before* a punctuation char that is to not
.\" be interpreted as punctuation, and especially with two-letter words
.\" but also (after) a period that does not end a sentence (“e.g.\&”).
.\"-
.\"
.\" Implement .Dd with the Mdocdate RCS keyword
.\"
.rn Dd xD
.de Dd
.ie \\$1$Mdocdate: \{\
. xD \\$2 \\$3, \\$4
.\}
.el .xD \\$1 \\$2 \\$3
..
.\"
.\" .Dd must come before most everything, because when called
.\" with -mandoc it loads -mdoc via .so in .Dd (first macro).
.\"
.Dd $Mdocdate: February 2 2024 $
.\"
.\" Check which macro package we use, and do other -mdoc setup.
.\"
.ie \n(.g \{\
. if n .ss \n[.ss] 0
. if \*[.T]ascii .tr \-\N'45'
. if \*[.T]latin1 .tr \-\N'45'
. if \*[.T]utf8 .tr \-\N'45'
. if \*[.T]utf8 .tr \(la\*(Lt
. if \*[.T]utf8 .tr \(ra\*(Gt
. ds <= \(<=
. ds >= \(>=
. ds Rq \(rq
. ds Lq \(lq
. ds sL \(aq
. ds sR \(aq
. if \*[.T]utf8 .ds sL `
. if \*[.T]ps .ds sL `
. if \*[.T]utf8 .ds sR '
. if \*[.T]ps .ds sR '
. ds aq \(aq
. ds TI \(ti
. ds ha \(ha
. ds en \(en
. ie d volume-ds-1 .ds tT gnu
. el .ie d doc-volume-ds-1 .ds tT gnp
. el .ds tT bsd
.\}
.el \{\
. ds aq '
. ds TI ~
. ds ha ^
. ds en \(em
. ds tT ucb
.\}
.ie n \{\
. ds EM \ \(em\ \&
.\}
.el \{\
. ds EM \f(TR\|\(em\|\fP
.\}
.\"
.\" Add UCB mdoc compatibility to GNU mdoc
.\" Implement .Mx (MirBSD)
.\"
.ie "\*(tT"gnu" \{\
. ds sP \s0
. ds tN \*[Tn-font-size]
. eo
. de Mx
. nr curr-font \n[.f]
. nr curr-size \n[.ps]
. ds str-Mx \f[\n[curr-font]]\s[\n[curr-size]u]
. ds str-Mx1 \*(tN\%MirBSD\*[str-Mx]
. if !\n[arg-limit] \
. if \n[.$] \{\
. ds macro-name Mx
. parse-args \$@
. \}
. if (\n[arg-limit] > \n[arg-ptr]) \{\
. nr arg-ptr +1
. ie (\n[type\n[arg-ptr]] == 2) \
. ds str-Mx1 \*(tN\%MirBSD\~#\*[arg\n[arg-ptr]]\*[str-Mx]
. el \
. nr arg-ptr -1
. \}
. ds arg\n[arg-ptr] "\*[str-Mx1]
. nr type\n[arg-ptr] 2
. ds space\n[arg-ptr] "\*[space]
. nr num-args (\n[arg-limit] - \n[arg-ptr])
. nr arg-limit \n[arg-ptr]
. if \n[num-args] \
. parse-space-vector
. print-recursive
..
. de Aq
. if !\n[arg-limit] \
. ds macro-name Aq
. ie \n[in-authors-section] \{\
. ds quote-left \*(Lt
. ds quote-right \*(Gt
. \}
. el \{\
. ds quote-left \[la]
. ds quote-right \[ra]
. \}
. enclose-string \$@
..
. ec
.\}
.el .ie "\*(tT"gnp" \{\
. ds sP \s0
. ie t .ds tN \s[(\n[.ps]u-1z)]
. el .ds tN
. eo
. de Mx
. nr doc-curr-font \n[.f]
. nr doc-curr-size \n[.ps]
. ds doc-str-Mx \f[\n[doc-curr-font]]\s[\n[doc-curr-size]u]
. ds doc-str-Mx1 \*(tN\%MirBSD\*[doc-str-Mx]
. if !\n[doc-arg-limit] \
. if \n[.$] \{\
. ds doc-macro-name Mx
. doc-parse-args \$@
. \}
. if (\n[doc-arg-limit] > \n[doc-arg-ptr]) \{\
. nr doc-arg-ptr +1
. ie (\n[doc-type\n[doc-arg-ptr]] == 2) \
. ds doc-str-Mx1 \*(tN\%MirBSD\~#\*[doc-arg\n[doc-arg-ptr]]\*[doc-str-Mx]
. el \
. nr doc-arg-ptr -1
. \}
. ds doc-arg\n[doc-arg-ptr] "\*[doc-str-Mx1]
. nr doc-type\n[doc-arg-ptr] 2
. ds doc-space\n[doc-arg-ptr] "\*[doc-space]
. nr doc-num-args (\n[doc-arg-limit] - \n[doc-arg-ptr])
. nr doc-arg-limit \n[doc-arg-ptr]
. if \n[doc-num-args] \
. doc-parse-space-vector
. doc-print-recursive
..
. ec
.\}
.el \{\
. de Mx
. nr cF \\n(.f
. nr cZ \\n(.s
. ds aa \&\f\\n(cF\s\\n(cZ
. if \\n(aC==0 \{\
. ie \\n(.$==0 \&\\*(tNMirBSD\\*(aa
. el .aV \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
. \}
. if \\n(aC>\\n(aP \{\
. nr aP \\n(aP+1
. ie \\n(C\\n(aP==2 \{\
. nr xX 0
. nr xX 1+\\*(A\\n(aP
. as b1 \&\\*(tNMirBSD\ \&
. if \\n(xX>0 .as b1 #\&
. as b1 \&\\*(A\\n(aP\\*(aa
. rr xX
. ie \\n(aC>\\n(aP \{\
. nr aP \\n(aP+1
. nR
. \}
. el .aZ
. \}
. el \{\
. as b1 \&\\*(tNMirBSD\\*(aa
. nR
. \}
. \}
..
.\}
.\" </MirCVS://contrib/samples/portmdoc>
.\"-
.Dt MKSH 1
.Os MirBSD
.Sh NAME
.Nm mksh ,
.Nm sh
.Nd MirBSD Korn shell
.Sh SYNOPSIS
.Nm
.Op Fl +abCefhiklmnprUuvXx
.Op Fl +o Ar option
.Oo Fl T Oo Ar !\&
.Oc Ns Ar tty Ns \*(Ba Ns Ar \-\&
.Oc
.Op Ar file Op Ar arg1 ...
.Nm
.Op Fl +abCefhiklmnprUuvXx
.Op Fl +o Ar option
.Oo Fl T Oo Ar !\&
.Oc Ns Ar tty Ns \*(Ba Ns Ar \-\&
.Oc
.Fl c
.Ar cmd
.Op Ar arg0 ...
.Nm
.Op Fl +abCefhiklmnprUuvXx
.Op Fl +o Ar option
.Oo Fl T Oo Ar !\&
.Oc Ns Ar tty Ns \*(Ba Ns Ar \-\&
.Oc
.Fl s
.Op Ar arg1 ...
.Nm builtin-name
.Op Ar argument ...
.Sh DESCRIPTION
.Nm
is a command interpreter intended for both interactive and shell
script use.
Its command language is a superset of both
.Xr sh C
and
.Tn POSIX
shell language and largely compatible to the original Korn shell.
At times, this manual page may give scripting advice; while it
sometimes does take portable shell scripting or various standards
into account all information is first and foremost presented with
.Nm
in mind and should be taken as such.
.Ss I use Android, OS/2, etc. so what...?
Please refer to:
.Pa http://www.mirbsd.org/mksh\-faq.htm#sowhatismksh
.Ss Invocation
Most builtins can be called directly, for example if a link or
.Xr symlink 7
points to
.Nm ,
or if
.Va argv[0]
is set correspondingly;
this does not make sense for, or works properly with, all built-in utilities though.
.Pp
The options are as follows:
.Bl -tag -width 2n
.It Fl c
.Nm
will execute the command(s) contained in
.Ar cmd ,
setting
.Li $0
to
.Ar arg0
.Pq if present ,
.Li $1
to the next argument, etc.
If compiled with
.Dv \-DMKSH_MIDNIGHTBSD01ASH_COMPAT
and in
.Fl o Ic sh
mode, a
.Dq Li \-\-
argument directly following
.Ar cmd
is ignored for compatibility with the legacy
.Fx
.Nm sh ;
this is deprecated and may go away in the future.
.It Fl i
Interactive shell.
A shell that reads commands from standard input is
.Dq interactive
if this
option is used or if both standard input and standard error are attached
to a
.Xr tty 4 .
An interactive shell has job control enabled, ignores the
.Dv SIGINT ,
.Dv SIGQUIT
and
.Dv SIGTERM
signals, and prints prompts before reading input (see the
.Ev PS1
and
.Ev PS2
parameters).
It also processes the
.Ev ENV
parameter or the
.Pa mkshrc
file (see below).
For non-interactive shells, the
.Ic trackall
option is on by default (see the
.Ic set
command below).
.It Fl l
Login shell.
If the name the shell is called as
.Pq i.e.\& Va argv[0]
or its basename begins with a dash
.Pq hyphen-minus
.Ql \-
or if this option is given,
the shell is a
.Dq login shell ;
see
.Sx Startup files
below.
.It Fl p
Privileged shell.
A shell is
.Dq privileged
if the real user ID from
.Xr getuid 2
or group ID from
.Xr getgid 2
does not match the effective user ID or group ID.
Clearing the privileged option causes the shell to set
its effective user ID and group ID to its initial real user ID
and group ID, respectively.
For further implications, see
.Ic set Fl p
and
.Sx Startup files .
If the shell is privileged and this flag is not set explicitly on invocation,
nor during processing the startup files, the
.Dq privileged
flag is cleared automatically afterwards.
.It Fl r
Restricted shell.
A shell is
.Dq restricted
if the basename the shell is called with, after dash removal, begins with
.Ql r
or if this option is used.
The following restrictions come into effect after the shell processes any
profile and
.Ev ENV
files:
.Pp
.Bl -bullet -compact
.It
Command names cannot be specified with pathnames, either absolute or relative;
the
.Fl p
flag of the
.Ic command
built-in utility is not usable.
The
.Ev ENV ,
.Ev PATH
and
.Ev SHELL
parameters cannot be changed.
.It
The current location is fixed: the
.Ic cd
builtin is disabled.
.It
Redirections that create files, i.e.\&
.Dq Li \*(Gt ,
.Dq Li \*(Gt\*(Ba ,
.Dq Li \*(Gt\*(Gt
and
.Dq Li \*(Lt\*(Gt ,
cannot be used, and the
.Ev HISTFILE
parameter cannot be changed.
.El
.It Fl s
.Nm
will read and execute commands from standard input; all non-option arguments
are assigned to the positional parameters.
.It Fl T Ar \-
Detach from the controlling terminal, return immediately
.Pq daemonise .
.It Xo
.Fl T Oo Ar !\& Oc Ns Ar name
.Xc
Spawn
.Nm
on the
.Xr tty 4
device given.
The paths
.Ar name ,
.Li /dev/ttyC Ns Ar name
and
.Li /dev/tty Ns Ar name
are attempted in order.
If
.Ar name
is prefixed with an exclamation mark
.Pq Ql \&! ,
wait for the spawned shell to return,
report its exit status or terminating signal visually.
Exit 0 if spawned.
.El
.Pp
In addition to the above, the flags
.Op Fl +abCefhkmnUuvXx
and
.Op Fl +o Ar option ,
respectively for single-letter and long options,
as described for the
.Ic set
built-in utility, can be used on the command line.
.Pp
If neither the
.Fl c
nor the
.Fl s
options are specified,
.Nm mksh
will read and execute commands as if the
.Fl s
flag was passed iff the
.Ar file
argument is absent or
.Dq Li \- ;
otherwise, it sets $0 to
.Ar file
and reads commands from it.
Further arguments
.Ar arg1 ...
are assigned to positional parameters.
.Pp
The exit status of the shell is 127 if the
.Ar file
specified on the command line could not be opened,
or non-zero if a fatal error occurred during execution of the script.
Otherwise, the errorlevel is that of the last command executed,
0 if no command was executed.
.Ss Startup files
For the actual location of these files, see
.Sx FILES .
A login shell processes the system profile first.
A privileged shell then processes the suid profile.
A non-privileged login shell processes the user profile next.
A non-privileged interactive shell checks the value of the
.Ev ENV
parameter after subjecting it to parameter, command, arithmetic and tilde
.Pq Ql \*(TI
substitution; if unset or empty, the user mkshrc profile is processed;
otherwise, if a file whose name is the substitution result exists,
it is processed; non-existence is silently ignored.
A privileged shell then drops privileges if neither was the
.Fl p
option given on the command line nor set during execution of the startup files.
.Ss Command syntax
The shell begins parsing its input by removing any backslash-newline
combinations, then breaking it into
.Em words .
Words (which are sequences of characters) are delimited by unquoted whitespace
characters (space, tab and newline) or meta-characters
.Po
.Ql \*(Lt ,
.Ql \*(Gt ,
.Ql \*(Ba ,
.Ql \&; ,
.Ql \&( ,
.Ql \&)
and
.Ql &
.Pc .
Aside from delimiting words, spaces and tabs are ignored, while newlines
usually delimit commands.
The meta-characters are used in building the following
.Em tokens :
.Dq Li \*(Lt ,
.Dq Li \*(Lt& ,
.Dq Li \*(Lt\*(Lt ,
.Dq Li \*(Lt\*(Lt\*(Lt ,
.Dq Li \*(Gt ,
.Dq Li \*(Gt& ,
.Dq Li \*(Gt\*(Gt ,
.Dq Li &\*(Gt ,
etc. are used to specify redirections (see
.Sx Input/output redirection
below);
.Dq Li \*(Ba
is used to create pipelines;
.Dq Li \*(Ba&
is used to create co-processes (see
.Sx Co-processes
below);
.Dq Li \&;
is used to separate commands;
.Dq Li &
is used to create asynchronous pipelines;
.Dq Li &&
and
.Dq Li \*(Ba\*(Ba
are used to specify conditional execution;
.Dq Li \&;; ,
.Dq Li \&;&
and
.Dq Li \&;\*(Ba
are used in
.Ic case
statements;
.Dq Li \&(( ... \&))
is used in arithmetic expressions;
and lastly,
.Dq Li \&( ... \&)
is used to create subshells.
.Pp
Whitespace and meta-characters can be quoted individually using a backslash
.Pq Ql \e ,
or in groups using double
.Pq Ql \&"
or single
.Pq Dq Li \*(aq
quotes.
Note that the following characters are also treated specially by the
shell and must be quoted if they are to represent themselves:
.Ql \e ,
.Ql \&" ,
.Dq Li \*(aq ,
.Ql # ,
.Ql $ ,
.Ql \` ,
.Ql \*(TI ,
.Ql { ,
.Ql } ,
.Ql * ,
.Ql \&?
and
.Ql \&[ .
The first three of these are the above mentioned quoting characters (see
.Sx Quoting
below);
.Ql # ,
if used at the beginning of a word,
introduces a comment\*(EMeverything after the
.Ql #
up to the nearest newline is ignored;
.Ql $
is used to introduce parameter, command and arithmetic substitutions (see
.Sx Substitution
below);
.Ql \`
introduces an old-style command substitution (see
.Sx Substitution
below);
.Ql \*(TI
begins a directory expansion (see
.Sx Tilde expansion
below);
.Ql {
and
.Ql }
delimit
.Xr csh 1 Ns -style
alternations (see
.Sx Brace expansion
below);
and finally,
.Ql * ,
.Ql \&?
and
.Ql \&[
are used in file name generation (see
.Sx File name patterns
below).
.Pp
As words and tokens are parsed, the shell builds commands, of which there
are two basic types:
.Em simple-commands ,
typically programmes that are executed, and
.Em compound-commands ,
such as
.Ic for
and
.Ic if
statements, grouping constructs and function definitions.
.Pp
A simple-command consists of some combination of parameter assignments
(see
.Sx Parameters
below),
input/output redirections (see
.Sx Input/output redirections
below)
and command words; the only restriction is that parameter assignments come
before any command words.
The command words, if any, define the command
that is to be executed and its arguments.
The command may be a shell built-in command, a function
or an external command
(i.e. a separate executable file that is located using the
.Ev PATH
parameter; see
.Sx Command execution
below).
Note that all command constructs have an exit status: for external commands,
this is related to the status returned by
.Xr wait 2
(if the command could not be found, the exit status is 127; if it could not
be executed, the exit status is 126); the exit status of other command
constructs (built-in commands, functions, compound-commands, pipelines, lists,
etc.) are all well-defined and are described where the construct is
described.
The exit status of a command consisting only of parameter
assignments is that of the last command substitution performed during the
parameter assignment or 0 if there were no command substitutions.
.Pp
Commands can be chained together using the
.Dq Li \*(Ba
token to form pipelines, in which the standard output of each command but the
last is piped (see
.Xr pipe 2 )
to the standard input of the following command.
The exit status of a pipeline is that of its last command, unless the
.Ic pipefail
option is set (see there).
All commands of a pipeline are executed in separate subshells;
this is allowed by POSIX but differs from both variants of
.At
.Nm ksh ,
where all but the last command were executed in subshells; see the
.Ic read
builtin's description for implications and workarounds.
A pipeline may be prefixed by the
.Dq Li \&!
reserved word which causes the exit status of the pipeline to be logically
complemented: if the original status was 0, the complemented status will be 1;
if the original status was not 0, the complemented status will be 0.
.Pp
.Em Lists
of commands can be created by separating pipelines by any of the following
tokens:
.Dq Li && ,
.Dq Li \*(Ba\*(Ba ,
.Dq Li & ,
.Dq Li \*(Ba&
and
.Dq Li \&; .
The first two are for conditional execution:
.Dq Ar cmd1 No && Ar cmd2
executes
.Ar cmd2
only if the exit status of
.Ar cmd1
is zero;
.Dq Li \*(Ba\*(Ba
.No is the opposite\*(EM Ns Ar cmd2
is executed only if the exit status of
.Ar cmd1
is non-zero.
.Dq Li &&
and
.Dq Li \*(Ba\*(Ba
have equal precedence which is higher than that of
.Dq Li & ,
.Dq Li \*(Ba&
and
.Dq Li \&; ,
which also have equal precedence.
Note that the
.Dq Li &&
and
.Dq Li \*(Ba\*(Ba
operators are
.Qq left-associative .
For example, both of these commands will print only
.Qq bar :
.Bd -literal -offset indent
$ false && echo foo \*(Ba\*(Ba echo bar
$ true \*(Ba\*(Ba echo foo && echo bar
.Ed
.Pp
The
.Dq Li &
token causes the preceding command to be executed asynchronously; that is,
the shell starts the command but does not wait for it to complete (the shell
does keep track of the status of asynchronous commands; see
.Sx Job control
below).
When an asynchronous command is started when job control is disabled
(i.e. in most scripts), the command is started with signals
.Dv SIGINT
and
.Dv SIGQUIT
ignored and with input redirected from
.Pa /dev/null
(however, redirections specified in the asynchronous command have precedence).
The
.Dq Li \*(Ba&
operator starts a co-process which is a special kind of asynchronous process
(see
.Sx Co-processes
below).
Note that a command must follow the
.Dq Li &&
and
.Dq Li \*(Ba\*(Ba
operators, while it need not follow
.Dq Li & ,
.Dq Li \*(Ba&
or
.Dq Li \&; .
The exit status of a list is that of the last command executed, with the
exception of asynchronous lists, for which the exit status is 0.
.Pp
Compound commands are created using the following reserved words.
These words
are only recognised if they are unquoted and if they are used as the first
word of a command (i.e. they can't be preceded by parameter assignments or
redirections):
.Bd -literal -offset indent
case else function then ! (
do esac if time [[ ((
done fi in until {
elif for select while }
.Ed
.Pp
In the following compound command descriptions, command lists (denoted as
.Em list )
that are followed by reserved words must end with a semicolon, a newline or
a (syntactically correct) reserved word.
For example, the following are all valid:
.Bd -literal -offset indent
$ { echo foo; echo bar; }
$ { echo foo; echo bar\*(Ltnewline\*(Gt}
$ { { echo foo; echo bar; } }
.Ed
.Pp
This is not valid:
.Pp
.Dl $ { echo foo; echo bar }
.Bl -tag -width 4n
.It Xo Ic case Ar word Ic in
.Oo Op \&(
.Ar pattern
.Op \*(Ba Ar pattern
.No ... Ns )
.Ar list
.Aq terminator
.Oc No ... Ic esac
.Xc
The
.Ic case
statement attempts to match
.Ar word
against a specified
.Ar pattern ;
the
.Ar list
associated with the first successfully matched pattern is executed.
Patterns used in
.Ic case
statements are the same as those used for file name patterns except that the
restrictions regarding
.Ql \&.
and
.Ql /
are dropped.
Note that any unquoted space before and after a pattern is
stripped; any space within a pattern must be quoted.
Both the word and the
patterns are subject to parameter, command and arithmetic substitution, as
well as tilde substitution.
.Pp
For historical reasons, open and close braces may be used instead of
.Ic in
and
.Ic esac ,
for example:
.Dq Li case $foo { (ba[rz]\*(Bablah) date ;; }
.Pp
The list
.Ao terminator Ac Ns s
are:
.Bl -tag -width 4n
.It Dq Li ;;
Terminate after the list.
.It Dq Li \&;&
Fall through into the next list.
.It Dq Li \&;\*(Ba
Evaluate the remaining pattern-list tuples.
.El
.Pp
The exit status of a
.Ic case
statement is that of the executed
.Ar list ;
if no
.Ar list
is executed, the exit status is zero.
.It Xo Ic for Ar name
.Oo Ic in Ar word ... Oc Ic ;
.Ic do Ar list ; Ic done
.Xc
For each
.Ar word
in the specified word list, the parameter
.Ar name
is set to the word and
.Ar list
is executed.
The exit status of a
.Ic for
statement is the last exit status of
.Ar list ;
if
.Ar list
is never executed, the exit status is zero.
If
.Ic in
is not used to specify a word list, the positional parameters ($1, $2,
etc.) are used instead; in this case, use a newline instead of the semicolon
.Pq Sq Ic ;\&
for portability.
For historical reasons, open and close braces may be used instead of
.Ic do
and
.Ic done ,
as in
.Dq Li for i; { echo $i; }
.Pq not portable .
.It Xo Ic function Ar name
.No { Ar list ; No }
.Xc
Defines the function
.Ar name
(see
.Sx Functions
below).
All redirections specified after a function definition are performed whenever
the function is executed, not when the function definition is executed.
.It Ar name Ns \&() Ar command
Mostly the same as
.Ic function
(see above and
.Sx Functions
below).
Most amounts of space and tab after
.Ar name
will be ignored.
.It Xo Ic function Ar name Ns \&()
.No { Ar list ; No }
.Xc
.Nm bash Ns ism for
.Ar name Ns Li ()\ {
.Ar list Ns Li ;\ }
.Pq the Ic function No keyword is ignored .
.It Xo Ic if Ar list ;
.Ic then Ar list ;
.Oo Ic elif Ar list ;
.Ic then Ar list ; Oc
.No ...
.Oo Ic else Ar list ; Oc
.Ic fi
.Xc
If the exit status of the first
.Ar list
is zero, the second
.Ar list
is executed; otherwise, the
.Ar list
following the
.Ic elif ,
if any, is executed with similar consequences.
If all the lists following the
.Ic if
and
.Ic elif Ns s
fail (i.e. exit with non-zero status), the
.Ar list
following the
.Ic else
is executed.
The exit status of an
.Ic if
statement is that of whatever non-conditional
.Pq not the first
.Ar list
that is executed; if no non-conditional
.Ar list
is executed, the exit status is zero.
.It Xo Ic select Ar name
.Oo Ic in Ar word No ... Oc ;
.Ic do Ar list ; Ic done
.Xc
The
.Ic select
statement provides an automatic method of presenting the user with a menu and
selecting from it.
An enumerated list of the specified
.Ar word Ns s
is printed on standard error, followed by a prompt
.Po
.Ev PS3 :
normally
.Dq Li #?\ \&
.Pc .
A number corresponding to one of the enumerated words is then read from
standard input,
.Ar name
is set to the selected word (or unset if the selection is not valid),
.Ev REPLY
is set to what was read (leading and trailing space is stripped), and
.Ar list
is executed.
If a blank line (i.e. zero or more
.Ev IFS
octets) is entered, the menu is reprinted without executing
.Ar list .
.Pp
When
.Ar list
completes, the enumerated list is printed if
.Ev REPLY
is empty, the prompt is printed, and so on.
This process continues until an end-of-file
is read, an interrupt is received, or a
.Ic break
statement is executed inside the loop.
The exit status of a
.Ic select
statement is zero if a
.Ic break
statement is used to exit the loop, non-zero otherwise.
If
.Dq Ic in Ar word ...
is omitted, the positional parameters are used.
For historical reasons, open and close braces may be used instead of
.Ic do
and
.Ic done ,
as in:
.Dq Li select i; { echo $i; }
.It Xo Ic time Op Fl p
.Op Ar pipeline
.Xc
The
.Sx Command execution
section describes the
.Ic time
reserved word.
When not a reserved word, a builtin runs the passed command.
.It Xo Ic until Ar list ;
.Ic do Ar list ; Ic done
.Xc
This works like
.Ic while Pq see below ,
except that the body
.Ar list
is executed only while the exit status of the first
.Ar list
is non-zero.
.It Xo Ic while Ar list ;
.Ic do Ar list ; Ic done
.Xc
A
.Ic while
is a pre-checked loop.
Its body
.Ar list
is executed as often as the exit status of the first
.Ar list
is zero.
The exit status of a
.Ic while
statement is the last exit status of the
.Ar list
in the body of the loop; if the body is not executed, the exit status is zero.
.It Bq Bq Ar \ \&expression\ \&