]> git.ipfire.org Git - thirdparty/bash.git/blob - doc/bashref.texi
more minor changes for builtins and array subscript expansion; fix to `wait -n'
[thirdparty/bash.git] / doc / bashref.texi
1 \input texinfo.tex @c -*- texinfo -*-
2 @c %**start of header
3 @setfilename bashref.info
4 @settitle Bash Reference Manual
5
6 @include version.texi
7 @c %**end of header
8
9 @copying
10 This text is a brief description of the features that are present in
11 the Bash shell (version @value{VERSION}, @value{UPDATED}).
12
13 This is Edition @value{EDITION}, last updated @value{UPDATED},
14 of @cite{The GNU Bash Reference Manual},
15 for @code{Bash}, Version @value{VERSION}.
16
17 Copyright @copyright{} 1988--2021 Free Software Foundation, Inc.
18
19 @quotation
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.3 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
24 A copy of the license is included in the section entitled
25 ``GNU Free Documentation License''.
26 @end quotation
27 @end copying
28
29 @defcodeindex bt
30 @defcodeindex rw
31 @set BashFeatures
32
33 @dircategory Basics
34 @direntry
35 * Bash: (bash). The GNU Bourne-Again SHell.
36 @end direntry
37
38 @finalout
39
40 @titlepage
41 @title Bash Reference Manual
42 @subtitle Reference Documentation for Bash
43 @subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}.
44 @subtitle @value{UPDATED-MONTH}
45 @author Chet Ramey, Case Western Reserve University
46 @author Brian Fox, Free Software Foundation
47
48 @page
49 @vskip 0pt plus 1filll
50 @insertcopying
51
52 @end titlepage
53
54 @contents
55
56 @ifnottex
57 @node Top, Introduction, (dir), (dir)
58 @top Bash Features
59
60 This text is a brief description of the features that are present in
61 the Bash shell (version @value{VERSION}, @value{UPDATED}).
62 The Bash home page is @url{http://www.gnu.org/software/bash/}.
63
64 This is Edition @value{EDITION}, last updated @value{UPDATED},
65 of @cite{The GNU Bash Reference Manual},
66 for @code{Bash}, Version @value{VERSION}.
67
68 Bash contains features that appear in other popular shells, and some
69 features that only appear in Bash. Some of the shells that Bash has
70 borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell
71 (@file{ksh}), and the C-shell (@file{csh} and its successor,
72 @file{tcsh}). The following menu breaks the features up into
73 categories, noting which features were inspired by other shells and
74 which are specific to Bash.
75
76 This manual is meant as a brief introduction to features found in
77 Bash. The Bash manual page should be used as the definitive
78 reference on shell behavior.
79
80 @menu
81 * Introduction:: An introduction to the shell.
82 * Definitions:: Some definitions used in the rest of this
83 manual.
84 * Basic Shell Features:: The shell "building blocks".
85 * Shell Builtin Commands:: Commands that are a part of the shell.
86 * Shell Variables:: Variables used or set by Bash.
87 * Bash Features:: Features found only in Bash.
88 * Job Control:: What job control is and how Bash allows you
89 to use it.
90 * Command Line Editing:: Chapter describing the command line
91 editing features.
92 * Using History Interactively:: Command History Expansion
93 * Installing Bash:: How to build and install Bash on your system.
94 * Reporting Bugs:: How to report bugs in Bash.
95 * Major Differences From The Bourne Shell:: A terse list of the differences
96 between Bash and historical
97 versions of /bin/sh.
98 * GNU Free Documentation License:: Copying and sharing this documentation.
99 * Indexes:: Various indexes for this manual.
100 @end menu
101 @end ifnottex
102
103 @node Introduction
104 @chapter Introduction
105 @menu
106 * What is Bash?:: A short description of Bash.
107 * What is a shell?:: A brief introduction to shells.
108 @end menu
109
110 @node What is Bash?
111 @section What is Bash?
112
113 Bash is the shell, or command language interpreter,
114 for the @sc{gnu} operating system.
115 The name is an acronym for the @samp{Bourne-Again SHell},
116 a pun on Stephen Bourne, the author of the direct ancestor of
117 the current Unix shell @code{sh},
118 which appeared in the Seventh Edition Bell Labs Research version
119 of Unix.
120
121 Bash is largely compatible with @code{sh} and incorporates useful
122 features from the Korn shell @code{ksh} and the C shell @code{csh}.
123 It is intended to be a conformant implementation of the @sc{ieee}
124 @sc{posix} Shell and Tools portion of the @sc{ieee} @sc{posix}
125 specification (@sc{ieee} Standard 1003.1).
126 It offers functional improvements over @code{sh} for both interactive and
127 programming use.
128
129 While the @sc{gnu} operating system provides other shells, including
130 a version of @code{csh}, Bash is the default shell.
131 Like other @sc{gnu} software, Bash is quite portable. It currently runs
132 on nearly every version of Unix and a few other operating systems @minus{}
133 independently-supported ports exist for @sc{ms-dos}, @sc{os/2},
134 and Windows platforms.
135
136 @node What is a shell?
137 @section What is a shell?
138
139 At its base, a shell is simply a macro processor that executes
140 commands. The term macro processor means functionality where text
141 and symbols are expanded to create larger expressions.
142
143 A Unix shell is both a command interpreter and a programming
144 language. As a command interpreter, the shell provides the user
145 interface to the rich set of @sc{gnu} utilities. The programming
146 language features allow these utilities to be combined.
147 Files containing commands can be created, and become
148 commands themselves. These new commands have the same status as
149 system commands in directories such as @file{/bin}, allowing users
150 or groups to establish custom environments to automate their common
151 tasks.
152
153 Shells may be used interactively or non-interactively. In
154 interactive mode, they accept input typed from the keyboard.
155 When executing non-interactively, shells execute commands read
156 from a file.
157
158 A shell allows execution of @sc{gnu} commands, both synchronously and
159 asynchronously.
160 The shell waits for synchronous commands to complete before accepting
161 more input; asynchronous commands continue to execute in parallel
162 with the shell while it reads and executes additional commands.
163 The @dfn{redirection} constructs permit
164 fine-grained control of the input and output of those commands.
165 Moreover, the shell allows control over the contents of commands'
166 environments.
167
168 Shells also provide a small set of built-in
169 commands (@dfn{builtins}) implementing functionality impossible
170 or inconvenient to obtain via separate utilities.
171 For example, @code{cd}, @code{break}, @code{continue}, and
172 @code{exec} cannot be implemented outside of the shell because
173 they directly manipulate the shell itself.
174 The @code{history}, @code{getopts}, @code{kill}, or @code{pwd}
175 builtins, among others, could be implemented in separate utilities,
176 but they are more convenient to use as builtin commands.
177 All of the shell builtins are described in
178 subsequent sections.
179
180 While executing commands is essential, most of the power (and
181 complexity) of shells is due to their embedded programming
182 languages. Like any high-level language, the shell provides
183 variables, flow control constructs, quoting, and functions.
184
185 Shells offer features geared specifically for
186 interactive use rather than to augment the programming language.
187 These interactive features include job control, command line
188 editing, command history and aliases. Each of these features is
189 described in this manual.
190
191 @node Definitions
192 @chapter Definitions
193 These definitions are used throughout the remainder of this manual.
194
195 @table @code
196
197 @item POSIX
198 @cindex POSIX
199 A family of open system standards based on Unix. Bash
200 is primarily concerned with the Shell and Utilities portion of the
201 @sc{posix} 1003.1 standard.
202
203 @item blank
204 A space or tab character.
205
206 @item builtin
207 @cindex builtin
208 A command that is implemented internally by the shell itself, rather
209 than by an executable program somewhere in the file system.
210
211 @item control operator
212 @cindex control operator
213 A @code{token} that performs a control function. It is a @code{newline}
214 or one of the following:
215 @samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;}, @samp{;&}, @samp{;;&},
216 @samp{|}, @samp{|&}, @samp{(}, or @samp{)}.
217
218 @item exit status
219 @cindex exit status
220 The value returned by a command to its caller. The value is restricted
221 to eight bits, so the maximum value is 255.
222
223 @item field
224 @cindex field
225 A unit of text that is the result of one of the shell expansions. After
226 expansion, when executing a command, the resulting fields are used as
227 the command name and arguments.
228
229 @item filename
230 @cindex filename
231 A string of characters used to identify a file.
232
233 @item job
234 @cindex job
235 A set of processes comprising a pipeline, and any processes descended
236 from it, that are all in the same process group.
237
238 @item job control
239 @cindex job control
240 A mechanism by which users can selectively stop (suspend) and restart
241 (resume) execution of processes.
242
243 @item metacharacter
244 @cindex metacharacter
245 A character that, when unquoted, separates words. A metacharacter is
246 a @code{space}, @code{tab}, @code{newline}, or one of the following characters:
247 @samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or
248 @samp{>}.
249
250 @item name
251 @cindex name
252 @cindex identifier
253 A @code{word} consisting solely of letters, numbers, and underscores,
254 and beginning with a letter or underscore. @code{Name}s are used as
255 shell variable and function names.
256 Also referred to as an @code{identifier}.
257
258 @item operator
259 @cindex operator, shell
260 A @code{control operator} or a @code{redirection operator}.
261 @xref{Redirections}, for a list of redirection operators.
262 Operators contain at least one unquoted @code{metacharacter}.
263
264 @item process group
265 @cindex process group
266 A collection of related processes each having the same process
267 group @sc{id}.
268
269 @item process group ID
270 @cindex process group ID
271 A unique identifier that represents a @code{process group}
272 during its lifetime.
273
274 @item reserved word
275 @cindex reserved word
276 A @code{word} that has a special meaning to the shell. Most reserved
277 words introduce shell flow control constructs, such as @code{for} and
278 @code{while}.
279
280 @item return status
281 @cindex return status
282 A synonym for @code{exit status}.
283
284 @item signal
285 @cindex signal
286 A mechanism by which a process may be notified by the kernel
287 of an event occurring in the system.
288
289 @item special builtin
290 @cindex special builtin
291 A shell builtin command that has been classified as special by the
292 @sc{posix} standard.
293
294 @item token
295 @cindex token
296 A sequence of characters considered a single unit by the shell.
297 It is either a @code{word} or an @code{operator}.
298
299 @item word
300 @cindex word
301 A sequence of characters treated as a unit by the shell.
302 Words may not include unquoted @code{metacharacters}.
303 @end table
304
305 @node Basic Shell Features
306 @chapter Basic Shell Features
307 @cindex Bourne shell
308
309 Bash is an acronym for @samp{Bourne-Again SHell}.
310 The Bourne shell is
311 the traditional Unix shell originally written by Stephen Bourne.
312 All of the Bourne shell builtin commands are available in Bash,
313 The rules for evaluation and quoting are taken from the @sc{posix}
314 specification for the `standard' Unix shell.
315
316 This chapter briefly summarizes the shell's `building blocks':
317 commands, control structures, shell functions, shell @i{parameters},
318 shell expansions,
319 @i{redirections}, which are a way to direct input and output from
320 and to named files, and how the shell executes commands.
321
322 @menu
323 * Shell Syntax:: What your input means to the shell.
324 * Shell Commands:: The types of commands you can use.
325 * Shell Functions:: Grouping commands by name.
326 * Shell Parameters:: How the shell stores values.
327 * Shell Expansions:: How Bash expands parameters and the various
328 expansions available.
329 * Redirections:: A way to control where input and output go.
330 * Executing Commands:: What happens when you run a command.
331 * Shell Scripts:: Executing files of shell commands.
332 @end menu
333
334 @node Shell Syntax
335 @section Shell Syntax
336 @menu
337 * Shell Operation:: The basic operation of the shell.
338 * Quoting:: How to remove the special meaning from characters.
339 * Comments:: How to specify comments.
340 @end menu
341
342 When the shell reads input, it proceeds through a
343 sequence of operations. If the input indicates the beginning of a
344 comment, the shell ignores the comment symbol (@samp{#}), and the rest
345 of that line.
346
347 Otherwise, roughly speaking, the shell reads its input and
348 divides the input into words and operators, employing the quoting rules
349 to select which meanings to assign various words and characters.
350
351 The shell then parses these tokens into commands and other constructs,
352 removes the special meaning of certain words or characters, expands
353 others, redirects input and output as needed, executes the specified
354 command, waits for the command's exit status, and makes that exit status
355 available for further inspection or processing.
356
357 @node Shell Operation
358 @subsection Shell Operation
359
360 The following is a brief description of the shell's operation when it
361 reads and executes a command. Basically, the shell does the
362 following:
363
364 @enumerate
365 @item
366 Reads its input from a file (@pxref{Shell Scripts}), from a string
367 supplied as an argument to the @option{-c} invocation option
368 (@pxref{Invoking Bash}), or from the user's terminal.
369
370 @item
371 Breaks the input into words and operators, obeying the quoting rules
372 described in @ref{Quoting}. These tokens are separated by
373 @code{metacharacters}. Alias expansion is performed by this step
374 (@pxref{Aliases}).
375
376 @item
377 Parses the tokens into simple and compound commands
378 (@pxref{Shell Commands}).
379
380 @item
381 Performs the various shell expansions (@pxref{Shell Expansions}), breaking
382 the expanded tokens into lists of filenames (@pxref{Filename Expansion})
383 and commands and arguments.
384
385 @item
386 Performs any necessary redirections (@pxref{Redirections}) and removes
387 the redirection operators and their operands from the argument list.
388
389 @item
390 Executes the command (@pxref{Executing Commands}).
391
392 @item
393 Optionally waits for the command to complete and collects its exit
394 status (@pxref{Exit Status}).
395
396 @end enumerate
397
398 @node Quoting
399 @subsection Quoting
400 @cindex quoting
401 @menu
402 * Escape Character:: How to remove the special meaning from a single
403 character.
404 * Single Quotes:: How to inhibit all interpretation of a sequence
405 of characters.
406 * Double Quotes:: How to suppress most of the interpretation of a
407 sequence of characters.
408 * ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings.
409 * Locale Translation:: How to translate strings into different languages.
410 @end menu
411
412 Quoting is used to remove the special meaning of certain
413 characters or words to the shell. Quoting can be used to
414 disable special treatment for special characters, to prevent
415 reserved words from being recognized as such, and to prevent
416 parameter expansion.
417
418 Each of the shell metacharacters (@pxref{Definitions})
419 has special meaning to the shell and must be quoted if it is to
420 represent itself.
421 When the command history expansion facilities are being used
422 (@pxref{History Interaction}), the
423 @dfn{history expansion} character, usually @samp{!}, must be quoted
424 to prevent history expansion. @xref{Bash History Facilities}, for
425 more details concerning history expansion.
426
427 There are three quoting mechanisms: the
428 @dfn{escape character}, single quotes, and double quotes.
429
430 @node Escape Character
431 @subsubsection Escape Character
432 A non-quoted backslash @samp{\} is the Bash escape character.
433 It preserves the literal value of the next character that follows,
434 with the exception of @code{newline}. If a @code{\newline} pair
435 appears, and the backslash itself is not quoted, the @code{\newline}
436 is treated as a line continuation (that is, it is removed from
437 the input stream and effectively ignored).
438
439 @node Single Quotes
440 @subsubsection Single Quotes
441
442 Enclosing characters in single quotes (@samp{'}) preserves the literal value
443 of each character within the quotes. A single quote may not occur
444 between single quotes, even when preceded by a backslash.
445
446 @node Double Quotes
447 @subsubsection Double Quotes
448
449 Enclosing characters in double quotes (@samp{"}) preserves the literal value
450 of all characters within the quotes, with the exception of
451 @samp{$}, @samp{`}, @samp{\},
452 and, when history expansion is enabled, @samp{!}.
453 When the shell is in
454 @sc{posix} mode (@pxref{Bash POSIX Mode}),
455 the @samp{!} has no special meaning
456 within double quotes, even when history expansion is enabled.
457 The characters @samp{$} and @samp{`}
458 retain their special meaning within double quotes (@pxref{Shell Expansions}).
459 The backslash retains its special meaning only when followed by one of
460 the following characters:
461 @samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}.
462 Within double quotes, backslashes that are followed by one of these
463 characters are removed. Backslashes preceding characters without a
464 special meaning are left unmodified.
465 A double quote may be quoted within double quotes by preceding it with
466 a backslash.
467 If enabled, history expansion will be performed unless an @samp{!}
468 appearing in double quotes is escaped using a backslash.
469 The backslash preceding the @samp{!} is not removed.
470
471 The special parameters @samp{*} and @samp{@@} have special meaning
472 when in double quotes (@pxref{Shell Parameter Expansion}).
473
474 @node ANSI-C Quoting
475 @subsubsection ANSI-C Quoting
476 @cindex quoting, ANSI
477
478 Character sequences of the form $'@var{string}' are treated as a special
479 kind of single quotes.
480 The sequence expands to @var{string}, with backslash-escaped characters
481 in @var{string} replaced as specified by the ANSI C standard.
482 Backslash escape sequences, if present, are decoded as follows:
483
484 @table @code
485 @item \a
486 alert (bell)
487 @item \b
488 backspace
489 @item \e
490 @itemx \E
491 an escape character (not ANSI C)
492 @item \f
493 form feed
494 @item \n
495 newline
496 @item \r
497 carriage return
498 @item \t
499 horizontal tab
500 @item \v
501 vertical tab
502 @item \\
503 backslash
504 @item \'
505 single quote
506 @item \"
507 double quote
508 @item \?
509 question mark
510 @item \@var{nnn}
511 the eight-bit character whose value is the octal value @var{nnn}
512 (one to three octal digits)
513 @item \x@var{HH}
514 the eight-bit character whose value is the hexadecimal value @var{HH}
515 (one or two hex digits)
516 @item \u@var{HHHH}
517 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
518 @var{HHHH} (one to four hex digits)
519 @item \U@var{HHHHHHHH}
520 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
521 @var{HHHHHHHH} (one to eight hex digits)
522 @item \c@var{x}
523 a control-@var{x} character
524 @end table
525
526 @noindent
527 The expanded result is single-quoted, as if the dollar sign had not
528 been present.
529
530 @node Locale Translation
531 @subsubsection Locale-Specific Translation
532 @cindex localization
533 @cindex internationalization
534 @cindex native languages
535 @cindex translation, native languages
536 @menu
537 * Creating Internationalized Scripts:: How to use translations and different
538 languages in your scripts.
539 @end menu
540
541 Prefixing a double-quoted string with a dollar sign (@samp{$}), such
542 as @verb{|$"hello, world"|},
543 will cause the string to be translated according to the current locale.
544 The @code{gettext} infrastructure performs the lookup and
545 translation, using the @code{LC_MESSAGES}, @code{TEXTDOMAINDIR},
546 and @code{TEXTDOMAIN} shell variables, as explained below.
547 See the gettext documentation for additional details not covered here.
548 If the current locale is @code{C} or @code{POSIX},
549 or if there are no translations available,
550 the dollar sign is ignored, and the shell doesn't attempt to translate the
551 string.
552 Since this is a form of quoting, the string always remains double-quoted,
553 whether or not it is translated and replaced.
554
555 The rest of this section is a brief overview of how you use gettext to
556 create translations for strings in a shell script named @var{scriptname}.
557 There are more details in the gettext documentation.
558
559 @node Creating Internationalized Scripts
560 @cindex internationalized scripts
561 @cindex string translations
562 Once you've marked the strings in your script
563 that you want to translate using $"...",
564 you create a gettext "template" file using the command
565
566 @example
567 bash --dump-po-strings @var{scriptname} > @var{domain}.pot
568 @end example
569
570 @noindent
571 The @var{domain} is your @dfn{message domain}.
572 It's just an arbitrary string that's used to identify the files gettext
573 needs, like a package or script name.
574 It needs to be unique among all
575 the message domains on systems where you install the translations, so
576 gettext knows which translations correspond to your script.
577 You'll use the template file to create translations for each target language.
578 The template file conventionally has the suffix @samp{.pot}.
579
580 You copy this template file to a separate file for each target language
581 you want to support (called "PO" files, which use the suffix @samp{.po}).
582 PO files use various naming conventions, but
583 when you are working to translate a template file into a particular
584 language, you first copy the template file to a file whose name is the
585 language you want to target, with the @samp{.po} suffix.
586 For instance, the Spanish translations of your strings would be
587 in a file named @samp{es.po}, and to get started using a message
588 domain named "example," you would run
589
590 @example
591 cp example.pot es.po
592 @end example
593
594 @noindent
595 Ultimately, PO files are often named @var{domain}.po and installed in
596 directories that contain multiple translation files for a particular language.
597
598 Whichever naming convention you choose, you will need to translate the
599 strings in the PO files into the appropriate languages.
600 This has to be done manually.
601
602 When you have the translations and PO files complete, you'll use the
603 gettext tools to produce what are called "MO" files, which are compiled
604 versions of the PO files the gettext tools use to look up translations
605 efficiently.
606 MO files are also called "message catalog" files.
607 You use the @command{msgfmt} program to do this.
608 For instance, if you had a file with Spanish translations, you could run
609
610 @example
611 msgfmt -o es.mo es.po
612 @end example
613
614 @noindent
615 to produce the corresponding MO file.
616
617 Once you have the MO files, you decide where to install them and use the
618 @code{TEXTDOMAINDIR} shell variable to tell the gettext tools where they are.
619 Make sure to use the same message domain to name the MO files
620 as you did for the PO files when you install them.
621
622 @vindex LANG
623 @vindex LC_MESSAGES
624 @vindex TEXTDOMAIN
625 @vindex TEXTDOMAINDIR
626 Your users will use the @env{LANG} or @env{LC_MESSAGES} shell variables to
627 select the desired language.
628
629 You set the @env{TEXTDOMAIN} variable to the script's message domain.
630 As above, you use the message domain to name your translation files.
631
632 You, or possibly your users, set the @env{TEXTDOMAINDIR} variable to the
633 name of a directory where the message catalog files are stored.
634 If you install the message files into the system's standard message catalog
635 directory, you don't need to worry about this variable.
636
637 The directory where the message catalog files are stored varies between
638 systems.
639 Some use the message catalog selected by the @env{LC_MESSAGES}
640 shell variable.
641 Others create the name of the message catalog from the value of the
642 @env{TEXTDOMAIN} shell variable, possibly adding the @samp{.mo} suffix.
643 If you use the @env{TEXTDOMAIN} variable, you may need to set the
644 @env{TEXTDOMAINDIR} variable to the location of the message catalog files,
645 as above.
646 It's common to use both variables in this fashion:
647 @env{$TEXTDOMAINDIR}/@env{$LC_MESSAGES}/LC_MESSAGES/@env{$TEXTDOMAIN}.mo.
648
649 If you used that last convention, and you wanted to store the message
650 catalog files with Spanish (es) and Esperanto (eo) translations into a
651 local directory you use for custom translation files, you could run
652
653 @example
654 TEXTDOMAIN=example
655 TEXTDOMAINDIR=/usr/local/share/locale
656
657 cp es.mo $@{TEXTDOMAINDIR@}/es/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
658 cp eo.mo $@{TEXTDOMAINDIR@}/eo/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
659 @end example
660
661 When all of this is done, and the message catalog files containing the
662 compiled translations are installed in the correct location,
663 your users will be able to see translated strings
664 in any of the supported languages by setting the @env{LANG} or
665 @env{LC_MESSAGES} environment variables before running your script.
666
667 @node Comments
668 @subsection Comments
669 @cindex comments, shell
670
671 In a non-interactive shell, or an interactive shell in which the
672 @code{interactive_comments} option to the @code{shopt}
673 builtin is enabled (@pxref{The Shopt Builtin}),
674 a word beginning with @samp{#}
675 causes that word and all remaining characters on that line to
676 be ignored. An interactive shell without the @code{interactive_comments}
677 option enabled does not allow comments. The @code{interactive_comments}
678 option is on by default in interactive shells.
679 @xref{Interactive Shells}, for a description of what makes
680 a shell interactive.
681
682 @node Shell Commands
683 @section Shell Commands
684 @cindex commands, shell
685
686 A simple shell command such as @code{echo a b c} consists of the command
687 itself followed by arguments, separated by spaces.
688
689 More complex shell commands are composed of simple commands arranged together
690 in a variety of ways: in a pipeline in which the output of one command
691 becomes the input of a second, in a loop or conditional construct, or in
692 some other grouping.
693
694 @menu
695 * Reserved Words:: Words that have special meaning to the shell.
696 * Simple Commands:: The most common type of command.
697 * Pipelines:: Connecting the input and output of several
698 commands.
699 * Lists:: How to execute commands sequentially.
700 * Compound Commands:: Shell commands for control flow.
701 * Coprocesses:: Two-way communication between commands.
702 * GNU Parallel:: Running commands in parallel.
703 @end menu
704
705 @node Reserved Words
706 @subsection Reserved Words
707 @cindex reserved words
708
709 Reserved words are words that have special meaning to the shell.
710 They are used to begin and end the shell's compound commands.
711
712 The following words are recognized as reserved when unquoted and
713 the first word of a command (see below for exceptions):
714
715 @multitable @columnfractions .1 .1 .1 .1 .12 .1
716 @item @code{if} @tab @code{then} @tab @code{elif}
717 @tab @code{else} @tab @code{fi} @tab @code{time}
718 @item @code{for} @tab @code{in} @tab @code{until}
719 @tab @code{while} @tab @code{do} @tab @code{done}
720 @item @code{case} @tab @code{esac} @tab @code{coproc}
721 @tab @code{select} @tab @code{function}
722 @item @code{@{} @tab @code{@}} @tab @code{[[} @tab @code{]]} @tab @code{!}
723 @end multitable
724
725 @noindent
726 @code{in} is recognized as a reserved word if it is the third word of a
727 @code{case} or @code{select} command.
728 @code{in} and @code{do} are recognized as reserved
729 words if they are the third word in a @code{for} command.
730
731 @node Simple Commands
732 @subsection Simple Commands
733 @cindex commands, simple
734
735 A simple command is the kind of command encountered most often.
736 It's just a sequence of words separated by @code{blank}s, terminated
737 by one of the shell's control operators (@pxref{Definitions}). The
738 first word generally specifies a command to be executed, with the
739 rest of the words being that command's arguments.
740
741 The return status (@pxref{Exit Status}) of a simple command is
742 its exit status as provided
743 by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
744 the command was terminated by signal @var{n}.
745
746 @node Pipelines
747 @subsection Pipelines
748 @cindex pipeline
749 @cindex commands, pipelines
750
751 A @code{pipeline} is a sequence of one or more commands separated by
752 one of the control operators @samp{|} or @samp{|&}.
753
754 @rwindex time
755 @rwindex !
756 @cindex command timing
757 The format for a pipeline is
758 @example
759 [time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{}
760 @end example
761
762 @noindent
763 The output of each command in the pipeline is connected via a pipe
764 to the input of the next command.
765 That is, each command reads the previous command's output. This
766 connection is performed before any redirections specified by
767 @var{command1}.
768
769 If @samp{|&} is used, @var{command1}'s standard error, in addition to
770 its standard output, is connected to
771 @var{command2}'s standard input through the pipe;
772 it is shorthand for @code{2>&1 |}.
773 This implicit redirection of the standard error to the standard output is
774 performed after any redirections specified by @var{command1}.
775
776 The reserved word @code{time} causes timing statistics
777 to be printed for the pipeline once it finishes.
778 The statistics currently consist of elapsed (wall-clock) time and
779 user and system time consumed by the command's execution.
780 The @option{-p} option changes the output format to that specified
781 by @sc{posix}.
782 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
783 it does not recognize @code{time} as a reserved word if the next
784 token begins with a @samp{-}.
785 The @env{TIMEFORMAT} variable may be set to a format string that
786 specifies how the timing information should be displayed.
787 @xref{Bash Variables}, for a description of the available formats.
788 The use of @code{time} as a reserved word permits the timing of
789 shell builtins, shell functions, and pipelines. An external
790 @code{time} command cannot time these easily.
791
792 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}
793 may be followed by a newline. In this case, the shell displays the
794 total user and system time consumed by the shell and its children.
795 The @env{TIMEFORMAT} variable may be used to specify the format of
796 the time information.
797
798 If the pipeline is not executed asynchronously (@pxref{Lists}), the
799 shell waits for all commands in the pipeline to complete.
800
801 Each command in a pipeline is executed in its own subshell, which is a
802 separate process (@pxref{Command Execution Environment}).
803 If the @code{lastpipe} option is enabled using the @code{shopt} builtin
804 (@pxref{The Shopt Builtin}),
805 the last element of a pipeline may be run by the shell process
806 when job control is not active.
807
808 The exit
809 status of a pipeline is the exit status of the last command in the
810 pipeline, unless the @code{pipefail} option is enabled
811 (@pxref{The Set Builtin}).
812 If @code{pipefail} is enabled, the pipeline's return status is the
813 value of the last (rightmost) command to exit with a non-zero status,
814 or zero if all commands exit successfully.
815 If the reserved word @samp{!} precedes the pipeline, the
816 exit status is the logical negation of the exit status as described
817 above.
818 The shell waits for all commands in the pipeline to terminate before
819 returning a value.
820
821 @node Lists
822 @subsection Lists of Commands
823 @cindex commands, lists
824
825 A @code{list} is a sequence of one or more pipelines separated by one
826 of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
827 and optionally terminated by one of @samp{;}, @samp{&}, or a
828 @code{newline}.
829
830 Of these list operators, @samp{&&} and @samp{||}
831 have equal precedence, followed by @samp{;} and @samp{&},
832 which have equal precedence.
833
834 A sequence of one or more newlines may appear in a @code{list}
835 to delimit commands, equivalent to a semicolon.
836
837 If a command is terminated by the control operator @samp{&},
838 the shell executes the command asynchronously in a subshell.
839 This is known as executing the command in the @dfn{background},
840 and these are referred to as @dfn{asynchronous} commands.
841 The shell does not wait for the command to finish, and the return
842 status is 0 (true).
843 When job control is not active (@pxref{Job Control}),
844 the standard input for asynchronous commands, in the absence of any
845 explicit redirections, is redirected from @code{/dev/null}.
846
847 Commands separated by a @samp{;} are executed sequentially; the shell
848 waits for each command to terminate in turn. The return status is the
849 exit status of the last command executed.
850
851 @sc{and} and @sc{or} lists are sequences of one or more pipelines
852 separated by the control operators @samp{&&} and @samp{||},
853 respectively. @sc{and} and @sc{or} lists are executed with left
854 associativity.
855
856 An @sc{and} list has the form
857 @example
858 @var{command1} && @var{command2}
859 @end example
860
861 @noindent
862 @var{command2} is executed if, and only if, @var{command1}
863 returns an exit status of zero (success).
864
865 An @sc{or} list has the form
866 @example
867 @var{command1} || @var{command2}
868 @end example
869
870 @noindent
871 @var{command2} is executed if, and only if, @var{command1}
872 returns a non-zero exit status.
873
874 The return status of
875 @sc{and} and @sc{or} lists is the exit status of the last command
876 executed in the list.
877
878 @node Compound Commands
879 @subsection Compound Commands
880 @cindex commands, compound
881
882 @menu
883 * Looping Constructs:: Shell commands for iterative action.
884 * Conditional Constructs:: Shell commands for conditional execution.
885 * Command Grouping:: Ways to group commands.
886 @end menu
887
888 Compound commands are the shell programming language constructs.
889 Each construct begins with a reserved word or control operator and is
890 terminated by a corresponding reserved word or operator.
891 Any redirections (@pxref{Redirections}) associated with a compound command
892 apply to all commands within that compound command unless explicitly overridden.
893
894 In most cases a list of commands in a compound command's description may be
895 separated from the rest of the command by one or more newlines, and may be
896 followed by a newline in place of a semicolon.
897
898 Bash provides looping constructs, conditional commands, and mechanisms
899 to group commands and execute them as a unit.
900
901 @node Looping Constructs
902 @subsubsection Looping Constructs
903 @cindex commands, looping
904
905 Bash supports the following looping constructs.
906
907 Note that wherever a @samp{;} appears in the description of a
908 command's syntax, it may be replaced with one or more newlines.
909
910 @table @code
911 @item until
912 @rwindex until
913 @rwindex do
914 @rwindex done
915 The syntax of the @code{until} command is:
916
917 @example
918 until @var{test-commands}; do @var{consequent-commands}; done
919 @end example
920
921 Execute @var{consequent-commands} as long as
922 @var{test-commands} has an exit status which is not zero.
923 The return status is the exit status of the last command executed
924 in @var{consequent-commands}, or zero if none was executed.
925
926 @item while
927 @rwindex while
928 The syntax of the @code{while} command is:
929
930 @example
931 while @var{test-commands}; do @var{consequent-commands}; done
932 @end example
933
934 Execute @var{consequent-commands} as long as
935 @var{test-commands} has an exit status of zero.
936 The return status is the exit status of the last command executed
937 in @var{consequent-commands}, or zero if none was executed.
938
939 @item for
940 @rwindex for
941 The syntax of the @code{for} command is:
942
943 @example
944 for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done
945 @end example
946
947 Expand @var{words} (@pxref{Shell Expansions}), and execute @var{commands}
948 once for each member
949 in the resultant list, with @var{name} bound to the current member.
950 If @samp{in @var{words}} is not present, the @code{for} command
951 executes the @var{commands} once for each positional parameter that is
952 set, as if @samp{in "$@@"} had been specified
953 (@pxref{Special Parameters}).
954
955 The return status is the exit status of the last command that executes.
956 If there are no items in the expansion of @var{words}, no commands are
957 executed, and the return status is zero.
958
959 An alternate form of the @code{for} command is also supported:
960
961 @example
962 for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
963 @end example
964
965 First, the arithmetic expression @var{expr1} is evaluated according
966 to the rules described below (@pxref{Shell Arithmetic}).
967 The arithmetic expression @var{expr2} is then evaluated repeatedly
968 until it evaluates to zero.
969 Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
970 executed and the arithmetic expression @var{expr3} is evaluated.
971 If any expression is omitted, it behaves as if it evaluates to 1.
972 The return value is the exit status of the last command in @var{commands}
973 that is executed, or false if any of the expressions is invalid.
974 @end table
975
976 The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
977 may be used to control loop execution.
978
979 @node Conditional Constructs
980 @subsubsection Conditional Constructs
981 @cindex commands, conditional
982
983 @table @code
984 @item if
985 @rwindex if
986 @rwindex then
987 @rwindex else
988 @rwindex elif
989 @rwindex fi
990 The syntax of the @code{if} command is:
991
992 @example
993 if @var{test-commands}; then
994 @var{consequent-commands};
995 [elif @var{more-test-commands}; then
996 @var{more-consequents};]
997 [else @var{alternate-consequents};]
998 fi
999 @end example
1000
1001 The @var{test-commands} list is executed, and if its return status is zero,
1002 the @var{consequent-commands} list is executed.
1003 If @var{test-commands} returns a non-zero status, each @code{elif} list
1004 is executed in turn, and if its exit status is zero,
1005 the corresponding @var{more-consequents} is executed and the
1006 command completes.
1007 If @samp{else @var{alternate-consequents}} is present, and
1008 the final command in the final @code{if} or @code{elif} clause
1009 has a non-zero exit status, then @var{alternate-consequents} is executed.
1010 The return status is the exit status of the last command executed, or
1011 zero if no condition tested true.
1012
1013 @item case
1014 @rwindex case
1015 @rwindex in
1016 @rwindex esac
1017 The syntax of the @code{case} command is:
1018
1019 @example
1020 case @var{word} in
1021 [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{}
1022 esac
1023 @end example
1024
1025 @code{case} will selectively execute the @var{command-list} corresponding to
1026 the first @var{pattern} that matches @var{word}.
1027 The match is performed according
1028 to the rules described below in @ref{Pattern Matching}.
1029 If the @code{nocasematch} shell option
1030 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1031 is enabled, the match is performed without regard to the case
1032 of alphabetic characters.
1033 The @samp{|} is used to separate multiple patterns, and the @samp{)}
1034 operator terminates a pattern list.
1035 A list of patterns and an associated command-list is known
1036 as a @var{clause}.
1037
1038 Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.
1039 The @var{word} undergoes tilde expansion, parameter expansion, command
1040 substitution, arithmetic expansion, and quote removal
1041 (@pxref{Shell Parameter Expansion})
1042 before matching is attempted.
1043 Each @var{pattern} undergoes tilde expansion, parameter expansion,
1044 command substitution, arithmetic expansion, process substitution, and
1045 quote removal.
1046
1047 There may be an arbitrary number of @code{case} clauses, each terminated
1048 by a @samp{;;}, @samp{;&}, or @samp{;;&}.
1049 The first pattern that matches determines the
1050 command-list that is executed.
1051 It's a common idiom to use @samp{*} as the final pattern to define the
1052 default case, since that pattern will always match.
1053
1054 Here is an example using @code{case} in a script that could be used to
1055 describe one interesting feature of an animal:
1056
1057 @example
1058 echo -n "Enter the name of an animal: "
1059 read ANIMAL
1060 echo -n "The $ANIMAL has "
1061 case $ANIMAL in
1062 horse | dog | cat) echo -n "four";;
1063 man | kangaroo ) echo -n "two";;
1064 *) echo -n "an unknown number of";;
1065 esac
1066 echo " legs."
1067 @end example
1068
1069 @noindent
1070
1071 If the @samp{;;} operator is used, no subsequent matches are attempted after
1072 the first pattern match.
1073 Using @samp{;&} in place of @samp{;;} causes execution to continue with
1074 the @var{command-list} associated with the next clause, if any.
1075 Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns
1076 in the next clause, if any, and execute any associated @var{command-list}
1077 on a successful match,
1078 continuing the case statement execution as if the pattern list had not matched.
1079
1080 The return status is zero if no @var{pattern} is matched. Otherwise, the
1081 return status is the exit status of the @var{command-list} executed.
1082
1083 @item select
1084 @rwindex select
1085
1086 The @code{select} construct allows the easy generation of menus.
1087 It has almost the same syntax as the @code{for} command:
1088
1089 @example
1090 select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
1091 @end example
1092
1093 The list of words following @code{in} is expanded, generating a list
1094 of items. The set of expanded words is printed on the standard
1095 error output stream, each preceded by a number. If the
1096 @samp{in @var{words}} is omitted, the positional parameters are printed,
1097 as if @samp{in "$@@"} had been specified.
1098 The @env{PS3} prompt is then displayed and a line is read from the
1099 standard input.
1100 If the line consists of a number corresponding to one of the displayed
1101 words, then the value of @var{name} is set to that word.
1102 If the line is empty, the words and prompt are displayed again.
1103 If @code{EOF} is read, the @code{select} command completes.
1104 Any other value read causes @var{name} to be set to null.
1105 The line read is saved in the variable @env{REPLY}.
1106
1107 The @var{commands} are executed after each selection until a
1108 @code{break} command is executed, at which
1109 point the @code{select} command completes.
1110
1111 Here is an example that allows the user to pick a filename from the
1112 current directory, and displays the name and index of the file
1113 selected.
1114
1115 @example
1116 select fname in *;
1117 do
1118 echo you picked $fname \($REPLY\)
1119 break;
1120 done
1121 @end example
1122
1123 @item ((@dots{}))
1124 @example
1125 (( @var{expression} ))
1126 @end example
1127
1128 The arithmetic @var{expression} is evaluated according to the rules
1129 described below (@pxref{Shell Arithmetic}).
1130 The @var{expression} is expanded as if it were within double quotes,
1131 but double quote characters in @var{expression} are not special and
1132 are removed.
1133 If the value of the expression is non-zero, the return status is 0;
1134 otherwise the return status is 1.
1135
1136
1137 @item [[@dots{}]]
1138 @rwindex [[
1139 @rwindex ]]
1140 @example
1141 [[ @var{expression} ]]
1142 @end example
1143
1144 Return a status of 0 or 1 depending on the evaluation of
1145 the conditional expression @var{expression}.
1146 Expressions are composed of the primaries described below in
1147 @ref{Bash Conditional Expressions}.
1148 Word splitting and filename expansion are not performed on the words
1149 between the @code{[[} and @code{]]}; tilde expansion, parameter and
1150 variable expansion, arithmetic expansion, command substitution, process
1151 substitution, and quote removal are performed.
1152 Conditional operators such as @samp{-f} must be unquoted to be recognized
1153 as primaries.
1154
1155 When used with @code{[[}, the @samp{<} and @samp{>} operators sort
1156 lexicographically using the current locale.
1157
1158 When the @samp{==} and @samp{!=} operators are used, the string to the
1159 right of the operator is considered a pattern and matched according
1160 to the rules described below in @ref{Pattern Matching},
1161 as if the @code{extglob} shell option were enabled.
1162 The @samp{=} operator is identical to @samp{==}.
1163 If the @code{nocasematch} shell option
1164 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1165 is enabled, the match is performed without regard to the case
1166 of alphabetic characters.
1167 The return value is 0 if the string matches (@samp{==}) or does not
1168 match (@samp{!=}) the pattern, and 1 otherwise.
1169
1170 If you quote any part of the pattern,
1171 using any of the shell's quoting mechanisms,
1172 the quoted portion is matched literally.
1173 This means every character in the quoted portion matches itself,
1174 instead of having any special pattern matching meaning.
1175
1176 An additional binary operator, @samp{=~}, is available, with the same
1177 precedence as @samp{==} and @samp{!=}.
1178 When you use @samp{=~}, the string to the right of the operator is considered
1179 a @sc{posix} extended regular expression pattern and matched accordingly
1180 (using the @sc{posix} @code{regcomp} and @code{regexec} interfaces
1181 usually described in @i{regex}(3)).
1182 The return value is 0 if the string matches the pattern, and 1 if it does not.
1183 If the regular expression is syntactically incorrect, the conditional
1184 expression returns 2.
1185 If the @code{nocasematch} shell option
1186 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1187 is enabled, the match is performed without regard to the case
1188 of alphabetic characters.
1189
1190 You can quote any part of the pattern
1191 to force the quoted portion to be matched literally
1192 instead of as a regular expression (see above).
1193 If the pattern is stored in a shell variable, quoting the variable
1194 expansion forces the entire pattern to be matched literally.
1195
1196 The pattern will match if it matches any part of the string.
1197 If you want to force the pattern to match the entire string,
1198 anchor the pattern using the @samp{^} and @samp{$} regular expression
1199 operators.
1200
1201 For example, the following will match a line
1202 (stored in the shell variable @code{line})
1203 if there is a sequence of characters anywhere in the value consisting of
1204 any number, including zero, of
1205 characters in the @code{space} character class,
1206 immediately followed by zero or one instances of @samp{a},
1207 then a @samp{b}:
1208
1209 @example
1210 [[ $line =~ [[:space:]]*(a)?b ]]
1211 @end example
1212
1213 @noindent
1214 That means values for @code{line} like
1215 @samp{aab}, @samp{ aaaaaab}, @samp{xaby}, and @samp{ ab}
1216 will all match,
1217 as will a line containing a @samp{b} anywhere in its value.
1218
1219 If you want to match a character that's special to the regular expression
1220 grammar (@samp{^$|[]()\.*+?}), it has to be quoted to remove its special
1221 meaning.
1222 This means that in the pattern @samp{xxx.txt}, the @samp{.} matches any
1223 character in the string (its usual regular expression meaning), but in the
1224 pattern @samp{"xxx.txt"}, it can only match a literal @samp{.}.
1225
1226 Likewise, if you want to include a character in your pattern that has a
1227 special meaning to the regular expression grammar, you must make sure it's
1228 not quoted.
1229 If you want to anchor a pattern at the beginning or end of the string,
1230 for instance, you cannot quote the @samp{^} or @samp{$}
1231 characters using any form of shell quoting.
1232
1233 If you want to match @samp{initial string} at the start of a line,
1234 the following will work:
1235 @example
1236 [[ $line =~ ^"initial string" ]]
1237 @end example
1238 @noindent
1239 but this will not:
1240 @example
1241 [[ $line =~ "^initial string" ]]
1242 @end example
1243 @noindent
1244 because in the second example the @samp{^} is quoted and doesn't have its
1245 usual special meaning.
1246
1247 It is sometimes difficult to specify a regular expression properly
1248 without using quotes, or to keep track of the quoting used by regular
1249 expressions while paying attention to
1250 shell quoting and the shell's quote removal.
1251 Storing the regular expression in a shell variable is often a useful
1252 way to avoid problems with quoting characters that are special to the
1253 shell.
1254 For example, the following is equivalent to the pattern used above:
1255
1256 @example
1257 pattern='[[:space:]]*(a)?b'
1258 [[ $line =~ $pattern ]]
1259 @end example
1260
1261 Shell programmers should take special care with backslashes, since
1262 backslashes are used by both the shell and regular expressions to remove
1263 the special meaning from the following character.
1264 This means that after the shell's word expansions complete
1265 (@pxref{Shell Expansions}),
1266 any backslashes remaining in parts of the pattern
1267 that were originally not quoted can remove the
1268 special meaning of pattern characters.
1269 If any part of the pattern is quoted, the shell does its best to ensure that
1270 the regular expression treats those remaining backslashes as literal,
1271 if they appeared in a quoted portion.
1272
1273 The following two sets of commands are @emph{not} equivalent:
1274
1275 @example
1276 pattern='\.'
1277
1278 [[ . =~ $pattern ]]
1279 [[ . =~ \. ]]
1280
1281 [[ . =~ "$pattern" ]]
1282 [[ . =~ '\.' ]]
1283 @end example
1284
1285 @noindent
1286 The first two matches will succeed, but the second two will not, because
1287 in the second two the backslash will be part of the pattern to be matched.
1288 In the first two examples, the pattern passed to the regular expression
1289 parser is @samp{\.}. The backslash removes the special meaning from
1290 @samp{.}, so the literal @samp{.} matches.
1291 In the second two examples, the pattern passed to the regular expression
1292 parser has the backslash quoted (e.g., @samp{\\\.}), which will not match
1293 the string, since it does not contain a backslash.
1294 If the string in the first examples were anything other than @samp{.}, say
1295 @samp{a}, the pattern would not match, because the quoted @samp{.} in the
1296 pattern loses its special meaning of matching any single character.
1297
1298 Bracket expressions in regular expressions can be sources of errors as well,
1299 since characters that are normally special in regular expressions
1300 lose their special meanings between brackets.
1301 However, you can use bracket expressions to match special pattern characters
1302 without quoting them, so they are sometimes useful for this purpose.
1303
1304 Though it might seem like a strange way to write it, the following pattern
1305 will match a @samp{.} in the string:
1306
1307 @example
1308 [[ . =~ [.] ]]
1309 @end example
1310
1311 The shell performs any word expansions before passing the pattern
1312 to the regular expression functions,
1313 so you can assume that the shell's quoting takes precedence.
1314 As noted above, the regular expression parser will interpret any
1315 unquoted backslashes remaining in the pattern after shell expansion
1316 according to its own rules.
1317 The intention is to avoid making shell programmers quote things twice
1318 as much as possible, so shell quoting should be sufficient to quote
1319 special pattern characters where that's necessary.
1320
1321 The array variable @code{BASH_REMATCH} records which parts of the string
1322 matched the pattern.
1323 The element of @code{BASH_REMATCH} with index 0 contains the portion of
1324 the string matching the entire regular expression.
1325 Substrings matched by parenthesized subexpressions within the regular
1326 expression are saved in the remaining @code{BASH_REMATCH} indices.
1327 The element of @code{BASH_REMATCH} with index @var{n} is the portion of the
1328 string matching the @var{n}th parenthesized subexpression.
1329
1330 Expressions may be combined using the following operators, listed
1331 in decreasing order of precedence:
1332
1333 @table @code
1334 @item ( @var{expression} )
1335 Returns the value of @var{expression}.
1336 This may be used to override the normal precedence of operators.
1337
1338 @item ! @var{expression}
1339 True if @var{expression} is false.
1340
1341 @item @var{expression1} && @var{expression2}
1342 True if both @var{expression1} and @var{expression2} are true.
1343
1344 @item @var{expression1} || @var{expression2}
1345 True if either @var{expression1} or @var{expression2} is true.
1346 @end table
1347
1348 @noindent
1349 The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the
1350 value of @var{expression1} is sufficient to determine the return
1351 value of the entire conditional expression.
1352 @end table
1353
1354 @node Command Grouping
1355 @subsubsection Grouping Commands
1356 @cindex commands, grouping
1357
1358 Bash provides two ways to group a list of commands to be executed
1359 as a unit. When commands are grouped, redirections may be applied
1360 to the entire command list. For example, the output of all the
1361 commands in the list may be redirected to a single stream.
1362
1363 @table @code
1364 @item ()
1365 @example
1366 ( @var{list} )
1367 @end example
1368
1369 Placing a list of commands between parentheses causes a subshell
1370 environment to be created (@pxref{Command Execution Environment}), and each
1371 of the commands in @var{list} to be executed in that subshell. Since the
1372 @var{list} is executed in a subshell, variable assignments do not remain in
1373 effect after the subshell completes.
1374
1375 @item @{@}
1376 @rwindex @{
1377 @rwindex @}
1378 @example
1379 @{ @var{list}; @}
1380 @end example
1381
1382 Placing a list of commands between curly braces causes the list to
1383 be executed in the current shell context. No subshell is created.
1384 The semicolon (or newline) following @var{list} is required.
1385 @end table
1386
1387 In addition to the creation of a subshell, there is a subtle difference
1388 between these two constructs due to historical reasons. The braces
1389 are reserved words, so they must be separated from the @var{list}
1390 by @code{blank}s or other shell metacharacters.
1391 The parentheses are operators, and are
1392 recognized as separate tokens by the shell even if they are not separated
1393 from the @var{list} by whitespace.
1394
1395 The exit status of both of these constructs is the exit status of
1396 @var{list}.
1397
1398 @node Coprocesses
1399 @subsection Coprocesses
1400 @cindex coprocess
1401
1402 A @code{coprocess} is a shell command preceded by the @code{coproc}
1403 reserved word.
1404 A coprocess is executed asynchronously in a subshell, as if the command
1405 had been terminated with the @samp{&} control operator, with a two-way pipe
1406 established between the executing shell and the coprocess.
1407
1408 The syntax for a coprocess is:
1409
1410 @example
1411 coproc [@var{NAME}] @var{command} [@var{redirections}]
1412 @end example
1413
1414 @noindent
1415 This creates a coprocess named @var{NAME}.
1416 @var{command} may be either a simple command (@pxref{Simple Commands})
1417 or a compound command (@pxref{Compound Commands}).
1418 @var{NAME} is a shell variable name.
1419 If @var{NAME} is not supplied, the default name is @code{COPROC}.
1420
1421 The recommended form to use for a coprocess is
1422
1423 @example
1424 coproc @var{NAME} @{ @var{command}; @}
1425 @end example
1426
1427 @noindent
1428 This form is recommended because simple commands result in the coprocess
1429 always being named @code{COPROC}, and it is simpler to use and more complete
1430 than the other compound commands.
1431
1432 There are other forms of coprocesses:
1433
1434 @example
1435 coproc @var{NAME} @var{compound-command}
1436 coproc @var{compound-command}
1437 coproc @var{simple-command}
1438 @end example
1439
1440 @noindent
1441 If @var{command} is a compound command, @var{NAME} is optional. The
1442 word following @code{coproc} determines whether that word is interpreted
1443 as a variable name: it is interpreted as @var{NAME} if it is not a
1444 reserved word that introduces a compound command.
1445 If @var{command} is a simple command, @var{NAME} is not allowed; this
1446 is to avoid confusion between @var{NAME} and the first word of the simple
1447 command.
1448
1449 When the coprocess is executed, the shell creates an array variable
1450 (@pxref{Arrays})
1451 named @var{NAME} in the context of the executing shell.
1452 The standard output of @var{command}
1453 is connected via a pipe to a file descriptor in the executing shell,
1454 and that file descriptor is assigned to @var{NAME}[0].
1455 The standard input of @var{command}
1456 is connected via a pipe to a file descriptor in the executing shell,
1457 and that file descriptor is assigned to @var{NAME}[1].
1458 This pipe is established before any redirections specified by the
1459 command (@pxref{Redirections}).
1460 The file descriptors can be utilized as arguments to shell commands
1461 and redirections using standard word expansions.
1462 Other than those created to execute command and process substitutions,
1463 the file descriptors are not available in subshells.
1464
1465 The process ID of the shell spawned to execute the coprocess is
1466 available as the value of the variable @env{@var{NAME}_PID}.
1467 The @code{wait}
1468 builtin command may be used to wait for the coprocess to terminate.
1469
1470 Since the coprocess is created as an asynchronous command,
1471 the @code{coproc} command always returns success.
1472 The return status of a coprocess is the exit status of @var{command}.
1473
1474 @node GNU Parallel
1475 @subsection GNU Parallel
1476
1477 There are ways to run commands in parallel that are not built into Bash.
1478 GNU Parallel is a tool to do just that.
1479
1480 GNU Parallel, as its name suggests, can be used to build and run commands
1481 in parallel. You may run the same command with different arguments, whether
1482 they are filenames, usernames, hostnames, or lines read from files. GNU
1483 Parallel provides shorthand references to many of the most common operations
1484 (input lines, various portions of the input line, different ways to specify
1485 the input source, and so on). Parallel can replace @code{xargs} or feed
1486 commands from its input sources to several different instances of Bash.
1487
1488 For a complete description, refer to the GNU Parallel documentation. A few
1489 examples should provide a brief introduction to its use.
1490
1491 For example, it is easy to replace @code{xargs} to gzip all html files in the
1492 current directory and its subdirectories:
1493 @example
1494 find . -type f -name '*.html' -print | parallel gzip
1495 @end example
1496 @noindent
1497 If you need to protect special characters such as newlines in file names,
1498 use find's @option{-print0} option and parallel's @option{-0} option.
1499
1500 You can use Parallel to move files from the current directory when the
1501 number of files is too large to process with one @code{mv} invocation:
1502 @example
1503 printf '%s\n' * | parallel mv @{@} destdir
1504 @end example
1505
1506 As you can see, the @{@} is replaced with each line read from standard input.
1507 While using @code{ls} will work in most instances, it is not sufficient to
1508 deal with all filenames. @code{printf} is a shell builtin, and therefore is
1509 not subject to the kernel's limit on the number of arguments to a program,
1510 so you can use @samp{*} (but see below about the @code{dotglob} shell option).
1511 If you need to accommodate special characters in filenames, you can use
1512
1513 @example
1514 printf '%s\0' * | parallel -0 mv @{@} destdir
1515 @end example
1516
1517 @noindent
1518 as alluded to above.
1519
1520 This will run as many @code{mv} commands as there are files in the current
1521 directory.
1522 You can emulate a parallel @code{xargs} by adding the @option{-X} option:
1523 @example
1524 printf '%s\0' * | parallel -0 -X mv @{@} destdir
1525 @end example
1526
1527 (You may have to modify the pattern if you have the @code{dotglob} option
1528 enabled.)
1529
1530 GNU Parallel can replace certain common idioms that operate on lines read
1531 from a file (in this case, filenames listed one per line):
1532 @example
1533 while IFS= read -r x; do
1534 do-something1 "$x" "config-$x"
1535 do-something2 < "$x"
1536 done < file | process-output
1537 @end example
1538
1539 @noindent
1540 with a more compact syntax reminiscent of lambdas:
1541 @example
1542 cat list | parallel "do-something1 @{@} config-@{@} ; do-something2 < @{@}" |
1543 process-output
1544 @end example
1545
1546 Parallel provides a built-in mechanism to remove filename extensions, which
1547 lends itself to batch file transformations or renaming:
1548 @example
1549 ls *.gz | parallel -j+0 "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}"
1550 @end example
1551 @noindent
1552 This will recompress all files in the current directory with names ending
1553 in .gz using bzip2, running one job per CPU (-j+0) in parallel.
1554 (We use @code{ls} for brevity here; using @code{find} as above is more
1555 robust in the face of filenames containing unexpected characters.)
1556 Parallel can take arguments from the command line; the above can also be
1557 written as
1558
1559 @example
1560 parallel "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}" ::: *.gz
1561 @end example
1562
1563 If a command generates output, you may want to preserve the input order in
1564 the output. For instance, the following command
1565 @example
1566 @{
1567 echo foss.org.my ;
1568 echo debian.org ;
1569 echo freenetproject.org ;
1570 @} | parallel traceroute
1571 @end example
1572 @noindent
1573 will display as output the traceroute invocation that finishes first.
1574 Adding the @option{-k} option
1575 @example
1576 @{
1577 echo foss.org.my ;
1578 echo debian.org ;
1579 echo freenetproject.org ;
1580 @} | parallel -k traceroute
1581 @end example
1582 @noindent
1583 will ensure that the output of @code{traceroute foss.org.my} is displayed first.
1584
1585 Finally, Parallel can be used to run a sequence of shell commands in parallel,
1586 similar to @samp{cat file | bash}.
1587 It is not uncommon to take a list of filenames, create a series of shell
1588 commands to operate on them, and feed that list of commands to a shell.
1589 Parallel can speed this up. Assuming that @file{file} contains a list of
1590 shell commands, one per line,
1591
1592 @example
1593 parallel -j 10 < file
1594 @end example
1595
1596 @noindent
1597 will evaluate the commands using the shell (since no explicit command is
1598 supplied as an argument), in blocks of ten shell jobs at a time.
1599
1600 @node Shell Functions
1601 @section Shell Functions
1602 @cindex shell function
1603 @cindex functions, shell
1604
1605 Shell functions are a way to group commands for later execution
1606 using a single name for the group. They are executed just like
1607 a "regular" command.
1608 When the name of a shell function is used as a simple command name,
1609 the list of commands associated with that function name is executed.
1610 Shell functions are executed in the current
1611 shell context; no new process is created to interpret them.
1612
1613 Functions are declared using this syntax:
1614 @rwindex function
1615 @example
1616 @var{fname} () @var{compound-command} [ @var{redirections} ]
1617 @end example
1618
1619 or
1620
1621 @example
1622 function @var{fname} [()] @var{compound-command} [ @var{redirections} ]
1623 @end example
1624
1625 This defines a shell function named @var{fname}. The reserved
1626 word @code{function} is optional.
1627 If the @code{function} reserved
1628 word is supplied, the parentheses are optional.
1629 The @dfn{body} of the function is the compound command
1630 @var{compound-command} (@pxref{Compound Commands}).
1631 That command is usually a @var{list} enclosed between @{ and @}, but
1632 may be any compound command listed above.
1633 If the @code{function} reserved word is used, but the
1634 parentheses are not supplied, the braces are recommended.
1635 @var{compound-command} is executed whenever @var{fname} is specified as the
1636 name of a simple command.
1637 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
1638 @var{fname} must be a valid shell name and
1639 may not be the same as one of the special builtins
1640 (@pxref{Special Builtins}).
1641 In default mode, a function name can be any unquoted shell word that does
1642 not contain @samp{$}.
1643 Any redirections (@pxref{Redirections}) associated with the shell function
1644 are performed when the function is executed.
1645 A function definition may be deleted using the @option{-f} option to the
1646 @code{unset} builtin (@pxref{Bourne Shell Builtins}).
1647
1648 The exit status of a function definition is zero unless a syntax error
1649 occurs or a readonly function with the same name already exists.
1650 When executed, the exit status of a function is the exit status of the
1651 last command executed in the body.
1652
1653 Note that for historical reasons, in the most common usage the curly braces
1654 that surround the body of the function must be separated from the body by
1655 @code{blank}s or newlines.
1656 This is because the braces are reserved words and are only recognized
1657 as such when they are separated from the command list
1658 by whitespace or another shell metacharacter.
1659 Also, when using the braces, the @var{list} must be terminated by a semicolon,
1660 a @samp{&}, or a newline.
1661
1662 When a function is executed, the arguments to the
1663 function become the positional parameters
1664 during its execution (@pxref{Positional Parameters}).
1665 The special parameter @samp{#} that expands to the number of
1666 positional parameters is updated to reflect the change.
1667 Special parameter @code{0} is unchanged.
1668 The first element of the @env{FUNCNAME} variable is set to the
1669 name of the function while the function is executing.
1670
1671 All other aspects of the shell execution
1672 environment are identical between a function and its caller
1673 with these exceptions:
1674 the @env{DEBUG} and @env{RETURN} traps
1675 are not inherited unless the function has been given the
1676 @code{trace} attribute using the @code{declare} builtin or
1677 the @code{-o functrace} option has been enabled with
1678 the @code{set} builtin,
1679 (in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps),
1680 and the @env{ERR} trap is not inherited unless the @code{-o errtrace}
1681 shell option has been enabled.
1682 @xref{Bourne Shell Builtins}, for the description of the
1683 @code{trap} builtin.
1684
1685 The @env{FUNCNEST} variable, if set to a numeric value greater
1686 than 0, defines a maximum function nesting level. Function
1687 invocations that exceed the limit cause the entire command to
1688 abort.
1689
1690 If the builtin command @code{return}
1691 is executed in a function, the function completes and
1692 execution resumes with the next command after the function
1693 call.
1694 Any command associated with the @code{RETURN} trap is executed
1695 before execution resumes.
1696 When a function completes, the values of the
1697 positional parameters and the special parameter @samp{#}
1698 are restored to the values they had prior to the function's
1699 execution. If a numeric argument is given to @code{return},
1700 that is the function's return status; otherwise the function's
1701 return status is the exit status of the last command executed
1702 before the @code{return}.
1703
1704 Variables local to the function may be declared with the
1705 @code{local} builtin. These variables are visible only to
1706 the function and the commands it invokes. This is particularly
1707 important when a shell function calls other functions.
1708
1709 Local variables "shadow" variables with the same name declared at
1710 previous scopes. For instance, a local variable declared in a function
1711 hides a global variable of the same name: references and assignments
1712 refer to the local variable, leaving the global variable unmodified.
1713 When the function returns, the global variable is once again visible.
1714
1715 The shell uses @dfn{dynamic scoping} to control a variable's visibility
1716 within functions.
1717 With dynamic scoping, visible variables and their values
1718 are a result of the sequence of function calls that caused execution
1719 to reach the current function.
1720 The value of a variable that a function sees depends
1721 on its value within its caller, if any, whether that caller is
1722 the "global" scope or another shell function.
1723 This is also the value that a local variable
1724 declaration "shadows", and the value that is restored when the function
1725 returns.
1726
1727 For example, if a variable @env{var} is declared as local in function
1728 @code{func1}, and @code{func1} calls another function @code{func2},
1729 references to @env{var} made from within @code{func2} will resolve to the
1730 local variable @env{var} from @code{func1}, shadowing any global variable
1731 named @env{var}.
1732
1733 The following script demonstrates this behavior.
1734 When executed, the script displays
1735
1736 @example
1737 In func2, var = func1 local
1738 @end example
1739
1740 @example
1741 func1()
1742 @{
1743 local var='func1 local'
1744 func2
1745 @}
1746
1747 func2()
1748 @{
1749 echo "In func2, var = $var"
1750 @}
1751
1752 var=global
1753 func1
1754 @end example
1755
1756 The @code{unset} builtin also acts using the same dynamic scope: if a
1757 variable is local to the current scope, @code{unset} will unset it;
1758 otherwise the unset will refer to the variable found in any calling scope
1759 as described above.
1760 If a variable at the current local scope is unset, it will remain so
1761 until it is reset in that scope or until the function returns.
1762 Once the function returns, any instance of the variable at a previous
1763 scope will become visible.
1764 If the unset acts on a variable at a previous scope, any instance of a
1765 variable with that name that had been shadowed will become visible.
1766
1767 Function names and definitions may be listed with the
1768 @option{-f} option to the @code{declare} (@code{typeset})
1769 builtin command (@pxref{Bash Builtins}).
1770 The @option{-F} option to @code{declare} or @code{typeset}
1771 will list the function names only
1772 (and optionally the source file and line number, if the @code{extdebug}
1773 shell option is enabled).
1774 Functions may be exported so that subshells
1775 automatically have them defined with the
1776 @option{-f} option to the @code{export} builtin
1777 (@pxref{Bourne Shell Builtins}).
1778
1779 Functions may be recursive.
1780 The @code{FUNCNEST} variable may be used to limit the depth of the
1781 function call stack and restrict the number of function invocations.
1782 By default, no limit is placed on the number of recursive calls.
1783
1784 @node Shell Parameters
1785 @section Shell Parameters
1786 @cindex parameters
1787 @cindex variable, shell
1788 @cindex shell variable
1789
1790 @menu
1791 * Positional Parameters:: The shell's command-line arguments.
1792 * Special Parameters:: Parameters denoted by special characters.
1793 @end menu
1794
1795 A @dfn{parameter} is an entity that stores values.
1796 It can be a @code{name}, a number, or one of the special characters
1797 listed below.
1798 A @dfn{variable} is a parameter denoted by a @code{name}.
1799 A variable has a @code{value} and zero or more @code{attributes}.
1800 Attributes are assigned using the @code{declare} builtin command
1801 (see the description of the @code{declare} builtin in @ref{Bash Builtins}).
1802
1803 A parameter is set if it has been assigned a value. The null string is
1804 a valid value. Once a variable is set, it may be unset only by using
1805 the @code{unset} builtin command.
1806
1807 A variable may be assigned to by a statement of the form
1808 @example
1809 @var{name}=[@var{value}]
1810 @end example
1811 @noindent
1812 If @var{value}
1813 is not given, the variable is assigned the null string. All
1814 @var{value}s undergo tilde expansion, parameter and variable expansion,
1815 command substitution, arithmetic expansion, and quote
1816 removal (detailed below). If the variable has its @code{integer}
1817 attribute set, then @var{value}
1818 is evaluated as an arithmetic expression even if the @code{$((@dots{}))}
1819 expansion is not used (@pxref{Arithmetic Expansion}).
1820 Word splitting is not performed, with the exception
1821 of @code{"$@@"} as explained below.
1822 Filename expansion is not performed.
1823 Assignment statements may also appear as arguments to the
1824 @code{alias},
1825 @code{declare}, @code{typeset}, @code{export}, @code{readonly},
1826 and @code{local} builtin commands (@dfn{declaration} commands).
1827 When in @sc{posix} mode (@pxref{Bash POSIX Mode}), these builtins may appear
1828 in a command after one or more instances of the @code{command} builtin
1829 and retain these assignment statement properties.
1830
1831 In the context where an assignment statement is assigning a value
1832 to a shell variable or array index (@pxref{Arrays}), the @samp{+=}
1833 operator can be used to
1834 append to or add to the variable's previous value.
1835 This includes arguments to builtin commands such as @code{declare} that
1836 accept assignment statements (declaration commands).
1837 When @samp{+=} is applied to a variable for which the @code{integer} attribute
1838 has been set, @var{value} is evaluated as an arithmetic expression and
1839 added to the variable's current value, which is also evaluated.
1840 When @samp{+=} is applied to an array variable using compound assignment
1841 (@pxref{Arrays}), the
1842 variable's value is not unset (as it is when using @samp{=}), and new
1843 values are appended to the array beginning at one greater than the array's
1844 maximum index (for indexed arrays), or added as additional key-value pairs
1845 in an associative array.
1846 When applied to a string-valued variable, @var{value} is expanded and
1847 appended to the variable's value.
1848
1849 A variable can be assigned the @code{nameref} attribute using the
1850 @option{-n} option to the @code{declare} or @code{local} builtin commands
1851 (@pxref{Bash Builtins})
1852 to create a @dfn{nameref}, or a reference to another variable.
1853 This allows variables to be manipulated indirectly.
1854 Whenever the nameref variable is referenced, assigned to, unset, or has
1855 its attributes modified (other than using or changing the nameref
1856 attribute itself), the
1857 operation is actually performed on the variable specified by the nameref
1858 variable's value.
1859 A nameref is commonly used within shell functions to refer to a variable
1860 whose name is passed as an argument to the function.
1861 For instance, if a variable name is passed to a shell function as its first
1862 argument, running
1863 @example
1864 declare -n ref=$1
1865 @end example
1866 @noindent
1867 inside the function creates a nameref variable @env{ref} whose value is
1868 the variable name passed as the first argument.
1869 References and assignments to @env{ref}, and changes to its attributes,
1870 are treated as references, assignments, and attribute modifications
1871 to the variable whose name was passed as @code{$1}.
1872
1873 If the control variable in a @code{for} loop has the nameref attribute,
1874 the list of words can be a list of shell variables, and a name reference
1875 will be established for each word in the list, in turn, when the loop is
1876 executed.
1877 Array variables cannot be given the nameref attribute.
1878 However, nameref variables can reference array variables and subscripted
1879 array variables.
1880 Namerefs can be unset using the @option{-n} option to the @code{unset} builtin
1881 (@pxref{Bourne Shell Builtins}).
1882 Otherwise, if @code{unset} is executed with the name of a nameref variable
1883 as an argument, the variable referenced by the nameref variable will be unset.
1884
1885 @node Positional Parameters
1886 @subsection Positional Parameters
1887 @cindex parameters, positional
1888
1889 A @dfn{positional parameter} is a parameter denoted by one or more
1890 digits, other than the single digit @code{0}. Positional parameters are
1891 assigned from the shell's arguments when it is invoked,
1892 and may be reassigned using the @code{set} builtin command.
1893 Positional parameter @code{N} may be referenced as @code{$@{N@}}, or
1894 as @code{$N} when @code{N} consists of a single digit.
1895 Positional parameters may not be assigned to with assignment statements.
1896 The @code{set} and @code{shift} builtins are used to set and
1897 unset them (@pxref{Shell Builtin Commands}).
1898 The positional parameters are
1899 temporarily replaced when a shell function is executed
1900 (@pxref{Shell Functions}).
1901
1902 When a positional parameter consisting of more than a single
1903 digit is expanded, it must be enclosed in braces.
1904
1905 @node Special Parameters
1906 @subsection Special Parameters
1907 @cindex parameters, special
1908
1909 The shell treats several parameters specially. These parameters may
1910 only be referenced; assignment to them is not allowed.
1911
1912 @vtable @code
1913
1914 @item *
1915 @vindex $*
1916 ($*) Expands to the positional parameters, starting from one.
1917 When the expansion is not within double quotes, each positional parameter
1918 expands to a separate word.
1919 In contexts where it is performed, those words
1920 are subject to further word splitting and filename expansion.
1921 When the expansion occurs within double quotes, it expands to a single word
1922 with the value of each parameter separated by the first character of the
1923 @env{IFS} special variable. That is, @code{"$*"} is equivalent
1924 to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c}
1925 is the first character of the value of the @code{IFS}
1926 variable.
1927 If @env{IFS} is unset, the parameters are separated by spaces.
1928 If @env{IFS} is null, the parameters are joined without intervening
1929 separators.
1930
1931 @item @@
1932 @vindex $@@
1933 ($@@) Expands to the positional parameters, starting from one.
1934 In contexts where word splitting is performed, this expands each
1935 positional parameter to a separate word; if not within double
1936 quotes, these words are subject to word splitting.
1937 In contexts where word splitting is not performed,
1938 this expands to a single word
1939 with each positional parameter separated by a space.
1940 When the
1941 expansion occurs within double quotes, and word splitting is performed,
1942 each parameter expands to a
1943 separate word. That is, @code{"$@@"} is equivalent to
1944 @code{"$1" "$2" @dots{}}.
1945 If the double-quoted expansion occurs within a word, the expansion of
1946 the first parameter is joined with the beginning part of the original
1947 word, and the expansion of the last parameter is joined with the last
1948 part of the original word.
1949 When there are no positional parameters, @code{"$@@"} and
1950 @code{$@@}
1951 expand to nothing (i.e., they are removed).
1952
1953 @item #
1954 @vindex $#
1955 ($#) Expands to the number of positional parameters in decimal.
1956
1957 @item ?
1958 @vindex $?
1959 ($?) Expands to the exit status of the most recently executed foreground
1960 pipeline.
1961
1962 @item -
1963 @vindex $-
1964 ($-, a hyphen.) Expands to the current option flags as specified upon
1965 invocation, by the @code{set}
1966 builtin command, or those set by the shell itself
1967 (such as the @option{-i} option).
1968
1969 @item $
1970 @vindex $$
1971 ($$) Expands to the process @sc{id} of the shell. In a @code{()} subshell, it
1972 expands to the process @sc{id} of the invoking shell, not the subshell.
1973
1974 @item !
1975 @vindex $!
1976 ($!) Expands to the process @sc{id} of the job most recently placed into the
1977 background, whether executed as an asynchronous command or using
1978 the @code{bg} builtin (@pxref{Job Control Builtins}).
1979
1980 @item 0
1981 @vindex $0
1982 ($0) Expands to the name of the shell or shell script. This is set at
1983 shell initialization. If Bash is invoked with a file of commands
1984 (@pxref{Shell Scripts}), @code{$0} is set to the name of that file.
1985 If Bash is started with the @option{-c} option (@pxref{Invoking Bash}),
1986 then @code{$0} is set to the first argument after the string to be
1987 executed, if one is present. Otherwise, it is set
1988 to the filename used to invoke Bash, as given by argument zero.
1989 @end vtable
1990
1991 @node Shell Expansions
1992 @section Shell Expansions
1993 @cindex expansion
1994
1995 Expansion is performed on the command line after it has been split into
1996 @code{token}s. There are seven kinds of expansion performed:
1997
1998 @itemize @bullet
1999 @item brace expansion
2000 @item tilde expansion
2001 @item parameter and variable expansion
2002 @item command substitution
2003 @item arithmetic expansion
2004 @item word splitting
2005 @item filename expansion
2006 @end itemize
2007
2008 @menu
2009 * Brace Expansion:: Expansion of expressions within braces.
2010 * Tilde Expansion:: Expansion of the ~ character.
2011 * Shell Parameter Expansion:: How Bash expands variables to their values.
2012 * Command Substitution:: Using the output of a command as an argument.
2013 * Arithmetic Expansion:: How to use arithmetic in shell expansions.
2014 * Process Substitution:: A way to write and read to and from a
2015 command.
2016 * Word Splitting:: How the results of expansion are split into separate
2017 arguments.
2018 * Filename Expansion:: A shorthand for specifying filenames matching patterns.
2019 * Quote Removal:: How and when quote characters are removed from
2020 words.
2021 @end menu
2022
2023 The order of expansions is:
2024 brace expansion;
2025 tilde expansion, parameter and variable expansion, arithmetic expansion,
2026 and command substitution (done in a left-to-right fashion);
2027 word splitting;
2028 and filename expansion.
2029
2030 On systems that can support it, there is an additional expansion
2031 available: @dfn{process substitution}.
2032 This is performed at the
2033 same time as tilde, parameter, variable, and arithmetic expansion and
2034 command substitution.
2035
2036 After these expansions are performed, quote characters present in the
2037 original word are removed unless they have been quoted themselves
2038 (@dfn{quote removal}).
2039
2040 Only brace expansion, word splitting, and filename expansion
2041 can increase the number of words of the expansion; other expansions
2042 expand a single word to a single word.
2043 The only exceptions to this are the expansions of
2044 @code{"$@@"} and @code{$*} (@pxref{Special Parameters}), and
2045 @code{"$@{@var{name}[@@]@}"} and @code{$@{@var{name}[*]@}}
2046 (@pxref{Arrays}).
2047
2048 After all expansions, @code{quote removal} (@pxref{Quote Removal})
2049 is performed.
2050
2051 @node Brace Expansion
2052 @subsection Brace Expansion
2053 @cindex brace expansion
2054 @cindex expansion, brace
2055
2056 Brace expansion is a mechanism by which arbitrary strings may be generated.
2057 This mechanism is similar to
2058 @dfn{filename expansion} (@pxref{Filename Expansion}),
2059 but the filenames generated need not exist.
2060 Patterns to be brace expanded take the form of an optional @var{preamble},
2061 followed by either a series of comma-separated strings or a sequence expression
2062 between a pair of braces,
2063 followed by an optional @var{postscript}.
2064 The preamble is prefixed to each string contained within the braces, and
2065 the postscript is then appended to each resulting string, expanding left
2066 to right.
2067
2068 Brace expansions may be nested.
2069 The results of each expanded string are not sorted; left to right order
2070 is preserved.
2071 For example,
2072 @example
2073 bash$ echo a@{d,c,b@}e
2074 ade ace abe
2075 @end example
2076
2077 A sequence expression takes the form @code{@{@var{x}..@var{y}[..@var{incr}]@}},
2078 where @var{x} and @var{y} are either integers or letters,
2079 and @var{incr}, an optional increment, is an integer.
2080 When integers are supplied, the expression expands to each number between
2081 @var{x} and @var{y}, inclusive.
2082 Supplied integers may be prefixed with @samp{0} to force each term to have the
2083 same width.
2084 When either @var{x} or @var{y} begins with a zero, the shell
2085 attempts to force all generated terms to contain the same number of digits,
2086 zero-padding where necessary.
2087 When letters are supplied, the expression expands to each character
2088 lexicographically between @var{x} and @var{y}, inclusive,
2089 using the default C locale.
2090 Note that both @var{x} and @var{y} must be of the same type
2091 (integer or letter).
2092 When the increment is supplied, it is used as the difference between
2093 each term. The default increment is 1 or -1 as appropriate.
2094
2095 Brace expansion is performed before any other expansions,
2096 and any characters special to other expansions are preserved
2097 in the result. It is strictly textual. Bash
2098 does not apply any syntactic interpretation to the context of the
2099 expansion or the text between the braces.
2100
2101 A correctly-formed brace expansion must contain unquoted opening
2102 and closing braces, and at least one unquoted comma or a valid
2103 sequence expression.
2104 Any incorrectly formed brace expansion is left unchanged.
2105
2106 A @{ or @samp{,} may be quoted with a backslash to prevent its
2107 being considered part of a brace expression.
2108 To avoid conflicts with parameter expansion, the string @samp{$@{}
2109 is not considered eligible for brace expansion,
2110 and inhibits brace expansion until the closing @samp{@}}.
2111
2112 This construct is typically used as shorthand when the common
2113 prefix of the strings to be generated is longer than in the
2114 above example:
2115 @example
2116 mkdir /usr/local/src/bash/@{old,new,dist,bugs@}
2117 @end example
2118 or
2119 @example
2120 chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@}
2121 @end example
2122
2123 @node Tilde Expansion
2124 @subsection Tilde Expansion
2125 @cindex tilde expansion
2126 @cindex expansion, tilde
2127
2128 If a word begins with an unquoted tilde character (@samp{~}), all of the
2129 characters up to the first unquoted slash (or all characters,
2130 if there is no unquoted slash) are considered a @dfn{tilde-prefix}.
2131 If none of the characters in the tilde-prefix are quoted, the
2132 characters in the tilde-prefix following the tilde are treated as a
2133 possible @dfn{login name}.
2134 If this login name is the null string, the tilde is replaced with the
2135 value of the @env{HOME} shell variable.
2136 If @env{HOME} is unset, the home directory of the user executing the
2137 shell is substituted instead.
2138 Otherwise, the tilde-prefix is replaced with the home directory
2139 associated with the specified login name.
2140
2141 If the tilde-prefix is @samp{~+}, the value of
2142 the shell variable @env{PWD} replaces the tilde-prefix.
2143 If the tilde-prefix is @samp{~-}, the value of the shell variable
2144 @env{OLDPWD}, if it is set, is substituted.
2145
2146 If the characters following the tilde in the tilde-prefix consist of a
2147 number @var{N}, optionally prefixed by a @samp{+} or a @samp{-},
2148 the tilde-prefix is replaced with the
2149 corresponding element from the directory stack, as it would be displayed
2150 by the @code{dirs} builtin invoked with the characters following tilde
2151 in the tilde-prefix as an argument (@pxref{The Directory Stack}).
2152 If the tilde-prefix, sans the tilde, consists of a number without a
2153 leading @samp{+} or @samp{-}, @samp{+} is assumed.
2154
2155 If the login name is invalid, or the tilde expansion fails, the word is
2156 left unchanged.
2157
2158 Each variable assignment is checked for unquoted tilde-prefixes immediately
2159 following a @samp{:} or the first @samp{=}.
2160 In these cases, tilde expansion is also performed.
2161 Consequently, one may use filenames with tildes in assignments to
2162 @env{PATH}, @env{MAILPATH}, and @env{CDPATH},
2163 and the shell assigns the expanded value.
2164
2165 The following table shows how Bash treats unquoted tilde-prefixes:
2166
2167 @table @code
2168 @item ~
2169 The value of @code{$HOME}
2170 @item ~/foo
2171 @file{$HOME/foo}
2172
2173 @item ~fred/foo
2174 The subdirectory @code{foo} of the home directory of the user
2175 @code{fred}
2176
2177 @item ~+/foo
2178 @file{$PWD/foo}
2179
2180 @item ~-/foo
2181 @file{$@{OLDPWD-'~-'@}/foo}
2182
2183 @item ~@var{N}
2184 The string that would be displayed by @samp{dirs +@var{N}}
2185
2186 @item ~+@var{N}
2187 The string that would be displayed by @samp{dirs +@var{N}}
2188
2189 @item ~-@var{N}
2190 The string that would be displayed by @samp{dirs -@var{N}}
2191 @end table
2192
2193 Bash also performs tilde expansion on words satisfying the conditions of
2194 variable assignments (@pxref{Shell Parameters})
2195 when they appear as arguments to simple commands.
2196 Bash does not do this, except for the declaration commands listed
2197 above, when in @sc{posix} mode.
2198
2199 @node Shell Parameter Expansion
2200 @subsection Shell Parameter Expansion
2201 @cindex parameter expansion
2202 @cindex expansion, parameter
2203
2204 The @samp{$} character introduces parameter expansion,
2205 command substitution, or arithmetic expansion. The parameter name
2206 or symbol to be expanded may be enclosed in braces, which
2207 are optional but serve to protect the variable to be expanded from
2208 characters immediately following it which could be
2209 interpreted as part of the name.
2210
2211 When braces are used, the matching ending brace is the first @samp{@}}
2212 not escaped by a backslash or within a quoted string, and not within an
2213 embedded arithmetic expansion, command substitution, or parameter
2214 expansion.
2215
2216 The basic form of parameter expansion is $@{@var{parameter}@}.
2217 The value of @var{parameter} is substituted.
2218 The @var{parameter} is a shell parameter as described above
2219 (@pxref{Shell Parameters}) or an array reference (@pxref{Arrays}).
2220 The braces are required when @var{parameter}
2221 is a positional parameter with more than one digit,
2222 or when @var{parameter} is followed by a character that is not to be
2223 interpreted as part of its name.
2224
2225 If the first character of @var{parameter} is an exclamation point (!),
2226 and @var{parameter} is not a nameref,
2227 it introduces a level of indirection.
2228 Bash uses the value formed by expanding the rest of
2229 @var{parameter} as the new @var{parameter}; this is then
2230 expanded and that value is used in the rest of the expansion, rather
2231 than the expansion of the original @var{parameter}.
2232 This is known as @code{indirect expansion}.
2233 The value is subject to tilde expansion,
2234 parameter expansion, command substitution, and arithmetic expansion.
2235 If @var{parameter} is a nameref, this expands to the name of the
2236 variable referenced by @var{parameter} instead of performing the
2237 complete indirect expansion.
2238 The exceptions to this are the expansions of $@{!@var{prefix}*@}
2239 and $@{!@var{name}[@@]@}
2240 described below.
2241 The exclamation point must immediately follow the left brace in order to
2242 introduce indirection.
2243
2244 In each of the cases below, @var{word} is subject to tilde expansion,
2245 parameter expansion, command substitution, and arithmetic expansion.
2246
2247 When not performing substring expansion, using the form described
2248 below (e.g., @samp{:-}), Bash tests for a parameter that is unset or null.
2249 Omitting the colon results in a test only for a parameter that is unset.
2250 Put another way, if the colon is included,
2251 the operator tests for both @var{parameter}'s existence and that its value
2252 is not null; if the colon is omitted, the operator tests only for existence.
2253
2254 @table @code
2255
2256 @item $@{@var{parameter}:@minus{}@var{word}@}
2257 If @var{parameter} is unset or null, the expansion of
2258 @var{word} is substituted. Otherwise, the value of
2259 @var{parameter} is substituted.
2260
2261 @item $@{@var{parameter}:=@var{word}@}
2262 If @var{parameter}
2263 is unset or null, the expansion of @var{word}
2264 is assigned to @var{parameter}.
2265 The value of @var{parameter} is then substituted.
2266 Positional parameters and special parameters may not be assigned to
2267 in this way.
2268
2269 @item $@{@var{parameter}:?@var{word}@}
2270 If @var{parameter}
2271 is null or unset, the expansion of @var{word} (or a message
2272 to that effect if @var{word}
2273 is not present) is written to the standard error and the shell, if it
2274 is not interactive, exits. Otherwise, the value of @var{parameter} is
2275 substituted.
2276
2277 @item $@{@var{parameter}:+@var{word}@}
2278 If @var{parameter}
2279 is null or unset, nothing is substituted, otherwise the expansion of
2280 @var{word} is substituted.
2281
2282 @item $@{@var{parameter}:@var{offset}@}
2283 @itemx $@{@var{parameter}:@var{offset}:@var{length}@}
2284 This is referred to as Substring Expansion.
2285 It expands to up to @var{length} characters of the value of @var{parameter}
2286 starting at the character specified by @var{offset}.
2287 If @var{parameter} is @samp{@@}, an indexed array subscripted by
2288 @samp{@@} or @samp{*}, or an associative array name, the results differ as
2289 described below.
2290 If @var{length} is omitted, it expands to the substring of the value of
2291 @var{parameter} starting at the character specified by @var{offset}
2292 and extending to the end of the value.
2293 @var{length} and @var{offset} are arithmetic expressions
2294 (@pxref{Shell Arithmetic}).
2295
2296 If @var{offset} evaluates to a number less than zero, the value
2297 is used as an offset in characters
2298 from the end of the value of @var{parameter}.
2299 If @var{length} evaluates to a number less than zero,
2300 it is interpreted as an offset in characters
2301 from the end of the value of @var{parameter} rather than
2302 a number of characters, and the expansion is the characters between
2303 @var{offset} and that result.
2304 Note that a negative offset must be separated from the colon by at least
2305 one space to avoid being confused with the @samp{:-} expansion.
2306
2307 Here are some examples illustrating substring expansion on parameters and
2308 subscripted arrays:
2309
2310 @verbatim
2311 $ string=01234567890abcdefgh
2312 $ echo ${string:7}
2313 7890abcdefgh
2314 $ echo ${string:7:0}
2315
2316 $ echo ${string:7:2}
2317 78
2318 $ echo ${string:7:-2}
2319 7890abcdef
2320 $ echo ${string: -7}
2321 bcdefgh
2322 $ echo ${string: -7:0}
2323
2324 $ echo ${string: -7:2}
2325 bc
2326 $ echo ${string: -7:-2}
2327 bcdef
2328 $ set -- 01234567890abcdefgh
2329 $ echo ${1:7}
2330 7890abcdefgh
2331 $ echo ${1:7:0}
2332
2333 $ echo ${1:7:2}
2334 78
2335 $ echo ${1:7:-2}
2336 7890abcdef
2337 $ echo ${1: -7}
2338 bcdefgh
2339 $ echo ${1: -7:0}
2340
2341 $ echo ${1: -7:2}
2342 bc
2343 $ echo ${1: -7:-2}
2344 bcdef
2345 $ array[0]=01234567890abcdefgh
2346 $ echo ${array[0]:7}
2347 7890abcdefgh
2348 $ echo ${array[0]:7:0}
2349
2350 $ echo ${array[0]:7:2}
2351 78
2352 $ echo ${array[0]:7:-2}
2353 7890abcdef
2354 $ echo ${array[0]: -7}
2355 bcdefgh
2356 $ echo ${array[0]: -7:0}
2357
2358 $ echo ${array[0]: -7:2}
2359 bc
2360 $ echo ${array[0]: -7:-2}
2361 bcdef
2362 @end verbatim
2363
2364 If @var{parameter} is @samp{@@}, the result is @var{length} positional
2365 parameters beginning at @var{offset}.
2366 A negative @var{offset} is taken relative to one greater than the greatest
2367 positional parameter, so an offset of -1 evaluates to the last positional
2368 parameter.
2369 It is an expansion error if @var{length} evaluates to a number less than zero.
2370
2371 The following examples illustrate substring expansion using positional
2372 parameters:
2373
2374 @verbatim
2375 $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2376 $ echo ${@:7}
2377 7 8 9 0 a b c d e f g h
2378 $ echo ${@:7:0}
2379
2380 $ echo ${@:7:2}
2381 7 8
2382 $ echo ${@:7:-2}
2383 bash: -2: substring expression < 0
2384 $ echo ${@: -7:2}
2385 b c
2386 $ echo ${@:0}
2387 ./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2388 $ echo ${@:0:2}
2389 ./bash 1
2390 $ echo ${@: -7:0}
2391
2392 @end verbatim
2393
2394 If @var{parameter} is an indexed array name subscripted
2395 by @samp{@@} or @samp{*}, the result is the @var{length}
2396 members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}.
2397 A negative @var{offset} is taken relative to one greater than the maximum
2398 index of the specified array.
2399 It is an expansion error if @var{length} evaluates to a number less than zero.
2400
2401 These examples show how you can use substring expansion with indexed
2402 arrays:
2403
2404 @verbatim
2405 $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)
2406 $ echo ${array[@]:7}
2407 7 8 9 0 a b c d e f g h
2408 $ echo ${array[@]:7:2}
2409 7 8
2410 $ echo ${array[@]: -7:2}
2411 b c
2412 $ echo ${array[@]: -7:-2}
2413 bash: -2: substring expression < 0
2414 $ echo ${array[@]:0}
2415 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2416 $ echo ${array[@]:0:2}
2417 0 1
2418 $ echo ${array[@]: -7:0}
2419
2420 @end verbatim
2421
2422 Substring expansion applied to an associative array produces undefined
2423 results.
2424
2425 Substring indexing is zero-based unless the positional parameters
2426 are used, in which case the indexing starts at 1 by default.
2427 If @var{offset} is 0, and the positional parameters are used, @code{$0} is
2428 prefixed to the list.
2429
2430 @item $@{!@var{prefix}*@}
2431 @itemx $@{!@var{prefix}@@@}
2432 Expands to the names of variables whose names begin with @var{prefix},
2433 separated by the first character of the @env{IFS} special variable.
2434 When @samp{@@} is used and the expansion appears within double quotes, each
2435 variable name expands to a separate word.
2436
2437 @item $@{!@var{name}[@@]@}
2438 @itemx $@{!@var{name}[*]@}
2439 If @var{name} is an array variable, expands to the list of array indices
2440 (keys) assigned in @var{name}.
2441 If @var{name} is not an array, expands to 0 if @var{name} is set and null
2442 otherwise.
2443 When @samp{@@} is used and the expansion appears within double quotes, each
2444 key expands to a separate word.
2445
2446 @item $@{#@var{parameter}@}
2447 The length in characters of the expanded value of @var{parameter} is
2448 substituted.
2449 If @var{parameter} is @samp{*} or @samp{@@}, the value substituted
2450 is the number of positional parameters.
2451 If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@},
2452 the value substituted is the number of elements in the array.
2453 If @var{parameter}
2454 is an indexed array name subscripted by a negative number, that number is
2455 interpreted as relative to one greater than the maximum index of
2456 @var{parameter}, so negative indices count back from the end of the
2457 array, and an index of -1 references the last element.
2458
2459 @item $@{@var{parameter}#@var{word}@}
2460 @itemx $@{@var{parameter}##@var{word}@}
2461 The @var{word}
2462 is expanded to produce a pattern and matched according to the rules
2463 described below (@pxref{Pattern Matching}). If the pattern matches
2464 the beginning of the expanded value of @var{parameter},
2465 then the result of the expansion is the expanded value of @var{parameter}
2466 with the shortest matching pattern (the @samp{#} case) or the
2467 longest matching pattern (the @samp{##} case) deleted.
2468 If @var{parameter} is @samp{@@} or @samp{*},
2469 the pattern removal operation is applied to each positional
2470 parameter in turn, and the expansion is the resultant list.
2471 If @var{parameter} is an array variable subscripted with
2472 @samp{@@} or @samp{*},
2473 the pattern removal operation is applied to each member of the
2474 array in turn, and the expansion is the resultant list.
2475
2476 @item $@{@var{parameter}%@var{word}@}
2477 @itemx $@{@var{parameter}%%@var{word}@}
2478 The @var{word}
2479 is expanded to produce a pattern and matched according to the rules
2480 described below (@pxref{Pattern Matching}).
2481 If the pattern matches a trailing portion of the expanded value of
2482 @var{parameter}, then the result of the expansion is the value of
2483 @var{parameter} with the shortest matching pattern (the @samp{%} case)
2484 or the longest matching pattern (the @samp{%%} case) deleted.
2485 If @var{parameter} is @samp{@@} or @samp{*},
2486 the pattern removal operation is applied to each positional
2487 parameter in turn, and the expansion is the resultant list.
2488 If @var{parameter}
2489 is an array variable subscripted with @samp{@@} or @samp{*},
2490 the pattern removal operation is applied to each member of the
2491 array in turn, and the expansion is the resultant list.
2492
2493 @item $@{@var{parameter}/@var{pattern}/@var{string}@}
2494
2495 The @var{pattern} is expanded to produce a pattern just as in
2496 filename expansion.
2497 @var{Parameter} is expanded and the longest match of @var{pattern}
2498 against its value is replaced with @var{string}.
2499 The match is performed according to the rules described below
2500 (@pxref{Pattern Matching}).
2501 If @var{pattern} begins with @samp{/}, all matches of @var{pattern} are
2502 replaced with @var{string}. Normally only the first match is replaced.
2503 If @var{pattern} begins with @samp{#}, it must match at the beginning
2504 of the expanded value of @var{parameter}.
2505 If @var{pattern} begins with @samp{%}, it must match at the end
2506 of the expanded value of @var{parameter}.
2507 If @var{string} is null, matches of @var{pattern} are deleted
2508 and the @samp{/} following @var{pattern} may be omitted.
2509 If the @code{nocasematch} shell option
2510 (see the description of @code{shopt} in @ref{The Shopt Builtin})
2511 is enabled, the match is performed without regard to the case
2512 of alphabetic characters.
2513 If @var{parameter} is @samp{@@} or @samp{*},
2514 the substitution operation is applied to each positional
2515 parameter in turn, and the expansion is the resultant list.
2516 If @var{parameter}
2517 is an array variable subscripted with @samp{@@} or @samp{*},
2518 the substitution operation is applied to each member of the
2519 array in turn, and the expansion is the resultant list.
2520
2521 @item $@{@var{parameter}^@var{pattern}@}
2522 @itemx $@{@var{parameter}^^@var{pattern}@}
2523 @itemx $@{@var{parameter},@var{pattern}@}
2524 @itemx $@{@var{parameter},,@var{pattern}@}
2525 This expansion modifies the case of alphabetic characters in @var{parameter}.
2526 The @var{pattern} is expanded to produce a pattern just as in
2527 filename expansion.
2528 Each character in the expanded value of @var{parameter} is tested against
2529 @var{pattern}, and, if it matches the pattern, its case is converted.
2530 The pattern should not attempt to match more than one character.
2531 The @samp{^} operator converts lowercase letters matching @var{pattern}
2532 to uppercase; the @samp{,} operator converts matching uppercase letters
2533 to lowercase.
2534 The @samp{^^} and @samp{,,} expansions convert each matched character in the
2535 expanded value; the @samp{^} and @samp{,} expansions match and convert only
2536 the first character in the expanded value.
2537 If @var{pattern} is omitted, it is treated like a @samp{?}, which matches
2538 every character.
2539 If @var{parameter} is @samp{@@} or @samp{*},
2540 the case modification operation is applied to each positional
2541 parameter in turn, and the expansion is the resultant list.
2542 If @var{parameter}
2543 is an array variable subscripted with @samp{@@} or @samp{*},
2544 the case modification operation is applied to each member of the
2545 array in turn, and the expansion is the resultant list.
2546
2547 @item $@{@var{parameter}@@@var{operator}@}
2548 The expansion is either a transformation of the value of @var{parameter}
2549 or information about @var{parameter} itself, depending on the value of
2550 @var{operator}. Each @var{operator} is a single letter:
2551
2552 @table @code
2553 @item U
2554 The expansion is a string that is the value of @var{parameter} with lowercase
2555 alphabetic characters converted to uppercase.
2556 @item u
2557 The expansion is a string that is the value of @var{parameter} with the first
2558 character converted to uppercase, if it is alphabetic.
2559 @item L
2560 The expansion is a string that is the value of @var{parameter} with uppercase
2561 alphabetic characters converted to lowercase.
2562 @item Q
2563 The expansion is a string that is the value of @var{parameter} quoted in a
2564 format that can be reused as input.
2565 @item E
2566 The expansion is a string that is the value of @var{parameter} with backslash
2567 escape sequences expanded as with the @code{$'@dots{}'} quoting mechanism.
2568 @item P
2569 The expansion is a string that is the result of expanding the value of
2570 @var{parameter} as if it were a prompt string (@pxref{Controlling the Prompt}).
2571 @item A
2572 The expansion is a string in the form of
2573 an assignment statement or @code{declare} command that, if
2574 evaluated, will recreate @var{parameter} with its attributes and value.
2575 @item K
2576 Produces a possibly-quoted version of the value of @var{parameter},
2577 except that it prints the values of
2578 indexed and associative arrays as a sequence of quoted key-value pairs
2579 (@pxref{Arrays}).
2580 @item a
2581 The expansion is a string consisting of flag values representing
2582 @var{parameter}'s attributes.
2583 @end table
2584
2585 If @var{parameter} is @samp{@@} or @samp{*},
2586 the operation is applied to each positional
2587 parameter in turn, and the expansion is the resultant list.
2588 If @var{parameter}
2589 is an array variable subscripted with @samp{@@} or @samp{*},
2590 the operation is applied to each member of the
2591 array in turn, and the expansion is the resultant list.
2592
2593 The result of the expansion is subject to word splitting and filename
2594 expansion as described below.
2595 @end table
2596
2597 @node Command Substitution
2598 @subsection Command Substitution
2599 @cindex command substitution
2600
2601 Command substitution allows the output of a command to replace
2602 the command itself.
2603 Command substitution occurs when a command is enclosed as follows:
2604 @example
2605 $(@var{command})
2606 @end example
2607 @noindent
2608 or
2609 @example
2610 `@var{command}`
2611 @end example
2612
2613 @noindent
2614 Bash performs the expansion by executing @var{command} in a subshell environment
2615 and replacing the command substitution with the standard output of the
2616 command, with any trailing newlines deleted.
2617 Embedded newlines are not deleted, but they may be removed during
2618 word splitting.
2619 The command substitution @code{$(cat @var{file})} can be
2620 replaced by the equivalent but faster @code{$(< @var{file})}.
2621
2622 When the old-style backquote form of substitution is used,
2623 backslash retains its literal meaning except when followed by
2624 @samp{$}, @samp{`}, or @samp{\}.
2625 The first backquote not preceded by a backslash terminates the
2626 command substitution.
2627 When using the @code{$(@var{command})} form, all characters between
2628 the parentheses make up the command; none are treated specially.
2629
2630 Command substitutions may be nested. To nest when using the backquoted
2631 form, escape the inner backquotes with backslashes.
2632
2633 If the substitution appears within double quotes, word splitting and
2634 filename expansion are not performed on the results.
2635
2636 @node Arithmetic Expansion
2637 @subsection Arithmetic Expansion
2638 @cindex expansion, arithmetic
2639 @cindex arithmetic expansion
2640
2641 Arithmetic expansion allows the evaluation of an arithmetic expression
2642 and the substitution of the result. The format for arithmetic expansion is:
2643
2644 @example
2645 $(( @var{expression} ))
2646 @end example
2647
2648 The @var{expression} is treated as if it were within double quotes, but
2649 a double quote inside the parentheses is not treated specially.
2650 All tokens in the expression undergo parameter and variable expansion,
2651 command substitution, and quote removal.
2652 The result is treated as the arithmetic expression to be evaluated.
2653 Arithmetic expansions may be nested.
2654
2655 The evaluation is performed according to the rules listed below
2656 (@pxref{Shell Arithmetic}).
2657 If the expression is invalid, Bash prints a message indicating
2658 failure to the standard error and no substitution occurs.
2659
2660 @node Process Substitution
2661 @subsection Process Substitution
2662 @cindex process substitution
2663
2664 Process substitution allows a process's input or output to be
2665 referred to using a filename.
2666 It takes the form of
2667 @example
2668 <(@var{list})
2669 @end example
2670 @noindent
2671 or
2672 @example
2673 >(@var{list})
2674 @end example
2675 @noindent
2676 The process @var{list} is run asynchronously, and its input or output
2677 appears as a filename.
2678 This filename is
2679 passed as an argument to the current command as the result of the
2680 expansion.
2681 If the @code{>(@var{list})} form is used, writing to
2682 the file will provide input for @var{list}. If the
2683 @code{<(@var{list})} form is used, the file passed as an
2684 argument should be read to obtain the output of @var{list}.
2685 Note that no space may appear between the @code{<} or @code{>}
2686 and the left parenthesis, otherwise the construct would be interpreted
2687 as a redirection.
2688 Process substitution is supported on systems that support named
2689 pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files.
2690
2691 When available, process substitution is performed simultaneously with
2692 parameter and variable expansion, command substitution, and arithmetic
2693 expansion.
2694
2695 @node Word Splitting
2696 @subsection Word Splitting
2697 @cindex word splitting
2698
2699 The shell scans the results of parameter expansion, command substitution,
2700 and arithmetic expansion that did not occur within double quotes for
2701 word splitting.
2702
2703 The shell treats each character of @env{$IFS} as a delimiter, and splits
2704 the results of the other expansions into words using these characters
2705 as field terminators.
2706 If @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>},
2707 the default, then sequences of
2708 @code{ <space>}, @code{<tab>}, and @code{<newline>}
2709 at the beginning and end of the results of the previous
2710 expansions are ignored, and any sequence of @env{IFS}
2711 characters not at the beginning or end serves to delimit words.
2712 If @env{IFS} has a value other than the default, then sequences of
2713 the whitespace characters @code{space}, @code{tab}, and @code{newline}
2714 are ignored at the beginning and end of the
2715 word, as long as the whitespace character is in the
2716 value of @env{IFS} (an @env{IFS} whitespace character).
2717 Any character in @env{IFS} that is not @env{IFS}
2718 whitespace, along with any adjacent @env{IFS}
2719 whitespace characters, delimits a field. A sequence of @env{IFS}
2720 whitespace characters is also treated as a delimiter.
2721 If the value of @env{IFS} is null, no word splitting occurs.
2722
2723 Explicit null arguments (@code{""} or @code{''}) are retained
2724 and passed to commands as empty strings.
2725 Unquoted implicit null arguments, resulting from the expansion of
2726 parameters that have no values, are removed.
2727 If a parameter with no value is expanded within double quotes, a
2728 null argument results and is retained
2729 and passed to a command as an empty string.
2730 When a quoted null argument appears as part of a word whose expansion is
2731 non-null, the null argument is removed.
2732 That is, the word
2733 @code{-d''} becomes @code{-d} after word splitting and
2734 null argument removal.
2735
2736 Note that if no expansion occurs, no splitting
2737 is performed.
2738
2739 @node Filename Expansion
2740 @subsection Filename Expansion
2741 @menu
2742 * Pattern Matching:: How the shell matches patterns.
2743 @end menu
2744 @cindex expansion, filename
2745 @cindex expansion, pathname
2746 @cindex filename expansion
2747 @cindex pathname expansion
2748
2749 After word splitting, unless the @option{-f} option has been set
2750 (@pxref{The Set Builtin}), Bash scans each word for the characters
2751 @samp{*}, @samp{?}, and @samp{[}.
2752 If one of these characters appears, and is not quoted, then the word is
2753 regarded as a @var{pattern},
2754 and replaced with an alphabetically sorted list of
2755 filenames matching the pattern (@pxref{Pattern Matching}).
2756 If no matching filenames are found,
2757 and the shell option @code{nullglob} is disabled, the word is left
2758 unchanged.
2759 If the @code{nullglob} option is set, and no matches are found, the word
2760 is removed.
2761 If the @code{failglob} shell option is set, and no matches are found,
2762 an error message is printed and the command is not executed.
2763 If the shell option @code{nocaseglob} is enabled, the match is performed
2764 without regard to the case of alphabetic characters.
2765
2766 When a pattern is used for filename expansion, the character @samp{.}
2767 at the start of a filename or immediately following a slash
2768 must be matched explicitly, unless the shell option @code{dotglob} is set.
2769 The filenames @samp{.} and @samp{..} must always be matched explicitly,
2770 even if @code{dotglob} is set.
2771 In other cases, the @samp{.} character is not treated specially.
2772
2773 When matching a filename, the slash character must always be
2774 matched explicitly by a slash in the pattern, but in other matching
2775 contexts it can be matched by a special pattern character as described
2776 below (@pxref{Pattern Matching}).
2777
2778 See the description of @code{shopt} in @ref{The Shopt Builtin},
2779 for a description of the @code{nocaseglob}, @code{nullglob},
2780 @code{failglob}, and @code{dotglob} options.
2781
2782 The @env{GLOBIGNORE}
2783 shell variable may be used to restrict the set of file names matching a
2784 pattern. If @env{GLOBIGNORE}
2785 is set, each matching file name that also matches one of the patterns in
2786 @env{GLOBIGNORE} is removed from the list of matches.
2787 If the @code{nocaseglob} option is set, the matching against the patterns in
2788 @env{GLOBIGNORE} is performed without regard to case.
2789 The filenames
2790 @file{.} and @file{..}
2791 are always ignored when @env{GLOBIGNORE}
2792 is set and not null.
2793 However, setting @env{GLOBIGNORE} to a non-null value has the effect of
2794 enabling the @code{dotglob}
2795 shell option, so all other filenames beginning with a
2796 @samp{.} will match.
2797 To get the old behavior of ignoring filenames beginning with a
2798 @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}.
2799 The @code{dotglob} option is disabled when @env{GLOBIGNORE}
2800 is unset.
2801
2802 @node Pattern Matching
2803 @subsubsection Pattern Matching
2804 @cindex pattern matching
2805 @cindex matching, pattern
2806
2807 Any character that appears in a pattern, other than the special pattern
2808 characters described below, matches itself.
2809 The @sc{nul} character may not occur in a pattern.
2810 A backslash escapes the following character; the
2811 escaping backslash is discarded when matching.
2812 The special pattern characters must be quoted if they are to be matched
2813 literally.
2814
2815 The special pattern characters have the following meanings:
2816 @table @code
2817 @item *
2818 Matches any string, including the null string.
2819 When the @code{globstar} shell option is enabled, and @samp{*} is used in
2820 a filename expansion context, two adjacent @samp{*}s used as a single
2821 pattern will match all files and zero or more directories and
2822 subdirectories.
2823 If followed by a @samp{/}, two adjacent @samp{*}s will match only
2824 directories and subdirectories.
2825 @item ?
2826 Matches any single character.
2827 @item [@dots{}]
2828 Matches any one of the enclosed characters. A pair of characters
2829 separated by a hyphen denotes a @var{range expression};
2830 any character that falls between those two characters, inclusive,
2831 using the current locale's collating sequence and character set,
2832 is matched. If the first character following the
2833 @samp{[} is a @samp{!} or a @samp{^}
2834 then any character not enclosed is matched. A @samp{@minus{}}
2835 may be matched by including it as the first or last character
2836 in the set. A @samp{]} may be matched by including it as the first
2837 character in the set.
2838 The sorting order of characters in range expressions is determined by
2839 the current locale and the values of the
2840 @env{LC_COLLATE} and @env{LC_ALL} shell variables, if set.
2841
2842 For example, in the default C locale, @samp{[a-dx-z]} is equivalent to
2843 @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in
2844 these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]};
2845 it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain
2846 the traditional interpretation of ranges in bracket expressions, you can
2847 force the use of the C locale by setting the @env{LC_COLLATE} or
2848 @env{LC_ALL} environment variable to the value @samp{C}, or enable the
2849 @code{globasciiranges} shell option.
2850
2851 Within @samp{[} and @samp{]}, @dfn{character classes} can be specified
2852 using the syntax
2853 @code{[:}@var{class}@code{:]}, where @var{class} is one of the
2854 following classes defined in the @sc{posix} standard:
2855 @example
2856 alnum alpha ascii blank cntrl digit graph lower
2857 print punct space upper word xdigit
2858 @end example
2859 @noindent
2860 A character class matches any character belonging to that class.
2861 The @code{word} character class matches letters, digits, and the character
2862 @samp{_}.
2863
2864 Within @samp{[} and @samp{]}, an @dfn{equivalence class} can be
2865 specified using the syntax @code{[=}@var{c}@code{=]}, which
2866 matches all characters with the same collation weight (as defined
2867 by the current locale) as the character @var{c}.
2868
2869 Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]}
2870 matches the collating symbol @var{symbol}.
2871 @end table
2872
2873 If the @code{extglob} shell option is enabled using the @code{shopt}
2874 builtin, the shell recognizes several extended pattern matching operators.
2875 In the following description, a @var{pattern-list} is a list of one
2876 or more patterns separated by a @samp{|}.
2877 Composite patterns may be formed using one or more of the following
2878 sub-patterns:
2879
2880 @table @code
2881 @item ?(@var{pattern-list})
2882 Matches zero or one occurrence of the given patterns.
2883
2884 @item *(@var{pattern-list})
2885 Matches zero or more occurrences of the given patterns.
2886
2887 @item +(@var{pattern-list})
2888 Matches one or more occurrences of the given patterns.
2889
2890 @item @@(@var{pattern-list})
2891 Matches one of the given patterns.
2892
2893 @item !(@var{pattern-list})
2894 Matches anything except one of the given patterns.
2895 @end table
2896
2897 Complicated extended pattern matching against long strings is slow,
2898 especially when the patterns contain alternations and the strings
2899 contain multiple matches.
2900 Using separate matches against shorter strings, or using arrays of
2901 strings instead of a single long string, may be faster.
2902
2903 @node Quote Removal
2904 @subsection Quote Removal
2905
2906 After the preceding expansions, all unquoted occurrences of the
2907 characters @samp{\}, @samp{'}, and @samp{"} that did not
2908 result from one of the above expansions are removed.
2909
2910 @node Redirections
2911 @section Redirections
2912 @cindex redirection
2913
2914 Before a command is executed, its input and output
2915 may be @dfn{redirected}
2916 using a special notation interpreted by the shell.
2917 @dfn{Redirection} allows commands' file handles to be
2918 duplicated, opened, closed,
2919 made to refer to different files,
2920 and can change the files the command reads from and writes to.
2921 Redirection may also be used to modify file handles in the
2922 current shell execution environment. The following redirection
2923 operators may precede or appear anywhere within a
2924 simple command or may follow a command.
2925 Redirections are processed in the order they appear, from
2926 left to right.
2927
2928 Each redirection that may be preceded by a file descriptor number
2929 may instead be preceded by a word of the form @{@var{varname}@}.
2930 In this case, for each redirection operator except
2931 >&- and <&-, the shell will allocate a file descriptor greater
2932 than 10 and assign it to @{@var{varname}@}. If >&- or <&- is preceded
2933 by @{@var{varname}@}, the value of @var{varname} defines the file
2934 descriptor to close.
2935 If @{@var{varname}@} is supplied, the redirection persists beyond
2936 the scope of the command, allowing the shell programmer to manage
2937 the file descriptor's lifetime manually.
2938
2939 In the following descriptions, if the file descriptor number is
2940 omitted, and the first character of the redirection operator is
2941 @samp{<}, the redirection refers to the standard input (file
2942 descriptor 0). If the first character of the redirection operator
2943 is @samp{>}, the redirection refers to the standard output (file
2944 descriptor 1).
2945
2946 The word following the redirection operator in the following
2947 descriptions, unless otherwise noted, is subjected to brace expansion,
2948 tilde expansion, parameter expansion, command substitution, arithmetic
2949 expansion, quote removal, filename expansion, and word splitting.
2950 If it expands to more than one word, Bash reports an error.
2951
2952 Note that the order of redirections is significant. For example,
2953 the command
2954 @example
2955 ls > @var{dirlist} 2>&1
2956 @end example
2957 @noindent
2958 directs both standard output (file descriptor 1) and standard error
2959 (file descriptor 2) to the file @var{dirlist}, while the command
2960 @example
2961 ls 2>&1 > @var{dirlist}
2962 @end example
2963 @noindent
2964 directs only the standard output to file @var{dirlist},
2965 because the standard error was made a copy of the standard output
2966 before the standard output was redirected to @var{dirlist}.
2967
2968 Bash handles several filenames specially when they are used in
2969 redirections, as described in the following table.
2970 If the operating system on which Bash is running provides these
2971 special files, bash will use them; otherwise it will emulate them
2972 internally with the behavior described below.
2973
2974 @table @code
2975 @item /dev/fd/@var{fd}
2976 If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated.
2977
2978 @item /dev/stdin
2979 File descriptor 0 is duplicated.
2980
2981 @item /dev/stdout
2982 File descriptor 1 is duplicated.
2983
2984 @item /dev/stderr
2985 File descriptor 2 is duplicated.
2986
2987 @item /dev/tcp/@var{host}/@var{port}
2988 If @var{host} is a valid hostname or Internet address, and @var{port}
2989 is an integer port number or service name, Bash attempts to open
2990 the corresponding TCP socket.
2991
2992 @item /dev/udp/@var{host}/@var{port}
2993 If @var{host} is a valid hostname or Internet address, and @var{port}
2994 is an integer port number or service name, Bash attempts to open
2995 the corresponding UDP socket.
2996 @end table
2997
2998 A failure to open or create a file causes the redirection to fail.
2999
3000 Redirections using file descriptors greater than 9 should be used with
3001 care, as they may conflict with file descriptors the shell uses
3002 internally.
3003
3004 @subsection Redirecting Input
3005 Redirection of input causes the file whose name results from
3006 the expansion of @var{word}
3007 to be opened for reading on file descriptor @code{n},
3008 or the standard input (file descriptor 0) if @code{n}
3009 is not specified.
3010
3011 The general format for redirecting input is:
3012 @example
3013 [@var{n}]<@var{word}
3014 @end example
3015
3016 @subsection Redirecting Output
3017 Redirection of output causes the file whose name results from
3018 the expansion of @var{word}
3019 to be opened for writing on file descriptor @var{n},
3020 or the standard output (file descriptor 1) if @var{n}
3021 is not specified. If the file does not exist it is created;
3022 if it does exist it is truncated to zero size.
3023
3024 The general format for redirecting output is:
3025 @example
3026 [@var{n}]>[|]@var{word}
3027 @end example
3028
3029 If the redirection operator is @samp{>}, and the @code{noclobber}
3030 option to the @code{set} builtin has been enabled, the redirection
3031 will fail if the file whose name results from the expansion of
3032 @var{word} exists and is a regular file.
3033 If the redirection operator is @samp{>|}, or the redirection operator is
3034 @samp{>} and the @code{noclobber} option is not enabled, the redirection
3035 is attempted even if the file named by @var{word} exists.
3036
3037 @subsection Appending Redirected Output
3038 Redirection of output in this fashion
3039 causes the file whose name results from
3040 the expansion of @var{word}
3041 to be opened for appending on file descriptor @var{n},
3042 or the standard output (file descriptor 1) if @var{n}
3043 is not specified. If the file does not exist it is created.
3044
3045 The general format for appending output is:
3046 @example
3047 [@var{n}]>>@var{word}
3048 @end example
3049
3050 @subsection Redirecting Standard Output and Standard Error
3051 This construct allows both the
3052 standard output (file descriptor 1) and
3053 the standard error output (file descriptor 2)
3054 to be redirected to the file whose name is the
3055 expansion of @var{word}.
3056
3057 There are two formats for redirecting standard output and
3058 standard error:
3059 @example
3060 &>@var{word}
3061 @end example
3062 @noindent
3063 and
3064 @example
3065 >&@var{word}
3066 @end example
3067 @noindent
3068 Of the two forms, the first is preferred.
3069 This is semantically equivalent to
3070 @example
3071 >@var{word} 2>&1
3072 @end example
3073 When using the second form, @var{word} may not expand to a number or
3074 @samp{-}. If it does, other redirection operators apply
3075 (see Duplicating File Descriptors below) for compatibility reasons.
3076
3077 @subsection Appending Standard Output and Standard Error
3078 This construct allows both the
3079 standard output (file descriptor 1) and
3080 the standard error output (file descriptor 2)
3081 to be appended to the file whose name is the
3082 expansion of @var{word}.
3083
3084 The format for appending standard output and standard error is:
3085 @example
3086 &>>@var{word}
3087 @end example
3088 @noindent
3089 This is semantically equivalent to
3090 @example
3091 >>@var{word} 2>&1
3092 @end example
3093 (see Duplicating File Descriptors below).
3094
3095 @subsection Here Documents
3096 This type of redirection instructs the shell to read input from the
3097 current source until a line containing only @var{word}
3098 (with no trailing blanks) is seen. All of
3099 the lines read up to that point are then used as the standard
3100 input (or file descriptor @var{n} if @var{n} is specified) for a command.
3101
3102 The format of here-documents is:
3103 @example
3104 [@var{n}]<<[@minus{}]@var{word}
3105 @var{here-document}
3106 @var{delimiter}
3107 @end example
3108
3109 No parameter and variable expansion, command substitution,
3110 arithmetic expansion, or filename expansion is performed on
3111 @var{word}. If any part of @var{word} is quoted, the
3112 @var{delimiter} is the result of quote removal on @var{word},
3113 and the lines in the here-document are not expanded.
3114 If @var{word} is unquoted,
3115 all lines of the here-document are subjected to
3116 parameter expansion, command substitution, and arithmetic expansion,
3117 the character sequence @code{\newline} is ignored, and @samp{\}
3118 must be used to quote the characters
3119 @samp{\}, @samp{$}, and @samp{`}.
3120
3121 If the redirection operator is @samp{<<-},
3122 then all leading tab characters are stripped from input lines and the
3123 line containing @var{delimiter}.
3124 This allows here-documents within shell scripts to be indented in a
3125 natural fashion.
3126
3127 @subsection Here Strings
3128 A variant of here documents, the format is:
3129 @example
3130 [@var{n}]<<< @var{word}
3131 @end example
3132
3133 The @var{word} undergoes
3134 tilde expansion, parameter and variable expansion,
3135 command substitution, arithmetic expansion, and quote removal.
3136 Filename expansion and word splitting are not performed.
3137 The result is supplied as a single string,
3138 with a newline appended,
3139 to the command on its
3140 standard input (or file descriptor @var{n} if @var{n} is specified).
3141
3142 @subsection Duplicating File Descriptors
3143 The redirection operator
3144 @example
3145 [@var{n}]<&@var{word}
3146 @end example
3147 @noindent
3148 is used to duplicate input file descriptors.
3149 If @var{word}
3150 expands to one or more digits, the file descriptor denoted by @var{n}
3151 is made to be a copy of that file descriptor.
3152 If the digits in @var{word} do not specify a file descriptor open for
3153 input, a redirection error occurs.
3154 If @var{word}
3155 evaluates to @samp{-}, file descriptor @var{n} is closed.
3156 If @var{n} is not specified, the standard input (file descriptor 0) is used.
3157
3158 The operator
3159 @example
3160 [@var{n}]>&@var{word}
3161 @end example
3162 @noindent
3163 is used similarly to duplicate output file descriptors. If
3164 @var{n} is not specified, the standard output (file descriptor 1) is used.
3165 If the digits in @var{word} do not specify a file descriptor open for
3166 output, a redirection error occurs.
3167 If @var{word}
3168 evaluates to @samp{-}, file descriptor @var{n} is closed.
3169 As a special case, if @var{n} is omitted, and @var{word} does not
3170 expand to one or more digits or @samp{-}, the standard output and standard
3171 error are redirected as described previously.
3172
3173 @subsection Moving File Descriptors
3174 The redirection operator
3175 @example
3176 [@var{n}]<&@var{digit}-
3177 @end example
3178 @noindent
3179 moves the file descriptor @var{digit} to file descriptor @var{n},
3180 or the standard input (file descriptor 0) if @var{n} is not specified.
3181 @var{digit} is closed after being duplicated to @var{n}.
3182
3183 Similarly, the redirection operator
3184 @example
3185 [@var{n}]>&@var{digit}-
3186 @end example
3187 @noindent
3188 moves the file descriptor @var{digit} to file descriptor @var{n},
3189 or the standard output (file descriptor 1) if @var{n} is not specified.
3190
3191 @subsection Opening File Descriptors for Reading and Writing
3192 The redirection operator
3193 @example
3194 [@var{n}]<>@var{word}
3195 @end example
3196 @noindent
3197 causes the file whose name is the expansion of @var{word}
3198 to be opened for both reading and writing on file descriptor
3199 @var{n}, or on file descriptor 0 if @var{n}
3200 is not specified. If the file does not exist, it is created.
3201
3202 @node Executing Commands
3203 @section Executing Commands
3204
3205 @menu
3206 * Simple Command Expansion:: How Bash expands simple commands before
3207 executing them.
3208 * Command Search and Execution:: How Bash finds commands and runs them.
3209 * Command Execution Environment:: The environment in which Bash
3210 executes commands that are not
3211 shell builtins.
3212 * Environment:: The environment given to a command.
3213 * Exit Status:: The status returned by commands and how Bash
3214 interprets it.
3215 * Signals:: What happens when Bash or a command it runs
3216 receives a signal.
3217 @end menu
3218
3219 @node Simple Command Expansion
3220 @subsection Simple Command Expansion
3221 @cindex command expansion
3222
3223 When a simple command is executed, the shell performs the following
3224 expansions, assignments, and redirections, from left to right, in
3225 the following order.
3226
3227 @enumerate
3228 @item
3229 The words that the parser has marked as variable assignments (those
3230 preceding the command name) and redirections are saved for later
3231 processing.
3232
3233 @item
3234 The words that are not variable assignments or redirections are
3235 expanded (@pxref{Shell Expansions}).
3236 If any words remain after expansion, the first word
3237 is taken to be the name of the command and the remaining words are
3238 the arguments.
3239
3240 @item
3241 Redirections are performed as described above (@pxref{Redirections}).
3242
3243 @item
3244 The text after the @samp{=} in each variable assignment undergoes tilde
3245 expansion, parameter expansion, command substitution, arithmetic expansion,
3246 and quote removal before being assigned to the variable.
3247 @end enumerate
3248
3249 If no command name results, the variable assignments affect the current
3250 shell environment. Otherwise, the variables are added to the environment
3251 of the executed command and do not affect the current shell environment.
3252 If any of the assignments attempts to assign a value to a readonly variable,
3253 an error occurs, and the command exits with a non-zero status.
3254
3255 If no command name results, redirections are performed, but do not
3256 affect the current shell environment. A redirection error causes the
3257 command to exit with a non-zero status.
3258
3259 If there is a command name left after expansion, execution proceeds as
3260 described below. Otherwise, the command exits. If one of the expansions
3261 contained a command substitution, the exit status of the command is
3262 the exit status of the last command substitution performed. If there
3263 were no command substitutions, the command exits with a status of zero.
3264
3265 @node Command Search and Execution
3266 @subsection Command Search and Execution
3267 @cindex command execution
3268 @cindex command search
3269
3270 After a command has been split into words, if it results in a
3271 simple command and an optional list of arguments, the following
3272 actions are taken.
3273
3274 @enumerate
3275 @item
3276 If the command name contains no slashes, the shell attempts to
3277 locate it. If there exists a shell function by that name, that
3278 function is invoked as described in @ref{Shell Functions}.
3279
3280 @item
3281 If the name does not match a function, the shell searches for
3282 it in the list of shell builtins. If a match is found, that
3283 builtin is invoked.
3284
3285 @item
3286 If the name is neither a shell function nor a builtin,
3287 and contains no slashes, Bash searches each element of
3288 @env{$PATH} for a directory containing an executable file
3289 by that name. Bash uses a hash table to remember the full
3290 pathnames of executable files to avoid multiple @env{PATH} searches
3291 (see the description of @code{hash} in @ref{Bourne Shell Builtins}).
3292 A full search of the directories in @env{$PATH}
3293 is performed only if the command is not found in the hash table.
3294 If the search is unsuccessful, the shell searches for a defined shell
3295 function named @code{command_not_found_handle}.
3296 If that function exists, it is invoked in a separate execution environment
3297 with the original command and
3298 the original command's arguments as its arguments, and the function's
3299 exit status becomes the exit status of that subshell.
3300 If that function is not defined, the shell prints an error
3301 message and returns an exit status of 127.
3302
3303 @item
3304 If the search is successful, or if the command name contains
3305 one or more slashes, the shell executes the named program in
3306 a separate execution environment.
3307 Argument 0 is set to the name given, and the remaining arguments
3308 to the command are set to the arguments supplied, if any.
3309
3310 @item
3311 If this execution fails because the file is not in executable
3312 format, and the file is not a directory, it is assumed to be a
3313 @dfn{shell script} and the shell executes it as described in
3314 @ref{Shell Scripts}.
3315
3316 @item
3317 If the command was not begun asynchronously, the shell waits for
3318 the command to complete and collects its exit status.
3319
3320 @end enumerate
3321
3322 @node Command Execution Environment
3323 @subsection Command Execution Environment
3324 @cindex execution environment
3325
3326 The shell has an @dfn{execution environment}, which consists of the
3327 following:
3328
3329 @itemize @bullet
3330 @item
3331 open files inherited by the shell at invocation, as modified by
3332 redirections supplied to the @code{exec} builtin
3333
3334 @item
3335 the current working directory as set by @code{cd}, @code{pushd}, or
3336 @code{popd}, or inherited by the shell at invocation
3337
3338 @item
3339 the file creation mode mask as set by @code{umask} or inherited from
3340 the shell's parent
3341
3342 @item
3343 current traps set by @code{trap}
3344
3345 @item
3346 shell parameters that are set by variable assignment or with @code{set}
3347 or inherited from the shell's parent in the environment
3348
3349 @item
3350 shell functions defined during execution or inherited from the shell's
3351 parent in the environment
3352
3353 @item
3354 options enabled at invocation (either by default or with command-line
3355 arguments) or by @code{set}
3356
3357 @item
3358 options enabled by @code{shopt} (@pxref{The Shopt Builtin})
3359
3360 @item
3361 shell aliases defined with @code{alias} (@pxref{Aliases})
3362
3363 @item
3364 various process @sc{id}s, including those of background jobs
3365 (@pxref{Lists}), the value of @code{$$}, and the value of
3366 @env{$PPID}
3367
3368 @end itemize
3369
3370 When a simple command other than a builtin or shell function
3371 is to be executed, it
3372 is invoked in a separate execution environment that consists of
3373 the following. Unless otherwise noted, the values are inherited
3374 from the shell.
3375
3376 @itemize @bullet
3377 @item
3378 the shell's open files, plus any modifications and additions specified
3379 by redirections to the command
3380
3381 @item
3382 the current working directory
3383
3384 @item
3385 the file creation mode mask
3386
3387 @item
3388 shell variables and functions marked for export, along with variables
3389 exported for the command, passed in the environment (@pxref{Environment})
3390
3391 @item
3392 traps caught by the shell are reset to the values inherited from the
3393 shell's parent, and traps ignored by the shell are ignored
3394
3395 @end itemize
3396
3397 A command invoked in this separate environment cannot affect the
3398 shell's execution environment.
3399
3400 Command substitution, commands grouped with parentheses,
3401 and asynchronous commands are invoked in a
3402 subshell environment that is a duplicate of the shell environment,
3403 except that traps caught by the shell are reset to the values
3404 that the shell inherited from its parent at invocation. Builtin
3405 commands that are invoked as part of a pipeline are also executed
3406 in a subshell environment. Changes made to the subshell environment
3407 cannot affect the shell's execution environment.
3408
3409 Subshells spawned to execute command substitutions inherit the value of
3410 the @option{-e} option from the parent shell. When not in @sc{posix} mode,
3411 Bash clears the @option{-e} option in such subshells.
3412
3413 If a command is followed by a @samp{&} and job control is not active, the
3414 default standard input for the command is the empty file @file{/dev/null}.
3415 Otherwise, the invoked command inherits the file descriptors of the calling
3416 shell as modified by redirections.
3417
3418 @node Environment
3419 @subsection Environment
3420 @cindex environment
3421
3422 When a program is invoked it is given an array of strings
3423 called the @dfn{environment}.
3424 This is a list of name-value pairs, of the form @code{name=value}.
3425
3426 Bash provides several ways to manipulate the environment.
3427 On invocation, the shell scans its own environment and
3428 creates a parameter for each name found, automatically marking
3429 it for @code{export}
3430 to child processes. Executed commands inherit the environment.
3431 The @code{export} and @samp{declare -x}
3432 commands allow parameters and functions to be added to and
3433 deleted from the environment. If the value of a parameter
3434 in the environment is modified, the new value becomes part
3435 of the environment, replacing the old. The environment
3436 inherited by any executed command consists of the shell's
3437 initial environment, whose values may be modified in the shell,
3438 less any pairs removed by the @code{unset} and @samp{export -n}
3439 commands, plus any additions via the @code{export} and
3440 @samp{declare -x} commands.
3441
3442 The environment for any simple command
3443 or function may be augmented temporarily by prefixing it with
3444 parameter assignments, as described in @ref{Shell Parameters}.
3445 These assignment statements affect only the environment seen
3446 by that command.
3447
3448 If the @option{-k} option is set (@pxref{The Set Builtin}), then all
3449 parameter assignments are placed in the environment for a command,
3450 not just those that precede the command name.
3451
3452 When Bash invokes an external command, the variable @samp{$_}
3453 is set to the full pathname of the command and passed to that
3454 command in its environment.
3455
3456 @node Exit Status
3457 @subsection Exit Status
3458 @cindex exit status
3459
3460 The exit status of an executed command is the value returned by the
3461 @code{waitpid} system call or equivalent function. Exit statuses
3462 fall between 0 and 255, though, as explained below, the shell may
3463 use values above 125 specially. Exit statuses from shell builtins and
3464 compound commands are also limited to this range. Under certain
3465 circumstances, the shell will use special values to indicate specific
3466 failure modes.
3467
3468 For the shell's purposes, a command which exits with a
3469 zero exit status has succeeded.
3470 A non-zero exit status indicates failure.
3471 This seemingly counter-intuitive scheme is used so there
3472 is one well-defined way to indicate success and a variety of
3473 ways to indicate various failure modes.
3474 When a command terminates on a fatal signal whose number is @var{N},
3475 Bash uses the value 128+@var{N} as the exit status.
3476
3477 If a command is not found, the child process created to
3478 execute it returns a status of 127. If a command is found
3479 but is not executable, the return status is 126.
3480
3481 If a command fails because of an error during expansion or redirection,
3482 the exit status is greater than zero.
3483
3484 The exit status is used by the Bash conditional commands
3485 (@pxref{Conditional Constructs}) and some of the list
3486 constructs (@pxref{Lists}).
3487
3488 All of the Bash builtins return an exit status of zero if they succeed
3489 and a non-zero status on failure, so they may be used by the
3490 conditional and list constructs.
3491 All builtins return an exit status of 2 to indicate incorrect usage,
3492 generally invalid options or missing arguments.
3493
3494 @node Signals
3495 @subsection Signals
3496 @cindex signal handling
3497
3498 When Bash is interactive, in the absence of any traps, it ignores
3499 @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell),
3500 and @code{SIGINT}
3501 is caught and handled (so that the @code{wait} builtin is interruptible).
3502 When Bash receives a @code{SIGINT}, it breaks out of any executing loops.
3503 In all cases, Bash ignores @code{SIGQUIT}.
3504 If job control is in effect (@pxref{Job Control}), Bash
3505 ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3506
3507 Non-builtin commands started by Bash have signal handlers set to the
3508 values inherited by the shell from its parent.
3509 When job control is not in effect, asynchronous commands
3510 ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited
3511 handlers.
3512 Commands run as a result of
3513 command substitution ignore the keyboard-generated job control signals
3514 @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3515
3516 The shell exits by default upon receipt of a @code{SIGHUP}.
3517 Before exiting, an interactive shell resends the @code{SIGHUP} to
3518 all jobs, running or stopped.
3519 Stopped jobs are sent @code{SIGCONT} to ensure that they receive
3520 the @code{SIGHUP}.
3521 To prevent the shell from sending the @code{SIGHUP} signal to a
3522 particular job, it should be removed
3523 from the jobs table with the @code{disown}
3524 builtin (@pxref{Job Control Builtins}) or marked
3525 to not receive @code{SIGHUP} using @code{disown -h}.
3526
3527 If the @code{huponexit} shell option has been set with @code{shopt}
3528 (@pxref{The Shopt Builtin}), Bash sends a @code{SIGHUP} to all jobs when
3529 an interactive login shell exits.
3530
3531 If Bash is waiting for a command to complete and receives a signal
3532 for which a trap has been set, the trap will not be executed until
3533 the command completes.
3534 When Bash is waiting for an asynchronous
3535 command via the @code{wait} builtin, the reception of a signal for
3536 which a trap has been set will cause the @code{wait} builtin to return
3537 immediately with an exit status greater than 128, immediately after
3538 which the trap is executed.
3539
3540 @node Shell Scripts
3541 @section Shell Scripts
3542 @cindex shell script
3543
3544 A shell script is a text file containing shell commands. When such
3545 a file is used as the first non-option argument when invoking Bash,
3546 and neither the @option{-c} nor @option{-s} option is supplied
3547 (@pxref{Invoking Bash}),
3548 Bash reads and executes commands from the file, then exits. This
3549 mode of operation creates a non-interactive shell. The shell first
3550 searches for the file in the current directory, and looks in the
3551 directories in @env{$PATH} if not found there.
3552
3553 When Bash runs
3554 a shell script, it sets the special parameter @code{0} to the name
3555 of the file, rather than the name of the shell, and the positional
3556 parameters are set to the remaining arguments, if any are given.
3557 If no additional arguments are supplied, the positional parameters
3558 are unset.
3559
3560 A shell script may be made executable by using the @code{chmod} command
3561 to turn on the execute bit. When Bash finds such a file while
3562 searching the @env{$PATH} for a command, it spawns a subshell to
3563 execute it. In other words, executing
3564 @example
3565 filename @var{arguments}
3566 @end example
3567 @noindent
3568 is equivalent to executing
3569 @example
3570 bash filename @var{arguments}
3571 @end example
3572
3573 @noindent
3574 if @code{filename} is an executable shell script.
3575 This subshell reinitializes itself, so that the effect is as if a
3576 new shell had been invoked to interpret the script, with the
3577 exception that the locations of commands remembered by the parent
3578 (see the description of @code{hash} in @ref{Bourne Shell Builtins})
3579 are retained by the child.
3580
3581 Most versions of Unix make this a part of the operating system's command
3582 execution mechanism. If the first line of a script begins with
3583 the two characters @samp{#!}, the remainder of the line specifies
3584 an interpreter for the program and, depending on the operating system, one
3585 or more optional arguments for that interpreter.
3586 Thus, you can specify Bash, @code{awk}, Perl, or some other
3587 interpreter and write the rest of the script file in that language.
3588
3589 The arguments to the interpreter
3590 consist of one or more optional arguments following the interpreter
3591 name on the first line of the script file, followed by the name of
3592 the script file, followed by the rest of the arguments supplied to the
3593 script.
3594 The details of how the interpreter line is split into an interpreter name
3595 and a set of arguments vary across systems.
3596 Bash will perform this action on operating systems that do not handle it
3597 themselves.
3598 Note that some older versions of Unix limit the interpreter
3599 name and a single argument to a maximum of 32 characters, so it's not
3600 portable to assume that using more than one argument will work.
3601
3602 Bash scripts often begin with @code{#! /bin/bash} (assuming that
3603 Bash has been installed in @file{/bin}), since this ensures that
3604 Bash will be used to interpret the script, even if it is executed
3605 under another shell. It's a common idiom to use @code{env} to find
3606 @code{bash} even if it's been installed in another directory:
3607 @code{#!/usr/bin/env bash} will find the first occurrence of @code{bash}
3608 in @env{$PATH}.
3609
3610 @node Shell Builtin Commands
3611 @chapter Shell Builtin Commands
3612
3613 @menu
3614 * Bourne Shell Builtins:: Builtin commands inherited from the Bourne
3615 Shell.
3616 * Bash Builtins:: Table of builtins specific to Bash.
3617 * Modifying Shell Behavior:: Builtins to modify shell attributes and
3618 optional behavior.
3619 * Special Builtins:: Builtin commands classified specially by
3620 POSIX.
3621 @end menu
3622
3623 Builtin commands are contained within the shell itself.
3624 When the name of a builtin command is used as the first word of
3625 a simple command (@pxref{Simple Commands}), the shell executes
3626 the command directly, without invoking another program.
3627 Builtin commands are necessary to implement functionality impossible
3628 or inconvenient to obtain with separate utilities.
3629
3630 This section briefly describes the builtins which Bash inherits from
3631 the Bourne Shell, as well as the builtin commands which are unique
3632 to or have been extended in Bash.
3633
3634 Several builtin commands are described in other chapters: builtin
3635 commands which provide the Bash interface to the job control
3636 facilities (@pxref{Job Control Builtins}), the directory stack
3637 (@pxref{Directory Stack Builtins}), the command history
3638 (@pxref{Bash History Builtins}), and the programmable completion
3639 facilities (@pxref{Programmable Completion Builtins}).
3640
3641 Many of the builtins have been extended by @sc{posix} or Bash.
3642
3643 Unless otherwise noted, each builtin command documented as accepting
3644 options preceded by @samp{-} accepts @samp{--}
3645 to signify the end of the options.
3646 The @code{:}, @code{true}, @code{false}, and @code{test}/@code{[}
3647 builtins do not accept options and do not treat @samp{--} specially.
3648 The @code{exit}, @code{logout}, @code{return},
3649 @code{break}, @code{continue}, @code{let},
3650 and @code{shift} builtins accept and process arguments beginning
3651 with @samp{-} without requiring @samp{--}.
3652 Other builtins that accept arguments but are not specified as accepting
3653 options interpret arguments beginning with @samp{-} as invalid options and
3654 require @samp{--} to prevent this interpretation.
3655
3656 @node Bourne Shell Builtins
3657 @section Bourne Shell Builtins
3658
3659 The following shell builtin commands are inherited from the Bourne Shell.
3660 These commands are implemented as specified by the @sc{posix} standard.
3661
3662 @table @code
3663 @item : @r{(a colon)}
3664 @btindex :
3665 @example
3666 : [@var{arguments}]
3667 @end example
3668
3669 Do nothing beyond expanding @var{arguments} and performing redirections.
3670 The return status is zero.
3671
3672 @item . @r{(a period)}
3673 @btindex .
3674 @example
3675 . @var{filename} [@var{arguments}]
3676 @end example
3677
3678 Read and execute commands from the @var{filename} argument in the
3679 current shell context. If @var{filename} does not contain a slash,
3680 the @env{PATH} variable is used to find @var{filename}.
3681 When Bash is not in @sc{posix} mode, the current directory is searched
3682 if @var{filename} is not found in @env{$PATH}.
3683 If any @var{arguments} are supplied, they become the positional
3684 parameters when @var{filename} is executed. Otherwise the positional
3685 parameters are unchanged.
3686 If the @option{-T} option is enabled, @code{source} inherits any trap on
3687 @code{DEBUG}; if it is not, any @code{DEBUG} trap string is saved and
3688 restored around the call to @code{source}, and @code{source} unsets the
3689 @code{DEBUG} trap while it executes.
3690 If @option{-T} is not set, and the sourced file changes
3691 the @code{DEBUG} trap, the new value is retained when @code{source} completes.
3692 The return status is the exit status of the last command executed, or
3693 zero if no commands are executed. If @var{filename} is not found, or
3694 cannot be read, the return status is non-zero.
3695 This builtin is equivalent to @code{source}.
3696
3697 @item break
3698 @btindex break
3699 @example
3700 break [@var{n}]
3701 @end example
3702
3703 Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop.
3704 If @var{n} is supplied, the @var{n}th enclosing loop is exited.
3705 @var{n} must be greater than or equal to 1.
3706 The return status is zero unless @var{n} is not greater than or equal to 1.
3707
3708 @item cd
3709 @btindex cd
3710 @example
3711 cd [-L|[-P [-e]] [-@@] [@var{directory}]
3712 @end example
3713
3714 Change the current working directory to @var{directory}.
3715 If @var{directory} is not supplied, the value of the @env{HOME}
3716 shell variable is used.
3717 Any additional arguments following @var{directory} are ignored.
3718 If the shell variable
3719 @env{CDPATH} exists, it is used as a search path:
3720 each directory name in @env{CDPATH} is searched for
3721 @var{directory}, with alternative directory names in @env{CDPATH}
3722 separated by a colon (@samp{:}).
3723 If @var{directory} begins with a slash, @env{CDPATH} is not used.
3724
3725 The @option{-P} option means to not follow symbolic links: symbolic links
3726 are resolved while @code{cd} is traversing @var{directory} and before
3727 processing an instance of @samp{..} in @var{directory}.
3728
3729 By default, or when the @option{-L} option is supplied, symbolic links
3730 in @var{directory} are resolved after @code{cd} processes an instance
3731 of @samp{..} in @var{directory}.
3732
3733 If @samp{..} appears in @var{directory}, it is processed by removing the
3734 immediately preceding pathname component, back to a slash or the beginning
3735 of @var{directory}.
3736
3737 If the @option{-e} option is supplied with @option{-P}
3738 and the current working directory cannot be successfully determined
3739 after a successful directory change, @code{cd} will return an unsuccessful
3740 status.
3741
3742 On systems that support it, the @option{-@@} option presents the extended
3743 attributes associated with a file as a directory.
3744
3745 If @var{directory} is @samp{-}, it is converted to @env{$OLDPWD}
3746 before the directory change is attempted.
3747
3748 If a non-empty directory name from @env{CDPATH} is used, or if
3749 @samp{-} is the first argument, and the directory change is
3750 successful, the absolute pathname of the new working directory is
3751 written to the standard output.
3752
3753 If the directory change is successful, @code{cd} sets the value of the
3754 @env{PWD} environment variable to the new directory name, and sets the
3755 @env{OLDPWD} environment variable to the value of the current working
3756 directory before the change.
3757
3758 The return status is zero if the directory is successfully changed,
3759 non-zero otherwise.
3760
3761 @item continue
3762 @btindex continue
3763 @example
3764 continue [@var{n}]
3765 @end example
3766
3767 Resume the next iteration of an enclosing @code{for}, @code{while},
3768 @code{until}, or @code{select} loop.
3769 If @var{n} is supplied, the execution of the @var{n}th enclosing loop
3770 is resumed.
3771 @var{n} must be greater than or equal to 1.
3772 The return status is zero unless @var{n} is not greater than or equal to 1.
3773
3774 @item eval
3775 @btindex eval
3776 @example
3777 eval [@var{arguments}]
3778 @end example
3779
3780 The arguments are concatenated together into a single command, which is
3781 then read and executed, and its exit status returned as the exit status
3782 of @code{eval}.
3783 If there are no arguments or only empty arguments, the return status is
3784 zero.
3785
3786 @item exec
3787 @btindex exec
3788 @example
3789 exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]]
3790 @end example
3791
3792 If @var{command}
3793 is supplied, it replaces the shell without creating a new process.
3794 If the @option{-l} option is supplied, the shell places a dash at the
3795 beginning of the zeroth argument passed to @var{command}.
3796 This is what the @code{login} program does.
3797 The @option{-c} option causes @var{command} to be executed with an empty
3798 environment.
3799 If @option{-a} is supplied, the shell passes @var{name} as the zeroth
3800 argument to @var{command}.
3801 If @var{command}
3802 cannot be executed for some reason, a non-interactive shell exits,
3803 unless the @code{execfail} shell option
3804 is enabled. In that case, it returns failure.
3805 An interactive shell returns failure if the file cannot be executed.
3806 A subshell exits unconditionally if @code{exec} fails.
3807 If no @var{command} is specified, redirections may be used to affect
3808 the current shell environment. If there are no redirection errors, the
3809 return status is zero; otherwise the return status is non-zero.
3810
3811 @item exit
3812 @btindex exit
3813 @example
3814 exit [@var{n}]
3815 @end example
3816
3817 Exit the shell, returning a status of @var{n} to the shell's parent.
3818 If @var{n} is omitted, the exit status is that of the last command executed.
3819 Any trap on @code{EXIT} is executed before the shell terminates.
3820
3821 @item export
3822 @btindex export
3823 @example
3824 export [-fn] [-p] [@var{name}[=@var{value}]]
3825 @end example
3826
3827 Mark each @var{name} to be passed to child processes
3828 in the environment. If the @option{-f} option is supplied, the @var{name}s
3829 refer to shell functions; otherwise the names refer to shell variables.
3830 The @option{-n} option means to no longer mark each @var{name} for export.
3831 If no @var{name}s are supplied, or if the @option{-p} option is given, a
3832 list of names of all exported variables is displayed.
3833 The @option{-p} option displays output in a form that may be reused as input.
3834 If a variable name is followed by =@var{value}, the value of
3835 the variable is set to @var{value}.
3836
3837 The return status is zero unless an invalid option is supplied, one of
3838 the names is not a valid shell variable name, or @option{-f} is supplied
3839 with a name that is not a shell function.
3840
3841 @item getopts
3842 @btindex getopts
3843 @example
3844 getopts @var{optstring} @var{name} [@var{arg} @dots{}]
3845 @end example
3846
3847 @code{getopts} is used by shell scripts to parse positional parameters.
3848 @var{optstring} contains the option characters to be recognized; if a
3849 character is followed by a colon, the option is expected to have an
3850 argument, which should be separated from it by whitespace.
3851 The colon (@samp{:}) and question mark (@samp{?}) may not be
3852 used as option characters.
3853 Each time it is invoked, @code{getopts}
3854 places the next option in the shell variable @var{name}, initializing
3855 @var{name} if it does not exist,
3856 and the index of the next argument to be processed into the
3857 variable @env{OPTIND}.
3858 @env{OPTIND} is initialized to 1 each time the shell or a shell script
3859 is invoked.
3860 When an option requires an argument,
3861 @code{getopts} places that argument into the variable @env{OPTARG}.
3862 The shell does not reset @env{OPTIND} automatically; it must be manually
3863 reset between multiple calls to @code{getopts} within the same shell
3864 invocation if a new set of parameters is to be used.
3865
3866 When the end of options is encountered, @code{getopts} exits with a
3867 return value greater than zero.
3868 @env{OPTIND} is set to the index of the first non-option argument,
3869 and @var{name} is set to @samp{?}.
3870
3871 @code{getopts}
3872 normally parses the positional parameters, but if more arguments are
3873 supplied as @var{arg} values, @code{getopts} parses those instead.
3874
3875 @code{getopts} can report errors in two ways. If the first character of
3876 @var{optstring} is a colon, @var{silent}
3877 error reporting is used. In normal operation, diagnostic messages
3878 are printed when invalid options or missing option arguments are
3879 encountered.
3880 If the variable @env{OPTERR}
3881 is set to 0, no error messages will be displayed, even if the first
3882 character of @code{optstring} is not a colon.
3883
3884 If an invalid option is seen,
3885 @code{getopts} places @samp{?} into @var{name} and, if not silent,
3886 prints an error message and unsets @env{OPTARG}.
3887 If @code{getopts} is silent, the option character found is placed in
3888 @env{OPTARG} and no diagnostic message is printed.
3889
3890 If a required argument is not found, and @code{getopts}
3891 is not silent, a question mark (@samp{?}) is placed in @var{name},
3892 @code{OPTARG} is unset, and a diagnostic message is printed.
3893 If @code{getopts} is silent, then a colon (@samp{:}) is placed in
3894 @var{name} and @env{OPTARG} is set to the option character found.
3895
3896 @item hash
3897 @btindex hash
3898 @example
3899 hash [-r] [-p @var{filename}] [-dt] [@var{name}]
3900 @end example
3901
3902 Each time @code{hash} is invoked, it remembers the full pathnames of the
3903 commands specified as @var{name} arguments,
3904 so they need not be searched for on subsequent invocations.
3905 The commands are found by searching through the directories listed in
3906 @env{$PATH}.
3907 Any previously-remembered pathname is discarded.
3908 The @option{-p} option inhibits the path search, and @var{filename} is
3909 used as the location of @var{name}.
3910 The @option{-r} option causes the shell to forget all remembered locations.
3911 The @option{-d} option causes the shell to forget the remembered location
3912 of each @var{name}.
3913 If the @option{-t} option is supplied, the full pathname to which each
3914 @var{name} corresponds is printed. If multiple @var{name} arguments are
3915 supplied with @option{-t}, the @var{name} is printed before the hashed
3916 full pathname.
3917 The @option{-l} option causes output to be displayed in a format
3918 that may be reused as input.
3919 If no arguments are given, or if only @option{-l} is supplied,
3920 information about remembered commands is printed.
3921 The return status is zero unless a @var{name} is not found or an invalid
3922 option is supplied.
3923
3924 @item pwd
3925 @btindex pwd
3926 @example
3927 pwd [-LP]
3928 @end example
3929
3930 Print the absolute pathname of the current working directory.
3931 If the @option{-P} option is supplied, the pathname printed will not
3932 contain symbolic links.
3933 If the @option{-L} option is supplied, the pathname printed may contain
3934 symbolic links.
3935 The return status is zero unless an error is encountered while
3936 determining the name of the current directory or an invalid option
3937 is supplied.
3938
3939 @item readonly
3940 @btindex readonly
3941 @example
3942 readonly [-aAf] [-p] [@var{name}[=@var{value}]] @dots{}
3943 @end example
3944
3945 Mark each @var{name} as readonly.
3946 The values of these names may not be changed by subsequent assignment.
3947 If the @option{-f} option is supplied, each @var{name} refers to a shell
3948 function.
3949 The @option{-a} option means each @var{name} refers to an indexed
3950 array variable; the @option{-A} option means each @var{name} refers
3951 to an associative array variable.
3952 If both options are supplied, @option{-A} takes precedence.
3953 If no @var{name} arguments are given, or if the @option{-p}
3954 option is supplied, a list of all readonly names is printed.
3955 The other options may be used to restrict the output to a subset of
3956 the set of readonly names.
3957 The @option{-p} option causes output to be displayed in a format that
3958 may be reused as input.
3959 If a variable name is followed by =@var{value}, the value of
3960 the variable is set to @var{value}.
3961 The return status is zero unless an invalid option is supplied, one of
3962 the @var{name} arguments is not a valid shell variable or function name,
3963 or the @option{-f} option is supplied with a name that is not a shell function.
3964
3965 @item return
3966 @btindex return
3967 @example
3968 return [@var{n}]
3969 @end example
3970
3971 Cause a shell function to stop executing and return the value @var{n}
3972 to its caller.
3973 If @var{n} is not supplied, the return value is the exit status of the
3974 last command executed in the function.
3975 If @code{return} is executed by a trap handler, the last command used to
3976 determine the status is the last command executed before the trap handler.
3977 If @code{return} is executed during a @code{DEBUG} trap, the last command
3978 used to determine the status is the last command executed by the trap
3979 handler before @code{return} was invoked.
3980 @code{return} may also be used to terminate execution of a script
3981 being executed with the @code{.} (@code{source}) builtin,
3982 returning either @var{n} or
3983 the exit status of the last command executed within the script as the exit
3984 status of the script.
3985 If @var{n} is supplied, the return value is its least significant
3986 8 bits.
3987 Any command associated with the @code{RETURN} trap is executed
3988 before execution resumes after the function or script.
3989 The return status is non-zero if @code{return} is supplied a non-numeric
3990 argument or is used outside a function
3991 and not during the execution of a script by @code{.} or @code{source}.
3992
3993 @item shift
3994 @btindex shift
3995 @example
3996 shift [@var{n}]
3997 @end example
3998
3999 Shift the positional parameters to the left by @var{n}.
4000 The positional parameters from @var{n}+1 @dots{} @code{$#} are
4001 renamed to @code{$1} @dots{} @code{$#}-@var{n}.
4002 Parameters represented by the numbers @code{$#} down to @code{$#}-@var{n}+1
4003 are unset.
4004 @var{n} must be a non-negative number less than or equal to @code{$#}.
4005 If @var{n} is zero or greater than @code{$#}, the positional parameters
4006 are not changed.
4007 If @var{n} is not supplied, it is assumed to be 1.
4008 The return status is zero unless @var{n} is greater than @code{$#} or
4009 less than zero, non-zero otherwise.
4010
4011 @item test
4012 @itemx [
4013 @btindex test
4014 @btindex [
4015 @example
4016 test @var{expr}
4017 @end example
4018
4019 Evaluate a conditional expression @var{expr} and return a status of 0
4020 (true) or 1 (false).
4021 Each operator and operand must be a separate argument.
4022 Expressions are composed of the primaries described below in
4023 @ref{Bash Conditional Expressions}.
4024 @code{test} does not accept any options, nor does it accept and ignore
4025 an argument of @option{--} as signifying the end of options.
4026
4027 When the @code{[} form is used, the last argument to the command must
4028 be a @code{]}.
4029
4030 Expressions may be combined using the following operators, listed in
4031 decreasing order of precedence.
4032 The evaluation depends on the number of arguments; see below.
4033 Operator precedence is used when there are five or more arguments.
4034
4035 @table @code
4036 @item ! @var{expr}
4037 True if @var{expr} is false.
4038
4039 @item ( @var{expr} )
4040 Returns the value of @var{expr}.
4041 This may be used to override the normal precedence of operators.
4042
4043 @item @var{expr1} -a @var{expr2}
4044 True if both @var{expr1} and @var{expr2} are true.
4045
4046 @item @var{expr1} -o @var{expr2}
4047 True if either @var{expr1} or @var{expr2} is true.
4048 @end table
4049
4050 The @code{test} and @code{[} builtins evaluate conditional
4051 expressions using a set of rules based on the number of arguments.
4052
4053 @table @asis
4054 @item 0 arguments
4055 The expression is false.
4056
4057 @item 1 argument
4058 The expression is true if, and only if, the argument is not null.
4059
4060 @item 2 arguments
4061 If the first argument is @samp{!}, the expression is true if and
4062 only if the second argument is null.
4063 If the first argument is one of the unary conditional operators
4064 (@pxref{Bash Conditional Expressions}), the expression
4065 is true if the unary test is true.
4066 If the first argument is not a valid unary operator, the expression is
4067 false.
4068
4069 @item 3 arguments
4070 The following conditions are applied in the order listed.
4071
4072 @enumerate
4073 @item
4074 If the second argument is one of the binary conditional
4075 operators (@pxref{Bash Conditional Expressions}), the
4076 result of the expression is the result of the binary test using the
4077 first and third arguments as operands.
4078 The @samp{-a} and @samp{-o} operators are considered binary operators
4079 when there are three arguments.
4080 @item
4081 If the first argument is @samp{!}, the value is the negation of
4082 the two-argument test using the second and third arguments.
4083 @item
4084 If the first argument is exactly @samp{(} and the third argument is
4085 exactly @samp{)}, the result is the one-argument test of the second
4086 argument.
4087 @item
4088 Otherwise, the expression is false.
4089 @end enumerate
4090
4091 @item 4 arguments
4092 If the first argument is @samp{!}, the result is the negation of
4093 the three-argument expression composed of the remaining arguments.
4094 Otherwise, the expression is parsed and evaluated according to
4095 precedence using the rules listed above.
4096
4097 @item 5 or more arguments
4098 The expression is parsed and evaluated according to precedence
4099 using the rules listed above.
4100 @end table
4101
4102 When used with @code{test} or @samp{[}, the @samp{<} and @samp{>}
4103 operators sort lexicographically using ASCII ordering.
4104
4105 @item times
4106 @btindex times
4107 @example
4108 times
4109 @end example
4110
4111 Print out the user and system times used by the shell and its children.
4112 The return status is zero.
4113
4114 @item trap
4115 @btindex trap
4116 @example
4117 trap [-lp] [@var{arg}] [@var{sigspec} @dots{}]
4118 @end example
4119
4120 The commands in @var{arg} are to be read and executed when the
4121 shell receives signal @var{sigspec}. If @var{arg} is absent (and
4122 there is a single @var{sigspec}) or
4123 equal to @samp{-}, each specified signal's disposition is reset
4124 to the value it had when the shell was started.
4125 If @var{arg} is the null string, then the signal specified by
4126 each @var{sigspec} is ignored by the shell and commands it invokes.
4127 If @var{arg} is not present and @option{-p} has been supplied,
4128 the shell displays the trap commands associated with each @var{sigspec}.
4129 If no arguments are supplied, or
4130 only @option{-p} is given, @code{trap} prints the list of commands
4131 associated with each signal number in a form that may be reused as
4132 shell input.
4133 The @option{-l} option causes the shell to print a list of signal names
4134 and their corresponding numbers.
4135 Each @var{sigspec} is either a signal name or a signal number.
4136 Signal names are case insensitive and the @code{SIG} prefix is optional.
4137
4138 If a @var{sigspec}
4139 is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits.
4140 If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed
4141 before every simple command, @code{for} command, @code{case} command,
4142 @code{select} command, every arithmetic @code{for} command, and before
4143 the first command executes in a shell function.
4144 Refer to the description of the @code{extdebug} option to the
4145 @code{shopt} builtin (@pxref{The Shopt Builtin}) for details of its
4146 effect on the @code{DEBUG} trap.
4147 If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed
4148 each time a shell function or a script executed with the @code{.} or
4149 @code{source} builtins finishes executing.
4150
4151 If a @var{sigspec} is @code{ERR}, the command @var{arg}
4152 is executed whenever
4153 a pipeline (which may consist of a single simple
4154 command), a list, or a compound command returns a
4155 non-zero exit status,
4156 subject to the following conditions.
4157 The @code{ERR} trap is not executed if the failed command is part of the
4158 command list immediately following an @code{until} or @code{while} keyword,
4159 part of the test following the @code{if} or @code{elif} reserved words,
4160 part of a command executed in a @code{&&} or @code{||} list
4161 except the command following the final @code{&&} or @code{||},
4162 any command in a pipeline but the last,
4163 or if the command's return
4164 status is being inverted using @code{!}.
4165 These are the same conditions obeyed by the @code{errexit} (@option{-e})
4166 option.
4167
4168 Signals ignored upon entry to the shell cannot be trapped or reset.
4169 Trapped signals that are not being ignored are reset to their original
4170 values in a subshell or subshell environment when one is created.
4171
4172 The return status is zero unless a @var{sigspec} does not specify a
4173 valid signal.
4174
4175 @item umask
4176 @btindex umask
4177 @example
4178 umask [-p] [-S] [@var{mode}]
4179 @end example
4180
4181 Set the shell process's file creation mask to @var{mode}. If
4182 @var{mode} begins with a digit, it is interpreted as an octal number;
4183 if not, it is interpreted as a symbolic mode mask similar
4184 to that accepted by the @code{chmod} command. If @var{mode} is
4185 omitted, the current value of the mask is printed. If the @option{-S}
4186 option is supplied without a @var{mode} argument, the mask is printed
4187 in a symbolic format.
4188 If the @option{-p} option is supplied, and @var{mode}
4189 is omitted, the output is in a form that may be reused as input.
4190 The return status is zero if the mode is successfully changed or if
4191 no @var{mode} argument is supplied, and non-zero otherwise.
4192
4193 Note that when the mode is interpreted as an octal number, each number
4194 of the umask is subtracted from @code{7}. Thus, a umask of @code{022}
4195 results in permissions of @code{755}.
4196
4197 @item unset
4198 @btindex unset
4199 @example
4200 unset [-fnv] [@var{name}]
4201 @end example
4202
4203 Remove each variable or function @var{name}.
4204 If the @option{-v} option is given, each
4205 @var{name} refers to a shell variable and that variable is removed.
4206 If the @option{-f} option is given, the @var{name}s refer to shell
4207 functions, and the function definition is removed.
4208 If the @option{-n} option is supplied, and @var{name} is a variable with
4209 the @code{nameref} attribute, @var{name} will be unset rather than the
4210 variable it references.
4211 @option{-n} has no effect if the @option{-f} option is supplied.
4212 If no options are supplied, each @var{name} refers to a variable; if
4213 there is no variable by that name, a function with that name, if any, is
4214 unset.
4215 Readonly variables and functions may not be unset.
4216 Some shell variables lose their special behavior if they are unset; such
4217 behavior is noted in the description of the individual variables.
4218 The return status is zero unless a @var{name} is readonly or may not be unset.
4219 @end table
4220
4221 @node Bash Builtins
4222 @section Bash Builtin Commands
4223
4224 This section describes builtin commands which are unique to
4225 or have been extended in Bash.
4226 Some of these commands are specified in the @sc{posix} standard.
4227
4228 @table @code
4229
4230 @item alias
4231 @btindex alias
4232 @example
4233 alias [-p] [@var{name}[=@var{value}] @dots{}]
4234 @end example
4235
4236 Without arguments or with the @option{-p} option, @code{alias} prints
4237 the list of aliases on the standard output in a form that allows
4238 them to be reused as input.
4239 If arguments are supplied, an alias is defined for each @var{name}
4240 whose @var{value} is given. If no @var{value} is given, the name
4241 and value of the alias is printed.
4242 Aliases are described in @ref{Aliases}.
4243
4244 @item bind
4245 @btindex bind
4246 @example
4247 bind [-m @var{keymap}] [-lpsvPSVX]
4248 bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}]
4249 bind [-m @var{keymap}] -f @var{filename}
4250 bind [-m @var{keymap}] -x @var{keyseq:shell-command}
4251 bind [-m @var{keymap}] @var{keyseq:function-name}
4252 bind [-m @var{keymap}] @var{keyseq:readline-command}
4253 bind @var{readline-command-line}
4254 @end example
4255
4256 Display current Readline (@pxref{Command Line Editing})
4257 key and function bindings,
4258 bind a key sequence to a Readline function or macro,
4259 or set a Readline variable.
4260 Each non-option argument is a command as it would appear in a
4261 Readline initialization file (@pxref{Readline Init File}),
4262 but each binding or command must be passed as a separate argument; e.g.,
4263 @samp{"\C-x\C-r":re-read-init-file}.
4264
4265 Options, if supplied, have the following meanings:
4266
4267 @table @code
4268 @item -m @var{keymap}
4269 Use @var{keymap} as the keymap to be affected by
4270 the subsequent bindings. Acceptable @var{keymap}
4271 names are
4272 @code{emacs},
4273 @code{emacs-standard},
4274 @code{emacs-meta},
4275 @code{emacs-ctlx},
4276 @code{vi},
4277 @code{vi-move},
4278 @code{vi-command}, and
4279 @code{vi-insert}.
4280 @code{vi} is equivalent to @code{vi-command} (@code{vi-move} is also a
4281 synonym); @code{emacs} is equivalent to @code{emacs-standard}.
4282
4283 @item -l
4284 List the names of all Readline functions.
4285
4286 @item -p
4287 Display Readline function names and bindings in such a way that they
4288 can be used as input or in a Readline initialization file.
4289
4290 @item -P
4291 List current Readline function names and bindings.
4292
4293 @item -v
4294 Display Readline variable names and values in such a way that they
4295 can be used as input or in a Readline initialization file.
4296
4297 @item -V
4298 List current Readline variable names and values.
4299
4300 @item -s
4301 Display Readline key sequences bound to macros and the strings they output
4302 in such a way that they can be used as input or in a Readline
4303 initialization file.
4304
4305 @item -S
4306 Display Readline key sequences bound to macros and the strings they output.
4307
4308 @item -f @var{filename}
4309 Read key bindings from @var{filename}.
4310
4311 @item -q @var{function}
4312 Query about which keys invoke the named @var{function}.
4313
4314 @item -u @var{function}
4315 Unbind all keys bound to the named @var{function}.
4316
4317 @item -r @var{keyseq}
4318 Remove any current binding for @var{keyseq}.
4319
4320 @item -x @var{keyseq:shell-command}
4321 Cause @var{shell-command} to be executed whenever @var{keyseq} is
4322 entered.
4323 When @var{shell-command} is executed, the shell sets the
4324 @code{READLINE_LINE} variable to the contents of the Readline line
4325 buffer and the @code{READLINE_POINT} and @code{READLINE_MARK} variables
4326 to the current location of the insertion point and the saved insertion
4327 point (the @var{mark}), respectively.
4328 If the executed command changes the value of any of @code{READLINE_LINE},
4329 @code{READLINE_POINT}, or @code{READLINE_MARK}, those new values will be
4330 reflected in the editing state.
4331
4332 @item -X
4333 List all key sequences bound to shell commands and the associated commands
4334 in a format that can be reused as input.
4335 @end table
4336
4337 @noindent
4338 The return status is zero unless an invalid option is supplied or an
4339 error occurs.
4340
4341 @item builtin
4342 @btindex builtin
4343 @example
4344 builtin [@var{shell-builtin} [@var{args}]]
4345 @end example
4346
4347 Run a shell builtin, passing it @var{args}, and return its exit status.
4348 This is useful when defining a shell function with the same
4349 name as a shell builtin, retaining the functionality of the builtin within
4350 the function.
4351 The return status is non-zero if @var{shell-builtin} is not a shell
4352 builtin command.
4353
4354 @item caller
4355 @btindex caller
4356 @example
4357 caller [@var{expr}]
4358 @end example
4359
4360 Returns the context of any active subroutine call (a shell function or
4361 a script executed with the @code{.} or @code{source} builtins).
4362
4363 Without @var{expr}, @code{caller} displays the line number and source
4364 filename of the current subroutine call.
4365 If a non-negative integer is supplied as @var{expr}, @code{caller}
4366 displays the line number, subroutine name, and source file corresponding
4367 to that position in the current execution call stack. This extra
4368 information may be used, for example, to print a stack trace. The
4369 current frame is frame 0.
4370
4371 The return value is 0 unless the shell is not executing a subroutine
4372 call or @var{expr} does not correspond to a valid position in the
4373 call stack.
4374
4375 @item command
4376 @btindex command
4377 @example
4378 command [-pVv] @var{command} [@var{arguments} @dots{}]
4379 @end example
4380
4381 Runs @var{command} with @var{arguments} ignoring any shell function
4382 named @var{command}.
4383 Only shell builtin commands or commands found by searching the
4384 @env{PATH} are executed.
4385 If there is a shell function named @code{ls}, running @samp{command ls}
4386 within the function will execute the external command @code{ls}
4387 instead of calling the function recursively.
4388 The @option{-p} option means to use a default value for @env{PATH}
4389 that is guaranteed to find all of the standard utilities.
4390 The return status in this case is 127 if @var{command} cannot be
4391 found or an error occurred, and the exit status of @var{command}
4392 otherwise.
4393
4394 If either the @option{-V} or @option{-v} option is supplied, a
4395 description of @var{command} is printed. The @option{-v} option
4396 causes a single word indicating the command or file name used to
4397 invoke @var{command} to be displayed; the @option{-V} option produces
4398 a more verbose description. In this case, the return status is
4399 zero if @var{command} is found, and non-zero if not.
4400
4401 @item declare
4402 @btindex declare
4403 @example
4404 declare [-aAfFgiIlnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
4405 @end example
4406
4407 Declare variables and give them attributes. If no @var{name}s
4408 are given, then display the values of variables instead.
4409
4410 The @option{-p} option will display the attributes and values of each
4411 @var{name}.
4412 When @option{-p} is used with @var{name} arguments, additional options,
4413 other than @option{-f} and @option{-F}, are ignored.
4414
4415 When @option{-p} is supplied without @var{name} arguments, @code{declare}
4416 will display the attributes and values of all variables having the
4417 attributes specified by the additional options.
4418 If no other options are supplied with @option{-p}, @code{declare} will
4419 display the attributes and values of all shell variables. The @option{-f}
4420 option will restrict the display to shell functions.
4421
4422 The @option{-F} option inhibits the display of function definitions;
4423 only the function name and attributes are printed.
4424 If the @code{extdebug} shell option is enabled using @code{shopt}
4425 (@pxref{The Shopt Builtin}), the source file name and line number where
4426 each @var{name} is defined are displayed as well.
4427 @option{-F} implies @option{-f}.
4428
4429 The @option{-g} option forces variables to be created or modified at
4430 the global scope, even when @code{declare} is executed in a shell function.
4431 It is ignored in all other cases.
4432
4433 The @option{-I} option causes local variables to inherit the attributes
4434 (except the @code{nameref} attribute)
4435 and value of any existing variable with the same
4436 @var{name} at a surrounding scope.
4437 If there is no existing variable, the local variable is initially unset.
4438
4439 The following options can be used to restrict output to variables with
4440 the specified attributes or to give variables attributes:
4441
4442 @table @code
4443 @item -a
4444 Each @var{name} is an indexed array variable (@pxref{Arrays}).
4445
4446 @item -A
4447 Each @var{name} is an associative array variable (@pxref{Arrays}).
4448
4449 @item -f
4450 Use function names only.
4451
4452 @item -i
4453 The variable is to be treated as
4454 an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is
4455 performed when the variable is assigned a value.
4456
4457 @item -l
4458 When the variable is assigned a value, all upper-case characters are
4459 converted to lower-case.
4460 The upper-case attribute is disabled.
4461
4462 @item -n
4463 Give each @var{name} the @code{nameref} attribute, making
4464 it a name reference to another variable.
4465 That other variable is defined by the value of @var{name}.
4466 All references, assignments, and attribute modifications
4467 to @var{name}, except for those using or changing the
4468 @option{-n} attribute itself, are performed on the variable referenced by
4469 @var{name}'s value.
4470 The nameref attribute cannot be applied to array variables.
4471
4472 @item -r
4473 Make @var{name}s readonly. These names cannot then be assigned values
4474 by subsequent assignment statements or unset.
4475
4476 @item -t
4477 Give each @var{name} the @code{trace} attribute.
4478 Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from
4479 the calling shell.
4480 The trace attribute has no special meaning for variables.
4481
4482 @item -u
4483 When the variable is assigned a value, all lower-case characters are
4484 converted to upper-case.
4485 The lower-case attribute is disabled.
4486
4487 @item -x
4488 Mark each @var{name} for export to subsequent commands via
4489 the environment.
4490 @end table
4491
4492 Using @samp{+} instead of @samp{-} turns off the attribute instead,
4493 with the exceptions that @samp{+a} and @samp{+A}
4494 may not be used to destroy array variables and @samp{+r} will not
4495 remove the readonly attribute.
4496 When used in a function, @code{declare} makes each @var{name} local,
4497 as with the @code{local} command, unless the @option{-g} option is used.
4498 If a variable name is followed by =@var{value}, the value of the variable
4499 is set to @var{value}.
4500
4501 When using @option{-a} or @option{-A} and the compound assignment syntax to
4502 create array variables, additional attributes do not take effect until
4503 subsequent assignments.
4504
4505 The return status is zero unless an invalid option is encountered,
4506 an attempt is made to define a function using @samp{-f foo=bar},
4507 an attempt is made to assign a value to a readonly variable,
4508 an attempt is made to assign a value to an array variable without
4509 using the compound assignment syntax (@pxref{Arrays}),
4510 one of the @var{name}s is not a valid shell variable name,
4511 an attempt is made to turn off readonly status for a readonly variable,
4512 an attempt is made to turn off array status for an array variable,
4513 or an attempt is made to display a non-existent function with @option{-f}.
4514
4515 @item echo
4516 @btindex echo
4517 @example
4518 echo [-neE] [@var{arg} @dots{}]
4519 @end example
4520
4521 Output the @var{arg}s, separated by spaces, terminated with a
4522 newline.
4523 The return status is 0 unless a write error occurs.
4524 If @option{-n} is specified, the trailing newline is suppressed.
4525 If the @option{-e} option is given, interpretation of the following
4526 backslash-escaped characters is enabled.
4527 The @option{-E} option disables the interpretation of these escape characters,
4528 even on systems where they are interpreted by default.
4529 The @code{xpg_echo} shell option may be used to
4530 dynamically determine whether or not @code{echo} expands these
4531 escape characters by default.
4532 @code{echo} does not interpret @option{--} to mean the end of options.
4533
4534 @code{echo} interprets the following escape sequences:
4535 @table @code
4536 @item \a
4537 alert (bell)
4538 @item \b
4539 backspace
4540 @item \c
4541 suppress further output
4542 @item \e
4543 @itemx \E
4544 escape
4545 @item \f
4546 form feed
4547 @item \n
4548 new line
4549 @item \r
4550 carriage return
4551 @item \t
4552 horizontal tab
4553 @item \v
4554 vertical tab
4555 @item \\
4556 backslash
4557 @item \0@var{nnn}
4558 the eight-bit character whose value is the octal value @var{nnn}
4559 (zero to three octal digits)
4560 @item \x@var{HH}
4561 the eight-bit character whose value is the hexadecimal value @var{HH}
4562 (one or two hex digits)
4563 @item \u@var{HHHH}
4564 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4565 @var{HHHH} (one to four hex digits)
4566 @item \U@var{HHHHHHHH}
4567 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4568 @var{HHHHHHHH} (one to eight hex digits)
4569 @end table
4570
4571 @item enable
4572 @btindex enable
4573 @example
4574 enable [-a] [-dnps] [-f @var{filename}] [@var{name} @dots{}]
4575 @end example
4576
4577 Enable and disable builtin shell commands.
4578 Disabling a builtin allows a disk command which has the same name
4579 as a shell builtin to be executed without specifying a full pathname,
4580 even though the shell normally searches for builtins before disk commands.
4581 If @option{-n} is used, the @var{name}s become disabled. Otherwise
4582 @var{name}s are enabled. For example, to use the @code{test} binary
4583 found via @env{$PATH} instead of the shell builtin version, type
4584 @samp{enable -n test}.
4585
4586 If the @option{-p} option is supplied, or no @var{name} arguments appear,
4587 a list of shell builtins is printed. With no other arguments, the list
4588 consists of all enabled shell builtins.
4589 The @option{-a} option means to list
4590 each builtin with an indication of whether or not it is enabled.
4591
4592 The @option{-f} option means to load the new builtin command @var{name}
4593 from shared object @var{filename}, on systems that support dynamic loading.
4594 Bash will use the value of the @enable{BASH_LOADABLES_PATH} variable as a
4595 colon-separated list of directories in which to search for @var{filename}.
4596 The default is system-dependent.
4597 The @option{-d} option will delete a builtin loaded with @option{-f}.
4598
4599 If there are no options, a list of the shell builtins is displayed.
4600 The @option{-s} option restricts @code{enable} to the @sc{posix} special
4601 builtins. If @option{-s} is used with @option{-f}, the new builtin becomes
4602 a special builtin (@pxref{Special Builtins}).
4603
4604 The return status is zero unless a @var{name} is not a shell builtin
4605 or there is an error loading a new builtin from a shared object.
4606
4607 @item help
4608 @btindex help
4609 @example
4610 help [-dms] [@var{pattern}]
4611 @end example
4612
4613 Display helpful information about builtin commands.
4614 If @var{pattern} is specified, @code{help} gives detailed help
4615 on all commands matching @var{pattern}, otherwise a list of
4616 the builtins is printed.
4617
4618 Options, if supplied, have the following meanings:
4619
4620 @table @code
4621 @item -d
4622 Display a short description of each @var{pattern}
4623 @item -m
4624 Display the description of each @var{pattern} in a manpage-like format
4625 @item -s
4626 Display only a short usage synopsis for each @var{pattern}
4627 @end table
4628
4629 The return status is zero unless no command matches @var{pattern}.
4630
4631 @item let
4632 @btindex let
4633 @example
4634 let @var{expression} [@var{expression} @dots{}]
4635 @end example
4636
4637 The @code{let} builtin allows arithmetic to be performed on shell
4638 variables. Each @var{expression} is evaluated according to the
4639 rules given below in @ref{Shell Arithmetic}. If the
4640 last @var{expression} evaluates to 0, @code{let} returns 1;
4641 otherwise 0 is returned.
4642
4643 @item local
4644 @btindex local
4645 @example
4646 local [@var{option}] @var{name}[=@var{value}] @dots{}
4647 @end example
4648
4649 For each argument, a local variable named @var{name} is created,
4650 and assigned @var{value}.
4651 The @var{option} can be any of the options accepted by @code{declare}.
4652 @code{local} can only be used within a function; it makes the variable
4653 @var{name} have a visible scope restricted to that function and its
4654 children.
4655 If @var{name} is @samp{-}, the set of shell options is made local to the
4656 function in which @code{local} is invoked: shell options changed using
4657 the @code{set} builtin inside the function are restored to their original
4658 values when the function returns.
4659 The restore is effected as if a series of @code{set} commands were executed
4660 to restore the values that were in place before the function.
4661 The return status is zero unless @code{local} is used outside
4662 a function, an invalid @var{name} is supplied, or @var{name} is a
4663 readonly variable.
4664
4665 @item logout
4666 @btindex logout
4667 @example
4668 logout [@var{n}]
4669 @end example
4670
4671 Exit a login shell, returning a status of @var{n} to the shell's
4672 parent.
4673
4674 @item mapfile
4675 @btindex mapfile
4676 @example
4677 mapfile [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
4678 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
4679 @end example
4680
4681 Read lines from the standard input into the indexed array variable @var{array},
4682 or from file descriptor @var{fd}
4683 if the @option{-u} option is supplied.
4684 The variable @code{MAPFILE} is the default @var{array}.
4685 Options, if supplied, have the following meanings:
4686
4687 @table @code
4688
4689 @item -d
4690 The first character of @var{delim} is used to terminate each input line,
4691 rather than newline.
4692 If @var{delim} is the empty string, @code{mapfile} will terminate a line
4693 when it reads a NUL character.
4694 @item -n
4695 Copy at most @var{count} lines. If @var{count} is 0, all lines are copied.
4696 @item -O
4697 Begin assigning to @var{array} at index @var{origin}.
4698 The default index is 0.
4699 @item -s
4700 Discard the first @var{count} lines read.
4701 @item -t
4702 Remove a trailing @var{delim} (default newline) from each line read.
4703 @item -u
4704 Read lines from file descriptor @var{fd} instead of the standard input.
4705 @item -C
4706 Evaluate @var{callback} each time @var{quantum} lines are read.
4707 The @option{-c} option specifies @var{quantum}.
4708 @item -c
4709 Specify the number of lines read between each call to @var{callback}.
4710 @end table
4711
4712 If @option{-C} is specified without @option{-c},
4713 the default quantum is 5000.
4714 When @var{callback} is evaluated, it is supplied the index of the next
4715 array element to be assigned and the line to be assigned to that element
4716 as additional arguments.
4717 @var{callback} is evaluated after the line is read but before the
4718 array element is assigned.
4719
4720 If not supplied with an explicit origin, @code{mapfile} will clear @var{array}
4721 before assigning to it.
4722
4723 @code{mapfile} returns successfully unless an invalid option or option
4724 argument is supplied, @var{array} is invalid or unassignable, or @var{array}
4725 is not an indexed array.
4726
4727 @item printf
4728 @btindex printf
4729 @example
4730 printf [-v @var{var}] @var{format} [@var{arguments}]
4731 @end example
4732
4733 Write the formatted @var{arguments} to the standard output under the
4734 control of the @var{format}.
4735 The @option{-v} option causes the output to be assigned to the variable
4736 @var{var} rather than being printed to the standard output.
4737
4738 The @var{format} is a character string which contains three types of objects:
4739 plain characters, which are simply copied to standard output, character
4740 escape sequences, which are converted and copied to the standard output, and
4741 format specifications, each of which causes printing of the next successive
4742 @var{argument}.
4743 In addition to the standard @code{printf(1)} formats, @code{printf}
4744 interprets the following extensions:
4745
4746 @table @code
4747 @item %b
4748 Causes @code{printf} to expand backslash escape sequences in the
4749 corresponding @var{argument} in the same way as @code{echo -e}
4750 (@pxref{Bash Builtins}).
4751 @item %q
4752 Causes @code{printf} to output the
4753 corresponding @var{argument} in a format that can be reused as shell input.
4754 @item %(@var{datefmt})T
4755 Causes @code{printf} to output the date-time string resulting from using
4756 @var{datefmt} as a format string for @code{strftime}(3).
4757 The corresponding @var{argument} is an integer representing the number of
4758 seconds since the epoch.
4759 Two special argument values may be used: -1 represents the current
4760 time, and -2 represents the time the shell was invoked.
4761 If no argument is specified, conversion behaves as if -1 had been given.
4762 This is an exception to the usual @code{printf} behavior.
4763 @end table
4764
4765 @noindent
4766 The %b, %q, and %T directives all use the field width and precision
4767 arguments from the format specification and write that many bytes from
4768 (or use that wide a field for) the expanded argument, which usually
4769 contains more characters than the original.
4770
4771 Arguments to non-string format specifiers are treated as C language constants,
4772 except that a leading plus or minus sign is allowed, and if the leading
4773 character is a single or double quote, the value is the ASCII value of
4774 the following character.
4775
4776 The @var{format} is reused as necessary to consume all of the @var{arguments}.
4777 If the @var{format} requires more @var{arguments} than are supplied, the
4778 extra format specifications behave as if a zero value or null string, as
4779 appropriate, had been supplied. The return value is zero on success,
4780 non-zero on failure.
4781
4782 @item read
4783 @btindex read
4784 @example
4785 read [-ers] [-a @var{aname}] [-d @var{delim}] [-i @var{text}] [-n @var{nchars}]
4786 [-N @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}]
4787 @end example
4788
4789 One line is read from the standard input, or from the file descriptor
4790 @var{fd} supplied as an argument to the @option{-u} option,
4791 split into words as described above in @ref{Word Splitting},
4792 and the first word
4793 is assigned to the first @var{name}, the second word to the second @var{name},
4794 and so on.
4795 If there are more words than names,
4796 the remaining words and their intervening delimiters are assigned
4797 to the last @var{name}.
4798 If there are fewer words read from the input stream than names,
4799 the remaining names are assigned empty values.
4800 The characters in the value of the @env{IFS} variable
4801 are used to split the line into words using the same rules the shell
4802 uses for expansion (described above in @ref{Word Splitting}).
4803 The backslash character @samp{\} may be used to remove any special
4804 meaning for the next character read and for line continuation.
4805
4806 Options, if supplied, have the following meanings:
4807
4808 @table @code
4809 @item -a @var{aname}
4810 The words are assigned to sequential indices of the array variable
4811 @var{aname}, starting at 0.
4812 All elements are removed from @var{aname} before the assignment.
4813 Other @var{name} arguments are ignored.
4814
4815 @item -d @var{delim}
4816 The first character of @var{delim} is used to terminate the input line,
4817 rather than newline.
4818 If @var{delim} is the empty string, @code{read} will terminate a line
4819 when it reads a NUL character.
4820
4821 @item -e
4822 Readline (@pxref{Command Line Editing}) is used to obtain the line.
4823 Readline uses the current (or default, if line editing was not previously
4824 active) editing settings, but uses Readline's default filename completion.
4825
4826 @item -i @var{text}
4827 If Readline is being used to read the line, @var{text} is placed into
4828 the editing buffer before editing begins.
4829
4830 @item -n @var{nchars}
4831 @code{read} returns after reading @var{nchars} characters rather than
4832 waiting for a complete line of input, but honors a delimiter if fewer
4833 than @var{nchars} characters are read before the delimiter.
4834
4835 @item -N @var{nchars}
4836 @code{read} returns after reading exactly @var{nchars} characters rather
4837 than waiting for a complete line of input, unless EOF is encountered or
4838 @code{read} times out.
4839 Delimiter characters encountered in the input are
4840 not treated specially and do not cause @code{read} to return until
4841 @var{nchars} characters are read.
4842 The result is not split on the characters in @code{IFS}; the intent is
4843 that the variable is assigned exactly the characters read
4844 (with the exception of backslash; see the @option{-r} option below).
4845
4846 @item -p @var{prompt}
4847 Display @var{prompt}, without a trailing newline, before attempting
4848 to read any input.
4849 The prompt is displayed only if input is coming from a terminal.
4850
4851 @item -r
4852 If this option is given, backslash does not act as an escape character.
4853 The backslash is considered to be part of the line.
4854 In particular, a backslash-newline pair may not then be used as a line
4855 continuation.
4856
4857 @item -s
4858 Silent mode. If input is coming from a terminal, characters are
4859 not echoed.
4860
4861 @item -t @var{timeout}
4862 Cause @code{read} to time out and return failure if a complete line of
4863 input (or a specified number of characters)
4864 is not read within @var{timeout} seconds.
4865 @var{timeout} may be a decimal number with a fractional portion following
4866 the decimal point.
4867 This option is only effective if @code{read} is reading input from a
4868 terminal, pipe, or other special file; it has no effect when reading
4869 from regular files.
4870 If @code{read} times out, @code{read} saves any partial input read into
4871 the specified variable @var{name}.
4872 If @var{timeout} is 0, @code{read} returns immediately, without trying to
4873 read any data. The exit status is 0 if input is available on
4874 the specified file descriptor, non-zero otherwise.
4875 The exit status is greater than 128 if the timeout is exceeded.
4876
4877 @item -u @var{fd}
4878 Read input from file descriptor @var{fd}.
4879 @end table
4880
4881 If no @var{name}s are supplied, the line read,
4882 without the ending delimiter but otherwise unmodified,
4883 is assigned to the
4884 variable @env{REPLY}.
4885 The exit status is zero, unless end-of-file is encountered, @code{read}
4886 times out (in which case the status is greater than 128),
4887 a variable assignment error (such as assigning to a readonly variable) occurs,
4888 or an invalid file descriptor is supplied as the argument to @option{-u}.
4889
4890 @item readarray
4891 @btindex readarray
4892 @example
4893 readarray [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
4894 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
4895 @end example
4896
4897 Read lines from the standard input into the indexed array variable @var{array},
4898 or from file descriptor @var{fd}
4899 if the @option{-u} option is supplied.
4900
4901 A synonym for @code{mapfile}.
4902
4903 @item source
4904 @btindex source
4905 @example
4906 source @var{filename}
4907 @end example
4908
4909 A synonym for @code{.} (@pxref{Bourne Shell Builtins}).
4910
4911 @item type
4912 @btindex type
4913 @example
4914 type [-afptP] [@var{name} @dots{}]
4915 @end example
4916
4917 For each @var{name}, indicate how it would be interpreted if used as a
4918 command name.
4919
4920 If the @option{-t} option is used, @code{type} prints a single word
4921 which is one of @samp{alias}, @samp{function}, @samp{builtin},
4922 @samp{file} or @samp{keyword},
4923 if @var{name} is an alias, shell function, shell builtin,
4924 disk file, or shell reserved word, respectively.
4925 If the @var{name} is not found, then nothing is printed, and
4926 @code{type} returns a failure status.
4927
4928 If the @option{-p} option is used, @code{type} either returns the name
4929 of the disk file that would be executed, or nothing if @option{-t}
4930 would not return @samp{file}.
4931
4932 The @option{-P} option forces a path search for each @var{name}, even if
4933 @option{-t} would not return @samp{file}.
4934
4935 If a command is hashed, @option{-p} and @option{-P} print the hashed value,
4936 which is not necessarily the file that appears first in @code{$PATH}.
4937
4938 If the @option{-a} option is used, @code{type} returns all of the places
4939 that contain an executable named @var{file}.
4940 This includes aliases and functions, if and only if the @option{-p} option
4941 is not also used.
4942
4943 If the @option{-f} option is used, @code{type} does not attempt to find
4944 shell functions, as with the @code{command} builtin.
4945
4946 The return status is zero if all of the @var{name}s are found, non-zero
4947 if any are not found.
4948
4949 @item typeset
4950 @btindex typeset
4951 @example
4952 typeset [-afFgrxilnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
4953 @end example
4954
4955 The @code{typeset} command is supplied for compatibility with the Korn
4956 shell.
4957 It is a synonym for the @code{declare} builtin command.
4958
4959 @item ulimit
4960 @btindex ulimit
4961 @example
4962 ulimit [-HS] -a
4963 ulimit [-HS] [-bcdefiklmnpqrstuvxPRT] [@var{limit}]
4964 @end example
4965
4966 @code{ulimit} provides control over the resources available to processes
4967 started by the shell, on systems that allow such control. If an
4968 option is given, it is interpreted as follows:
4969
4970 @table @code
4971 @item -S
4972 Change and report the soft limit associated with a resource.
4973
4974 @item -H
4975 Change and report the hard limit associated with a resource.
4976
4977 @item -a
4978 All current limits are reported; no limits are set.
4979
4980 @item -b
4981 The maximum socket buffer size.
4982
4983 @item -c
4984 The maximum size of core files created.
4985
4986 @item -d
4987 The maximum size of a process's data segment.
4988
4989 @item -e
4990 The maximum scheduling priority ("nice").
4991
4992 @item -f
4993 The maximum size of files written by the shell and its children.
4994
4995 @item -i
4996 The maximum number of pending signals.
4997
4998 @item -k
4999 The maximum number of kqueues that may be allocated.
5000
5001 @item -l
5002 The maximum size that may be locked into memory.
5003
5004 @item -m
5005 The maximum resident set size (many systems do not honor this limit).
5006
5007 @item -n
5008 The maximum number of open file descriptors (most systems do not
5009 allow this value to be set).
5010
5011 @item -p
5012 The pipe buffer size.
5013
5014 @item -q
5015 The maximum number of bytes in @sc{posix} message queues.
5016
5017 @item -r
5018 The maximum real-time scheduling priority.
5019
5020 @item -s
5021 The maximum stack size.
5022
5023 @item -t
5024 The maximum amount of cpu time in seconds.
5025
5026 @item -u
5027 The maximum number of processes available to a single user.
5028
5029 @item -v
5030 The maximum amount of virtual memory available to the shell, and, on
5031 some systems, to its children.
5032
5033 @item -x
5034 The maximum number of file locks.
5035
5036 @item -P
5037 The maximum number of pseudoterminals.
5038
5039 @item -R
5040 The maximum time a real-time process can run before blocking, in microseconds.
5041
5042 @item -T
5043 The maximum number of threads.
5044 @end table
5045
5046 If @var{limit} is given, and the @option{-a} option is not used,
5047 @var{limit} is the new value of the specified resource.
5048 The special @var{limit} values @code{hard}, @code{soft}, and
5049 @code{unlimited} stand for the current hard limit, the current soft limit,
5050 and no limit, respectively.
5051 A hard limit cannot be increased by a non-root user once it is set;
5052 a soft limit may be increased up to the value of the hard limit.
5053 Otherwise, the current value of the soft limit for the specified resource
5054 is printed, unless the @option{-H} option is supplied.
5055 When more than one
5056 resource is specified, the limit name and unit, if appropriate,
5057 are printed before the value.
5058 When setting new limits, if neither @option{-H} nor @option{-S} is supplied,
5059 both the hard and soft limits are set.
5060 If no option is given, then @option{-f} is assumed. Values are in 1024-byte
5061 increments, except for
5062 @option{-t}, which is in seconds;
5063 @option{-R}, which is in microseconds;
5064 @option{-p}, which is in units of 512-byte blocks;
5065 @option{-P},
5066 @option{-T},
5067 @option{-b},
5068 @option{-k},
5069 @option{-n} and @option{-u}, which are unscaled values;
5070 and, when in @sc{posix} Mode (@pxref{Bash POSIX Mode}),
5071 @option{-c} and @option{-f}, which are in 512-byte increments.
5072
5073 The return status is zero unless an invalid option or argument is supplied,
5074 or an error occurs while setting a new limit.
5075
5076 @item unalias
5077 @btindex unalias
5078 @example
5079 unalias [-a] [@var{name} @dots{} ]
5080 @end example
5081
5082 Remove each @var{name} from the list of aliases. If @option{-a} is
5083 supplied, all aliases are removed.
5084 Aliases are described in @ref{Aliases}.
5085 @end table
5086
5087 @node Modifying Shell Behavior
5088 @section Modifying Shell Behavior
5089
5090 @menu
5091 * The Set Builtin:: Change the values of shell attributes and
5092 positional parameters.
5093 * The Shopt Builtin:: Modify shell optional behavior.
5094 @end menu
5095
5096 @node The Set Builtin
5097 @subsection The Set Builtin
5098
5099 This builtin is so complicated that it deserves its own section. @code{set}
5100 allows you to change the values of shell options and set the positional
5101 parameters, or to display the names and values of shell variables.
5102
5103 @table @code
5104 @item set
5105 @btindex set
5106 @example
5107 set [--abefhkmnptuvxBCEHPT] [-o @var{option-name}] [@var{argument} @dots{}]
5108 set [+abefhkmnptuvxBCEHPT] [+o @var{option-name}] [@var{argument} @dots{}]
5109 @end example
5110
5111 If no options or arguments are supplied, @code{set} displays the names
5112 and values of all shell variables and functions, sorted according to the
5113 current locale, in a format that may be reused as input
5114 for setting or resetting the currently-set variables.
5115 Read-only variables cannot be reset.
5116 In @sc{posix} mode, only shell variables are listed.
5117
5118 When options are supplied, they set or unset shell attributes.
5119 Options, if specified, have the following meanings:
5120
5121 @table @code
5122 @item -a
5123 Each variable or function that is created or modified is given the
5124 export attribute and marked for export to the environment of
5125 subsequent commands.
5126
5127 @item -b
5128 Cause the status of terminated background jobs to be reported
5129 immediately, rather than before printing the next primary prompt.
5130
5131 @item -e
5132 Exit immediately if
5133 a pipeline (@pxref{Pipelines}), which may consist of a single simple command
5134 (@pxref{Simple Commands}),
5135 a list (@pxref{Lists}),
5136 or a compound command (@pxref{Compound Commands})
5137 returns a non-zero status.
5138 The shell does not exit if the command that fails is part of the
5139 command list immediately following a @code{while} or @code{until} keyword,
5140 part of the test in an @code{if} statement,
5141 part of any command executed in a @code{&&} or @code{||} list except
5142 the command following the final @code{&&} or @code{||},
5143 any command in a pipeline but the last,
5144 or if the command's return status is being inverted with @code{!}.
5145 If a compound command other than a subshell
5146 returns a non-zero status because a command failed
5147 while @option{-e} was being ignored, the shell does not exit.
5148 A trap on @code{ERR}, if set, is executed before the shell exits.
5149
5150 This option applies to the shell environment and each subshell environment
5151 separately (@pxref{Command Execution Environment}), and may cause
5152 subshells to exit before executing all the commands in the subshell.
5153
5154 If a compound command or shell function executes in a context where
5155 @option{-e} is being ignored,
5156 none of the commands executed within the compound command or function body
5157 will be affected by the @option{-e} setting, even if @option{-e} is set
5158 and a command returns a failure status.
5159 If a compound command or shell function sets @option{-e} while executing in
5160 a context where @option{-e} is ignored, that setting will not have any
5161 effect until the compound command or the command containing the function
5162 call completes.
5163
5164 @item -f
5165 Disable filename expansion (globbing).
5166
5167 @item -h
5168 Locate and remember (hash) commands as they are looked up for execution.
5169 This option is enabled by default.
5170
5171 @item -k
5172 All arguments in the form of assignment statements are placed
5173 in the environment for a command, not just those that precede
5174 the command name.
5175
5176 @item -m
5177 Job control is enabled (@pxref{Job Control}).
5178 All processes run in a separate process group.
5179 When a background job completes, the shell prints a line
5180 containing its exit status.
5181
5182 @item -n
5183 Read commands but do not execute them.
5184 This may be used to check a script for syntax errors.
5185 This option is ignored by interactive shells.
5186
5187 @item -o @var{option-name}
5188
5189 Set the option corresponding to @var{option-name}:
5190
5191 @table @code
5192 @item allexport
5193 Same as @code{-a}.
5194
5195 @item braceexpand
5196 Same as @code{-B}.
5197
5198 @item emacs
5199 Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}).
5200 This also affects the editing interface used for @code{read -e}.
5201
5202 @item errexit
5203 Same as @code{-e}.
5204
5205 @item errtrace
5206 Same as @code{-E}.
5207
5208 @item functrace
5209 Same as @code{-T}.
5210
5211 @item hashall
5212 Same as @code{-h}.
5213
5214 @item histexpand
5215 Same as @code{-H}.
5216
5217 @item history
5218 Enable command history, as described in @ref{Bash History Facilities}.
5219 This option is on by default in interactive shells.
5220
5221 @item ignoreeof
5222 An interactive shell will not exit upon reading EOF.
5223
5224 @item keyword
5225 Same as @code{-k}.
5226
5227 @item monitor
5228 Same as @code{-m}.
5229
5230 @item noclobber
5231 Same as @code{-C}.
5232
5233 @item noexec
5234 Same as @code{-n}.
5235
5236 @item noglob
5237 Same as @code{-f}.
5238
5239 @item nolog
5240 Currently ignored.
5241
5242 @item notify
5243 Same as @code{-b}.
5244
5245 @item nounset
5246 Same as @code{-u}.
5247
5248 @item onecmd
5249 Same as @code{-t}.
5250
5251 @item physical
5252 Same as @code{-P}.
5253
5254 @item pipefail
5255 If set, the return value of a pipeline is the value of the last
5256 (rightmost) command to exit with a non-zero status, or zero if all
5257 commands in the pipeline exit successfully.
5258 This option is disabled by default.
5259
5260 @item posix
5261 Change the behavior of Bash where the default operation differs
5262 from the @sc{posix} standard to match the standard
5263 (@pxref{Bash POSIX Mode}).
5264 This is intended to make Bash behave as a strict superset of that
5265 standard.
5266
5267 @item privileged
5268 Same as @code{-p}.
5269
5270 @item verbose
5271 Same as @code{-v}.
5272
5273 @item vi
5274 Use a @code{vi}-style line editing interface.
5275 This also affects the editing interface used for @code{read -e}.
5276
5277 @item xtrace
5278 Same as @code{-x}.
5279 @end table
5280
5281 @item -p
5282 Turn on privileged mode.
5283 In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not
5284 processed, shell functions are not inherited from the environment,
5285 and the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH} and @env{GLOBIGNORE}
5286 variables, if they appear in the environment, are ignored.
5287 If the shell is started with the effective user (group) id not equal to the
5288 real user (group) id, and the @option{-p} option is not supplied, these actions
5289 are taken and the effective user id is set to the real user id.
5290 If the @option{-p} option is supplied at startup, the effective user id is
5291 not reset.
5292 Turning this option off causes the effective user
5293 and group ids to be set to the real user and group ids.
5294
5295 @item -t
5296 Exit after reading and executing one command.
5297
5298 @item -u
5299 Treat unset variables and parameters other than the special parameters
5300 @samp{@@} or @samp{*} as an error when performing parameter expansion.
5301 An error message will be written to the standard error, and a non-interactive
5302 shell will exit.
5303
5304 @item -v
5305 Print shell input lines as they are read.
5306
5307 @item -x
5308 Print a trace of simple commands, @code{for} commands, @code{case}
5309 commands, @code{select} commands, and arithmetic @code{for} commands
5310 and their arguments or associated word lists after they are
5311 expanded and before they are executed. The value of the @env{PS4}
5312 variable is expanded and the resultant value is printed before
5313 the command and its expanded arguments.
5314
5315 @item -B
5316 The shell will perform brace expansion (@pxref{Brace Expansion}).
5317 This option is on by default.
5318
5319 @item -C
5320 Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>}
5321 from overwriting existing files.
5322
5323 @item -E
5324 If set, any trap on @code{ERR} is inherited by shell functions, command
5325 substitutions, and commands executed in a subshell environment.
5326 The @code{ERR} trap is normally not inherited in such cases.
5327
5328 @item -H
5329 Enable @samp{!} style history substitution (@pxref{History Interaction}).
5330 This option is on by default for interactive shells.
5331
5332 @item -P
5333 If set, do not resolve symbolic links when performing commands such as
5334 @code{cd} which change the current directory. The physical directory
5335 is used instead. By default, Bash follows
5336 the logical chain of directories when performing commands
5337 which change the current directory.
5338
5339 For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys}
5340 then:
5341 @example
5342 $ cd /usr/sys; echo $PWD
5343 /usr/sys
5344 $ cd ..; pwd
5345 /usr
5346 @end example
5347
5348 @noindent
5349 If @code{set -P} is on, then:
5350 @example
5351 $ cd /usr/sys; echo $PWD
5352 /usr/local/sys
5353 $ cd ..; pwd
5354 /usr/local
5355 @end example
5356
5357 @item -T
5358 If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by
5359 shell functions, command substitutions, and commands executed
5360 in a subshell environment.
5361 The @code{DEBUG} and @code{RETURN} traps are normally not inherited
5362 in such cases.
5363
5364 @item --
5365 If no arguments follow this option, then the positional parameters are
5366 unset. Otherwise, the positional parameters are set to the
5367 @var{arguments}, even if some of them begin with a @samp{-}.
5368
5369 @item -
5370 Signal the end of options, cause all remaining @var{arguments}
5371 to be assigned to the positional parameters. The @option{-x}
5372 and @option{-v} options are turned off.
5373 If there are no arguments, the positional parameters remain unchanged.
5374 @end table
5375
5376 Using @samp{+} rather than @samp{-} causes these options to be
5377 turned off. The options can also be used upon invocation of the
5378 shell. The current set of options may be found in @code{$-}.
5379
5380 The remaining N @var{arguments} are positional parameters and are
5381 assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}.
5382 The special parameter @code{#} is set to N.
5383
5384 The return status is always zero unless an invalid option is supplied.
5385 @end table
5386
5387 @node The Shopt Builtin
5388 @subsection The Shopt Builtin
5389
5390 This builtin allows you to change additional shell optional behavior.
5391
5392 @table @code
5393
5394 @item shopt
5395 @btindex shopt
5396 @example
5397 shopt [-pqsu] [-o] [@var{optname} @dots{}]
5398 @end example
5399
5400 Toggle the values of settings controlling optional shell behavior.
5401 The settings can be either those listed below, or, if the
5402 @option{-o} option is used, those available with the @option{-o}
5403 option to the @code{set} builtin command (@pxref{The Set Builtin}).
5404 With no options, or with the @option{-p} option, a list of all settable
5405 options is displayed, with an indication of whether or not each is set;
5406 if @var{optname}s are supplied, the output is restricted to those options.
5407 The @option{-p} option causes output to be displayed in a form that
5408 may be reused as input.
5409 Other options have the following meanings:
5410
5411 @table @code
5412 @item -s
5413 Enable (set) each @var{optname}.
5414
5415 @item -u
5416 Disable (unset) each @var{optname}.
5417
5418 @item -q
5419 Suppresses normal output; the return status
5420 indicates whether the @var{optname} is set or unset.
5421 If multiple @var{optname} arguments are given with @option{-q},
5422 the return status is zero if all @var{optname}s are enabled;
5423 non-zero otherwise.
5424
5425 @item -o
5426 Restricts the values of
5427 @var{optname} to be those defined for the @option{-o} option to the
5428 @code{set} builtin (@pxref{The Set Builtin}).
5429 @end table
5430
5431 If either @option{-s} or @option{-u}
5432 is used with no @var{optname} arguments, @code{shopt} shows only
5433 those options which are set or unset, respectively.
5434
5435 Unless otherwise noted, the @code{shopt} options are disabled (off)
5436 by default.
5437
5438 The return status when listing options is zero if all @var{optname}s
5439 are enabled, non-zero otherwise. When setting or unsetting options,
5440 the return status is zero unless an @var{optname} is not a valid shell
5441 option.
5442
5443 The list of @code{shopt} options is:
5444 @table @code
5445
5446 @item assoc_expand_once
5447 If set, the shell suppresses multiple evaluation of associative array
5448 subscripts during arithmetic expression evaluation, while executing
5449 builtins that can perform variable assignments,
5450 and while executing builtins that perform array dereferencing.
5451
5452 @item autocd
5453 If set, a command name that is the name of a directory is executed as if
5454 it were the argument to the @code{cd} command.
5455 This option is only used by interactive shells.
5456
5457 @item cdable_vars
5458 If this is set, an argument to the @code{cd} builtin command that
5459 is not a directory is assumed to be the name of a variable whose
5460 value is the directory to change to.
5461
5462 @item cdspell
5463 If set, minor errors in the spelling of a directory component in a
5464 @code{cd} command will be corrected.
5465 The errors checked for are transposed characters,
5466 a missing character, and a character too many.
5467 If a correction is found, the corrected path is printed,
5468 and the command proceeds.
5469 This option is only used by interactive shells.
5470
5471 @item checkhash
5472 If this is set, Bash checks that a command found in the hash
5473 table exists before trying to execute it. If a hashed command no
5474 longer exists, a normal path search is performed.
5475
5476 @item checkjobs
5477 If set, Bash lists the status of any stopped and running jobs before
5478 exiting an interactive shell. If any jobs are running, this causes
5479 the exit to be deferred until a second exit is attempted without an
5480 intervening command (@pxref{Job Control}).
5481 The shell always postpones exiting if any jobs are stopped.
5482
5483 @item checkwinsize
5484 If set, Bash checks the window size after each external (non-builtin)
5485 command and, if necessary, updates the values of
5486 @env{LINES} and @env{COLUMNS}.
5487 This option is enabled by default.
5488
5489 @item cmdhist
5490 If set, Bash
5491 attempts to save all lines of a multiple-line
5492 command in the same history entry. This allows
5493 easy re-editing of multi-line commands.
5494 This option is enabled by default, but only has an effect if command
5495 history is enabled (@pxref{Bash History Facilities}).
5496
5497 @item compat31
5498 @itemx compat32
5499 @itemx compat40
5500 @itemx compat41
5501 @itemx compat42
5502 @itemx compat43
5503 @itemx compat44
5504 These control aspects of the shell's compatibility mode
5505 (@pxref{Shell Compatibility Mode}).
5506
5507 @item complete_fullquote
5508 If set, Bash
5509 quotes all shell metacharacters in filenames and directory names when
5510 performing completion.
5511 If not set, Bash
5512 removes metacharacters such as the dollar sign from the set of
5513 characters that will be quoted in completed filenames
5514 when these metacharacters appear in shell variable references in words to be
5515 completed.
5516 This means that dollar signs in variable names that expand to directories
5517 will not be quoted;
5518 however, any dollar signs appearing in filenames will not be quoted, either.
5519 This is active only when bash is using backslashes to quote completed
5520 filenames.
5521 This variable is set by default, which is the default Bash behavior in
5522 versions through 4.2.
5523
5524 @item direxpand
5525 If set, Bash
5526 replaces directory names with the results of word expansion when performing
5527 filename completion. This changes the contents of the readline editing
5528 buffer.
5529 If not set, Bash attempts to preserve what the user typed.
5530
5531 @item dirspell
5532 If set, Bash
5533 attempts spelling correction on directory names during word completion
5534 if the directory name initially supplied does not exist.
5535
5536 @item dotglob
5537 If set, Bash includes filenames beginning with a `.' in
5538 the results of filename expansion.
5539 The filenames @samp{.} and @samp{..} must always be matched explicitly,
5540 even if @code{dotglob} is set.
5541
5542 @item execfail
5543 If this is set, a non-interactive shell will not exit if
5544 it cannot execute the file specified as an argument to the @code{exec}
5545 builtin command. An interactive shell does not exit if @code{exec}
5546 fails.
5547
5548 @item expand_aliases
5549 If set, aliases are expanded as described below under Aliases,
5550 @ref{Aliases}.
5551 This option is enabled by default for interactive shells.
5552
5553 @item extdebug
5554 If set at shell invocation,
5555 or in a shell startup file,
5556 arrange to execute the debugger profile
5557 before the shell starts, identical to the @option{--debugger} option.
5558 If set after invocation, behavior intended for use by debuggers is enabled:
5559
5560 @enumerate
5561 @item
5562 The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins})
5563 displays the source file name and line number corresponding to each function
5564 name supplied as an argument.
5565
5566 @item
5567 If the command run by the @code{DEBUG} trap returns a non-zero value, the
5568 next command is skipped and not executed.
5569
5570 @item
5571 If the command run by the @code{DEBUG} trap returns a value of 2, and the
5572 shell is executing in a subroutine (a shell function or a shell script
5573 executed by the @code{.} or @code{source} builtins), the shell simulates
5574 a call to @code{return}.
5575
5576 @item
5577 @code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their
5578 descriptions (@pxref{Bash Variables}).
5579
5580 @item
5581 Function tracing is enabled: command substitution, shell functions, and
5582 subshells invoked with @code{( @var{command} )} inherit the
5583 @code{DEBUG} and @code{RETURN} traps.
5584
5585 @item
5586 Error tracing is enabled: command substitution, shell functions, and
5587 subshells invoked with @code{( @var{command} )} inherit the
5588 @code{ERR} trap.
5589 @end enumerate
5590
5591 @item extglob
5592 If set, the extended pattern matching features described above
5593 (@pxref{Pattern Matching}) are enabled.
5594
5595 @item extquote
5596 If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is
5597 performed within @code{$@{@var{parameter}@}} expansions
5598 enclosed in double quotes. This option is enabled by default.
5599
5600 @item failglob
5601 If set, patterns which fail to match filenames during filename expansion
5602 result in an expansion error.
5603
5604 @item force_fignore
5605 If set, the suffixes specified by the @env{FIGNORE} shell variable
5606 cause words to be ignored when performing word completion even if
5607 the ignored words are the only possible completions.
5608 @xref{Bash Variables}, for a description of @env{FIGNORE}.
5609 This option is enabled by default.
5610
5611 @item globasciiranges
5612 If set, range expressions used in pattern matching bracket expressions
5613 (@pxref{Pattern Matching})
5614 behave as if in the traditional C locale when performing
5615 comparisons. That is, the current locale's collating sequence
5616 is not taken into account, so
5617 @samp{b} will not collate between @samp{A} and @samp{B},
5618 and upper-case and lower-case ASCII characters will collate together.
5619
5620 @item globstar
5621 If set, the pattern @samp{**} used in a filename expansion context will
5622 match all files and zero or more directories and subdirectories.
5623 If the pattern is followed by a @samp{/}, only directories and
5624 subdirectories match.
5625
5626 @item gnu_errfmt
5627 If set, shell error messages are written in the standard @sc{gnu} error
5628 message format.
5629
5630 @item histappend
5631 If set, the history list is appended to the file named by the value
5632 of the @env{HISTFILE}
5633 variable when the shell exits, rather than overwriting the file.
5634
5635 @item histreedit
5636 If set, and Readline
5637 is being used, a user is given the opportunity to re-edit a
5638 failed history substitution.
5639
5640 @item histverify
5641 If set, and Readline
5642 is being used, the results of history substitution are not immediately
5643 passed to the shell parser. Instead, the resulting line is loaded into
5644 the Readline editing buffer, allowing further modification.
5645
5646 @item hostcomplete
5647 If set, and Readline is being used, Bash will attempt to perform
5648 hostname completion when a word containing a @samp{@@} is being
5649 completed (@pxref{Commands For Completion}). This option is enabled
5650 by default.
5651
5652 @item huponexit
5653 If set, Bash will send @code{SIGHUP} to all jobs when an interactive
5654 login shell exits (@pxref{Signals}).
5655
5656 @item inherit_errexit
5657 If set, command substitution inherits the value of the @code{errexit} option,
5658 instead of unsetting it in the subshell environment.
5659 This option is enabled when @sc{posix} mode is enabled.
5660
5661 @item interactive_comments
5662 Allow a word beginning with @samp{#}
5663 to cause that word and all remaining characters on that
5664 line to be ignored in an interactive shell.
5665 This option is enabled by default.
5666
5667 @item lastpipe
5668 If set, and job control is not active, the shell runs the last command of
5669 a pipeline not executed in the background in the current shell environment.
5670
5671 @item lithist
5672 If enabled, and the @code{cmdhist}
5673 option is enabled, multi-line commands are saved to the history with
5674 embedded newlines rather than using semicolon separators where possible.
5675
5676 @item localvar_inherit
5677 If set, local variables inherit the value and attributes of a variable of
5678 the same name that exists at a previous scope before any new value is
5679 assigned. The @code{nameref} attribute is not inherited.
5680
5681 @item localvar_unset
5682 If set, calling @code{unset} on local variables in previous function scopes
5683 marks them so subsequent lookups find them unset until that function
5684 returns. This is identical to the behavior of unsetting local variables
5685 at the current function scope.
5686
5687 @item login_shell
5688 The shell sets this option if it is started as a login shell
5689 (@pxref{Invoking Bash}).
5690 The value may not be changed.
5691
5692 @item mailwarn
5693 If set, and a file that Bash is checking for mail has been
5694 accessed since the last time it was checked, the message
5695 @code{"The mail in @var{mailfile} has been read"} is displayed.
5696
5697 @item no_empty_cmd_completion
5698 If set, and Readline is being used, Bash will not attempt to search
5699 the @env{PATH} for possible completions when completion is attempted
5700 on an empty line.
5701
5702 @item nocaseglob
5703 If set, Bash matches filenames in a case-insensitive fashion when
5704 performing filename expansion.
5705
5706 @item nocasematch
5707 If set, Bash matches patterns in a case-insensitive fashion when
5708 performing matching while executing @code{case} or @code{[[}
5709 conditional commands (@pxref{Conditional Constructs},
5710 when performing pattern substitution word expansions,
5711 or when filtering possible completions as part of programmable completion.
5712
5713 @item nullglob
5714 If set, Bash allows filename patterns which match no
5715 files to expand to a null string, rather than themselves.
5716
5717 @item progcomp
5718 If set, the programmable completion facilities
5719 (@pxref{Programmable Completion}) are enabled.
5720 This option is enabled by default.
5721
5722 @item progcomp_alias
5723 If set, and programmable completion is enabled, Bash treats a command
5724 name that doesn't have any completions as a possible alias and attempts
5725 alias expansion. If it has an alias, Bash attempts programmable
5726 completion using the command word resulting from the expanded alias.
5727
5728 @item promptvars
5729 If set, prompt strings undergo
5730 parameter expansion, command substitution, arithmetic
5731 expansion, and quote removal after being expanded
5732 as described below (@pxref{Controlling the Prompt}).
5733 This option is enabled by default.
5734
5735 @item restricted_shell
5736 The shell sets this option if it is started in restricted mode
5737 (@pxref{The Restricted Shell}).
5738 The value may not be changed.
5739 This is not reset when the startup files are executed, allowing
5740 the startup files to discover whether or not a shell is restricted.
5741
5742 @item shift_verbose
5743 If this is set, the @code{shift}
5744 builtin prints an error message when the shift count exceeds the
5745 number of positional parameters.
5746
5747 @item sourcepath
5748 If set, the @code{source} builtin uses the value of @env{PATH}
5749 to find the directory containing the file supplied as an argument.
5750 This option is enabled by default.
5751
5752 @item xpg_echo
5753 If set, the @code{echo} builtin expands backslash-escape sequences
5754 by default.
5755
5756 @end table
5757 @end table
5758
5759 @node Special Builtins
5760 @section Special Builtins
5761 @cindex special builtin
5762
5763 For historical reasons, the @sc{posix} standard has classified
5764 several builtin commands as @emph{special}.
5765 When Bash is executing in @sc{posix} mode, the special builtins
5766 differ from other builtin commands in three respects:
5767
5768 @enumerate
5769 @item
5770 Special builtins are found before shell functions during command lookup.
5771
5772 @item
5773 If a special builtin returns an error status, a non-interactive shell exits.
5774
5775 @item
5776 Assignment statements preceding the command stay in effect in the shell
5777 environment after the command completes.
5778 @end enumerate
5779
5780 When Bash is not executing in @sc{posix} mode, these builtins behave no
5781 differently than the rest of the Bash builtin commands.
5782 The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}.
5783
5784 These are the @sc{posix} special builtins:
5785 @example
5786 @w{break : . continue eval exec exit export readonly return set}
5787 @w{shift trap unset}
5788 @end example
5789
5790 @node Shell Variables
5791 @chapter Shell Variables
5792
5793 @menu
5794 * Bourne Shell Variables:: Variables which Bash uses in the same way
5795 as the Bourne Shell.
5796 * Bash Variables:: List of variables that exist in Bash.
5797 @end menu
5798
5799 This chapter describes the shell variables that Bash uses.
5800 Bash automatically assigns default values to a number of variables.
5801
5802 @node Bourne Shell Variables
5803 @section Bourne Shell Variables
5804
5805 Bash uses certain shell variables in the same way as the Bourne shell.
5806 In some cases, Bash assigns a default value to the variable.
5807
5808 @vtable @code
5809
5810 @item CDPATH
5811 A colon-separated list of directories used as a search path for
5812 the @code{cd} builtin command.
5813
5814 @item HOME
5815 The current user's home directory; the default for the @code{cd} builtin
5816 command.
5817 The value of this variable is also used by tilde expansion
5818 (@pxref{Tilde Expansion}).
5819
5820 @item IFS
5821 A list of characters that separate fields; used when the shell splits
5822 words as part of expansion.
5823
5824 @item MAIL
5825 If this parameter is set to a filename or directory name
5826 and the @env{MAILPATH} variable
5827 is not set, Bash informs the user of the arrival of mail in
5828 the specified file or Maildir-format directory.
5829
5830 @item MAILPATH
5831 A colon-separated list of filenames which the shell periodically checks
5832 for new mail.
5833 Each list entry can specify the message that is printed when new mail
5834 arrives in the mail file by separating the filename from the message with
5835 a @samp{?}.
5836 When used in the text of the message, @code{$_} expands to the name of
5837 the current mail file.
5838
5839 @item OPTARG
5840 The value of the last option argument processed by the @code{getopts} builtin.
5841
5842 @item OPTIND
5843 The index of the last option argument processed by the @code{getopts} builtin.
5844
5845 @item PATH
5846 A colon-separated list of directories in which the shell looks for
5847 commands.
5848 A zero-length (null) directory name in the value of @code{PATH} indicates the
5849 current directory.
5850 A null directory name may appear as two adjacent colons, or as an initial
5851 or trailing colon.
5852
5853 @item PS1
5854 The primary prompt string. The default value is @samp{\s-\v\$ }.
5855 @xref{Controlling the Prompt}, for the complete list of escape
5856 sequences that are expanded before @env{PS1} is displayed.
5857
5858 @item PS2
5859 The secondary prompt string. The default value is @samp{> }.
5860 @env{PS2} is expanded in the same way as @env{PS1} before being
5861 displayed.
5862
5863 @end vtable
5864
5865 @node Bash Variables
5866 @section Bash Variables
5867
5868 These variables are set or used by Bash, but other shells
5869 do not normally treat them specially.
5870
5871 A few variables used by Bash are described in different chapters:
5872 variables for controlling the job control facilities
5873 (@pxref{Job Control Variables}).
5874
5875 @vtable @code
5876
5877 @item _
5878 @vindex $_
5879 ($_, an underscore.)
5880 At shell startup, set to the pathname used to invoke the
5881 shell or shell script being executed as passed in the environment
5882 or argument list.
5883 Subsequently, expands to the last argument to the previous simple
5884 command executed in the foreground, after expansion.
5885 Also set to the full pathname used to invoke each command executed
5886 and placed in the environment exported to that command.
5887 When checking mail, this parameter holds the name of the mail file.
5888
5889 @item BASH
5890 The full pathname used to execute the current instance of Bash.
5891
5892 @item BASHOPTS
5893 A colon-separated list of enabled shell options. Each word in
5894 the list is a valid argument for the @option{-s} option to the
5895 @code{shopt} builtin command (@pxref{The Shopt Builtin}).
5896 The options appearing in @env{BASHOPTS} are those reported
5897 as @samp{on} by @samp{shopt}.
5898 If this variable is in the environment when Bash
5899 starts up, each shell option in the list will be enabled before
5900 reading any startup files. This variable is readonly.
5901
5902 @item BASHPID
5903 Expands to the process ID of the current Bash process.
5904 This differs from @code{$$} under certain circumstances, such as subshells
5905 that do not require Bash to be re-initialized.
5906 Assignments to @env{BASHPID} have no effect.
5907 If @env{BASHPID}
5908 is unset, it loses its special properties, even if it is
5909 subsequently reset.
5910
5911 @item BASH_ALIASES
5912 An associative array variable whose members correspond to the internal
5913 list of aliases as maintained by the @code{alias} builtin.
5914 (@pxref{Bourne Shell Builtins}).
5915 Elements added to this array appear in the alias list; however,
5916 unsetting array elements currently does not cause aliases to be removed
5917 from the alias list.
5918 If @env{BASH_ALIASES}
5919 is unset, it loses its special properties, even if it is
5920 subsequently reset.
5921
5922 @item BASH_ARGC
5923 An array variable whose values are the number of parameters in each
5924 frame of the current bash execution call stack. The number of
5925 parameters to the current subroutine (shell function or script executed
5926 with @code{.} or @code{source}) is at the top of the stack. When a
5927 subroutine is executed, the number of parameters passed is pushed onto
5928 @code{BASH_ARGC}.
5929 The shell sets @code{BASH_ARGC} only when in extended debugging mode
5930 (see @ref{The Shopt Builtin}
5931 for a description of the @code{extdebug} option to the @code{shopt}
5932 builtin).
5933 Setting @code{extdebug} after the shell has started to execute a script,
5934 or referencing this variable when @code{extdebug} is not set,
5935 may result in inconsistent values.
5936
5937 @item BASH_ARGV
5938 An array variable containing all of the parameters in the current bash
5939 execution call stack. The final parameter of the last subroutine call
5940 is at the top of the stack; the first parameter of the initial call is
5941 at the bottom. When a subroutine is executed, the parameters supplied
5942 are pushed onto @code{BASH_ARGV}.
5943 The shell sets @code{BASH_ARGV} only when in extended debugging mode
5944 (see @ref{The Shopt Builtin}
5945 for a description of the @code{extdebug} option to the @code{shopt}
5946 builtin).
5947 Setting @code{extdebug} after the shell has started to execute a script,
5948 or referencing this variable when @code{extdebug} is not set,
5949 may result in inconsistent values.
5950
5951 @item BASH_ARGV0
5952 When referenced, this variable expands to the name of the shell or shell
5953 script (identical to @code{$0}; @xref{Special Parameters},
5954 for the description of special parameter 0).
5955 Assignment to @code{BASH_ARGV0}
5956 causes the value assigned to also be assigned to @code{$0}.
5957 If @env{BASH_ARGV0}
5958 is unset, it loses its special properties, even if it is
5959 subsequently reset.
5960
5961 @item BASH_CMDS
5962 An associative array variable whose members correspond to the internal
5963 hash table of commands as maintained by the @code{hash} builtin
5964 (@pxref{Bourne Shell Builtins}).
5965 Elements added to this array appear in the hash table; however,
5966 unsetting array elements currently does not cause command names to be removed
5967 from the hash table.
5968 If @env{BASH_CMDS}
5969 is unset, it loses its special properties, even if it is
5970 subsequently reset.
5971
5972 @item BASH_COMMAND
5973 The command currently being executed or about to be executed, unless the
5974 shell is executing a command as the result of a trap,
5975 in which case it is the command executing at the time of the trap.
5976 If @env{BASH_COMMAND}
5977 is unset, it loses its special properties, even if it is
5978 subsequently reset.
5979
5980 @item BASH_COMPAT
5981 The value is used to set the shell's compatibility level.
5982 @xref{Shell Compatibility Mode}, for a description of the various
5983 compatibility levels and their effects.
5984 The value may be a decimal number (e.g., 4.2) or an integer (e.g., 42)
5985 corresponding to the desired compatibility level.
5986 If @env{BASH_COMPAT} is unset or set to the empty string, the compatibility
5987 level is set to the default for the current version.
5988 If @env{BASH_COMPAT} is set to a value that is not one of the valid
5989 compatibility levels, the shell prints an error message and sets the
5990 compatibility level to the default for the current version.
5991 The valid values correspond to the compatibility levels
5992 described below (@pxref{Shell Compatibility Mode}).
5993 For example, 4.2 and 42 are valid values that correspond
5994 to the @code{compat42} @code{shopt} option
5995 and set the compatibility level to 42.
5996 The current version is also a valid value.
5997
5998 @item BASH_ENV
5999 If this variable is set when Bash is invoked to execute a shell
6000 script, its value is expanded and used as the name of a startup file
6001 to read before executing the script. @xref{Bash Startup Files}.
6002
6003 @item BASH_EXECUTION_STRING
6004 The command argument to the @option{-c} invocation option.
6005
6006 @item BASH_LINENO
6007 An array variable whose members are the line numbers in source files
6008 where each corresponding member of @env{FUNCNAME} was invoked.
6009 @code{$@{BASH_LINENO[$i]@}} is the line number in the source file
6010 (@code{$@{BASH_SOURCE[$i+1]@}}) where
6011 @code{$@{FUNCNAME[$i]@}} was called (or @code{$@{BASH_LINENO[$i-1]@}} if
6012 referenced within another shell function).
6013 Use @code{LINENO} to obtain the current line number.
6014
6015 @item BASH_LOADABLES_PATH
6016 A colon-separated list of directories in which the shell looks for
6017 dynamically loadable builtins specified by the
6018 @code{enable} command.
6019
6020 @item BASH_REMATCH
6021 An array variable whose members are assigned by the @samp{=~} binary
6022 operator to the @code{[[} conditional command
6023 (@pxref{Conditional Constructs}).
6024 The element with index 0 is the portion of the string
6025 matching the entire regular expression.
6026 The element with index @var{n} is the portion of the
6027 string matching the @var{n}th parenthesized subexpression.
6028
6029 @item BASH_SOURCE
6030 An array variable whose members are the source filenames where the
6031 corresponding shell function names in the @code{FUNCNAME} array
6032 variable are defined.
6033 The shell function @code{$@{FUNCNAME[$i]@}} is defined in the file
6034 @code{$@{BASH_SOURCE[$i]@}} and called from @code{$@{BASH_SOURCE[$i+1]@}}
6035
6036 @item BASH_SUBSHELL
6037 Incremented by one within each subshell or subshell environment when
6038 the shell begins executing in that environment.
6039 The initial value is 0.
6040 If @env{BASH_SUBSHELL}
6041 is unset, it loses its special properties, even if it is
6042 subsequently reset.
6043
6044 @item BASH_VERSINFO
6045 A readonly array variable (@pxref{Arrays})
6046 whose members hold version information for this instance of Bash.
6047 The values assigned to the array members are as follows:
6048
6049 @table @code
6050
6051 @item BASH_VERSINFO[0]
6052 The major version number (the @dfn{release}).
6053
6054 @item BASH_VERSINFO[1]
6055 The minor version number (the @dfn{version}).
6056
6057 @item BASH_VERSINFO[2]
6058 The patch level.
6059
6060 @item BASH_VERSINFO[3]
6061 The build version.
6062
6063 @item BASH_VERSINFO[4]
6064 The release status (e.g., @code{beta1}).
6065
6066 @item BASH_VERSINFO[5]
6067 The value of @env{MACHTYPE}.
6068 @end table
6069
6070 @item BASH_VERSION
6071 The version number of the current instance of Bash.
6072
6073 @item BASH_XTRACEFD
6074 If set to an integer corresponding to a valid file descriptor, Bash
6075 will write the trace output generated when @samp{set -x}
6076 is enabled to that file descriptor.
6077 This allows tracing output to be separated from diagnostic and error
6078 messages.
6079 The file descriptor is closed when @code{BASH_XTRACEFD} is unset or assigned
6080 a new value.
6081 Unsetting @code{BASH_XTRACEFD} or assigning it the empty string causes the
6082 trace output to be sent to the standard error.
6083 Note that setting @code{BASH_XTRACEFD} to 2 (the standard error file
6084 descriptor) and then unsetting it will result in the standard error
6085 being closed.
6086
6087 @item CHILD_MAX
6088 Set the number of exited child status values for the shell to remember.
6089 Bash will not allow this value to be decreased below a @sc{posix}-mandated
6090 minimum, and there is a maximum value (currently 8192) that this may
6091 not exceed.
6092 The minimum value is system-dependent.
6093
6094 @item COLUMNS
6095 Used by the @code{select} command to determine the terminal width
6096 when printing selection lists.
6097 Automatically set if the @code{checkwinsize} option is enabled
6098 (@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
6099 @code{SIGWINCH}.
6100
6101 @item COMP_CWORD
6102 An index into @env{$@{COMP_WORDS@}} of the word containing the current
6103 cursor position.
6104 This variable is available only in shell functions invoked by the
6105 programmable completion facilities (@pxref{Programmable Completion}).
6106
6107 @item COMP_LINE
6108 The current command line.
6109 This variable is available only in shell functions and external
6110 commands invoked by the
6111 programmable completion facilities (@pxref{Programmable Completion}).
6112
6113 @item COMP_POINT
6114 The index of the current cursor position relative to the beginning of
6115 the current command.
6116 If the current cursor position is at the end of the current command,
6117 the value of this variable is equal to @code{$@{#COMP_LINE@}}.
6118 This variable is available only in shell functions and external
6119 commands invoked by the
6120 programmable completion facilities (@pxref{Programmable Completion}).
6121
6122 @item COMP_TYPE
6123 Set to an integer value corresponding to the type of completion attempted
6124 that caused a completion function to be called:
6125 @key{TAB}, for normal completion,
6126 @samp{?}, for listing completions after successive tabs,
6127 @samp{!}, for listing alternatives on partial word completion,
6128 @samp{@@}, to list completions if the word is not unmodified,
6129 or
6130 @samp{%}, for menu completion.
6131 This variable is available only in shell functions and external
6132 commands invoked by the
6133 programmable completion facilities (@pxref{Programmable Completion}).
6134
6135 @item COMP_KEY
6136 The key (or final key of a key sequence) used to invoke the current
6137 completion function.
6138
6139 @item COMP_WORDBREAKS
6140 The set of characters that the Readline library treats as word
6141 separators when performing word completion.
6142 If @env{COMP_WORDBREAKS}
6143 is unset, it loses its special properties,
6144 even if it is subsequently reset.
6145
6146 @item COMP_WORDS
6147 An array variable consisting of the individual
6148 words in the current command line.
6149 The line is split into words as Readline would split it, using
6150 @code{COMP_WORDBREAKS} as described above.
6151 This variable is available only in shell functions invoked by the
6152 programmable completion facilities (@pxref{Programmable Completion}).
6153
6154 @item COMPREPLY
6155 An array variable from which Bash reads the possible completions
6156 generated by a shell function invoked by the programmable completion
6157 facility (@pxref{Programmable Completion}).
6158 Each array element contains one possible completion.
6159
6160 @item COPROC
6161 An array variable created to hold the file descriptors
6162 for output from and input to an unnamed coprocess (@pxref{Coprocesses}).
6163
6164 @item DIRSTACK
6165 An array variable containing the current contents of the directory stack.
6166 Directories appear in the stack in the order they are displayed by the
6167 @code{dirs} builtin.
6168 Assigning to members of this array variable may be used to modify
6169 directories already in the stack, but the @code{pushd} and @code{popd}
6170 builtins must be used to add and remove directories.
6171 Assignment to this variable will not change the current directory.
6172 If @env{DIRSTACK}
6173 is unset, it loses its special properties, even if
6174 it is subsequently reset.
6175
6176 @item EMACS
6177 If Bash finds this variable in the environment when the shell
6178 starts with value @samp{t}, it assumes that the shell is running in an
6179 Emacs shell buffer and disables line editing.
6180
6181 @item ENV
6182 Expanded and executed similarlty to @code{BASH_ENV}
6183 (@pxref{Bash Startup Files})
6184 when an interactive shell is invoked in
6185 @sc{posix} Mode (@pxref{Bash POSIX Mode}).
6186
6187 @item EPOCHREALTIME
6188 Each time this parameter is referenced, it expands to the number of seconds
6189 since the Unix Epoch as a floating point value with micro-second granularity
6190 (see the documentation for the C library function @code{time} for the
6191 definition of Epoch).
6192 Assignments to @env{EPOCHREALTIME} are ignored.
6193 If @env{EPOCHREALTIME}
6194 is unset, it loses its special properties, even if
6195 it is subsequently reset.
6196
6197 @item EPOCHSECONDS
6198 Each time this parameter is referenced, it expands to the number of seconds
6199 since the Unix Epoch (see the documentation for the C library function
6200 @code{time} for the definition of Epoch).
6201 Assignments to @env{EPOCHSECONDS} are ignored.
6202 If @env{EPOCHSECONDS}
6203 is unset, it loses its special properties, even if
6204 it is subsequently reset.
6205
6206 @item EUID
6207 The numeric effective user id of the current user. This variable
6208 is readonly.
6209
6210 @item EXECIGNORE
6211 A colon-separated list of shell patterns (@pxref{Pattern Matching})
6212 defining the list of filenames to be ignored by command search using
6213 @code{PATH}.
6214 Files whose full pathnames match one of these patterns are not considered
6215 executable files for the purposes of completion and command execution
6216 via @code{PATH} lookup.
6217 This does not affect the behavior of the @code{[}, @code{test}, and @code{[[}
6218 commands.
6219 Full pathnames in the command hash table are not subject to @code{EXECIGNORE}.
6220 Use this variable to ignore shared library files that have the executable
6221 bit set, but are not executable files.
6222 The pattern matching honors the setting of the @code{extglob} shell
6223 option.
6224
6225 @item FCEDIT
6226 The editor used as a default by the @option{-e} option to the @code{fc}
6227 builtin command.
6228
6229 @item FIGNORE
6230 A colon-separated list of suffixes to ignore when performing
6231 filename completion.
6232 A filename whose suffix matches one of the entries in
6233 @env{FIGNORE}
6234 is excluded from the list of matched filenames. A sample
6235 value is @samp{.o:~}
6236
6237 @item FUNCNAME
6238 An array variable containing the names of all shell functions
6239 currently in the execution call stack.
6240 The element with index 0 is the name of any currently-executing
6241 shell function.
6242 The bottom-most element (the one with the highest index)
6243 is @code{"main"}.
6244 This variable exists only when a shell function is executing.
6245 Assignments to @env{FUNCNAME} have no effect.
6246 If @env{FUNCNAME}
6247 is unset, it loses its special properties, even if
6248 it is subsequently reset.
6249
6250 This variable can be used with @code{BASH_LINENO} and @code{BASH_SOURCE}.
6251 Each element of @code{FUNCNAME} has corresponding elements in
6252 @code{BASH_LINENO} and @code{BASH_SOURCE} to describe the call stack.
6253 For instance, @code{$@{FUNCNAME[$i]@}} was called from the file
6254 @code{$@{BASH_SOURCE[$i+1]@}} at line number @code{$@{BASH_LINENO[$i]@}}.
6255 The @code{caller} builtin displays the current call stack using this
6256 information.
6257
6258 @item FUNCNEST
6259 If set to a numeric value greater than 0, defines a maximum function
6260 nesting level. Function invocations that exceed this nesting level
6261 will cause the current command to abort.
6262
6263 @item GLOBIGNORE
6264 A colon-separated list of patterns defining the set of file names to
6265 be ignored by filename expansion.
6266 If a file name matched by a filename expansion pattern also matches one
6267 of the patterns in @env{GLOBIGNORE}, it is removed from the list
6268 of matches.
6269 The pattern matching honors the setting of the @code{extglob} shell
6270 option.
6271
6272 @item GROUPS
6273 An array variable containing the list of groups of which the current
6274 user is a member.
6275 Assignments to @env{GROUPS} have no effect.
6276 If @env{GROUPS}
6277 is unset, it loses its special properties, even if it is
6278 subsequently reset.
6279
6280 @item histchars
6281 Up to three characters which control history expansion, quick
6282 substitution, and tokenization (@pxref{History Interaction}).
6283 The first character is the
6284 @dfn{history expansion} character, that is, the character which signifies the
6285 start of a history expansion, normally @samp{!}. The second character is the
6286 character which signifies `quick substitution' when seen as the first
6287 character on a line, normally @samp{^}. The optional third character is the
6288 character which indicates that the remainder of the line is a comment when
6289 found as the first character of a word, usually @samp{#}. The history
6290 comment character causes history substitution to be skipped for the
6291 remaining words on the line. It does not necessarily cause the shell
6292 parser to treat the rest of the line as a comment.
6293
6294 @item HISTCMD
6295 The history number, or index in the history list, of the current
6296 command.
6297 Assignments to @env{HISTCMD} are ignored.
6298 If @env{HISTCMD}
6299 is unset, it loses its special properties,
6300 even if it is subsequently reset.
6301
6302 @item HISTCONTROL
6303 A colon-separated list of values controlling how commands are saved on
6304 the history list.
6305 If the list of values includes @samp{ignorespace}, lines which begin
6306 with a space character are not saved in the history list.
6307 A value of @samp{ignoredups} causes lines which match the previous
6308 history entry to not be saved.
6309 A value of @samp{ignoreboth} is shorthand for
6310 @samp{ignorespace} and @samp{ignoredups}.
6311 A value of @samp{erasedups} causes all previous lines matching the
6312 current line to be removed from the history list before that line
6313 is saved.
6314 Any value not in the above list is ignored.
6315 If @env{HISTCONTROL} is unset, or does not include a valid value,
6316 all lines read by the shell parser are saved on the history list,
6317 subject to the value of @env{HISTIGNORE}.
6318 The second and subsequent lines of a multi-line compound command are
6319 not tested, and are added to the history regardless of the value of
6320 @env{HISTCONTROL}.
6321
6322 @item HISTFILE
6323 The name of the file to which the command history is saved. The
6324 default value is @file{~/.bash_history}.
6325
6326 @item HISTFILESIZE
6327 The maximum number of lines contained in the history file.
6328 When this variable is assigned a value, the history file is truncated,
6329 if necessary, to contain no more than that number of lines
6330 by removing the oldest entries.
6331 The history file is also truncated to this size after
6332 writing it when a shell exits.
6333 If the value is 0, the history file is truncated to zero size.
6334 Non-numeric values and numeric values less than zero inhibit truncation.
6335 The shell sets the default value to the value of @env{HISTSIZE}
6336 after reading any startup files.
6337
6338 @item HISTIGNORE
6339 A colon-separated list of patterns used to decide which command
6340 lines should be saved on the history list. Each pattern is
6341 anchored at the beginning of the line and must match the complete
6342 line (no implicit @samp{*} is appended). Each pattern is tested
6343 against the line after the checks specified by @env{HISTCONTROL}
6344 are applied. In addition to the normal shell pattern matching
6345 characters, @samp{&} matches the previous history line. @samp{&}
6346 may be escaped using a backslash; the backslash is removed
6347 before attempting a match.
6348 The second and subsequent lines of a multi-line compound command are
6349 not tested, and are added to the history regardless of the value of
6350 @env{HISTIGNORE}.
6351 The pattern matching honors the setting of the @code{extglob} shell
6352 option.
6353
6354 @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A
6355 pattern of @samp{&} is identical to @code{ignoredups}, and a
6356 pattern of @samp{[ ]*} is identical to @code{ignorespace}.
6357 Combining these two patterns, separating them with a colon,
6358 provides the functionality of @code{ignoreboth}.
6359
6360 @item HISTSIZE
6361 The maximum number of commands to remember on the history list.
6362 If the value is 0, commands are not saved in the history list.
6363 Numeric values less than zero result in every command being saved
6364 on the history list (there is no limit).
6365 The shell sets the default value to 500 after reading any startup files.
6366
6367 @item HISTTIMEFORMAT
6368 If this variable is set and not null, its value is used as a format string
6369 for @code{strftime} to print the time stamp associated with each history
6370 entry displayed by the @code{history} builtin.
6371 If this variable is set, time stamps are written to the history file so
6372 they may be preserved across shell sessions.
6373 This uses the history comment character to distinguish timestamps from
6374 other history lines.
6375
6376 @item HOSTFILE
6377 Contains the name of a file in the same format as @file{/etc/hosts} that
6378 should be read when the shell needs to complete a hostname.
6379 The list of possible hostname completions may be changed while the shell
6380 is running;
6381 the next time hostname completion is attempted after the
6382 value is changed, Bash adds the contents of the new file to the
6383 existing list.
6384 If @env{HOSTFILE} is set, but has no value, or does not name a readable file,
6385 Bash attempts to read
6386 @file{/etc/hosts} to obtain the list of possible hostname completions.
6387 When @env{HOSTFILE} is unset, the hostname list is cleared.
6388
6389 @item HOSTNAME
6390 The name of the current host.
6391
6392 @item HOSTTYPE
6393 A string describing the machine Bash is running on.
6394
6395 @item IGNOREEOF
6396 Controls the action of the shell on receipt of an @code{EOF} character
6397 as the sole input. If set, the value denotes the number
6398 of consecutive @code{EOF} characters that can be read as the
6399 first character on an input line
6400 before the shell will exit. If the variable exists but does not
6401 have a numeric value, or has no value, then the default is 10.
6402 If the variable does not exist, then @code{EOF} signifies the end of
6403 input to the shell. This is only in effect for interactive shells.
6404
6405 @item INPUTRC
6406 The name of the Readline initialization file, overriding the default
6407 of @file{~/.inputrc}.
6408
6409 @item INSIDE_EMACS
6410 If Bash finds this variable in the environment when the shell
6411 starts, it assumes that the shell is running in an Emacs shell buffer
6412 and may disable line editing depending on the value of @env{TERM}.
6413
6414 @item LANG
6415 Used to determine the locale category for any category not specifically
6416 selected with a variable starting with @code{LC_}.
6417
6418 @item LC_ALL
6419 This variable overrides the value of @env{LANG} and any other
6420 @code{LC_} variable specifying a locale category.
6421
6422 @item LC_COLLATE
6423 This variable determines the collation order used when sorting the
6424 results of filename expansion, and
6425 determines the behavior of range expressions, equivalence classes,
6426 and collating sequences within filename expansion and pattern matching
6427 (@pxref{Filename Expansion}).
6428
6429 @item LC_CTYPE
6430 This variable determines the interpretation of characters and the
6431 behavior of character classes within filename expansion and pattern
6432 matching (@pxref{Filename Expansion}).
6433
6434 @item LC_MESSAGES
6435 This variable determines the locale used to translate double-quoted
6436 strings preceded by a @samp{$} (@pxref{Locale Translation}).
6437
6438 @item LC_NUMERIC
6439 This variable determines the locale category used for number formatting.
6440
6441 @item LC_TIME
6442 This variable determines the locale category used for data and time
6443 formatting.
6444
6445 @item LINENO
6446 The line number in the script or shell function currently executing.
6447 If @env{LINENO}
6448 is unset, it loses its special properties, even if it is
6449 subsequently reset.
6450
6451 @item LINES
6452 Used by the @code{select} command to determine the column length
6453 for printing selection lists.
6454 Automatically set if the @code{checkwinsize} option is enabled
6455 (@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
6456 @code{SIGWINCH}.
6457
6458 @item MACHTYPE
6459 A string that fully describes the system type on which Bash
6460 is executing, in the standard @sc{gnu} @var{cpu-company-system} format.
6461
6462 @item MAILCHECK
6463 How often (in seconds) that the shell should check for mail in the
6464 files specified in the @env{MAILPATH} or @env{MAIL} variables.
6465 The default is 60 seconds. When it is time to check
6466 for mail, the shell does so before displaying the primary prompt.
6467 If this variable is unset, or set to a value that is not a number
6468 greater than or equal to zero, the shell disables mail checking.
6469
6470 @item MAPFILE
6471 An array variable created to hold the text read by the
6472 @code{mapfile} builtin when no variable name is supplied.
6473
6474 @item OLDPWD
6475 The previous working directory as set by the @code{cd} builtin.
6476
6477 @item OPTERR
6478 If set to the value 1, Bash displays error messages
6479 generated by the @code{getopts} builtin command.
6480
6481 @item OSTYPE
6482 A string describing the operating system Bash is running on.
6483
6484 @item PIPESTATUS
6485 An array variable (@pxref{Arrays})
6486 containing a list of exit status values from the processes
6487 in the most-recently-executed foreground pipeline (which may
6488 contain only a single command).
6489
6490 @item POSIXLY_CORRECT
6491 If this variable is in the environment when Bash starts, the shell
6492 enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the
6493 startup files, as if the @option{--posix} invocation option had been supplied.
6494 If it is set while the shell is running, Bash enables @sc{posix} mode,
6495 as if the command
6496 @example
6497 @code{set -o posix}
6498 @end example
6499 @noindent
6500 had been executed.
6501 When the shell enters @sc{posix} mode, it sets this variable if it was
6502 not already set.
6503
6504 @item PPID
6505 The process @sc{id} of the shell's parent process. This variable
6506 is readonly.
6507
6508 @item PROMPT_COMMAND
6509 If this variable is set, and is an array,
6510 the value of each set element is interpreted as a command to execute
6511 before printing the primary prompt (@env{$PS1}).
6512 If this is set but not an array variable,
6513 its value is used as a command to execute instead.
6514
6515 @item PROMPT_DIRTRIM
6516 If set to a number greater than zero, the value is used as the number of
6517 trailing directory components to retain when expanding the @code{\w} and
6518 @code{\W} prompt string escapes (@pxref{Controlling the Prompt}).
6519 Characters removed are replaced with an ellipsis.
6520
6521 @item PS0
6522 The value of this parameter is expanded like @env{PS1}
6523 and displayed by interactive shells after reading a command
6524 and before the command is executed.
6525
6526 @item PS3
6527 The value of this variable is used as the prompt for the
6528 @code{select} command. If this variable is not set, the
6529 @code{select} command prompts with @samp{#? }
6530
6531 @item PS4
6532 The value of this parameter is expanded like @env{PS1}
6533 and the expanded value is the prompt printed before the command line
6534 is echoed when the @option{-x} option is set (@pxref{The Set Builtin}).
6535 The first character of the expanded value is replicated multiple times,
6536 as necessary, to indicate multiple levels of indirection.
6537 The default is @samp{+ }.
6538
6539 @item PWD
6540 The current working directory as set by the @code{cd} builtin.
6541
6542 @item RANDOM
6543 Each time this parameter is referenced, it expands to a random integer
6544 between 0 and 32767. Assigning a value to this
6545 variable seeds the random number generator.
6546 If @env{RANDOM}
6547 is unset, it loses its special properties, even if it is
6548 subsequently reset.
6549
6550 @item READLINE_LINE
6551 The contents of the Readline line buffer, for use
6552 with @samp{bind -x} (@pxref{Bash Builtins}).
6553
6554 @item READLINE_MARK
6555 The position of the @dfn{mark} (saved insertion point) in the
6556 Readline line buffer, for use
6557 with @samp{bind -x} (@pxref{Bash Builtins}).
6558 The characters between the insertion point and the mark are often
6559 called the @dfn{region}.
6560
6561 @item READLINE_POINT
6562 The position of the insertion point in the Readline line buffer, for use
6563 with @samp{bind -x} (@pxref{Bash Builtins}).
6564
6565 @item REPLY
6566 The default variable for the @code{read} builtin.
6567
6568 @item SECONDS
6569 This variable expands to the number of seconds since the
6570 shell was started. Assignment to this variable resets
6571 the count to the value assigned, and the expanded value
6572 becomes the value assigned plus the number of seconds
6573 since the assignment.
6574 The number of seconds at shell invocation and the current time is always
6575 determined by querying the system clock.
6576 If @env{SECONDS}
6577 is unset, it loses its special properties,
6578 even if it is subsequently reset.
6579
6580 @item SHELL
6581 This environment variable expands to the full pathname to the shell.
6582 If it is not set when the shell starts,
6583 Bash assigns to it the full pathname of the current user's login shell.
6584
6585 @item SHELLOPTS
6586 A colon-separated list of enabled shell options. Each word in
6587 the list is a valid argument for the @option{-o} option to the
6588 @code{set} builtin command (@pxref{The Set Builtin}).
6589 The options appearing in @env{SHELLOPTS} are those reported
6590 as @samp{on} by @samp{set -o}.
6591 If this variable is in the environment when Bash
6592 starts up, each shell option in the list will be enabled before
6593 reading any startup files. This variable is readonly.
6594
6595 @item SHLVL
6596 Incremented by one each time a new instance of Bash is started. This is
6597 intended to be a count of how deeply your Bash shells are nested.
6598
6599 @item SRANDOM
6600 This variable expands to a 32-bit pseudo-random number each time it is
6601 referenced. The random number generator is not linear on systems that
6602 support @file{/dev/urandom} or @code{arc4random}, so each returned number
6603 has no relationship to the numbers preceding it.
6604 The random number generator cannot be seeded, so assignments to this
6605 variable have no effect.
6606 If @env{SRANDOM}
6607 is unset, it loses its special properties,
6608 even if it is subsequently reset.
6609
6610 @item TIMEFORMAT
6611 The value of this parameter is used as a format string specifying
6612 how the timing information for pipelines prefixed with the @code{time}
6613 reserved word should be displayed.
6614 The @samp{%} character introduces an
6615 escape sequence that is expanded to a time value or other
6616 information.
6617 The escape sequences and their meanings are as
6618 follows; the braces denote optional portions.
6619
6620 @table @code
6621
6622 @item %%
6623 A literal @samp{%}.
6624
6625 @item %[@var{p}][l]R
6626 The elapsed time in seconds.
6627
6628 @item %[@var{p}][l]U
6629 The number of CPU seconds spent in user mode.
6630
6631 @item %[@var{p}][l]S
6632 The number of CPU seconds spent in system mode.
6633
6634 @item %P
6635 The CPU percentage, computed as (%U + %S) / %R.
6636 @end table
6637
6638 The optional @var{p} is a digit specifying the precision, the number of
6639 fractional digits after a decimal point.
6640 A value of 0 causes no decimal point or fraction to be output.
6641 At most three places after the decimal point may be specified; values
6642 of @var{p} greater than 3 are changed to 3.
6643 If @var{p} is not specified, the value 3 is used.
6644
6645 The optional @code{l} specifies a longer format, including minutes, of
6646 the form @var{MM}m@var{SS}.@var{FF}s.
6647 The value of @var{p} determines whether or not the fraction is included.
6648
6649 If this variable is not set, Bash acts as if it had the value
6650 @example
6651 @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'}
6652 @end example
6653 If the value is null, no timing information is displayed.
6654 A trailing newline is added when the format string is displayed.
6655
6656 @item TMOUT
6657 If set to a value greater than zero, @code{TMOUT} is treated as the
6658 default timeout for the @code{read} builtin (@pxref{Bash Builtins}).
6659 The @code{select} command (@pxref{Conditional Constructs}) terminates
6660 if input does not arrive after @code{TMOUT} seconds when input is coming
6661 from a terminal.
6662
6663 In an interactive shell, the value is interpreted as
6664 the number of seconds to wait for a line of input after issuing
6665 the primary prompt.
6666 Bash
6667 terminates after waiting for that number of seconds if a complete
6668 line of input does not arrive.
6669
6670 @item TMPDIR
6671 If set, Bash uses its value as the name of a directory in which
6672 Bash creates temporary files for the shell's use.
6673
6674 @item UID
6675 The numeric real user id of the current user. This variable is readonly.
6676
6677 @end vtable
6678
6679 @node Bash Features
6680 @chapter Bash Features
6681
6682 This chapter describes features unique to Bash.
6683
6684 @menu
6685 * Invoking Bash:: Command line options that you can give
6686 to Bash.
6687 * Bash Startup Files:: When and how Bash executes scripts.
6688 * Interactive Shells:: What an interactive shell is.
6689 * Bash Conditional Expressions:: Primitives used in composing expressions for
6690 the @code{test} builtin.
6691 * Shell Arithmetic:: Arithmetic on shell variables.
6692 * Aliases:: Substituting one command for another.
6693 * Arrays:: Array Variables.
6694 * The Directory Stack:: History of visited directories.
6695 * Controlling the Prompt:: Customizing the various prompt strings.
6696 * The Restricted Shell:: A more controlled mode of shell execution.
6697 * Bash POSIX Mode:: Making Bash behave more closely to what
6698 the POSIX standard specifies.
6699 * Shell Compatibility Mode:: How Bash supports behavior that was present
6700 in earlier versions and has changed.
6701 @end menu
6702
6703 @node Invoking Bash
6704 @section Invoking Bash
6705
6706 @example
6707 bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6708 [-O @var{shopt_option}] [@var{argument} @dots{}]
6709 bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6710 [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}]
6711 bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6712 [-O @var{shopt_option}] [@var{argument} @dots{}]
6713 @end example
6714
6715 All of the single-character options used with the @code{set} builtin
6716 (@pxref{The Set Builtin}) can be used as options when the shell is invoked.
6717 In addition, there are several multi-character
6718 options that you can use. These options must appear on the command
6719 line before the single-character options to be recognized.
6720
6721 @table @code
6722 @item --debugger
6723 Arrange for the debugger profile to be executed before the shell
6724 starts. Turns on extended debugging mode (see @ref{The Shopt Builtin}
6725 for a description of the @code{extdebug} option to the @code{shopt}
6726 builtin).
6727
6728 @item --dump-po-strings
6729 A list of all double-quoted strings preceded by @samp{$}
6730 is printed on the standard output
6731 in the @sc{gnu} @code{gettext} PO (portable object) file format.
6732 Equivalent to @option{-D} except for the output format.
6733
6734 @item --dump-strings
6735 Equivalent to @option{-D}.
6736
6737 @item --help
6738 Display a usage message on standard output and exit successfully.
6739
6740 @item --init-file @var{filename}
6741 @itemx --rcfile @var{filename}
6742 Execute commands from @var{filename} (instead of @file{~/.bashrc})
6743 in an interactive shell.
6744
6745 @item --login
6746 Equivalent to @option{-l}.
6747
6748 @item --noediting
6749 Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing})
6750 to read command lines when the shell is interactive.
6751
6752 @item --noprofile
6753 Don't load the system-wide startup file @file{/etc/profile}
6754 or any of the personal initialization files
6755 @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile}
6756 when Bash is invoked as a login shell.
6757
6758 @item --norc
6759 Don't read the @file{~/.bashrc} initialization file in an
6760 interactive shell. This is on by default if the shell is
6761 invoked as @code{sh}.
6762
6763 @item --posix
6764 Change the behavior of Bash where the default operation differs
6765 from the @sc{posix} standard to match the standard. This
6766 is intended to make Bash behave as a strict superset of that
6767 standard. @xref{Bash POSIX Mode}, for a description of the Bash
6768 @sc{posix} mode.
6769
6770 @item --restricted
6771 Make the shell a restricted shell (@pxref{The Restricted Shell}).
6772
6773 @item --verbose
6774 Equivalent to @option{-v}. Print shell input lines as they're read.
6775
6776 @item --version
6777 Show version information for this instance of
6778 Bash on the standard output and exit successfully.
6779 @end table
6780
6781 There are several single-character options that may be supplied at
6782 invocation which are not available with the @code{set} builtin.
6783
6784 @table @code
6785 @item -c
6786 Read and execute commands from the first non-option argument
6787 @var{command_string}, then exit.
6788 If there are arguments after the @var{command_string},
6789 the first argument is assigned to @code{$0}
6790 and any remaining arguments are assigned to the positional parameters.
6791 The assignment to @code{$0} sets the name of the shell, which is used
6792 in warning and error messages.
6793
6794 @item -i
6795 Force the shell to run interactively. Interactive shells are
6796 described in @ref{Interactive Shells}.
6797
6798 @item -l
6799 Make this shell act as if it had been directly invoked by login.
6800 When the shell is interactive, this is equivalent to starting a
6801 login shell with @samp{exec -l bash}.
6802 When the shell is not interactive, the login shell startup files will
6803 be executed.
6804 @samp{exec bash -l} or @samp{exec bash --login}
6805 will replace the current shell with a Bash login shell.
6806 @xref{Bash Startup Files}, for a description of the special behavior
6807 of a login shell.
6808
6809 @item -r
6810 Make the shell a restricted shell (@pxref{The Restricted Shell}).
6811
6812 @item -s
6813 If this option is present, or if no arguments remain after option
6814 processing, then commands are read from the standard input.
6815 This option allows the positional parameters to be set
6816 when invoking an interactive shell or when reading input
6817 through a pipe.
6818
6819 @item -D
6820 A list of all double-quoted strings preceded by @samp{$}
6821 is printed on the standard output.
6822 These are the strings that
6823 are subject to language translation when the current locale
6824 is not @code{C} or @code{POSIX} (@pxref{Locale Translation}).
6825 This implies the @option{-n} option; no commands will be executed.
6826
6827 @item [-+]O [@var{shopt_option}]
6828 @var{shopt_option} is one of the shell options accepted by the
6829 @code{shopt} builtin (@pxref{The Shopt Builtin}).
6830 If @var{shopt_option} is present, @option{-O} sets the value of that option;
6831 @option{+O} unsets it.
6832 If @var{shopt_option} is not supplied, the names and values of the shell
6833 options accepted by @code{shopt} are printed on the standard output.
6834 If the invocation option is @option{+O}, the output is displayed in a format
6835 that may be reused as input.
6836
6837 @item --
6838 A @code{--} signals the end of options and disables further option
6839 processing.
6840 Any arguments after the @code{--} are treated as filenames and arguments.
6841 @end table
6842
6843 @cindex login shell
6844 A @emph{login} shell is one whose first character of argument zero is
6845 @samp{-}, or one invoked with the @option{--login} option.
6846
6847 @cindex interactive shell
6848 An @emph{interactive} shell is one started without non-option arguments,
6849 unless @option{-s} is specified,
6850 without specifying the @option{-c} option, and whose input and output are both
6851 connected to terminals (as determined by @code{isatty(3)}), or one
6852 started with the @option{-i} option. @xref{Interactive Shells}, for more
6853 information.
6854
6855 If arguments remain after option processing, and neither the
6856 @option{-c} nor the @option{-s}
6857 option has been supplied, the first argument is assumed to
6858 be the name of a file containing shell commands (@pxref{Shell Scripts}).
6859 When Bash is invoked in this fashion, @code{$0}
6860 is set to the name of the file, and the positional parameters
6861 are set to the remaining arguments.
6862 Bash reads and executes commands from this file, then exits.
6863 Bash's exit status is the exit status of the last command executed
6864 in the script. If no commands are executed, the exit status is 0.
6865
6866 @node Bash Startup Files
6867 @section Bash Startup Files
6868 @cindex startup files
6869
6870 This section describes how Bash executes its startup files.
6871 If any of the files exist but cannot be read, Bash reports an error.
6872 Tildes are expanded in filenames as described above under
6873 Tilde Expansion (@pxref{Tilde Expansion}).
6874
6875 Interactive shells are described in @ref{Interactive Shells}.
6876
6877 @subsubheading Invoked as an interactive login shell, or with @option{--login}
6878
6879 When Bash is invoked as an interactive login shell, or as a
6880 non-interactive shell with the @option{--login} option, it first reads and
6881 executes commands from the file @file{/etc/profile}, if that file exists.
6882 After reading that file, it looks for @file{~/.bash_profile},
6883 @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads
6884 and executes commands from the first one that exists and is readable.
6885 The @option{--noprofile} option may be used when the shell is started to
6886 inhibit this behavior.
6887
6888 When an interactive login shell exits,
6889 or a non-interactive login shell executes the @code{exit} builtin command,
6890 Bash reads and executes commands from
6891 the file @file{~/.bash_logout}, if it exists.
6892
6893 @subsubheading Invoked as an interactive non-login shell
6894
6895 When an interactive shell that is not a login shell is started, Bash
6896 reads and executes commands from @file{~/.bashrc}, if that file exists.
6897 This may be inhibited by using the @option{--norc} option.
6898 The @option{--rcfile @var{file}} option will force Bash to read and
6899 execute commands from @var{file} instead of @file{~/.bashrc}.
6900
6901 So, typically, your @file{~/.bash_profile} contains the line
6902 @example
6903 @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi}
6904 @end example
6905 @noindent
6906 after (or before) any login-specific initializations.
6907
6908 @subsubheading Invoked non-interactively
6909
6910 When Bash is started non-interactively, to run a shell script,
6911 for example, it looks for the variable @env{BASH_ENV} in the environment,
6912 expands its value if it appears there, and uses the expanded value as
6913 the name of a file to read and execute. Bash behaves as if the
6914 following command were executed:
6915 @example
6916 @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi}
6917 @end example
6918 @noindent
6919 but the value of the @env{PATH} variable is not used to search for the
6920 filename.
6921
6922 As noted above, if a non-interactive shell is invoked with the
6923 @option{--login} option, Bash attempts to read and execute commands from the
6924 login shell startup files.
6925
6926 @subsubheading Invoked with name @code{sh}
6927
6928 If Bash is invoked with the name @code{sh}, it tries to mimic the
6929 startup behavior of historical versions of @code{sh} as closely as
6930 possible, while conforming to the @sc{posix} standard as well.
6931
6932 When invoked as an interactive login shell, or as a non-interactive
6933 shell with the @option{--login} option, it first attempts to read
6934 and execute commands from @file{/etc/profile} and @file{~/.profile}, in
6935 that order.
6936 The @option{--noprofile} option may be used to inhibit this behavior.
6937 When invoked as an interactive shell with the name @code{sh}, Bash
6938 looks for the variable @env{ENV}, expands its value if it is defined,
6939 and uses the expanded value as the name of a file to read and execute.
6940 Since a shell invoked as @code{sh} does not attempt to read and execute
6941 commands from any other startup files, the @option{--rcfile} option has
6942 no effect.
6943 A non-interactive shell invoked with the name @code{sh} does not attempt
6944 to read any other startup files.
6945
6946 When invoked as @code{sh}, Bash enters @sc{posix} mode after
6947 the startup files are read.
6948
6949 @subsubheading Invoked in @sc{posix} mode
6950
6951 When Bash is started in @sc{posix} mode, as with the
6952 @option{--posix} command line option, it follows the @sc{posix} standard
6953 for startup files.
6954 In this mode, interactive shells expand the @env{ENV} variable
6955 and commands are read and executed from the file whose name is the
6956 expanded value.
6957 No other startup files are read.
6958
6959 @subsubheading Invoked by remote shell daemon
6960
6961 Bash attempts to determine when it is being run with its standard input
6962 connected to a network connection, as when executed by the remote shell
6963 daemon, usually @code{rshd}, or the secure shell daemon @code{sshd}.
6964 If Bash determines it is being run in
6965 this fashion, it reads and executes commands from @file{~/.bashrc}, if that
6966 file exists and is readable.
6967 It will not do this if invoked as @code{sh}.
6968 The @option{--norc} option may be used to inhibit this behavior, and the
6969 @option{--rcfile} option may be used to force another file to be read, but
6970 neither @code{rshd} nor @code{sshd} generally invoke the shell with those
6971 options or allow them to be specified.
6972
6973 @subsubheading Invoked with unequal effective and real @sc{uid/gid}s
6974
6975 If Bash is started with the effective user (group) id not equal to the
6976 real user (group) id, and the @option{-p} option is not supplied, no startup
6977 files are read, shell functions are not inherited from the environment,
6978 the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH}, and @env{GLOBIGNORE}
6979 variables, if they appear in the environment, are ignored, and the effective
6980 user id is set to the real user id.
6981 If the @option{-p} option is supplied at invocation, the startup behavior is
6982 the same, but the effective user id is not reset.
6983
6984 @node Interactive Shells
6985 @section Interactive Shells
6986 @cindex interactive shell
6987 @cindex shell, interactive
6988
6989 @menu
6990 * What is an Interactive Shell?:: What determines whether a shell is Interactive.
6991 * Is this Shell Interactive?:: How to tell if a shell is interactive.
6992 * Interactive Shell Behavior:: What changes in a interactive shell?
6993 @end menu
6994
6995 @node What is an Interactive Shell?
6996 @subsection What is an Interactive Shell?
6997
6998 An interactive shell
6999 is one started without non-option arguments, unless @option{-s} is
7000 specified, without specifying the @option{-c} option, and
7001 whose input and error output are both
7002 connected to terminals (as determined by @code{isatty(3)}),
7003 or one started with the @option{-i} option.
7004
7005 An interactive shell generally reads from and writes to a user's
7006 terminal.
7007
7008 The @option{-s} invocation option may be used to set the positional parameters
7009 when an interactive shell is started.
7010
7011 @node Is this Shell Interactive?
7012 @subsection Is this Shell Interactive?
7013
7014 To determine within a startup script whether or not Bash is
7015 running interactively,
7016 test the value of the @samp{-} special parameter.
7017 It contains @code{i} when the shell is interactive. For example:
7018
7019 @example
7020 case "$-" in
7021 *i*) echo This shell is interactive ;;
7022 *) echo This shell is not interactive ;;
7023 esac
7024 @end example
7025
7026 Alternatively, startup scripts may examine the variable
7027 @env{PS1}; it is unset in non-interactive shells, and set in
7028 interactive shells. Thus:
7029
7030 @example
7031 if [ -z "$PS1" ]; then
7032 echo This shell is not interactive
7033 else
7034 echo This shell is interactive
7035 fi
7036 @end example
7037
7038 @node Interactive Shell Behavior
7039 @subsection Interactive Shell Behavior
7040
7041 When the shell is running interactively, it changes its behavior in
7042 several ways.
7043
7044 @enumerate
7045 @item
7046 Startup files are read and executed as described in @ref{Bash Startup Files}.
7047
7048 @item
7049 Job Control (@pxref{Job Control}) is enabled by default. When job
7050 control is in effect, Bash ignores the keyboard-generated job control
7051 signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
7052
7053 @item
7054 Bash expands and displays @env{PS1} before reading the first line
7055 of a command, and expands and displays @env{PS2} before reading the
7056 second and subsequent lines of a multi-line command.
7057 Bash expands and displays @env{PS0} after it reads a command but before
7058 executing it.
7059 See @ref{Controlling the Prompt}, for a complete list of prompt
7060 string escape sequences.
7061
7062 @item
7063 Bash executes the values of the set elements of the @env{PROMPT_COMMAND}
7064 array variable as commands before printing the primary prompt, @env{$PS1}
7065 (@pxref{Bash Variables}).
7066
7067 @item
7068 Readline (@pxref{Command Line Editing}) is used to read commands from
7069 the user's terminal.
7070
7071 @item
7072 Bash inspects the value of the @code{ignoreeof} option to @code{set -o}
7073 instead of exiting immediately when it receives an @code{EOF} on its
7074 standard input when reading a command (@pxref{The Set Builtin}).
7075
7076 @item
7077 Command history (@pxref{Bash History Facilities})
7078 and history expansion (@pxref{History Interaction})
7079 are enabled by default.
7080 Bash will save the command history to the file named by @env{$HISTFILE}
7081 when a shell with history enabled exits.
7082
7083 @item
7084 Alias expansion (@pxref{Aliases}) is performed by default.
7085
7086 @item
7087 In the absence of any traps, Bash ignores @code{SIGTERM}
7088 (@pxref{Signals}).
7089
7090 @item
7091 In the absence of any traps, @code{SIGINT} is caught and handled
7092 (@pxref{Signals}).
7093 @code{SIGINT} will interrupt some shell builtins.
7094
7095 @item
7096 An interactive login shell sends a @code{SIGHUP} to all jobs on exit
7097 if the @code{huponexit} shell option has been enabled (@pxref{Signals}).
7098
7099 @item
7100 The @option{-n} invocation option is ignored, and @samp{set -n} has
7101 no effect (@pxref{The Set Builtin}).
7102
7103 @item
7104 Bash will check for mail periodically, depending on the values of the
7105 @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables
7106 (@pxref{Bash Variables}).
7107
7108 @item
7109 Expansion errors due to references to unbound shell variables after
7110 @samp{set -u} has been enabled will not cause the shell to exit
7111 (@pxref{The Set Builtin}).
7112
7113 @item
7114 The shell will not exit on expansion errors caused by @var{var} being unset
7115 or null in @code{$@{@var{var}:?@var{word}@}} expansions
7116 (@pxref{Shell Parameter Expansion}).
7117
7118 @item
7119 Redirection errors encountered by shell builtins will not cause the
7120 shell to exit.
7121
7122 @item
7123 When running in @sc{posix} mode, a special builtin returning an error
7124 status will not cause the shell to exit (@pxref{Bash POSIX Mode}).
7125
7126 @item
7127 A failed @code{exec} will not cause the shell to exit
7128 (@pxref{Bourne Shell Builtins}).
7129
7130 @item
7131 Parser syntax errors will not cause the shell to exit.
7132
7133 @item
7134 Simple spelling correction for directory arguments to the @code{cd}
7135 builtin is enabled by default (see the description of the @code{cdspell}
7136 option to the @code{shopt} builtin in @ref{The Shopt Builtin}).
7137
7138 @item
7139 The shell will check the value of the @env{TMOUT} variable and exit
7140 if a command is not read within the specified number of seconds after
7141 printing @env{$PS1} (@pxref{Bash Variables}).
7142
7143 @end enumerate
7144
7145 @node Bash Conditional Expressions
7146 @section Bash Conditional Expressions
7147 @cindex expressions, conditional
7148
7149 Conditional expressions are used by the @code{[[} compound command
7150 (@pxref{Conditional Constructs})
7151 and the @code{test} and @code{[} builtin commands
7152 (@pxref{Bourne Shell Builtins}).
7153 The @code{test}
7154 and @code{[} commands determine their behavior based on the number
7155 of arguments; see the descriptions of those commands for any other
7156 command-specific actions.
7157
7158 Expressions may be unary or binary,
7159 and are formed from the following primaries.
7160 Unary expressions are often used to examine the status of a file.
7161 There are string operators and numeric comparison operators as well.
7162 Bash handles several filenames specially when they are used in
7163 expressions.
7164 If the operating system on which Bash is running provides these
7165 special files, Bash will use them; otherwise it will emulate them
7166 internally with this behavior:
7167 If the @var{file} argument to one of the primaries is of the form
7168 @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked.
7169 If the @var{file} argument to one of the primaries is one of
7170 @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file
7171 descriptor 0, 1, or 2, respectively, is checked.
7172
7173 When used with @code{[[}, the @samp{<} and @samp{>} operators sort
7174 lexicographically using the current locale.
7175 The @code{test} command uses ASCII ordering.
7176
7177 Unless otherwise specified, primaries that operate on files follow symbolic
7178 links and operate on the target of the link, rather than the link itself.
7179
7180 @table @code
7181 @item -a @var{file}
7182 True if @var{file} exists.
7183
7184 @item -b @var{file}
7185 True if @var{file} exists and is a block special file.
7186
7187 @item -c @var{file}
7188 True if @var{file} exists and is a character special file.
7189
7190 @item -d @var{file}
7191 True if @var{file} exists and is a directory.
7192
7193 @item -e @var{file}
7194 True if @var{file} exists.
7195
7196 @item -f @var{file}
7197 True if @var{file} exists and is a regular file.
7198
7199 @item -g @var{file}
7200 True if @var{file} exists and its set-group-id bit is set.
7201
7202 @item -h @var{file}
7203 True if @var{file} exists and is a symbolic link.
7204
7205 @item -k @var{file}
7206 True if @var{file} exists and its "sticky" bit is set.
7207
7208 @item -p @var{file}
7209 True if @var{file} exists and is a named pipe (FIFO).
7210
7211 @item -r @var{file}
7212 True if @var{file} exists and is readable.
7213
7214 @item -s @var{file}
7215 True if @var{file} exists and has a size greater than zero.
7216
7217 @item -t @var{fd}
7218 True if file descriptor @var{fd} is open and refers to a terminal.
7219
7220 @item -u @var{file}
7221 True if @var{file} exists and its set-user-id bit is set.
7222
7223 @item -w @var{file}
7224 True if @var{file} exists and is writable.
7225
7226 @item -x @var{file}
7227 True if @var{file} exists and is executable.
7228
7229 @item -G @var{file}
7230 True if @var{file} exists and is owned by the effective group id.
7231
7232 @item -L @var{file}
7233 True if @var{file} exists and is a symbolic link.
7234
7235 @item -N @var{file}
7236 True if @var{file} exists and has been modified since it was last read.
7237
7238 @item -O @var{file}
7239 True if @var{file} exists and is owned by the effective user id.
7240
7241 @item -S @var{file}
7242 True if @var{file} exists and is a socket.
7243
7244 @item @var{file1} -ef @var{file2}
7245 True if @var{file1} and @var{file2} refer to the same device and
7246 inode numbers.
7247
7248 @item @var{file1} -nt @var{file2}
7249 True if @var{file1} is newer (according to modification date)
7250 than @var{file2}, or if @var{file1} exists and @var{file2} does not.
7251
7252 @item @var{file1} -ot @var{file2}
7253 True if @var{file1} is older than @var{file2},
7254 or if @var{file2} exists and @var{file1} does not.
7255
7256 @item -o @var{optname}
7257 True if the shell option @var{optname} is enabled.
7258 The list of options appears in the description of the @option{-o}
7259 option to the @code{set} builtin (@pxref{The Set Builtin}).
7260
7261 @item -v @var{varname}
7262 True if the shell variable @var{varname} is set (has been assigned a value).
7263
7264 @item -R @var{varname}
7265 True if the shell variable @var{varname} is set and is a name reference.
7266
7267 @item -z @var{string}
7268 True if the length of @var{string} is zero.
7269
7270 @item -n @var{string}
7271 @itemx @var{string}
7272 True if the length of @var{string} is non-zero.
7273
7274 @item @var{string1} == @var{string2}
7275 @itemx @var{string1} = @var{string2}
7276 True if the strings are equal.
7277 When used with the @code{[[} command, this performs pattern matching as
7278 described above (@pxref{Conditional Constructs}).
7279
7280 @samp{=} should be used with the @code{test} command for @sc{posix} conformance.
7281
7282 @item @var{string1} != @var{string2}
7283 True if the strings are not equal.
7284
7285 @item @var{string1} < @var{string2}
7286 True if @var{string1} sorts before @var{string2} lexicographically.
7287
7288 @item @var{string1} > @var{string2}
7289 True if @var{string1} sorts after @var{string2} lexicographically.
7290
7291 @item @var{arg1} OP @var{arg2}
7292 @code{OP} is one of
7293 @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}.
7294 These arithmetic binary operators return true if @var{arg1}
7295 is equal to, not equal to, less than, less than or equal to,
7296 greater than, or greater than or equal to @var{arg2},
7297 respectively. @var{Arg1} and @var{arg2}
7298 may be positive or negative integers.
7299 When used with the @code{[[} command, @var{Arg1} and @var{Arg2}
7300 are evaluated as arithmetic expressions (@pxref{Shell Arithmetic}).
7301 @end table
7302
7303 @node Shell Arithmetic
7304 @section Shell Arithmetic
7305 @cindex arithmetic, shell
7306 @cindex shell arithmetic
7307 @cindex expressions, arithmetic
7308 @cindex evaluation, arithmetic
7309 @cindex arithmetic evaluation
7310
7311 The shell allows arithmetic expressions to be evaluated, as one of
7312 the shell expansions or by using the @code{((} compound command, the
7313 @code{let} builtin, or the @option{-i} option to the @code{declare} builtin.
7314
7315 Evaluation is done in fixed-width integers with no check for overflow,
7316 though division by 0 is trapped and flagged as an error.
7317 The operators and their precedence, associativity, and values
7318 are the same as in the C language.
7319 The following list of operators is grouped into levels of
7320 equal-precedence operators.
7321 The levels are listed in order of decreasing precedence.
7322
7323 @table @code
7324
7325 @item @var{id}++ @var{id}--
7326 variable post-increment and post-decrement
7327
7328 @item ++@var{id} --@var{id}
7329 variable pre-increment and pre-decrement
7330
7331 @item - +
7332 unary minus and plus
7333
7334 @item ! ~
7335 logical and bitwise negation
7336
7337 @item **
7338 exponentiation
7339
7340 @item * / %
7341 multiplication, division, remainder
7342
7343 @item + -
7344 addition, subtraction
7345
7346 @item << >>
7347 left and right bitwise shifts
7348
7349 @item <= >= < >
7350 comparison
7351
7352 @item == !=
7353 equality and inequality
7354
7355 @item &
7356 bitwise AND
7357
7358 @item ^
7359 bitwise exclusive OR
7360
7361 @item |
7362 bitwise OR
7363
7364 @item &&
7365 logical AND
7366
7367 @item ||
7368 logical OR
7369
7370 @item expr ? expr : expr
7371 conditional operator
7372
7373 @item = *= /= %= += -= <<= >>= &= ^= |=
7374 assignment
7375
7376 @item expr1 , expr2
7377 comma
7378 @end table
7379
7380 Shell variables are allowed as operands; parameter expansion is
7381 performed before the expression is evaluated.
7382 Within an expression, shell variables may also be referenced by name
7383 without using the parameter expansion syntax.
7384 A shell variable that is null or unset evaluates to 0 when referenced
7385 by name without using the parameter expansion syntax.
7386 The value of a variable is evaluated as an arithmetic expression
7387 when it is referenced, or when a variable which has been given the
7388 @code{integer} attribute using @samp{declare -i} is assigned a value.
7389 A null value evaluates to 0.
7390 A shell variable need not have its @code{integer} attribute turned on
7391 to be used in an expression.
7392
7393 Integer constants follow the C language definition, without suffixes or
7394 character constants.
7395 Constants with a leading 0 are interpreted as octal numbers.
7396 A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise,
7397 numbers take the form [@var{base}@code{#}]@var{n}, where the optional @var{base}
7398 is a decimal number between 2 and 64 representing the arithmetic
7399 base, and @var{n} is a number in that base.
7400 If @var{base}@code{#} is omitted, then base 10 is used.
7401 When specifying @var{n},
7402 if a non-digit is required,
7403 the digits greater than 9 are represented by the lowercase letters,
7404 the uppercase letters, @samp{@@}, and @samp{_}, in that order.
7405 If @var{base} is less than or equal to 36, lowercase and uppercase
7406 letters may be used interchangeably to represent numbers between 10
7407 and 35.
7408
7409 Operators are evaluated in order of precedence. Sub-expressions in
7410 parentheses are evaluated first and may override the precedence
7411 rules above.
7412
7413 @node Aliases
7414 @section Aliases
7415 @cindex alias expansion
7416
7417 @dfn{Aliases} allow a string to be substituted for a word when it is used
7418 as the first word of a simple command.
7419 The shell maintains a list of aliases that may be set and unset with
7420 the @code{alias} and @code{unalias} builtin commands.
7421
7422 The first word of each simple command, if unquoted, is checked to see
7423 if it has an alias.
7424 If so, that word is replaced by the text of the alias.
7425 The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the
7426 shell metacharacters or quoting characters listed above may not appear
7427 in an alias name.
7428 The replacement text may contain any valid
7429 shell input, including shell metacharacters.
7430 The first word of the replacement text is tested for
7431 aliases, but a word that is identical to an alias being expanded
7432 is not expanded a second time.
7433 This means that one may alias @code{ls} to @code{"ls -F"},
7434 for instance, and Bash does not try to recursively expand the
7435 replacement text.
7436 If the last character of the alias value is a
7437 @code{blank}, then the next command word following the
7438 alias is also checked for alias expansion.
7439
7440 Aliases are created and listed with the @code{alias}
7441 command, and removed with the @code{unalias} command.
7442
7443 There is no mechanism for using arguments in the replacement text,
7444 as in @code{csh}.
7445 If arguments are needed, use a shell function
7446 (@pxref{Shell Functions}).
7447
7448 Aliases are not expanded when the shell is not interactive,
7449 unless the @code{expand_aliases} shell option is set using
7450 @code{shopt} (@pxref{The Shopt Builtin}).
7451
7452 The rules concerning the definition and use of aliases are
7453 somewhat confusing. Bash
7454 always reads at least one complete line of input,
7455 and all lines that make up a compound command,
7456 before executing any of the commands on that line or the compound command.
7457 Aliases are expanded when a
7458 command is read, not when it is executed. Therefore, an
7459 alias definition appearing on the same line as another
7460 command does not take effect until the next line of input is read.
7461 The commands following the alias definition
7462 on that line are not affected by the new alias.
7463 This behavior is also an issue when functions are executed.
7464 Aliases are expanded when a function definition is read,
7465 not when the function is executed, because a function definition
7466 is itself a command. As a consequence, aliases
7467 defined in a function are not available until after that
7468 function is executed. To be safe, always put
7469 alias definitions on a separate line, and do not use @code{alias}
7470 in compound commands.
7471
7472 For almost every purpose, shell functions are preferred over aliases.
7473
7474 @node Arrays
7475 @section Arrays
7476 @cindex arrays
7477
7478 Bash provides one-dimensional indexed and associative array variables.
7479 Any variable may be used as an indexed array;
7480 the @code{declare} builtin will explicitly declare an array.
7481 There is no maximum
7482 limit on the size of an array, nor any requirement that members
7483 be indexed or assigned contiguously.
7484 Indexed arrays are referenced using integers (including arithmetic
7485 expressions (@pxref{Shell Arithmetic})) and are zero-based;
7486 associative arrays use arbitrary strings.
7487 Unless otherwise noted, indexed array indices must be non-negative integers.
7488
7489 An indexed array is created automatically if any variable is assigned to
7490 using the syntax
7491 @example
7492 @var{name}[@var{subscript}]=@var{value}
7493 @end example
7494
7495 @noindent
7496 The @var{subscript}
7497 is treated as an arithmetic expression that must evaluate to a number.
7498 To explicitly declare an array, use
7499 @example
7500 declare -a @var{name}
7501 @end example
7502 @noindent
7503 The syntax
7504 @example
7505 declare -a @var{name}[@var{subscript}]
7506 @end example
7507 @noindent
7508 is also accepted; the @var{subscript} is ignored.
7509
7510 @noindent
7511 Associative arrays are created using
7512 @example
7513 declare -A @var{name}
7514 @end example
7515
7516 Attributes may be
7517 specified for an array variable using the @code{declare} and
7518 @code{readonly} builtins. Each attribute applies to all members of
7519 an array.
7520
7521 Arrays are assigned to using compound assignments of the form
7522 @example
7523 @var{name}=(@var{value1} @var{value2} @dots{} )
7524 @end example
7525 @noindent
7526 where each
7527 @var{value} may be of the form @code{[@var{subscript}]=}@var{string}.
7528 Indexed array assignments do not require anything but @var{string}.
7529 When assigning to indexed arrays, if
7530 the optional subscript is supplied, that index is assigned to;
7531 otherwise the index of the element assigned is the last index assigned
7532 to by the statement plus one. Indexing starts at zero.
7533
7534 Each @var{value} in the list undergoes all the shell expansions
7535 described above (@pxref{Shell Expansions}).
7536
7537 When assigning to an associative array, the words in a compound assignment
7538 may be either assignment statements, for which the subscript is required,
7539 or a list of words that is interpreted as a sequence of alternating keys
7540 and values:
7541 @var{name}=(@var{key1} @var{value1} @var{key2} @var{value2} @dots{} ).
7542 These are treated identically to
7543 @var{name}=( [@var{key1}]=@var{value1} [@var{key2}]=@var{value2} @dots{} ).
7544 The first word in the list determines how the remaining words
7545 are interpreted; all assignments in a list must be of the same type.
7546 When using key/value pairs, the keys may not be missing or empty;
7547 a final missing value is treated like the empty string.
7548
7549 This syntax is also accepted by the @code{declare}
7550 builtin. Individual array elements may be assigned to using the
7551 @code{@var{name}[@var{subscript}]=@var{value}} syntax introduced above.
7552
7553 When assigning to an indexed array, if @var{name}
7554 is subscripted by a negative number, that number is
7555 interpreted as relative to one greater than the maximum index of
7556 @var{name}, so negative indices count back from the end of the
7557 array, and an index of -1 references the last element.
7558
7559 Any element of an array may be referenced using
7560 @code{$@{@var{name}[@var{subscript}]@}}.
7561 The braces are required to avoid
7562 conflicts with the shell's filename expansion operators. If the
7563 @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members
7564 of the array @var{name}. These subscripts differ only when the word
7565 appears within double quotes.
7566 If the word is double-quoted,
7567 @code{$@{@var{name}[*]@}} expands to a single word with
7568 the value of each array member separated by the first character of the
7569 @env{IFS} variable, and @code{$@{@var{name}[@@]@}} expands each element of
7570 @var{name} to a separate word. When there are no array members,
7571 @code{$@{@var{name}[@@]@}} expands to nothing.
7572 If the double-quoted expansion occurs within a word, the expansion of
7573 the first parameter is joined with the beginning part of the original
7574 word, and the expansion of the last parameter is joined with the last
7575 part of the original word.
7576 This is analogous to the
7577 expansion of the special parameters @samp{@@} and @samp{*}.
7578 @code{$@{#@var{name}[@var{subscript}]@}} expands to the length of
7579 @code{$@{@var{name}[@var{subscript}]@}}.
7580 If @var{subscript} is @samp{@@} or
7581 @samp{*}, the expansion is the number of elements in the array.
7582 If the @var{subscript}
7583 used to reference an element of an indexed array
7584 evaluates to a number less than zero, it is
7585 interpreted as relative to one greater than the maximum index of the array,
7586 so negative indices count back from the end of the array,
7587 and an index of -1 refers to the last element.
7588
7589 Referencing an array variable without a subscript is equivalent to
7590 referencing with a subscript of 0.
7591 Any reference to a variable using a valid subscript is legal, and
7592 @code{bash} will create an array if necessary.
7593
7594 An array variable is considered set if a subscript has been assigned a
7595 value. The null string is a valid value.
7596
7597 It is possible to obtain the keys (indices) of an array as well as the values.
7598 $@{!@var{name}[@@]@} and $@{!@var{name}[*]@} expand to the indices
7599 assigned in array variable @var{name}.
7600 The treatment when in double quotes is similar to the expansion of the
7601 special parameters @samp{@@} and @samp{*} within double quotes.
7602
7603 The @code{unset} builtin is used to destroy arrays.
7604 @code{unset @var{name}[@var{subscript}]}
7605 destroys the array element at index @var{subscript}.
7606 Negative subscripts to indexed arrays are interpreted as described above.
7607 Unsetting the last element of an array variable does not unset the variable.
7608 @code{unset @var{name}}, where @var{name} is an array, removes the
7609 entire array. A subscript of @samp{*} or @samp{@@} also removes the
7610 entire array.
7611
7612 When using a variable name with a subscript as an argument to a command,
7613 such as with @code{unset}, without using the word expansion syntax
7614 described above, the argument is subject to the shell's filename expansion.
7615 If filename expansion is not desired, the argument should be quoted.
7616
7617 The @code{declare}, @code{local}, and @code{readonly}
7618 builtins each accept a @option{-a} option to specify an indexed
7619 array and a @option{-A} option to specify an associative array.
7620 If both options are supplied, @option{-A} takes precedence.
7621 The @code{read} builtin accepts a @option{-a}
7622 option to assign a list of words read from the standard input
7623 to an array, and can read values from the standard input into
7624 individual array elements. The @code{set} and @code{declare}
7625 builtins display array values in a way that allows them to be
7626 reused as input.
7627
7628 @node The Directory Stack
7629 @section The Directory Stack
7630 @cindex directory stack
7631
7632 @menu
7633 * Directory Stack Builtins:: Bash builtin commands to manipulate
7634 the directory stack.
7635 @end menu
7636
7637 The directory stack is a list of recently-visited directories. The
7638 @code{pushd} builtin adds directories to the stack as it changes
7639 the current directory, and the @code{popd} builtin removes specified
7640 directories from the stack and changes the current directory to
7641 the directory removed. The @code{dirs} builtin displays the contents
7642 of the directory stack. The current directory is always the "top"
7643 of the directory stack.
7644
7645 The contents of the directory stack are also visible
7646 as the value of the @env{DIRSTACK} shell variable.
7647
7648 @node Directory Stack Builtins
7649 @subsection Directory Stack Builtins
7650
7651 @table @code
7652
7653 @item dirs
7654 @btindex dirs
7655 @example
7656 dirs [-clpv] [+@var{N} | -@var{N}]
7657 @end example
7658
7659 Display the list of currently remembered directories. Directories
7660 are added to the list with the @code{pushd} command; the
7661 @code{popd} command removes directories from the list.
7662 The current directory is always the first directory in the stack.
7663
7664 @table @code
7665 @item -c
7666 Clears the directory stack by deleting all of the elements.
7667 @item -l
7668 Produces a listing using full pathnames;
7669 the default listing format uses a tilde to denote the home directory.
7670 @item -p
7671 Causes @code{dirs} to print the directory stack with one entry per
7672 line.
7673 @item -v
7674 Causes @code{dirs} to print the directory stack with one entry per
7675 line, prefixing each entry with its index in the stack.
7676 @item +@var{N}
7677 Displays the @var{N}th directory (counting from the left of the
7678 list printed by @code{dirs} when invoked without options), starting
7679 with zero.
7680 @item -@var{N}
7681 Displays the @var{N}th directory (counting from the right of the
7682 list printed by @code{dirs} when invoked without options), starting
7683 with zero.
7684 @end table
7685
7686 @item popd
7687 @btindex popd
7688 @example
7689 popd [-n] [+@var{N} | -@var{N}]
7690 @end example
7691
7692 Removes elements from the directory stack.
7693 The elements are numbered from 0 starting at the first directory
7694 listed by @code{dirs};
7695 that is, @code{popd} is equivalent to @code{popd +0}.
7696
7697 When no arguments are given, @code{popd}
7698 removes the top directory from the stack and changes to
7699 the new top directory.
7700
7701 Arguments, if supplied, have the following meanings:
7702
7703 @table @code
7704 @item -n
7705 Suppresses the normal change of directory when removing directories
7706 from the stack, so that only the stack is manipulated.
7707 @item +@var{N}
7708 Removes the @var{N}th directory (counting from the left of the
7709 list printed by @code{dirs}), starting with zero, from the stack.
7710 @item -@var{N}
7711 Removes the @var{N}th directory (counting from the right of the
7712 list printed by @code{dirs}), starting with zero, from the stack.
7713 @end table
7714
7715 If the top element of the directory stack is modified, and
7716 the @option{-n} option was not supplied, @code{popd} uses the @code{cd}
7717 builtin to change to the directory at the top of the stack.
7718 If the @code{cd} fails, @code{popd} returns a non-zero value.
7719
7720 Otherwise, @code{popd} returns an unsuccessful status if
7721 an invalid option is encountered, the directory stack
7722 is empty, or a non-existent directory stack entry is specified.
7723
7724 If the @code{popd} command is successful,
7725 Bash runs @code{dirs} to show the final contents of the directory stack,
7726 and the return status is 0.
7727
7728 @btindex pushd
7729 @item pushd
7730 @example
7731 pushd [-n] [@var{+N} | @var{-N} | @var{dir}]
7732 @end example
7733
7734 Adds a directory to the top of the directory stack, or rotates
7735 the stack, making the new top of the stack the current working
7736 directory.
7737 With no arguments, @code{pushd} exchanges the top two elements
7738 of the directory stack.
7739
7740 Arguments, if supplied, have the following meanings:
7741
7742 @table @code
7743 @item -n
7744 Suppresses the normal change of directory when rotating or
7745 adding directories to the stack, so that only the stack is manipulated.
7746 @item +@var{N}
7747 Brings the @var{N}th directory (counting from the left of the
7748 list printed by @code{dirs}, starting with zero) to the top of
7749 the list by rotating the stack.
7750 @item -@var{N}
7751 Brings the @var{N}th directory (counting from the right of the
7752 list printed by @code{dirs}, starting with zero) to the top of
7753 the list by rotating the stack.
7754 @item @var{dir}
7755 Makes @var{dir} be the top of the stack.
7756 @end table
7757
7758 After the stack has been modified, if the @option{-n} option was not
7759 supplied, @code{pushd} uses the @code{cd} builtin to change to the
7760 directory at the top of the stack.
7761 If the @code{cd} fails, @code{pushd} returns a non-zero value.
7762
7763 Otherwise, if no arguments are supplied, @code{pushd} returns 0 unless the
7764 directory stack is empty.
7765 When rotating the directory stack, @code{pushd} returns 0 unless
7766 the directory stack is empty or a non-existent directory stack element
7767 is specified.
7768
7769 If the @code{pushd} command is successful,
7770 Bash runs @code{dirs} to show the final contents of the directory stack.
7771
7772 @end table
7773
7774 @node Controlling the Prompt
7775 @section Controlling the Prompt
7776 @cindex prompting
7777
7778 Bash examines the value of the array variable @env{PROMPT_COMMAND} just before
7779 printing each primary prompt.
7780 If any elements in @env{PROMPT_COMMAND} are set and non-null, Bash
7781 executes each value, in numeric order,
7782 just as if it had been typed on the command line.
7783
7784 In addition, the following table describes the special characters which
7785 can appear in the prompt variables @env{PS0}, @env{PS1}, @env{PS2}, and
7786 @env{PS4}:
7787
7788 @table @code
7789 @item \a
7790 A bell character.
7791 @item \d
7792 The date, in "Weekday Month Date" format (e.g., "Tue May 26").
7793 @item \D@{@var{format}@}
7794 The @var{format} is passed to @code{strftime}(3) and the result is inserted
7795 into the prompt string; an empty @var{format} results in a locale-specific
7796 time representation. The braces are required.
7797 @item \e
7798 An escape character.
7799 @item \h
7800 The hostname, up to the first `.'.
7801 @item \H
7802 The hostname.
7803 @item \j
7804 The number of jobs currently managed by the shell.
7805 @item \l
7806 The basename of the shell's terminal device name.
7807 @item \n
7808 A newline.
7809 @item \r
7810 A carriage return.
7811 @item \s
7812 The name of the shell, the basename of @code{$0} (the portion
7813 following the final slash).
7814 @item \t
7815 The time, in 24-hour HH:MM:SS format.
7816 @item \T
7817 The time, in 12-hour HH:MM:SS format.
7818 @item \@@
7819 The time, in 12-hour am/pm format.
7820 @item \A
7821 The time, in 24-hour HH:MM format.
7822 @item \u
7823 The username of the current user.
7824 @item \v
7825 The version of Bash (e.g., 2.00)
7826 @item \V
7827 The release of Bash, version + patchlevel (e.g., 2.00.0)
7828 @item \w
7829 The value of the @code{PWD} shell variable (@env{$PWD}),
7830 with @env{$HOME} abbreviated with a tilde
7831 (uses the @env{$PROMPT_DIRTRIM} variable).
7832 @item \W
7833 The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde.
7834 @item \!
7835 The history number of this command.
7836 @item \#
7837 The command number of this command.
7838 @item \$
7839 If the effective uid is 0, @code{#}, otherwise @code{$}.
7840 @item \@var{nnn}
7841 The character whose ASCII code is the octal value @var{nnn}.
7842 @item \\
7843 A backslash.
7844 @item \[
7845 Begin a sequence of non-printing characters. This could be used to
7846 embed a terminal control sequence into the prompt.
7847 @item \]
7848 End a sequence of non-printing characters.
7849 @end table
7850
7851 The command number and the history number are usually different:
7852 the history number of a command is its position in the history
7853 list, which may include commands restored from the history file
7854 (@pxref{Bash History Facilities}), while the command number is
7855 the position in the sequence of commands executed during the current
7856 shell session.
7857
7858 After the string is decoded, it is expanded via
7859 parameter expansion, command substitution, arithmetic
7860 expansion, and quote removal, subject to the value of the
7861 @code{promptvars} shell option (@pxref{The Shopt Builtin}).
7862 This can have unwanted side effects if escaped portions of the string
7863 appear within command substitution or contain characters special to
7864 word expansion.
7865
7866 @node The Restricted Shell
7867 @section The Restricted Shell
7868 @cindex restricted shell
7869
7870 If Bash is started with the name @code{rbash}, or the
7871 @option{--restricted}
7872 or
7873 @option{-r}
7874 option is supplied at invocation, the shell becomes restricted.
7875 A restricted shell is used to
7876 set up an environment more controlled than the standard shell.
7877 A restricted shell behaves identically to @code{bash}
7878 with the exception that the following are disallowed or not performed:
7879
7880 @itemize @bullet
7881 @item
7882 Changing directories with the @code{cd} builtin.
7883 @item
7884 Setting or unsetting the values of the @env{SHELL}, @env{PATH},
7885 @env{HISTFILE},
7886 @env{ENV}, or @env{BASH_ENV} variables.
7887 @item
7888 Specifying command names containing slashes.
7889 @item
7890 Specifying a filename containing a slash as an argument to the @code{.}
7891 builtin command.
7892 @item
7893 Specifying a filename containing a slash as an argument to the @code{history}
7894 builtin command.
7895 @item
7896 Specifying a filename containing a slash as an argument to the @option{-p}
7897 option to the @code{hash} builtin command.
7898 @item
7899 Importing function definitions from the shell environment at startup.
7900 @item
7901 Parsing the value of @env{SHELLOPTS} from the shell environment at startup.
7902 @item
7903 Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&},
7904 @samp{&>}, and @samp{>>} redirection operators.
7905 @item
7906 Using the @code{exec} builtin to replace the shell with another command.
7907 @item
7908 Adding or deleting builtin commands with the
7909 @option{-f} and @option{-d} options to the @code{enable} builtin.
7910 @item
7911 Using the @code{enable} builtin command to enable disabled shell builtins.
7912 @item
7913 Specifying the @option{-p} option to the @code{command} builtin.
7914 @item
7915 Turning off restricted mode with @samp{set +r} or @samp{set +o restricted}.
7916 @end itemize
7917
7918 These restrictions are enforced after any startup files are read.
7919
7920 When a command that is found to be a shell script is executed
7921 (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in
7922 the shell spawned to execute the script.
7923
7924 The restricted shell mode is only one component of a useful restricted
7925 environment. It should be accompanied by setting @env{PATH} to a value
7926 that allows execution of only a few verified commands (commands that
7927 allow shell escapes are particularly vulnerable), leaving the user
7928 in a non-writable directory other than his home directory after login,
7929 not allowing the restricted shell to execute shell scripts, and cleaning
7930 the environment of variables that cause some commands to modify their
7931 behavior (e.g., @env{VISUAL} or @env{PAGER}).
7932
7933 Modern systems provide more secure ways to implement a restricted environment,
7934 such as @code{jails}, @code{zones}, or @code{containers}.
7935
7936
7937 @node Bash POSIX Mode
7938 @section Bash POSIX Mode
7939 @cindex POSIX Mode
7940
7941 Starting Bash with the @option{--posix} command-line option or executing
7942 @samp{set -o posix} while Bash is running will cause Bash to conform more
7943 closely to the @sc{posix} standard by changing the behavior to
7944 match that specified by @sc{posix} in areas where the Bash default differs.
7945
7946 When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the
7947 startup files.
7948
7949 The following list is what's changed when `@sc{posix} mode' is in effect:
7950
7951 @enumerate
7952 @item
7953 Bash ensures that the @env{POSIXLY_CORRECT} variable is set.
7954
7955 @item
7956 When a command in the hash table no longer exists, Bash will re-search
7957 @env{$PATH} to find the new location. This is also available with
7958 @samp{shopt -s checkhash}.
7959
7960 @item
7961 Bash will not insert a command without the execute bit set into the
7962 command hash table, even if it returns it as a (last-ditch) result
7963 from a @env{$PATH} search.
7964
7965 @item
7966 The message printed by the job control code and builtins when a job
7967 exits with a non-zero status is `Done(status)'.
7968
7969 @item
7970 The message printed by the job control code and builtins when a job
7971 is stopped is `Stopped(@var{signame})', where @var{signame} is, for
7972 example, @code{SIGTSTP}.
7973
7974 @item
7975 Alias expansion is always enabled, even in non-interactive shells.
7976
7977 @item
7978 Reserved words appearing in a context where reserved words are recognized
7979 do not undergo alias expansion.
7980
7981 @item
7982 The @sc{posix} @env{PS1} and @env{PS2} expansions of @samp{!} to
7983 the history number and @samp{!!} to @samp{!} are enabled,
7984 and parameter expansion is performed on the values of @env{PS1} and
7985 @env{PS2} regardless of the setting of the @code{promptvars} option.
7986
7987 @item
7988 The @sc{posix} startup files are executed (@env{$ENV}) rather than
7989 the normal Bash files.
7990
7991 @item
7992 Tilde expansion is only performed on assignments preceding a command
7993 name, rather than on all assignment statements on the line.
7994
7995 @item
7996 The default history file is @file{~/.sh_history} (this is the
7997 default value of @env{$HISTFILE}).
7998
7999 @item
8000 Redirection operators do not perform filename expansion on the word
8001 in the redirection unless the shell is interactive.
8002
8003 @item
8004 Redirection operators do not perform word splitting on the word in the
8005 redirection.
8006
8007 @item
8008 Function names must be valid shell @code{name}s. That is, they may not
8009 contain characters other than letters, digits, and underscores, and
8010 may not start with a digit. Declaring a function with an invalid name
8011 causes a fatal syntax error in non-interactive shells.
8012
8013 @item
8014 Function names may not be the same as one of the @sc{posix} special
8015 builtins.
8016
8017 @item
8018 @sc{posix} special builtins are found before shell functions
8019 during command lookup.
8020
8021 @item
8022 When printing shell function definitions (e.g., by @code{type}), Bash does
8023 not print the @code{function} keyword.
8024
8025 @item
8026 Literal tildes that appear as the first character in elements of
8027 the @env{PATH} variable are not expanded as described above
8028 under @ref{Tilde Expansion}.
8029
8030 @item
8031 The @code{time} reserved word may be used by itself as a command. When
8032 used in this way, it displays timing statistics for the shell and its
8033 completed children. The @env{TIMEFORMAT} variable controls the format
8034 of the timing information.
8035
8036 @item
8037 When parsing and expanding a $@{@dots{}@} expansion that appears within
8038 double quotes, single quotes are no longer special and cannot be used to
8039 quote a closing brace or other special character, unless the operator is
8040 one of those defined to perform pattern removal. In this case, they do
8041 not have to appear as matched pairs.
8042
8043 @item
8044 The parser does not recognize @code{time} as a reserved word if the next
8045 token begins with a @samp{-}.
8046
8047 @ignore
8048 @item
8049 When parsing @code{$()} command substitutions containing here-documents,
8050 the parser does not allow a here-document to be delimited by the closing
8051 right parenthesis. The newline after the here-document delimiter is required.
8052 @end ignore
8053
8054 @item
8055 The @samp{!} character does not introduce history expansion within a
8056 double-quoted string, even if the @code{histexpand} option is enabled.
8057
8058 @item
8059 If a @sc{posix} special builtin returns an error status, a
8060 non-interactive shell exits. The fatal errors are those listed in
8061 the @sc{posix} standard, and include things like passing incorrect options,
8062 redirection errors, variable assignment errors for assignments preceding
8063 the command name, and so on.
8064
8065 @item
8066 A non-interactive shell exits with an error status if a variable
8067 assignment error occurs when no command name follows the assignment
8068 statements.
8069 A variable assignment error occurs, for example, when trying to assign
8070 a value to a readonly variable.
8071
8072 @item
8073 A non-interactive shell exits with an error status if a variable
8074 assignment error occurs in an assignment statement preceding a special
8075 builtin, but not with any other simple command. For any other simple
8076 command, the shell aborts execution of that command, and execution continues
8077 at the top level ("the shell shall not perform any further processing of the
8078 command in which the error occurred").
8079
8080 @item
8081 A non-interactive shell exits with an error status if the iteration
8082 variable in a @code{for} statement or the selection variable in a
8083 @code{select} statement is a readonly variable.
8084
8085 @item
8086 Non-interactive shells exit if @var{filename} in @code{.} @var{filename}
8087 is not found.
8088
8089 @item
8090 Non-interactive shells exit if a syntax error in an arithmetic expansion
8091 results in an invalid expression.
8092
8093 @item
8094 Non-interactive shells exit if a parameter expansion error occurs.
8095
8096 @item
8097 Non-interactive shells exit if there is a syntax error in a script read
8098 with the @code{.} or @code{source} builtins, or in a string processed by
8099 the @code{eval} builtin.
8100
8101 @item
8102 While variable indirection is available, it may not be applied to the
8103 @samp{#} and @samp{?} special parameters.
8104
8105 @item
8106 When expanding the @samp{*} special parameter in a pattern context where the
8107 expansion is double-quoted does not treat the @code{$*} as if it were
8108 double-quoted.
8109
8110 @item
8111 Assignment statements preceding @sc{posix} special builtins
8112 persist in the shell environment after the builtin completes.
8113
8114 @item
8115 The @code{command} builtin does not prevent builtins that take assignment
8116 statements as arguments from expanding them as assignment statements;
8117 when not in @sc{posix} mode, assignment builtins lose their assignment
8118 statement expansion properties when preceded by @code{command}.
8119
8120 @item
8121 The @code{bg} builtin uses the required format to describe each job placed
8122 in the background, which does not include an indication of whether the job
8123 is the current or previous job.
8124
8125 @item
8126 The output of @samp{kill -l} prints all the signal names on a single line,
8127 separated by spaces, without the @samp{SIG} prefix.
8128
8129 @item
8130 The @code{kill} builtin does not accept signal names with a @samp{SIG}
8131 prefix.
8132
8133 @item
8134 The @code{export} and @code{readonly} builtin commands display their
8135 output in the format required by @sc{posix}.
8136
8137 @item
8138 The @code{trap} builtin displays signal names without the leading
8139 @code{SIG}.
8140
8141 @item
8142 The @code{trap} builtin doesn't check the first argument for a possible
8143 signal specification and revert the signal handling to the original
8144 disposition if it is, unless that argument consists solely of digits and
8145 is a valid signal number. If users want to reset the handler for a given
8146 signal to the original disposition, they should use @samp{-} as the
8147 first argument.
8148
8149 @item
8150 @code{trap -p} displays signals whose dispositions are set to SIG_DFL and
8151 those that were ignored when the shell started.
8152
8153 @item
8154 The @code{.} and @code{source} builtins do not search the current directory
8155 for the filename argument if it is not found by searching @env{PATH}.
8156
8157 @item
8158 Enabling @sc{posix} mode has the effect of setting the
8159 @code{inherit_errexit} option, so
8160 subshells spawned to execute command substitutions inherit the value of
8161 the @option{-e} option from the parent shell.
8162 When the @code{inherit_errexit} option is not enabled,
8163 Bash clears the @option{-e} option in such subshells.
8164
8165 @item
8166 Enabling @sc{posix} mode has the effect of setting the
8167 @code{shift_verbose} option, so numeric arguments to @code{shift}
8168 that exceed the number of positional parameters will result in an
8169 error message.
8170
8171 @item
8172 When the @code{alias} builtin displays alias definitions, it does not
8173 display them with a leading @samp{alias } unless the @option{-p} option
8174 is supplied.
8175
8176 @item
8177 When the @code{set} builtin is invoked without options, it does not display
8178 shell function names and definitions.
8179
8180 @item
8181 When the @code{set} builtin is invoked without options, it displays
8182 variable values without quotes, unless they contain shell metacharacters,
8183 even if the result contains nonprinting characters.
8184
8185 @item
8186 When the @code{cd} builtin is invoked in logical mode, and the pathname
8187 constructed from @code{$PWD} and the directory name supplied as an argument
8188 does not refer to an existing directory, @code{cd} will fail instead of
8189 falling back to physical mode.
8190
8191 @item
8192 When the @code{cd} builtin cannot change a directory because the
8193 length of the pathname
8194 constructed from @code{$PWD} and the directory name supplied as an argument
8195 exceeds @code{PATH_MAX} when all symbolic links are expanded, @code{cd} will
8196 fail instead of attempting to use only the supplied directory name.
8197
8198 @item
8199 The @code{pwd} builtin verifies that the value it prints is the same as the
8200 current directory, even if it is not asked to check the file system with the
8201 @option{-P} option.
8202
8203 @item
8204 When listing the history, the @code{fc} builtin does not include an
8205 indication of whether or not a history entry has been modified.
8206
8207 @item
8208 The default editor used by @code{fc} is @code{ed}.
8209
8210 @item
8211 The @code{type} and @code{command} builtins will not report a non-executable
8212 file as having been found, though the shell will attempt to execute such a
8213 file if it is the only so-named file found in @code{$PATH}.
8214
8215 @item
8216 The @code{vi} editing mode will invoke the @code{vi} editor directly when
8217 the @samp{v} command is run, instead of checking @code{$VISUAL} and
8218 @code{$EDITOR}.
8219
8220 @item
8221 When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret
8222 any arguments to @code{echo} as options. Each argument is displayed, after
8223 escape characters are converted.
8224
8225 @item
8226 The @code{ulimit} builtin uses a block size of 512 bytes for the @option{-c}
8227 and @option{-f} options.
8228
8229 @item
8230 The arrival of @code{SIGCHLD} when a trap is set on @code{SIGCHLD} does
8231 not interrupt the @code{wait} builtin and cause it to return immediately.
8232 The trap command is run once for each child that exits.
8233
8234 @item
8235 The @code{read} builtin may be interrupted by a signal for which a trap
8236 has been set.
8237 If Bash receives a trapped signal while executing @code{read}, the trap
8238 handler executes and @code{read} returns an exit status greater than 128.
8239
8240 @item
8241 Bash removes an exited background process's status from the list of such
8242 statuses after the @code{wait} builtin is used to obtain it.
8243
8244 @end enumerate
8245
8246 There is other @sc{posix} behavior that Bash does not implement by
8247 default even when in @sc{posix} mode.
8248 Specifically:
8249
8250 @enumerate
8251
8252 @item
8253 The @code{fc} builtin checks @code{$EDITOR} as a program to edit history
8254 entries if @code{FCEDIT} is unset, rather than defaulting directly to
8255 @code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset.
8256
8257 @item
8258 As noted above, Bash requires the @code{xpg_echo} option to be enabled for
8259 the @code{echo} builtin to be fully conformant.
8260
8261 @end enumerate
8262
8263 Bash can be configured to be @sc{posix}-conformant by default, by specifying
8264 the @option{--enable-strict-posix-default} to @code{configure} when building
8265 (@pxref{Optional Features}).
8266
8267 @node Shell Compatibility Mode
8268 @section Shell Compatibility Mode
8269 @cindex Compatibility Level
8270 @cindex Compatibility Mode
8271
8272 Bash-4.0 introduced the concept of a `shell compatibility level', specified
8273 as a set of options to the shopt builtin
8274 (@code{compat31},
8275 @code{compat32},
8276 @code{compat40},
8277 @code{compat41},
8278 and so on).
8279 There is only one current
8280 compatibility level -- each option is mutually exclusive.
8281 The compatibility level is intended to allow users to select behavior
8282 from previous versions that is incompatible with newer versions
8283 while they migrate scripts to use current features and
8284 behavior. It's intended to be a temporary solution.
8285
8286 This section does not mention behavior that is standard for a particular
8287 version (e.g., setting @code{compat32} means that quoting the rhs of the regexp
8288 matching operator quotes special regexp characters in the word, which is
8289 default behavior in bash-3.2 and above).
8290
8291 If a user enables, say, @code{compat32}, it may affect the behavior of other
8292 compatibility levels up to and including the current compatibility level.
8293 The idea is that each compatibility level controls behavior that changed
8294 in that version of Bash,
8295 but that behavior may have been present in earlier versions.
8296 For instance, the change to use locale-based comparisons with the @code{[[}
8297 command came in bash-4.1, and earlier versions used ASCII-based comparisons,
8298 so enabling @code{compat32} will enable ASCII-based comparisons as well.
8299 That granularity may not be sufficient for
8300 all uses, and as a result users should employ compatibility levels carefully.
8301 Read the documentation for a particular feature to find out the
8302 current behavior.
8303
8304 Bash-4.3 introduced a new shell variable: @env{BASH_COMPAT}.
8305 The value assigned
8306 to this variable (a decimal version number like 4.2, or an integer
8307 corresponding to the @code{compat}@var{NN} option, like 42) determines the
8308 compatibility level.
8309
8310 Starting with bash-4.4, Bash has begun deprecating older compatibility
8311 levels.
8312 Eventually, the options will be removed in favor of @env{BASH_COMPAT}.
8313
8314 Bash-5.0 is the final version for which there will be an individual shopt
8315 option for the previous version. Users should use @env{BASH_COMPAT}
8316 on bash-5.0 and later versions.
8317
8318 The following table describes the behavior changes controlled by each
8319 compatibility level setting.
8320 The @code{compat}@var{NN} tag is used as shorthand for setting the
8321 compatibility level
8322 to @var{NN} using one of the following mechanisms.
8323 For versions prior to bash-5.0, the compatibility level may be set using
8324 the corresponding @code{compat}@var{NN} shopt option.
8325 For bash-4.3 and later versions, the @env{BASH_COMPAT} variable is preferred,
8326 and it is required for bash-5.1 and later versions.
8327
8328 @table @code
8329 @item compat31
8330 @itemize @bullet
8331 @item
8332 quoting the rhs of the @code{[[} command's regexp matching operator (=~)
8333 has no special effect
8334 @end itemize
8335
8336 @item compat32
8337 @itemize @bullet
8338 @item
8339 interrupting a command list such as "a ; b ; c" causes the execution
8340 of the next command in the list (in bash-4.0 and later versions,
8341 the shell acts as if it received the interrupt, so
8342 interrupting one command in a list aborts the execution of the
8343 entire list)
8344 @end itemize
8345
8346 @item compat40
8347 @itemize @bullet
8348 @item
8349 the @samp{<} and @samp{>} operators to the @code{[[} command do not
8350 consider the current locale when comparing strings; they use ASCII
8351 ordering.
8352 Bash versions prior to bash-4.1 use ASCII collation and strcmp(3);
8353 bash-4.1 and later use the current locale's collation sequence and
8354 strcoll(3).
8355 @end itemize
8356
8357 @item compat41
8358 @itemize @bullet
8359 @item
8360 in posix mode, @code{time} may be followed by options and still be
8361 recognized as a reserved word (this is @sc{posix} interpretation 267)
8362 @item
8363 in posix mode, the parser requires that an even number of single
8364 quotes occur in the @var{word} portion of a double-quoted $@{@dots{}@}
8365 parameter expansion and treats them specially, so that characters within
8366 the single quotes are considered quoted
8367 (this is @sc{posix} interpretation 221)
8368 @end itemize
8369
8370 @item compat42
8371 @itemize @bullet
8372 @item
8373 the replacement string in double-quoted pattern substitution does not
8374 undergo quote removal, as it does in versions after bash-4.2
8375 @item
8376 in posix mode, single quotes are considered special when expanding
8377 the @var{word} portion of a double-quoted $@{@dots{}@} parameter expansion
8378 and can be used to quote a closing brace or other special character
8379 (this is part of @sc{posix} interpretation 221);
8380 in later versions, single quotes
8381 are not special within double-quoted word expansions
8382 @end itemize
8383
8384 @item compat43
8385 @itemize @bullet
8386 @item
8387 the shell does not print a warning message if an attempt is made to
8388 use a quoted compound assignment as an argument to declare
8389 (declare -a foo='(1 2)'). Later versions warn that this usage is
8390 deprecated
8391 @item
8392 word expansion errors are considered non-fatal errors that cause the
8393 current command to fail, even in posix mode
8394 (the default behavior is to make them fatal errors that cause the shell
8395 to exit)
8396 @item
8397 when executing a shell function, the loop state (while/until/etc.)
8398 is not reset, so @code{break} or @code{continue} in that function will break
8399 or continue loops in the calling context. Bash-4.4 and later reset
8400 the loop state to prevent this
8401 @end itemize
8402
8403 @item compat44
8404 @itemize @bullet
8405 @item
8406 the shell sets up the values used by @env{BASH_ARGV} and @env{BASH_ARGC}
8407 so they can expand to the shell's positional parameters even if extended
8408 debugging mode is not enabled
8409 @item
8410 a subshell inherits loops from its parent context, so @code{break}
8411 or @code{continue} will cause the subshell to exit.
8412 Bash-5.0 and later reset the loop state to prevent the exit
8413 @item
8414 variable assignments preceding builtins like @code{export} and @code{readonly}
8415 that set attributes continue to affect variables with the same
8416 name in the calling environment even if the shell is not in posix
8417 mode
8418 @end itemize
8419
8420 @item compat50 (set using BASH_COMPAT)
8421 @itemize @bullet
8422 @item
8423 Bash-5.1 changed the way @code{$RANDOM} is generated to introduce slightly
8424 more randomness. If the shell compatibility level is set to 50 or
8425 lower, it reverts to the method from bash-5.0 and previous versions,
8426 so seeding the random number generator by assigning a value to
8427 @env{RANDOM} will produce the same sequence as in bash-5.0
8428 @item
8429 If the command hash table is empty, Bash versions prior to bash-5.1
8430 printed an informational message to that effect, even when producing
8431 output that can be reused as input. Bash-5.1 suppresses that message
8432 when the @option{-l} option is supplied.
8433 @end itemize
8434 @end table
8435
8436 @node Job Control
8437 @chapter Job Control
8438
8439 This chapter discusses what job control is, how it works, and how
8440 Bash allows you to access its facilities.
8441
8442 @menu
8443 * Job Control Basics:: How job control works.
8444 * Job Control Builtins:: Bash builtin commands used to interact
8445 with job control.
8446 * Job Control Variables:: Variables Bash uses to customize job
8447 control.
8448 @end menu
8449
8450 @node Job Control Basics
8451 @section Job Control Basics
8452 @cindex job control
8453 @cindex foreground
8454 @cindex background
8455 @cindex suspending jobs
8456
8457 Job control
8458 refers to the ability to selectively stop (suspend)
8459 the execution of processes and continue (resume)
8460 their execution at a later point. A user typically employs
8461 this facility via an interactive interface supplied jointly
8462 by the operating system kernel's terminal driver and Bash.
8463
8464 The shell associates a @var{job} with each pipeline. It keeps a
8465 table of currently executing jobs, which may be listed with the
8466 @code{jobs} command. When Bash starts a job
8467 asynchronously, it prints a line that looks
8468 like:
8469 @example
8470 [1] 25647
8471 @end example
8472 @noindent
8473 indicating that this job is job number 1 and that the process @sc{id}
8474 of the last process in the pipeline associated with this job is
8475 25647. All of the processes in a single pipeline are members of
8476 the same job. Bash uses the @var{job} abstraction as the
8477 basis for job control.
8478
8479 To facilitate the implementation of the user interface to job
8480 control, the operating system maintains the notion of a current terminal
8481 process group @sc{id}. Members of this process group (processes whose
8482 process group @sc{id} is equal to the current terminal process group
8483 @sc{id}) receive keyboard-generated signals such as @code{SIGINT}.
8484 These processes are said to be in the foreground. Background
8485 processes are those whose process group @sc{id} differs from the
8486 terminal's; such processes are immune to keyboard-generated
8487 signals. Only foreground processes are allowed to read from or, if
8488 the user so specifies with @code{stty tostop}, write to the terminal.
8489 Background processes which attempt to
8490 read from (write to when @code{stty tostop} is in effect) the
8491 terminal are sent a @code{SIGTTIN} (@code{SIGTTOU})
8492 signal by the kernel's terminal driver,
8493 which, unless caught, suspends the process.
8494
8495 If the operating system on which Bash is running supports
8496 job control, Bash contains facilities to use it. Typing the
8497 @dfn{suspend} character (typically @samp{^Z}, Control-Z) while a
8498 process is running causes that process to be stopped and returns
8499 control to Bash. Typing the @dfn{delayed suspend} character
8500 (typically @samp{^Y}, Control-Y) causes the process to be stopped
8501 when it attempts to read input from the terminal, and control to
8502 be returned to Bash. The user then manipulates the state of
8503 this job, using the @code{bg} command to continue it in the
8504 background, the @code{fg} command to continue it in the
8505 foreground, or the @code{kill} command to kill it. A @samp{^Z}
8506 takes effect immediately, and has the additional side effect of
8507 causing pending output and typeahead to be discarded.
8508
8509 There are a number of ways to refer to a job in the shell. The
8510 character @samp{%} introduces a job specification (@dfn{jobspec}).
8511
8512 Job number @code{n} may be referred to as @samp{%n}.
8513 The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the
8514 current job, which is the last job stopped while it was in the foreground
8515 or started in the background.
8516 A single @samp{%} (with no accompanying job specification) also refers
8517 to the current job.
8518 The previous job may be referenced using @samp{%-}.
8519 If there is only a single job, @samp{%+} and @samp{%-} can both be used
8520 to refer to that job.
8521 In output pertaining to jobs (e.g., the output of the @code{jobs}
8522 command), the current job is always flagged with a @samp{+}, and the
8523 previous job with a @samp{-}.
8524
8525 A job may also be referred to
8526 using a prefix of the name used to start it, or using a substring
8527 that appears in its command line. For example, @samp{%ce} refers
8528 to a stopped job whose command name begins with @samp{ce}.
8529 Using @samp{%?ce}, on the
8530 other hand, refers to any job containing the string @samp{ce} in
8531 its command line. If the prefix or substring matches more than one job,
8532 Bash reports an error.
8533
8534 Simply naming a job can be used to bring it into the foreground:
8535 @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the
8536 background into the foreground. Similarly, @samp{%1 &} resumes
8537 job 1 in the background, equivalent to @samp{bg %1}
8538
8539 The shell learns immediately whenever a job changes state.
8540 Normally, Bash waits until it is about to print a prompt
8541 before reporting changes in a job's status so as to not interrupt
8542 any other output.
8543 If the @option{-b} option to the @code{set} builtin is enabled,
8544 Bash reports such changes immediately (@pxref{The Set Builtin}).
8545 Any trap on @code{SIGCHLD} is executed for each child process
8546 that exits.
8547
8548 If an attempt to exit Bash is made while jobs are stopped, (or running, if
8549 the @code{checkjobs} option is enabled -- see @ref{The Shopt Builtin}), the
8550 shell prints a warning message, and if the @code{checkjobs} option is
8551 enabled, lists the jobs and their statuses.
8552 The @code{jobs} command may then be used to inspect their status.
8553 If a second attempt to exit is made without an intervening command,
8554 Bash does not print another warning, and any stopped jobs are terminated.
8555
8556 When the shell is waiting for a job or process using the @code{wait}
8557 builtin, and job control is enabled, @code{wait} will return when the
8558 job changes state. The @option{-f} option causes @code{wait} to wait
8559 until the job or process terminates before returning.
8560
8561 @node Job Control Builtins
8562 @section Job Control Builtins
8563
8564 @table @code
8565
8566 @item bg
8567 @btindex bg
8568 @example
8569 bg [@var{jobspec} @dots{}]
8570 @end example
8571
8572 Resume each suspended job @var{jobspec} in the background, as if it
8573 had been started with @samp{&}.
8574 If @var{jobspec} is not supplied, the current job is used.
8575 The return status is zero unless it is run when job control is not
8576 enabled, or, when run with job control enabled, any
8577 @var{jobspec} was not found or specifies a job
8578 that was started without job control.
8579
8580 @item fg
8581 @btindex fg
8582 @example
8583 fg [@var{jobspec}]
8584 @end example
8585
8586 Resume the job @var{jobspec} in the foreground and make it the current job.
8587 If @var{jobspec} is not supplied, the current job is used.
8588 The return status is that of the command placed into the foreground,
8589 or non-zero if run when job control is disabled or, when run with
8590 job control enabled, @var{jobspec} does not specify a valid job or
8591 @var{jobspec} specifies a job that was started without job control.
8592
8593 @item jobs
8594 @btindex jobs
8595 @example
8596 jobs [-lnprs] [@var{jobspec}]
8597 jobs -x @var{command} [@var{arguments}]
8598 @end example
8599
8600 The first form lists the active jobs. The options have the
8601 following meanings:
8602
8603 @table @code
8604 @item -l
8605 List process @sc{id}s in addition to the normal information.
8606
8607 @item -n
8608 Display information only about jobs that have changed status since
8609 the user was last notified of their status.
8610
8611 @item -p
8612 List only the process @sc{id} of the job's process group leader.
8613
8614 @item -r
8615 Display only running jobs.
8616
8617 @item -s
8618 Display only stopped jobs.
8619 @end table
8620
8621 If @var{jobspec} is given,
8622 output is restricted to information about that job.
8623 If @var{jobspec} is not supplied, the status of all jobs is
8624 listed.
8625
8626 If the @option{-x} option is supplied, @code{jobs} replaces any
8627 @var{jobspec} found in @var{command} or @var{arguments} with the
8628 corresponding process group @sc{id}, and executes @var{command},
8629 passing it @var{argument}s, returning its exit status.
8630
8631 @item kill
8632 @btindex kill
8633 @example
8634 kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid}
8635 kill -l|-L [@var{exit_status}]
8636 @end example
8637
8638 Send a signal specified by @var{sigspec} or @var{signum} to the process
8639 named by job specification @var{jobspec} or process @sc{id} @var{pid}.
8640 @var{sigspec} is either a case-insensitive signal name such as
8641 @code{SIGINT} (with or without the @code{SIG} prefix)
8642 or a signal number; @var{signum} is a signal number.
8643 If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used.
8644 The @option{-l} option lists the signal names.
8645 If any arguments are supplied when @option{-l} is given, the names of the
8646 signals corresponding to the arguments are listed, and the return status
8647 is zero.
8648 @var{exit_status} is a number specifying a signal number or the exit
8649 status of a process terminated by a signal.
8650 The @option{-L} option is equivalent to @option{-l}.
8651 The return status is zero if at least one signal was successfully sent,
8652 or non-zero if an error occurs or an invalid option is encountered.
8653
8654 @item wait
8655 @btindex wait
8656 @example
8657 wait [-fn] [-p @var{varname}] [@var{jobspec} or @var{pid} @dots{}]
8658 @end example
8659
8660 Wait until the child process specified by each process @sc{id} @var{pid}
8661 or job specification @var{jobspec} exits and return the exit status of the
8662 last command waited for.
8663 If a job spec is given, all processes in the job are waited for.
8664 If no arguments are given,
8665 @code{wait} waits for all running background jobs and
8666 the last-executed process substitution, if its process id is the same as
8667 @var{$!},
8668 and the return status is zero.
8669 If the @option{-n} option is supplied, @code{wait} waits for a single job
8670 from the list of @var{pid}s or @var{jobspec}s or, if no arguments are
8671 supplied, any job,
8672 to complete and returns its exit status.
8673 If none of the supplied arguments is a child of the shell, or if no arguments
8674 are supplied and the shell has no unwaited-for children, the exit status
8675 is 127.
8676 If the @option{-p} option is supplied, the process or job identifier of the job
8677 for which the exit status is returned is assigned to the variable
8678 @var{varname} named by the option argument.
8679 The variable will be unset initially, before any assignment.
8680 This is useful only when the @option{-n} option is supplied.
8681 Supplying the @option{-f} option, when job control is enabled,
8682 forces @code{wait} to wait for each @var{pid} or @var{jobspec} to
8683 terminate before returning its status, intead of returning when it changes
8684 status.
8685 If neither @var{jobspec} nor @var{pid} specifies an active child process
8686 of the shell, the return status is 127.
8687
8688 @item disown
8689 @btindex disown
8690 @example
8691 disown [-ar] [-h] [@var{jobspec} @dots{} | @var{pid} @dots{} ]
8692 @end example
8693
8694 Without options, remove each @var{jobspec} from the table of
8695 active jobs.
8696 If the @option{-h} option is given, the job is not removed from the table,
8697 but is marked so that @code{SIGHUP} is not sent to the job if the shell
8698 receives a @code{SIGHUP}.
8699 If @var{jobspec} is not present, and neither the @option{-a} nor the
8700 @option{-r} option is supplied, the current job is used.
8701 If no @var{jobspec} is supplied, the @option{-a} option means to remove or
8702 mark all jobs; the @option{-r} option without a @var{jobspec}
8703 argument restricts operation to running jobs.
8704
8705 @item suspend
8706 @btindex suspend
8707 @example
8708 suspend [-f]
8709 @end example
8710
8711 Suspend the execution of this shell until it receives a
8712 @code{SIGCONT} signal.
8713 A login shell cannot be suspended; the @option{-f}
8714 option can be used to override this and force the suspension.
8715 @end table
8716
8717 When job control is not active, the @code{kill} and @code{wait}
8718 builtins do not accept @var{jobspec} arguments. They must be
8719 supplied process @sc{id}s.
8720
8721 @node Job Control Variables
8722 @section Job Control Variables
8723
8724 @vtable @code
8725
8726 @item auto_resume
8727 This variable controls how the shell interacts with the user and
8728 job control. If this variable exists then single word simple
8729 commands without redirections are treated as candidates for resumption
8730 of an existing job. There is no ambiguity allowed; if there is
8731 more than one job beginning with the string typed, then
8732 the most recently accessed job will be selected.
8733 The name of a stopped job, in this context, is the command line
8734 used to start it. If this variable is set to the value @samp{exact},
8735 the string supplied must match the name of a stopped job exactly;
8736 if set to @samp{substring},
8737 the string supplied needs to match a substring of the name of a
8738 stopped job. The @samp{substring} value provides functionality
8739 analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}).
8740 If set to any other value, the supplied string must
8741 be a prefix of a stopped job's name; this provides functionality
8742 analogous to the @samp{%} job @sc{id}.
8743
8744 @end vtable
8745
8746 @set readline-appendix
8747 @set history-appendix
8748 @cindex Readline, how to use
8749 @include rluser.texi
8750 @cindex History, how to use
8751 @include hsuser.texi
8752 @clear readline-appendix
8753 @clear history-appendix
8754
8755 @node Installing Bash
8756 @chapter Installing Bash
8757
8758 This chapter provides basic instructions for installing Bash on
8759 the various supported platforms. The distribution supports the
8760 @sc{gnu} operating systems, nearly every version of Unix, and several
8761 non-Unix systems such as BeOS and Interix.
8762 Other independent ports exist for
8763 @sc{ms-dos}, @sc{os/2}, and Windows platforms.
8764
8765 @menu
8766 * Basic Installation:: Installation instructions.
8767 * Compilers and Options:: How to set special options for various
8768 systems.
8769 * Compiling For Multiple Architectures:: How to compile Bash for more
8770 than one kind of system from
8771 the same source tree.
8772 * Installation Names:: How to set the various paths used by the installation.
8773 * Specifying the System Type:: How to configure Bash for a particular system.
8774 * Sharing Defaults:: How to share default configuration values among GNU
8775 programs.
8776 * Operation Controls:: Options recognized by the configuration program.
8777 * Optional Features:: How to enable and disable optional features when
8778 building Bash.
8779 @end menu
8780
8781 @node Basic Installation
8782 @section Basic Installation
8783 @cindex installation
8784 @cindex configuration
8785 @cindex Bash installation
8786 @cindex Bash configuration
8787
8788 These are installation instructions for Bash.
8789
8790 The simplest way to compile Bash is:
8791
8792 @enumerate
8793 @item
8794 @code{cd} to the directory containing the source code and type
8795 @samp{./configure} to configure Bash for your system. If you're
8796 using @code{csh} on an old version of System V, you might need to
8797 type @samp{sh ./configure} instead to prevent @code{csh} from trying
8798 to execute @code{configure} itself.
8799
8800 Running @code{configure} takes some time.
8801 While running, it prints messages telling which features it is
8802 checking for.
8803
8804 @item
8805 Type @samp{make} to compile Bash and build the @code{bashbug} bug
8806 reporting script.
8807
8808 @item
8809 Optionally, type @samp{make tests} to run the Bash test suite.
8810
8811 @item
8812 Type @samp{make install} to install @code{bash} and @code{bashbug}.
8813 This will also install the manual pages and Info file.
8814
8815 @end enumerate
8816
8817 The @code{configure} shell script attempts to guess correct
8818 values for various system-dependent variables used during
8819 compilation. It uses those values to create a @file{Makefile} in
8820 each directory of the package (the top directory, the
8821 @file{builtins}, @file{doc}, and @file{support} directories,
8822 each directory under @file{lib}, and several others). It also creates a
8823 @file{config.h} file containing system-dependent definitions.
8824 Finally, it creates a shell script named @code{config.status} that you
8825 can run in the future to recreate the current configuration, a
8826 file @file{config.cache} that saves the results of its tests to
8827 speed up reconfiguring, and a file @file{config.log} containing
8828 compiler output (useful mainly for debugging @code{configure}).
8829 If at some point
8830 @file{config.cache} contains results you don't want to keep, you
8831 may remove or edit it.
8832
8833 To find out more about the options and arguments that the
8834 @code{configure} script understands, type
8835
8836 @example
8837 bash-4.2$ ./configure --help
8838 @end example
8839
8840 @noindent
8841 at the Bash prompt in your Bash source directory.
8842
8843 If you want to build Bash in a directory separate from the source
8844 directory -- to build for multiple architectures, for example --
8845 just use the full path to the configure script. The following commands
8846 will build bash in a directory under @file{/usr/local/build} from
8847 the source code in @file{/usr/local/src/bash-4.4}:
8848
8849 @example
8850 mkdir /usr/local/build/bash-4.4
8851 cd /usr/local/build/bash-4.4
8852 bash /usr/local/src/bash-4.4/configure
8853 make
8854 @end example
8855
8856 See @ref{Compiling For Multiple Architectures} for more information
8857 about building in a directory separate from the source.
8858
8859 If you need to do unusual things to compile Bash, please
8860 try to figure out how @code{configure} could check whether or not
8861 to do them, and mail diffs or instructions to
8862 @email{bash-maintainers@@gnu.org} so they can be
8863 considered for the next release.
8864
8865 The file @file{configure.ac} is used to create @code{configure}
8866 by a program called Autoconf. You only need
8867 @file{configure.ac} if you want to change it or regenerate
8868 @code{configure} using a newer version of Autoconf. If
8869 you do this, make sure you are using Autoconf version 2.50 or
8870 newer.
8871
8872 You can remove the program binaries and object files from the
8873 source code directory by typing @samp{make clean}. To also remove the
8874 files that @code{configure} created (so you can compile Bash for
8875 a different kind of computer), type @samp{make distclean}.
8876
8877 @node Compilers and Options
8878 @section Compilers and Options
8879
8880 Some systems require unusual options for compilation or linking
8881 that the @code{configure} script does not know about. You can
8882 give @code{configure} initial values for variables by setting
8883 them in the environment. Using a Bourne-compatible shell, you
8884 can do that on the command line like this:
8885
8886 @example
8887 CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
8888 @end example
8889
8890 On systems that have the @code{env} program, you can do it like this:
8891
8892 @example
8893 env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
8894 @end example
8895
8896 The configuration process uses GCC to build Bash if it
8897 is available.
8898
8899 @node Compiling For Multiple Architectures
8900 @section Compiling For Multiple Architectures
8901
8902 You can compile Bash for more than one kind of computer at the
8903 same time, by placing the object files for each architecture in their
8904 own directory. To do this, you must use a version of @code{make} that
8905 supports the @code{VPATH} variable, such as GNU @code{make}.
8906 @code{cd} to the
8907 directory where you want the object files and executables to go and run
8908 the @code{configure} script from the source directory
8909 (@pxref{Basic Installation}).
8910 You may need to
8911 supply the @option{--srcdir=PATH} argument to tell @code{configure} where the
8912 source files are. @code{configure} automatically checks for the
8913 source code in the directory that @code{configure} is in and in `..'.
8914
8915 If you have to use a @code{make} that does not supports the @code{VPATH}
8916 variable, you can compile Bash for one architecture at a
8917 time in the source code directory. After you have installed
8918 Bash for one architecture, use @samp{make distclean} before
8919 reconfiguring for another architecture.
8920
8921 Alternatively, if your system supports symbolic links, you can use the
8922 @file{support/mkclone} script to create a build tree which has
8923 symbolic links back to each file in the source directory. Here's an
8924 example that creates a build directory in the current directory from a
8925 source directory @file{/usr/gnu/src/bash-2.0}:
8926
8927 @example
8928 bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 .
8929 @end example
8930
8931 @noindent
8932 The @code{mkclone} script requires Bash, so you must have already built
8933 Bash for at least one architecture before you can create build
8934 directories for other architectures.
8935
8936 @node Installation Names
8937 @section Installation Names
8938
8939 By default, @samp{make install} will install into
8940 @file{/usr/local/bin}, @file{/usr/local/man}, etc. You can
8941 specify an installation prefix other than @file{/usr/local} by
8942 giving @code{configure} the option @option{--prefix=@var{PATH}},
8943 or by specifying a value for the @env{DESTDIR} @samp{make}
8944 variable when running @samp{make install}.
8945
8946 You can specify separate installation prefixes for
8947 architecture-specific files and architecture-independent files.
8948 If you give @code{configure} the option
8949 @option{--exec-prefix=@var{PATH}}, @samp{make install} will use
8950 @var{PATH} as the prefix for installing programs and libraries.
8951 Documentation and other data files will still use the regular prefix.
8952
8953 @node Specifying the System Type
8954 @section Specifying the System Type
8955
8956 There may be some features @code{configure} can not figure out
8957 automatically, but need to determine by the type of host Bash
8958 will run on. Usually @code{configure} can figure that
8959 out, but if it prints a message saying it can not guess the host
8960 type, give it the @option{--host=TYPE} option. @samp{TYPE} can
8961 either be a short name for the system type, such as @samp{sun4},
8962 or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM}
8963 (e.g., @samp{i386-unknown-freebsd4.2}).
8964
8965 See the file @file{support/config.sub} for the possible
8966 values of each field.
8967
8968 @node Sharing Defaults
8969 @section Sharing Defaults
8970
8971 If you want to set default values for @code{configure} scripts to
8972 share, you can create a site shell script called
8973 @code{config.site} that gives default values for variables like
8974 @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure}
8975 looks for @file{PREFIX/share/config.site} if it exists, then
8976 @file{PREFIX/etc/config.site} if it exists. Or, you can set the
8977 @code{CONFIG_SITE} environment variable to the location of the site
8978 script. A warning: the Bash @code{configure} looks for a site script,
8979 but not all @code{configure} scripts do.
8980
8981 @node Operation Controls
8982 @section Operation Controls
8983
8984 @code{configure} recognizes the following options to control how it
8985 operates.
8986
8987 @table @code
8988
8989 @item --cache-file=@var{file}
8990 Use and save the results of the tests in
8991 @var{file} instead of @file{./config.cache}. Set @var{file} to
8992 @file{/dev/null} to disable caching, for debugging
8993 @code{configure}.
8994
8995 @item --help
8996 Print a summary of the options to @code{configure}, and exit.
8997
8998 @item --quiet
8999 @itemx --silent
9000 @itemx -q
9001 Do not print messages saying which checks are being made.
9002
9003 @item --srcdir=@var{dir}
9004 Look for the Bash source code in directory @var{dir}. Usually
9005 @code{configure} can determine that directory automatically.
9006
9007 @item --version
9008 Print the version of Autoconf used to generate the @code{configure}
9009 script, and exit.
9010 @end table
9011
9012 @code{configure} also accepts some other, not widely used, boilerplate
9013 options. @samp{configure --help} prints the complete list.
9014
9015 @node Optional Features
9016 @section Optional Features
9017
9018 The Bash @code{configure} has a number of @option{--enable-@var{feature}}
9019 options, where @var{feature} indicates an optional part of Bash.
9020 There are also several @option{--with-@var{package}} options,
9021 where @var{package} is something like @samp{bash-malloc} or @samp{purify}.
9022 To turn off the default use of a package, use
9023 @option{--without-@var{package}}. To configure Bash without a feature
9024 that is enabled by default, use @option{--disable-@var{feature}}.
9025
9026 Here is a complete list of the @option{--enable-} and
9027 @option{--with-} options that the Bash @code{configure} recognizes.
9028
9029 @table @code
9030 @item --with-afs
9031 Define if you are using the Andrew File System from Transarc.
9032
9033 @item --with-bash-malloc
9034 Use the Bash version of
9035 @code{malloc} in the directory @file{lib/malloc}. This is not the same
9036 @code{malloc} that appears in @sc{gnu} libc, but an older version
9037 originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc}
9038 is very fast, but wastes some space on each allocation.
9039 This option is enabled by default.
9040 The @file{NOTES} file contains a list of systems for
9041 which this should be turned off, and @code{configure} disables this
9042 option automatically for a number of systems.
9043
9044 @item --with-curses
9045 Use the curses library instead of the termcap library. This should
9046 be supplied if your system has an inadequate or incomplete termcap
9047 database.
9048
9049 @item --with-gnu-malloc
9050 A synonym for @code{--with-bash-malloc}.
9051
9052 @item --with-installed-readline[=@var{PREFIX}]
9053 Define this to make Bash link with a locally-installed version of Readline
9054 rather than the version in @file{lib/readline}. This works only with
9055 Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not
9056 supplied, @code{configure} uses the values of the make variables
9057 @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix}
9058 by default, to find the installed version of Readline if it is not in
9059 the standard system include and library directories.
9060 If @var{PREFIX} is @code{no}, Bash links with the version in
9061 @file{lib/readline}.
9062 If @var{PREFIX} is set to any other value, @code{configure} treats it as
9063 a directory pathname and looks for
9064 the installed version of Readline in subdirectories of that directory
9065 (include files in @var{PREFIX}/@code{include} and the library in
9066 @var{PREFIX}/@code{lib}).
9067
9068 @item --with-libintl-prefix[=@var{PREFIX}]
9069 Define this to make Bash link with a locally-installed version of the
9070 libintl library instead ofthe version in @file{lib/intl}.
9071
9072 @item --with-libiconv-prefix[=@var{PREFIX}]
9073 Define this to make Bash look for libiconv in @var{PREFIX} instead of the
9074 standard system locations. There is no version included with Bash.
9075
9076 @item --enable-minimal-config
9077 This produces a shell with minimal features, close to the historical
9078 Bourne shell.
9079 @end table
9080
9081 There are several @option{--enable-} options that alter how Bash is
9082 compiled, linked, and installed, rather than changing run-time features.
9083
9084 @table @code
9085 @item --enable-largefile
9086 Enable support for @uref{http://www.unix.org/version2/whatsnew/lfs20mar.html,
9087 large files} if the operating system requires special compiler options
9088 to build programs which can access large files. This is enabled by
9089 default, if the operating system provides large file support.
9090
9091 @item --enable-profiling
9092 This builds a Bash binary that produces profiling information to be
9093 processed by @code{gprof} each time it is executed.
9094
9095 @item --enable-separate-helpfiles
9096 Use external files for the documentation displayed by the @code{help} builtin
9097 instead of storing the text internally.
9098
9099 @item --enable-static-link
9100 This causes Bash to be linked statically, if @code{gcc} is being used.
9101 This could be used to build a version to use as root's shell.
9102
9103 @end table
9104
9105 The @samp{minimal-config} option can be used to disable all of
9106 the following options, but it is processed first, so individual
9107 options may be enabled using @samp{enable-@var{feature}}.
9108
9109 All of the following options except for @samp{disabled-builtins},
9110 @samp{direxpand-default},
9111 @samp{strict-posix-default},
9112 and
9113 @samp{xpg-echo-default} are
9114 enabled by default, unless the operating system does not provide the
9115 necessary support.
9116
9117 @table @code
9118 @item --enable-alias
9119 Allow alias expansion and include the @code{alias} and @code{unalias}
9120 builtins (@pxref{Aliases}).
9121
9122 @item --enable-arith-for-command
9123 Include support for the alternate form of the @code{for} command
9124 that behaves like the C language @code{for} statement
9125 (@pxref{Looping Constructs}).
9126
9127 @item --enable-array-variables
9128 Include support for one-dimensional array shell variables
9129 (@pxref{Arrays}).
9130
9131 @item --enable-bang-history
9132 Include support for @code{csh}-like history substitution
9133 (@pxref{History Interaction}).
9134
9135 @item --enable-brace-expansion
9136 Include @code{csh}-like brace expansion
9137 ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ).
9138 See @ref{Brace Expansion}, for a complete description.
9139
9140 @item --enable-casemod-attributes
9141 Include support for case-modifying attributes in the @code{declare} builtin
9142 and assignment statements. Variables with the @code{uppercase} attribute,
9143 for example, will have their values converted to uppercase upon assignment.
9144
9145 @item --enable-casemod-expansion
9146 Include support for case-modifying word expansions.
9147
9148 @item --enable-command-timing
9149 Include support for recognizing @code{time} as a reserved word and for
9150 displaying timing statistics for the pipeline following @code{time}
9151 (@pxref{Pipelines}).
9152 This allows pipelines as well as shell builtins and functions to be timed.
9153
9154 @item --enable-cond-command
9155 Include support for the @code{[[} conditional command.
9156 (@pxref{Conditional Constructs}).
9157
9158 @item --enable-cond-regexp
9159 Include support for matching @sc{posix} regular expressions using the
9160 @samp{=~} binary operator in the @code{[[} conditional command.
9161 (@pxref{Conditional Constructs}).
9162
9163 @item --enable-coprocesses
9164 Include support for coprocesses and the @code{coproc} reserved word
9165 (@pxref{Pipelines}).
9166
9167 @item --enable-debugger
9168 Include support for the bash debugger (distributed separately).
9169
9170 @item --enable-dev-fd-stat-broken
9171 If calling @code{stat} on /dev/fd/@var{N} returns different results than
9172 calling @code{fstat} on file descriptor @var{N}, supply this option to
9173 enable a workaround.
9174 This has implications for conditional commands that test file attributes.
9175
9176 @item --enable-direxpand-default
9177 Cause the @code{direxpand} shell option (@pxref{The Shopt Builtin})
9178 to be enabled by default when the shell starts.
9179 It is normally disabled by default.
9180
9181 @item --enable-directory-stack
9182 Include support for a @code{csh}-like directory stack and the
9183 @code{pushd}, @code{popd}, and @code{dirs} builtins
9184 (@pxref{The Directory Stack}).
9185
9186 @item --enable-disabled-builtins
9187 Allow builtin commands to be invoked via @samp{builtin xxx}
9188 even after @code{xxx} has been disabled using @samp{enable -n xxx}.
9189 See @ref{Bash Builtins}, for details of the @code{builtin} and
9190 @code{enable} builtin commands.
9191
9192 @item --enable-dparen-arithmetic
9193 Include support for the @code{((@dots{}))} command
9194 (@pxref{Conditional Constructs}).
9195
9196 @item --enable-extended-glob
9197 Include support for the extended pattern matching features described
9198 above under @ref{Pattern Matching}.
9199
9200 @item --enable-extended-glob-default
9201 Set the default value of the @code{extglob} shell option described
9202 above under @ref{The Shopt Builtin} to be enabled.
9203
9204 @item --enable-function-import
9205 Include support for importing function definitions exported by another
9206 instance of the shell from the environment. This option is enabled by
9207 default.
9208
9209 @item --enable-glob-asciirange-default
9210 Set the default value of the @code{globasciiranges} shell option described
9211 above under @ref{The Shopt Builtin} to be enabled.
9212 This controls the behavior of character ranges when used in pattern matching
9213 bracket expressions.
9214
9215 @item --enable-help-builtin
9216 Include the @code{help} builtin, which displays help on shell builtins and
9217 variables (@pxref{Bash Builtins}).
9218
9219 @item --enable-history
9220 Include command history and the @code{fc} and @code{history}
9221 builtin commands (@pxref{Bash History Facilities}).
9222
9223 @item --enable-job-control
9224 This enables the job control features (@pxref{Job Control}),
9225 if the operating system supports them.
9226
9227 @item --enable-multibyte
9228 This enables support for multibyte characters if the operating
9229 system provides the necessary support.
9230
9231 @item --enable-net-redirections
9232 This enables the special handling of filenames of the form
9233 @code{/dev/tcp/@var{host}/@var{port}} and
9234 @code{/dev/udp/@var{host}/@var{port}}
9235 when used in redirections (@pxref{Redirections}).
9236
9237 @item --enable-process-substitution
9238 This enables process substitution (@pxref{Process Substitution}) if
9239 the operating system provides the necessary support.
9240
9241 @item --enable-progcomp
9242 Enable the programmable completion facilities
9243 (@pxref{Programmable Completion}).
9244 If Readline is not enabled, this option has no effect.
9245
9246 @item --enable-prompt-string-decoding
9247 Turn on the interpretation of a number of backslash-escaped characters
9248 in the @env{$PS0}, @env{$PS1}, @env{$PS2}, and @env{$PS4} prompt
9249 strings. See @ref{Controlling the Prompt}, for a complete list of prompt
9250 string escape sequences.
9251
9252 @item --enable-readline
9253 Include support for command-line editing and history with the Bash
9254 version of the Readline library (@pxref{Command Line Editing}).
9255
9256 @item --enable-restricted
9257 Include support for a @dfn{restricted shell}. If this is enabled, Bash,
9258 when called as @code{rbash}, enters a restricted mode. See
9259 @ref{The Restricted Shell}, for a description of restricted mode.
9260
9261 @item --enable-select
9262 Include the @code{select} compound command, which allows the generation of
9263 simple menus (@pxref{Conditional Constructs}).
9264
9265 @item --enable-single-help-strings
9266 Store the text displayed by the @code{help} builtin as a single string for
9267 each help topic. This aids in translating the text to different languages.
9268 You may need to disable this if your compiler cannot handle very long string
9269 literals.
9270
9271 @item --enable-strict-posix-default
9272 Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}).
9273
9274 @item --enable-usg-echo-default
9275 A synonym for @code{--enable-xpg-echo-default}.
9276
9277 @item --enable-xpg-echo-default
9278 Make the @code{echo} builtin expand backslash-escaped characters by default,
9279 without requiring the @option{-e} option.
9280 This sets the default value of the @code{xpg_echo} shell option to @code{on},
9281 which makes the Bash @code{echo} behave more like the version specified in
9282 the Single Unix Specification, version 3.
9283 @xref{Bash Builtins}, for a description of the escape sequences that
9284 @code{echo} recognizes.
9285 @end table
9286
9287 The file @file{config-top.h} contains C Preprocessor
9288 @samp{#define} statements for options which are not settable from
9289 @code{configure}.
9290 Some of these are not meant to be changed; beware of the consequences if
9291 you do.
9292 Read the comments associated with each definition for more
9293 information about its effect.
9294
9295 @node Reporting Bugs
9296 @appendix Reporting Bugs
9297
9298 Please report all bugs you find in Bash.
9299 But first, you should
9300 make sure that it really is a bug, and that it appears in the latest
9301 version of Bash.
9302 The latest version of Bash is always available for FTP from
9303 @uref{ftp://ftp.gnu.org/pub/gnu/bash/} and from
9304 @uref{http://git.savannah.gnu.org/cgit/bash.git/snapshot/bash-master.tar.gz}.
9305
9306 Once you have determined that a bug actually exists, use the
9307 @code{bashbug} command to submit a bug report.
9308 If you have a fix, you are encouraged to mail that as well!
9309 Suggestions and `philosophical' bug reports may be mailed
9310 to @email{bug-bash@@gnu.org} or posted to the Usenet
9311 newsgroup @code{gnu.bash.bug}.
9312
9313 All bug reports should include:
9314 @itemize @bullet
9315 @item
9316 The version number of Bash.
9317 @item
9318 The hardware and operating system.
9319 @item
9320 The compiler used to compile Bash.
9321 @item
9322 A description of the bug behaviour.
9323 @item
9324 A short script or `recipe' which exercises the bug and may be used
9325 to reproduce it.
9326 @end itemize
9327
9328 @noindent
9329 @code{bashbug} inserts the first three items automatically into
9330 the template it provides for filing a bug report.
9331
9332 Please send all reports concerning this manual to
9333 @email{bug-bash@@gnu.org}.
9334
9335 @node Major Differences From The Bourne Shell
9336 @appendix Major Differences From The Bourne Shell
9337
9338 Bash implements essentially the same grammar, parameter and
9339 variable expansion, redirection, and quoting as the Bourne Shell.
9340 Bash uses the @sc{posix} standard as the specification of
9341 how these features are to be implemented. There are some
9342 differences between the traditional Bourne shell and Bash; this
9343 section quickly details the differences of significance. A
9344 number of these differences are explained in greater depth in
9345 previous sections.
9346 This section uses the version of @code{sh} included in SVR4.2 (the
9347 last version of the historical Bourne shell) as the baseline reference.
9348
9349 @itemize @bullet
9350
9351 @item
9352 Bash is @sc{posix}-conformant, even where the @sc{posix} specification
9353 differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}).
9354
9355 @item
9356 Bash has multi-character invocation options (@pxref{Invoking Bash}).
9357
9358 @item
9359 Bash has command-line editing (@pxref{Command Line Editing}) and
9360 the @code{bind} builtin.
9361
9362 @item
9363 Bash provides a programmable word completion mechanism
9364 (@pxref{Programmable Completion}), and builtin commands
9365 @code{complete}, @code{compgen}, and @code{compopt}, to
9366 manipulate it.
9367
9368 @item
9369 Bash has command history (@pxref{Bash History Facilities}) and the
9370 @code{history} and @code{fc} builtins to manipulate it.
9371 The Bash history list maintains timestamp information and uses the
9372 value of the @code{HISTTIMEFORMAT} variable to display it.
9373
9374 @item
9375 Bash implements @code{csh}-like history expansion
9376 (@pxref{History Interaction}).
9377
9378 @item
9379 Bash has one-dimensional array variables (@pxref{Arrays}), and the
9380 appropriate variable expansions and assignment syntax to use them.
9381 Several of the Bash builtins take options to act on arrays.
9382 Bash provides a number of built-in array variables.
9383
9384 @item
9385 The @code{$'@dots{}'} quoting syntax, which expands ANSI-C
9386 backslash-escaped characters in the text between the single quotes,
9387 is supported (@pxref{ANSI-C Quoting}).
9388
9389 @item
9390 Bash supports the @code{$"@dots{}"} quoting syntax to do
9391 locale-specific translation of the characters between the double
9392 quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings}
9393 invocation options list the translatable strings found in a script
9394 (@pxref{Locale Translation}).
9395
9396 @item
9397 Bash implements the @code{!} keyword to negate the return value of
9398 a pipeline (@pxref{Pipelines}).
9399 Very useful when an @code{if} statement needs to act only if a test fails.
9400 The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to
9401 return a failure status if any command fails.
9402
9403 @item
9404 Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}).
9405 The display of the timing statistics may be controlled with the
9406 @env{TIMEFORMAT} variable.
9407
9408 @item
9409 Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))}
9410 arithmetic for command, similar to the C language (@pxref{Looping Constructs}).
9411
9412 @item
9413 Bash includes the @code{select} compound command, which allows the
9414 generation of simple menus (@pxref{Conditional Constructs}).
9415
9416 @item
9417 Bash includes the @code{[[} compound command, which makes conditional
9418 testing part of the shell grammar (@pxref{Conditional Constructs}), including
9419 optional regular expression matching.
9420
9421 @item
9422 Bash provides optional case-insensitive matching for the @code{case} and
9423 @code{[[} constructs.
9424
9425 @item
9426 Bash includes brace expansion (@pxref{Brace Expansion}) and tilde
9427 expansion (@pxref{Tilde Expansion}).
9428
9429 @item
9430 Bash implements command aliases and the @code{alias} and @code{unalias}
9431 builtins (@pxref{Aliases}).
9432
9433 @item
9434 Bash provides shell arithmetic, the @code{((} compound command
9435 (@pxref{Conditional Constructs}),
9436 and arithmetic expansion (@pxref{Shell Arithmetic}).
9437
9438 @item
9439 Variables present in the shell's initial environment are automatically
9440 exported to child processes. The Bourne shell does not normally do
9441 this unless the variables are explicitly marked using the @code{export}
9442 command.
9443
9444 @item
9445 Bash supports the @samp{+=} assignment operator, which appends to the value
9446 of the variable named on the left hand side.
9447
9448 @item
9449 Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%}
9450 and @samp{##} expansions to remove leading or trailing substrings from
9451 variable values (@pxref{Shell Parameter Expansion}).
9452
9453 @item
9454 The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}},
9455 is supported (@pxref{Shell Parameter Expansion}).
9456
9457 @item
9458 The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}},
9459 which expands to the substring of @code{var}'s value of length
9460 @var{length}, beginning at @var{offset}, is present
9461 (@pxref{Shell Parameter Expansion}).
9462
9463 @item
9464 The expansion
9465 @code{$@{@var{var}/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}},
9466 which matches @var{pattern} and replaces it with @var{replacement} in
9467 the value of @var{var}, is available (@pxref{Shell Parameter Expansion}).
9468
9469 @item
9470 The expansion @code{$@{!@var{prefix}*@}} expansion, which expands to
9471 the names of all shell variables whose names begin with @var{prefix},
9472 is available (@pxref{Shell Parameter Expansion}).
9473
9474 @item
9475 Bash has indirect variable expansion using @code{$@{!word@}}
9476 (@pxref{Shell Parameter Expansion}).
9477
9478 @item
9479 Bash can expand positional parameters beyond @code{$9} using
9480 @code{$@{@var{num}@}}.
9481
9482 @item
9483 The @sc{posix} @code{$()} form of command substitution
9484 is implemented (@pxref{Command Substitution}),
9485 and preferred to the Bourne shell's @code{``} (which
9486 is also implemented for backwards compatibility).
9487
9488 @item
9489 Bash has process substitution (@pxref{Process Substitution}).
9490
9491 @item
9492 Bash automatically assigns variables that provide information about the
9493 current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host
9494 (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}),
9495 and the instance of Bash that is running (@env{BASH},
9496 @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables},
9497 for details.
9498
9499 @item
9500 The @env{IFS} variable is used to split only the results of expansion,
9501 not all words (@pxref{Word Splitting}).
9502 This closes a longstanding shell security hole.
9503
9504 @item
9505 The filename expansion bracket expression code uses @samp{!} and @samp{^}
9506 to negate the set of characters between the brackets.
9507 The Bourne shell uses only @samp{!}.
9508
9509 @item
9510 Bash implements the full set of @sc{posix} filename expansion operators,
9511 including character classes, equivalence classes, and
9512 collating symbols (@pxref{Filename Expansion}).
9513
9514 @item
9515 Bash implements extended pattern matching features when the @code{extglob}
9516 shell option is enabled (@pxref{Pattern Matching}).
9517
9518 @item
9519 It is possible to have a variable and a function with the same name;
9520 @code{sh} does not separate the two name spaces.
9521
9522 @item
9523 Bash functions are permitted to have local variables using the
9524 @code{local} builtin, and thus useful recursive functions may be written
9525 (@pxref{Bash Builtins}).
9526
9527 @item
9528 Variable assignments preceding commands affect only that command, even
9529 builtins and functions (@pxref{Environment}).
9530 In @code{sh}, all variable assignments
9531 preceding commands are global unless the command is executed from the
9532 file system.
9533
9534 @item
9535 Bash performs filename expansion on filenames specified as operands
9536 to input and output redirection operators (@pxref{Redirections}).
9537
9538 @item
9539 Bash contains the @samp{<>} redirection operator, allowing a file to be
9540 opened for both reading and writing, and the @samp{&>} redirection
9541 operator, for directing standard output and standard error to the same
9542 file (@pxref{Redirections}).
9543
9544 @item
9545 Bash includes the @samp{<<<} redirection operator, allowing a string to
9546 be used as the standard input to a command.
9547
9548 @item
9549 Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}}
9550 redirection operators, which move one file descriptor to another.
9551
9552 @item
9553 Bash treats a number of filenames specially when they are
9554 used in redirection operators (@pxref{Redirections}).
9555
9556 @item
9557 Bash can open network connections to arbitrary machines and services
9558 with the redirection operators (@pxref{Redirections}).
9559
9560 @item
9561 The @code{noclobber} option is available to avoid overwriting existing
9562 files with output redirection (@pxref{The Set Builtin}).
9563 The @samp{>|} redirection operator may be used to override @code{noclobber}.
9564
9565 @item
9566 The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins})
9567 each take @option{-L} and @option{-P} options to switch between logical and
9568 physical modes.
9569
9570 @item
9571 Bash allows a function to override a builtin with the same name, and provides
9572 access to that builtin's functionality within the function via the
9573 @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}).
9574
9575 @item
9576 The @code{command} builtin allows selective disabling of functions
9577 when command lookup is performed (@pxref{Bash Builtins}).
9578
9579 @item
9580 Individual builtins may be enabled or disabled using the @code{enable}
9581 builtin (@pxref{Bash Builtins}).
9582
9583 @item
9584 The Bash @code{exec} builtin takes additional options that allow users
9585 to control the contents of the environment passed to the executed
9586 command, and what the zeroth argument to the command is to be
9587 (@pxref{Bourne Shell Builtins}).
9588
9589 @item
9590 Shell functions may be exported to children via the environment
9591 using @code{export -f} (@pxref{Shell Functions}).
9592
9593 @item
9594 The Bash @code{export}, @code{readonly}, and @code{declare} builtins can
9595 take a @option{-f} option to act on shell functions, a @option{-p} option to
9596 display variables with various attributes set in a format that can be
9597 used as shell input, a @option{-n} option to remove various variable
9598 attributes, and @samp{name=value} arguments to set variable attributes
9599 and values simultaneously.
9600
9601 @item
9602 The Bash @code{hash} builtin allows a name to be associated with
9603 an arbitrary filename, even when that filename cannot be found by
9604 searching the @env{$PATH}, using @samp{hash -p}
9605 (@pxref{Bourne Shell Builtins}).
9606
9607 @item
9608 Bash includes a @code{help} builtin for quick reference to shell
9609 facilities (@pxref{Bash Builtins}).
9610
9611 @item
9612 The @code{printf} builtin is available to display formatted output
9613 (@pxref{Bash Builtins}).
9614
9615 @item
9616 The Bash @code{read} builtin (@pxref{Bash Builtins})
9617 will read a line ending in @samp{\} with
9618 the @option{-r} option, and will use the @env{REPLY} variable as a
9619 default if no non-option arguments are supplied.
9620 The Bash @code{read} builtin
9621 also accepts a prompt string with the @option{-p} option and will use
9622 Readline to obtain the line when given the @option{-e} option.
9623 The @code{read} builtin also has additional options to control input:
9624 the @option{-s} option will turn off echoing of input characters as
9625 they are read, the @option{-t} option will allow @code{read} to time out
9626 if input does not arrive within a specified number of seconds, the
9627 @option{-n} option will allow reading only a specified number of
9628 characters rather than a full line, and the @option{-d} option will read
9629 until a particular character rather than newline.
9630
9631 @item
9632 The @code{return} builtin may be used to abort execution of scripts
9633 executed with the @code{.} or @code{source} builtins
9634 (@pxref{Bourne Shell Builtins}).
9635
9636 @item
9637 Bash includes the @code{shopt} builtin, for finer control of shell
9638 optional capabilities (@pxref{The Shopt Builtin}), and allows these options
9639 to be set and unset at shell invocation (@pxref{Invoking Bash}).
9640
9641 @item
9642 Bash has much more optional behavior controllable with the @code{set}
9643 builtin (@pxref{The Set Builtin}).
9644
9645 @item
9646 The @samp{-x} (@option{xtrace}) option displays commands other than
9647 simple commands when performing an execution trace
9648 (@pxref{The Set Builtin}).
9649
9650 @item
9651 The @code{test} builtin (@pxref{Bourne Shell Builtins})
9652 is slightly different, as it implements the @sc{posix} algorithm,
9653 which specifies the behavior based on the number of arguments.
9654
9655 @item
9656 Bash includes the @code{caller} builtin, which displays the context of
9657 any active subroutine call (a shell function or a script executed with
9658 the @code{.} or @code{source} builtins). This supports the Bash
9659 debugger.
9660
9661 @item
9662 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9663 @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}.
9664 Commands specified with a @code{DEBUG} trap are executed before every
9665 simple command, @code{for} command, @code{case} command,
9666 @code{select} command, every arithmetic @code{for} command, and before
9667 the first command executes in a shell function.
9668 The @code{DEBUG} trap is not inherited by shell functions unless the
9669 function has been given the @code{trace} attribute or the
9670 @code{functrace} option has been enabled using the @code{shopt} builtin.
9671 The @code{extdebug} shell option has additional effects on the
9672 @code{DEBUG} trap.
9673
9674 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an
9675 @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}.
9676 Commands specified with an @code{ERR} trap are executed after a simple
9677 command fails, with a few exceptions.
9678 The @code{ERR} trap is not inherited by shell functions unless the
9679 @code{-o errtrace} option to the @code{set} builtin is enabled.
9680
9681 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9682 @code{RETURN} pseudo-signal specification, similar to
9683 @code{EXIT} and @code{DEBUG}.
9684 Commands specified with an @code{RETURN} trap are executed before
9685 execution resumes after a shell function or a shell script executed with
9686 @code{.} or @code{source} returns.
9687 The @code{RETURN} trap is not inherited by shell functions unless the
9688 function has been given the @code{trace} attribute or the
9689 @code{functrace} option has been enabled using the @code{shopt} builtin.
9690
9691 @item
9692 The Bash @code{type} builtin is more extensive and gives more information
9693 about the names it finds (@pxref{Bash Builtins}).
9694
9695 @item
9696 The Bash @code{umask} builtin permits a @option{-p} option to cause
9697 the output to be displayed in the form of a @code{umask} command
9698 that may be reused as input (@pxref{Bourne Shell Builtins}).
9699
9700 @item
9701 Bash implements a @code{csh}-like directory stack, and provides the
9702 @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it
9703 (@pxref{The Directory Stack}).
9704 Bash also makes the directory stack visible as the value of the
9705 @env{DIRSTACK} shell variable.
9706
9707 @item
9708 Bash interprets special backslash-escaped characters in the prompt
9709 strings when interactive (@pxref{Controlling the Prompt}).
9710
9711 @item
9712 The Bash restricted mode is more useful (@pxref{The Restricted Shell});
9713 the SVR4.2 shell restricted mode is too limited.
9714
9715 @item
9716 The @code{disown} builtin can remove a job from the internal shell
9717 job table (@pxref{Job Control Builtins}) or suppress the sending
9718 of @code{SIGHUP} to a job when the shell exits as the result of a
9719 @code{SIGHUP}.
9720
9721 @item
9722 Bash includes a number of features to support a separate debugger for
9723 shell scripts.
9724
9725 @item
9726 The SVR4.2 shell has two privilege-related builtins
9727 (@code{mldmode} and @code{priv}) not present in Bash.
9728
9729 @item
9730 Bash does not have the @code{stop} or @code{newgrp} builtins.
9731
9732 @item
9733 Bash does not use the @env{SHACCT} variable or perform shell accounting.
9734
9735 @item
9736 The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses
9737 @env{TMOUT}.
9738
9739 @end itemize
9740
9741 @noindent
9742 More features unique to Bash may be found in @ref{Bash Features}.
9743
9744
9745 @appendixsec Implementation Differences From The SVR4.2 Shell
9746
9747 Since Bash is a completely new implementation, it does not suffer from
9748 many of the limitations of the SVR4.2 shell. For instance:
9749
9750 @itemize @bullet
9751
9752 @item
9753 Bash does not fork a subshell when redirecting into or out of
9754 a shell control structure such as an @code{if} or @code{while}
9755 statement.
9756
9757 @item
9758 Bash does not allow unbalanced quotes. The SVR4.2 shell will silently
9759 insert a needed closing quote at @code{EOF} under certain circumstances.
9760 This can be the cause of some hard-to-find errors.
9761
9762 @item
9763 The SVR4.2 shell uses a baroque memory management scheme based on
9764 trapping @code{SIGSEGV}. If the shell is started from a process with
9765 @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library
9766 function call), it misbehaves badly.
9767
9768 @item
9769 In a questionable attempt at security, the SVR4.2 shell,
9770 when invoked without the @option{-p} option, will alter its real
9771 and effective @sc{uid} and @sc{gid} if they are less than some
9772 magic threshold value, commonly 100.
9773 This can lead to unexpected results.
9774
9775 @item
9776 The SVR4.2 shell does not allow users to trap @code{SIGSEGV},
9777 @code{SIGALRM}, or @code{SIGCHLD}.
9778
9779 @item
9780 The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK},
9781 @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset.
9782
9783 @item
9784 The SVR4.2 shell treats @samp{^} as the undocumented equivalent of
9785 @samp{|}.
9786
9787 @item
9788 Bash allows multiple option arguments when it is invoked (@code{-x -v});
9789 the SVR4.2 shell allows only one option argument (@code{-xv}). In
9790 fact, some versions of the shell dump core if the second argument begins
9791 with a @samp{-}.
9792
9793 @item
9794 The SVR4.2 shell exits a script if any builtin fails; Bash exits
9795 a script only if one of the @sc{posix} special builtins fails, and
9796 only for certain failures, as enumerated in the @sc{posix} standard.
9797
9798 @item
9799 The SVR4.2 shell behaves differently when invoked as @code{jsh}
9800 (it turns on job control).
9801 @end itemize
9802
9803 @node GNU Free Documentation License
9804 @appendix GNU Free Documentation License
9805
9806 @include fdl.texi
9807
9808 @node Indexes
9809 @appendix Indexes
9810
9811 @menu
9812 * Builtin Index:: Index of Bash builtin commands.
9813 * Reserved Word Index:: Index of Bash reserved words.
9814 * Variable Index:: Quick reference helps you find the
9815 variable you want.
9816 * Function Index:: Index of bindable Readline functions.
9817 * Concept Index:: General index for concepts described in
9818 this manual.
9819 @end menu
9820
9821 @node Builtin Index
9822 @appendixsec Index of Shell Builtin Commands
9823 @printindex bt
9824
9825 @node Reserved Word Index
9826 @appendixsec Index of Shell Reserved Words
9827 @printindex rw
9828
9829 @node Variable Index
9830 @appendixsec Parameter and Variable Index
9831 @printindex vr
9832
9833 @node Function Index
9834 @appendixsec Function Index
9835 @printindex fn
9836
9837 @node Concept Index
9838 @appendixsec Concept Index
9839 @printindex cp
9840
9841 @bye