forked from Mujffar/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh.1
2896 lines (2896 loc) · 72.7 KB
/
sh.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
.\"-
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" Kenneth Almquist.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\" may be used to endorse or promote products derived from this software
.\" without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$
.\"
.Dd July 6, 2020
.Dt SH 1
.Os
.Sh NAME
.Nm sh
.Nd command interpreter (shell)
.Sh SYNOPSIS
.Nm
.Op Fl /+abCEefhIimnPpTuVvx
.Op Fl /+o Ar longname
.Oo
.Ar script
.Op Ar arg ...
.Oc
.Nm
.Op Fl /+abCEefhIimnPpTuVvx
.Op Fl /+o Ar longname
.Fl c Ar string
.Oo
.Ar name
.Op Ar arg ...
.Oc
.Nm
.Op Fl /+abCEefhIimnPpTuVvx
.Op Fl /+o Ar longname
.Fl s
.Op Ar arg ...
.Sh DESCRIPTION
The
.Nm
utility is the standard command interpreter for the system.
The current version of
.Nm
is close to the
.St -p1003.1
specification for the shell.
It only supports features
designated by POSIX,
plus a few Berkeley extensions.
This man page is not intended to be a tutorial nor a complete
specification of the shell.
.Ss Overview
The shell is a command that reads lines from
either a file or the terminal, interprets them, and
generally executes other commands.
It is the program that is started when a user logs into the system,
although a user can select a different shell with the
.Xr chsh 1
command.
The shell
implements a language that has flow control constructs,
a macro facility that provides a variety of features in
addition to data storage, along with built-in history and line
editing capabilities.
It incorporates many features to
aid interactive use and has the advantage that the interpretative
language is common to both interactive and non-interactive
use (shell scripts).
That is, commands can be typed directly
to the running shell or can be put into a file,
which can be executed directly by the shell.
.Ss Invocation
.\"
.\" XXX This next sentence is incredibly confusing.
.\"
If no arguments are present and if the standard input of the shell
is connected to a terminal
(or if the
.Fl i
option is set),
the shell is considered an interactive shell.
An interactive shell
generally prompts before each command and handles programming
and command errors differently (as described below).
When first starting, the shell inspects argument 0, and
if it begins with a dash
.Pq Ql - ,
the shell is also considered a login shell.
This is normally done automatically by the system
when the user first logs in.
A login shell first reads commands
from the files
.Pa /etc/profile
and then
.Pa .profile
in a user's home directory,
if they exist.
If the environment variable
.Ev ENV
is set on entry to a shell, or is set in the
.Pa .profile
of a login shell, the shell then subjects its value to parameter expansion
and arithmetic expansion and reads commands from the named file.
Therefore, a user should place commands that are to be executed only
at login time in the
.Pa .profile
file, and commands that are executed for every shell inside the
.Ev ENV
file.
The user can set the
.Ev ENV
variable to some file by placing the following line in the file
.Pa .profile
in the home directory,
substituting for
.Pa .shrc
the filename desired:
.Pp
.Dl "ENV=$HOME/.shrc; export ENV"
.Pp
The first non-option argument specified on the command line
will be treated as the
name of a file from which to read commands (a shell script), and
the remaining arguments are set as the positional parameters
of the shell
.Li ( $1 , $2 ,
etc.).
Otherwise, the shell reads commands
from its standard input.
.Pp
Unlike older versions of
.Nm
the
.Ev ENV
script is only sourced on invocation of interactive shells.
This
closes a well-known, and sometimes easily exploitable security
hole related to poorly thought out
.Ev ENV
scripts.
.Ss Argument List Processing
All of the single letter options to
.Nm
have a corresponding long name,
with the exception of
.Fl c
and
.Fl /+o .
These long names are provided next to the single letter options
in the descriptions below.
The long name for an option may be specified as an argument to the
.Fl /+o
option of
.Nm .
Once the shell is running,
the long name for an option may be specified as an argument to the
.Fl /+o
option of the
.Ic set
built-in command
(described later in the section called
.Sx Built-in Commands ) .
Introducing an option with a dash
.Pq Ql -
enables the option,
while using a plus
.Pq Ql +
disables the option.
A
.Dq Li --
or plain
.Ql -
will stop option processing and will force the remaining
words on the command line to be treated as arguments.
The
.Fl /+o
and
.Fl c
options do not have long names.
They take arguments and are described after the single letter options.
.Bl -tag -width indent
.It Fl a Li allexport
Flag variables for export when assignments are made to them.
.It Fl b Li notify
Enable asynchronous notification of background job
completion.
(UNIMPLEMENTED)
.It Fl C Li noclobber
Do not overwrite existing files with
.Ql > .
.It Fl E Li emacs
Enable the built-in
.Xr emacs 1
command line editor (disables the
.Fl V
option if it has been set;
set automatically when interactive on terminals).
.It Fl e Li errexit
Exit immediately if any untested command fails in non-interactive mode.
The exit status of a command is considered to be
explicitly tested if the command is part of the list used to control
an
.Ic if , elif , while ,
or
.Ic until ;
if the command is the left
hand operand of an
.Dq Li &&
or
.Dq Li ||
operator; or if the command is a pipeline preceded by the
.Ic !\&
keyword.
If a shell function is executed and its exit status is explicitly
tested, all commands of the function are considered to be tested as
well.
.Pp
It is recommended to check for failures explicitly
instead of relying on
.Fl e
because it tends to behave in unexpected ways,
particularly in larger scripts.
.It Fl f Li noglob
Disable pathname expansion.
.It Fl h Li trackall
A do-nothing option for POSIX compliance.
.It Fl I Li ignoreeof
Ignore
.Dv EOF Ap s
from input when in interactive mode.
.It Fl i Li interactive
Force the shell to behave interactively.
.It Fl m Li monitor
Turn on job control (set automatically when interactive).
A new process group is created for each pipeline (called a job).
It is possible to suspend jobs or to have them run in the foreground or
in the background.
In a non-interactive shell,
this option can be set even if no terminal is available
and is useful to place processes in separate process groups.
.It Fl n Li noexec
If not interactive, read commands but do not
execute them.
This is useful for checking the
syntax of shell scripts.
.It Fl P Li physical
Change the default for the
.Ic cd
and
.Ic pwd
commands from
.Fl L
(logical directory layout)
to
.Fl P
(physical directory layout).
.It Fl p Li privileged
Turn on privileged mode.
This mode is enabled on startup
if either the effective user or group ID is not equal to the
real user or group ID.
Turning this mode off sets the
effective user and group IDs to the real user and group IDs.
When this mode is enabled for interactive shells, the file
.Pa /etc/suid_profile
is sourced instead of
.Pa ~/.profile
after
.Pa /etc/profile
is sourced, and the contents of the
.Ev ENV
variable are ignored.
.It Fl s Li stdin
Read commands from standard input (set automatically
if no file arguments are present).
This option has
no effect when set after the shell has already started
running (i.e., when set with the
.Ic set
command).
.It Fl T Li trapsasync
When waiting for a child, execute traps immediately.
If this option is not set,
traps are executed after the child exits,
as specified in
.St -p1003.2 .
This nonstandard option is useful for putting guarding shells around
children that block signals.
The surrounding shell may kill the child
or it may just return control to the tty and leave the child alone,
like this:
.Bd -literal -offset indent
sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
.Ed
.It Fl u Li nounset
Write a message to standard error when attempting
to expand a variable, a positional parameter or
the special parameter
.Va \&!
that is not set, and if the
shell is not interactive, exit immediately.
.It Fl V Li vi
Enable the built-in
.Xr vi 1
command line editor (disables
.Fl E
if it has been set).
.It Fl v Li verbose
The shell writes its input to standard error
as it is read.
Useful for debugging.
.It Fl x Li xtrace
Write each command
(preceded by the value of the
.Va PS4
variable subjected to parameter expansion and arithmetic expansion)
to standard error before it is executed.
Useful for debugging.
.It Li nolog
Another do-nothing option for POSIX compliance.
It only has a long name.
.It Li pipefail
Change the exit status of a pipeline to the last non-zero exit status of
any command in the pipeline, if any.
Since an exit due to
.Dv SIGPIPE
counts as a non-zero exit status,
this option may cause non-zero exit status for successful pipelines
if a command such as
.Xr head 1
in the pipeline terminates with status 0 without reading its
input completely.
This option only has a long name.
.El
.Pp
The
.Fl c
option causes the commands to be read from the
.Ar string
operand instead of from the standard input.
Keep in mind that this option only accepts a single string as its
argument, hence multi-word strings must be quoted.
.Pp
The
.Fl /+o
option takes as its only argument the long name of an option
to be enabled or disabled.
For example, the following two invocations of
.Nm
both enable the built-in
.Xr emacs 1
command line editor:
.Bd -literal -offset indent
set -E
set -o emacs
.Ed
.Pp
If used without an argument, the
.Fl o
option displays the current option settings in a human-readable format.
If
.Cm +o
is used without an argument, the current option settings are output
in a format suitable for re-input into the shell.
.Ss Lexical Structure
The shell reads input in terms of lines from a file and breaks
it up into words at whitespace (blanks and tabs), and at
certain sequences of
characters called
.Dq operators ,
which are special to the shell.
There are two types of operators: control operators and
redirection operators (their meaning is discussed later).
The following is a list of valid operators:
.Bl -tag -width indent
.It Control operators:
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
.It Li & Ta Li && Ta Li \&( Ta Li \&) Ta Li \en
.It Li ;; Ta Li ;& Ta Li \&; Ta Li \&| Ta Li ||
.El
.It Redirection operators:
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
.It Li <& Ta Li >& Ta Li <<- Ta Li >| Ta \&
.El
.El
.Pp
The character
.Ql #
introduces a comment if used at the beginning of a word.
The word starting with
.Ql #
and the rest of the line are ignored.
.Pp
ASCII
.Dv NUL
characters (character code 0) are not allowed in shell input.
.Ss Quoting
Quoting is used to remove the special meaning of certain characters
or words to the shell, such as operators, whitespace, keywords,
or alias names.
.Pp
There are four types of quoting: matched single quotes,
dollar-single quotes,
matched double quotes, and backslash.
.Bl -tag -width indent
.It Single Quotes
Enclosing characters in single quotes preserves the literal
meaning of all the characters (except single quotes, making
it impossible to put single-quotes in a single-quoted string).
.It Dollar-Single Quotes
Enclosing characters between
.Li $'
and
.Li '
preserves the literal meaning of all characters
except backslashes and single quotes.
A backslash introduces a C-style escape sequence:
.Bl -tag -width xUnnnnnnnn
.It \ea
Alert (ring the terminal bell)
.It \eb
Backspace
.It \ec Ns Ar c
The control character denoted by
.Li ^ Ns Ar c
in
.Xr stty 1 .
If
.Ar c
is a backslash, it must be doubled.
.It \ee
The ESC character (ASCII 0x1b)
.It \ef
Formfeed
.It \en
Newline
.It \er
Carriage return
.It \et
Horizontal tab
.It \ev
Vertical tab
.It \e\e
Literal backslash
.It \e\&'
Literal single-quote
.It \e\&"
Literal double-quote
.It \e Ns Ar nnn
The byte whose octal value is
.Ar nnn
(one to three digits)
.It \ex Ns Ar nn
The byte whose hexadecimal value is
.Ar nn
(one or more digits only the last two of which are used)
.It \eu Ns Ar nnnn
The Unicode code point
.Ar nnnn
(four hexadecimal digits)
.It \eU Ns Ar nnnnnnnn
The Unicode code point
.Ar nnnnnnnn
(eight hexadecimal digits)
.El
.Pp
The sequences for Unicode code points are currently only useful with
UTF-8 locales.
They reject code point 0 and UTF-16 surrogates.
.Pp
If an escape sequence would produce a byte with value 0,
that byte and the rest of the string until the matching single-quote
are ignored.
.Pp
Any other string starting with a backslash is an error.
.It Double Quotes
Enclosing characters within double quotes preserves the literal
meaning of all characters except dollar sign
.Pq Ql $ ,
backquote
.Pq Ql ` ,
and backslash
.Pq Ql \e .
The backslash inside double quotes is historically weird.
It remains literal unless it precedes the following characters,
which it serves to quote:
.Pp
.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
.It Li $ Ta Li ` Ta Li \&" Ta Li \e Ta Li \en
.El
.It Backslash
A backslash preserves the literal meaning of the following
character, with the exception of the newline character
.Pq Ql \en .
A backslash preceding a newline is treated as a line continuation.
.El
.Ss Keywords
Keywords or reserved words are words that have special meaning to the
shell and are recognized at the beginning of a line and
after a control operator.
The following are keywords:
.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
.El
.Ss Aliases
An alias is a name and corresponding value set using the
.Ic alias
built-in command.
Wherever the command word of a simple command may occur,
and after checking for keywords if a keyword may occur, the shell
checks the word to see if it matches an alias.
If it does, it replaces it in the input stream with its value.
For example, if there is an alias called
.Dq Li lf
with the value
.Dq Li "ls -F" ,
then the input
.Pp
.Dl "lf foobar"
.Pp
would become
.Pp
.Dl "ls -F foobar"
.Pp
Aliases are also recognized after an alias
whose value ends with a space or tab.
For example, if there is also an alias called
.Dq Li nohup
with the value
.Dq Li "nohup " ,
then the input
.Pp
.Dl "nohup lf foobar"
.Pp
would become
.Pp
.Dl "nohup ls -F foobar"
.Pp
Aliases provide a convenient way for naive users to
create shorthands for commands without having to learn how
to create functions with arguments.
Using aliases in scripts is discouraged
because the command that defines them must be executed
before the code that uses them is parsed.
This is fragile and not portable.
.Pp
An alias name may be escaped in a command line, so that it is not
replaced by its alias value, by using quoting characters within or
adjacent to the alias name.
This is most often done by prefixing
an alias name with a backslash to execute a function, built-in, or
normal program with the same name.
See the
.Sx Quoting
subsection.
.Ss Commands
The shell interprets the words it reads according to a
language, the specification of which is outside the scope
of this man page (refer to the BNF in the
.St -p1003.2
document).
Essentially though, a line is read and if
the first word of the line (or after a control operator)
is not a keyword, then the shell has recognized a
simple command.
Otherwise, a complex command or some
other special construct may have been recognized.
.Ss Simple Commands
If a simple command has been recognized, the shell performs
the following actions:
.Bl -enum
.It
Leading words of the form
.Dq Li name=value
are stripped off and assigned to the environment of
the simple command
(they do not affect expansions).
Redirection operators and
their arguments (as described below) are stripped
off and saved for processing.
.It
The remaining words are expanded as described in
the section called
.Sx Word Expansions ,
and the first remaining word is considered the command
name and the command is located.
The remaining
words are considered the arguments of the command.
If no command name resulted, then the
.Dq Li name=value
variable assignments recognized in 1) affect the
current shell.
.It
Redirections are performed as described in
the next section.
.El
.Ss Redirections
Redirections are used to change where a command reads its input
or sends its output.
In general, redirections open, close, or
duplicate an existing reference to a file.
The overall format
used for redirection is:
.Pp
.D1 Oo Ar n Oc Ar redir-op file
.Pp
The
.Ar redir-op
is one of the redirection operators mentioned
previously.
The following gives some examples of how these
operators can be used.
Note that stdin and stdout are commonly used abbreviations
for standard input and standard output respectively.
.Bl -tag -width "1234567890XX" -offset indent
.It Oo Ar n Oc Ns Li > Ar file
redirect stdout (or file descriptor
.Ar n )
to
.Ar file
.It Oo Ar n Oc Ns Li >| Ar file
same as above, but override the
.Fl C
option
.It Oo Ar n Oc Ns Li >> Ar file
append stdout (or file descriptor
.Ar n )
to
.Ar file
.It Oo Ar n Oc Ns Li < Ar file
redirect stdin (or file descriptor
.Ar n )
from
.Ar file
.It Oo Ar n Oc Ns Li <> Ar file
redirect stdin (or file descriptor
.Ar n )
to and from
.Ar file
.It Oo Ar n1 Oc Ns Li <& Ns Ar n2
duplicate stdin (or file descriptor
.Ar n1 )
from file descriptor
.Ar n2
.It Oo Ar n Oc Ns Li <&-
close stdin (or file descriptor
.Ar n )
.It Oo Ar n1 Oc Ns Li >& Ns Ar n2
duplicate stdout (or file descriptor
.Ar n1 )
to file descriptor
.Ar n2
.It Oo Ar n Oc Ns Li >&-
close stdout (or file descriptor
.Ar n )
.El
.Pp
The following redirection is often called a
.Dq here-document .
.Bd -unfilled -offset indent
.Oo Ar n Oc Ns Li << Ar delimiter
.Ar here-doc-text
.Ar ...
.Ar delimiter
.Ed
.Pp
All the text on successive lines up to the delimiter is
saved away and made available to the command on standard
input, or file descriptor
.Ar n
if it is specified.
If the
.Ar delimiter
as specified on the initial line is quoted, then the
.Ar here-doc-text
is treated literally, otherwise the text is subjected to
parameter expansion, command substitution, and arithmetic
expansion (as described in the section on
.Sx Word Expansions ) .
If the operator is
.Dq Li <<-
instead of
.Dq Li << ,
then leading tabs
in the
.Ar here-doc-text
are stripped.
.Ss Search and Execution
There are three types of commands: shell functions,
built-in commands, and normal programs.
The command is searched for (by name) in that order.
The three types of commands are all executed in a different way.
.Pp
When a shell function is executed, all of the shell positional
parameters (except
.Li $0 ,
which remains unchanged) are
set to the arguments of the shell function.
The variables which are explicitly placed in the environment of
the command (by placing assignments to them before the
function name) are made local to the function and are set
to the values given.
Then the command given in the function definition is executed.
The positional parameters are restored to their original values
when the command completes.
This all occurs within the current shell.
.Pp
Shell built-in commands are executed internally to the shell, without
spawning a new process.
There are two kinds of built-in commands: regular and special.
Assignments before special builtins persist after they finish
executing and assignment errors, redirection errors and certain
operand errors cause a script to be aborted.
Special builtins cannot be overridden with a function.
Both regular and special builtins can affect the shell in ways
normal programs cannot.
.Pp
Otherwise, if the command name does not match a function
or built-in command, the command is searched for as a normal
program in the file system (as described in the next section).
When a normal program is executed, the shell runs the program,
passing the arguments and the environment to the program.
If the program is not a normal executable file
(i.e., if it does not begin with the
.Dq "magic number"
whose ASCII representation is
.Dq Li #! ,
resulting in an
.Er ENOEXEC
return value from
.Xr execve 2 )
but appears to be a text file,
the shell will run a new instance of
.Nm
to interpret it.
.Pp
Note that previous versions of this document
and the source code itself misleadingly and sporadically
refer to a shell script without a magic number
as a
.Dq "shell procedure" .
.Ss Path Search
When locating a command, the shell first looks to see if
it has a shell function by that name.
Then it looks for a
built-in command by that name.
If a built-in command is not found,
one of two things happen:
.Bl -enum
.It
Command names containing a slash are simply executed without
performing any searches.
.It
The shell searches each entry in the
.Va PATH
variable
in turn for the command.
The value of the
.Va PATH
variable should be a series of
entries separated by colons.
Each entry consists of a
directory name.
The current directory
may be indicated implicitly by an empty directory name,
or explicitly by a single period.
.El
.Ss Command Exit Status
Each command has an exit status that can influence the behavior
of other shell commands.
The paradigm is that a command exits
with zero for normal or success, and non-zero for failure,
error, or a false indication.
The man page for each command
should indicate the various exit codes and what they mean.
Additionally, the built-in commands return exit codes, as does
an executed shell function.
.Pp
If a command is terminated by a signal, its exit status is greater than 128.
The signal name can be found by passing the exit status to
.Li kill -l .
.Pp
If there is no command word,
the exit status is the exit status of the last command substitution executed,
or zero if the command does not contain any command substitutions.
.Ss Complex Commands
Complex commands are combinations of simple commands
with control operators or keywords, together creating a larger complex
command.
More generally, a command is one of the following:
.Bl -item -offset indent
.It
simple command
.It
pipeline
.It
list or compound-list
.It
compound command
.It
function definition
.El
.Pp
Unless otherwise stated, the exit status of a command is
that of the last simple command executed by the command,
or zero if no simple command was executed.
.Ss Pipelines
A pipeline is a sequence of one or more commands separated
by the control operator
.Ql \&| .
The standard output of all but
the last command is connected to the standard input
of the next command.
The standard output of the last
command is inherited from the shell, as usual.
.Pp
The format for a pipeline is:
.Pp
.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
.Pp
The standard output of
.Ar command1
is connected to the standard input of
.Ar command2 .
The standard input, standard output, or
both of a command is considered to be assigned by the
pipeline before any redirection specified by redirection
operators that are part of the command.
.Pp
Note that unlike some other shells,
.Nm
executes each process in a pipeline with more than one command
in a subshell environment and as a child of the
.Nm
process.
.Pp
If the pipeline is not in the background (discussed later),
the shell waits for all commands to complete.
.Pp
If the keyword
.Ic !\&
does not precede the pipeline, the
exit status is the exit status of the last command specified
in the pipeline if the
.Cm pipefail
option is not set or all commands returned zero,
or the last non-zero exit status of any command in the pipeline otherwise.
Otherwise, the exit status is the logical
NOT of that exit status.
That is, if
that status is zero, the exit status is 1; if
that status is greater than zero, the exit status
is zero.
.Pp
Because pipeline assignment of standard input or standard
output or both takes place before redirection, it can be
modified by redirection.
For example:
.Pp
.Dl "command1 2>&1 | command2"
.Pp
sends both the standard output and standard error of
.Ar command1
to the standard input of
.Ar command2 .
.Pp
A
.Ql \&;
or newline terminator causes the preceding
AND-OR-list
(described below in the section called
.Sx Short-Circuit List Operators )
to be executed sequentially;
an
.Ql &
causes asynchronous execution of the preceding AND-OR-list.
.Ss Background Commands (&)
If a command is terminated by the control operator ampersand
.Pq Ql & ,
the shell executes the command in a subshell environment (see
.Sx Grouping Commands Together
below) and asynchronously;
the shell does not wait for the command to finish
before executing the next command.
.Pp
The format for running a command in background is:
.Pp
.D1 Ar command1 Li & Op Ar command2 Li & Ar ...
.Pp
If the shell is not interactive, the standard input of an
asynchronous command is set to
.Pa /dev/null .
.Pp
The exit status is zero.
.Ss Lists (Generally Speaking)
A list is a sequence of zero or more commands separated by
newlines, semicolons, or ampersands,
and optionally terminated by one of these three characters.
The commands in a
list are executed in the order they are written.
If command is followed by an ampersand, the shell starts the
command and immediately proceeds onto the next command;
otherwise it waits for the command to terminate before
proceeding to the next one.
.Ss Short-Circuit List Operators
.Dq Li &&
and
.Dq Li ||
are AND-OR list operators.
.Dq Li &&
executes the first command, and then executes the second command
if the exit status of the first command is zero.
.Dq Li ||
is similar, but executes the second command if the exit
status of the first command is nonzero.
.Dq Li &&
and
.Dq Li ||
both have the same priority.
.Ss Flow-Control Constructs (if, while, for, case)
The syntax of the
.Ic if
command is:
.Bd -unfilled -offset indent -compact
.Ic if Ar list
.Ic then Ar list
.Oo Ic elif Ar list
.Ic then Ar list Oc Ar ...
.Op Ic else Ar list
.Ic fi
.Ed
.Pp
The exit status is that of selected
.Ic then
or
.Ic else
list,
or zero if no list was selected.
.Pp
The syntax of the
.Ic while
command is:
.Bd -unfilled -offset indent -compact
.Ic while Ar list
.Ic do Ar list
.Ic done
.Ed
.Pp
The two lists are executed repeatedly while the exit status of the
first list is zero.
The
.Ic until
command is similar, but has the word
.Ic until
in place of
.Ic while ,
which causes it to
repeat until the exit status of the first list is zero.
.Pp
The exit status is that of the last execution of the second list,
or zero if it was never executed.
.Pp
The syntax of the
.Ic for
command is:
.Bd -unfilled -offset indent -compact
.Ic for Ar variable Op Ic in Ar word ...
.Ic do Ar list
.Ic done
.Ed
.Pp
If
.Ic in