-
Notifications
You must be signed in to change notification settings - Fork 75
/
bashref.texi
8743 lines (7268 loc) · 311 KB
/
bashref.texi
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
\input texinfo.tex @c -*- texinfo -*-
@c %**start of header
@setfilename bashref.info
@settitle Bash Reference Manual
@include version.texi
@c %**end of header
@copying
This text is a brief description of the features that are present in
the Bash shell (version @value{VERSION}, @value{UPDATED}).
This is Edition @value{EDITION}, last updated @value{UPDATED},
of @cite{The GNU Bash Reference Manual},
for @code{Bash}, Version @value{VERSION}.
Copyright @copyright{} 1988--2014 Free Software Foundation, Inc.
@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled
``GNU Free Documentation License''.
@end quotation
@end copying
@defcodeindex bt
@defcodeindex rw
@set BashFeatures
@dircategory Basics
@direntry
* Bash: (bash). The GNU Bourne-Again SHell.
@end direntry
@finalout
@titlepage
@title Bash Reference Manual
@subtitle Reference Documentation for Bash
@subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}.
@subtitle @value{UPDATED-MONTH}
@author Chet Ramey, Case Western Reserve University
@author Brian Fox, Free Software Foundation
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@contents
@ifnottex
@node Top, Introduction, (dir), (dir)
@top Bash Features
This text is a brief description of the features that are present in
the Bash shell (version @value{VERSION}, @value{UPDATED}).
The Bash home page is @url{http://www.gnu.org/software/bash/}.
This is Edition @value{EDITION}, last updated @value{UPDATED},
of @cite{The GNU Bash Reference Manual},
for @code{Bash}, Version @value{VERSION}.
Bash contains features that appear in other popular shells, and some
features that only appear in Bash. Some of the shells that Bash has
borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell
(@file{ksh}), and the C-shell (@file{csh} and its successor,
@file{tcsh}). The following menu breaks the features up into
categories, noting which features were inspired by other shells and
which are specific to Bash.
This manual is meant as a brief introduction to features found in
Bash. The Bash manual page should be used as the definitive
reference on shell behavior.
@menu
* Introduction:: An introduction to the shell.
* Definitions:: Some definitions used in the rest of this
manual.
* Basic Shell Features:: The shell "building blocks".
* Shell Builtin Commands:: Commands that are a part of the shell.
* Shell Variables:: Variables used or set by Bash.
* Bash Features:: Features found only in Bash.
* Job Control:: What job control is and how Bash allows you
to use it.
* Command Line Editing:: Chapter describing the command line
editing features.
* Using History Interactively:: Command History Expansion
* Installing Bash:: How to build and install Bash on your system.
* Reporting Bugs:: How to report bugs in Bash.
* Major Differences From The Bourne Shell:: A terse list of the differences
between Bash and historical
versions of /bin/sh.
* GNU Free Documentation License:: Copying and sharing this documentation.
* Indexes:: Various indexes for this manual.
@end menu
@end ifnottex
@node Introduction
@chapter Introduction
@menu
* What is Bash?:: A short description of Bash.
* What is a shell?:: A brief introduction to shells.
@end menu
@node What is Bash?
@section What is Bash?
Bash is the shell, or command language interpreter,
for the @sc{gnu} operating system.
The name is an acronym for the @samp{Bourne-Again SHell},
a pun on Stephen Bourne, the author of the direct ancestor of
the current Unix shell @code{sh},
which appeared in the Seventh Edition Bell Labs Research version
of Unix.
Bash is largely compatible with @code{sh} and incorporates useful
features from the Korn shell @code{ksh} and the C shell @code{csh}.
It is intended to be a conformant implementation of the @sc{ieee}
@sc{posix} Shell and Tools portion of the @sc{ieee} @sc{posix}
specification (@sc{ieee} Standard 1003.1).
It offers functional improvements over @code{sh} for both interactive and
programming use.
While the @sc{gnu} operating system provides other shells, including
a version of @code{csh}, Bash is the default shell.
Like other @sc{gnu} software, Bash is quite portable. It currently runs
on nearly every version of Unix and a few other operating systems @minus{}
independently-supported ports exist for @sc{ms-dos}, @sc{os/2},
and Windows platforms.
@node What is a shell?
@section What is a shell?
At its base, a shell is simply a macro processor that executes
commands. The term macro processor means functionality where text
and symbols are expanded to create larger expressions.
A Unix shell is both a command interpreter and a programming
language. As a command interpreter, the shell provides the user
interface to the rich set of @sc{gnu} utilities. The programming
language features allow these utilities to be combined.
Files containing commands can be created, and become
commands themselves. These new commands have the same status as
system commands in directories such as @file{/bin}, allowing users
or groups to establish custom environments to automate their common
tasks.
Shells may be used interactively or non-interactively. In
interactive mode, they accept input typed from the keyboard.
When executing non-interactively, shells execute commands read
from a file.
A shell allows execution of @sc{gnu} commands, both synchronously and
asynchronously.
The shell waits for synchronous commands to complete before accepting
more input; asynchronous commands continue to execute in parallel
with the shell while it reads and executes additional commands.
The @dfn{redirection} constructs permit
fine-grained control of the input and output of those commands.
Moreover, the shell allows control over the contents of commands'
environments.
Shells also provide a small set of built-in
commands (@dfn{builtins}) implementing functionality impossible
or inconvenient to obtain via separate utilities.
For example, @code{cd}, @code{break}, @code{continue}, and
@code{exec} cannot be implemented outside of the shell because
they directly manipulate the shell itself.
The @code{history}, @code{getopts}, @code{kill}, or @code{pwd}
builtins, among others, could be implemented in separate utilities,
but they are more convenient to use as builtin commands.
All of the shell builtins are described in
subsequent sections.
While executing commands is essential, most of the power (and
complexity) of shells is due to their embedded programming
languages. Like any high-level language, the shell provides
variables, flow control constructs, quoting, and functions.
Shells offer features geared specifically for
interactive use rather than to augment the programming language.
These interactive features include job control, command line
editing, command history and aliases. Each of these features is
described in this manual.
@node Definitions
@chapter Definitions
These definitions are used throughout the remainder of this manual.
@table @code
@item POSIX
@cindex POSIX
A family of open system standards based on Unix. Bash
is primarily concerned with the Shell and Utilities portion of the
@sc{posix} 1003.1 standard.
@item blank
A space or tab character.
@item builtin
@cindex builtin
A command that is implemented internally by the shell itself, rather
than by an executable program somewhere in the file system.
@item control operator
@cindex control operator
A @code{token} that performs a control function. It is a @code{newline}
or one of the following:
@samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;},
@samp{|}, @samp{|&}, @samp{(}, or @samp{)}.
@item exit status
@cindex exit status
The value returned by a command to its caller. The value is restricted
to eight bits, so the maximum value is 255.
@item field
@cindex field
A unit of text that is the result of one of the shell expansions. After
expansion, when executing a command, the resulting fields are used as
the command name and arguments.
@item filename
@cindex filename
A string of characters used to identify a file.
@item job
@cindex job
A set of processes comprising a pipeline, and any processes descended
from it, that are all in the same process group.
@item job control
@cindex job control
A mechanism by which users can selectively stop (suspend) and restart
(resume) execution of processes.
@item metacharacter
@cindex metacharacter
A character that, when unquoted, separates words. A metacharacter is
a @code{blank} or one of the following characters:
@samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or
@samp{>}.
@item name
@cindex name
@cindex identifier
A @code{word} consisting solely of letters, numbers, and underscores,
and beginning with a letter or underscore. @code{Name}s are used as
shell variable and function names.
Also referred to as an @code{identifier}.
@item operator
@cindex operator, shell
A @code{control operator} or a @code{redirection operator}.
@xref{Redirections}, for a list of redirection operators.
Operators contain at least one unquoted @code{metacharacter}.
@item process group
@cindex process group
A collection of related processes each having the same process
group @sc{id}.
@item process group ID
@cindex process group ID
A unique identifier that represents a @code{process group}
during its lifetime.
@item reserved word
@cindex reserved word
A @code{word} that has a special meaning to the shell. Most reserved
words introduce shell flow control constructs, such as @code{for} and
@code{while}.
@item return status
@cindex return status
A synonym for @code{exit status}.
@item signal
@cindex signal
A mechanism by which a process may be notified by the kernel
of an event occurring in the system.
@item special builtin
@cindex special builtin
A shell builtin command that has been classified as special by the
@sc{posix} standard.
@item token
@cindex token
A sequence of characters considered a single unit by the shell.
It is either a @code{word} or an @code{operator}.
@item word
@cindex word
A sequence of characters treated as a unit by the shell.
Words may not include unquoted @code{metacharacters}.
@end table
@node Basic Shell Features
@chapter Basic Shell Features
@cindex Bourne shell
Bash is an acronym for @samp{Bourne-Again SHell}.
The Bourne shell is
the traditional Unix shell originally written by Stephen Bourne.
All of the Bourne shell builtin commands are available in Bash,
The rules for evaluation and quoting are taken from the @sc{posix}
specification for the `standard' Unix shell.
This chapter briefly summarizes the shell's `building blocks':
commands, control structures, shell functions, shell @i{parameters},
shell expansions,
@i{redirections}, which are a way to direct input and output from
and to named files, and how the shell executes commands.
@menu
* Shell Syntax:: What your input means to the shell.
* Shell Commands:: The types of commands you can use.
* Shell Functions:: Grouping commands by name.
* Shell Parameters:: How the shell stores values.
* Shell Expansions:: How Bash expands parameters and the various
expansions available.
* Redirections:: A way to control where input and output go.
* Executing Commands:: What happens when you run a command.
* Shell Scripts:: Executing files of shell commands.
@end menu
@node Shell Syntax
@section Shell Syntax
@menu
* Shell Operation:: The basic operation of the shell.
* Quoting:: How to remove the special meaning from characters.
* Comments:: How to specify comments.
@end menu
When the shell reads input, it proceeds through a
sequence of operations. If the input indicates the beginning of a
comment, the shell ignores the comment symbol (@samp{#}), and the rest
of that line.
Otherwise, roughly speaking, the shell reads its input and
divides the input into words and operators, employing the quoting rules
to select which meanings to assign various words and characters.
The shell then parses these tokens into commands and other constructs,
removes the special meaning of certain words or characters, expands
others, redirects input and output as needed, executes the specified
command, waits for the command's exit status, and makes that exit status
available for further inspection or processing.
@node Shell Operation
@subsection Shell Operation
The following is a brief description of the shell's operation when it
reads and executes a command. Basically, the shell does the
following:
@enumerate
@item
Reads its input from a file (@pxref{Shell Scripts}), from a string
supplied as an argument to the @option{-c} invocation option
(@pxref{Invoking Bash}), or from the user's terminal.
@item
Breaks the input into words and operators, obeying the quoting rules
described in @ref{Quoting}. These tokens are separated by
@code{metacharacters}. Alias expansion is performed by this step
(@pxref{Aliases}).
@item
Parses the tokens into simple and compound commands
(@pxref{Shell Commands}).
@item
Performs the various shell expansions (@pxref{Shell Expansions}), breaking
the expanded tokens into lists of filenames (@pxref{Filename Expansion})
and commands and arguments.
@item
Performs any necessary redirections (@pxref{Redirections}) and removes
the redirection operators and their operands from the argument list.
@item
Executes the command (@pxref{Executing Commands}).
@item
Optionally waits for the command to complete and collects its exit
status (@pxref{Exit Status}).
@end enumerate
@node Quoting
@subsection Quoting
@cindex quoting
@menu
* Escape Character:: How to remove the special meaning from a single
character.
* Single Quotes:: How to inhibit all interpretation of a sequence
of characters.
* Double Quotes:: How to suppress most of the interpretation of a
sequence of characters.
* ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings.
* Locale Translation:: How to translate strings into different languages.
@end menu
Quoting is used to remove the special meaning of certain
characters or words to the shell. Quoting can be used to
disable special treatment for special characters, to prevent
reserved words from being recognized as such, and to prevent
parameter expansion.
Each of the shell metacharacters (@pxref{Definitions})
has special meaning to the shell and must be quoted if it is to
represent itself.
When the command history expansion facilities are being used
(@pxref{History Interaction}), the
@var{history expansion} character, usually @samp{!}, must be quoted
to prevent history expansion. @xref{Bash History Facilities}, for
more details concerning history expansion.
There are three quoting mechanisms: the
@var{escape character}, single quotes, and double quotes.
@node Escape Character
@subsubsection Escape Character
A non-quoted backslash @samp{\} is the Bash escape character.
It preserves the literal value of the next character that follows,
with the exception of @code{newline}. If a @code{\newline} pair
appears, and the backslash itself is not quoted, the @code{\newline}
is treated as a line continuation (that is, it is removed from
the input stream and effectively ignored).
@node Single Quotes
@subsubsection Single Quotes
Enclosing characters in single quotes (@samp{'}) preserves the literal value
of each character within the quotes. A single quote may not occur
between single quotes, even when preceded by a backslash.
@node Double Quotes
@subsubsection Double Quotes
Enclosing characters in double quotes (@samp{"}) preserves the literal value
of all characters within the quotes, with the exception of
@samp{$}, @samp{`}, @samp{\},
and, when history expansion is enabled, @samp{!}.
The characters @samp{$} and @samp{`}
retain their special meaning within double quotes (@pxref{Shell Expansions}).
The backslash retains its special meaning only when followed by one of
the following characters:
@samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}.
Within double quotes, backslashes that are followed by one of these
characters are removed. Backslashes preceding characters without a
special meaning are left unmodified.
A double quote may be quoted within double quotes by preceding it with
a backslash.
If enabled, history expansion will be performed unless an @samp{!}
appearing in double quotes is escaped using a backslash.
The backslash preceding the @samp{!} is not removed.
The special parameters @samp{*} and @samp{@@} have special meaning
when in double quotes (@pxref{Shell Parameter Expansion}).
@node ANSI-C Quoting
@subsubsection ANSI-C Quoting
@cindex quoting, ANSI
Words of the form @code{$'@var{string}'} are treated specially. The
word expands to @var{string}, with backslash-escaped characters replaced
as specified by the ANSI C standard. Backslash escape sequences, if
present, are decoded as follows:
@table @code
@item \a
alert (bell)
@item \b
backspace
@item \e
@itemx \E
an escape character (not ANSI C)
@item \f
form feed
@item \n
newline
@item \r
carriage return
@item \t
horizontal tab
@item \v
vertical tab
@item \\
backslash
@item \'
single quote
@item \"
double quote
@item \@var{nnn}
the eight-bit character whose value is the octal value @var{nnn}
(one to three digits)
@item \x@var{HH}
the eight-bit character whose value is the hexadecimal value @var{HH}
(one or two hex digits)
@item \u@var{HHHH}
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
@var{HHHH} (one to four hex digits)
@item \U@var{HHHHHHHH}
the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
@var{HHHHHHHH} (one to eight hex digits)
@item \c@var{x}
a control-@var{x} character
@end table
@noindent
The expanded result is single-quoted, as if the dollar sign had not
been present.
@node Locale Translation
@subsubsection Locale-Specific Translation
@cindex localization
@cindex internationalization
@cindex native languages
@cindex translation, native languages
A double-quoted string preceded by a dollar sign (@samp{$}) will cause
the string to be translated according to the current locale.
If the current locale is @code{C} or @code{POSIX}, the dollar sign
is ignored.
If the string is translated and replaced, the replacement is
double-quoted.
@vindex LC_MESSAGES
@vindex TEXTDOMAIN
@vindex TEXTDOMAINDIR
Some systems use the message catalog selected by the @env{LC_MESSAGES}
shell variable. Others create the name of the message catalog from the
value of the @env{TEXTDOMAIN} shell variable, possibly adding a
suffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, you
may need to set the @env{TEXTDOMAINDIR} variable to the location of
the message catalog files. Still others use both variables in this
fashion:
@env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo.
@node Comments
@subsection Comments
@cindex comments, shell
In a non-interactive shell, or an interactive shell in which the
@code{interactive_comments} option to the @code{shopt}
builtin is enabled (@pxref{The Shopt Builtin}),
a word beginning with @samp{#}
causes that word and all remaining characters on that line to
be ignored. An interactive shell without the @code{interactive_comments}
option enabled does not allow comments. The @code{interactive_comments}
option is on by default in interactive shells.
@xref{Interactive Shells}, for a description of what makes
a shell interactive.
@node Shell Commands
@section Shell Commands
@cindex commands, shell
A simple shell command such as @code{echo a b c} consists of the command
itself followed by arguments, separated by spaces.
More complex shell commands are composed of simple commands arranged together
in a variety of ways: in a pipeline in which the output of one command
becomes the input of a second, in a loop or conditional construct, or in
some other grouping.
@menu
* Simple Commands:: The most common type of command.
* Pipelines:: Connecting the input and output of several
commands.
* Lists:: How to execute commands sequentially.
* Compound Commands:: Shell commands for control flow.
* Coprocesses:: Two-way communication between commands.
* GNU Parallel:: Running commands in parallel.
@end menu
@node Simple Commands
@subsection Simple Commands
@cindex commands, simple
A simple command is the kind of command encountered most often.
It's just a sequence of words separated by @code{blank}s, terminated
by one of the shell's control operators (@pxref{Definitions}). The
first word generally specifies a command to be executed, with the
rest of the words being that command's arguments.
The return status (@pxref{Exit Status}) of a simple command is
its exit status as provided
by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
the command was terminated by signal @var{n}.
@node Pipelines
@subsection Pipelines
@cindex pipeline
@cindex commands, pipelines
A @code{pipeline} is a sequence of simple commands separated by one of
the control operators @samp{|} or @samp{|&}.
@rwindex time
@rwindex !
@cindex command timing
The format for a pipeline is
@example
[time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{}
@end example
@noindent
The output of each command in the pipeline is connected via a pipe
to the input of the next command.
That is, each command reads the previous command's output. This
connection is performed before any redirections specified by the
command.
If @samp{|&} is used, @var{command1}'s standard error, in addition to
its standard output, is connected to
@var{command2}'s standard input through the pipe;
it is shorthand for @code{2>&1 |}.
This implicit redirection of the standard error to the standard output is
performed after any redirections specified by the command.
The reserved word @code{time} causes timing statistics
to be printed for the pipeline once it finishes.
The statistics currently consist of elapsed (wall-clock) time and
user and system time consumed by the command's execution.
The @option{-p} option changes the output format to that specified
by @sc{posix}.
When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
it does not recognize @code{time} as a reserved word if the next
token begins with a @samp{-}.
The @env{TIMEFORMAT} variable may be set to a format string that
specifies how the timing information should be displayed.
@xref{Bash Variables}, for a description of the available formats.
The use of @code{time} as a reserved word permits the timing of
shell builtins, shell functions, and pipelines. An external
@code{time} command cannot time these easily.
When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}
may be followed by a newline. In this case, the shell displays the
total user and system time consumed by the shell and its children.
The @env{TIMEFORMAT} variable may be used to specify the format of
the time information.
If the pipeline is not executed asynchronously (@pxref{Lists}), the
shell waits for all commands in the pipeline to complete.
Each command in a pipeline is executed in its own subshell
(@pxref{Command Execution Environment}). The exit
status of a pipeline is the exit status of the last command in the
pipeline, unless the @code{pipefail} option is enabled
(@pxref{The Set Builtin}).
If @code{pipefail} is enabled, the pipeline's return status is the
value of the last (rightmost) command to exit with a non-zero status,
or zero if all commands exit successfully.
If the reserved word @samp{!} precedes the pipeline, the
exit status is the logical negation of the exit status as described
above.
The shell waits for all commands in the pipeline to terminate before
returning a value.
@node Lists
@subsection Lists of Commands
@cindex commands, lists
A @code{list} is a sequence of one or more pipelines separated by one
of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
and optionally terminated by one of @samp{;}, @samp{&}, or a
@code{newline}.
Of these list operators, @samp{&&} and @samp{||}
have equal precedence, followed by @samp{;} and @samp{&},
which have equal precedence.
A sequence of one or more newlines may appear in a @code{list}
to delimit commands, equivalent to a semicolon.
If a command is terminated by the control operator @samp{&},
the shell executes the command asynchronously in a subshell.
This is known as executing the command in the @var{background}.
The shell does not wait for the command to finish, and the return
status is 0 (true).
When job control is not active (@pxref{Job Control}),
the standard input for asynchronous commands, in the absence of any
explicit redirections, is redirected from @code{/dev/null}.
Commands separated by a @samp{;} are executed sequentially; the shell
waits for each command to terminate in turn. The return status is the
exit status of the last command executed.
@sc{and} and @sc{or} lists are sequences of one or more pipelines
separated by the control operators @samp{&&} and @samp{||},
respectively. @sc{and} and @sc{or} lists are executed with left
associativity.
An @sc{and} list has the form
@example
@var{command1} && @var{command2}
@end example
@noindent
@var{command2} is executed if, and only if, @var{command1}
returns an exit status of zero.
An @sc{or} list has the form
@example
@var{command1} || @var{command2}
@end example
@noindent
@var{command2} is executed if, and only if, @var{command1}
returns a non-zero exit status.
The return status of
@sc{and} and @sc{or} lists is the exit status of the last command
executed in the list.
@node Compound Commands
@subsection Compound Commands
@cindex commands, compound
@menu
* Looping Constructs:: Shell commands for iterative action.
* Conditional Constructs:: Shell commands for conditional execution.
* Command Grouping:: Ways to group commands.
@end menu
Compound commands are the shell programming constructs.
Each construct begins with a reserved word or control operator and is
terminated by a corresponding reserved word or operator.
Any redirections (@pxref{Redirections}) associated with a compound command
apply to all commands within that compound command unless explicitly overridden.
In most cases a list of commands in a compound command's description may be
separated from the rest of the command by one or more newlines, and may be
followed by a newline in place of a semicolon.
Bash provides looping constructs, conditional commands, and mechanisms
to group commands and execute them as a unit.
@node Looping Constructs
@subsubsection Looping Constructs
@cindex commands, looping
Bash supports the following looping constructs.
Note that wherever a @samp{;} appears in the description of a
command's syntax, it may be replaced with one or more newlines.
@table @code
@item until
@rwindex until
@rwindex do
@rwindex done
The syntax of the @code{until} command is:
@example
until @var{test-commands}; do @var{consequent-commands}; done
@end example
Execute @var{consequent-commands} as long as
@var{test-commands} has an exit status which is not zero.
The return status is the exit status of the last command executed
in @var{consequent-commands}, or zero if none was executed.
@item while
@rwindex while
The syntax of the @code{while} command is:
@example
while @var{test-commands}; do @var{consequent-commands}; done
@end example
Execute @var{consequent-commands} as long as
@var{test-commands} has an exit status of zero.
The return status is the exit status of the last command executed
in @var{consequent-commands}, or zero if none was executed.
@item for
@rwindex for
The syntax of the @code{for} command is:
@example
for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done
@end example
Expand @var{words}, and execute @var{commands} once for each member
in the resultant list, with @var{name} bound to the current member.
If @samp{in @var{words}} is not present, the @code{for} command
executes the @var{commands} once for each positional parameter that is
set, as if @samp{in "$@@"} had been specified
(@pxref{Special Parameters}).
The return status is the exit status of the last command that executes.
If there are no items in the expansion of @var{words}, no commands are
executed, and the return status is zero.
An alternate form of the @code{for} command is also supported:
@example
for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
@end example
First, the arithmetic expression @var{expr1} is evaluated according
to the rules described below (@pxref{Shell Arithmetic}).
The arithmetic expression @var{expr2} is then evaluated repeatedly
until it evaluates to zero.
Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
executed and the arithmetic expression @var{expr3} is evaluated.
If any expression is omitted, it behaves as if it evaluates to 1.
The return value is the exit status of the last command in @var{commands}
that is executed, or false if any of the expressions is invalid.
@end table
The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
may be used to control loop execution.
@node Conditional Constructs
@subsubsection Conditional Constructs
@cindex commands, conditional
@table @code
@item if
@rwindex if
@rwindex then
@rwindex else
@rwindex elif
@rwindex fi
The syntax of the @code{if} command is:
@example
if @var{test-commands}; then
@var{consequent-commands};
[elif @var{more-test-commands}; then
@var{more-consequents};]
[else @var{alternate-consequents};]
fi
@end example
The @var{test-commands} list is executed, and if its return status is zero,
the @var{consequent-commands} list is executed.
If @var{test-commands} returns a non-zero status, each @code{elif} list
is executed in turn, and if its exit status is zero,
the corresponding @var{more-consequents} is executed and the
command completes.
If @samp{else @var{alternate-consequents}} is present, and
the final command in the final @code{if} or @code{elif} clause
has a non-zero exit status, then @var{alternate-consequents} is executed.
The return status is the exit status of the last command executed, or
zero if no condition tested true.
@item case
@rwindex case
@rwindex in
@rwindex esac
The syntax of the @code{case} command is:
@example
case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac
@end example
@code{case} will selectively execute the @var{command-list} corresponding to
the first @var{pattern} that matches @var{word}.
If the shell option @code{nocasematch}
(see the description of @code{shopt} in @ref{The Shopt Builtin})
is enabled, the match is performed without regard to the case
of alphabetic characters.
The @samp{|} is used to separate multiple patterns, and the @samp{)}
operator terminates a pattern list.
A list of patterns and an associated command-list is known
as a @var{clause}.
Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.
The @var{word} undergoes tilde expansion, parameter expansion, command
substitution, arithmetic expansion, and quote removal before matching is
attempted. Each @var{pattern} undergoes tilde expansion, parameter
expansion, command substitution, and arithmetic expansion.
There may be an arbitrary number of @code{case} clauses, each terminated
by a @samp{;;}, @samp{;&}, or @samp{;;&}.
The first pattern that matches determines the
command-list that is executed.
It's a common idiom to use @samp{*} as the final pattern to define the
default case, since that pattern will always match.
Here is an example using @code{case} in a script that could be used to
describe one interesting feature of an animal:
@example
echo -n "Enter the name of an animal: "
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat) echo -n "four";;
man | kangaroo ) echo -n "two";;
*) echo -n "an unknown number of";;
esac
echo " legs."
@end example
@noindent
If the @samp{;;} operator is used, no subsequent matches are attempted after
the first pattern match.
Using @samp{;&} in place of @samp{;;} causes execution to continue with
the @var{command-list} associated with the next clause, if any.
Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns
in the next clause, if any, and execute any associated @var{command-list}
on a successful match.
The return status is zero if no @var{pattern} is matched. Otherwise, the
return status is the exit status of the @var{command-list} executed.
@item select
@rwindex select
The @code{select} construct allows the easy generation of menus.
It has almost the same syntax as the @code{for} command:
@example
select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
@end example
The list of words following @code{in} is expanded, generating a list
of items. The set of expanded words is printed on the standard
error output stream, each preceded by a number. If the
@samp{in @var{words}} is omitted, the positional parameters are printed,
as if @samp{in "$@@"} had been specified.
The @env{PS3} prompt is then displayed and a line is read from the
standard input.
If the line consists of a number corresponding to one of the displayed
words, then the value of @var{name} is set to that word.
If the line is empty, the words and prompt are displayed again.
If @code{EOF} is read, the @code{select} command completes.
Any other value read causes @var{name} to be set to null.
The line read is saved in the variable @env{REPLY}.
The @var{commands} are executed after each selection until a
@code{break} command is executed, at which
point the @code{select} command completes.
Here is an example that allows the user to pick a filename from the
current directory, and displays the name and index of the file
selected.
@example
select fname in *;
do
echo you picked $fname \($REPLY\)
break;
done
@end example
@item ((@dots{}))
@example
(( @var{expression} ))
@end example
The arithmetic @var{expression} is evaluated according to the rules
described below (@pxref{Shell Arithmetic}).
If the value of the expression is non-zero, the return status is 0;
otherwise the return status is 1. This is exactly equivalent to
@example
let "@var{expression}"
@end example
@noindent
@xref{Bash Builtins}, for a full description of the @code{let} builtin.
@item [[@dots{}]]
@rwindex [[
@rwindex ]]
@example
[[ @var{expression} ]]
@end example
Return a status of 0 or 1 depending on the evaluation of
the conditional expression @var{expression}.
Expressions are composed of the primaries described below in
@ref{Bash Conditional Expressions}.
Word splitting and filename expansion are not performed on the words
between the @code{[[} and @code{]]}; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution, process
substitution, and quote removal are performed.
Conditional operators such as @samp{-f} must be unquoted to be recognized
as primaries.
When used with @code{[[}, the @samp{<} and @samp{>} operators sort
lexicographically using the current locale.
When the @samp{==} and @samp{!=} operators are used, the string to the
right of the operator is considered a pattern and matched according
to the rules described below in @ref{Pattern Matching},
as if the @code{extglob} shell option were enabled.