1 \input texinfo.tex @c -*- texinfo -*-
3 @setfilename bashref.info
4 @settitle Bash Reference Manual
10 This text is a brief description of the features that are present in
11 the Bash shell (version @value{VERSION}, @value{UPDATED}).
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}.
17 Copyright @copyright{} 1988--2021 Free Software Foundation, Inc.
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''.
35 * Bash: (bash). The GNU Bourne-Again SHell.
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
49 @vskip 0pt plus 1filll
57 @node Top, Introduction, (dir), (dir)
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/}.
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}.
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.
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.
81 * Introduction:: An introduction to the shell.
82 * Definitions:: Some definitions used in the rest of this
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
90 * Command Line Editing:: Chapter describing the command line
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
98 * GNU Free Documentation License:: Copying and sharing this documentation.
99 * Indexes:: Various indexes for this manual.
104 @chapter Introduction
106 * What is Bash?:: A short description of Bash.
107 * What is a shell?:: A brief introduction to shells.
111 @section What is Bash?
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
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
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.
136 @node What is a shell?
137 @section What is a shell?
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.
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
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
158 A shell allows execution of @sc{gnu} commands, both synchronously and
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'
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
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.
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.
193 These definitions are used throughout the remainder of this manual.
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.
204 A space or tab character.
208 A command that is implemented internally by the shell itself, rather
209 than by an executable program somewhere in the file system.
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{)}.
220 The value returned by a command to its caller. The value is restricted
221 to eight bits, so the maximum value is 255.
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.
231 A string of characters used to identify a file.
235 A set of processes comprising a pipeline, and any processes descended
236 from it, that are all in the same process group.
240 A mechanism by which users can selectively stop (suspend) and restart
241 (resume) execution of processes.
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
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}.
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}.
265 @cindex process group
266 A collection of related processes each having the same process
269 @item process group ID
270 @cindex process group ID
271 A unique identifier that represents a @code{process group}
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
281 @cindex return status
282 A synonym for @code{exit status}.
286 A mechanism by which a process may be notified by the kernel
287 of an event occurring in the system.
289 @item special builtin
290 @cindex special builtin
291 A shell builtin command that has been classified as special by the
296 A sequence of characters considered a single unit by the shell.
297 It is either a @code{word} or an @code{operator}.
301 A sequence of characters treated as a unit by the shell.
302 Words may not include unquoted @code{metacharacters}.
305 @node Basic Shell Features
306 @chapter Basic Shell Features
309 Bash is an acronym for @samp{Bourne-Again SHell}.
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.
316 This chapter briefly summarizes the shell's `building blocks':
317 commands, control structures, shell functions, shell @i{parameters},
319 @i{redirections}, which are a way to direct input and output from
320 and to named files, and how the shell executes commands.
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.
335 @section Shell Syntax
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.
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
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.
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.
357 @node Shell Operation
358 @subsection Shell Operation
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
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.
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
377 Parses the tokens into simple and compound commands
378 (@pxref{Shell Commands}).
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.
386 Performs any necessary redirections (@pxref{Redirections}) and removes
387 the redirection operators and their operands from the argument list.
390 Executes the command (@pxref{Executing Commands}).
393 Optionally waits for the command to complete and collects its exit
394 status (@pxref{Exit Status}).
402 * Escape Character:: How to remove the special meaning from a single
404 * Single Quotes:: How to inhibit all interpretation of a sequence
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.
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
418 Each of the shell metacharacters (@pxref{Definitions})
419 has special meaning to the shell and must be quoted if it is to
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.
427 There are three quoting mechanisms: the
428 @dfn{escape character}, single quotes, and double quotes.
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).
440 @subsubsection Single Quotes
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.
447 @subsubsection Double Quotes
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{!}.
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
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.
471 The special parameters @samp{*} and @samp{@@} have special meaning
472 when in double quotes (@pxref{Shell Parameter Expansion}).
475 @subsubsection ANSI-C Quoting
476 @cindex quoting, ANSI
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:
491 an escape character (not ANSI C)
511 the eight-bit character whose value is the octal value @var{nnn}
512 (one to three octal digits)
514 the eight-bit character whose value is the hexadecimal value @var{HH}
515 (one or two hex digits)
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)
523 a control-@var{x} character
527 The expanded result is single-quoted, as if the dollar sign had not
530 @node Locale Translation
531 @subsubsection Locale-Specific Translation
533 @cindex internationalization
534 @cindex native languages
535 @cindex translation, native languages
537 * Creating Internationalized Scripts:: How to use translations and different
538 languages in your scripts.
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 if there are no translations available,
550 of if the string is not translated,
551 the dollar sign is ignored.
552 Since this is a form of double quoting, the string remains double-quoted
553 by default, whether or not it is translated and replaced.
554 If the @code{noexpand_translation} option is enabled
555 using the @code{shopt} builtin (@pxref{The Shopt Builtin}),
556 translated strings are single-quoted instead of double-quoted.
558 The rest of this section is a brief overview of how you use gettext to
559 create translations for strings in a shell script named @var{scriptname}.
560 There are more details in the gettext documentation.
562 @node Creating Internationalized Scripts
563 @cindex internationalized scripts
564 @cindex string translations
565 Once you've marked the strings in your script
566 that you want to translate using $"...",
567 you create a gettext "template" file using the command
570 bash --dump-po-strings @var{scriptname} > @var{domain}.pot
574 The @var{domain} is your @dfn{message domain}.
575 It's just an arbitrary string that's used to identify the files gettext
576 needs, like a package or script name.
577 It needs to be unique among all
578 the message domains on systems where you install the translations, so
579 gettext knows which translations correspond to your script.
580 You'll use the template file to create translations for each target language.
581 The template file conventionally has the suffix @samp{.pot}.
583 You copy this template file to a separate file for each target language
584 you want to support (called "PO" files, which use the suffix @samp{.po}).
585 PO files use various naming conventions, but
586 when you are working to translate a template file into a particular
587 language, you first copy the template file to a file whose name is the
588 language you want to target, with the @samp{.po} suffix.
589 For instance, the Spanish translations of your strings would be
590 in a file named @samp{es.po}, and to get started using a message
591 domain named "example," you would run
598 Ultimately, PO files are often named @var{domain}.po and installed in
599 directories that contain multiple translation files for a particular language.
601 Whichever naming convention you choose, you will need to translate the
602 strings in the PO files into the appropriate languages.
603 This has to be done manually.
605 When you have the translations and PO files complete, you'll use the
606 gettext tools to produce what are called "MO" files, which are compiled
607 versions of the PO files the gettext tools use to look up translations
609 MO files are also called "message catalog" files.
610 You use the @command{msgfmt} program to do this.
611 For instance, if you had a file with Spanish translations, you could run
614 msgfmt -o es.mo es.po
618 to produce the corresponding MO file.
620 Once you have the MO files, you decide where to install them and use the
621 @code{TEXTDOMAINDIR} shell variable to tell the gettext tools where they are.
622 Make sure to use the same message domain to name the MO files
623 as you did for the PO files when you install them.
628 @vindex TEXTDOMAINDIR
629 Your users will use the @env{LANG} or @env{LC_MESSAGES} shell variables to
630 select the desired language.
632 You set the @env{TEXTDOMAIN} variable to the script's message domain.
633 As above, you use the message domain to name your translation files.
635 You, or possibly your users, set the @env{TEXTDOMAINDIR} variable to the
636 name of a directory where the message catalog files are stored.
637 If you install the message files into the system's standard message catalog
638 directory, you don't need to worry about this variable.
640 The directory where the message catalog files are stored varies between
642 Some use the message catalog selected by the @env{LC_MESSAGES}
644 Others create the name of the message catalog from the value of the
645 @env{TEXTDOMAIN} shell variable, possibly adding the @samp{.mo} suffix.
646 If you use the @env{TEXTDOMAIN} variable, you may need to set the
647 @env{TEXTDOMAINDIR} variable to the location of the message catalog files,
649 It's common to use both variables in this fashion:
650 @env{$TEXTDOMAINDIR}/@env{$LC_MESSAGES}/LC_MESSAGES/@env{$TEXTDOMAIN}.mo.
652 If you used that last convention, and you wanted to store the message
653 catalog files with Spanish (es) and Esperanto (eo) translations into a
654 local directory you use for custom translation files, you could run
658 TEXTDOMAINDIR=/usr/local/share/locale
660 cp es.mo $@{TEXTDOMAINDIR@}/es/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
661 cp eo.mo $@{TEXTDOMAINDIR@}/eo/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
664 When all of this is done, and the message catalog files containing the
665 compiled translations are installed in the correct location,
666 your users will be able to see translated strings
667 in any of the supported languages by setting the @env{LANG} or
668 @env{LC_MESSAGES} environment variables before running your script.
672 @cindex comments, shell
674 In a non-interactive shell, or an interactive shell in which the
675 @code{interactive_comments} option to the @code{shopt}
676 builtin is enabled (@pxref{The Shopt Builtin}),
677 a word beginning with @samp{#}
678 causes that word and all remaining characters on that line to
679 be ignored. An interactive shell without the @code{interactive_comments}
680 option enabled does not allow comments. The @code{interactive_comments}
681 option is on by default in interactive shells.
682 @xref{Interactive Shells}, for a description of what makes
686 @section Shell Commands
687 @cindex commands, shell
689 A simple shell command such as @code{echo a b c} consists of the command
690 itself followed by arguments, separated by spaces.
692 More complex shell commands are composed of simple commands arranged together
693 in a variety of ways: in a pipeline in which the output of one command
694 becomes the input of a second, in a loop or conditional construct, or in
698 * Reserved Words:: Words that have special meaning to the shell.
699 * Simple Commands:: The most common type of command.
700 * Pipelines:: Connecting the input and output of several
702 * Lists:: How to execute commands sequentially.
703 * Compound Commands:: Shell commands for control flow.
704 * Coprocesses:: Two-way communication between commands.
705 * GNU Parallel:: Running commands in parallel.
709 @subsection Reserved Words
710 @cindex reserved words
712 Reserved words are words that have special meaning to the shell.
713 They are used to begin and end the shell's compound commands.
715 The following words are recognized as reserved when unquoted and
716 the first word of a command (see below for exceptions):
718 @multitable @columnfractions .1 .1 .1 .1 .12 .1
719 @item @code{if} @tab @code{then} @tab @code{elif}
720 @tab @code{else} @tab @code{fi} @tab @code{time}
721 @item @code{for} @tab @code{in} @tab @code{until}
722 @tab @code{while} @tab @code{do} @tab @code{done}
723 @item @code{case} @tab @code{esac} @tab @code{coproc}
724 @tab @code{select} @tab @code{function}
725 @item @code{@{} @tab @code{@}} @tab @code{[[} @tab @code{]]} @tab @code{!}
729 @code{in} is recognized as a reserved word if it is the third word of a
730 @code{case} or @code{select} command.
731 @code{in} and @code{do} are recognized as reserved
732 words if they are the third word in a @code{for} command.
734 @node Simple Commands
735 @subsection Simple Commands
736 @cindex commands, simple
738 A simple command is the kind of command encountered most often.
739 It's just a sequence of words separated by @code{blank}s, terminated
740 by one of the shell's control operators (@pxref{Definitions}). The
741 first word generally specifies a command to be executed, with the
742 rest of the words being that command's arguments.
744 The return status (@pxref{Exit Status}) of a simple command is
745 its exit status as provided
746 by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
747 the command was terminated by signal @var{n}.
750 @subsection Pipelines
752 @cindex commands, pipelines
754 A @code{pipeline} is a sequence of one or more commands separated by
755 one of the control operators @samp{|} or @samp{|&}.
759 @cindex command timing
760 The format for a pipeline is
762 [time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{}
766 The output of each command in the pipeline is connected via a pipe
767 to the input of the next command.
768 That is, each command reads the previous command's output. This
769 connection is performed before any redirections specified by
772 If @samp{|&} is used, @var{command1}'s standard error, in addition to
773 its standard output, is connected to
774 @var{command2}'s standard input through the pipe;
775 it is shorthand for @code{2>&1 |}.
776 This implicit redirection of the standard error to the standard output is
777 performed after any redirections specified by @var{command1}.
779 The reserved word @code{time} causes timing statistics
780 to be printed for the pipeline once it finishes.
781 The statistics currently consist of elapsed (wall-clock) time and
782 user and system time consumed by the command's execution.
783 The @option{-p} option changes the output format to that specified
785 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
786 it does not recognize @code{time} as a reserved word if the next
787 token begins with a @samp{-}.
788 The @env{TIMEFORMAT} variable may be set to a format string that
789 specifies how the timing information should be displayed.
790 @xref{Bash Variables}, for a description of the available formats.
791 The use of @code{time} as a reserved word permits the timing of
792 shell builtins, shell functions, and pipelines. An external
793 @code{time} command cannot time these easily.
795 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}
796 may be followed by a newline. In this case, the shell displays the
797 total user and system time consumed by the shell and its children.
798 The @env{TIMEFORMAT} variable may be used to specify the format of
799 the time information.
801 If the pipeline is not executed asynchronously (@pxref{Lists}), the
802 shell waits for all commands in the pipeline to complete.
804 Each command in a pipeline is executed in its own @dfn{subshell}, which is a
805 separate process (@pxref{Command Execution Environment}).
806 If the @code{lastpipe} option is enabled using the @code{shopt} builtin
807 (@pxref{The Shopt Builtin}),
808 the last element of a pipeline may be run by the shell process
809 when job control is not active.
812 status of a pipeline is the exit status of the last command in the
813 pipeline, unless the @code{pipefail} option is enabled
814 (@pxref{The Set Builtin}).
815 If @code{pipefail} is enabled, the pipeline's return status is the
816 value of the last (rightmost) command to exit with a non-zero status,
817 or zero if all commands exit successfully.
818 If the reserved word @samp{!} precedes the pipeline, the
819 exit status is the logical negation of the exit status as described
821 The shell waits for all commands in the pipeline to terminate before
825 @subsection Lists of Commands
826 @cindex commands, lists
828 A @code{list} is a sequence of one or more pipelines separated by one
829 of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
830 and optionally terminated by one of @samp{;}, @samp{&}, or a
833 Of these list operators, @samp{&&} and @samp{||}
834 have equal precedence, followed by @samp{;} and @samp{&},
835 which have equal precedence.
837 A sequence of one or more newlines may appear in a @code{list}
838 to delimit commands, equivalent to a semicolon.
840 If a command is terminated by the control operator @samp{&},
841 the shell executes the command asynchronously in a subshell.
842 This is known as executing the command in the @dfn{background},
843 and these are referred to as @dfn{asynchronous} commands.
844 The shell does not wait for the command to finish, and the return
846 When job control is not active (@pxref{Job Control}),
847 the standard input for asynchronous commands, in the absence of any
848 explicit redirections, is redirected from @code{/dev/null}.
850 Commands separated by a @samp{;} are executed sequentially; the shell
851 waits for each command to terminate in turn. The return status is the
852 exit status of the last command executed.
854 @sc{and} and @sc{or} lists are sequences of one or more pipelines
855 separated by the control operators @samp{&&} and @samp{||},
856 respectively. @sc{and} and @sc{or} lists are executed with left
859 An @sc{and} list has the form
861 @var{command1} && @var{command2}
865 @var{command2} is executed if, and only if, @var{command1}
866 returns an exit status of zero (success).
868 An @sc{or} list has the form
870 @var{command1} || @var{command2}
874 @var{command2} is executed if, and only if, @var{command1}
875 returns a non-zero exit status.
878 @sc{and} and @sc{or} lists is the exit status of the last command
879 executed in the list.
881 @node Compound Commands
882 @subsection Compound Commands
883 @cindex commands, compound
886 * Looping Constructs:: Shell commands for iterative action.
887 * Conditional Constructs:: Shell commands for conditional execution.
888 * Command Grouping:: Ways to group commands.
891 Compound commands are the shell programming language constructs.
892 Each construct begins with a reserved word or control operator and is
893 terminated by a corresponding reserved word or operator.
894 Any redirections (@pxref{Redirections}) associated with a compound command
895 apply to all commands within that compound command unless explicitly overridden.
897 In most cases a list of commands in a compound command's description may be
898 separated from the rest of the command by one or more newlines, and may be
899 followed by a newline in place of a semicolon.
901 Bash provides looping constructs, conditional commands, and mechanisms
902 to group commands and execute them as a unit.
904 @node Looping Constructs
905 @subsubsection Looping Constructs
906 @cindex commands, looping
908 Bash supports the following looping constructs.
910 Note that wherever a @samp{;} appears in the description of a
911 command's syntax, it may be replaced with one or more newlines.
918 The syntax of the @code{until} command is:
921 until @var{test-commands}; do @var{consequent-commands}; done
924 Execute @var{consequent-commands} as long as
925 @var{test-commands} has an exit status which is not zero.
926 The return status is the exit status of the last command executed
927 in @var{consequent-commands}, or zero if none was executed.
931 The syntax of the @code{while} command is:
934 while @var{test-commands}; do @var{consequent-commands}; done
937 Execute @var{consequent-commands} as long as
938 @var{test-commands} has an exit status of zero.
939 The return status is the exit status of the last command executed
940 in @var{consequent-commands}, or zero if none was executed.
944 The syntax of the @code{for} command is:
947 for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done
950 Expand @var{words} (@pxref{Shell Expansions}), and execute @var{commands}
952 in the resultant list, with @var{name} bound to the current member.
953 If @samp{in @var{words}} is not present, the @code{for} command
954 executes the @var{commands} once for each positional parameter that is
955 set, as if @samp{in "$@@"} had been specified
956 (@pxref{Special Parameters}).
958 The return status is the exit status of the last command that executes.
959 If there are no items in the expansion of @var{words}, no commands are
960 executed, and the return status is zero.
962 An alternate form of the @code{for} command is also supported:
965 for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
968 First, the arithmetic expression @var{expr1} is evaluated according
969 to the rules described below (@pxref{Shell Arithmetic}).
970 The arithmetic expression @var{expr2} is then evaluated repeatedly
971 until it evaluates to zero.
972 Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
973 executed and the arithmetic expression @var{expr3} is evaluated.
974 If any expression is omitted, it behaves as if it evaluates to 1.
975 The return value is the exit status of the last command in @var{commands}
976 that is executed, or false if any of the expressions is invalid.
979 The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
980 may be used to control loop execution.
982 @node Conditional Constructs
983 @subsubsection Conditional Constructs
984 @cindex commands, conditional
993 The syntax of the @code{if} command is:
996 if @var{test-commands}; then
997 @var{consequent-commands};
998 [elif @var{more-test-commands}; then
999 @var{more-consequents};]
1000 [else @var{alternate-consequents};]
1004 The @var{test-commands} list is executed, and if its return status is zero,
1005 the @var{consequent-commands} list is executed.
1006 If @var{test-commands} returns a non-zero status, each @code{elif} list
1007 is executed in turn, and if its exit status is zero,
1008 the corresponding @var{more-consequents} is executed and the
1010 If @samp{else @var{alternate-consequents}} is present, and
1011 the final command in the final @code{if} or @code{elif} clause
1012 has a non-zero exit status, then @var{alternate-consequents} is executed.
1013 The return status is the exit status of the last command executed, or
1014 zero if no condition tested true.
1020 The syntax of the @code{case} command is:
1024 [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{}
1028 @code{case} will selectively execute the @var{command-list} corresponding to
1029 the first @var{pattern} that matches @var{word}.
1030 The match is performed according
1031 to the rules described below in @ref{Pattern Matching}.
1032 If the @code{nocasematch} shell option
1033 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1034 is enabled, the match is performed without regard to the case
1035 of alphabetic characters.
1036 The @samp{|} is used to separate multiple patterns, and the @samp{)}
1037 operator terminates a pattern list.
1038 A list of patterns and an associated command-list is known
1041 Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.
1042 The @var{word} undergoes tilde expansion, parameter expansion, command
1043 substitution, arithmetic expansion, and quote removal
1044 (@pxref{Shell Parameter Expansion})
1045 before matching is attempted.
1046 Each @var{pattern} undergoes tilde expansion, parameter expansion,
1047 command substitution, arithmetic expansion, process substitution, and
1050 There may be an arbitrary number of @code{case} clauses, each terminated
1051 by a @samp{;;}, @samp{;&}, or @samp{;;&}.
1052 The first pattern that matches determines the
1053 command-list that is executed.
1054 It's a common idiom to use @samp{*} as the final pattern to define the
1055 default case, since that pattern will always match.
1057 Here is an example using @code{case} in a script that could be used to
1058 describe one interesting feature of an animal:
1061 echo -n "Enter the name of an animal: "
1063 echo -n "The $ANIMAL has "
1065 horse | dog | cat) echo -n "four";;
1066 man | kangaroo ) echo -n "two";;
1067 *) echo -n "an unknown number of";;
1074 If the @samp{;;} operator is used, no subsequent matches are attempted after
1075 the first pattern match.
1076 Using @samp{;&} in place of @samp{;;} causes execution to continue with
1077 the @var{command-list} associated with the next clause, if any.
1078 Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns
1079 in the next clause, if any, and execute any associated @var{command-list}
1080 on a successful match,
1081 continuing the case statement execution as if the pattern list had not matched.
1083 The return status is zero if no @var{pattern} is matched. Otherwise, the
1084 return status is the exit status of the @var{command-list} executed.
1089 The @code{select} construct allows the easy generation of menus.
1090 It has almost the same syntax as the @code{for} command:
1093 select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
1096 The list of words following @code{in} is expanded, generating a list
1097 of items. The set of expanded words is printed on the standard
1098 error output stream, each preceded by a number. If the
1099 @samp{in @var{words}} is omitted, the positional parameters are printed,
1100 as if @samp{in "$@@"} had been specified.
1101 The @env{PS3} prompt is then displayed and a line is read from the
1103 If the line consists of a number corresponding to one of the displayed
1104 words, then the value of @var{name} is set to that word.
1105 If the line is empty, the words and prompt are displayed again.
1106 If @code{EOF} is read, the @code{select} command completes.
1107 Any other value read causes @var{name} to be set to null.
1108 The line read is saved in the variable @env{REPLY}.
1110 The @var{commands} are executed after each selection until a
1111 @code{break} command is executed, at which
1112 point the @code{select} command completes.
1114 Here is an example that allows the user to pick a filename from the
1115 current directory, and displays the name and index of the file
1121 echo you picked $fname \($REPLY\)
1128 (( @var{expression} ))
1131 The arithmetic @var{expression} is evaluated according to the rules
1132 described below (@pxref{Shell Arithmetic}).
1133 The @var{expression} undergoes the same expansions
1134 as if it were within double quotes,
1135 but double quote characters in @var{expression} are not treated specially
1137 If the value of the expression is non-zero, the return status is 0;
1138 otherwise the return status is 1.
1145 [[ @var{expression} ]]
1148 Return a status of 0 or 1 depending on the evaluation of
1149 the conditional expression @var{expression}.
1150 Expressions are composed of the primaries described below in
1151 @ref{Bash Conditional Expressions}.
1152 The words between the @code{[[} and @code{]]} do not undergo word splitting
1153 and filename expansion.
1154 The shell performs tilde expansion, parameter and
1155 variable expansion, arithmetic expansion, command substitution, process
1156 substitution, and quote removal on those words
1157 (the expansions that would occur if the words were enclosed in double quotes).
1158 Conditional operators such as @samp{-f} must be unquoted to be recognized
1161 When used with @code{[[}, the @samp{<} and @samp{>} operators sort
1162 lexicographically using the current locale.
1164 When the @samp{==} and @samp{!=} operators are used, the string to the
1165 right of the operator is considered a pattern and matched according
1166 to the rules described below in @ref{Pattern Matching},
1167 as if the @code{extglob} shell option were enabled.
1168 The @samp{=} operator is identical to @samp{==}.
1169 If the @code{nocasematch} shell option
1170 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1171 is enabled, the match is performed without regard to the case
1172 of alphabetic characters.
1173 The return value is 0 if the string matches (@samp{==}) or does not
1174 match (@samp{!=}) the pattern, and 1 otherwise.
1176 If you quote any part of the pattern,
1177 using any of the shell's quoting mechanisms,
1178 the quoted portion is matched literally.
1179 This means every character in the quoted portion matches itself,
1180 instead of having any special pattern matching meaning.
1182 An additional binary operator, @samp{=~}, is available, with the same
1183 precedence as @samp{==} and @samp{!=}.
1184 When you use @samp{=~}, the string to the right of the operator is considered
1185 a @sc{posix} extended regular expression pattern and matched accordingly
1186 (using the @sc{posix} @code{regcomp} and @code{regexec} interfaces
1187 usually described in @i{regex}(3)).
1188 The return value is 0 if the string matches the pattern, and 1 if it does not.
1189 If the regular expression is syntactically incorrect, the conditional
1190 expression returns 2.
1191 If the @code{nocasematch} shell option
1192 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1193 is enabled, the match is performed without regard to the case
1194 of alphabetic characters.
1196 You can quote any part of the pattern
1197 to force the quoted portion to be matched literally
1198 instead of as a regular expression (see above).
1199 If the pattern is stored in a shell variable, quoting the variable
1200 expansion forces the entire pattern to be matched literally.
1202 The pattern will match if it matches any part of the string.
1203 If you want to force the pattern to match the entire string,
1204 anchor the pattern using the @samp{^} and @samp{$} regular expression
1207 For example, the following will match a line
1208 (stored in the shell variable @code{line})
1209 if there is a sequence of characters anywhere in the value consisting of
1210 any number, including zero, of
1211 characters in the @code{space} character class,
1212 immediately followed by zero or one instances of @samp{a},
1216 [[ $line =~ [[:space:]]*(a)?b ]]
1220 That means values for @code{line} like
1221 @samp{aab}, @samp{ aaaaaab}, @samp{xaby}, and @samp{ ab}
1223 as will a line containing a @samp{b} anywhere in its value.
1225 If you want to match a character that's special to the regular expression
1226 grammar (@samp{^$|[]()\.*+?}), it has to be quoted to remove its special
1228 This means that in the pattern @samp{xxx.txt}, the @samp{.} matches any
1229 character in the string (its usual regular expression meaning), but in the
1230 pattern @samp{"xxx.txt"}, it can only match a literal @samp{.}.
1232 Likewise, if you want to include a character in your pattern that has a
1233 special meaning to the regular expression grammar, you must make sure it's
1235 If you want to anchor a pattern at the beginning or end of the string,
1236 for instance, you cannot quote the @samp{^} or @samp{$}
1237 characters using any form of shell quoting.
1239 If you want to match @samp{initial string} at the start of a line,
1240 the following will work:
1242 [[ $line =~ ^"initial string" ]]
1247 [[ $line =~ "^initial string" ]]
1250 because in the second example the @samp{^} is quoted and doesn't have its
1251 usual special meaning.
1253 It is sometimes difficult to specify a regular expression properly
1254 without using quotes, or to keep track of the quoting used by regular
1255 expressions while paying attention to
1256 shell quoting and the shell's quote removal.
1257 Storing the regular expression in a shell variable is often a useful
1258 way to avoid problems with quoting characters that are special to the
1260 For example, the following is equivalent to the pattern used above:
1263 pattern='[[:space:]]*(a)?b'
1264 [[ $line =~ $pattern ]]
1267 Shell programmers should take special care with backslashes, since
1268 backslashes are used by both the shell and regular expressions to remove
1269 the special meaning from the following character.
1270 This means that after the shell's word expansions complete
1271 (@pxref{Shell Expansions}),
1272 any backslashes remaining in parts of the pattern
1273 that were originally not quoted can remove the
1274 special meaning of pattern characters.
1275 If any part of the pattern is quoted, the shell does its best to ensure that
1276 the regular expression treats those remaining backslashes as literal,
1277 if they appeared in a quoted portion.
1279 The following two sets of commands are @emph{not} equivalent:
1287 [[ . =~ "$pattern" ]]
1292 The first two matches will succeed, but the second two will not, because
1293 in the second two the backslash will be part of the pattern to be matched.
1294 In the first two examples, the pattern passed to the regular expression
1295 parser is @samp{\.}. The backslash removes the special meaning from
1296 @samp{.}, so the literal @samp{.} matches.
1297 In the second two examples, the pattern passed to the regular expression
1298 parser has the backslash quoted (e.g., @samp{\\\.}), which will not match
1299 the string, since it does not contain a backslash.
1300 If the string in the first examples were anything other than @samp{.}, say
1301 @samp{a}, the pattern would not match, because the quoted @samp{.} in the
1302 pattern loses its special meaning of matching any single character.
1304 Bracket expressions in regular expressions can be sources of errors as well,
1305 since characters that are normally special in regular expressions
1306 lose their special meanings between brackets.
1307 However, you can use bracket expressions to match special pattern characters
1308 without quoting them, so they are sometimes useful for this purpose.
1310 Though it might seem like a strange way to write it, the following pattern
1311 will match a @samp{.} in the string:
1317 The shell performs any word expansions before passing the pattern
1318 to the regular expression functions,
1319 so you can assume that the shell's quoting takes precedence.
1320 As noted above, the regular expression parser will interpret any
1321 unquoted backslashes remaining in the pattern after shell expansion
1322 according to its own rules.
1323 The intention is to avoid making shell programmers quote things twice
1324 as much as possible, so shell quoting should be sufficient to quote
1325 special pattern characters where that's necessary.
1327 The array variable @code{BASH_REMATCH} records which parts of the string
1328 matched the pattern.
1329 The element of @code{BASH_REMATCH} with index 0 contains the portion of
1330 the string matching the entire regular expression.
1331 Substrings matched by parenthesized subexpressions within the regular
1332 expression are saved in the remaining @code{BASH_REMATCH} indices.
1333 The element of @code{BASH_REMATCH} with index @var{n} is the portion of the
1334 string matching the @var{n}th parenthesized subexpression.
1336 Expressions may be combined using the following operators, listed
1337 in decreasing order of precedence:
1340 @item ( @var{expression} )
1341 Returns the value of @var{expression}.
1342 This may be used to override the normal precedence of operators.
1344 @item ! @var{expression}
1345 True if @var{expression} is false.
1347 @item @var{expression1} && @var{expression2}
1348 True if both @var{expression1} and @var{expression2} are true.
1350 @item @var{expression1} || @var{expression2}
1351 True if either @var{expression1} or @var{expression2} is true.
1355 The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the
1356 value of @var{expression1} is sufficient to determine the return
1357 value of the entire conditional expression.
1360 @node Command Grouping
1361 @subsubsection Grouping Commands
1362 @cindex commands, grouping
1364 Bash provides two ways to group a list of commands to be executed
1365 as a unit. When commands are grouped, redirections may be applied
1366 to the entire command list. For example, the output of all the
1367 commands in the list may be redirected to a single stream.
1375 Placing a list of commands between parentheses forces the shell to create
1376 a subshell (@pxref{Command Execution Environment}), and each
1377 of the commands in @var{list} is executed in that subshell environment.
1378 Since the @var{list} is executed in a subshell, variable assignments do not
1379 remain in effect after the subshell completes.
1388 Placing a list of commands between curly braces causes the list to
1389 be executed in the current shell context. No subshell is created.
1390 The semicolon (or newline) following @var{list} is required.
1393 In addition to the creation of a subshell, there is a subtle difference
1394 between these two constructs due to historical reasons. The braces
1395 are reserved words, so they must be separated from the @var{list}
1396 by @code{blank}s or other shell metacharacters.
1397 The parentheses are operators, and are
1398 recognized as separate tokens by the shell even if they are not separated
1399 from the @var{list} by whitespace.
1401 The exit status of both of these constructs is the exit status of
1405 @subsection Coprocesses
1408 A @code{coprocess} is a shell command preceded by the @code{coproc}
1410 A coprocess is executed asynchronously in a subshell, as if the command
1411 had been terminated with the @samp{&} control operator, with a two-way pipe
1412 established between the executing shell and the coprocess.
1414 The syntax for a coprocess is:
1417 coproc [@var{NAME}] @var{command} [@var{redirections}]
1421 This creates a coprocess named @var{NAME}.
1422 @var{command} may be either a simple command (@pxref{Simple Commands})
1423 or a compound command (@pxref{Compound Commands}).
1424 @var{NAME} is a shell variable name.
1425 If @var{NAME} is not supplied, the default name is @code{COPROC}.
1427 The recommended form to use for a coprocess is
1430 coproc @var{NAME} @{ @var{command}; @}
1434 This form is recommended because simple commands result in the coprocess
1435 always being named @code{COPROC}, and it is simpler to use and more complete
1436 than the other compound commands.
1438 There are other forms of coprocesses:
1441 coproc @var{NAME} @var{compound-command}
1442 coproc @var{compound-command}
1443 coproc @var{simple-command}
1447 If @var{command} is a compound command, @var{NAME} is optional. The
1448 word following @code{coproc} determines whether that word is interpreted
1449 as a variable name: it is interpreted as @var{NAME} if it is not a
1450 reserved word that introduces a compound command.
1451 If @var{command} is a simple command, @var{NAME} is not allowed; this
1452 is to avoid confusion between @var{NAME} and the first word of the simple
1455 When the coprocess is executed, the shell creates an array variable
1457 named @var{NAME} in the context of the executing shell.
1458 The standard output of @var{command}
1459 is connected via a pipe to a file descriptor in the executing shell,
1460 and that file descriptor is assigned to @var{NAME}[0].
1461 The standard input of @var{command}
1462 is connected via a pipe to a file descriptor in the executing shell,
1463 and that file descriptor is assigned to @var{NAME}[1].
1464 This pipe is established before any redirections specified by the
1465 command (@pxref{Redirections}).
1466 The file descriptors can be utilized as arguments to shell commands
1467 and redirections using standard word expansions.
1468 Other than those created to execute command and process substitutions,
1469 the file descriptors are not available in subshells.
1471 The process ID of the shell spawned to execute the coprocess is
1472 available as the value of the variable @env{@var{NAME}_PID}.
1474 builtin command may be used to wait for the coprocess to terminate.
1476 Since the coprocess is created as an asynchronous command,
1477 the @code{coproc} command always returns success.
1478 The return status of a coprocess is the exit status of @var{command}.
1481 @subsection GNU Parallel
1483 There are ways to run commands in parallel that are not built into Bash.
1484 GNU Parallel is a tool to do just that.
1486 GNU Parallel, as its name suggests, can be used to build and run commands
1487 in parallel. You may run the same command with different arguments, whether
1488 they are filenames, usernames, hostnames, or lines read from files. GNU
1489 Parallel provides shorthand references to many of the most common operations
1490 (input lines, various portions of the input line, different ways to specify
1491 the input source, and so on). Parallel can replace @code{xargs} or feed
1492 commands from its input sources to several different instances of Bash.
1494 For a complete description, refer to the GNU Parallel documentation, which
1496 @url{https://www.gnu.org/software/parallel/parallel_tutorial.html}.
1498 @node Shell Functions
1499 @section Shell Functions
1500 @cindex shell function
1501 @cindex functions, shell
1503 Shell functions are a way to group commands for later execution
1504 using a single name for the group. They are executed just like
1505 a "regular" command.
1506 When the name of a shell function is used as a simple command name,
1507 the list of commands associated with that function name is executed.
1508 Shell functions are executed in the current
1509 shell context; no new process is created to interpret them.
1511 Functions are declared using this syntax:
1514 @var{fname} () @var{compound-command} [ @var{redirections} ]
1520 function @var{fname} [()] @var{compound-command} [ @var{redirections} ]
1523 This defines a shell function named @var{fname}. The reserved
1524 word @code{function} is optional.
1525 If the @code{function} reserved
1526 word is supplied, the parentheses are optional.
1527 The @dfn{body} of the function is the compound command
1528 @var{compound-command} (@pxref{Compound Commands}).
1529 That command is usually a @var{list} enclosed between @{ and @}, but
1530 may be any compound command listed above.
1531 If the @code{function} reserved word is used, but the
1532 parentheses are not supplied, the braces are recommended.
1533 @var{compound-command} is executed whenever @var{fname} is specified as the
1534 name of a simple command.
1535 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
1536 @var{fname} must be a valid shell name and
1537 may not be the same as one of the special builtins
1538 (@pxref{Special Builtins}).
1539 In default mode, a function name can be any unquoted shell word that does
1540 not contain @samp{$}.
1541 Any redirections (@pxref{Redirections}) associated with the shell function
1542 are performed when the function is executed.
1543 A function definition may be deleted using the @option{-f} option to the
1544 @code{unset} builtin (@pxref{Bourne Shell Builtins}).
1546 The exit status of a function definition is zero unless a syntax error
1547 occurs or a readonly function with the same name already exists.
1548 When executed, the exit status of a function is the exit status of the
1549 last command executed in the body.
1551 Note that for historical reasons, in the most common usage the curly braces
1552 that surround the body of the function must be separated from the body by
1553 @code{blank}s or newlines.
1554 This is because the braces are reserved words and are only recognized
1555 as such when they are separated from the command list
1556 by whitespace or another shell metacharacter.
1557 Also, when using the braces, the @var{list} must be terminated by a semicolon,
1558 a @samp{&}, or a newline.
1560 When a function is executed, the arguments to the
1561 function become the positional parameters
1562 during its execution (@pxref{Positional Parameters}).
1563 The special parameter @samp{#} that expands to the number of
1564 positional parameters is updated to reflect the change.
1565 Special parameter @code{0} is unchanged.
1566 The first element of the @env{FUNCNAME} variable is set to the
1567 name of the function while the function is executing.
1569 All other aspects of the shell execution
1570 environment are identical between a function and its caller
1571 with these exceptions:
1572 the @env{DEBUG} and @env{RETURN} traps
1573 are not inherited unless the function has been given the
1574 @code{trace} attribute using the @code{declare} builtin or
1575 the @code{-o functrace} option has been enabled with
1576 the @code{set} builtin,
1577 (in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps),
1578 and the @env{ERR} trap is not inherited unless the @code{-o errtrace}
1579 shell option has been enabled.
1580 @xref{Bourne Shell Builtins}, for the description of the
1581 @code{trap} builtin.
1583 The @env{FUNCNEST} variable, if set to a numeric value greater
1584 than 0, defines a maximum function nesting level. Function
1585 invocations that exceed the limit cause the entire command to
1588 If the builtin command @code{return}
1589 is executed in a function, the function completes and
1590 execution resumes with the next command after the function
1592 Any command associated with the @code{RETURN} trap is executed
1593 before execution resumes.
1594 When a function completes, the values of the
1595 positional parameters and the special parameter @samp{#}
1596 are restored to the values they had prior to the function's
1597 execution. If a numeric argument is given to @code{return},
1598 that is the function's return status; otherwise the function's
1599 return status is the exit status of the last command executed
1600 before the @code{return}.
1602 Variables local to the function may be declared with the
1603 @code{local} builtin. These variables are visible only to
1604 the function and the commands it invokes. This is particularly
1605 important when a shell function calls other functions.
1607 Local variables "shadow" variables with the same name declared at
1608 previous scopes. For instance, a local variable declared in a function
1609 hides a global variable of the same name: references and assignments
1610 refer to the local variable, leaving the global variable unmodified.
1611 When the function returns, the global variable is once again visible.
1613 The shell uses @dfn{dynamic scoping} to control a variable's visibility
1615 With dynamic scoping, visible variables and their values
1616 are a result of the sequence of function calls that caused execution
1617 to reach the current function.
1618 The value of a variable that a function sees depends
1619 on its value within its caller, if any, whether that caller is
1620 the "global" scope or another shell function.
1621 This is also the value that a local variable
1622 declaration "shadows", and the value that is restored when the function
1625 For example, if a variable @env{var} is declared as local in function
1626 @code{func1}, and @code{func1} calls another function @code{func2},
1627 references to @env{var} made from within @code{func2} will resolve to the
1628 local variable @env{var} from @code{func1}, shadowing any global variable
1631 The following script demonstrates this behavior.
1632 When executed, the script displays
1635 In func2, var = func1 local
1641 local var='func1 local'
1647 echo "In func2, var = $var"
1654 The @code{unset} builtin also acts using the same dynamic scope: if a
1655 variable is local to the current scope, @code{unset} will unset it;
1656 otherwise the unset will refer to the variable found in any calling scope
1658 If a variable at the current local scope is unset, it will remain so
1659 until it is reset in that scope or until the function returns.
1660 Once the function returns, any instance of the variable at a previous
1661 scope will become visible.
1662 If the unset acts on a variable at a previous scope, any instance of a
1663 variable with that name that had been shadowed will become visible.
1665 Function names and definitions may be listed with the
1666 @option{-f} option to the @code{declare} (@code{typeset})
1667 builtin command (@pxref{Bash Builtins}).
1668 The @option{-F} option to @code{declare} or @code{typeset}
1669 will list the function names only
1670 (and optionally the source file and line number, if the @code{extdebug}
1671 shell option is enabled).
1672 Functions may be exported so that child shell processes
1673 (those created when executing a separate shell invocation)
1674 automatically have them defined with the
1675 @option{-f} option to the @code{export} builtin
1676 (@pxref{Bourne Shell Builtins}).
1678 Functions may be recursive.
1679 The @code{FUNCNEST} variable may be used to limit the depth of the
1680 function call stack and restrict the number of function invocations.
1681 By default, no limit is placed on the number of recursive calls.
1683 @node Shell Parameters
1684 @section Shell Parameters
1686 @cindex variable, shell
1687 @cindex shell variable
1690 * Positional Parameters:: The shell's command-line arguments.
1691 * Special Parameters:: Parameters denoted by special characters.
1694 A @dfn{parameter} is an entity that stores values.
1695 It can be a @code{name}, a number, or one of the special characters
1697 A @dfn{variable} is a parameter denoted by a @code{name}.
1698 A variable has a @code{value} and zero or more @code{attributes}.
1699 Attributes are assigned using the @code{declare} builtin command
1700 (see the description of the @code{declare} builtin in @ref{Bash Builtins}).
1702 A parameter is set if it has been assigned a value. The null string is
1703 a valid value. Once a variable is set, it may be unset only by using
1704 the @code{unset} builtin command.
1706 A variable may be assigned to by a statement of the form
1708 @var{name}=[@var{value}]
1712 is not given, the variable is assigned the null string. All
1713 @var{value}s undergo tilde expansion, parameter and variable expansion,
1714 command substitution, arithmetic expansion, and quote
1715 removal (@pxref{Shell Parameter Expansion}).
1716 If the variable has its @code{integer}
1717 attribute set, then @var{value}
1718 is evaluated as an arithmetic expression even if the @code{$((@dots{}))}
1719 expansion is not used (@pxref{Arithmetic Expansion}).
1720 Word splitting and filename expansion are not performed.
1721 Assignment statements may also appear as arguments to the
1723 @code{declare}, @code{typeset}, @code{export}, @code{readonly},
1724 and @code{local} builtin commands (@dfn{declaration} commands).
1725 When in @sc{posix} mode (@pxref{Bash POSIX Mode}), these builtins may appear
1726 in a command after one or more instances of the @code{command} builtin
1727 and retain these assignment statement properties.
1729 In the context where an assignment statement is assigning a value
1730 to a shell variable or array index (@pxref{Arrays}), the @samp{+=}
1731 operator can be used to
1732 append to or add to the variable's previous value.
1733 This includes arguments to builtin commands such as @code{declare} that
1734 accept assignment statements (declaration commands).
1735 When @samp{+=} is applied to a variable for which the @code{integer} attribute
1736 has been set, @var{value} is evaluated as an arithmetic expression and
1737 added to the variable's current value, which is also evaluated.
1738 When @samp{+=} is applied to an array variable using compound assignment
1739 (@pxref{Arrays}), the
1740 variable's value is not unset (as it is when using @samp{=}), and new
1741 values are appended to the array beginning at one greater than the array's
1742 maximum index (for indexed arrays), or added as additional key-value pairs
1743 in an associative array.
1744 When applied to a string-valued variable, @var{value} is expanded and
1745 appended to the variable's value.
1747 A variable can be assigned the @code{nameref} attribute using the
1748 @option{-n} option to the @code{declare} or @code{local} builtin commands
1749 (@pxref{Bash Builtins})
1750 to create a @dfn{nameref}, or a reference to another variable.
1751 This allows variables to be manipulated indirectly.
1752 Whenever the nameref variable is referenced, assigned to, unset, or has
1753 its attributes modified (other than using or changing the nameref
1754 attribute itself), the
1755 operation is actually performed on the variable specified by the nameref
1757 A nameref is commonly used within shell functions to refer to a variable
1758 whose name is passed as an argument to the function.
1759 For instance, if a variable name is passed to a shell function as its first
1765 inside the function creates a nameref variable @env{ref} whose value is
1766 the variable name passed as the first argument.
1767 References and assignments to @env{ref}, and changes to its attributes,
1768 are treated as references, assignments, and attribute modifications
1769 to the variable whose name was passed as @code{$1}.
1771 If the control variable in a @code{for} loop has the nameref attribute,
1772 the list of words can be a list of shell variables, and a name reference
1773 will be established for each word in the list, in turn, when the loop is
1775 Array variables cannot be given the nameref attribute.
1776 However, nameref variables can reference array variables and subscripted
1778 Namerefs can be unset using the @option{-n} option to the @code{unset} builtin
1779 (@pxref{Bourne Shell Builtins}).
1780 Otherwise, if @code{unset} is executed with the name of a nameref variable
1781 as an argument, the variable referenced by the nameref variable will be unset.
1783 @node Positional Parameters
1784 @subsection Positional Parameters
1785 @cindex parameters, positional
1787 A @dfn{positional parameter} is a parameter denoted by one or more
1788 digits, other than the single digit @code{0}. Positional parameters are
1789 assigned from the shell's arguments when it is invoked,
1790 and may be reassigned using the @code{set} builtin command.
1791 Positional parameter @code{N} may be referenced as @code{$@{N@}}, or
1792 as @code{$N} when @code{N} consists of a single digit.
1793 Positional parameters may not be assigned to with assignment statements.
1794 The @code{set} and @code{shift} builtins are used to set and
1795 unset them (@pxref{Shell Builtin Commands}).
1796 The positional parameters are
1797 temporarily replaced when a shell function is executed
1798 (@pxref{Shell Functions}).
1800 When a positional parameter consisting of more than a single
1801 digit is expanded, it must be enclosed in braces.
1803 @node Special Parameters
1804 @subsection Special Parameters
1805 @cindex parameters, special
1807 The shell treats several parameters specially. These parameters may
1808 only be referenced; assignment to them is not allowed.
1814 ($*) Expands to the positional parameters, starting from one.
1815 When the expansion is not within double quotes, each positional parameter
1816 expands to a separate word.
1817 In contexts where it is performed, those words
1818 are subject to further word splitting and filename expansion.
1819 When the expansion occurs within double quotes, it expands to a single word
1820 with the value of each parameter separated by the first character of the
1821 @env{IFS} special variable. That is, @code{"$*"} is equivalent
1822 to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c}
1823 is the first character of the value of the @code{IFS}
1825 If @env{IFS} is unset, the parameters are separated by spaces.
1826 If @env{IFS} is null, the parameters are joined without intervening
1831 ($@@) Expands to the positional parameters, starting from one.
1832 In contexts where word splitting is performed, this expands each
1833 positional parameter to a separate word; if not within double
1834 quotes, these words are subject to word splitting.
1835 In contexts where word splitting is not performed,
1836 this expands to a single word
1837 with each positional parameter separated by a space.
1839 expansion occurs within double quotes, and word splitting is performed,
1840 each parameter expands to a
1841 separate word. That is, @code{"$@@"} is equivalent to
1842 @code{"$1" "$2" @dots{}}.
1843 If the double-quoted expansion occurs within a word, the expansion of
1844 the first parameter is joined with the beginning part of the original
1845 word, and the expansion of the last parameter is joined with the last
1846 part of the original word.
1847 When there are no positional parameters, @code{"$@@"} and
1849 expand to nothing (i.e., they are removed).
1853 ($#) Expands to the number of positional parameters in decimal.
1857 ($?) Expands to the exit status of the most recently executed foreground
1862 ($-, a hyphen.) Expands to the current option flags as specified upon
1863 invocation, by the @code{set}
1864 builtin command, or those set by the shell itself
1865 (such as the @option{-i} option).
1869 ($$) Expands to the process @sc{id} of the shell. In a subshell, it
1870 expands to the process @sc{id} of the invoking shell, not the subshell.
1874 ($!) Expands to the process @sc{id} of the job most recently placed into the
1875 background, whether executed as an asynchronous command or using
1876 the @code{bg} builtin (@pxref{Job Control Builtins}).
1880 ($0) Expands to the name of the shell or shell script. This is set at
1881 shell initialization. If Bash is invoked with a file of commands
1882 (@pxref{Shell Scripts}), @code{$0} is set to the name of that file.
1883 If Bash is started with the @option{-c} option (@pxref{Invoking Bash}),
1884 then @code{$0} is set to the first argument after the string to be
1885 executed, if one is present. Otherwise, it is set
1886 to the filename used to invoke Bash, as given by argument zero.
1889 @node Shell Expansions
1890 @section Shell Expansions
1893 Expansion is performed on the command line after it has been split into
1894 @code{token}s. There are seven kinds of expansion performed:
1897 @item brace expansion
1898 @item tilde expansion
1899 @item parameter and variable expansion
1900 @item command substitution
1901 @item arithmetic expansion
1902 @item word splitting
1903 @item filename expansion
1907 * Brace Expansion:: Expansion of expressions within braces.
1908 * Tilde Expansion:: Expansion of the ~ character.
1909 * Shell Parameter Expansion:: How Bash expands variables to their values.
1910 * Command Substitution:: Using the output of a command as an argument.
1911 * Arithmetic Expansion:: How to use arithmetic in shell expansions.
1912 * Process Substitution:: A way to write and read to and from a
1914 * Word Splitting:: How the results of expansion are split into separate
1916 * Filename Expansion:: A shorthand for specifying filenames matching patterns.
1917 * Quote Removal:: How and when quote characters are removed from
1921 The order of expansions is:
1923 tilde expansion, parameter and variable expansion, arithmetic expansion,
1924 and command substitution (done in a left-to-right fashion);
1926 and filename expansion.
1928 On systems that can support it, there is an additional expansion
1929 available: @dfn{process substitution}.
1930 This is performed at the
1931 same time as tilde, parameter, variable, and arithmetic expansion and
1932 command substitution.
1934 After these expansions are performed, quote characters present in the
1935 original word are removed unless they have been quoted themselves
1936 (@dfn{quote removal}).
1938 Only brace expansion, word splitting, and filename expansion
1939 can increase the number of words of the expansion; other expansions
1940 expand a single word to a single word.
1941 The only exceptions to this are the expansions of
1942 @code{"$@@"} and @code{$*} (@pxref{Special Parameters}), and
1943 @code{"$@{@var{name}[@@]@}"} and @code{$@{@var{name}[*]@}}
1946 After all expansions, @code{quote removal} (@pxref{Quote Removal})
1949 @node Brace Expansion
1950 @subsection Brace Expansion
1951 @cindex brace expansion
1952 @cindex expansion, brace
1954 Brace expansion is a mechanism by which arbitrary strings may be generated.
1955 This mechanism is similar to
1956 @dfn{filename expansion} (@pxref{Filename Expansion}),
1957 but the filenames generated need not exist.
1958 Patterns to be brace expanded take the form of an optional @var{preamble},
1959 followed by either a series of comma-separated strings or a sequence expression
1960 between a pair of braces,
1961 followed by an optional @var{postscript}.
1962 The preamble is prefixed to each string contained within the braces, and
1963 the postscript is then appended to each resulting string, expanding left
1966 Brace expansions may be nested.
1967 The results of each expanded string are not sorted; left to right order
1971 bash$ echo a@{d,c,b@}e
1975 A sequence expression takes the form @code{@{@var{x}..@var{y}[..@var{incr}]@}},
1976 where @var{x} and @var{y} are either integers or letters,
1977 and @var{incr}, an optional increment, is an integer.
1978 When integers are supplied, the expression expands to each number between
1979 @var{x} and @var{y}, inclusive.
1980 Supplied integers may be prefixed with @samp{0} to force each term to have the
1982 When either @var{x} or @var{y} begins with a zero, the shell
1983 attempts to force all generated terms to contain the same number of digits,
1984 zero-padding where necessary.
1985 When letters are supplied, the expression expands to each character
1986 lexicographically between @var{x} and @var{y}, inclusive,
1987 using the default C locale.
1988 Note that both @var{x} and @var{y} must be of the same type
1989 (integer or letter).
1990 When the increment is supplied, it is used as the difference between
1991 each term. The default increment is 1 or -1 as appropriate.
1993 Brace expansion is performed before any other expansions,
1994 and any characters special to other expansions are preserved
1995 in the result. It is strictly textual. Bash
1996 does not apply any syntactic interpretation to the context of the
1997 expansion or the text between the braces.
1999 A correctly-formed brace expansion must contain unquoted opening
2000 and closing braces, and at least one unquoted comma or a valid
2001 sequence expression.
2002 Any incorrectly formed brace expansion is left unchanged.
2004 A @{ or @samp{,} may be quoted with a backslash to prevent its
2005 being considered part of a brace expression.
2006 To avoid conflicts with parameter expansion, the string @samp{$@{}
2007 is not considered eligible for brace expansion,
2008 and inhibits brace expansion until the closing @samp{@}}.
2010 This construct is typically used as shorthand when the common
2011 prefix of the strings to be generated is longer than in the
2014 mkdir /usr/local/src/bash/@{old,new,dist,bugs@}
2018 chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@}
2021 @node Tilde Expansion
2022 @subsection Tilde Expansion
2023 @cindex tilde expansion
2024 @cindex expansion, tilde
2026 If a word begins with an unquoted tilde character (@samp{~}), all of the
2027 characters up to the first unquoted slash (or all characters,
2028 if there is no unquoted slash) are considered a @dfn{tilde-prefix}.
2029 If none of the characters in the tilde-prefix are quoted, the
2030 characters in the tilde-prefix following the tilde are treated as a
2031 possible @dfn{login name}.
2032 If this login name is the null string, the tilde is replaced with the
2033 value of the @env{HOME} shell variable.
2034 If @env{HOME} is unset, the home directory of the user executing the
2035 shell is substituted instead.
2036 Otherwise, the tilde-prefix is replaced with the home directory
2037 associated with the specified login name.
2039 If the tilde-prefix is @samp{~+}, the value of
2040 the shell variable @env{PWD} replaces the tilde-prefix.
2041 If the tilde-prefix is @samp{~-}, the value of the shell variable
2042 @env{OLDPWD}, if it is set, is substituted.
2044 If the characters following the tilde in the tilde-prefix consist of a
2045 number @var{N}, optionally prefixed by a @samp{+} or a @samp{-},
2046 the tilde-prefix is replaced with the
2047 corresponding element from the directory stack, as it would be displayed
2048 by the @code{dirs} builtin invoked with the characters following tilde
2049 in the tilde-prefix as an argument (@pxref{The Directory Stack}).
2050 If the tilde-prefix, sans the tilde, consists of a number without a
2051 leading @samp{+} or @samp{-}, @samp{+} is assumed.
2053 If the login name is invalid, or the tilde expansion fails, the word is
2056 Each variable assignment is checked for unquoted tilde-prefixes immediately
2057 following a @samp{:} or the first @samp{=}.
2058 In these cases, tilde expansion is also performed.
2059 Consequently, one may use filenames with tildes in assignments to
2060 @env{PATH}, @env{MAILPATH}, and @env{CDPATH},
2061 and the shell assigns the expanded value.
2063 The following table shows how Bash treats unquoted tilde-prefixes:
2067 The value of @code{$HOME}
2072 The subdirectory @code{foo} of the home directory of the user
2079 @file{$@{OLDPWD-'~-'@}/foo}
2082 The string that would be displayed by @samp{dirs +@var{N}}
2085 The string that would be displayed by @samp{dirs +@var{N}}
2088 The string that would be displayed by @samp{dirs -@var{N}}
2091 Bash also performs tilde expansion on words satisfying the conditions of
2092 variable assignments (@pxref{Shell Parameters})
2093 when they appear as arguments to simple commands.
2094 Bash does not do this, except for the declaration commands listed
2095 above, when in @sc{posix} mode.
2097 @node Shell Parameter Expansion
2098 @subsection Shell Parameter Expansion
2099 @cindex parameter expansion
2100 @cindex expansion, parameter
2102 The @samp{$} character introduces parameter expansion,
2103 command substitution, or arithmetic expansion. The parameter name
2104 or symbol to be expanded may be enclosed in braces, which
2105 are optional but serve to protect the variable to be expanded from
2106 characters immediately following it which could be
2107 interpreted as part of the name.
2109 When braces are used, the matching ending brace is the first @samp{@}}
2110 not escaped by a backslash or within a quoted string, and not within an
2111 embedded arithmetic expansion, command substitution, or parameter
2114 The basic form of parameter expansion is $@{@var{parameter}@}.
2115 The value of @var{parameter} is substituted.
2116 The @var{parameter} is a shell parameter as described above
2117 (@pxref{Shell Parameters}) or an array reference (@pxref{Arrays}).
2118 The braces are required when @var{parameter}
2119 is a positional parameter with more than one digit,
2120 or when @var{parameter} is followed by a character that is not to be
2121 interpreted as part of its name.
2123 If the first character of @var{parameter} is an exclamation point (!),
2124 and @var{parameter} is not a nameref,
2125 it introduces a level of indirection.
2126 Bash uses the value formed by expanding the rest of
2127 @var{parameter} as the new @var{parameter}; this is then
2128 expanded and that value is used in the rest of the expansion, rather
2129 than the expansion of the original @var{parameter}.
2130 This is known as @code{indirect expansion}.
2131 The value is subject to tilde expansion,
2132 parameter expansion, command substitution, and arithmetic expansion.
2133 If @var{parameter} is a nameref, this expands to the name of the
2134 variable referenced by @var{parameter} instead of performing the
2135 complete indirect expansion.
2136 The exceptions to this are the expansions of $@{!@var{prefix}*@}
2137 and $@{!@var{name}[@@]@}
2139 The exclamation point must immediately follow the left brace in order to
2140 introduce indirection.
2142 In each of the cases below, @var{word} is subject to tilde expansion,
2143 parameter expansion, command substitution, and arithmetic expansion.
2145 When not performing substring expansion, using the form described
2146 below (e.g., @samp{:-}), Bash tests for a parameter that is unset or null.
2147 Omitting the colon results in a test only for a parameter that is unset.
2148 Put another way, if the colon is included,
2149 the operator tests for both @var{parameter}'s existence and that its value
2150 is not null; if the colon is omitted, the operator tests only for existence.
2154 @item $@{@var{parameter}:@minus{}@var{word}@}
2155 If @var{parameter} is unset or null, the expansion of
2156 @var{word} is substituted. Otherwise, the value of
2157 @var{parameter} is substituted.
2165 @item $@{@var{parameter}:=@var{word}@}
2167 is unset or null, the expansion of @var{word}
2168 is assigned to @var{parameter}.
2169 The value of @var{parameter} is then substituted.
2170 Positional parameters and special parameters may not be assigned to
2175 $ : $@{var:=DEFAULT@}
2180 @item $@{@var{parameter}:?@var{word}@}
2182 is null or unset, the expansion of @var{word} (or a message
2183 to that effect if @var{word}
2184 is not present) is written to the standard error and the shell, if it
2185 is not interactive, exits. Otherwise, the value of @var{parameter} is
2190 $ : $@{var:?var is unset or null@}
2191 bash: var: var is unset or null
2194 @item $@{@var{parameter}:+@var{word}@}
2196 is null or unset, nothing is substituted, otherwise the expansion of
2197 @var{word} is substituted.
2201 $ echo $@{var:+var is set and not null@}
2202 var is set and not null
2205 @item $@{@var{parameter}:@var{offset}@}
2206 @itemx $@{@var{parameter}:@var{offset}:@var{length}@}
2207 This is referred to as Substring Expansion.
2208 It expands to up to @var{length} characters of the value of @var{parameter}
2209 starting at the character specified by @var{offset}.
2210 If @var{parameter} is @samp{@@}, an indexed array subscripted by
2211 @samp{@@} or @samp{*}, or an associative array name, the results differ as
2213 If @var{length} is omitted, it expands to the substring of the value of
2214 @var{parameter} starting at the character specified by @var{offset}
2215 and extending to the end of the value.
2216 @var{length} and @var{offset} are arithmetic expressions
2217 (@pxref{Shell Arithmetic}).
2219 If @var{offset} evaluates to a number less than zero, the value
2220 is used as an offset in characters
2221 from the end of the value of @var{parameter}.
2222 If @var{length} evaluates to a number less than zero,
2223 it is interpreted as an offset in characters
2224 from the end of the value of @var{parameter} rather than
2225 a number of characters, and the expansion is the characters between
2226 @var{offset} and that result.
2227 Note that a negative offset must be separated from the colon by at least
2228 one space to avoid being confused with the @samp{:-} expansion.
2230 Here are some examples illustrating substring expansion on parameters and
2234 $ string=01234567890abcdefgh
2237 $ echo ${string:7:0}
2239 $ echo ${string:7:2}
2241 $ echo ${string:7:-2}
2243 $ echo ${string: -7}
2245 $ echo ${string: -7:0}
2247 $ echo ${string: -7:2}
2249 $ echo ${string: -7:-2}
2251 $ set -- 01234567890abcdefgh
2268 $ array[0]=01234567890abcdefgh
2269 $ echo ${array[0]:7}
2271 $ echo ${array[0]:7:0}
2273 $ echo ${array[0]:7:2}
2275 $ echo ${array[0]:7:-2}
2277 $ echo ${array[0]: -7}
2279 $ echo ${array[0]: -7:0}
2281 $ echo ${array[0]: -7:2}
2283 $ echo ${array[0]: -7:-2}
2287 If @var{parameter} is @samp{@@}, the result is @var{length} positional
2288 parameters beginning at @var{offset}.
2289 A negative @var{offset} is taken relative to one greater than the greatest
2290 positional parameter, so an offset of -1 evaluates to the last positional
2292 It is an expansion error if @var{length} evaluates to a number less than zero.
2294 The following examples illustrate substring expansion using positional
2298 $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2300 7 8 9 0 a b c d e f g h
2306 bash: -2: substring expression < 0
2310 ./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2317 If @var{parameter} is an indexed array name subscripted
2318 by @samp{@@} or @samp{*}, the result is the @var{length}
2319 members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}.
2320 A negative @var{offset} is taken relative to one greater than the maximum
2321 index of the specified array.
2322 It is an expansion error if @var{length} evaluates to a number less than zero.
2324 These examples show how you can use substring expansion with indexed
2328 $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)
2329 $ echo ${array[@]:7}
2330 7 8 9 0 a b c d e f g h
2331 $ echo ${array[@]:7:2}
2333 $ echo ${array[@]: -7:2}
2335 $ echo ${array[@]: -7:-2}
2336 bash: -2: substring expression < 0
2337 $ echo ${array[@]:0}
2338 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2339 $ echo ${array[@]:0:2}
2341 $ echo ${array[@]: -7:0}
2345 Substring expansion applied to an associative array produces undefined
2348 Substring indexing is zero-based unless the positional parameters
2349 are used, in which case the indexing starts at 1 by default.
2350 If @var{offset} is 0, and the positional parameters are used, @code{$0} is
2351 prefixed to the list.
2353 @item $@{!@var{prefix}*@}
2354 @itemx $@{!@var{prefix}@@@}
2355 Expands to the names of variables whose names begin with @var{prefix},
2356 separated by the first character of the @env{IFS} special variable.
2357 When @samp{@@} is used and the expansion appears within double quotes, each
2358 variable name expands to a separate word.
2360 @item $@{!@var{name}[@@]@}
2361 @itemx $@{!@var{name}[*]@}
2362 If @var{name} is an array variable, expands to the list of array indices
2363 (keys) assigned in @var{name}.
2364 If @var{name} is not an array, expands to 0 if @var{name} is set and null
2366 When @samp{@@} is used and the expansion appears within double quotes, each
2367 key expands to a separate word.
2369 @item $@{#@var{parameter}@}
2370 The length in characters of the expanded value of @var{parameter} is
2372 If @var{parameter} is @samp{*} or @samp{@@}, the value substituted
2373 is the number of positional parameters.
2374 If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@},
2375 the value substituted is the number of elements in the array.
2377 is an indexed array name subscripted by a negative number, that number is
2378 interpreted as relative to one greater than the maximum index of
2379 @var{parameter}, so negative indices count back from the end of the
2380 array, and an index of -1 references the last element.
2382 @item $@{@var{parameter}#@var{word}@}
2383 @itemx $@{@var{parameter}##@var{word}@}
2385 is expanded to produce a pattern and matched according to the rules
2386 described below (@pxref{Pattern Matching}). If the pattern matches
2387 the beginning of the expanded value of @var{parameter},
2388 then the result of the expansion is the expanded value of @var{parameter}
2389 with the shortest matching pattern (the @samp{#} case) or the
2390 longest matching pattern (the @samp{##} case) deleted.
2391 If @var{parameter} is @samp{@@} or @samp{*},
2392 the pattern removal operation is applied to each positional
2393 parameter in turn, and the expansion is the resultant list.
2394 If @var{parameter} is an array variable subscripted with
2395 @samp{@@} or @samp{*},
2396 the pattern removal operation is applied to each member of the
2397 array in turn, and the expansion is the resultant list.
2399 @item $@{@var{parameter}%@var{word}@}
2400 @itemx $@{@var{parameter}%%@var{word}@}
2402 is expanded to produce a pattern and matched according to the rules
2403 described below (@pxref{Pattern Matching}).
2404 If the pattern matches a trailing portion of the expanded value of
2405 @var{parameter}, then the result of the expansion is the value of
2406 @var{parameter} with the shortest matching pattern (the @samp{%} case)
2407 or the longest matching pattern (the @samp{%%} case) deleted.
2408 If @var{parameter} is @samp{@@} or @samp{*},
2409 the pattern removal operation is applied to each positional
2410 parameter in turn, and the expansion is the resultant list.
2412 is an array variable subscripted with @samp{@@} or @samp{*},
2413 the pattern removal operation is applied to each member of the
2414 array in turn, and the expansion is the resultant list.
2416 @item $@{@var{parameter}/@var{pattern}/@var{string}@}
2417 @itemx $@{@var{parameter}//@var{pattern}/@var{string}@}
2418 @itemx $@{@var{parameter}/#@var{pattern}/@var{string}@}
2419 @itemx $@{@var{parameter}/%@var{pattern}/@var{string}@}
2420 The @var{pattern} is expanded to produce a pattern just as in
2422 @var{Parameter} is expanded and the longest match of @var{pattern}
2423 against its value is replaced with @var{string}.
2424 @var{string} undergoes tilde expansion, parameter and variable expansion,
2425 arithmetic expansion, command and process substitution, and quote removal.
2426 The match is performed according to the rules described below
2427 (@pxref{Pattern Matching}).
2429 In the first form above, only the first match is replaced.
2430 If there are two slashes separating @var{parameter} and @var{pattern}
2431 (the second form above), all matches of @var{pattern} are
2432 replaced with @var{string}.
2433 If @var{pattern} is preceded by @samp{#} (the third form above),
2434 it must match at the beginning of the expanded value of @var{parameter}.
2435 If @var{pattern} is preceded by @samp{%} (the fourth form above),
2436 it must match at the end of the expanded value of @var{parameter}.
2437 If the expansion of @var{string} is null,
2438 matches of @var{pattern} are deleted.
2439 If @var{string} is null,
2440 matches of @var{pattern} are deleted
2441 and the @samp{/} following @var{pattern} may be omitted.
2443 If the @code{patsub_replacement} shell option is enabled using @code{shopt},
2444 any unquoted instances of @samp{&} in @var{string} are replaced with the
2445 matching portion of @var{pattern}.
2446 This is intended to duplicate a common @code{sed} idiom.
2447 Backslash is used to quote @samp{&} in @var{string}; the backslash is removed
2448 in order to permit a literal @samp{&} in the replacement string.
2449 Pattern substitution performs the check for @samp{&} after expanding
2451 so users should take care to quote backslashes intended to escape
2452 the @samp{&} and inhibit replacement so they survive any quote removal
2453 performed by the expansion of @var{string}.
2458 echo $@{var/abc/& @}
2459 echo "$@{var/abc/& @}"
2460 echo $@{var/abc/"& "@}
2464 will display three lines of "abc def", while
2468 echo $@{var/abc/\& @}
2469 echo "$@{var/abc/\& @}"
2470 echo $@{var/abc/"\& "@}
2474 will display two lines of "abc def" and a third line of "& def".
2475 The first two are replaced because the backslash is removed by quote
2476 removal performed during the expansion of @var{string}
2477 (the expansion is performed in a
2478 context that doesn't take any enclosing double quotes into account, as
2479 with other word expansions).
2480 In the third case, the double quotes affect the expansion
2481 of @samp{\&}, and, because @samp{&} is not one of the characters for
2482 which backslash is special in double quotes,
2483 the backslash survives the expansion, inhibits the replacement,
2484 but is removed because it is treated specially.
2485 One could use @samp{\\&}, unquoted, as the replacement string to achive
2487 It should rarely be necessary to enclose only @var{string} in double
2490 If the @code{nocasematch} shell option
2491 (see the description of @code{shopt} in @ref{The Shopt Builtin})
2492 is enabled, the match is performed without regard to the case
2493 of alphabetic characters.
2494 If @var{parameter} is @samp{@@} or @samp{*},
2495 the substitution operation is applied to each positional
2496 parameter in turn, and the expansion is the resultant list.
2498 is an array variable subscripted with @samp{@@} or @samp{*},
2499 the substitution operation is applied to each member of the
2500 array in turn, and the expansion is the resultant list.
2502 @item $@{@var{parameter}^@var{pattern}@}
2503 @itemx $@{@var{parameter}^^@var{pattern}@}
2504 @itemx $@{@var{parameter},@var{pattern}@}
2505 @itemx $@{@var{parameter},,@var{pattern}@}
2506 This expansion modifies the case of alphabetic characters in @var{parameter}.
2507 The @var{pattern} is expanded to produce a pattern just as in
2509 Each character in the expanded value of @var{parameter} is tested against
2510 @var{pattern}, and, if it matches the pattern, its case is converted.
2511 The pattern should not attempt to match more than one character.
2513 The @samp{^} operator converts lowercase letters matching @var{pattern}
2514 to uppercase; the @samp{,} operator converts matching uppercase letters
2516 The @samp{^^} and @samp{,,} expansions convert each matched character in the
2517 expanded value; the @samp{^} and @samp{,} expansions match and convert only
2518 the first character in the expanded value.
2519 If @var{pattern} is omitted, it is treated like a @samp{?}, which matches
2522 If @var{parameter} is @samp{@@} or @samp{*},
2523 the case modification operation is applied to each positional
2524 parameter in turn, and the expansion is the resultant list.
2526 is an array variable subscripted with @samp{@@} or @samp{*},
2527 the case modification operation is applied to each member of the
2528 array in turn, and the expansion is the resultant list.
2530 @item $@{@var{parameter}@@@var{operator}@}
2531 The expansion is either a transformation of the value of @var{parameter}
2532 or information about @var{parameter} itself, depending on the value of
2533 @var{operator}. Each @var{operator} is a single letter:
2537 The expansion is a string that is the value of @var{parameter} with lowercase
2538 alphabetic characters converted to uppercase.
2540 The expansion is a string that is the value of @var{parameter} with the first
2541 character converted to uppercase, if it is alphabetic.
2543 The expansion is a string that is the value of @var{parameter} with uppercase
2544 alphabetic characters converted to lowercase.
2546 The expansion is a string that is the value of @var{parameter} quoted in a
2547 format that can be reused as input.
2549 The expansion is a string that is the value of @var{parameter} with backslash
2550 escape sequences expanded as with the @code{$'@dots{}'} quoting mechanism.
2552 The expansion is a string that is the result of expanding the value of
2553 @var{parameter} as if it were a prompt string (@pxref{Controlling the Prompt}).
2555 The expansion is a string in the form of
2556 an assignment statement or @code{declare} command that, if
2557 evaluated, will recreate @var{parameter} with its attributes and value.
2559 Produces a possibly-quoted version of the value of @var{parameter},
2560 except that it prints the values of
2561 indexed and associative arrays as a sequence of quoted key-value pairs
2564 The expansion is a string consisting of flag values representing
2565 @var{parameter}'s attributes.
2567 Like the @samp{K} transformation, but expands the keys and values of
2568 indexed and associative arrays to separate words after word splitting.
2571 If @var{parameter} is @samp{@@} or @samp{*},
2572 the operation is applied to each positional
2573 parameter in turn, and the expansion is the resultant list.
2575 is an array variable subscripted with @samp{@@} or @samp{*},
2576 the operation is applied to each member of the
2577 array in turn, and the expansion is the resultant list.
2579 The result of the expansion is subject to word splitting and filename
2580 expansion as described below.
2583 @node Command Substitution
2584 @subsection Command Substitution
2585 @cindex command substitution
2587 Command substitution allows the output of a command to replace
2589 Command substitution occurs when a command is enclosed as follows:
2600 Bash performs the expansion by executing @var{command} in a subshell environment
2601 and replacing the command substitution with the standard output of the
2602 command, with any trailing newlines deleted.
2603 Embedded newlines are not deleted, but they may be removed during
2605 The command substitution @code{$(cat @var{file})} can be
2606 replaced by the equivalent but faster @code{$(< @var{file})}.
2608 When the old-style backquote form of substitution is used,
2609 backslash retains its literal meaning except when followed by
2610 @samp{$}, @samp{`}, or @samp{\}.
2611 The first backquote not preceded by a backslash terminates the
2612 command substitution.
2613 When using the @code{$(@var{command})} form, all characters between
2614 the parentheses make up the command; none are treated specially.
2616 Command substitutions may be nested. To nest when using the backquoted
2617 form, escape the inner backquotes with backslashes.
2619 If the substitution appears within double quotes, word splitting and
2620 filename expansion are not performed on the results.
2622 @node Arithmetic Expansion
2623 @subsection Arithmetic Expansion
2624 @cindex expansion, arithmetic
2625 @cindex arithmetic expansion
2627 Arithmetic expansion allows the evaluation of an arithmetic expression
2628 and the substitution of the result. The format for arithmetic expansion is:
2631 $(( @var{expression} ))
2634 The @var{expression} undergoes the same expansions
2635 as if it were within double quotes,
2636 but double quote characters in @var{expression} are not treated specially
2638 All tokens in the expression undergo parameter and variable expansion,
2639 command substitution, and quote removal.
2640 The result is treated as the arithmetic expression to be evaluated.
2641 Arithmetic expansions may be nested.
2643 The evaluation is performed according to the rules listed below
2644 (@pxref{Shell Arithmetic}).
2645 If the expression is invalid, Bash prints a message indicating
2646 failure to the standard error and no substitution occurs.
2648 @node Process Substitution
2649 @subsection Process Substitution
2650 @cindex process substitution
2652 Process substitution allows a process's input or output to be
2653 referred to using a filename.
2654 It takes the form of
2664 The process @var{list} is run asynchronously, and its input or output
2665 appears as a filename.
2667 passed as an argument to the current command as the result of the
2669 If the @code{>(@var{list})} form is used, writing to
2670 the file will provide input for @var{list}. If the
2671 @code{<(@var{list})} form is used, the file passed as an
2672 argument should be read to obtain the output of @var{list}.
2673 Note that no space may appear between the @code{<} or @code{>}
2674 and the left parenthesis, otherwise the construct would be interpreted
2676 Process substitution is supported on systems that support named
2677 pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files.
2679 When available, process substitution is performed simultaneously with
2680 parameter and variable expansion, command substitution, and arithmetic
2683 @node Word Splitting
2684 @subsection Word Splitting
2685 @cindex word splitting
2687 The shell scans the results of parameter expansion, command substitution,
2688 and arithmetic expansion that did not occur within double quotes for
2691 The shell treats each character of @env{$IFS} as a delimiter, and splits
2692 the results of the other expansions into words using these characters
2693 as field terminators.
2694 If @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>},
2695 the default, then sequences of
2696 @code{ <space>}, @code{<tab>}, and @code{<newline>}
2697 at the beginning and end of the results of the previous
2698 expansions are ignored, and any sequence of @env{IFS}
2699 characters not at the beginning or end serves to delimit words.
2700 If @env{IFS} has a value other than the default, then sequences of
2701 the whitespace characters @code{space}, @code{tab}, and @code{newline}
2702 are ignored at the beginning and end of the
2703 word, as long as the whitespace character is in the
2704 value of @env{IFS} (an @env{IFS} whitespace character).
2705 Any character in @env{IFS} that is not @env{IFS}
2706 whitespace, along with any adjacent @env{IFS}
2707 whitespace characters, delimits a field. A sequence of @env{IFS}
2708 whitespace characters is also treated as a delimiter.
2709 If the value of @env{IFS} is null, no word splitting occurs.
2711 Explicit null arguments (@code{""} or @code{''}) are retained
2712 and passed to commands as empty strings.
2713 Unquoted implicit null arguments, resulting from the expansion of
2714 parameters that have no values, are removed.
2715 If a parameter with no value is expanded within double quotes, a
2716 null argument results and is retained
2717 and passed to a command as an empty string.
2718 When a quoted null argument appears as part of a word whose expansion is
2719 non-null, the null argument is removed.
2721 @code{-d''} becomes @code{-d} after word splitting and
2722 null argument removal.
2724 Note that if no expansion occurs, no splitting
2727 @node Filename Expansion
2728 @subsection Filename Expansion
2730 * Pattern Matching:: How the shell matches patterns.
2732 @cindex expansion, filename
2733 @cindex expansion, pathname
2734 @cindex filename expansion
2735 @cindex pathname expansion
2737 After word splitting, unless the @option{-f} option has been set
2738 (@pxref{The Set Builtin}), Bash scans each word for the characters
2739 @samp{*}, @samp{?}, and @samp{[}.
2740 If one of these characters appears, and is not quoted, then the word is
2741 regarded as a @var{pattern},
2742 and replaced with an alphabetically sorted list of
2743 filenames matching the pattern (@pxref{Pattern Matching}).
2744 If no matching filenames are found,
2745 and the shell option @code{nullglob} is disabled, the word is left
2747 If the @code{nullglob} option is set, and no matches are found, the word
2749 If the @code{failglob} shell option is set, and no matches are found,
2750 an error message is printed and the command is not executed.
2751 If the shell option @code{nocaseglob} is enabled, the match is performed
2752 without regard to the case of alphabetic characters.
2754 When a pattern is used for filename expansion, the character @samp{.}
2755 at the start of a filename or immediately following a slash
2756 must be matched explicitly, unless the shell option @code{dotglob} is set.
2757 In order to match the filenames @samp{.} and @samp{..},
2758 the pattern must begin with @samp{.} (for example, @samp{.?}),
2759 even if @code{dotglob} is set.
2760 When not matching filenames, the @samp{.} character is not treated specially.
2762 When matching a filename, the slash character must always be
2763 matched explicitly by a slash in the pattern, but in other matching
2764 contexts it can be matched by a special pattern character as described
2765 below (@pxref{Pattern Matching}).
2767 See the description of @code{shopt} in @ref{The Shopt Builtin},
2768 for a description of the @code{nocaseglob}, @code{nullglob},
2769 @code{failglob}, and @code{dotglob} options.
2771 The @env{GLOBIGNORE}
2772 shell variable may be used to restrict the set of file names matching a
2773 pattern. If @env{GLOBIGNORE}
2774 is set, each matching file name that also matches one of the patterns in
2775 @env{GLOBIGNORE} is removed from the list of matches.
2776 If the @code{nocaseglob} option is set, the matching against the patterns in
2777 @env{GLOBIGNORE} is performed without regard to case.
2779 @file{.} and @file{..}
2780 are always ignored when @env{GLOBIGNORE}
2781 is set and not null.
2782 However, setting @env{GLOBIGNORE} to a non-null value has the effect of
2783 enabling the @code{dotglob}
2784 shell option, so all other filenames beginning with a
2785 @samp{.} will match.
2786 To get the old behavior of ignoring filenames beginning with a
2787 @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}.
2788 The @code{dotglob} option is disabled when @env{GLOBIGNORE}
2791 @node Pattern Matching
2792 @subsubsection Pattern Matching
2793 @cindex pattern matching
2794 @cindex matching, pattern
2796 Any character that appears in a pattern, other than the special pattern
2797 characters described below, matches itself.
2798 The @sc{nul} character may not occur in a pattern.
2799 A backslash escapes the following character; the
2800 escaping backslash is discarded when matching.
2801 The special pattern characters must be quoted if they are to be matched
2804 The special pattern characters have the following meanings:
2807 Matches any string, including the null string.
2808 When the @code{globstar} shell option is enabled, and @samp{*} is used in
2809 a filename expansion context, two adjacent @samp{*}s used as a single
2810 pattern will match all files and zero or more directories and
2812 If followed by a @samp{/}, two adjacent @samp{*}s will match only
2813 directories and subdirectories.
2815 Matches any single character.
2817 Matches any one of the enclosed characters. A pair of characters
2818 separated by a hyphen denotes a @var{range expression};
2819 any character that falls between those two characters, inclusive,
2820 using the current locale's collating sequence and character set,
2821 is matched. If the first character following the
2822 @samp{[} is a @samp{!} or a @samp{^}
2823 then any character not enclosed is matched. A @samp{@minus{}}
2824 may be matched by including it as the first or last character
2825 in the set. A @samp{]} may be matched by including it as the first
2826 character in the set.
2827 The sorting order of characters in range expressions is determined by
2828 the current locale and the values of the
2829 @env{LC_COLLATE} and @env{LC_ALL} shell variables, if set.
2831 For example, in the default C locale, @samp{[a-dx-z]} is equivalent to
2832 @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in
2833 these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]};
2834 it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain
2835 the traditional interpretation of ranges in bracket expressions, you can
2836 force the use of the C locale by setting the @env{LC_COLLATE} or
2837 @env{LC_ALL} environment variable to the value @samp{C}, or enable the
2838 @code{globasciiranges} shell option.
2840 Within @samp{[} and @samp{]}, @dfn{character classes} can be specified
2842 @code{[:}@var{class}@code{:]}, where @var{class} is one of the
2843 following classes defined in the @sc{posix} standard:
2845 alnum alpha ascii blank cntrl digit graph lower
2846 print punct space upper word xdigit
2849 A character class matches any character belonging to that class.
2850 The @code{word} character class matches letters, digits, and the character
2853 Within @samp{[} and @samp{]}, an @dfn{equivalence class} can be
2854 specified using the syntax @code{[=}@var{c}@code{=]}, which
2855 matches all characters with the same collation weight (as defined
2856 by the current locale) as the character @var{c}.
2858 Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]}
2859 matches the collating symbol @var{symbol}.
2862 If the @code{extglob} shell option is enabled using the @code{shopt}
2863 builtin, the shell recognizes several extended pattern matching operators.
2864 In the following description, a @var{pattern-list} is a list of one
2865 or more patterns separated by a @samp{|}.
2866 When matching filenames, the @code{dotglob} shell option determines
2867 the set of filenames that are tested, as described above.
2868 Composite patterns may be formed using one or more of the following
2872 @item ?(@var{pattern-list})
2873 Matches zero or one occurrence of the given patterns.
2875 @item *(@var{pattern-list})
2876 Matches zero or more occurrences of the given patterns.
2878 @item +(@var{pattern-list})
2879 Matches one or more occurrences of the given patterns.
2881 @item @@(@var{pattern-list})
2882 Matches one of the given patterns.
2884 @item !(@var{pattern-list})
2885 Matches anything except one of the given patterns.
2888 When matching filenames, the @code{dotglob} shell option determines
2889 the set of filenames that are tested:
2890 when @code{dotglob} is enabled, the set of filenames includes all files
2891 beginning with @samp{.}, but the filenames
2892 @samp{.} and @samp{..} must be matched by a
2893 pattern or sub-pattern that begins with a dot;
2894 when it is disabled, the set does not
2895 include any filenames beginning with ``.'' unless the pattern
2896 or sub-pattern begins with a @samp{.}.
2897 As above, @samp{.} only has a special meaning when matching filenames.
2899 Complicated extended pattern matching against long strings is slow,
2900 especially when the patterns contain alternations and the strings
2901 contain multiple matches.
2902 Using separate matches against shorter strings, or using arrays of
2903 strings instead of a single long string, may be faster.
2906 @subsection Quote Removal
2908 After the preceding expansions, all unquoted occurrences of the
2909 characters @samp{\}, @samp{'}, and @samp{"} that did not
2910 result from one of the above expansions are removed.
2913 @section Redirections
2916 Before a command is executed, its input and output
2917 may be @dfn{redirected}
2918 using a special notation interpreted by the shell.
2919 @dfn{Redirection} allows commands' file handles to be
2920 duplicated, opened, closed,
2921 made to refer to different files,
2922 and can change the files the command reads from and writes to.
2923 Redirection may also be used to modify file handles in the
2924 current shell execution environment. The following redirection
2925 operators may precede or appear anywhere within a
2926 simple command or may follow a command.
2927 Redirections are processed in the order they appear, from
2930 Each redirection that may be preceded by a file descriptor number
2931 may instead be preceded by a word of the form @{@var{varname}@}.
2932 In this case, for each redirection operator except
2933 >&- and <&-, the shell will allocate a file descriptor greater
2934 than 10 and assign it to @{@var{varname}@}. If >&- or <&- is preceded
2935 by @{@var{varname}@}, the value of @var{varname} defines the file
2936 descriptor to close.
2937 If @{@var{varname}@} is supplied, the redirection persists beyond
2938 the scope of the command, allowing the shell programmer to manage
2939 the file descriptor's lifetime manually.
2940 The @code{varredir_close} shell option manages this behavior
2941 (@pxref{The Shopt Builtin}).
2943 In the following descriptions, if the file descriptor number is
2944 omitted, and the first character of the redirection operator is
2945 @samp{<}, the redirection refers to the standard input (file
2946 descriptor 0). If the first character of the redirection operator
2947 is @samp{>}, the redirection refers to the standard output (file
2950 The word following the redirection operator in the following
2951 descriptions, unless otherwise noted, is subjected to brace expansion,
2952 tilde expansion, parameter expansion, command substitution, arithmetic
2953 expansion, quote removal, filename expansion, and word splitting.
2954 If it expands to more than one word, Bash reports an error.
2956 Note that the order of redirections is significant. For example,
2959 ls > @var{dirlist} 2>&1
2962 directs both standard output (file descriptor 1) and standard error
2963 (file descriptor 2) to the file @var{dirlist}, while the command
2965 ls 2>&1 > @var{dirlist}
2968 directs only the standard output to file @var{dirlist},
2969 because the standard error was made a copy of the standard output
2970 before the standard output was redirected to @var{dirlist}.
2972 Bash handles several filenames specially when they are used in
2973 redirections, as described in the following table.
2974 If the operating system on which Bash is running provides these
2975 special files, bash will use them; otherwise it will emulate them
2976 internally with the behavior described below.
2979 @item /dev/fd/@var{fd}
2980 If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated.
2983 File descriptor 0 is duplicated.
2986 File descriptor 1 is duplicated.
2989 File descriptor 2 is duplicated.
2991 @item /dev/tcp/@var{host}/@var{port}
2992 If @var{host} is a valid hostname or Internet address, and @var{port}
2993 is an integer port number or service name, Bash attempts to open
2994 the corresponding TCP socket.
2996 @item /dev/udp/@var{host}/@var{port}
2997 If @var{host} is a valid hostname or Internet address, and @var{port}
2998 is an integer port number or service name, Bash attempts to open
2999 the corresponding UDP socket.
3002 A failure to open or create a file causes the redirection to fail.
3004 Redirections using file descriptors greater than 9 should be used with
3005 care, as they may conflict with file descriptors the shell uses
3008 @subsection Redirecting Input
3009 Redirection of input causes the file whose name results from
3010 the expansion of @var{word}
3011 to be opened for reading on file descriptor @code{n},
3012 or the standard input (file descriptor 0) if @code{n}
3015 The general format for redirecting input is:
3017 [@var{n}]<@var{word}
3020 @subsection Redirecting Output
3021 Redirection of output causes the file whose name results from
3022 the expansion of @var{word}
3023 to be opened for writing on file descriptor @var{n},
3024 or the standard output (file descriptor 1) if @var{n}
3025 is not specified. If the file does not exist it is created;
3026 if it does exist it is truncated to zero size.
3028 The general format for redirecting output is:
3030 [@var{n}]>[|]@var{word}
3033 If the redirection operator is @samp{>}, and the @code{noclobber}
3034 option to the @code{set} builtin has been enabled, the redirection
3035 will fail if the file whose name results from the expansion of
3036 @var{word} exists and is a regular file.
3037 If the redirection operator is @samp{>|}, or the redirection operator is
3038 @samp{>} and the @code{noclobber} option is not enabled, the redirection
3039 is attempted even if the file named by @var{word} exists.
3041 @subsection Appending Redirected Output
3042 Redirection of output in this fashion
3043 causes the file whose name results from
3044 the expansion of @var{word}
3045 to be opened for appending on file descriptor @var{n},
3046 or the standard output (file descriptor 1) if @var{n}
3047 is not specified. If the file does not exist it is created.
3049 The general format for appending output is:
3051 [@var{n}]>>@var{word}
3054 @subsection Redirecting Standard Output and Standard Error
3055 This construct allows both the
3056 standard output (file descriptor 1) and
3057 the standard error output (file descriptor 2)
3058 to be redirected to the file whose name is the
3059 expansion of @var{word}.
3061 There are two formats for redirecting standard output and
3072 Of the two forms, the first is preferred.
3073 This is semantically equivalent to
3077 When using the second form, @var{word} may not expand to a number or
3078 @samp{-}. If it does, other redirection operators apply
3079 (see Duplicating File Descriptors below) for compatibility reasons.
3081 @subsection Appending Standard Output and Standard Error
3082 This construct allows both the
3083 standard output (file descriptor 1) and
3084 the standard error output (file descriptor 2)
3085 to be appended to the file whose name is the
3086 expansion of @var{word}.
3088 The format for appending standard output and standard error is:
3093 This is semantically equivalent to
3097 (see Duplicating File Descriptors below).
3099 @subsection Here Documents
3100 This type of redirection instructs the shell to read input from the
3101 current source until a line containing only @var{word}
3102 (with no trailing blanks) is seen. All of
3103 the lines read up to that point are then used as the standard
3104 input (or file descriptor @var{n} if @var{n} is specified) for a command.
3106 The format of here-documents is:
3108 [@var{n}]<<[@minus{}]@var{word}
3113 No parameter and variable expansion, command substitution,
3114 arithmetic expansion, or filename expansion is performed on
3115 @var{word}. If any part of @var{word} is quoted, the
3116 @var{delimiter} is the result of quote removal on @var{word},
3117 and the lines in the here-document are not expanded.
3118 If @var{word} is unquoted,
3119 all lines of the here-document are subjected to
3120 parameter expansion, command substitution, and arithmetic expansion,
3121 the character sequence @code{\newline} is ignored, and @samp{\}
3122 must be used to quote the characters
3123 @samp{\}, @samp{$}, and @samp{`}.
3125 If the redirection operator is @samp{<<-},
3126 then all leading tab characters are stripped from input lines and the
3127 line containing @var{delimiter}.
3128 This allows here-documents within shell scripts to be indented in a
3131 @subsection Here Strings
3132 A variant of here documents, the format is:
3134 [@var{n}]<<< @var{word}
3137 The @var{word} undergoes
3138 tilde expansion, parameter and variable expansion,
3139 command substitution, arithmetic expansion, and quote removal.
3140 Filename expansion and word splitting are not performed.
3141 The result is supplied as a single string,
3142 with a newline appended,
3143 to the command on its
3144 standard input (or file descriptor @var{n} if @var{n} is specified).
3146 @subsection Duplicating File Descriptors
3147 The redirection operator
3149 [@var{n}]<&@var{word}
3152 is used to duplicate input file descriptors.
3154 expands to one or more digits, the file descriptor denoted by @var{n}
3155 is made to be a copy of that file descriptor.
3156 If the digits in @var{word} do not specify a file descriptor open for
3157 input, a redirection error occurs.
3159 evaluates to @samp{-}, file descriptor @var{n} is closed.
3160 If @var{n} is not specified, the standard input (file descriptor 0) is used.
3164 [@var{n}]>&@var{word}
3167 is used similarly to duplicate output file descriptors. If
3168 @var{n} is not specified, the standard output (file descriptor 1) is used.
3169 If the digits in @var{word} do not specify a file descriptor open for
3170 output, a redirection error occurs.
3172 evaluates to @samp{-}, file descriptor @var{n} is closed.
3173 As a special case, if @var{n} is omitted, and @var{word} does not
3174 expand to one or more digits or @samp{-}, the standard output and standard
3175 error are redirected as described previously.
3177 @subsection Moving File Descriptors
3178 The redirection operator
3180 [@var{n}]<&@var{digit}-
3183 moves the file descriptor @var{digit} to file descriptor @var{n},
3184 or the standard input (file descriptor 0) if @var{n} is not specified.
3185 @var{digit} is closed after being duplicated to @var{n}.
3187 Similarly, the redirection operator
3189 [@var{n}]>&@var{digit}-
3192 moves the file descriptor @var{digit} to file descriptor @var{n},
3193 or the standard output (file descriptor 1) if @var{n} is not specified.
3195 @subsection Opening File Descriptors for Reading and Writing
3196 The redirection operator
3198 [@var{n}]<>@var{word}
3201 causes the file whose name is the expansion of @var{word}
3202 to be opened for both reading and writing on file descriptor
3203 @var{n}, or on file descriptor 0 if @var{n}
3204 is not specified. If the file does not exist, it is created.
3206 @node Executing Commands
3207 @section Executing Commands
3210 * Simple Command Expansion:: How Bash expands simple commands before
3212 * Command Search and Execution:: How Bash finds commands and runs them.
3213 * Command Execution Environment:: The environment in which Bash
3214 executes commands that are not
3216 * Environment:: The environment given to a command.
3217 * Exit Status:: The status returned by commands and how Bash
3219 * Signals:: What happens when Bash or a command it runs
3223 @node Simple Command Expansion
3224 @subsection Simple Command Expansion
3225 @cindex command expansion
3227 When a simple command is executed, the shell performs the following
3228 expansions, assignments, and redirections, from left to right, in
3229 the following order.
3233 The words that the parser has marked as variable assignments (those
3234 preceding the command name) and redirections are saved for later
3238 The words that are not variable assignments or redirections are
3239 expanded (@pxref{Shell Expansions}).
3240 If any words remain after expansion, the first word
3241 is taken to be the name of the command and the remaining words are
3245 Redirections are performed as described above (@pxref{Redirections}).
3248 The text after the @samp{=} in each variable assignment undergoes tilde
3249 expansion, parameter expansion, command substitution, arithmetic expansion,
3250 and quote removal before being assigned to the variable.
3253 If no command name results, the variable assignments affect the current
3255 In the case of such a command (one that consists only of assignment
3256 statements and redirections), assignment statements are performed before
3258 Otherwise, the variables are added to the environment
3259 of the executed command and do not affect the current shell environment.
3260 If any of the assignments attempts to assign a value to a readonly variable,
3261 an error occurs, and the command exits with a non-zero status.
3263 If no command name results, redirections are performed, but do not
3264 affect the current shell environment. A redirection error causes the
3265 command to exit with a non-zero status.
3267 If there is a command name left after expansion, execution proceeds as
3268 described below. Otherwise, the command exits. If one of the expansions
3269 contained a command substitution, the exit status of the command is
3270 the exit status of the last command substitution performed. If there
3271 were no command substitutions, the command exits with a status of zero.
3273 @node Command Search and Execution
3274 @subsection Command Search and Execution
3275 @cindex command execution
3276 @cindex command search
3278 After a command has been split into words, if it results in a
3279 simple command and an optional list of arguments, the following
3284 If the command name contains no slashes, the shell attempts to
3285 locate it. If there exists a shell function by that name, that
3286 function is invoked as described in @ref{Shell Functions}.
3289 If the name does not match a function, the shell searches for
3290 it in the list of shell builtins. If a match is found, that
3294 If the name is neither a shell function nor a builtin,
3295 and contains no slashes, Bash searches each element of
3296 @env{$PATH} for a directory containing an executable file
3297 by that name. Bash uses a hash table to remember the full
3298 pathnames of executable files to avoid multiple @env{PATH} searches
3299 (see the description of @code{hash} in @ref{Bourne Shell Builtins}).
3300 A full search of the directories in @env{$PATH}
3301 is performed only if the command is not found in the hash table.
3302 If the search is unsuccessful, the shell searches for a defined shell
3303 function named @code{command_not_found_handle}.
3304 If that function exists, it is invoked in a separate execution environment
3305 with the original command and
3306 the original command's arguments as its arguments, and the function's
3307 exit status becomes the exit status of that subshell.
3308 If that function is not defined, the shell prints an error
3309 message and returns an exit status of 127.
3312 If the search is successful, or if the command name contains
3313 one or more slashes, the shell executes the named program in
3314 a separate execution environment.
3315 Argument 0 is set to the name given, and the remaining arguments
3316 to the command are set to the arguments supplied, if any.
3319 If this execution fails because the file is not in executable
3320 format, and the file is not a directory, it is assumed to be a
3321 @dfn{shell script} and the shell executes it as described in
3322 @ref{Shell Scripts}.
3325 If the command was not begun asynchronously, the shell waits for
3326 the command to complete and collects its exit status.
3330 @node Command Execution Environment
3331 @subsection Command Execution Environment
3332 @cindex execution environment
3334 The shell has an @dfn{execution environment}, which consists of the
3339 open files inherited by the shell at invocation, as modified by
3340 redirections supplied to the @code{exec} builtin
3343 the current working directory as set by @code{cd}, @code{pushd}, or
3344 @code{popd}, or inherited by the shell at invocation
3347 the file creation mode mask as set by @code{umask} or inherited from
3351 current traps set by @code{trap}
3354 shell parameters that are set by variable assignment or with @code{set}
3355 or inherited from the shell's parent in the environment
3358 shell functions defined during execution or inherited from the shell's
3359 parent in the environment
3362 options enabled at invocation (either by default or with command-line
3363 arguments) or by @code{set}
3366 options enabled by @code{shopt} (@pxref{The Shopt Builtin})
3369 shell aliases defined with @code{alias} (@pxref{Aliases})
3372 various process @sc{id}s, including those of background jobs
3373 (@pxref{Lists}), the value of @code{$$}, and the value of
3378 When a simple command other than a builtin or shell function
3379 is to be executed, it
3380 is invoked in a separate execution environment that consists of
3381 the following. Unless otherwise noted, the values are inherited
3386 the shell's open files, plus any modifications and additions specified
3387 by redirections to the command
3390 the current working directory
3393 the file creation mode mask
3396 shell variables and functions marked for export, along with variables
3397 exported for the command, passed in the environment (@pxref{Environment})
3400 traps caught by the shell are reset to the values inherited from the
3401 shell's parent, and traps ignored by the shell are ignored
3405 A command invoked in this separate environment cannot affect the
3406 shell's execution environment.
3408 A @dfn{subshell} is a copy of the shell process.
3410 Command substitution, commands grouped with parentheses,
3411 and asynchronous commands are invoked in a
3412 subshell environment that is a duplicate of the shell environment,
3413 except that traps caught by the shell are reset to the values
3414 that the shell inherited from its parent at invocation. Builtin
3415 commands that are invoked as part of a pipeline are also executed
3416 in a subshell environment. Changes made to the subshell environment
3417 cannot affect the shell's execution environment.
3419 Subshells spawned to execute command substitutions inherit the value of
3420 the @option{-e} option from the parent shell. When not in @sc{posix} mode,
3421 Bash clears the @option{-e} option in such subshells.
3423 If a command is followed by a @samp{&} and job control is not active, the
3424 default standard input for the command is the empty file @file{/dev/null}.
3425 Otherwise, the invoked command inherits the file descriptors of the calling
3426 shell as modified by redirections.
3429 @subsection Environment
3432 When a program is invoked it is given an array of strings
3433 called the @dfn{environment}.
3434 This is a list of name-value pairs, of the form @code{name=value}.
3436 Bash provides several ways to manipulate the environment.
3437 On invocation, the shell scans its own environment and
3438 creates a parameter for each name found, automatically marking
3439 it for @code{export}
3440 to child processes. Executed commands inherit the environment.
3441 The @code{export} and @samp{declare -x}
3442 commands allow parameters and functions to be added to and
3443 deleted from the environment. If the value of a parameter
3444 in the environment is modified, the new value becomes part
3445 of the environment, replacing the old. The environment
3446 inherited by any executed command consists of the shell's
3447 initial environment, whose values may be modified in the shell,
3448 less any pairs removed by the @code{unset} and @samp{export -n}
3449 commands, plus any additions via the @code{export} and
3450 @samp{declare -x} commands.
3452 The environment for any simple command
3453 or function may be augmented temporarily by prefixing it with
3454 parameter assignments, as described in @ref{Shell Parameters}.
3455 These assignment statements affect only the environment seen
3458 If the @option{-k} option is set (@pxref{The Set Builtin}), then all
3459 parameter assignments are placed in the environment for a command,
3460 not just those that precede the command name.
3462 When Bash invokes an external command, the variable @samp{$_}
3463 is set to the full pathname of the command and passed to that
3464 command in its environment.
3467 @subsection Exit Status
3470 The exit status of an executed command is the value returned by the
3471 @code{waitpid} system call or equivalent function. Exit statuses
3472 fall between 0 and 255, though, as explained below, the shell may
3473 use values above 125 specially. Exit statuses from shell builtins and
3474 compound commands are also limited to this range. Under certain
3475 circumstances, the shell will use special values to indicate specific
3478 For the shell's purposes, a command which exits with a
3479 zero exit status has succeeded.
3480 A non-zero exit status indicates failure.
3481 This seemingly counter-intuitive scheme is used so there
3482 is one well-defined way to indicate success and a variety of
3483 ways to indicate various failure modes.
3484 When a command terminates on a fatal signal whose number is @var{N},
3485 Bash uses the value 128+@var{N} as the exit status.
3487 If a command is not found, the child process created to
3488 execute it returns a status of 127. If a command is found
3489 but is not executable, the return status is 126.
3491 If a command fails because of an error during expansion or redirection,
3492 the exit status is greater than zero.
3494 The exit status is used by the Bash conditional commands
3495 (@pxref{Conditional Constructs}) and some of the list
3496 constructs (@pxref{Lists}).
3498 All of the Bash builtins return an exit status of zero if they succeed
3499 and a non-zero status on failure, so they may be used by the
3500 conditional and list constructs.
3501 All builtins return an exit status of 2 to indicate incorrect usage,
3502 generally invalid options or missing arguments.
3504 The exit status of the last command is available in the special
3505 parameter $? (@pxref{Special Parameters}).
3509 @cindex signal handling
3511 When Bash is interactive, in the absence of any traps, it ignores
3512 @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell),
3514 is caught and handled (so that the @code{wait} builtin is interruptible).
3515 When Bash receives a @code{SIGINT}, it breaks out of any executing loops.
3516 In all cases, Bash ignores @code{SIGQUIT}.
3517 If job control is in effect (@pxref{Job Control}), Bash
3518 ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3520 Non-builtin commands started by Bash have signal handlers set to the
3521 values inherited by the shell from its parent.
3522 When job control is not in effect, asynchronous commands
3523 ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited
3525 Commands run as a result of
3526 command substitution ignore the keyboard-generated job control signals
3527 @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3529 The shell exits by default upon receipt of a @code{SIGHUP}.
3530 Before exiting, an interactive shell resends the @code{SIGHUP} to
3531 all jobs, running or stopped.
3532 Stopped jobs are sent @code{SIGCONT} to ensure that they receive
3534 To prevent the shell from sending the @code{SIGHUP} signal to a
3535 particular job, it should be removed
3536 from the jobs table with the @code{disown}
3537 builtin (@pxref{Job Control Builtins}) or marked
3538 to not receive @code{SIGHUP} using @code{disown -h}.
3540 If the @code{huponexit} shell option has been set with @code{shopt}
3541 (@pxref{The Shopt Builtin}), Bash sends a @code{SIGHUP} to all jobs when
3542 an interactive login shell exits.
3544 If Bash is waiting for a command to complete and receives a signal
3545 for which a trap has been set, the trap will not be executed until
3546 the command completes.
3547 When Bash is waiting for an asynchronous
3548 command via the @code{wait} builtin, the reception of a signal for
3549 which a trap has been set will cause the @code{wait} builtin to return
3550 immediately with an exit status greater than 128, immediately after
3551 which the trap is executed.
3553 When job control is not enabled, and Bash is waiting for a foreground
3554 command to complete, the shell receives keyboard-generated signals
3555 such as @code{SIGINT} (usually generated by @samp{^C}) that users
3556 commonly intend to send to that command.
3557 This happens because the shell and the command are in the same process
3558 group as the terminal, and @samp{^C} sends @code{SIGINT} to all processes
3559 in that process group.
3560 See @ref{Job Control}, for a more in-depth discussion of process groups.
3562 When Bash is running without job control enabled and receives @code{SIGINT}
3563 while waiting for a foreground command, it waits until that foreground
3564 command terminates and then decides what to do about the @code{SIGINT}:
3568 If the command terminates due to the @code{SIGINT}, Bash concludes
3569 that the user meant to end the entire script, and acts on the
3570 @code{SIGINT} (e.g., by running a @code{SIGINT} trap or exiting itself);
3573 If the pipeline does not terminate due to @code{SIGINT}, the program
3574 handled the @code{SIGINT} itself and did not treat it as a fatal signal.
3575 In that case, Bash does not treat @code{SIGINT} as a fatal signal,
3576 either, instead assuming that the @code{SIGINT} was used as part of the
3577 program's normal operation (e.g., @command{emacs} uses it to abort editing
3578 commands) or deliberately discarded. However, Bash will run any
3579 trap set on @code{SIGINT}, as it does with any other trapped signal it
3580 receives while it is waiting for the foreground command to
3581 complete, for compatibility.
3585 @section Shell Scripts
3586 @cindex shell script
3588 A shell script is a text file containing shell commands. When such
3589 a file is used as the first non-option argument when invoking Bash,
3590 and neither the @option{-c} nor @option{-s} option is supplied
3591 (@pxref{Invoking Bash}),
3592 Bash reads and executes commands from the file, then exits. This
3593 mode of operation creates a non-interactive shell. The shell first
3594 searches for the file in the current directory, and looks in the
3595 directories in @env{$PATH} if not found there.
3598 a shell script, it sets the special parameter @code{0} to the name
3599 of the file, rather than the name of the shell, and the positional
3600 parameters are set to the remaining arguments, if any are given.
3601 If no additional arguments are supplied, the positional parameters
3604 A shell script may be made executable by using the @code{chmod} command
3605 to turn on the execute bit. When Bash finds such a file while
3606 searching the @env{$PATH} for a command, it creates a
3607 new instance of itself
3609 In other words, executing
3611 filename @var{arguments}
3614 is equivalent to executing
3616 bash filename @var{arguments}
3620 if @code{filename} is an executable shell script.
3621 This subshell reinitializes itself, so that the effect is as if a
3622 new shell had been invoked to interpret the script, with the
3623 exception that the locations of commands remembered by the parent
3624 (see the description of @code{hash} in @ref{Bourne Shell Builtins})
3625 are retained by the child.
3627 Most versions of Unix make this a part of the operating system's command
3628 execution mechanism. If the first line of a script begins with
3629 the two characters @samp{#!}, the remainder of the line specifies
3630 an interpreter for the program and, depending on the operating system, one
3631 or more optional arguments for that interpreter.
3632 Thus, you can specify Bash, @code{awk}, Perl, or some other
3633 interpreter and write the rest of the script file in that language.
3635 The arguments to the interpreter
3636 consist of one or more optional arguments following the interpreter
3637 name on the first line of the script file, followed by the name of
3638 the script file, followed by the rest of the arguments supplied to the
3640 The details of how the interpreter line is split into an interpreter name
3641 and a set of arguments vary across systems.
3642 Bash will perform this action on operating systems that do not handle it
3644 Note that some older versions of Unix limit the interpreter
3645 name and a single argument to a maximum of 32 characters, so it's not
3646 portable to assume that using more than one argument will work.
3648 Bash scripts often begin with @code{#! /bin/bash} (assuming that
3649 Bash has been installed in @file{/bin}), since this ensures that
3650 Bash will be used to interpret the script, even if it is executed
3651 under another shell. It's a common idiom to use @code{env} to find
3652 @code{bash} even if it's been installed in another directory:
3653 @code{#!/usr/bin/env bash} will find the first occurrence of @code{bash}
3656 @node Shell Builtin Commands
3657 @chapter Shell Builtin Commands
3660 * Bourne Shell Builtins:: Builtin commands inherited from the Bourne
3662 * Bash Builtins:: Table of builtins specific to Bash.
3663 * Modifying Shell Behavior:: Builtins to modify shell attributes and
3665 * Special Builtins:: Builtin commands classified specially by
3669 Builtin commands are contained within the shell itself.
3670 When the name of a builtin command is used as the first word of
3671 a simple command (@pxref{Simple Commands}), the shell executes
3672 the command directly, without invoking another program.
3673 Builtin commands are necessary to implement functionality impossible
3674 or inconvenient to obtain with separate utilities.
3676 This section briefly describes the builtins which Bash inherits from
3677 the Bourne Shell, as well as the builtin commands which are unique
3678 to or have been extended in Bash.
3680 Several builtin commands are described in other chapters: builtin
3681 commands which provide the Bash interface to the job control
3682 facilities (@pxref{Job Control Builtins}), the directory stack
3683 (@pxref{Directory Stack Builtins}), the command history
3684 (@pxref{Bash History Builtins}), and the programmable completion
3685 facilities (@pxref{Programmable Completion Builtins}).
3687 Many of the builtins have been extended by @sc{posix} or Bash.
3689 Unless otherwise noted, each builtin command documented as accepting
3690 options preceded by @samp{-} accepts @samp{--}
3691 to signify the end of the options.
3692 The @code{:}, @code{true}, @code{false}, and @code{test}/@code{[}
3693 builtins do not accept options and do not treat @samp{--} specially.
3694 The @code{exit}, @code{logout}, @code{return},
3695 @code{break}, @code{continue}, @code{let},
3696 and @code{shift} builtins accept and process arguments beginning
3697 with @samp{-} without requiring @samp{--}.
3698 Other builtins that accept arguments but are not specified as accepting
3699 options interpret arguments beginning with @samp{-} as invalid options and
3700 require @samp{--} to prevent this interpretation.
3702 @node Bourne Shell Builtins
3703 @section Bourne Shell Builtins
3705 The following shell builtin commands are inherited from the Bourne Shell.
3706 These commands are implemented as specified by the @sc{posix} standard.
3709 @item : @r{(a colon)}
3715 Do nothing beyond expanding @var{arguments} and performing redirections.
3716 The return status is zero.
3718 @item . @r{(a period)}
3721 . @var{filename} [@var{arguments}]
3724 Read and execute commands from the @var{filename} argument in the
3725 current shell context. If @var{filename} does not contain a slash,
3726 the @env{PATH} variable is used to find @var{filename},
3727 but @var{filename} does not need to be executable.
3728 When Bash is not in @sc{posix} mode, the current directory is searched
3729 if @var{filename} is not found in @env{$PATH}.
3730 If any @var{arguments} are supplied, they become the positional
3731 parameters when @var{filename} is executed. Otherwise the positional
3732 parameters are unchanged.
3733 If the @option{-T} option is enabled, @code{.} inherits any trap on
3734 @code{DEBUG}; if it is not, any @code{DEBUG} trap string is saved and
3735 restored around the call to @code{.}, and @code{.} unsets the
3736 @code{DEBUG} trap while it executes.
3737 If @option{-T} is not set, and the sourced file changes
3738 the @code{DEBUG} trap, the new value is retained when @code{.} completes.
3739 The return status is the exit status of the last command executed, or
3740 zero if no commands are executed. If @var{filename} is not found, or
3741 cannot be read, the return status is non-zero.
3742 This builtin is equivalent to @code{source}.
3750 Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop.
3751 If @var{n} is supplied, the @var{n}th enclosing loop is exited.
3752 @var{n} must be greater than or equal to 1.
3753 The return status is zero unless @var{n} is not greater than or equal to 1.
3758 cd [-L|[-P [-e]] [-@@] [@var{directory}]
3761 Change the current working directory to @var{directory}.
3762 If @var{directory} is not supplied, the value of the @env{HOME}
3763 shell variable is used.
3764 If the shell variable
3765 @env{CDPATH} exists, it is used as a search path:
3766 each directory name in @env{CDPATH} is searched for
3767 @var{directory}, with alternative directory names in @env{CDPATH}
3768 separated by a colon (@samp{:}).
3769 If @var{directory} begins with a slash, @env{CDPATH} is not used.
3771 The @option{-P} option means to not follow symbolic links: symbolic links
3772 are resolved while @code{cd} is traversing @var{directory} and before
3773 processing an instance of @samp{..} in @var{directory}.
3775 By default, or when the @option{-L} option is supplied, symbolic links
3776 in @var{directory} are resolved after @code{cd} processes an instance
3777 of @samp{..} in @var{directory}.
3779 If @samp{..} appears in @var{directory}, it is processed by removing the
3780 immediately preceding pathname component, back to a slash or the beginning
3783 If the @option{-e} option is supplied with @option{-P}
3784 and the current working directory cannot be successfully determined
3785 after a successful directory change, @code{cd} will return an unsuccessful
3788 On systems that support it, the @option{-@@} option presents the extended
3789 attributes associated with a file as a directory.
3791 If @var{directory} is @samp{-}, it is converted to @env{$OLDPWD}
3792 before the directory change is attempted.
3794 If a non-empty directory name from @env{CDPATH} is used, or if
3795 @samp{-} is the first argument, and the directory change is
3796 successful, the absolute pathname of the new working directory is
3797 written to the standard output.
3799 If the directory change is successful, @code{cd} sets the value of the
3800 @env{PWD} environment variable to the new directory name, and sets the
3801 @env{OLDPWD} environment variable to the value of the current working
3802 directory before the change.
3804 The return status is zero if the directory is successfully changed,
3813 Resume the next iteration of an enclosing @code{for}, @code{while},
3814 @code{until}, or @code{select} loop.
3815 If @var{n} is supplied, the execution of the @var{n}th enclosing loop
3817 @var{n} must be greater than or equal to 1.
3818 The return status is zero unless @var{n} is not greater than or equal to 1.
3823 eval [@var{arguments}]
3826 The arguments are concatenated together into a single command, which is
3827 then read and executed, and its exit status returned as the exit status
3829 If there are no arguments or only empty arguments, the return status is
3835 exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]]
3839 is supplied, it replaces the shell without creating a new process.
3840 If the @option{-l} option is supplied, the shell places a dash at the
3841 beginning of the zeroth argument passed to @var{command}.
3842 This is what the @code{login} program does.
3843 The @option{-c} option causes @var{command} to be executed with an empty
3845 If @option{-a} is supplied, the shell passes @var{name} as the zeroth
3846 argument to @var{command}.
3848 cannot be executed for some reason, a non-interactive shell exits,
3849 unless the @code{execfail} shell option
3850 is enabled. In that case, it returns failure.
3851 An interactive shell returns failure if the file cannot be executed.
3852 A subshell exits unconditionally if @code{exec} fails.
3853 If no @var{command} is specified, redirections may be used to affect
3854 the current shell environment. If there are no redirection errors, the
3855 return status is zero; otherwise the return status is non-zero.
3863 Exit the shell, returning a status of @var{n} to the shell's parent.
3864 If @var{n} is omitted, the exit status is that of the last command executed.
3865 Any trap on @code{EXIT} is executed before the shell terminates.
3870 export [-fn] [-p] [@var{name}[=@var{value}]]
3873 Mark each @var{name} to be passed to child processes
3874 in the environment. If the @option{-f} option is supplied, the @var{name}s
3875 refer to shell functions; otherwise the names refer to shell variables.
3876 The @option{-n} option means to no longer mark each @var{name} for export.
3877 If no @var{name}s are supplied, or if the @option{-p} option is given, a
3878 list of names of all exported variables is displayed.
3879 The @option{-p} option displays output in a form that may be reused as input.
3880 If a variable name is followed by =@var{value}, the value of
3881 the variable is set to @var{value}.
3883 The return status is zero unless an invalid option is supplied, one of
3884 the names is not a valid shell variable name, or @option{-f} is supplied
3885 with a name that is not a shell function.
3890 getopts @var{optstring} @var{name} [@var{arg} @dots{}]
3893 @code{getopts} is used by shell scripts to parse positional parameters.
3894 @var{optstring} contains the option characters to be recognized; if a
3895 character is followed by a colon, the option is expected to have an
3896 argument, which should be separated from it by whitespace.
3897 The colon (@samp{:}) and question mark (@samp{?}) may not be
3898 used as option characters.
3899 Each time it is invoked, @code{getopts}
3900 places the next option in the shell variable @var{name}, initializing
3901 @var{name} if it does not exist,
3902 and the index of the next argument to be processed into the
3903 variable @env{OPTIND}.
3904 @env{OPTIND} is initialized to 1 each time the shell or a shell script
3906 When an option requires an argument,
3907 @code{getopts} places that argument into the variable @env{OPTARG}.
3908 The shell does not reset @env{OPTIND} automatically; it must be manually
3909 reset between multiple calls to @code{getopts} within the same shell
3910 invocation if a new set of parameters is to be used.
3912 When the end of options is encountered, @code{getopts} exits with a
3913 return value greater than zero.
3914 @env{OPTIND} is set to the index of the first non-option argument,
3915 and @var{name} is set to @samp{?}.
3918 normally parses the positional parameters, but if more arguments are
3919 supplied as @var{arg} values, @code{getopts} parses those instead.
3921 @code{getopts} can report errors in two ways. If the first character of
3922 @var{optstring} is a colon, @var{silent}
3923 error reporting is used. In normal operation, diagnostic messages
3924 are printed when invalid options or missing option arguments are
3926 If the variable @env{OPTERR}
3927 is set to 0, no error messages will be displayed, even if the first
3928 character of @code{optstring} is not a colon.
3930 If an invalid option is seen,
3931 @code{getopts} places @samp{?} into @var{name} and, if not silent,
3932 prints an error message and unsets @env{OPTARG}.
3933 If @code{getopts} is silent, the option character found is placed in
3934 @env{OPTARG} and no diagnostic message is printed.
3936 If a required argument is not found, and @code{getopts}
3937 is not silent, a question mark (@samp{?}) is placed in @var{name},
3938 @code{OPTARG} is unset, and a diagnostic message is printed.
3939 If @code{getopts} is silent, then a colon (@samp{:}) is placed in
3940 @var{name} and @env{OPTARG} is set to the option character found.
3945 hash [-r] [-p @var{filename}] [-dt] [@var{name}]
3948 Each time @code{hash} is invoked, it remembers the full pathnames of the
3949 commands specified as @var{name} arguments,
3950 so they need not be searched for on subsequent invocations.
3951 The commands are found by searching through the directories listed in
3953 Any previously-remembered pathname is discarded.
3954 The @option{-p} option inhibits the path search, and @var{filename} is
3955 used as the location of @var{name}.
3956 The @option{-r} option causes the shell to forget all remembered locations.
3957 The @option{-d} option causes the shell to forget the remembered location
3959 If the @option{-t} option is supplied, the full pathname to which each
3960 @var{name} corresponds is printed. If multiple @var{name} arguments are
3961 supplied with @option{-t}, the @var{name} is printed before the hashed
3963 The @option{-l} option causes output to be displayed in a format
3964 that may be reused as input.
3965 If no arguments are given, or if only @option{-l} is supplied,
3966 information about remembered commands is printed.
3967 The return status is zero unless a @var{name} is not found or an invalid
3976 Print the absolute pathname of the current working directory.
3977 If the @option{-P} option is supplied, the pathname printed will not
3978 contain symbolic links.
3979 If the @option{-L} option is supplied, the pathname printed may contain
3981 The return status is zero unless an error is encountered while
3982 determining the name of the current directory or an invalid option
3988 readonly [-aAf] [-p] [@var{name}[=@var{value}]] @dots{}
3991 Mark each @var{name} as readonly.
3992 The values of these names may not be changed by subsequent assignment.
3993 If the @option{-f} option is supplied, each @var{name} refers to a shell
3995 The @option{-a} option means each @var{name} refers to an indexed
3996 array variable; the @option{-A} option means each @var{name} refers
3997 to an associative array variable.
3998 If both options are supplied, @option{-A} takes precedence.
3999 If no @var{name} arguments are given, or if the @option{-p}
4000 option is supplied, a list of all readonly names is printed.
4001 The other options may be used to restrict the output to a subset of
4002 the set of readonly names.
4003 The @option{-p} option causes output to be displayed in a format that
4004 may be reused as input.
4005 If a variable name is followed by =@var{value}, the value of
4006 the variable is set to @var{value}.
4007 The return status is zero unless an invalid option is supplied, one of
4008 the @var{name} arguments is not a valid shell variable or function name,
4009 or the @option{-f} option is supplied with a name that is not a shell function.
4017 Cause a shell function to stop executing and return the value @var{n}
4019 If @var{n} is not supplied, the return value is the exit status of the
4020 last command executed in the function.
4021 If @code{return} is executed by a trap handler, the last command used to
4022 determine the status is the last command executed before the trap handler.
4023 If @code{return} is executed during a @code{DEBUG} trap, the last command
4024 used to determine the status is the last command executed by the trap
4025 handler before @code{return} was invoked.
4026 @code{return} may also be used to terminate execution of a script
4027 being executed with the @code{.} (@code{source}) builtin,
4028 returning either @var{n} or
4029 the exit status of the last command executed within the script as the exit
4030 status of the script.
4031 If @var{n} is supplied, the return value is its least significant
4033 Any command associated with the @code{RETURN} trap is executed
4034 before execution resumes after the function or script.
4035 The return status is non-zero if @code{return} is supplied a non-numeric
4036 argument or is used outside a function
4037 and not during the execution of a script by @code{.} or @code{source}.
4045 Shift the positional parameters to the left by @var{n}.
4046 The positional parameters from @var{n}+1 @dots{} @code{$#} are
4047 renamed to @code{$1} @dots{} @code{$#}-@var{n}.
4048 Parameters represented by the numbers @code{$#} down to @code{$#}-@var{n}+1
4050 @var{n} must be a non-negative number less than or equal to @code{$#}.
4051 If @var{n} is zero or greater than @code{$#}, the positional parameters
4053 If @var{n} is not supplied, it is assumed to be 1.
4054 The return status is zero unless @var{n} is greater than @code{$#} or
4055 less than zero, non-zero otherwise.
4065 Evaluate a conditional expression @var{expr} and return a status of 0
4066 (true) or 1 (false).
4067 Each operator and operand must be a separate argument.
4068 Expressions are composed of the primaries described below in
4069 @ref{Bash Conditional Expressions}.
4070 @code{test} does not accept any options, nor does it accept and ignore
4071 an argument of @option{--} as signifying the end of options.
4073 When the @code{[} form is used, the last argument to the command must
4076 Expressions may be combined using the following operators, listed in
4077 decreasing order of precedence.
4078 The evaluation depends on the number of arguments; see below.
4079 Operator precedence is used when there are five or more arguments.
4083 True if @var{expr} is false.
4085 @item ( @var{expr} )
4086 Returns the value of @var{expr}.
4087 This may be used to override the normal precedence of operators.
4089 @item @var{expr1} -a @var{expr2}
4090 True if both @var{expr1} and @var{expr2} are true.
4092 @item @var{expr1} -o @var{expr2}
4093 True if either @var{expr1} or @var{expr2} is true.
4096 The @code{test} and @code{[} builtins evaluate conditional
4097 expressions using a set of rules based on the number of arguments.
4101 The expression is false.
4104 The expression is true if, and only if, the argument is not null.
4107 If the first argument is @samp{!}, the expression is true if and
4108 only if the second argument is null.
4109 If the first argument is one of the unary conditional operators
4110 (@pxref{Bash Conditional Expressions}), the expression
4111 is true if the unary test is true.
4112 If the first argument is not a valid unary operator, the expression is
4116 The following conditions are applied in the order listed.
4120 If the second argument is one of the binary conditional
4121 operators (@pxref{Bash Conditional Expressions}), the
4122 result of the expression is the result of the binary test using the
4123 first and third arguments as operands.
4124 The @samp{-a} and @samp{-o} operators are considered binary operators
4125 when there are three arguments.
4127 If the first argument is @samp{!}, the value is the negation of
4128 the two-argument test using the second and third arguments.
4130 If the first argument is exactly @samp{(} and the third argument is
4131 exactly @samp{)}, the result is the one-argument test of the second
4134 Otherwise, the expression is false.
4138 The following conditions are applied in the order listed.
4142 If the first argument is @samp{!}, the result is the negation of
4143 the three-argument expression composed of the remaining arguments.
4145 If the first argument is exactly @samp{(} and the fourth argument is
4146 exactly @samp{)}, the result is the two-argument test of the second
4147 and third arguments.
4149 Otherwise, the expression is parsed and evaluated according to
4150 precedence using the rules listed above.
4153 @item 5 or more arguments
4154 The expression is parsed and evaluated according to precedence
4155 using the rules listed above.
4158 When used with @code{test} or @samp{[}, the @samp{<} and @samp{>}
4159 operators sort lexicographically using ASCII ordering.
4167 Print out the user and system times used by the shell and its children.
4168 The return status is zero.
4173 trap [-lp] [@var{arg}] [@var{sigspec} @dots{}]
4176 The commands in @var{arg} are to be read and executed when the
4177 shell receives signal @var{sigspec}. If @var{arg} is absent (and
4178 there is a single @var{sigspec}) or
4179 equal to @samp{-}, each specified signal's disposition is reset
4180 to the value it had when the shell was started.
4181 If @var{arg} is the null string, then the signal specified by
4182 each @var{sigspec} is ignored by the shell and commands it invokes.
4183 If @var{arg} is not present and @option{-p} has been supplied,
4184 the shell displays the trap commands associated with each @var{sigspec}.
4185 If no arguments are supplied, or
4186 only @option{-p} is given, @code{trap} prints the list of commands
4187 associated with each signal number in a form that may be reused as
4189 The @option{-l} option causes the shell to print a list of signal names
4190 and their corresponding numbers.
4191 Each @var{sigspec} is either a signal name or a signal number.
4192 Signal names are case insensitive and the @code{SIG} prefix is optional.
4195 is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits.
4196 If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed
4197 before every simple command, @code{for} command, @code{case} command,
4198 @code{select} command, every arithmetic @code{for} command, and before
4199 the first command executes in a shell function.
4200 Refer to the description of the @code{extdebug} option to the
4201 @code{shopt} builtin (@pxref{The Shopt Builtin}) for details of its
4202 effect on the @code{DEBUG} trap.
4203 If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed
4204 each time a shell function or a script executed with the @code{.} or
4205 @code{source} builtins finishes executing.
4207 If a @var{sigspec} is @code{ERR}, the command @var{arg}
4208 is executed whenever
4209 a pipeline (which may consist of a single simple
4210 command), a list, or a compound command returns a
4211 non-zero exit status,
4212 subject to the following conditions.
4213 The @code{ERR} trap is not executed if the failed command is part of the
4214 command list immediately following an @code{until} or @code{while} keyword,
4215 part of the test following the @code{if} or @code{elif} reserved words,
4216 part of a command executed in a @code{&&} or @code{||} list
4217 except the command following the final @code{&&} or @code{||},
4218 any command in a pipeline but the last,
4219 or if the command's return
4220 status is being inverted using @code{!}.
4221 These are the same conditions obeyed by the @code{errexit} (@option{-e})
4224 Signals ignored upon entry to the shell cannot be trapped or reset.
4225 Trapped signals that are not being ignored are reset to their original
4226 values in a subshell or subshell environment when one is created.
4228 The return status is zero unless a @var{sigspec} does not specify a
4234 umask [-p] [-S] [@var{mode}]
4237 Set the shell process's file creation mask to @var{mode}. If
4238 @var{mode} begins with a digit, it is interpreted as an octal number;
4239 if not, it is interpreted as a symbolic mode mask similar
4240 to that accepted by the @code{chmod} command. If @var{mode} is
4241 omitted, the current value of the mask is printed. If the @option{-S}
4242 option is supplied without a @var{mode} argument, the mask is printed
4243 in a symbolic format.
4244 If the @option{-p} option is supplied, and @var{mode}
4245 is omitted, the output is in a form that may be reused as input.
4246 The return status is zero if the mode is successfully changed or if
4247 no @var{mode} argument is supplied, and non-zero otherwise.
4249 Note that when the mode is interpreted as an octal number, each number
4250 of the umask is subtracted from @code{7}. Thus, a umask of @code{022}
4251 results in permissions of @code{755}.
4256 unset [-fnv] [@var{name}]
4259 Remove each variable or function @var{name}.
4260 If the @option{-v} option is given, each
4261 @var{name} refers to a shell variable and that variable is removed.
4262 If the @option{-f} option is given, the @var{name}s refer to shell
4263 functions, and the function definition is removed.
4264 If the @option{-n} option is supplied, and @var{name} is a variable with
4265 the @code{nameref} attribute, @var{name} will be unset rather than the
4266 variable it references.
4267 @option{-n} has no effect if the @option{-f} option is supplied.
4268 If no options are supplied, each @var{name} refers to a variable; if
4269 there is no variable by that name, a function with that name, if any, is
4271 Readonly variables and functions may not be unset.
4272 Some shell variables lose their special behavior if they are unset; such
4273 behavior is noted in the description of the individual variables.
4274 The return status is zero unless a @var{name} is readonly or may not be unset.
4278 @section Bash Builtin Commands
4280 This section describes builtin commands which are unique to
4281 or have been extended in Bash.
4282 Some of these commands are specified in the @sc{posix} standard.
4289 alias [-p] [@var{name}[=@var{value}] @dots{}]
4292 Without arguments or with the @option{-p} option, @code{alias} prints
4293 the list of aliases on the standard output in a form that allows
4294 them to be reused as input.
4295 If arguments are supplied, an alias is defined for each @var{name}
4296 whose @var{value} is given. If no @var{value} is given, the name
4297 and value of the alias is printed.
4298 Aliases are described in @ref{Aliases}.
4303 bind [-m @var{keymap}] [-lpsvPSVX]
4304 bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}]
4305 bind [-m @var{keymap}] -f @var{filename}
4306 bind [-m @var{keymap}] -x @var{keyseq:shell-command}
4307 bind [-m @var{keymap}] @var{keyseq:function-name}
4308 bind [-m @var{keymap}] @var{keyseq:readline-command}
4309 bind @var{readline-command-line}
4312 Display current Readline (@pxref{Command Line Editing})
4313 key and function bindings,
4314 bind a key sequence to a Readline function or macro,
4315 or set a Readline variable.
4316 Each non-option argument is a command as it would appear in a
4317 Readline initialization file (@pxref{Readline Init File}),
4318 but each binding or command must be passed as a separate argument; e.g.,
4319 @samp{"\C-x\C-r":re-read-init-file}.
4321 Options, if supplied, have the following meanings:
4324 @item -m @var{keymap}
4325 Use @var{keymap} as the keymap to be affected by
4326 the subsequent bindings. Acceptable @var{keymap}
4329 @code{emacs-standard},
4334 @code{vi-command}, and
4336 @code{vi} is equivalent to @code{vi-command} (@code{vi-move} is also a
4337 synonym); @code{emacs} is equivalent to @code{emacs-standard}.
4340 List the names of all Readline functions.
4343 Display Readline function names and bindings in such a way that they
4344 can be used as input or in a Readline initialization file.
4347 List current Readline function names and bindings.
4350 Display Readline variable names and values in such a way that they
4351 can be used as input or in a Readline initialization file.
4354 List current Readline variable names and values.
4357 Display Readline key sequences bound to macros and the strings they output
4358 in such a way that they can be used as input or in a Readline
4359 initialization file.
4362 Display Readline key sequences bound to macros and the strings they output.
4364 @item -f @var{filename}
4365 Read key bindings from @var{filename}.
4367 @item -q @var{function}
4368 Query about which keys invoke the named @var{function}.
4370 @item -u @var{function}
4371 Unbind all keys bound to the named @var{function}.
4373 @item -r @var{keyseq}
4374 Remove any current binding for @var{keyseq}.
4376 @item -x @var{keyseq:shell-command}
4377 Cause @var{shell-command} to be executed whenever @var{keyseq} is
4379 When @var{shell-command} is executed, the shell sets the
4380 @code{READLINE_LINE} variable to the contents of the Readline line
4381 buffer and the @code{READLINE_POINT} and @code{READLINE_MARK} variables
4382 to the current location of the insertion point and the saved insertion
4383 point (the @var{mark}), respectively.
4384 The shell assigns any numeric argument the user supplied to the
4385 @code{READLINE_ARGUMENT} variable.
4386 If there was no argument, that variable is not set.
4387 If the executed command changes the value of any of @code{READLINE_LINE},
4388 @code{READLINE_POINT}, or @code{READLINE_MARK}, those new values will be
4389 reflected in the editing state.
4392 List all key sequences bound to shell commands and the associated commands
4393 in a format that can be reused as input.
4397 The return status is zero unless an invalid option is supplied or an
4403 builtin [@var{shell-builtin} [@var{args}]]
4406 Run a shell builtin, passing it @var{args}, and return its exit status.
4407 This is useful when defining a shell function with the same
4408 name as a shell builtin, retaining the functionality of the builtin within
4410 The return status is non-zero if @var{shell-builtin} is not a shell
4419 Returns the context of any active subroutine call (a shell function or
4420 a script executed with the @code{.} or @code{source} builtins).
4422 Without @var{expr}, @code{caller} displays the line number and source
4423 filename of the current subroutine call.
4424 If a non-negative integer is supplied as @var{expr}, @code{caller}
4425 displays the line number, subroutine name, and source file corresponding
4426 to that position in the current execution call stack. This extra
4427 information may be used, for example, to print a stack trace. The
4428 current frame is frame 0.
4430 The return value is 0 unless the shell is not executing a subroutine
4431 call or @var{expr} does not correspond to a valid position in the
4437 command [-pVv] @var{command} [@var{arguments} @dots{}]
4440 Runs @var{command} with @var{arguments} ignoring any shell function
4441 named @var{command}.
4442 Only shell builtin commands or commands found by searching the
4443 @env{PATH} are executed.
4444 If there is a shell function named @code{ls}, running @samp{command ls}
4445 within the function will execute the external command @code{ls}
4446 instead of calling the function recursively.
4447 The @option{-p} option means to use a default value for @env{PATH}
4448 that is guaranteed to find all of the standard utilities.
4449 The return status in this case is 127 if @var{command} cannot be
4450 found or an error occurred, and the exit status of @var{command}
4453 If either the @option{-V} or @option{-v} option is supplied, a
4454 description of @var{command} is printed. The @option{-v} option
4455 causes a single word indicating the command or file name used to
4456 invoke @var{command} to be displayed; the @option{-V} option produces
4457 a more verbose description. In this case, the return status is
4458 zero if @var{command} is found, and non-zero if not.
4463 declare [-aAfFgiIlnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
4466 Declare variables and give them attributes. If no @var{name}s
4467 are given, then display the values of variables instead.
4469 The @option{-p} option will display the attributes and values of each
4471 When @option{-p} is used with @var{name} arguments, additional options,
4472 other than @option{-f} and @option{-F}, are ignored.
4474 When @option{-p} is supplied without @var{name} arguments, @code{declare}
4475 will display the attributes and values of all variables having the
4476 attributes specified by the additional options.
4477 If no other options are supplied with @option{-p}, @code{declare} will
4478 display the attributes and values of all shell variables. The @option{-f}
4479 option will restrict the display to shell functions.
4481 The @option{-F} option inhibits the display of function definitions;
4482 only the function name and attributes are printed.
4483 If the @code{extdebug} shell option is enabled using @code{shopt}
4484 (@pxref{The Shopt Builtin}), the source file name and line number where
4485 each @var{name} is defined are displayed as well.
4486 @option{-F} implies @option{-f}.
4488 The @option{-g} option forces variables to be created or modified at
4489 the global scope, even when @code{declare} is executed in a shell function.
4490 It is ignored in all other cases.
4492 The @option{-I} option causes local variables to inherit the attributes
4493 (except the @code{nameref} attribute)
4494 and value of any existing variable with the same
4495 @var{name} at a surrounding scope.
4496 If there is no existing variable, the local variable is initially unset.
4498 The following options can be used to restrict output to variables with
4499 the specified attributes or to give variables attributes:
4503 Each @var{name} is an indexed array variable (@pxref{Arrays}).
4506 Each @var{name} is an associative array variable (@pxref{Arrays}).
4509 Use function names only.
4512 The variable is to be treated as
4513 an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is
4514 performed when the variable is assigned a value.
4517 When the variable is assigned a value, all upper-case characters are
4518 converted to lower-case.
4519 The upper-case attribute is disabled.
4522 Give each @var{name} the @code{nameref} attribute, making
4523 it a name reference to another variable.
4524 That other variable is defined by the value of @var{name}.
4525 All references, assignments, and attribute modifications
4526 to @var{name}, except for those using or changing the
4527 @option{-n} attribute itself, are performed on the variable referenced by
4529 The nameref attribute cannot be applied to array variables.
4532 Make @var{name}s readonly. These names cannot then be assigned values
4533 by subsequent assignment statements or unset.
4536 Give each @var{name} the @code{trace} attribute.
4537 Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from
4539 The trace attribute has no special meaning for variables.
4542 When the variable is assigned a value, all lower-case characters are
4543 converted to upper-case.
4544 The lower-case attribute is disabled.
4547 Mark each @var{name} for export to subsequent commands via
4551 Using @samp{+} instead of @samp{-} turns off the attribute instead,
4552 with the exceptions that @samp{+a} and @samp{+A}
4553 may not be used to destroy array variables and @samp{+r} will not
4554 remove the readonly attribute.
4555 When used in a function, @code{declare} makes each @var{name} local,
4556 as with the @code{local} command, unless the @option{-g} option is used.
4557 If a variable name is followed by =@var{value}, the value of the variable
4558 is set to @var{value}.
4560 When using @option{-a} or @option{-A} and the compound assignment syntax to
4561 create array variables, additional attributes do not take effect until
4562 subsequent assignments.
4564 The return status is zero unless an invalid option is encountered,
4565 an attempt is made to define a function using @samp{-f foo=bar},
4566 an attempt is made to assign a value to a readonly variable,
4567 an attempt is made to assign a value to an array variable without
4568 using the compound assignment syntax (@pxref{Arrays}),
4569 one of the @var{name}s is not a valid shell variable name,
4570 an attempt is made to turn off readonly status for a readonly variable,
4571 an attempt is made to turn off array status for an array variable,
4572 or an attempt is made to display a non-existent function with @option{-f}.
4577 echo [-neE] [@var{arg} @dots{}]
4580 Output the @var{arg}s, separated by spaces, terminated with a
4582 The return status is 0 unless a write error occurs.
4583 If @option{-n} is specified, the trailing newline is suppressed.
4584 If the @option{-e} option is given, interpretation of the following
4585 backslash-escaped characters is enabled.
4586 The @option{-E} option disables the interpretation of these escape characters,
4587 even on systems where they are interpreted by default.
4588 The @code{xpg_echo} shell option may be used to
4589 dynamically determine whether or not @code{echo} expands these
4590 escape characters by default.
4591 @code{echo} does not interpret @option{--} to mean the end of options.
4593 @code{echo} interprets the following escape sequences:
4600 suppress further output
4617 the eight-bit character whose value is the octal value @var{nnn}
4618 (zero to three octal digits)
4620 the eight-bit character whose value is the hexadecimal value @var{HH}
4621 (one or two hex digits)
4623 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4624 @var{HHHH} (one to four hex digits)
4625 @item \U@var{HHHHHHHH}
4626 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4627 @var{HHHHHHHH} (one to eight hex digits)
4633 enable [-a] [-dnps] [-f @var{filename}] [@var{name} @dots{}]
4636 Enable and disable builtin shell commands.
4637 Disabling a builtin allows a disk command which has the same name
4638 as a shell builtin to be executed without specifying a full pathname,
4639 even though the shell normally searches for builtins before disk commands.
4640 If @option{-n} is used, the @var{name}s become disabled. Otherwise
4641 @var{name}s are enabled. For example, to use the @code{test} binary
4642 found via @env{$PATH} instead of the shell builtin version, type
4643 @samp{enable -n test}.
4645 If the @option{-p} option is supplied, or no @var{name} arguments appear,
4646 a list of shell builtins is printed. With no other arguments, the list
4647 consists of all enabled shell builtins.
4648 The @option{-a} option means to list
4649 each builtin with an indication of whether or not it is enabled.
4651 The @option{-f} option means to load the new builtin command @var{name}
4652 from shared object @var{filename}, on systems that support dynamic loading.
4653 Bash will use the value of the @env{BASH_LOADABLES_PATH} variable as a
4654 colon-separated list of directories in which to search for @var{filename}.
4655 The default is system-dependent.
4656 The @option{-d} option will delete a builtin loaded with @option{-f}.
4658 If there are no options, a list of the shell builtins is displayed.
4659 The @option{-s} option restricts @code{enable} to the @sc{posix} special
4660 builtins. If @option{-s} is used with @option{-f}, the new builtin becomes
4661 a special builtin (@pxref{Special Builtins}).
4663 If no options are supplied and a @var{name} is not a shell builtin,
4664 @code{enable} will attempt to load @var{name} from a shared object named
4665 @var{name}, as if the command were
4666 @samp{enable -f @var{name} @var{name}}.
4668 The return status is zero unless a @var{name} is not a shell builtin
4669 or there is an error loading a new builtin from a shared object.
4674 help [-dms] [@var{pattern}]
4677 Display helpful information about builtin commands.
4678 If @var{pattern} is specified, @code{help} gives detailed help
4679 on all commands matching @var{pattern}, otherwise a list of
4680 the builtins is printed.
4682 Options, if supplied, have the following meanings:
4686 Display a short description of each @var{pattern}
4688 Display the description of each @var{pattern} in a manpage-like format
4690 Display only a short usage synopsis for each @var{pattern}
4693 The return status is zero unless no command matches @var{pattern}.
4698 let @var{expression} [@var{expression} @dots{}]
4701 The @code{let} builtin allows arithmetic to be performed on shell
4702 variables. Each @var{expression} is evaluated according to the
4703 rules given below in @ref{Shell Arithmetic}. If the
4704 last @var{expression} evaluates to 0, @code{let} returns 1;
4705 otherwise 0 is returned.
4710 local [@var{option}] @var{name}[=@var{value}] @dots{}
4713 For each argument, a local variable named @var{name} is created,
4714 and assigned @var{value}.
4715 The @var{option} can be any of the options accepted by @code{declare}.
4716 @code{local} can only be used within a function; it makes the variable
4717 @var{name} have a visible scope restricted to that function and its
4719 If @var{name} is @samp{-}, the set of shell options is made local to the
4720 function in which @code{local} is invoked: shell options changed using
4721 the @code{set} builtin inside the function are restored to their original
4722 values when the function returns.
4723 The restore is effected as if a series of @code{set} commands were executed
4724 to restore the values that were in place before the function.
4725 The return status is zero unless @code{local} is used outside
4726 a function, an invalid @var{name} is supplied, or @var{name} is a
4735 Exit a login shell, returning a status of @var{n} to the shell's
4741 mapfile [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
4742 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
4745 Read lines from the standard input into the indexed array variable @var{array},
4746 or from file descriptor @var{fd}
4747 if the @option{-u} option is supplied.
4748 The variable @code{MAPFILE} is the default @var{array}.
4749 Options, if supplied, have the following meanings:
4754 The first character of @var{delim} is used to terminate each input line,
4755 rather than newline.
4756 If @var{delim} is the empty string, @code{mapfile} will terminate a line
4757 when it reads a NUL character.
4759 Copy at most @var{count} lines. If @var{count} is 0, all lines are copied.
4761 Begin assigning to @var{array} at index @var{origin}.
4762 The default index is 0.
4764 Discard the first @var{count} lines read.
4766 Remove a trailing @var{delim} (default newline) from each line read.
4768 Read lines from file descriptor @var{fd} instead of the standard input.
4770 Evaluate @var{callback} each time @var{quantum} lines are read.
4771 The @option{-c} option specifies @var{quantum}.
4773 Specify the number of lines read between each call to @var{callback}.
4776 If @option{-C} is specified without @option{-c},
4777 the default quantum is 5000.
4778 When @var{callback} is evaluated, it is supplied the index of the next
4779 array element to be assigned and the line to be assigned to that element
4780 as additional arguments.
4781 @var{callback} is evaluated after the line is read but before the
4782 array element is assigned.
4784 If not supplied with an explicit origin, @code{mapfile} will clear @var{array}
4785 before assigning to it.
4787 @code{mapfile} returns successfully unless an invalid option or option
4788 argument is supplied, @var{array} is invalid or unassignable, or @var{array}
4789 is not an indexed array.
4794 printf [-v @var{var}] @var{format} [@var{arguments}]
4797 Write the formatted @var{arguments} to the standard output under the
4798 control of the @var{format}.
4799 The @option{-v} option causes the output to be assigned to the variable
4800 @var{var} rather than being printed to the standard output.
4802 The @var{format} is a character string which contains three types of objects:
4803 plain characters, which are simply copied to standard output, character
4804 escape sequences, which are converted and copied to the standard output, and
4805 format specifications, each of which causes printing of the next successive
4807 In addition to the standard @code{printf(1)} formats, @code{printf}
4808 interprets the following extensions:
4812 Causes @code{printf} to expand backslash escape sequences in the
4813 corresponding @var{argument} in the same way as @code{echo -e}
4814 (@pxref{Bash Builtins}).
4816 Causes @code{printf} to output the
4817 corresponding @var{argument} in a format that can be reused as shell input.
4819 like @code{%q}, but applies any supplied precision to the @var{argument}
4821 @item %(@var{datefmt})T
4822 Causes @code{printf} to output the date-time string resulting from using
4823 @var{datefmt} as a format string for @code{strftime}(3).
4824 The corresponding @var{argument} is an integer representing the number of
4825 seconds since the epoch.
4826 Two special argument values may be used: -1 represents the current
4827 time, and -2 represents the time the shell was invoked.
4828 If no argument is specified, conversion behaves as if -1 had been given.
4829 This is an exception to the usual @code{printf} behavior.
4833 The %b, %q, and %T directives all use the field width and precision
4834 arguments from the format specification and write that many bytes from
4835 (or use that wide a field for) the expanded argument, which usually
4836 contains more characters than the original.
4838 Arguments to non-string format specifiers are treated as C language constants,
4839 except that a leading plus or minus sign is allowed, and if the leading
4840 character is a single or double quote, the value is the ASCII value of
4841 the following character.
4843 The @var{format} is reused as necessary to consume all of the @var{arguments}.
4844 If the @var{format} requires more @var{arguments} than are supplied, the
4845 extra format specifications behave as if a zero value or null string, as
4846 appropriate, had been supplied. The return value is zero on success,
4847 non-zero on failure.
4852 read [-ers] [-a @var{aname}] [-d @var{delim}] [-i @var{text}] [-n @var{nchars}]
4853 [-N @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}]
4856 One line is read from the standard input, or from the file descriptor
4857 @var{fd} supplied as an argument to the @option{-u} option,
4858 split into words as described above in @ref{Word Splitting},
4860 is assigned to the first @var{name}, the second word to the second @var{name},
4862 If there are more words than names,
4863 the remaining words and their intervening delimiters are assigned
4864 to the last @var{name}.
4865 If there are fewer words read from the input stream than names,
4866 the remaining names are assigned empty values.
4867 The characters in the value of the @env{IFS} variable
4868 are used to split the line into words using the same rules the shell
4869 uses for expansion (described above in @ref{Word Splitting}).
4870 The backslash character @samp{\} may be used to remove any special
4871 meaning for the next character read and for line continuation.
4873 Options, if supplied, have the following meanings:
4876 @item -a @var{aname}
4877 The words are assigned to sequential indices of the array variable
4878 @var{aname}, starting at 0.
4879 All elements are removed from @var{aname} before the assignment.
4880 Other @var{name} arguments are ignored.
4882 @item -d @var{delim}
4883 The first character of @var{delim} is used to terminate the input line,
4884 rather than newline.
4885 If @var{delim} is the empty string, @code{read} will terminate a line
4886 when it reads a NUL character.
4889 Readline (@pxref{Command Line Editing}) is used to obtain the line.
4890 Readline uses the current (or default, if line editing was not previously
4891 active) editing settings, but uses Readline's default filename completion.
4894 If Readline is being used to read the line, @var{text} is placed into
4895 the editing buffer before editing begins.
4897 @item -n @var{nchars}
4898 @code{read} returns after reading @var{nchars} characters rather than
4899 waiting for a complete line of input, but honors a delimiter if fewer
4900 than @var{nchars} characters are read before the delimiter.
4902 @item -N @var{nchars}
4903 @code{read} returns after reading exactly @var{nchars} characters rather
4904 than waiting for a complete line of input, unless EOF is encountered or
4905 @code{read} times out.
4906 Delimiter characters encountered in the input are
4907 not treated specially and do not cause @code{read} to return until
4908 @var{nchars} characters are read.
4909 The result is not split on the characters in @code{IFS}; the intent is
4910 that the variable is assigned exactly the characters read
4911 (with the exception of backslash; see the @option{-r} option below).
4913 @item -p @var{prompt}
4914 Display @var{prompt}, without a trailing newline, before attempting
4916 The prompt is displayed only if input is coming from a terminal.
4919 If this option is given, backslash does not act as an escape character.
4920 The backslash is considered to be part of the line.
4921 In particular, a backslash-newline pair may not then be used as a line
4925 Silent mode. If input is coming from a terminal, characters are
4928 @item -t @var{timeout}
4929 Cause @code{read} to time out and return failure if a complete line of
4930 input (or a specified number of characters)
4931 is not read within @var{timeout} seconds.
4932 @var{timeout} may be a decimal number with a fractional portion following
4934 This option is only effective if @code{read} is reading input from a
4935 terminal, pipe, or other special file; it has no effect when reading
4937 If @code{read} times out, @code{read} saves any partial input read into
4938 the specified variable @var{name}.
4939 If @var{timeout} is 0, @code{read} returns immediately, without trying to
4941 The exit status is 0 if input is available on the specified file descriptor,
4942 or the read will return EOF,
4944 The exit status is greater than 128 if the timeout is exceeded.
4947 Read input from file descriptor @var{fd}.
4950 If no @var{name}s are supplied, the line read,
4951 without the ending delimiter but otherwise unmodified,
4953 variable @env{REPLY}.
4954 The exit status is zero, unless end-of-file is encountered, @code{read}
4955 times out (in which case the status is greater than 128),
4956 a variable assignment error (such as assigning to a readonly variable) occurs,
4957 or an invalid file descriptor is supplied as the argument to @option{-u}.
4962 readarray [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
4963 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
4966 Read lines from the standard input into the indexed array variable @var{array},
4967 or from file descriptor @var{fd}
4968 if the @option{-u} option is supplied.
4970 A synonym for @code{mapfile}.
4975 source @var{filename}
4978 A synonym for @code{.} (@pxref{Bourne Shell Builtins}).
4983 type [-afptP] [@var{name} @dots{}]
4986 For each @var{name}, indicate how it would be interpreted if used as a
4989 If the @option{-t} option is used, @code{type} prints a single word
4990 which is one of @samp{alias}, @samp{function}, @samp{builtin},
4991 @samp{file} or @samp{keyword},
4992 if @var{name} is an alias, shell function, shell builtin,
4993 disk file, or shell reserved word, respectively.
4994 If the @var{name} is not found, then nothing is printed, and
4995 @code{type} returns a failure status.
4997 If the @option{-p} option is used, @code{type} either returns the name
4998 of the disk file that would be executed, or nothing if @option{-t}
4999 would not return @samp{file}.
5001 The @option{-P} option forces a path search for each @var{name}, even if
5002 @option{-t} would not return @samp{file}.
5004 If a command is hashed, @option{-p} and @option{-P} print the hashed value,
5005 which is not necessarily the file that appears first in @code{$PATH}.
5007 If the @option{-a} option is used, @code{type} returns all of the places
5008 that contain an executable named @var{file}.
5009 This includes aliases and functions, if and only if the @option{-p} option
5012 If the @option{-f} option is used, @code{type} does not attempt to find
5013 shell functions, as with the @code{command} builtin.
5015 The return status is zero if all of the @var{name}s are found, non-zero
5016 if any are not found.
5021 typeset [-afFgrxilnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
5024 The @code{typeset} command is supplied for compatibility with the Korn
5026 It is a synonym for the @code{declare} builtin command.
5032 ulimit [-HS] [-bcdefiklmnpqrstuvxPRT] [@var{limit}]
5035 @code{ulimit} provides control over the resources available to processes
5036 started by the shell, on systems that allow such control. If an
5037 option is given, it is interpreted as follows:
5041 Change and report the soft limit associated with a resource.
5044 Change and report the hard limit associated with a resource.
5047 All current limits are reported; no limits are set.
5050 The maximum socket buffer size.
5053 The maximum size of core files created.
5056 The maximum size of a process's data segment.
5059 The maximum scheduling priority ("nice").
5062 The maximum size of files written by the shell and its children.
5065 The maximum number of pending signals.
5068 The maximum number of kqueues that may be allocated.
5071 The maximum size that may be locked into memory.
5074 The maximum resident set size (many systems do not honor this limit).
5077 The maximum number of open file descriptors (most systems do not
5078 allow this value to be set).
5081 The pipe buffer size.
5084 The maximum number of bytes in @sc{posix} message queues.
5087 The maximum real-time scheduling priority.
5090 The maximum stack size.
5093 The maximum amount of cpu time in seconds.
5096 The maximum number of processes available to a single user.
5099 The maximum amount of virtual memory available to the shell, and, on
5100 some systems, to its children.
5103 The maximum number of file locks.
5106 The maximum number of pseudoterminals.
5109 The maximum time a real-time process can run before blocking, in microseconds.
5112 The maximum number of threads.
5115 If @var{limit} is given, and the @option{-a} option is not used,
5116 @var{limit} is the new value of the specified resource.
5117 The special @var{limit} values @code{hard}, @code{soft}, and
5118 @code{unlimited} stand for the current hard limit, the current soft limit,
5119 and no limit, respectively.
5120 A hard limit cannot be increased by a non-root user once it is set;
5121 a soft limit may be increased up to the value of the hard limit.
5122 Otherwise, the current value of the soft limit for the specified resource
5123 is printed, unless the @option{-H} option is supplied.
5125 resource is specified, the limit name and unit, if appropriate,
5126 are printed before the value.
5127 When setting new limits, if neither @option{-H} nor @option{-S} is supplied,
5128 both the hard and soft limits are set.
5129 If no option is given, then @option{-f} is assumed. Values are in 1024-byte
5130 increments, except for
5131 @option{-t}, which is in seconds;
5132 @option{-R}, which is in microseconds;
5133 @option{-p}, which is in units of 512-byte blocks;
5138 @option{-n} and @option{-u}, which are unscaled values;
5139 and, when in @sc{posix} Mode (@pxref{Bash POSIX Mode}),
5140 @option{-c} and @option{-f}, which are in 512-byte increments.
5142 The return status is zero unless an invalid option or argument is supplied,
5143 or an error occurs while setting a new limit.
5148 unalias [-a] [@var{name} @dots{} ]
5151 Remove each @var{name} from the list of aliases. If @option{-a} is
5152 supplied, all aliases are removed.
5153 Aliases are described in @ref{Aliases}.
5156 @node Modifying Shell Behavior
5157 @section Modifying Shell Behavior
5160 * The Set Builtin:: Change the values of shell attributes and
5161 positional parameters.
5162 * The Shopt Builtin:: Modify shell optional behavior.
5165 @node The Set Builtin
5166 @subsection The Set Builtin
5168 This builtin is so complicated that it deserves its own section. @code{set}
5169 allows you to change the values of shell options and set the positional
5170 parameters, or to display the names and values of shell variables.
5176 set [-abefhkmnptuvxBCEHPT] [-o @var{option-name}] [--] [-] [@var{argument} @dots{}]
5177 set [+abefhkmnptuvxBCEHPT] [+o @var{option-name}] [--] [-] [@var{argument} @dots{}]
5180 If no options or arguments are supplied, @code{set} displays the names
5181 and values of all shell variables and functions, sorted according to the
5182 current locale, in a format that may be reused as input
5183 for setting or resetting the currently-set variables.
5184 Read-only variables cannot be reset.
5185 In @sc{posix} mode, only shell variables are listed.
5187 When options are supplied, they set or unset shell attributes.
5188 Options, if specified, have the following meanings:
5192 Each variable or function that is created or modified is given the
5193 export attribute and marked for export to the environment of
5194 subsequent commands.
5197 Cause the status of terminated background jobs to be reported
5198 immediately, rather than before printing the next primary prompt.
5202 a pipeline (@pxref{Pipelines}), which may consist of a single simple command
5203 (@pxref{Simple Commands}),
5204 a list (@pxref{Lists}),
5205 or a compound command (@pxref{Compound Commands})
5206 returns a non-zero status.
5207 The shell does not exit if the command that fails is part of the
5208 command list immediately following a @code{while} or @code{until} keyword,
5209 part of the test in an @code{if} statement,
5210 part of any command executed in a @code{&&} or @code{||} list except
5211 the command following the final @code{&&} or @code{||},
5212 any command in a pipeline but the last,
5213 or if the command's return status is being inverted with @code{!}.
5214 If a compound command other than a subshell
5215 returns a non-zero status because a command failed
5216 while @option{-e} was being ignored, the shell does not exit.
5217 A trap on @code{ERR}, if set, is executed before the shell exits.
5219 This option applies to the shell environment and each subshell environment
5220 separately (@pxref{Command Execution Environment}), and may cause
5221 subshells to exit before executing all the commands in the subshell.
5223 If a compound command or shell function executes in a context where
5224 @option{-e} is being ignored,
5225 none of the commands executed within the compound command or function body
5226 will be affected by the @option{-e} setting, even if @option{-e} is set
5227 and a command returns a failure status.
5228 If a compound command or shell function sets @option{-e} while executing in
5229 a context where @option{-e} is ignored, that setting will not have any
5230 effect until the compound command or the command containing the function
5234 Disable filename expansion (globbing).
5237 Locate and remember (hash) commands as they are looked up for execution.
5238 This option is enabled by default.
5241 All arguments in the form of assignment statements are placed
5242 in the environment for a command, not just those that precede
5246 Job control is enabled (@pxref{Job Control}).
5247 All processes run in a separate process group.
5248 When a background job completes, the shell prints a line
5249 containing its exit status.
5252 Read commands but do not execute them.
5253 This may be used to check a script for syntax errors.
5254 This option is ignored by interactive shells.
5256 @item -o @var{option-name}
5258 Set the option corresponding to @var{option-name}:
5268 Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}).
5269 This also affects the editing interface used for @code{read -e}.
5287 Enable command history, as described in @ref{Bash History Facilities}.
5288 This option is on by default in interactive shells.
5291 An interactive shell will not exit upon reading EOF.
5324 If set, the return value of a pipeline is the value of the last
5325 (rightmost) command to exit with a non-zero status, or zero if all
5326 commands in the pipeline exit successfully.
5327 This option is disabled by default.
5330 Change the behavior of Bash where the default operation differs
5331 from the @sc{posix} standard to match the standard
5332 (@pxref{Bash POSIX Mode}).
5333 This is intended to make Bash behave as a strict superset of that
5343 Use a @code{vi}-style line editing interface.
5344 This also affects the editing interface used for @code{read -e}.
5351 Turn on privileged mode.
5352 In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not
5353 processed, shell functions are not inherited from the environment,
5354 and the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH} and @env{GLOBIGNORE}
5355 variables, if they appear in the environment, are ignored.
5356 If the shell is started with the effective user (group) id not equal to the
5357 real user (group) id, and the @option{-p} option is not supplied, these actions
5358 are taken and the effective user id is set to the real user id.
5359 If the @option{-p} option is supplied at startup, the effective user id is
5361 Turning this option off causes the effective user
5362 and group ids to be set to the real user and group ids.
5365 Enable restricted shell mode.
5366 This option cannot be unset once it has been set.
5369 Exit after reading and executing one command.
5372 Treat unset variables and parameters other than the special parameters
5373 @samp{@@} or @samp{*} as an error when performing parameter expansion.
5374 An error message will be written to the standard error, and a non-interactive
5378 Print shell input lines as they are read.
5381 Print a trace of simple commands, @code{for} commands, @code{case}
5382 commands, @code{select} commands, and arithmetic @code{for} commands
5383 and their arguments or associated word lists after they are
5384 expanded and before they are executed. The value of the @env{PS4}
5385 variable is expanded and the resultant value is printed before
5386 the command and its expanded arguments.
5389 The shell will perform brace expansion (@pxref{Brace Expansion}).
5390 This option is on by default.
5393 Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>}
5394 from overwriting existing files.
5397 If set, any trap on @code{ERR} is inherited by shell functions, command
5398 substitutions, and commands executed in a subshell environment.
5399 The @code{ERR} trap is normally not inherited in such cases.
5402 Enable @samp{!} style history substitution (@pxref{History Interaction}).
5403 This option is on by default for interactive shells.
5406 If set, do not resolve symbolic links when performing commands such as
5407 @code{cd} which change the current directory. The physical directory
5408 is used instead. By default, Bash follows
5409 the logical chain of directories when performing commands
5410 which change the current directory.
5412 For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys}
5415 $ cd /usr/sys; echo $PWD
5422 If @code{set -P} is on, then:
5424 $ cd /usr/sys; echo $PWD
5431 If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by
5432 shell functions, command substitutions, and commands executed
5433 in a subshell environment.
5434 The @code{DEBUG} and @code{RETURN} traps are normally not inherited
5438 If no arguments follow this option, then the positional parameters are
5439 unset. Otherwise, the positional parameters are set to the
5440 @var{arguments}, even if some of them begin with a @samp{-}.
5443 Signal the end of options, cause all remaining @var{arguments}
5444 to be assigned to the positional parameters. The @option{-x}
5445 and @option{-v} options are turned off.
5446 If there are no arguments, the positional parameters remain unchanged.
5449 Using @samp{+} rather than @samp{-} causes these options to be
5450 turned off. The options can also be used upon invocation of the
5451 shell. The current set of options may be found in @code{$-}.
5453 The remaining N @var{arguments} are positional parameters and are
5454 assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}.
5455 The special parameter @code{#} is set to N.
5457 The return status is always zero unless an invalid option is supplied.
5460 @node The Shopt Builtin
5461 @subsection The Shopt Builtin
5463 This builtin allows you to change additional shell optional behavior.
5470 shopt [-pqsu] [-o] [@var{optname} @dots{}]
5473 Toggle the values of settings controlling optional shell behavior.
5474 The settings can be either those listed below, or, if the
5475 @option{-o} option is used, those available with the @option{-o}
5476 option to the @code{set} builtin command (@pxref{The Set Builtin}).
5477 With no options, or with the @option{-p} option, a list of all settable
5478 options is displayed, with an indication of whether or not each is set;
5479 if @var{optname}s are supplied, the output is restricted to those options.
5480 The @option{-p} option causes output to be displayed in a form that
5481 may be reused as input.
5482 Other options have the following meanings:
5486 Enable (set) each @var{optname}.
5489 Disable (unset) each @var{optname}.
5492 Suppresses normal output; the return status
5493 indicates whether the @var{optname} is set or unset.
5494 If multiple @var{optname} arguments are given with @option{-q},
5495 the return status is zero if all @var{optname}s are enabled;
5499 Restricts the values of
5500 @var{optname} to be those defined for the @option{-o} option to the
5501 @code{set} builtin (@pxref{The Set Builtin}).
5504 If either @option{-s} or @option{-u}
5505 is used with no @var{optname} arguments, @code{shopt} shows only
5506 those options which are set or unset, respectively.
5508 Unless otherwise noted, the @code{shopt} options are disabled (off)
5511 The return status when listing options is zero if all @var{optname}s
5512 are enabled, non-zero otherwise. When setting or unsetting options,
5513 the return status is zero unless an @var{optname} is not a valid shell
5516 The list of @code{shopt} options is:
5519 @item assoc_expand_once
5520 If set, the shell suppresses multiple evaluation of associative array
5521 subscripts during arithmetic expression evaluation, while executing
5522 builtins that can perform variable assignments,
5523 and while executing builtins that perform array dereferencing.
5526 If set, a command name that is the name of a directory is executed as if
5527 it were the argument to the @code{cd} command.
5528 This option is only used by interactive shells.
5531 If this is set, an argument to the @code{cd} builtin command that
5532 is not a directory is assumed to be the name of a variable whose
5533 value is the directory to change to.
5536 If set, minor errors in the spelling of a directory component in a
5537 @code{cd} command will be corrected.
5538 The errors checked for are transposed characters,
5539 a missing character, and a character too many.
5540 If a correction is found, the corrected path is printed,
5541 and the command proceeds.
5542 This option is only used by interactive shells.
5545 If this is set, Bash checks that a command found in the hash
5546 table exists before trying to execute it. If a hashed command no
5547 longer exists, a normal path search is performed.
5550 If set, Bash lists the status of any stopped and running jobs before
5551 exiting an interactive shell. If any jobs are running, this causes
5552 the exit to be deferred until a second exit is attempted without an
5553 intervening command (@pxref{Job Control}).
5554 The shell always postpones exiting if any jobs are stopped.
5557 If set, Bash checks the window size after each external (non-builtin)
5558 command and, if necessary, updates the values of
5559 @env{LINES} and @env{COLUMNS}.
5560 This option is enabled by default.
5564 attempts to save all lines of a multiple-line
5565 command in the same history entry. This allows
5566 easy re-editing of multi-line commands.
5567 This option is enabled by default, but only has an effect if command
5568 history is enabled (@pxref{Bash History Facilities}).
5577 These control aspects of the shell's compatibility mode
5578 (@pxref{Shell Compatibility Mode}).
5580 @item complete_fullquote
5582 quotes all shell metacharacters in filenames and directory names when
5583 performing completion.
5585 removes metacharacters such as the dollar sign from the set of
5586 characters that will be quoted in completed filenames
5587 when these metacharacters appear in shell variable references in words to be
5589 This means that dollar signs in variable names that expand to directories
5591 however, any dollar signs appearing in filenames will not be quoted, either.
5592 This is active only when bash is using backslashes to quote completed
5594 This variable is set by default, which is the default Bash behavior in
5595 versions through 4.2.
5599 replaces directory names with the results of word expansion when performing
5600 filename completion. This changes the contents of the readline editing
5602 If not set, Bash attempts to preserve what the user typed.
5606 attempts spelling correction on directory names during word completion
5607 if the directory name initially supplied does not exist.
5610 If set, Bash includes filenames beginning with a `.' in
5611 the results of filename expansion.
5612 The filenames @samp{.} and @samp{..} must always be matched explicitly,
5613 even if @code{dotglob} is set.
5616 If this is set, a non-interactive shell will not exit if
5617 it cannot execute the file specified as an argument to the @code{exec}
5618 builtin command. An interactive shell does not exit if @code{exec}
5621 @item expand_aliases
5622 If set, aliases are expanded as described below under Aliases,
5624 This option is enabled by default for interactive shells.
5627 If set at shell invocation,
5628 or in a shell startup file,
5629 arrange to execute the debugger profile
5630 before the shell starts, identical to the @option{--debugger} option.
5631 If set after invocation, behavior intended for use by debuggers is enabled:
5635 The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins})
5636 displays the source file name and line number corresponding to each function
5637 name supplied as an argument.
5640 If the command run by the @code{DEBUG} trap returns a non-zero value, the
5641 next command is skipped and not executed.
5644 If the command run by the @code{DEBUG} trap returns a value of 2, and the
5645 shell is executing in a subroutine (a shell function or a shell script
5646 executed by the @code{.} or @code{source} builtins), the shell simulates
5647 a call to @code{return}.
5650 @code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their
5651 descriptions (@pxref{Bash Variables}).
5654 Function tracing is enabled: command substitution, shell functions, and
5655 subshells invoked with @code{( @var{command} )} inherit the
5656 @code{DEBUG} and @code{RETURN} traps.
5659 Error tracing is enabled: command substitution, shell functions, and
5660 subshells invoked with @code{( @var{command} )} inherit the
5665 If set, the extended pattern matching features described above
5666 (@pxref{Pattern Matching}) are enabled.
5669 If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is
5670 performed within @code{$@{@var{parameter}@}} expansions
5671 enclosed in double quotes. This option is enabled by default.
5674 If set, patterns which fail to match filenames during filename expansion
5675 result in an expansion error.
5678 If set, the suffixes specified by the @env{FIGNORE} shell variable
5679 cause words to be ignored when performing word completion even if
5680 the ignored words are the only possible completions.
5681 @xref{Bash Variables}, for a description of @env{FIGNORE}.
5682 This option is enabled by default.
5684 @item globasciiranges
5685 If set, range expressions used in pattern matching bracket expressions
5686 (@pxref{Pattern Matching})
5687 behave as if in the traditional C locale when performing
5688 comparisons. That is, the current locale's collating sequence
5689 is not taken into account, so
5690 @samp{b} will not collate between @samp{A} and @samp{B},
5691 and upper-case and lower-case ASCII characters will collate together.
5694 If set, the pattern @samp{**} used in a filename expansion context will
5695 match all files and zero or more directories and subdirectories.
5696 If the pattern is followed by a @samp{/}, only directories and
5697 subdirectories match.
5700 If set, shell error messages are written in the standard @sc{gnu} error
5704 If set, the history list is appended to the file named by the value
5705 of the @env{HISTFILE}
5706 variable when the shell exits, rather than overwriting the file.
5709 If set, and Readline
5710 is being used, a user is given the opportunity to re-edit a
5711 failed history substitution.
5714 If set, and Readline
5715 is being used, the results of history substitution are not immediately
5716 passed to the shell parser. Instead, the resulting line is loaded into
5717 the Readline editing buffer, allowing further modification.
5720 If set, and Readline is being used, Bash will attempt to perform
5721 hostname completion when a word containing a @samp{@@} is being
5722 completed (@pxref{Commands For Completion}). This option is enabled
5726 If set, Bash will send @code{SIGHUP} to all jobs when an interactive
5727 login shell exits (@pxref{Signals}).
5729 @item inherit_errexit
5730 If set, command substitution inherits the value of the @code{errexit} option,
5731 instead of unsetting it in the subshell environment.
5732 This option is enabled when @sc{posix} mode is enabled.
5734 @item interactive_comments
5735 Allow a word beginning with @samp{#}
5736 to cause that word and all remaining characters on that
5737 line to be ignored in an interactive shell.
5738 This option is enabled by default.
5741 If set, and job control is not active, the shell runs the last command of
5742 a pipeline not executed in the background in the current shell environment.
5745 If enabled, and the @code{cmdhist}
5746 option is enabled, multi-line commands are saved to the history with
5747 embedded newlines rather than using semicolon separators where possible.
5749 @item localvar_inherit
5750 If set, local variables inherit the value and attributes of a variable of
5751 the same name that exists at a previous scope before any new value is
5752 assigned. The @code{nameref} attribute is not inherited.
5754 @item localvar_unset
5755 If set, calling @code{unset} on local variables in previous function scopes
5756 marks them so subsequent lookups find them unset until that function
5757 returns. This is identical to the behavior of unsetting local variables
5758 at the current function scope.
5761 The shell sets this option if it is started as a login shell
5762 (@pxref{Invoking Bash}).
5763 The value may not be changed.
5766 If set, and a file that Bash is checking for mail has been
5767 accessed since the last time it was checked, the message
5768 @code{"The mail in @var{mailfile} has been read"} is displayed.
5770 @item no_empty_cmd_completion
5771 If set, and Readline is being used, Bash will not attempt to search
5772 the @env{PATH} for possible completions when completion is attempted
5776 If set, Bash matches filenames in a case-insensitive fashion when
5777 performing filename expansion.
5780 If set, Bash matches patterns in a case-insensitive fashion when
5781 performing matching while executing @code{case} or @code{[[}
5782 conditional commands (@pxref{Conditional Constructs},
5783 when performing pattern substitution word expansions,
5784 or when filtering possible completions as part of programmable completion.
5786 @item noexpand_translation
5788 encloses the translated results of $"..." quoting in single quotes
5789 instead of double quotes.
5790 If the string is not translated, this has no effect.
5793 If set, Bash allows filename patterns which match no
5794 files to expand to a null string, rather than themselves.
5796 @item patsub_replacement
5798 expands occurrences of @samp{&} in the replacement string of pattern
5799 substitution to the text matched by the pattern, as described
5800 above (@pxref{Shell Parameter Expansion}).
5801 This option is enabled by default.
5804 If set, the programmable completion facilities
5805 (@pxref{Programmable Completion}) are enabled.
5806 This option is enabled by default.
5808 @item progcomp_alias
5809 If set, and programmable completion is enabled, Bash treats a command
5810 name that doesn't have any completions as a possible alias and attempts
5811 alias expansion. If it has an alias, Bash attempts programmable
5812 completion using the command word resulting from the expanded alias.
5815 If set, prompt strings undergo
5816 parameter expansion, command substitution, arithmetic
5817 expansion, and quote removal after being expanded
5818 as described below (@pxref{Controlling the Prompt}).
5819 This option is enabled by default.
5821 @item restricted_shell
5822 The shell sets this option if it is started in restricted mode
5823 (@pxref{The Restricted Shell}).
5824 The value may not be changed.
5825 This is not reset when the startup files are executed, allowing
5826 the startup files to discover whether or not a shell is restricted.
5829 If this is set, the @code{shift}
5830 builtin prints an error message when the shift count exceeds the
5831 number of positional parameters.
5834 If set, the @code{.} (@code{source}) builtin uses the value of @env{PATH}
5835 to find the directory containing the file supplied as an argument.
5836 This option is enabled by default.
5838 @item varredir_close
5839 If set, the shell automatically closes file descriptors assigned using the
5840 @code{@{varname@}} redirection syntax (@pxref{Redirections}) instead of
5841 leaving them open when the command completes.
5844 If set, the @code{echo} builtin expands backslash-escape sequences
5850 @node Special Builtins
5851 @section Special Builtins
5852 @cindex special builtin
5854 For historical reasons, the @sc{posix} standard has classified
5855 several builtin commands as @emph{special}.
5856 When Bash is executing in @sc{posix} mode, the special builtins
5857 differ from other builtin commands in three respects:
5861 Special builtins are found before shell functions during command lookup.
5864 If a special builtin returns an error status, a non-interactive shell exits.
5867 Assignment statements preceding the command stay in effect in the shell
5868 environment after the command completes.
5871 When Bash is not executing in @sc{posix} mode, these builtins behave no
5872 differently than the rest of the Bash builtin commands.
5873 The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}.
5875 These are the @sc{posix} special builtins:
5877 @w{break : . continue eval exec exit export readonly return set}
5878 @w{shift trap unset}
5881 @node Shell Variables
5882 @chapter Shell Variables
5885 * Bourne Shell Variables:: Variables which Bash uses in the same way
5886 as the Bourne Shell.
5887 * Bash Variables:: List of variables that exist in Bash.
5890 This chapter describes the shell variables that Bash uses.
5891 Bash automatically assigns default values to a number of variables.
5893 @node Bourne Shell Variables
5894 @section Bourne Shell Variables
5896 Bash uses certain shell variables in the same way as the Bourne shell.
5897 In some cases, Bash assigns a default value to the variable.
5902 A colon-separated list of directories used as a search path for
5903 the @code{cd} builtin command.
5906 The current user's home directory; the default for the @code{cd} builtin
5908 The value of this variable is also used by tilde expansion
5909 (@pxref{Tilde Expansion}).
5912 A list of characters that separate fields; used when the shell splits
5913 words as part of expansion.
5916 If this parameter is set to a filename or directory name
5917 and the @env{MAILPATH} variable
5918 is not set, Bash informs the user of the arrival of mail in
5919 the specified file or Maildir-format directory.
5922 A colon-separated list of filenames which the shell periodically checks
5924 Each list entry can specify the message that is printed when new mail
5925 arrives in the mail file by separating the filename from the message with
5927 When used in the text of the message, @code{$_} expands to the name of
5928 the current mail file.
5931 The value of the last option argument processed by the @code{getopts} builtin.
5934 The index of the last option argument processed by the @code{getopts} builtin.
5937 A colon-separated list of directories in which the shell looks for
5939 A zero-length (null) directory name in the value of @code{PATH} indicates the
5941 A null directory name may appear as two adjacent colons, or as an initial
5945 The primary prompt string. The default value is @samp{\s-\v\$ }.
5946 @xref{Controlling the Prompt}, for the complete list of escape
5947 sequences that are expanded before @env{PS1} is displayed.
5950 The secondary prompt string. The default value is @samp{> }.
5951 @env{PS2} is expanded in the same way as @env{PS1} before being
5956 @node Bash Variables
5957 @section Bash Variables
5959 These variables are set or used by Bash, but other shells
5960 do not normally treat them specially.
5962 A few variables used by Bash are described in different chapters:
5963 variables for controlling the job control facilities
5964 (@pxref{Job Control Variables}).
5970 ($_, an underscore.)
5971 At shell startup, set to the pathname used to invoke the
5972 shell or shell script being executed as passed in the environment
5974 Subsequently, expands to the last argument to the previous simple
5975 command executed in the foreground, after expansion.
5976 Also set to the full pathname used to invoke each command executed
5977 and placed in the environment exported to that command.
5978 When checking mail, this parameter holds the name of the mail file.
5981 The full pathname used to execute the current instance of Bash.
5984 A colon-separated list of enabled shell options. Each word in
5985 the list is a valid argument for the @option{-s} option to the
5986 @code{shopt} builtin command (@pxref{The Shopt Builtin}).
5987 The options appearing in @env{BASHOPTS} are those reported
5988 as @samp{on} by @samp{shopt}.
5989 If this variable is in the environment when Bash
5990 starts up, each shell option in the list will be enabled before
5991 reading any startup files. This variable is readonly.
5994 Expands to the process ID of the current Bash process.
5995 This differs from @code{$$} under certain circumstances, such as subshells
5996 that do not require Bash to be re-initialized.
5997 Assignments to @env{BASHPID} have no effect.
5999 is unset, it loses its special properties, even if it is
6003 An associative array variable whose members correspond to the internal
6004 list of aliases as maintained by the @code{alias} builtin.
6005 (@pxref{Bourne Shell Builtins}).
6006 Elements added to this array appear in the alias list; however,
6007 unsetting array elements currently does not cause aliases to be removed
6008 from the alias list.
6009 If @env{BASH_ALIASES}
6010 is unset, it loses its special properties, even if it is
6014 An array variable whose values are the number of parameters in each
6015 frame of the current bash execution call stack. The number of
6016 parameters to the current subroutine (shell function or script executed
6017 with @code{.} or @code{source}) is at the top of the stack. When a
6018 subroutine is executed, the number of parameters passed is pushed onto
6020 The shell sets @code{BASH_ARGC} only when in extended debugging mode
6021 (see @ref{The Shopt Builtin}
6022 for a description of the @code{extdebug} option to the @code{shopt}
6024 Setting @code{extdebug} after the shell has started to execute a script,
6025 or referencing this variable when @code{extdebug} is not set,
6026 may result in inconsistent values.
6029 An array variable containing all of the parameters in the current bash
6030 execution call stack. The final parameter of the last subroutine call
6031 is at the top of the stack; the first parameter of the initial call is
6032 at the bottom. When a subroutine is executed, the parameters supplied
6033 are pushed onto @code{BASH_ARGV}.
6034 The shell sets @code{BASH_ARGV} only when in extended debugging mode
6035 (see @ref{The Shopt Builtin}
6036 for a description of the @code{extdebug} option to the @code{shopt}
6038 Setting @code{extdebug} after the shell has started to execute a script,
6039 or referencing this variable when @code{extdebug} is not set,
6040 may result in inconsistent values.
6043 When referenced, this variable expands to the name of the shell or shell
6044 script (identical to @code{$0}; @xref{Special Parameters},
6045 for the description of special parameter 0).
6046 Assignment to @code{BASH_ARGV0}
6047 causes the value assigned to also be assigned to @code{$0}.
6049 is unset, it loses its special properties, even if it is
6053 An associative array variable whose members correspond to the internal
6054 hash table of commands as maintained by the @code{hash} builtin
6055 (@pxref{Bourne Shell Builtins}).
6056 Elements added to this array appear in the hash table; however,
6057 unsetting array elements currently does not cause command names to be removed
6058 from the hash table.
6060 is unset, it loses its special properties, even if it is
6064 The command currently being executed or about to be executed, unless the
6065 shell is executing a command as the result of a trap,
6066 in which case it is the command executing at the time of the trap.
6067 If @env{BASH_COMMAND}
6068 is unset, it loses its special properties, even if it is
6072 The value is used to set the shell's compatibility level.
6073 @xref{Shell Compatibility Mode}, for a description of the various
6074 compatibility levels and their effects.
6075 The value may be a decimal number (e.g., 4.2) or an integer (e.g., 42)
6076 corresponding to the desired compatibility level.
6077 If @env{BASH_COMPAT} is unset or set to the empty string, the compatibility
6078 level is set to the default for the current version.
6079 If @env{BASH_COMPAT} is set to a value that is not one of the valid
6080 compatibility levels, the shell prints an error message and sets the
6081 compatibility level to the default for the current version.
6082 The valid values correspond to the compatibility levels
6083 described below (@pxref{Shell Compatibility Mode}).
6084 For example, 4.2 and 42 are valid values that correspond
6085 to the @code{compat42} @code{shopt} option
6086 and set the compatibility level to 42.
6087 The current version is also a valid value.
6090 If this variable is set when Bash is invoked to execute a shell
6091 script, its value is expanded and used as the name of a startup file
6092 to read before executing the script. @xref{Bash Startup Files}.
6094 @item BASH_EXECUTION_STRING
6095 The command argument to the @option{-c} invocation option.
6098 An array variable whose members are the line numbers in source files
6099 where each corresponding member of @env{FUNCNAME} was invoked.
6100 @code{$@{BASH_LINENO[$i]@}} is the line number in the source file
6101 (@code{$@{BASH_SOURCE[$i+1]@}}) where
6102 @code{$@{FUNCNAME[$i]@}} was called (or @code{$@{BASH_LINENO[$i-1]@}} if
6103 referenced within another shell function).
6104 Use @code{LINENO} to obtain the current line number.
6106 @item BASH_LOADABLES_PATH
6107 A colon-separated list of directories in which the shell looks for
6108 dynamically loadable builtins specified by the
6109 @code{enable} command.
6112 An array variable whose members are assigned by the @samp{=~} binary
6113 operator to the @code{[[} conditional command
6114 (@pxref{Conditional Constructs}).
6115 The element with index 0 is the portion of the string
6116 matching the entire regular expression.
6117 The element with index @var{n} is the portion of the
6118 string matching the @var{n}th parenthesized subexpression.
6121 An array variable whose members are the source filenames where the
6122 corresponding shell function names in the @code{FUNCNAME} array
6123 variable are defined.
6124 The shell function @code{$@{FUNCNAME[$i]@}} is defined in the file
6125 @code{$@{BASH_SOURCE[$i]@}} and called from @code{$@{BASH_SOURCE[$i+1]@}}
6128 Incremented by one within each subshell or subshell environment when
6129 the shell begins executing in that environment.
6130 The initial value is 0.
6131 If @env{BASH_SUBSHELL}
6132 is unset, it loses its special properties, even if it is
6136 A readonly array variable (@pxref{Arrays})
6137 whose members hold version information for this instance of Bash.
6138 The values assigned to the array members are as follows:
6142 @item BASH_VERSINFO[0]
6143 The major version number (the @dfn{release}).
6145 @item BASH_VERSINFO[1]
6146 The minor version number (the @dfn{version}).
6148 @item BASH_VERSINFO[2]
6151 @item BASH_VERSINFO[3]
6154 @item BASH_VERSINFO[4]
6155 The release status (e.g., @code{beta1}).
6157 @item BASH_VERSINFO[5]
6158 The value of @env{MACHTYPE}.
6162 The version number of the current instance of Bash.
6165 If set to an integer corresponding to a valid file descriptor, Bash
6166 will write the trace output generated when @samp{set -x}
6167 is enabled to that file descriptor.
6168 This allows tracing output to be separated from diagnostic and error
6170 The file descriptor is closed when @code{BASH_XTRACEFD} is unset or assigned
6172 Unsetting @code{BASH_XTRACEFD} or assigning it the empty string causes the
6173 trace output to be sent to the standard error.
6174 Note that setting @code{BASH_XTRACEFD} to 2 (the standard error file
6175 descriptor) and then unsetting it will result in the standard error
6179 Set the number of exited child status values for the shell to remember.
6180 Bash will not allow this value to be decreased below a @sc{posix}-mandated
6181 minimum, and there is a maximum value (currently 8192) that this may
6183 The minimum value is system-dependent.
6186 Used by the @code{select} command to determine the terminal width
6187 when printing selection lists.
6188 Automatically set if the @code{checkwinsize} option is enabled
6189 (@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
6193 An index into @env{$@{COMP_WORDS@}} of the word containing the current
6195 This variable is available only in shell functions invoked by the
6196 programmable completion facilities (@pxref{Programmable Completion}).
6199 The current command line.
6200 This variable is available only in shell functions and external
6201 commands invoked by the
6202 programmable completion facilities (@pxref{Programmable Completion}).
6205 The index of the current cursor position relative to the beginning of
6206 the current command.
6207 If the current cursor position is at the end of the current command,
6208 the value of this variable is equal to @code{$@{#COMP_LINE@}}.
6209 This variable is available only in shell functions and external
6210 commands invoked by the
6211 programmable completion facilities (@pxref{Programmable Completion}).
6214 Set to an integer value corresponding to the type of completion attempted
6215 that caused a completion function to be called:
6216 @key{TAB}, for normal completion,
6217 @samp{?}, for listing completions after successive tabs,
6218 @samp{!}, for listing alternatives on partial word completion,
6219 @samp{@@}, to list completions if the word is not unmodified,
6221 @samp{%}, for menu completion.
6222 This variable is available only in shell functions and external
6223 commands invoked by the
6224 programmable completion facilities (@pxref{Programmable Completion}).
6227 The key (or final key of a key sequence) used to invoke the current
6228 completion function.
6230 @item COMP_WORDBREAKS
6231 The set of characters that the Readline library treats as word
6232 separators when performing word completion.
6233 If @env{COMP_WORDBREAKS}
6234 is unset, it loses its special properties,
6235 even if it is subsequently reset.
6238 An array variable consisting of the individual
6239 words in the current command line.
6240 The line is split into words as Readline would split it, using
6241 @code{COMP_WORDBREAKS} as described above.
6242 This variable is available only in shell functions invoked by the
6243 programmable completion facilities (@pxref{Programmable Completion}).
6246 An array variable from which Bash reads the possible completions
6247 generated by a shell function invoked by the programmable completion
6248 facility (@pxref{Programmable Completion}).
6249 Each array element contains one possible completion.
6252 An array variable created to hold the file descriptors
6253 for output from and input to an unnamed coprocess (@pxref{Coprocesses}).
6256 An array variable containing the current contents of the directory stack.
6257 Directories appear in the stack in the order they are displayed by the
6258 @code{dirs} builtin.
6259 Assigning to members of this array variable may be used to modify
6260 directories already in the stack, but the @code{pushd} and @code{popd}
6261 builtins must be used to add and remove directories.
6262 Assignment to this variable will not change the current directory.
6264 is unset, it loses its special properties, even if
6265 it is subsequently reset.
6268 If Bash finds this variable in the environment when the shell
6269 starts with value @samp{t}, it assumes that the shell is running in an
6270 Emacs shell buffer and disables line editing.
6273 Expanded and executed similarlty to @code{BASH_ENV}
6274 (@pxref{Bash Startup Files})
6275 when an interactive shell is invoked in
6276 @sc{posix} Mode (@pxref{Bash POSIX Mode}).
6279 Each time this parameter is referenced, it expands to the number of seconds
6280 since the Unix Epoch as a floating point value with micro-second granularity
6281 (see the documentation for the C library function @code{time} for the
6282 definition of Epoch).
6283 Assignments to @env{EPOCHREALTIME} are ignored.
6284 If @env{EPOCHREALTIME}
6285 is unset, it loses its special properties, even if
6286 it is subsequently reset.
6289 Each time this parameter is referenced, it expands to the number of seconds
6290 since the Unix Epoch (see the documentation for the C library function
6291 @code{time} for the definition of Epoch).
6292 Assignments to @env{EPOCHSECONDS} are ignored.
6293 If @env{EPOCHSECONDS}
6294 is unset, it loses its special properties, even if
6295 it is subsequently reset.
6298 The numeric effective user id of the current user. This variable
6302 A colon-separated list of shell patterns (@pxref{Pattern Matching})
6303 defining the list of filenames to be ignored by command search using
6305 Files whose full pathnames match one of these patterns are not considered
6306 executable files for the purposes of completion and command execution
6307 via @code{PATH} lookup.
6308 This does not affect the behavior of the @code{[}, @code{test}, and @code{[[}
6310 Full pathnames in the command hash table are not subject to @code{EXECIGNORE}.
6311 Use this variable to ignore shared library files that have the executable
6312 bit set, but are not executable files.
6313 The pattern matching honors the setting of the @code{extglob} shell
6317 The editor used as a default by the @option{-e} option to the @code{fc}
6321 A colon-separated list of suffixes to ignore when performing
6322 filename completion.
6323 A filename whose suffix matches one of the entries in
6325 is excluded from the list of matched filenames. A sample
6326 value is @samp{.o:~}
6329 An array variable containing the names of all shell functions
6330 currently in the execution call stack.
6331 The element with index 0 is the name of any currently-executing
6333 The bottom-most element (the one with the highest index)
6335 This variable exists only when a shell function is executing.
6336 Assignments to @env{FUNCNAME} have no effect.
6338 is unset, it loses its special properties, even if
6339 it is subsequently reset.
6341 This variable can be used with @code{BASH_LINENO} and @code{BASH_SOURCE}.
6342 Each element of @code{FUNCNAME} has corresponding elements in
6343 @code{BASH_LINENO} and @code{BASH_SOURCE} to describe the call stack.
6344 For instance, @code{$@{FUNCNAME[$i]@}} was called from the file
6345 @code{$@{BASH_SOURCE[$i+1]@}} at line number @code{$@{BASH_LINENO[$i]@}}.
6346 The @code{caller} builtin displays the current call stack using this
6350 If set to a numeric value greater than 0, defines a maximum function
6351 nesting level. Function invocations that exceed this nesting level
6352 will cause the current command to abort.
6355 A colon-separated list of patterns defining the set of file names to
6356 be ignored by filename expansion.
6357 If a file name matched by a filename expansion pattern also matches one
6358 of the patterns in @env{GLOBIGNORE}, it is removed from the list
6360 The pattern matching honors the setting of the @code{extglob} shell
6364 An array variable containing the list of groups of which the current
6366 Assignments to @env{GROUPS} have no effect.
6368 is unset, it loses its special properties, even if it is
6372 Up to three characters which control history expansion, quick
6373 substitution, and tokenization (@pxref{History Interaction}).
6374 The first character is the
6375 @dfn{history expansion} character, that is, the character which signifies the
6376 start of a history expansion, normally @samp{!}. The second character is the
6377 character which signifies `quick substitution' when seen as the first
6378 character on a line, normally @samp{^}. The optional third character is the
6379 character which indicates that the remainder of the line is a comment when
6380 found as the first character of a word, usually @samp{#}. The history
6381 comment character causes history substitution to be skipped for the
6382 remaining words on the line. It does not necessarily cause the shell
6383 parser to treat the rest of the line as a comment.
6386 The history number, or index in the history list, of the current
6388 Assignments to @env{HISTCMD} are ignored.
6390 is unset, it loses its special properties,
6391 even if it is subsequently reset.
6394 A colon-separated list of values controlling how commands are saved on
6396 If the list of values includes @samp{ignorespace}, lines which begin
6397 with a space character are not saved in the history list.
6398 A value of @samp{ignoredups} causes lines which match the previous
6399 history entry to not be saved.
6400 A value of @samp{ignoreboth} is shorthand for
6401 @samp{ignorespace} and @samp{ignoredups}.
6402 A value of @samp{erasedups} causes all previous lines matching the
6403 current line to be removed from the history list before that line
6405 Any value not in the above list is ignored.
6406 If @env{HISTCONTROL} is unset, or does not include a valid value,
6407 all lines read by the shell parser are saved on the history list,
6408 subject to the value of @env{HISTIGNORE}.
6409 The second and subsequent lines of a multi-line compound command are
6410 not tested, and are added to the history regardless of the value of
6414 The name of the file to which the command history is saved. The
6415 default value is @file{~/.bash_history}.
6418 The maximum number of lines contained in the history file.
6419 When this variable is assigned a value, the history file is truncated,
6420 if necessary, to contain no more than that number of lines
6421 by removing the oldest entries.
6422 The history file is also truncated to this size after
6423 writing it when a shell exits.
6424 If the value is 0, the history file is truncated to zero size.
6425 Non-numeric values and numeric values less than zero inhibit truncation.
6426 The shell sets the default value to the value of @env{HISTSIZE}
6427 after reading any startup files.
6430 A colon-separated list of patterns used to decide which command
6431 lines should be saved on the history list. Each pattern is
6432 anchored at the beginning of the line and must match the complete
6433 line (no implicit @samp{*} is appended). Each pattern is tested
6434 against the line after the checks specified by @env{HISTCONTROL}
6435 are applied. In addition to the normal shell pattern matching
6436 characters, @samp{&} matches the previous history line. @samp{&}
6437 may be escaped using a backslash; the backslash is removed
6438 before attempting a match.
6439 The second and subsequent lines of a multi-line compound command are
6440 not tested, and are added to the history regardless of the value of
6442 The pattern matching honors the setting of the @code{extglob} shell
6445 @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A
6446 pattern of @samp{&} is identical to @code{ignoredups}, and a
6447 pattern of @samp{[ ]*} is identical to @code{ignorespace}.
6448 Combining these two patterns, separating them with a colon,
6449 provides the functionality of @code{ignoreboth}.
6452 The maximum number of commands to remember on the history list.
6453 If the value is 0, commands are not saved in the history list.
6454 Numeric values less than zero result in every command being saved
6455 on the history list (there is no limit).
6456 The shell sets the default value to 500 after reading any startup files.
6458 @item HISTTIMEFORMAT
6459 If this variable is set and not null, its value is used as a format string
6460 for @code{strftime} to print the time stamp associated with each history
6461 entry displayed by the @code{history} builtin.
6462 If this variable is set, time stamps are written to the history file so
6463 they may be preserved across shell sessions.
6464 This uses the history comment character to distinguish timestamps from
6465 other history lines.
6468 Contains the name of a file in the same format as @file{/etc/hosts} that
6469 should be read when the shell needs to complete a hostname.
6470 The list of possible hostname completions may be changed while the shell
6472 the next time hostname completion is attempted after the
6473 value is changed, Bash adds the contents of the new file to the
6475 If @env{HOSTFILE} is set, but has no value, or does not name a readable file,
6476 Bash attempts to read
6477 @file{/etc/hosts} to obtain the list of possible hostname completions.
6478 When @env{HOSTFILE} is unset, the hostname list is cleared.
6481 The name of the current host.
6484 A string describing the machine Bash is running on.
6487 Controls the action of the shell on receipt of an @code{EOF} character
6488 as the sole input. If set, the value denotes the number
6489 of consecutive @code{EOF} characters that can be read as the
6490 first character on an input line
6491 before the shell will exit. If the variable exists but does not
6492 have a numeric value, or has no value, then the default is 10.
6493 If the variable does not exist, then @code{EOF} signifies the end of
6494 input to the shell. This is only in effect for interactive shells.
6497 The name of the Readline initialization file, overriding the default
6498 of @file{~/.inputrc}.
6501 If Bash finds this variable in the environment when the shell
6502 starts, it assumes that the shell is running in an Emacs shell buffer
6503 and may disable line editing depending on the value of @env{TERM}.
6506 Used to determine the locale category for any category not specifically
6507 selected with a variable starting with @code{LC_}.
6510 This variable overrides the value of @env{LANG} and any other
6511 @code{LC_} variable specifying a locale category.
6514 This variable determines the collation order used when sorting the
6515 results of filename expansion, and
6516 determines the behavior of range expressions, equivalence classes,
6517 and collating sequences within filename expansion and pattern matching
6518 (@pxref{Filename Expansion}).
6521 This variable determines the interpretation of characters and the
6522 behavior of character classes within filename expansion and pattern
6523 matching (@pxref{Filename Expansion}).
6526 This variable determines the locale used to translate double-quoted
6527 strings preceded by a @samp{$} (@pxref{Locale Translation}).
6530 This variable determines the locale category used for number formatting.
6533 This variable determines the locale category used for data and time
6537 The line number in the script or shell function currently executing.
6539 is unset, it loses its special properties, even if it is
6543 Used by the @code{select} command to determine the column length
6544 for printing selection lists.
6545 Automatically set if the @code{checkwinsize} option is enabled
6546 (@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
6550 A string that fully describes the system type on which Bash
6551 is executing, in the standard @sc{gnu} @var{cpu-company-system} format.
6554 How often (in seconds) that the shell should check for mail in the
6555 files specified in the @env{MAILPATH} or @env{MAIL} variables.
6556 The default is 60 seconds. When it is time to check
6557 for mail, the shell does so before displaying the primary prompt.
6558 If this variable is unset, or set to a value that is not a number
6559 greater than or equal to zero, the shell disables mail checking.
6562 An array variable created to hold the text read by the
6563 @code{mapfile} builtin when no variable name is supplied.
6566 The previous working directory as set by the @code{cd} builtin.
6569 If set to the value 1, Bash displays error messages
6570 generated by the @code{getopts} builtin command.
6573 A string describing the operating system Bash is running on.
6576 An array variable (@pxref{Arrays})
6577 containing a list of exit status values from the processes
6578 in the most-recently-executed foreground pipeline (which may
6579 contain only a single command).
6581 @item POSIXLY_CORRECT
6582 If this variable is in the environment when Bash starts, the shell
6583 enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the
6584 startup files, as if the @option{--posix} invocation option had been supplied.
6585 If it is set while the shell is running, Bash enables @sc{posix} mode,
6592 When the shell enters @sc{posix} mode, it sets this variable if it was
6596 The process @sc{id} of the shell's parent process. This variable
6599 @item PROMPT_COMMAND
6600 If this variable is set, and is an array,
6601 the value of each set element is interpreted as a command to execute
6602 before printing the primary prompt (@env{$PS1}).
6603 If this is set but not an array variable,
6604 its value is used as a command to execute instead.
6606 @item PROMPT_DIRTRIM
6607 If set to a number greater than zero, the value is used as the number of
6608 trailing directory components to retain when expanding the @code{\w} and
6609 @code{\W} prompt string escapes (@pxref{Controlling the Prompt}).
6610 Characters removed are replaced with an ellipsis.
6613 The value of this parameter is expanded like @env{PS1}
6614 and displayed by interactive shells after reading a command
6615 and before the command is executed.
6618 The value of this variable is used as the prompt for the
6619 @code{select} command. If this variable is not set, the
6620 @code{select} command prompts with @samp{#? }
6623 The value of this parameter is expanded like @env{PS1}
6624 and the expanded value is the prompt printed before the command line
6625 is echoed when the @option{-x} option is set (@pxref{The Set Builtin}).
6626 The first character of the expanded value is replicated multiple times,
6627 as necessary, to indicate multiple levels of indirection.
6628 The default is @samp{+ }.
6631 The current working directory as set by the @code{cd} builtin.
6634 Each time this parameter is referenced, it expands to a random integer
6635 between 0 and 32767. Assigning a value to this
6636 variable seeds the random number generator.
6638 is unset, it loses its special properties, even if it is
6641 @item READLINE_ARGUMENT
6642 Any numeric argument given to a Readline command that was defined using
6643 @samp{bind -x} (@pxref{Bash Builtins}
6644 when it was invoked.
6647 The contents of the Readline line buffer, for use
6648 with @samp{bind -x} (@pxref{Bash Builtins}).
6651 The position of the @dfn{mark} (saved insertion point) in the
6652 Readline line buffer, for use
6653 with @samp{bind -x} (@pxref{Bash Builtins}).
6654 The characters between the insertion point and the mark are often
6655 called the @dfn{region}.
6657 @item READLINE_POINT
6658 The position of the insertion point in the Readline line buffer, for use
6659 with @samp{bind -x} (@pxref{Bash Builtins}).
6662 The default variable for the @code{read} builtin.
6665 This variable expands to the number of seconds since the shell was started.
6666 Assignment to this variable resets the count to the value assigned, and the
6667 expanded value becomes the value assigned plus the number of seconds
6668 since the assignment.
6669 The number of seconds at shell invocation and the current time are always
6670 determined by querying the system clock.
6672 is unset, it loses its special properties,
6673 even if it is subsequently reset.
6676 This environment variable expands to the full pathname to the shell.
6677 If it is not set when the shell starts,
6678 Bash assigns to it the full pathname of the current user's login shell.
6681 A colon-separated list of enabled shell options. Each word in
6682 the list is a valid argument for the @option{-o} option to the
6683 @code{set} builtin command (@pxref{The Set Builtin}).
6684 The options appearing in @env{SHELLOPTS} are those reported
6685 as @samp{on} by @samp{set -o}.
6686 If this variable is in the environment when Bash
6687 starts up, each shell option in the list will be enabled before
6688 reading any startup files. This variable is readonly.
6691 Incremented by one each time a new instance of Bash is started. This is
6692 intended to be a count of how deeply your Bash shells are nested.
6695 This variable expands to a 32-bit pseudo-random number each time it is
6696 referenced. The random number generator is not linear on systems that
6697 support @file{/dev/urandom} or @code{arc4random}, so each returned number
6698 has no relationship to the numbers preceding it.
6699 The random number generator cannot be seeded, so assignments to this
6700 variable have no effect.
6702 is unset, it loses its special properties,
6703 even if it is subsequently reset.
6706 The value of this parameter is used as a format string specifying
6707 how the timing information for pipelines prefixed with the @code{time}
6708 reserved word should be displayed.
6709 The @samp{%} character introduces an
6710 escape sequence that is expanded to a time value or other
6712 The escape sequences and their meanings are as
6713 follows; the braces denote optional portions.
6720 @item %[@var{p}][l]R
6721 The elapsed time in seconds.
6723 @item %[@var{p}][l]U
6724 The number of CPU seconds spent in user mode.
6726 @item %[@var{p}][l]S
6727 The number of CPU seconds spent in system mode.
6730 The CPU percentage, computed as (%U + %S) / %R.
6733 The optional @var{p} is a digit specifying the precision, the number of
6734 fractional digits after a decimal point.
6735 A value of 0 causes no decimal point or fraction to be output.
6736 At most three places after the decimal point may be specified; values
6737 of @var{p} greater than 3 are changed to 3.
6738 If @var{p} is not specified, the value 3 is used.
6740 The optional @code{l} specifies a longer format, including minutes, of
6741 the form @var{MM}m@var{SS}.@var{FF}s.
6742 The value of @var{p} determines whether or not the fraction is included.
6744 If this variable is not set, Bash acts as if it had the value
6746 @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'}
6748 If the value is null, no timing information is displayed.
6749 A trailing newline is added when the format string is displayed.
6752 If set to a value greater than zero, @code{TMOUT} is treated as the
6753 default timeout for the @code{read} builtin (@pxref{Bash Builtins}).
6754 The @code{select} command (@pxref{Conditional Constructs}) terminates
6755 if input does not arrive after @code{TMOUT} seconds when input is coming
6758 In an interactive shell, the value is interpreted as
6759 the number of seconds to wait for a line of input after issuing
6762 terminates after waiting for that number of seconds if a complete
6763 line of input does not arrive.
6766 If set, Bash uses its value as the name of a directory in which
6767 Bash creates temporary files for the shell's use.
6770 The numeric real user id of the current user. This variable is readonly.
6775 @chapter Bash Features
6777 This chapter describes features unique to Bash.
6780 * Invoking Bash:: Command line options that you can give
6782 * Bash Startup Files:: When and how Bash executes scripts.
6783 * Interactive Shells:: What an interactive shell is.
6784 * Bash Conditional Expressions:: Primitives used in composing expressions for
6785 the @code{test} builtin.
6786 * Shell Arithmetic:: Arithmetic on shell variables.
6787 * Aliases:: Substituting one command for another.
6788 * Arrays:: Array Variables.
6789 * The Directory Stack:: History of visited directories.
6790 * Controlling the Prompt:: Customizing the various prompt strings.
6791 * The Restricted Shell:: A more controlled mode of shell execution.
6792 * Bash POSIX Mode:: Making Bash behave more closely to what
6793 the POSIX standard specifies.
6794 * Shell Compatibility Mode:: How Bash supports behavior that was present
6795 in earlier versions and has changed.
6799 @section Invoking Bash
6802 bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6803 [-O @var{shopt_option}] [@var{argument} @dots{}]
6804 bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6805 [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}]
6806 bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6807 [-O @var{shopt_option}] [@var{argument} @dots{}]
6810 All of the single-character options used with the @code{set} builtin
6811 (@pxref{The Set Builtin}) can be used as options when the shell is invoked.
6812 In addition, there are several multi-character
6813 options that you can use. These options must appear on the command
6814 line before the single-character options to be recognized.
6818 Arrange for the debugger profile to be executed before the shell
6819 starts. Turns on extended debugging mode (see @ref{The Shopt Builtin}
6820 for a description of the @code{extdebug} option to the @code{shopt}
6823 @item --dump-po-strings
6824 A list of all double-quoted strings preceded by @samp{$}
6825 is printed on the standard output
6826 in the @sc{gnu} @code{gettext} PO (portable object) file format.
6827 Equivalent to @option{-D} except for the output format.
6829 @item --dump-strings
6830 Equivalent to @option{-D}.
6833 Display a usage message on standard output and exit successfully.
6835 @item --init-file @var{filename}
6836 @itemx --rcfile @var{filename}
6837 Execute commands from @var{filename} (instead of @file{~/.bashrc})
6838 in an interactive shell.
6841 Equivalent to @option{-l}.
6844 Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing})
6845 to read command lines when the shell is interactive.
6848 Don't load the system-wide startup file @file{/etc/profile}
6849 or any of the personal initialization files
6850 @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile}
6851 when Bash is invoked as a login shell.
6854 Don't read the @file{~/.bashrc} initialization file in an
6855 interactive shell. This is on by default if the shell is
6856 invoked as @code{sh}.
6859 Change the behavior of Bash where the default operation differs
6860 from the @sc{posix} standard to match the standard. This
6861 is intended to make Bash behave as a strict superset of that
6862 standard. @xref{Bash POSIX Mode}, for a description of the Bash
6866 Make the shell a restricted shell (@pxref{The Restricted Shell}).
6869 Equivalent to @option{-v}. Print shell input lines as they're read.
6872 Show version information for this instance of
6873 Bash on the standard output and exit successfully.
6876 There are several single-character options that may be supplied at
6877 invocation which are not available with the @code{set} builtin.
6881 Read and execute commands from the first non-option argument
6882 @var{command_string}, then exit.
6883 If there are arguments after the @var{command_string},
6884 the first argument is assigned to @code{$0}
6885 and any remaining arguments are assigned to the positional parameters.
6886 The assignment to @code{$0} sets the name of the shell, which is used
6887 in warning and error messages.
6890 Force the shell to run interactively. Interactive shells are
6891 described in @ref{Interactive Shells}.
6894 Make this shell act as if it had been directly invoked by login.
6895 When the shell is interactive, this is equivalent to starting a
6896 login shell with @samp{exec -l bash}.
6897 When the shell is not interactive, the login shell startup files will
6899 @samp{exec bash -l} or @samp{exec bash --login}
6900 will replace the current shell with a Bash login shell.
6901 @xref{Bash Startup Files}, for a description of the special behavior
6905 Make the shell a restricted shell (@pxref{The Restricted Shell}).
6908 If this option is present, or if no arguments remain after option
6909 processing, then commands are read from the standard input.
6910 This option allows the positional parameters to be set
6911 when invoking an interactive shell or when reading input
6915 A list of all double-quoted strings preceded by @samp{$}
6916 is printed on the standard output.
6917 These are the strings that
6918 are subject to language translation when the current locale
6919 is not @code{C} or @code{POSIX} (@pxref{Locale Translation}).
6920 This implies the @option{-n} option; no commands will be executed.
6922 @item [-+]O [@var{shopt_option}]
6923 @var{shopt_option} is one of the shell options accepted by the
6924 @code{shopt} builtin (@pxref{The Shopt Builtin}).
6925 If @var{shopt_option} is present, @option{-O} sets the value of that option;
6926 @option{+O} unsets it.
6927 If @var{shopt_option} is not supplied, the names and values of the shell
6928 options accepted by @code{shopt} are printed on the standard output.
6929 If the invocation option is @option{+O}, the output is displayed in a format
6930 that may be reused as input.
6933 A @code{--} signals the end of options and disables further option
6935 Any arguments after the @code{--} are treated as filenames and arguments.
6939 A @emph{login} shell is one whose first character of argument zero is
6940 @samp{-}, or one invoked with the @option{--login} option.
6942 @cindex interactive shell
6943 An @emph{interactive} shell is one started without non-option arguments,
6944 unless @option{-s} is specified,
6945 without specifying the @option{-c} option, and whose input and output are both
6946 connected to terminals (as determined by @code{isatty(3)}), or one
6947 started with the @option{-i} option. @xref{Interactive Shells}, for more
6950 If arguments remain after option processing, and neither the
6951 @option{-c} nor the @option{-s}
6952 option has been supplied, the first argument is assumed to
6953 be the name of a file containing shell commands (@pxref{Shell Scripts}).
6954 When Bash is invoked in this fashion, @code{$0}
6955 is set to the name of the file, and the positional parameters
6956 are set to the remaining arguments.
6957 Bash reads and executes commands from this file, then exits.
6958 Bash's exit status is the exit status of the last command executed
6959 in the script. If no commands are executed, the exit status is 0.
6961 @node Bash Startup Files
6962 @section Bash Startup Files
6963 @cindex startup files
6965 This section describes how Bash executes its startup files.
6966 If any of the files exist but cannot be read, Bash reports an error.
6967 Tildes are expanded in filenames as described above under
6968 Tilde Expansion (@pxref{Tilde Expansion}).
6970 Interactive shells are described in @ref{Interactive Shells}.
6972 @subsubheading Invoked as an interactive login shell, or with @option{--login}
6974 When Bash is invoked as an interactive login shell, or as a
6975 non-interactive shell with the @option{--login} option, it first reads and
6976 executes commands from the file @file{/etc/profile}, if that file exists.
6977 After reading that file, it looks for @file{~/.bash_profile},
6978 @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads
6979 and executes commands from the first one that exists and is readable.
6980 The @option{--noprofile} option may be used when the shell is started to
6981 inhibit this behavior.
6983 When an interactive login shell exits,
6984 or a non-interactive login shell executes the @code{exit} builtin command,
6985 Bash reads and executes commands from
6986 the file @file{~/.bash_logout}, if it exists.
6988 @subsubheading Invoked as an interactive non-login shell
6990 When an interactive shell that is not a login shell is started, Bash
6991 reads and executes commands from @file{~/.bashrc}, if that file exists.
6992 This may be inhibited by using the @option{--norc} option.
6993 The @option{--rcfile @var{file}} option will force Bash to read and
6994 execute commands from @var{file} instead of @file{~/.bashrc}.
6996 So, typically, your @file{~/.bash_profile} contains the line
6998 @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi}
7001 after (or before) any login-specific initializations.
7003 @subsubheading Invoked non-interactively
7005 When Bash is started non-interactively, to run a shell script,
7006 for example, it looks for the variable @env{BASH_ENV} in the environment,
7007 expands its value if it appears there, and uses the expanded value as
7008 the name of a file to read and execute. Bash behaves as if the
7009 following command were executed:
7011 @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi}
7014 but the value of the @env{PATH} variable is not used to search for the
7017 As noted above, if a non-interactive shell is invoked with the
7018 @option{--login} option, Bash attempts to read and execute commands from the
7019 login shell startup files.
7021 @subsubheading Invoked with name @code{sh}
7023 If Bash is invoked with the name @code{sh}, it tries to mimic the
7024 startup behavior of historical versions of @code{sh} as closely as
7025 possible, while conforming to the @sc{posix} standard as well.
7027 When invoked as an interactive login shell, or as a non-interactive
7028 shell with the @option{--login} option, it first attempts to read
7029 and execute commands from @file{/etc/profile} and @file{~/.profile}, in
7031 The @option{--noprofile} option may be used to inhibit this behavior.
7032 When invoked as an interactive shell with the name @code{sh}, Bash
7033 looks for the variable @env{ENV}, expands its value if it is defined,
7034 and uses the expanded value as the name of a file to read and execute.
7035 Since a shell invoked as @code{sh} does not attempt to read and execute
7036 commands from any other startup files, the @option{--rcfile} option has
7038 A non-interactive shell invoked with the name @code{sh} does not attempt
7039 to read any other startup files.
7041 When invoked as @code{sh}, Bash enters @sc{posix} mode after
7042 the startup files are read.
7044 @subsubheading Invoked in @sc{posix} mode
7046 When Bash is started in @sc{posix} mode, as with the
7047 @option{--posix} command line option, it follows the @sc{posix} standard
7049 In this mode, interactive shells expand the @env{ENV} variable
7050 and commands are read and executed from the file whose name is the
7052 No other startup files are read.
7054 @subsubheading Invoked by remote shell daemon
7056 Bash attempts to determine when it is being run with its standard input
7057 connected to a network connection, as when executed by the remote shell
7058 daemon, usually @code{rshd}, or the secure shell daemon @code{sshd}.
7059 If Bash determines it is being run in
7060 this fashion, it reads and executes commands from @file{~/.bashrc}, if that
7061 file exists and is readable.
7062 It will not do this if invoked as @code{sh}.
7063 The @option{--norc} option may be used to inhibit this behavior, and the
7064 @option{--rcfile} option may be used to force another file to be read, but
7065 neither @code{rshd} nor @code{sshd} generally invoke the shell with those
7066 options or allow them to be specified.
7068 @subsubheading Invoked with unequal effective and real @sc{uid/gid}s
7070 If Bash is started with the effective user (group) id not equal to the
7071 real user (group) id, and the @option{-p} option is not supplied, no startup
7072 files are read, shell functions are not inherited from the environment,
7073 the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH}, and @env{GLOBIGNORE}
7074 variables, if they appear in the environment, are ignored, and the effective
7075 user id is set to the real user id.
7076 If the @option{-p} option is supplied at invocation, the startup behavior is
7077 the same, but the effective user id is not reset.
7079 @node Interactive Shells
7080 @section Interactive Shells
7081 @cindex interactive shell
7082 @cindex shell, interactive
7085 * What is an Interactive Shell?:: What determines whether a shell is Interactive.
7086 * Is this Shell Interactive?:: How to tell if a shell is interactive.
7087 * Interactive Shell Behavior:: What changes in a interactive shell?
7090 @node What is an Interactive Shell?
7091 @subsection What is an Interactive Shell?
7093 An interactive shell
7094 is one started without non-option arguments, unless @option{-s} is
7095 specified, without specifying the @option{-c} option, and
7096 whose input and error output are both
7097 connected to terminals (as determined by @code{isatty(3)}),
7098 or one started with the @option{-i} option.
7100 An interactive shell generally reads from and writes to a user's
7103 The @option{-s} invocation option may be used to set the positional parameters
7104 when an interactive shell is started.
7106 @node Is this Shell Interactive?
7107 @subsection Is this Shell Interactive?
7109 To determine within a startup script whether or not Bash is
7110 running interactively,
7111 test the value of the @samp{-} special parameter.
7112 It contains @code{i} when the shell is interactive. For example:
7116 *i*) echo This shell is interactive ;;
7117 *) echo This shell is not interactive ;;
7121 Alternatively, startup scripts may examine the variable
7122 @env{PS1}; it is unset in non-interactive shells, and set in
7123 interactive shells. Thus:
7126 if [ -z "$PS1" ]; then
7127 echo This shell is not interactive
7129 echo This shell is interactive
7133 @node Interactive Shell Behavior
7134 @subsection Interactive Shell Behavior
7136 When the shell is running interactively, it changes its behavior in
7141 Startup files are read and executed as described in @ref{Bash Startup Files}.
7144 Job Control (@pxref{Job Control}) is enabled by default. When job
7145 control is in effect, Bash ignores the keyboard-generated job control
7146 signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
7149 Bash expands and displays @env{PS1} before reading the first line
7150 of a command, and expands and displays @env{PS2} before reading the
7151 second and subsequent lines of a multi-line command.
7152 Bash expands and displays @env{PS0} after it reads a command but before
7154 See @ref{Controlling the Prompt}, for a complete list of prompt
7155 string escape sequences.
7158 Bash executes the values of the set elements of the @env{PROMPT_COMMAND}
7159 array variable as commands before printing the primary prompt, @env{$PS1}
7160 (@pxref{Bash Variables}).
7163 Readline (@pxref{Command Line Editing}) is used to read commands from
7164 the user's terminal.
7167 Bash inspects the value of the @code{ignoreeof} option to @code{set -o}
7168 instead of exiting immediately when it receives an @code{EOF} on its
7169 standard input when reading a command (@pxref{The Set Builtin}).
7172 Command history (@pxref{Bash History Facilities})
7173 and history expansion (@pxref{History Interaction})
7174 are enabled by default.
7175 Bash will save the command history to the file named by @env{$HISTFILE}
7176 when a shell with history enabled exits.
7179 Alias expansion (@pxref{Aliases}) is performed by default.
7182 In the absence of any traps, Bash ignores @code{SIGTERM}
7186 In the absence of any traps, @code{SIGINT} is caught and handled
7188 @code{SIGINT} will interrupt some shell builtins.
7191 An interactive login shell sends a @code{SIGHUP} to all jobs on exit
7192 if the @code{huponexit} shell option has been enabled (@pxref{Signals}).
7195 The @option{-n} invocation option is ignored, and @samp{set -n} has
7196 no effect (@pxref{The Set Builtin}).
7199 Bash will check for mail periodically, depending on the values of the
7200 @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables
7201 (@pxref{Bash Variables}).
7204 Expansion errors due to references to unbound shell variables after
7205 @samp{set -u} has been enabled will not cause the shell to exit
7206 (@pxref{The Set Builtin}).
7209 The shell will not exit on expansion errors caused by @var{var} being unset
7210 or null in @code{$@{@var{var}:?@var{word}@}} expansions
7211 (@pxref{Shell Parameter Expansion}).
7214 Redirection errors encountered by shell builtins will not cause the
7218 When running in @sc{posix} mode, a special builtin returning an error
7219 status will not cause the shell to exit (@pxref{Bash POSIX Mode}).
7222 A failed @code{exec} will not cause the shell to exit
7223 (@pxref{Bourne Shell Builtins}).
7226 Parser syntax errors will not cause the shell to exit.
7229 If the @code{cdspell} shell option is enabled, the shell will attempt
7230 simple spelling correction for directory arguments to the @code{cd}
7231 builtin (see the description of the @code{cdspell}
7232 option to the @code{shopt} builtin in @ref{The Shopt Builtin}).
7233 The @code{cdspell} option is only effective in interactive shells.
7236 The shell will check the value of the @env{TMOUT} variable and exit
7237 if a command is not read within the specified number of seconds after
7238 printing @env{$PS1} (@pxref{Bash Variables}).
7242 @node Bash Conditional Expressions
7243 @section Bash Conditional Expressions
7244 @cindex expressions, conditional
7246 Conditional expressions are used by the @code{[[} compound command
7247 (@pxref{Conditional Constructs})
7248 and the @code{test} and @code{[} builtin commands
7249 (@pxref{Bourne Shell Builtins}).
7251 and @code{[} commands determine their behavior based on the number
7252 of arguments; see the descriptions of those commands for any other
7253 command-specific actions.
7255 Expressions may be unary or binary,
7256 and are formed from the following primaries.
7257 Unary expressions are often used to examine the status of a file.
7258 There are string operators and numeric comparison operators as well.
7259 Bash handles several filenames specially when they are used in
7261 If the operating system on which Bash is running provides these
7262 special files, Bash will use them; otherwise it will emulate them
7263 internally with this behavior:
7264 If the @var{file} argument to one of the primaries is of the form
7265 @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked.
7266 If the @var{file} argument to one of the primaries is one of
7267 @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file
7268 descriptor 0, 1, or 2, respectively, is checked.
7270 When used with @code{[[}, the @samp{<} and @samp{>} operators sort
7271 lexicographically using the current locale.
7272 The @code{test} command uses ASCII ordering.
7274 Unless otherwise specified, primaries that operate on files follow symbolic
7275 links and operate on the target of the link, rather than the link itself.
7279 True if @var{file} exists.
7282 True if @var{file} exists and is a block special file.
7285 True if @var{file} exists and is a character special file.
7288 True if @var{file} exists and is a directory.
7291 True if @var{file} exists.
7294 True if @var{file} exists and is a regular file.
7297 True if @var{file} exists and its set-group-id bit is set.
7300 True if @var{file} exists and is a symbolic link.
7303 True if @var{file} exists and its "sticky" bit is set.
7306 True if @var{file} exists and is a named pipe (FIFO).
7309 True if @var{file} exists and is readable.
7312 True if @var{file} exists and has a size greater than zero.
7315 True if file descriptor @var{fd} is open and refers to a terminal.
7318 True if @var{file} exists and its set-user-id bit is set.
7321 True if @var{file} exists and is writable.
7324 True if @var{file} exists and is executable.
7327 True if @var{file} exists and is owned by the effective group id.
7330 True if @var{file} exists and is a symbolic link.
7333 True if @var{file} exists and has been modified since it was last read.
7336 True if @var{file} exists and is owned by the effective user id.
7339 True if @var{file} exists and is a socket.
7341 @item @var{file1} -ef @var{file2}
7342 True if @var{file1} and @var{file2} refer to the same device and
7345 @item @var{file1} -nt @var{file2}
7346 True if @var{file1} is newer (according to modification date)
7347 than @var{file2}, or if @var{file1} exists and @var{file2} does not.
7349 @item @var{file1} -ot @var{file2}
7350 True if @var{file1} is older than @var{file2},
7351 or if @var{file2} exists and @var{file1} does not.
7353 @item -o @var{optname}
7354 True if the shell option @var{optname} is enabled.
7355 The list of options appears in the description of the @option{-o}
7356 option to the @code{set} builtin (@pxref{The Set Builtin}).
7358 @item -v @var{varname}
7359 True if the shell variable @var{varname} is set (has been assigned a value).
7361 @item -R @var{varname}
7362 True if the shell variable @var{varname} is set and is a name reference.
7364 @item -z @var{string}
7365 True if the length of @var{string} is zero.
7367 @item -n @var{string}
7369 True if the length of @var{string} is non-zero.
7371 @item @var{string1} == @var{string2}
7372 @itemx @var{string1} = @var{string2}
7373 True if the strings are equal.
7374 When used with the @code{[[} command, this performs pattern matching as
7375 described above (@pxref{Conditional Constructs}).
7377 @samp{=} should be used with the @code{test} command for @sc{posix} conformance.
7379 @item @var{string1} != @var{string2}
7380 True if the strings are not equal.
7382 @item @var{string1} < @var{string2}
7383 True if @var{string1} sorts before @var{string2} lexicographically.
7385 @item @var{string1} > @var{string2}
7386 True if @var{string1} sorts after @var{string2} lexicographically.
7388 @item @var{arg1} OP @var{arg2}
7390 @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}.
7391 These arithmetic binary operators return true if @var{arg1}
7392 is equal to, not equal to, less than, less than or equal to,
7393 greater than, or greater than or equal to @var{arg2},
7394 respectively. @var{Arg1} and @var{arg2}
7395 may be positive or negative integers.
7396 When used with the @code{[[} command, @var{Arg1} and @var{Arg2}
7397 are evaluated as arithmetic expressions (@pxref{Shell Arithmetic}).
7400 @node Shell Arithmetic
7401 @section Shell Arithmetic
7402 @cindex arithmetic, shell
7403 @cindex shell arithmetic
7404 @cindex expressions, arithmetic
7405 @cindex evaluation, arithmetic
7406 @cindex arithmetic evaluation
7408 The shell allows arithmetic expressions to be evaluated, as one of
7409 the shell expansions or by using the @code{((} compound command, the
7410 @code{let} builtin, or the @option{-i} option to the @code{declare} builtin.
7412 Evaluation is done in fixed-width integers with no check for overflow,
7413 though division by 0 is trapped and flagged as an error.
7414 The operators and their precedence, associativity, and values
7415 are the same as in the C language.
7416 The following list of operators is grouped into levels of
7417 equal-precedence operators.
7418 The levels are listed in order of decreasing precedence.
7422 @item @var{id}++ @var{id}--
7423 variable post-increment and post-decrement
7425 @item ++@var{id} --@var{id}
7426 variable pre-increment and pre-decrement
7429 unary minus and plus
7432 logical and bitwise negation
7438 multiplication, division, remainder
7441 addition, subtraction
7444 left and right bitwise shifts
7450 equality and inequality
7456 bitwise exclusive OR
7467 @item expr ? expr : expr
7468 conditional operator
7470 @item = *= /= %= += -= <<= >>= &= ^= |=
7477 Shell variables are allowed as operands; parameter expansion is
7478 performed before the expression is evaluated.
7479 Within an expression, shell variables may also be referenced by name
7480 without using the parameter expansion syntax.
7481 A shell variable that is null or unset evaluates to 0 when referenced
7482 by name without using the parameter expansion syntax.
7483 The value of a variable is evaluated as an arithmetic expression
7484 when it is referenced, or when a variable which has been given the
7485 @code{integer} attribute using @samp{declare -i} is assigned a value.
7486 A null value evaluates to 0.
7487 A shell variable need not have its @code{integer} attribute turned on
7488 to be used in an expression.
7490 Integer constants follow the C language definition, without suffixes or
7491 character constants.
7492 Constants with a leading 0 are interpreted as octal numbers.
7493 A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise,
7494 numbers take the form [@var{base}@code{#}]@var{n}, where the optional @var{base}
7495 is a decimal number between 2 and 64 representing the arithmetic
7496 base, and @var{n} is a number in that base.
7497 If @var{base}@code{#} is omitted, then base 10 is used.
7498 When specifying @var{n},
7499 if a non-digit is required,
7500 the digits greater than 9 are represented by the lowercase letters,
7501 the uppercase letters, @samp{@@}, and @samp{_}, in that order.
7502 If @var{base} is less than or equal to 36, lowercase and uppercase
7503 letters may be used interchangeably to represent numbers between 10
7506 Operators are evaluated in order of precedence. Sub-expressions in
7507 parentheses are evaluated first and may override the precedence
7512 @cindex alias expansion
7514 @dfn{Aliases} allow a string to be substituted for a word when it is used
7515 as the first word of a simple command.
7516 The shell maintains a list of aliases that may be set and unset with
7517 the @code{alias} and @code{unalias} builtin commands.
7519 The first word of each simple command, if unquoted, is checked to see
7521 If so, that word is replaced by the text of the alias.
7522 The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the
7523 shell metacharacters or quoting characters listed above may not appear
7525 The replacement text may contain any valid
7526 shell input, including shell metacharacters.
7527 The first word of the replacement text is tested for
7528 aliases, but a word that is identical to an alias being expanded
7529 is not expanded a second time.
7530 This means that one may alias @code{ls} to @code{"ls -F"},
7531 for instance, and Bash does not try to recursively expand the
7533 If the last character of the alias value is a
7534 @code{blank}, then the next command word following the
7535 alias is also checked for alias expansion.
7537 Aliases are created and listed with the @code{alias}
7538 command, and removed with the @code{unalias} command.
7540 There is no mechanism for using arguments in the replacement text,
7542 If arguments are needed, use a shell function
7543 (@pxref{Shell Functions}).
7545 Aliases are not expanded when the shell is not interactive,
7546 unless the @code{expand_aliases} shell option is set using
7547 @code{shopt} (@pxref{The Shopt Builtin}).
7549 The rules concerning the definition and use of aliases are
7550 somewhat confusing. Bash
7551 always reads at least one complete line of input,
7552 and all lines that make up a compound command,
7553 before executing any of the commands on that line or the compound command.
7554 Aliases are expanded when a
7555 command is read, not when it is executed. Therefore, an
7556 alias definition appearing on the same line as another
7557 command does not take effect until the next line of input is read.
7558 The commands following the alias definition
7559 on that line are not affected by the new alias.
7560 This behavior is also an issue when functions are executed.
7561 Aliases are expanded when a function definition is read,
7562 not when the function is executed, because a function definition
7563 is itself a command. As a consequence, aliases
7564 defined in a function are not available until after that
7565 function is executed. To be safe, always put
7566 alias definitions on a separate line, and do not use @code{alias}
7567 in compound commands.
7569 For almost every purpose, shell functions are preferred over aliases.
7575 Bash provides one-dimensional indexed and associative array variables.
7576 Any variable may be used as an indexed array;
7577 the @code{declare} builtin will explicitly declare an array.
7579 limit on the size of an array, nor any requirement that members
7580 be indexed or assigned contiguously.
7581 Indexed arrays are referenced using integers (including arithmetic
7582 expressions (@pxref{Shell Arithmetic})) and are zero-based;
7583 associative arrays use arbitrary strings.
7584 Unless otherwise noted, indexed array indices must be non-negative integers.
7586 An indexed array is created automatically if any variable is assigned to
7589 @var{name}[@var{subscript}]=@var{value}
7594 is treated as an arithmetic expression that must evaluate to a number.
7595 To explicitly declare an array, use
7597 declare -a @var{name}
7602 declare -a @var{name}[@var{subscript}]
7605 is also accepted; the @var{subscript} is ignored.
7608 Associative arrays are created using
7610 declare -A @var{name}
7614 specified for an array variable using the @code{declare} and
7615 @code{readonly} builtins. Each attribute applies to all members of
7618 Arrays are assigned to using compound assignments of the form
7620 @var{name}=(@var{value1} @var{value2} @dots{} )
7624 @var{value} may be of the form @code{[@var{subscript}]=}@var{string}.
7625 Indexed array assignments do not require anything but @var{string}.
7626 When assigning to indexed arrays, if
7627 the optional subscript is supplied, that index is assigned to;
7628 otherwise the index of the element assigned is the last index assigned
7629 to by the statement plus one. Indexing starts at zero.
7631 Each @var{value} in the list undergoes all the shell expansions
7632 described above (@pxref{Shell Expansions}).
7634 When assigning to an associative array, the words in a compound assignment
7635 may be either assignment statements, for which the subscript is required,
7636 or a list of words that is interpreted as a sequence of alternating keys
7638 @var{name}=(@var{key1} @var{value1} @var{key2} @var{value2} @dots{} ).
7639 These are treated identically to
7640 @var{name}=( [@var{key1}]=@var{value1} [@var{key2}]=@var{value2} @dots{} ).
7641 The first word in the list determines how the remaining words
7642 are interpreted; all assignments in a list must be of the same type.
7643 When using key/value pairs, the keys may not be missing or empty;
7644 a final missing value is treated like the empty string.
7646 This syntax is also accepted by the @code{declare}
7647 builtin. Individual array elements may be assigned to using the
7648 @code{@var{name}[@var{subscript}]=@var{value}} syntax introduced above.
7650 When assigning to an indexed array, if @var{name}
7651 is subscripted by a negative number, that number is
7652 interpreted as relative to one greater than the maximum index of
7653 @var{name}, so negative indices count back from the end of the
7654 array, and an index of -1 references the last element.
7656 Any element of an array may be referenced using
7657 @code{$@{@var{name}[@var{subscript}]@}}.
7658 The braces are required to avoid
7659 conflicts with the shell's filename expansion operators. If the
7660 @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members
7661 of the array @var{name}. These subscripts differ only when the word
7662 appears within double quotes.
7663 If the word is double-quoted,
7664 @code{$@{@var{name}[*]@}} expands to a single word with
7665 the value of each array member separated by the first character of the
7666 @env{IFS} variable, and @code{$@{@var{name}[@@]@}} expands each element of
7667 @var{name} to a separate word. When there are no array members,
7668 @code{$@{@var{name}[@@]@}} expands to nothing.
7669 If the double-quoted expansion occurs within a word, the expansion of
7670 the first parameter is joined with the beginning part of the original
7671 word, and the expansion of the last parameter is joined with the last
7672 part of the original word.
7673 This is analogous to the
7674 expansion of the special parameters @samp{@@} and @samp{*}.
7675 @code{$@{#@var{name}[@var{subscript}]@}} expands to the length of
7676 @code{$@{@var{name}[@var{subscript}]@}}.
7677 If @var{subscript} is @samp{@@} or
7678 @samp{*}, the expansion is the number of elements in the array.
7679 If the @var{subscript}
7680 used to reference an element of an indexed array
7681 evaluates to a number less than zero, it is
7682 interpreted as relative to one greater than the maximum index of the array,
7683 so negative indices count back from the end of the array,
7684 and an index of -1 refers to the last element.
7686 Referencing an array variable without a subscript is equivalent to
7687 referencing with a subscript of 0.
7688 Any reference to a variable using a valid subscript is legal, and
7689 @code{bash} will create an array if necessary.
7691 An array variable is considered set if a subscript has been assigned a
7692 value. The null string is a valid value.
7694 It is possible to obtain the keys (indices) of an array as well as the values.
7695 $@{!@var{name}[@@]@} and $@{!@var{name}[*]@} expand to the indices
7696 assigned in array variable @var{name}.
7697 The treatment when in double quotes is similar to the expansion of the
7698 special parameters @samp{@@} and @samp{*} within double quotes.
7700 The @code{unset} builtin is used to destroy arrays.
7701 @code{unset @var{name}[@var{subscript}]}
7702 destroys the array element at index @var{subscript}.
7703 Negative subscripts to indexed arrays are interpreted as described above.
7704 Unsetting the last element of an array variable does not unset the variable.
7705 @code{unset @var{name}}, where @var{name} is an array, removes the
7707 @code{unset @var{name}[@var{subscript}]} behaves differently
7708 depending on the array type when given a
7709 subscript of @samp{*} or @samp{@@}.
7710 When @var{name} is an associative array, it removes the element with key
7711 @samp{*} or @samp{@@}.
7712 If @var{name} is an indexed array, @code{unset} removes all of the elements,
7713 but does not remove the array itself.
7715 When using a variable name with a subscript as an argument to a command,
7716 such as with @code{unset}, without using the word expansion syntax
7717 described above, the argument is subject to the shell's filename expansion.
7718 If filename expansion is not desired, the argument should be quoted.
7720 The @code{declare}, @code{local}, and @code{readonly}
7721 builtins each accept a @option{-a} option to specify an indexed
7722 array and a @option{-A} option to specify an associative array.
7723 If both options are supplied, @option{-A} takes precedence.
7724 The @code{read} builtin accepts a @option{-a}
7725 option to assign a list of words read from the standard input
7726 to an array, and can read values from the standard input into
7727 individual array elements. The @code{set} and @code{declare}
7728 builtins display array values in a way that allows them to be
7731 @node The Directory Stack
7732 @section The Directory Stack
7733 @cindex directory stack
7736 * Directory Stack Builtins:: Bash builtin commands to manipulate
7737 the directory stack.
7740 The directory stack is a list of recently-visited directories. The
7741 @code{pushd} builtin adds directories to the stack as it changes
7742 the current directory, and the @code{popd} builtin removes specified
7743 directories from the stack and changes the current directory to
7744 the directory removed. The @code{dirs} builtin displays the contents
7745 of the directory stack. The current directory is always the "top"
7746 of the directory stack.
7748 The contents of the directory stack are also visible
7749 as the value of the @env{DIRSTACK} shell variable.
7751 @node Directory Stack Builtins
7752 @subsection Directory Stack Builtins
7759 dirs [-clpv] [+@var{N} | -@var{N}]
7762 Display the list of currently remembered directories. Directories
7763 are added to the list with the @code{pushd} command; the
7764 @code{popd} command removes directories from the list.
7765 The current directory is always the first directory in the stack.
7769 Clears the directory stack by deleting all of the elements.
7771 Produces a listing using full pathnames;
7772 the default listing format uses a tilde to denote the home directory.
7774 Causes @code{dirs} to print the directory stack with one entry per
7777 Causes @code{dirs} to print the directory stack with one entry per
7778 line, prefixing each entry with its index in the stack.
7780 Displays the @var{N}th directory (counting from the left of the
7781 list printed by @code{dirs} when invoked without options), starting
7784 Displays the @var{N}th directory (counting from the right of the
7785 list printed by @code{dirs} when invoked without options), starting
7792 popd [-n] [+@var{N} | -@var{N}]
7795 Removes elements from the directory stack.
7796 The elements are numbered from 0 starting at the first directory
7797 listed by @code{dirs};
7798 that is, @code{popd} is equivalent to @code{popd +0}.
7800 When no arguments are given, @code{popd}
7801 removes the top directory from the stack and changes to
7802 the new top directory.
7804 Arguments, if supplied, have the following meanings:
7808 Suppresses the normal change of directory when removing directories
7809 from the stack, so that only the stack is manipulated.
7811 Removes the @var{N}th directory (counting from the left of the
7812 list printed by @code{dirs}), starting with zero, from the stack.
7814 Removes the @var{N}th directory (counting from the right of the
7815 list printed by @code{dirs}), starting with zero, from the stack.
7818 If the top element of the directory stack is modified, and
7819 the @option{-n} option was not supplied, @code{popd} uses the @code{cd}
7820 builtin to change to the directory at the top of the stack.
7821 If the @code{cd} fails, @code{popd} returns a non-zero value.
7823 Otherwise, @code{popd} returns an unsuccessful status if
7824 an invalid option is encountered, the directory stack
7825 is empty, or a non-existent directory stack entry is specified.
7827 If the @code{popd} command is successful,
7828 Bash runs @code{dirs} to show the final contents of the directory stack,
7829 and the return status is 0.
7834 pushd [-n] [@var{+N} | @var{-N} | @var{dir}]
7837 Adds a directory to the top of the directory stack, or rotates
7838 the stack, making the new top of the stack the current working
7840 With no arguments, @code{pushd} exchanges the top two elements
7841 of the directory stack.
7843 Arguments, if supplied, have the following meanings:
7847 Suppresses the normal change of directory when rotating or
7848 adding directories to the stack, so that only the stack is manipulated.
7850 Brings the @var{N}th directory (counting from the left of the
7851 list printed by @code{dirs}, starting with zero) to the top of
7852 the list by rotating the stack.
7854 Brings the @var{N}th directory (counting from the right of the
7855 list printed by @code{dirs}, starting with zero) to the top of
7856 the list by rotating the stack.
7858 Makes @var{dir} be the top of the stack.
7861 After the stack has been modified, if the @option{-n} option was not
7862 supplied, @code{pushd} uses the @code{cd} builtin to change to the
7863 directory at the top of the stack.
7864 If the @code{cd} fails, @code{pushd} returns a non-zero value.
7866 Otherwise, if no arguments are supplied, @code{pushd} returns 0 unless the
7867 directory stack is empty.
7868 When rotating the directory stack, @code{pushd} returns 0 unless
7869 the directory stack is empty or a non-existent directory stack element
7872 If the @code{pushd} command is successful,
7873 Bash runs @code{dirs} to show the final contents of the directory stack.
7877 @node Controlling the Prompt
7878 @section Controlling the Prompt
7881 Bash examines the value of the array variable @env{PROMPT_COMMAND} just before
7882 printing each primary prompt.
7883 If any elements in @env{PROMPT_COMMAND} are set and non-null, Bash
7884 executes each value, in numeric order,
7885 just as if it had been typed on the command line.
7887 In addition, the following table describes the special characters which
7888 can appear in the prompt variables @env{PS0}, @env{PS1}, @env{PS2}, and
7895 The date, in "Weekday Month Date" format (e.g., "Tue May 26").
7896 @item \D@{@var{format}@}
7897 The @var{format} is passed to @code{strftime}(3) and the result is inserted
7898 into the prompt string; an empty @var{format} results in a locale-specific
7899 time representation. The braces are required.
7901 An escape character.
7903 The hostname, up to the first `.'.
7907 The number of jobs currently managed by the shell.
7909 The basename of the shell's terminal device name.
7915 The name of the shell, the basename of @code{$0} (the portion
7916 following the final slash).
7918 The time, in 24-hour HH:MM:SS format.
7920 The time, in 12-hour HH:MM:SS format.
7922 The time, in 12-hour am/pm format.
7924 The time, in 24-hour HH:MM format.
7926 The username of the current user.
7928 The version of Bash (e.g., 2.00)
7930 The release of Bash, version + patchlevel (e.g., 2.00.0)
7932 The value of the @code{PWD} shell variable (@env{$PWD}),
7933 with @env{$HOME} abbreviated with a tilde
7934 (uses the @env{$PROMPT_DIRTRIM} variable).
7936 The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde.
7938 The history number of this command.
7940 The command number of this command.
7942 If the effective uid is 0, @code{#}, otherwise @code{$}.
7944 The character whose ASCII code is the octal value @var{nnn}.
7948 Begin a sequence of non-printing characters. This could be used to
7949 embed a terminal control sequence into the prompt.
7951 End a sequence of non-printing characters.
7954 The command number and the history number are usually different:
7955 the history number of a command is its position in the history
7956 list, which may include commands restored from the history file
7957 (@pxref{Bash History Facilities}), while the command number is
7958 the position in the sequence of commands executed during the current
7961 After the string is decoded, it is expanded via
7962 parameter expansion, command substitution, arithmetic
7963 expansion, and quote removal, subject to the value of the
7964 @code{promptvars} shell option (@pxref{The Shopt Builtin}).
7965 This can have unwanted side effects if escaped portions of the string
7966 appear within command substitution or contain characters special to
7969 @node The Restricted Shell
7970 @section The Restricted Shell
7971 @cindex restricted shell
7973 If Bash is started with the name @code{rbash}, or the
7974 @option{--restricted}
7977 option is supplied at invocation, the shell becomes restricted.
7978 A restricted shell is used to
7979 set up an environment more controlled than the standard shell.
7980 A restricted shell behaves identically to @code{bash}
7981 with the exception that the following are disallowed or not performed:
7985 Changing directories with the @code{cd} builtin.
7987 Setting or unsetting the values of the @env{SHELL}, @env{PATH},
7989 @env{ENV}, or @env{BASH_ENV} variables.
7991 Specifying command names containing slashes.
7993 Specifying a filename containing a slash as an argument to the @code{.}
7996 Specifying a filename containing a slash as an argument to the @code{history}
7999 Specifying a filename containing a slash as an argument to the @option{-p}
8000 option to the @code{hash} builtin command.
8002 Importing function definitions from the shell environment at startup.
8004 Parsing the value of @env{SHELLOPTS} from the shell environment at startup.
8006 Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&},
8007 @samp{&>}, and @samp{>>} redirection operators.
8009 Using the @code{exec} builtin to replace the shell with another command.
8011 Adding or deleting builtin commands with the
8012 @option{-f} and @option{-d} options to the @code{enable} builtin.
8014 Using the @code{enable} builtin command to enable disabled shell builtins.
8016 Specifying the @option{-p} option to the @code{command} builtin.
8018 Turning off restricted mode with @samp{set +r} or @samp{shopt -u restricted_shell}.
8021 These restrictions are enforced after any startup files are read.
8023 When a command that is found to be a shell script is executed
8024 (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in
8025 the shell spawned to execute the script.
8027 The restricted shell mode is only one component of a useful restricted
8028 environment. It should be accompanied by setting @env{PATH} to a value
8029 that allows execution of only a few verified commands (commands that
8030 allow shell escapes are particularly vulnerable), changing the current
8031 directory to a non-writable directory other than @env{$HOME} after login,
8032 not allowing the restricted shell to execute shell scripts, and cleaning
8033 the environment of variables that cause some commands to modify their
8034 behavior (e.g., @env{VISUAL} or @env{PAGER}).
8036 Modern systems provide more secure ways to implement a restricted environment,
8037 such as @code{jails}, @code{zones}, or @code{containers}.
8040 @node Bash POSIX Mode
8041 @section Bash POSIX Mode
8044 Starting Bash with the @option{--posix} command-line option or executing
8045 @samp{set -o posix} while Bash is running will cause Bash to conform more
8046 closely to the @sc{posix} standard by changing the behavior to
8047 match that specified by @sc{posix} in areas where the Bash default differs.
8049 When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the
8052 The following list is what's changed when `@sc{posix} mode' is in effect:
8056 Bash ensures that the @env{POSIXLY_CORRECT} variable is set.
8059 When a command in the hash table no longer exists, Bash will re-search
8060 @env{$PATH} to find the new location. This is also available with
8061 @samp{shopt -s checkhash}.
8064 Bash will not insert a command without the execute bit set into the
8065 command hash table, even if it returns it as a (last-ditch) result
8066 from a @env{$PATH} search.
8069 The message printed by the job control code and builtins when a job
8070 exits with a non-zero status is `Done(status)'.
8073 The message printed by the job control code and builtins when a job
8074 is stopped is `Stopped(@var{signame})', where @var{signame} is, for
8075 example, @code{SIGTSTP}.
8078 Alias expansion is always enabled, even in non-interactive shells.
8081 Reserved words appearing in a context where reserved words are recognized
8082 do not undergo alias expansion.
8085 The @sc{posix} @env{PS1} and @env{PS2} expansions of @samp{!} to
8086 the history number and @samp{!!} to @samp{!} are enabled,
8087 and parameter expansion is performed on the values of @env{PS1} and
8088 @env{PS2} regardless of the setting of the @code{promptvars} option.
8091 The @sc{posix} startup files are executed (@env{$ENV}) rather than
8092 the normal Bash files.
8095 Tilde expansion is only performed on assignments preceding a command
8096 name, rather than on all assignment statements on the line.
8099 The default history file is @file{~/.sh_history} (this is the
8100 default value of @env{$HISTFILE}).
8103 Redirection operators do not perform filename expansion on the word
8104 in the redirection unless the shell is interactive.
8107 Redirection operators do not perform word splitting on the word in the
8111 Function names must be valid shell @code{name}s. That is, they may not
8112 contain characters other than letters, digits, and underscores, and
8113 may not start with a digit. Declaring a function with an invalid name
8114 causes a fatal syntax error in non-interactive shells.
8117 Function names may not be the same as one of the @sc{posix} special
8121 @sc{posix} special builtins are found before shell functions
8122 during command lookup.
8125 When printing shell function definitions (e.g., by @code{type}), Bash does
8126 not print the @code{function} keyword.
8129 Literal tildes that appear as the first character in elements of
8130 the @env{PATH} variable are not expanded as described above
8131 under @ref{Tilde Expansion}.
8134 The @code{time} reserved word may be used by itself as a command. When
8135 used in this way, it displays timing statistics for the shell and its
8136 completed children. The @env{TIMEFORMAT} variable controls the format
8137 of the timing information.
8140 When parsing and expanding a $@{@dots{}@} expansion that appears within
8141 double quotes, single quotes are no longer special and cannot be used to
8142 quote a closing brace or other special character, unless the operator is
8143 one of those defined to perform pattern removal. In this case, they do
8144 not have to appear as matched pairs.
8147 The parser does not recognize @code{time} as a reserved word if the next
8148 token begins with a @samp{-}.
8152 When parsing @code{$()} command substitutions containing here-documents,
8153 the parser does not allow a here-document to be delimited by the closing
8154 right parenthesis. The newline after the here-document delimiter is required.
8158 The @samp{!} character does not introduce history expansion within a
8159 double-quoted string, even if the @code{histexpand} option is enabled.
8162 If a @sc{posix} special builtin returns an error status, a
8163 non-interactive shell exits. The fatal errors are those listed in
8164 the @sc{posix} standard, and include things like passing incorrect options,
8165 redirection errors, variable assignment errors for assignments preceding
8166 the command name, and so on.
8169 A non-interactive shell exits with an error status if a variable
8170 assignment error occurs when no command name follows the assignment
8172 A variable assignment error occurs, for example, when trying to assign
8173 a value to a readonly variable.
8176 A non-interactive shell exits with an error status if a variable
8177 assignment error occurs in an assignment statement preceding a special
8178 builtin, but not with any other simple command. For any other simple
8179 command, the shell aborts execution of that command, and execution continues
8180 at the top level ("the shell shall not perform any further processing of the
8181 command in which the error occurred").
8184 A non-interactive shell exits with an error status if the iteration
8185 variable in a @code{for} statement or the selection variable in a
8186 @code{select} statement is a readonly variable.
8189 Non-interactive shells exit if @var{filename} in @code{.} @var{filename}
8193 Non-interactive shells exit if a syntax error in an arithmetic expansion
8194 results in an invalid expression.
8197 Non-interactive shells exit if a parameter expansion error occurs.
8200 Non-interactive shells exit if there is a syntax error in a script read
8201 with the @code{.} or @code{source} builtins, or in a string processed by
8202 the @code{eval} builtin.
8205 While variable indirection is available, it may not be applied to the
8206 @samp{#} and @samp{?} special parameters.
8209 When expanding the @samp{*} special parameter in a pattern context where the
8210 expansion is double-quoted does not treat the @code{$*} as if it were
8214 Assignment statements preceding @sc{posix} special builtins
8215 persist in the shell environment after the builtin completes.
8218 The @code{command} builtin does not prevent builtins that take assignment
8219 statements as arguments from expanding them as assignment statements;
8220 when not in @sc{posix} mode, assignment builtins lose their assignment
8221 statement expansion properties when preceded by @code{command}.
8224 The @code{bg} builtin uses the required format to describe each job placed
8225 in the background, which does not include an indication of whether the job
8226 is the current or previous job.
8229 The output of @samp{kill -l} prints all the signal names on a single line,
8230 separated by spaces, without the @samp{SIG} prefix.
8233 The @code{kill} builtin does not accept signal names with a @samp{SIG}
8237 The @code{export} and @code{readonly} builtin commands display their
8238 output in the format required by @sc{posix}.
8241 The @code{trap} builtin displays signal names without the leading
8245 The @code{trap} builtin doesn't check the first argument for a possible
8246 signal specification and revert the signal handling to the original
8247 disposition if it is, unless that argument consists solely of digits and
8248 is a valid signal number. If users want to reset the handler for a given
8249 signal to the original disposition, they should use @samp{-} as the
8253 @code{trap -p} displays signals whose dispositions are set to SIG_DFL and
8254 those that were ignored when the shell started.
8257 The @code{.} and @code{source} builtins do not search the current directory
8258 for the filename argument if it is not found by searching @env{PATH}.
8261 Enabling @sc{posix} mode has the effect of setting the
8262 @code{inherit_errexit} option, so
8263 subshells spawned to execute command substitutions inherit the value of
8264 the @option{-e} option from the parent shell.
8265 When the @code{inherit_errexit} option is not enabled,
8266 Bash clears the @option{-e} option in such subshells.
8269 Enabling @sc{posix} mode has the effect of setting the
8270 @code{shift_verbose} option, so numeric arguments to @code{shift}
8271 that exceed the number of positional parameters will result in an
8275 When the @code{alias} builtin displays alias definitions, it does not
8276 display them with a leading @samp{alias } unless the @option{-p} option
8280 When the @code{set} builtin is invoked without options, it does not display
8281 shell function names and definitions.
8284 When the @code{set} builtin is invoked without options, it displays
8285 variable values without quotes, unless they contain shell metacharacters,
8286 even if the result contains nonprinting characters.
8289 When the @code{cd} builtin is invoked in logical mode, and the pathname
8290 constructed from @code{$PWD} and the directory name supplied as an argument
8291 does not refer to an existing directory, @code{cd} will fail instead of
8292 falling back to physical mode.
8295 When the @code{cd} builtin cannot change a directory because the
8296 length of the pathname
8297 constructed from @code{$PWD} and the directory name supplied as an argument
8298 exceeds @code{PATH_MAX} when all symbolic links are expanded, @code{cd} will
8299 fail instead of attempting to use only the supplied directory name.
8302 The @code{pwd} builtin verifies that the value it prints is the same as the
8303 current directory, even if it is not asked to check the file system with the
8307 When listing the history, the @code{fc} builtin does not include an
8308 indication of whether or not a history entry has been modified.
8311 The default editor used by @code{fc} is @code{ed}.
8314 The @code{type} and @code{command} builtins will not report a non-executable
8315 file as having been found, though the shell will attempt to execute such a
8316 file if it is the only so-named file found in @code{$PATH}.
8319 The @code{vi} editing mode will invoke the @code{vi} editor directly when
8320 the @samp{v} command is run, instead of checking @code{$VISUAL} and
8324 When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret
8325 any arguments to @code{echo} as options. Each argument is displayed, after
8326 escape characters are converted.
8329 The @code{ulimit} builtin uses a block size of 512 bytes for the @option{-c}
8330 and @option{-f} options.
8333 The arrival of @code{SIGCHLD} when a trap is set on @code{SIGCHLD} does
8334 not interrupt the @code{wait} builtin and cause it to return immediately.
8335 The trap command is run once for each child that exits.
8338 The @code{read} builtin may be interrupted by a signal for which a trap
8340 If Bash receives a trapped signal while executing @code{read}, the trap
8341 handler executes and @code{read} returns an exit status greater than 128.
8344 Bash removes an exited background process's status from the list of such
8345 statuses after the @code{wait} builtin is used to obtain it.
8349 There is other @sc{posix} behavior that Bash does not implement by
8350 default even when in @sc{posix} mode.
8356 The @code{fc} builtin checks @code{$EDITOR} as a program to edit history
8357 entries if @code{FCEDIT} is unset, rather than defaulting directly to
8358 @code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset.
8361 As noted above, Bash requires the @code{xpg_echo} option to be enabled for
8362 the @code{echo} builtin to be fully conformant.
8366 Bash can be configured to be @sc{posix}-conformant by default, by specifying
8367 the @option{--enable-strict-posix-default} to @code{configure} when building
8368 (@pxref{Optional Features}).
8370 @node Shell Compatibility Mode
8371 @section Shell Compatibility Mode
8372 @cindex Compatibility Level
8373 @cindex Compatibility Mode
8375 Bash-4.0 introduced the concept of a `shell compatibility level', specified
8376 as a set of options to the shopt builtin
8382 There is only one current
8383 compatibility level -- each option is mutually exclusive.
8384 The compatibility level is intended to allow users to select behavior
8385 from previous versions that is incompatible with newer versions
8386 while they migrate scripts to use current features and
8387 behavior. It's intended to be a temporary solution.
8389 This section does not mention behavior that is standard for a particular
8390 version (e.g., setting @code{compat32} means that quoting the rhs of the regexp
8391 matching operator quotes special regexp characters in the word, which is
8392 default behavior in bash-3.2 and above).
8394 If a user enables, say, @code{compat32}, it may affect the behavior of other
8395 compatibility levels up to and including the current compatibility level.
8396 The idea is that each compatibility level controls behavior that changed
8397 in that version of Bash,
8398 but that behavior may have been present in earlier versions.
8399 For instance, the change to use locale-based comparisons with the @code{[[}
8400 command came in bash-4.1, and earlier versions used ASCII-based comparisons,
8401 so enabling @code{compat32} will enable ASCII-based comparisons as well.
8402 That granularity may not be sufficient for
8403 all uses, and as a result users should employ compatibility levels carefully.
8404 Read the documentation for a particular feature to find out the
8407 Bash-4.3 introduced a new shell variable: @env{BASH_COMPAT}.
8409 to this variable (a decimal version number like 4.2, or an integer
8410 corresponding to the @code{compat}@var{NN} option, like 42) determines the
8411 compatibility level.
8413 Starting with bash-4.4, Bash has begun deprecating older compatibility
8415 Eventually, the options will be removed in favor of @env{BASH_COMPAT}.
8417 Bash-5.0 is the final version for which there will be an individual shopt
8418 option for the previous version. Users should use @env{BASH_COMPAT}
8419 on bash-5.0 and later versions.
8421 The following table describes the behavior changes controlled by each
8422 compatibility level setting.
8423 The @code{compat}@var{NN} tag is used as shorthand for setting the
8425 to @var{NN} using one of the following mechanisms.
8426 For versions prior to bash-5.0, the compatibility level may be set using
8427 the corresponding @code{compat}@var{NN} shopt option.
8428 For bash-4.3 and later versions, the @env{BASH_COMPAT} variable is preferred,
8429 and it is required for bash-5.1 and later versions.
8435 quoting the rhs of the @code{[[} command's regexp matching operator (=~)
8436 has no special effect
8442 interrupting a command list such as "a ; b ; c" causes the execution
8443 of the next command in the list (in bash-4.0 and later versions,
8444 the shell acts as if it received the interrupt, so
8445 interrupting one command in a list aborts the execution of the
8452 the @samp{<} and @samp{>} operators to the @code{[[} command do not
8453 consider the current locale when comparing strings; they use ASCII
8455 Bash versions prior to bash-4.1 use ASCII collation and strcmp(3);
8456 bash-4.1 and later use the current locale's collation sequence and
8463 in posix mode, @code{time} may be followed by options and still be
8464 recognized as a reserved word (this is @sc{posix} interpretation 267)
8466 in posix mode, the parser requires that an even number of single
8467 quotes occur in the @var{word} portion of a double-quoted $@{@dots{}@}
8468 parameter expansion and treats them specially, so that characters within
8469 the single quotes are considered quoted
8470 (this is @sc{posix} interpretation 221)
8476 the replacement string in double-quoted pattern substitution does not
8477 undergo quote removal, as it does in versions after bash-4.2
8479 in posix mode, single quotes are considered special when expanding
8480 the @var{word} portion of a double-quoted $@{@dots{}@} parameter expansion
8481 and can be used to quote a closing brace or other special character
8482 (this is part of @sc{posix} interpretation 221);
8483 in later versions, single quotes
8484 are not special within double-quoted word expansions
8490 the shell does not print a warning message if an attempt is made to
8491 use a quoted compound assignment as an argument to declare
8492 (declare -a foo='(1 2)'). Later versions warn that this usage is
8495 word expansion errors are considered non-fatal errors that cause the
8496 current command to fail, even in posix mode
8497 (the default behavior is to make them fatal errors that cause the shell
8500 when executing a shell function, the loop state (while/until/etc.)
8501 is not reset, so @code{break} or @code{continue} in that function will break
8502 or continue loops in the calling context. Bash-4.4 and later reset
8503 the loop state to prevent this
8509 the shell sets up the values used by @env{BASH_ARGV} and @env{BASH_ARGC}
8510 so they can expand to the shell's positional parameters even if extended
8511 debugging mode is not enabled
8513 a subshell inherits loops from its parent context, so @code{break}
8514 or @code{continue} will cause the subshell to exit.
8515 Bash-5.0 and later reset the loop state to prevent the exit
8517 variable assignments preceding builtins like @code{export} and @code{readonly}
8518 that set attributes continue to affect variables with the same
8519 name in the calling environment even if the shell is not in posix
8523 @item compat50 (set using BASH_COMPAT)
8526 Bash-5.1 changed the way @code{$RANDOM} is generated to introduce slightly
8527 more randomness. If the shell compatibility level is set to 50 or
8528 lower, it reverts to the method from bash-5.0 and previous versions,
8529 so seeding the random number generator by assigning a value to
8530 @env{RANDOM} will produce the same sequence as in bash-5.0
8532 If the command hash table is empty, Bash versions prior to bash-5.1
8533 printed an informational message to that effect, even when producing
8534 output that can be reused as input. Bash-5.1 suppresses that message
8535 when the @option{-l} option is supplied.
8538 @item compat51 (set using BASH_COMPAT)
8541 The @code{unset} builtin will unset the array @code{a} given an argument like
8543 Bash-5.2 will unset an element with key @samp{@@} (associative arrays)
8544 or remove all the elements without unsetting the array (indexed arrays)
8546 arithmetic commands ( ((...)) ) and the expressions in an arithmetic for
8547 statement can be expanded more than once
8549 expressions used as arguments to arithmetic operators in the @code{[[}
8550 conditional command can be expanded more than once
8552 the expressions in substring parameter brace expansion can be
8553 expanded more than once
8555 the expressions in the $(( ... )) word expansion can be expanded
8558 arithmetic expressions used as indexed array subscripts can be
8559 expanded more than once
8561 @code{test -v}, when given an argument of @samp{A[@@]}, where @var{A} is
8562 an existing associative array, will return true if the array has any set
8564 Bash-5.2 will look for and report on a key named @samp{@@}
8566 the $@{@var{parameter}[:]=@var{value}@} word expansion will return
8567 @var{value}, before any variable-specific transformations have been
8568 performed (e.g., converting to lowercase).
8569 Bash-5.2 will return the final value assigned to the variable.
8574 @chapter Job Control
8576 This chapter discusses what job control is, how it works, and how
8577 Bash allows you to access its facilities.
8580 * Job Control Basics:: How job control works.
8581 * Job Control Builtins:: Bash builtin commands used to interact
8583 * Job Control Variables:: Variables Bash uses to customize job
8587 @node Job Control Basics
8588 @section Job Control Basics
8592 @cindex suspending jobs
8595 refers to the ability to selectively stop (suspend)
8596 the execution of processes and continue (resume)
8597 their execution at a later point. A user typically employs
8598 this facility via an interactive interface supplied jointly
8599 by the operating system kernel's terminal driver and Bash.
8601 The shell associates a @var{job} with each pipeline. It keeps a
8602 table of currently executing jobs, which may be listed with the
8603 @code{jobs} command. When Bash starts a job
8604 asynchronously, it prints a line that looks
8610 indicating that this job is job number 1 and that the process @sc{id}
8611 of the last process in the pipeline associated with this job is
8612 25647. All of the processes in a single pipeline are members of
8613 the same job. Bash uses the @var{job} abstraction as the
8614 basis for job control.
8616 To facilitate the implementation of the user interface to job
8617 control, the operating system maintains the notion of a current terminal
8618 process group @sc{id}. Members of this process group (processes whose
8619 process group @sc{id} is equal to the current terminal process group
8620 @sc{id}) receive keyboard-generated signals such as @code{SIGINT}.
8621 These processes are said to be in the foreground. Background
8622 processes are those whose process group @sc{id} differs from the
8623 terminal's; such processes are immune to keyboard-generated
8624 signals. Only foreground processes are allowed to read from or, if
8625 the user so specifies with @code{stty tostop}, write to the terminal.
8626 Background processes which attempt to
8627 read from (write to when @code{stty tostop} is in effect) the
8628 terminal are sent a @code{SIGTTIN} (@code{SIGTTOU})
8629 signal by the kernel's terminal driver,
8630 which, unless caught, suspends the process.
8632 If the operating system on which Bash is running supports
8633 job control, Bash contains facilities to use it. Typing the
8634 @dfn{suspend} character (typically @samp{^Z}, Control-Z) while a
8635 process is running causes that process to be stopped and returns
8636 control to Bash. Typing the @dfn{delayed suspend} character
8637 (typically @samp{^Y}, Control-Y) causes the process to be stopped
8638 when it attempts to read input from the terminal, and control to
8639 be returned to Bash. The user then manipulates the state of
8640 this job, using the @code{bg} command to continue it in the
8641 background, the @code{fg} command to continue it in the
8642 foreground, or the @code{kill} command to kill it. A @samp{^Z}
8643 takes effect immediately, and has the additional side effect of
8644 causing pending output and typeahead to be discarded.
8646 There are a number of ways to refer to a job in the shell. The
8647 character @samp{%} introduces a job specification (@dfn{jobspec}).
8649 Job number @code{n} may be referred to as @samp{%n}.
8650 The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the
8651 current job, which is the last job stopped while it was in the foreground
8652 or started in the background.
8653 A single @samp{%} (with no accompanying job specification) also refers
8655 The previous job may be referenced using @samp{%-}.
8656 If there is only a single job, @samp{%+} and @samp{%-} can both be used
8657 to refer to that job.
8658 In output pertaining to jobs (e.g., the output of the @code{jobs}
8659 command), the current job is always flagged with a @samp{+}, and the
8660 previous job with a @samp{-}.
8662 A job may also be referred to
8663 using a prefix of the name used to start it, or using a substring
8664 that appears in its command line. For example, @samp{%ce} refers
8665 to a stopped job whose command name begins with @samp{ce}.
8666 Using @samp{%?ce}, on the
8667 other hand, refers to any job containing the string @samp{ce} in
8668 its command line. If the prefix or substring matches more than one job,
8669 Bash reports an error.
8671 Simply naming a job can be used to bring it into the foreground:
8672 @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the
8673 background into the foreground. Similarly, @samp{%1 &} resumes
8674 job 1 in the background, equivalent to @samp{bg %1}
8676 The shell learns immediately whenever a job changes state.
8677 Normally, Bash waits until it is about to print a prompt
8678 before reporting changes in a job's status so as to not interrupt
8680 If the @option{-b} option to the @code{set} builtin is enabled,
8681 Bash reports such changes immediately (@pxref{The Set Builtin}).
8682 Any trap on @code{SIGCHLD} is executed for each child process
8685 If an attempt to exit Bash is made while jobs are stopped, (or running, if
8686 the @code{checkjobs} option is enabled -- see @ref{The Shopt Builtin}), the
8687 shell prints a warning message, and if the @code{checkjobs} option is
8688 enabled, lists the jobs and their statuses.
8689 The @code{jobs} command may then be used to inspect their status.
8690 If a second attempt to exit is made without an intervening command,
8691 Bash does not print another warning, and any stopped jobs are terminated.
8693 When the shell is waiting for a job or process using the @code{wait}
8694 builtin, and job control is enabled, @code{wait} will return when the
8695 job changes state. The @option{-f} option causes @code{wait} to wait
8696 until the job or process terminates before returning.
8698 @node Job Control Builtins
8699 @section Job Control Builtins
8706 bg [@var{jobspec} @dots{}]
8709 Resume each suspended job @var{jobspec} in the background, as if it
8710 had been started with @samp{&}.
8711 If @var{jobspec} is not supplied, the current job is used.
8712 The return status is zero unless it is run when job control is not
8713 enabled, or, when run with job control enabled, any
8714 @var{jobspec} was not found or specifies a job
8715 that was started without job control.
8723 Resume the job @var{jobspec} in the foreground and make it the current job.
8724 If @var{jobspec} is not supplied, the current job is used.
8725 The return status is that of the command placed into the foreground,
8726 or non-zero if run when job control is disabled or, when run with
8727 job control enabled, @var{jobspec} does not specify a valid job or
8728 @var{jobspec} specifies a job that was started without job control.
8733 jobs [-lnprs] [@var{jobspec}]
8734 jobs -x @var{command} [@var{arguments}]
8737 The first form lists the active jobs. The options have the
8742 List process @sc{id}s in addition to the normal information.
8745 Display information only about jobs that have changed status since
8746 the user was last notified of their status.
8749 List only the process @sc{id} of the job's process group leader.
8752 Display only running jobs.
8755 Display only stopped jobs.
8758 If @var{jobspec} is given,
8759 output is restricted to information about that job.
8760 If @var{jobspec} is not supplied, the status of all jobs is
8763 If the @option{-x} option is supplied, @code{jobs} replaces any
8764 @var{jobspec} found in @var{command} or @var{arguments} with the
8765 corresponding process group @sc{id}, and executes @var{command},
8766 passing it @var{argument}s, returning its exit status.
8771 kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid}
8772 kill -l|-L [@var{exit_status}]
8775 Send a signal specified by @var{sigspec} or @var{signum} to the process
8776 named by job specification @var{jobspec} or process @sc{id} @var{pid}.
8777 @var{sigspec} is either a case-insensitive signal name such as
8778 @code{SIGINT} (with or without the @code{SIG} prefix)
8779 or a signal number; @var{signum} is a signal number.
8780 If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used.
8781 The @option{-l} option lists the signal names.
8782 If any arguments are supplied when @option{-l} is given, the names of the
8783 signals corresponding to the arguments are listed, and the return status
8785 @var{exit_status} is a number specifying a signal number or the exit
8786 status of a process terminated by a signal.
8787 The @option{-L} option is equivalent to @option{-l}.
8788 The return status is zero if at least one signal was successfully sent,
8789 or non-zero if an error occurs or an invalid option is encountered.
8794 wait [-fn] [-p @var{varname}] [@var{jobspec} or @var{pid} @dots{}]
8797 Wait until the child process specified by each process @sc{id} @var{pid}
8798 or job specification @var{jobspec} exits and return the exit status of the
8799 last command waited for.
8800 If a job spec is given, all processes in the job are waited for.
8801 If no arguments are given,
8802 @code{wait} waits for all running background jobs and
8803 the last-executed process substitution, if its process id is the same as
8805 and the return status is zero.
8806 If the @option{-n} option is supplied, @code{wait} waits for a single job
8807 from the list of @var{pid}s or @var{jobspec}s or, if no arguments are
8809 to complete and returns its exit status.
8810 If none of the supplied arguments is a child of the shell, or if no arguments
8811 are supplied and the shell has no unwaited-for children, the exit status
8813 If the @option{-p} option is supplied, the process or job identifier of the job
8814 for which the exit status is returned is assigned to the variable
8815 @var{varname} named by the option argument.
8816 The variable will be unset initially, before any assignment.
8817 This is useful only when the @option{-n} option is supplied.
8818 Supplying the @option{-f} option, when job control is enabled,
8819 forces @code{wait} to wait for each @var{pid} or @var{jobspec} to
8820 terminate before returning its status, intead of returning when it changes
8822 If neither @var{jobspec} nor @var{pid} specifies an active child process
8823 of the shell, the return status is 127.
8828 disown [-ar] [-h] [@var{jobspec} @dots{} | @var{pid} @dots{} ]
8831 Without options, remove each @var{jobspec} from the table of
8833 If the @option{-h} option is given, the job is not removed from the table,
8834 but is marked so that @code{SIGHUP} is not sent to the job if the shell
8835 receives a @code{SIGHUP}.
8836 If @var{jobspec} is not present, and neither the @option{-a} nor the
8837 @option{-r} option is supplied, the current job is used.
8838 If no @var{jobspec} is supplied, the @option{-a} option means to remove or
8839 mark all jobs; the @option{-r} option without a @var{jobspec}
8840 argument restricts operation to running jobs.
8848 Suspend the execution of this shell until it receives a
8849 @code{SIGCONT} signal.
8850 A login shell cannot be suspended; the @option{-f}
8851 option can be used to override this and force the suspension.
8854 When job control is not active, the @code{kill} and @code{wait}
8855 builtins do not accept @var{jobspec} arguments. They must be
8856 supplied process @sc{id}s.
8858 @node Job Control Variables
8859 @section Job Control Variables
8864 This variable controls how the shell interacts with the user and
8865 job control. If this variable exists then single word simple
8866 commands without redirections are treated as candidates for resumption
8867 of an existing job. There is no ambiguity allowed; if there is
8868 more than one job beginning with the string typed, then
8869 the most recently accessed job will be selected.
8870 The name of a stopped job, in this context, is the command line
8871 used to start it. If this variable is set to the value @samp{exact},
8872 the string supplied must match the name of a stopped job exactly;
8873 if set to @samp{substring},
8874 the string supplied needs to match a substring of the name of a
8875 stopped job. The @samp{substring} value provides functionality
8876 analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}).
8877 If set to any other value, the supplied string must
8878 be a prefix of a stopped job's name; this provides functionality
8879 analogous to the @samp{%} job @sc{id}.
8883 @set readline-appendix
8884 @set history-appendix
8885 @cindex Readline, how to use
8886 @include rluser.texi
8887 @cindex History, how to use
8888 @include hsuser.texi
8889 @clear readline-appendix
8890 @clear history-appendix
8892 @node Installing Bash
8893 @chapter Installing Bash
8895 This chapter provides basic instructions for installing Bash on
8896 the various supported platforms. The distribution supports the
8897 @sc{gnu} operating systems, nearly every version of Unix, and several
8898 non-Unix systems such as BeOS and Interix.
8899 Other independent ports exist for
8900 @sc{ms-dos}, @sc{os/2}, and Windows platforms.
8903 * Basic Installation:: Installation instructions.
8904 * Compilers and Options:: How to set special options for various
8906 * Compiling For Multiple Architectures:: How to compile Bash for more
8907 than one kind of system from
8908 the same source tree.
8909 * Installation Names:: How to set the various paths used by the installation.
8910 * Specifying the System Type:: How to configure Bash for a particular system.
8911 * Sharing Defaults:: How to share default configuration values among GNU
8913 * Operation Controls:: Options recognized by the configuration program.
8914 * Optional Features:: How to enable and disable optional features when
8918 @node Basic Installation
8919 @section Basic Installation
8920 @cindex installation
8921 @cindex configuration
8922 @cindex Bash installation
8923 @cindex Bash configuration
8925 These are installation instructions for Bash.
8927 The simplest way to compile Bash is:
8931 @code{cd} to the directory containing the source code and type
8932 @samp{./configure} to configure Bash for your system. If you're
8933 using @code{csh} on an old version of System V, you might need to
8934 type @samp{sh ./configure} instead to prevent @code{csh} from trying
8935 to execute @code{configure} itself.
8937 Running @code{configure} takes some time.
8938 While running, it prints messages telling which features it is
8942 Type @samp{make} to compile Bash and build the @code{bashbug} bug
8946 Optionally, type @samp{make tests} to run the Bash test suite.
8949 Type @samp{make install} to install @code{bash} and @code{bashbug}.
8950 This will also install the manual pages and Info file, message translation
8951 files, some supplemental documentation, a number of example loadable
8952 builtin commands, and a set of header files for developing loadable
8954 You may need additional privileges to install @code{bash} to your
8955 desired destination, so @samp{sudo make install} might be required.
8956 More information about controlling the locations where @code{bash} and
8957 other files are installed is below (@pxref{Installation Names}).
8961 The @code{configure} shell script attempts to guess correct
8962 values for various system-dependent variables used during
8963 compilation. It uses those values to create a @file{Makefile} in
8964 each directory of the package (the top directory, the
8965 @file{builtins}, @file{doc}, @file{po}, and @file{support} directories,
8966 each directory under @file{lib}, and several others). It also creates a
8967 @file{config.h} file containing system-dependent definitions.
8968 Finally, it creates a shell script named @code{config.status} that you
8969 can run in the future to recreate the current configuration, a
8970 file @file{config.cache} that saves the results of its tests to
8971 speed up reconfiguring, and a file @file{config.log} containing
8972 compiler output (useful mainly for debugging @code{configure}).
8974 @file{config.cache} contains results you don't want to keep, you
8975 may remove or edit it.
8977 To find out more about the options and arguments that the
8978 @code{configure} script understands, type
8981 bash-4.2$ ./configure --help
8985 at the Bash prompt in your Bash source directory.
8987 If you want to build Bash in a directory separate from the source
8988 directory -- to build for multiple architectures, for example --
8989 just use the full path to the configure script. The following commands
8990 will build bash in a directory under @file{/usr/local/build} from
8991 the source code in @file{/usr/local/src/bash-4.4}:
8994 mkdir /usr/local/build/bash-4.4
8995 cd /usr/local/build/bash-4.4
8996 bash /usr/local/src/bash-4.4/configure
9000 See @ref{Compiling For Multiple Architectures} for more information
9001 about building in a directory separate from the source.
9003 If you need to do unusual things to compile Bash, please
9004 try to figure out how @code{configure} could check whether or not
9005 to do them, and mail diffs or instructions to
9006 @email{bash-maintainers@@gnu.org} so they can be
9007 considered for the next release.
9009 The file @file{configure.ac} is used to create @code{configure}
9010 by a program called Autoconf.
9011 You only need @file{configure.ac} if you want to change it or regenerate
9012 @code{configure} using a newer version of Autoconf.
9013 If you do this, make sure you are using Autoconf version 2.69 or
9016 You can remove the program binaries and object files from the
9017 source code directory by typing @samp{make clean}. To also remove the
9018 files that @code{configure} created (so you can compile Bash for
9019 a different kind of computer), type @samp{make distclean}.
9021 @node Compilers and Options
9022 @section Compilers and Options
9024 Some systems require unusual options for compilation or linking
9025 that the @code{configure} script does not know about. You can
9026 give @code{configure} initial values for variables by setting
9027 them in the environment. Using a Bourne-compatible shell, you
9028 can do that on the command line like this:
9031 CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
9034 On systems that have the @code{env} program, you can do it like this:
9037 env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
9040 The configuration process uses GCC to build Bash if it
9043 @node Compiling For Multiple Architectures
9044 @section Compiling For Multiple Architectures
9046 You can compile Bash for more than one kind of computer at the
9047 same time, by placing the object files for each architecture in their
9048 own directory. To do this, you must use a version of @code{make} that
9049 supports the @code{VPATH} variable, such as GNU @code{make}.
9051 directory where you want the object files and executables to go and run
9052 the @code{configure} script from the source directory
9053 (@pxref{Basic Installation}).
9055 supply the @option{--srcdir=PATH} argument to tell @code{configure} where the
9056 source files are. @code{configure} automatically checks for the
9057 source code in the directory that @code{configure} is in and in `..'.
9059 If you have to use a @code{make} that does not supports the @code{VPATH}
9060 variable, you can compile Bash for one architecture at a
9061 time in the source code directory. After you have installed
9062 Bash for one architecture, use @samp{make distclean} before
9063 reconfiguring for another architecture.
9065 Alternatively, if your system supports symbolic links, you can use the
9066 @file{support/mkclone} script to create a build tree which has
9067 symbolic links back to each file in the source directory. Here's an
9068 example that creates a build directory in the current directory from a
9069 source directory @file{/usr/gnu/src/bash-2.0}:
9072 bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 .
9076 The @code{mkclone} script requires Bash, so you must have already built
9077 Bash for at least one architecture before you can create build
9078 directories for other architectures.
9080 @node Installation Names
9081 @section Installation Names
9083 By default, @samp{make install} will install into
9084 @file{/usr/local/bin}, @file{/usr/local/man}, etc.;
9085 that is, the @dfn{installation prefix} defaults to @file{/usr/local}.
9086 You can specify an installation prefix other than @file{/usr/local} by
9087 giving @code{configure} the option @option{--prefix=@var{PATH}},
9088 or by specifying a value for the @env{prefix} @samp{make}
9089 variable when running @samp{make install}
9090 (e.g., @samp{make install prefix=@var{PATH}}).
9091 The @env{prefix} variable provides a default for @env{exec_prefix} and
9092 other variables used when installing bash.
9094 You can specify separate installation prefixes for
9095 architecture-specific files and architecture-independent files.
9096 If you give @code{configure} the option
9097 @option{--exec-prefix=@var{PATH}}, @samp{make install} will use
9098 @var{PATH} as the prefix for installing programs and libraries.
9099 Documentation and other data files will still use the regular prefix.
9101 If you would like to change the installation locations for a single run,
9102 you can specify these variables as arguments to @code{make}:
9103 @samp{make install exec_prefix=/} will install @code{bash} and
9104 @code{bashbug} into @file{/bin} instead of the default @file{/usr/local/bin}.
9106 If you want to see the files bash will install and where it will install
9107 them without changing anything on your system, specify the variable
9108 @env{DESTDIR} as an argument to @code{make}. Its value should be the
9109 absolute directory path you'd like to use as the root of your sample
9110 installation tree. For example,
9113 mkdir /fs1/bash-install
9114 make install DESTDIR=/fs1/bash-install
9118 will install @code{bash} into @file{/fs1/bash-install/usr/local/bin/bash},
9119 the documentation into directories within
9120 @file{/fs1/bash-install/usr/local/share}, the example loadable builtins into
9121 @file{/fs1/bash-install/usr/local/lib/bash}, and so on.
9122 You can use the usual @env{exec_prefix} and @env{prefix} variables to alter
9123 the directory paths beneath the value of @env{DESTDIR}.
9125 The GNU Makefile standards provide a more complete description of these
9126 variables and their effects.
9128 @node Specifying the System Type
9129 @section Specifying the System Type
9131 There may be some features @code{configure} can not figure out
9132 automatically, but need to determine by the type of host Bash
9133 will run on. Usually @code{configure} can figure that
9134 out, but if it prints a message saying it can not guess the host
9135 type, give it the @option{--host=TYPE} option. @samp{TYPE} can
9136 either be a short name for the system type, such as @samp{sun4},
9137 or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM}
9138 (e.g., @samp{i386-unknown-freebsd4.2}).
9140 See the file @file{support/config.sub} for the possible
9141 values of each field.
9143 @node Sharing Defaults
9144 @section Sharing Defaults
9146 If you want to set default values for @code{configure} scripts to
9147 share, you can create a site shell script called
9148 @code{config.site} that gives default values for variables like
9149 @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure}
9150 looks for @file{PREFIX/share/config.site} if it exists, then
9151 @file{PREFIX/etc/config.site} if it exists. Or, you can set the
9152 @code{CONFIG_SITE} environment variable to the location of the site
9153 script. A warning: the Bash @code{configure} looks for a site script,
9154 but not all @code{configure} scripts do.
9156 @node Operation Controls
9157 @section Operation Controls
9159 @code{configure} recognizes the following options to control how it
9164 @item --cache-file=@var{file}
9165 Use and save the results of the tests in
9166 @var{file} instead of @file{./config.cache}. Set @var{file} to
9167 @file{/dev/null} to disable caching, for debugging
9171 Print a summary of the options to @code{configure}, and exit.
9176 Do not print messages saying which checks are being made.
9178 @item --srcdir=@var{dir}
9179 Look for the Bash source code in directory @var{dir}. Usually
9180 @code{configure} can determine that directory automatically.
9183 Print the version of Autoconf used to generate the @code{configure}
9187 @code{configure} also accepts some other, not widely used, boilerplate
9188 options. @samp{configure --help} prints the complete list.
9190 @node Optional Features
9191 @section Optional Features
9193 The Bash @code{configure} has a number of @option{--enable-@var{feature}}
9194 options, where @var{feature} indicates an optional part of Bash.
9195 There are also several @option{--with-@var{package}} options,
9196 where @var{package} is something like @samp{bash-malloc} or @samp{purify}.
9197 To turn off the default use of a package, use
9198 @option{--without-@var{package}}. To configure Bash without a feature
9199 that is enabled by default, use @option{--disable-@var{feature}}.
9201 Here is a complete list of the @option{--enable-} and
9202 @option{--with-} options that the Bash @code{configure} recognizes.
9206 Define if you are using the Andrew File System from Transarc.
9208 @item --with-bash-malloc
9209 Use the Bash version of
9210 @code{malloc} in the directory @file{lib/malloc}. This is not the same
9211 @code{malloc} that appears in @sc{gnu} libc, but an older version
9212 originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc}
9213 is very fast, but wastes some space on each allocation.
9214 This option is enabled by default.
9215 The @file{NOTES} file contains a list of systems for
9216 which this should be turned off, and @code{configure} disables this
9217 option automatically for a number of systems.
9220 Use the curses library instead of the termcap library. This should
9221 be supplied if your system has an inadequate or incomplete termcap
9224 @item --with-gnu-malloc
9225 A synonym for @code{--with-bash-malloc}.
9227 @item --with-installed-readline[=@var{PREFIX}]
9228 Define this to make Bash link with a locally-installed version of Readline
9229 rather than the version in @file{lib/readline}. This works only with
9230 Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not
9231 supplied, @code{configure} uses the values of the make variables
9232 @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix}
9233 by default, to find the installed version of Readline if it is not in
9234 the standard system include and library directories.
9235 If @var{PREFIX} is @code{no}, Bash links with the version in
9236 @file{lib/readline}.
9237 If @var{PREFIX} is set to any other value, @code{configure} treats it as
9238 a directory pathname and looks for
9239 the installed version of Readline in subdirectories of that directory
9240 (include files in @var{PREFIX}/@code{include} and the library in
9241 @var{PREFIX}/@code{lib}).
9243 @item --with-libintl-prefix[=@var{PREFIX}]
9244 Define this to make Bash link with a locally-installed version of the
9245 libintl library instead ofthe version in @file{lib/intl}.
9247 @item --with-libiconv-prefix[=@var{PREFIX}]
9248 Define this to make Bash look for libiconv in @var{PREFIX} instead of the
9249 standard system locations. There is no version included with Bash.
9251 @item --enable-minimal-config
9252 This produces a shell with minimal features, close to the historical
9256 There are several @option{--enable-} options that alter how Bash is
9257 compiled, linked, and installed, rather than changing run-time features.
9260 @item --enable-largefile
9261 Enable support for @uref{http://www.unix.org/version2/whatsnew/lfs20mar.html,
9262 large files} if the operating system requires special compiler options
9263 to build programs which can access large files. This is enabled by
9264 default, if the operating system provides large file support.
9266 @item --enable-profiling
9267 This builds a Bash binary that produces profiling information to be
9268 processed by @code{gprof} each time it is executed.
9270 @item --enable-separate-helpfiles
9271 Use external files for the documentation displayed by the @code{help} builtin
9272 instead of storing the text internally.
9274 @item --enable-static-link
9275 This causes Bash to be linked statically, if @code{gcc} is being used.
9276 This could be used to build a version to use as root's shell.
9280 The @samp{minimal-config} option can be used to disable all of
9281 the following options, but it is processed first, so individual
9282 options may be enabled using @samp{enable-@var{feature}}.
9284 All of the following options except for
9285 @samp{alt-array-implementation},
9286 @samp{disabled-builtins},
9287 @samp{direxpand-default},
9288 @samp{strict-posix-default},
9290 @samp{xpg-echo-default} are
9291 enabled by default, unless the operating system does not provide the
9295 @item --enable-alias
9296 Allow alias expansion and include the @code{alias} and @code{unalias}
9297 builtins (@pxref{Aliases}).
9299 @item --enable-alt-array-implementation
9300 This builds bash using an alternate implementation of arrays
9301 (@pxref{Arrays}) that provides faster access at the expense of using
9302 more memory (sometimes many times more, depending on how sparse an array is).
9304 @item --enable-arith-for-command
9305 Include support for the alternate form of the @code{for} command
9306 that behaves like the C language @code{for} statement
9307 (@pxref{Looping Constructs}).
9309 @item --enable-array-variables
9310 Include support for one-dimensional array shell variables
9313 @item --enable-bang-history
9314 Include support for @code{csh}-like history substitution
9315 (@pxref{History Interaction}).
9317 @item --enable-brace-expansion
9318 Include @code{csh}-like brace expansion
9319 ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ).
9320 See @ref{Brace Expansion}, for a complete description.
9322 @item --enable-casemod-attributes
9323 Include support for case-modifying attributes in the @code{declare} builtin
9324 and assignment statements. Variables with the @code{uppercase} attribute,
9325 for example, will have their values converted to uppercase upon assignment.
9327 @item --enable-casemod-expansion
9328 Include support for case-modifying word expansions.
9330 @item --enable-command-timing
9331 Include support for recognizing @code{time} as a reserved word and for
9332 displaying timing statistics for the pipeline following @code{time}
9333 (@pxref{Pipelines}).
9334 This allows pipelines as well as shell builtins and functions to be timed.
9336 @item --enable-cond-command
9337 Include support for the @code{[[} conditional command.
9338 (@pxref{Conditional Constructs}).
9340 @item --enable-cond-regexp
9341 Include support for matching @sc{posix} regular expressions using the
9342 @samp{=~} binary operator in the @code{[[} conditional command.
9343 (@pxref{Conditional Constructs}).
9345 @item --enable-coprocesses
9346 Include support for coprocesses and the @code{coproc} reserved word
9347 (@pxref{Pipelines}).
9349 @item --enable-debugger
9350 Include support for the bash debugger (distributed separately).
9352 @item --enable-dev-fd-stat-broken
9353 If calling @code{stat} on /dev/fd/@var{N} returns different results than
9354 calling @code{fstat} on file descriptor @var{N}, supply this option to
9355 enable a workaround.
9356 This has implications for conditional commands that test file attributes.
9358 @item --enable-direxpand-default
9359 Cause the @code{direxpand} shell option (@pxref{The Shopt Builtin})
9360 to be enabled by default when the shell starts.
9361 It is normally disabled by default.
9363 @item --enable-directory-stack
9364 Include support for a @code{csh}-like directory stack and the
9365 @code{pushd}, @code{popd}, and @code{dirs} builtins
9366 (@pxref{The Directory Stack}).
9368 @item --enable-disabled-builtins
9369 Allow builtin commands to be invoked via @samp{builtin xxx}
9370 even after @code{xxx} has been disabled using @samp{enable -n xxx}.
9371 See @ref{Bash Builtins}, for details of the @code{builtin} and
9372 @code{enable} builtin commands.
9374 @item --enable-dparen-arithmetic
9375 Include support for the @code{((@dots{}))} command
9376 (@pxref{Conditional Constructs}).
9378 @item --enable-extended-glob
9379 Include support for the extended pattern matching features described
9380 above under @ref{Pattern Matching}.
9382 @item --enable-extended-glob-default
9383 Set the default value of the @code{extglob} shell option described
9384 above under @ref{The Shopt Builtin} to be enabled.
9386 @item --enable-function-import
9387 Include support for importing function definitions exported by another
9388 instance of the shell from the environment. This option is enabled by
9391 @item --enable-glob-asciirange-default
9392 Set the default value of the @code{globasciiranges} shell option described
9393 above under @ref{The Shopt Builtin} to be enabled.
9394 This controls the behavior of character ranges when used in pattern matching
9395 bracket expressions.
9397 @item --enable-help-builtin
9398 Include the @code{help} builtin, which displays help on shell builtins and
9399 variables (@pxref{Bash Builtins}).
9401 @item --enable-history
9402 Include command history and the @code{fc} and @code{history}
9403 builtin commands (@pxref{Bash History Facilities}).
9405 @item --enable-job-control
9406 This enables the job control features (@pxref{Job Control}),
9407 if the operating system supports them.
9409 @item --enable-multibyte
9410 This enables support for multibyte characters if the operating
9411 system provides the necessary support.
9413 @item --enable-net-redirections
9414 This enables the special handling of filenames of the form
9415 @code{/dev/tcp/@var{host}/@var{port}} and
9416 @code{/dev/udp/@var{host}/@var{port}}
9417 when used in redirections (@pxref{Redirections}).
9419 @item --enable-process-substitution
9420 This enables process substitution (@pxref{Process Substitution}) if
9421 the operating system provides the necessary support.
9423 @item --enable-progcomp
9424 Enable the programmable completion facilities
9425 (@pxref{Programmable Completion}).
9426 If Readline is not enabled, this option has no effect.
9428 @item --enable-prompt-string-decoding
9429 Turn on the interpretation of a number of backslash-escaped characters
9430 in the @env{$PS0}, @env{$PS1}, @env{$PS2}, and @env{$PS4} prompt
9431 strings. See @ref{Controlling the Prompt}, for a complete list of prompt
9432 string escape sequences.
9434 @item --enable-readline
9435 Include support for command-line editing and history with the Bash
9436 version of the Readline library (@pxref{Command Line Editing}).
9438 @item --enable-restricted
9439 Include support for a @dfn{restricted shell}. If this is enabled, Bash,
9440 when called as @code{rbash}, enters a restricted mode. See
9441 @ref{The Restricted Shell}, for a description of restricted mode.
9443 @item --enable-select
9444 Include the @code{select} compound command, which allows the generation of
9445 simple menus (@pxref{Conditional Constructs}).
9447 @item --enable-single-help-strings
9448 Store the text displayed by the @code{help} builtin as a single string for
9449 each help topic. This aids in translating the text to different languages.
9450 You may need to disable this if your compiler cannot handle very long string
9453 @item --enable-strict-posix-default
9454 Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}).
9456 @item --enable-translatable-strings
9457 Enable support for @code{$"@var{string}"} translatable strings
9458 (@pxref{Locale Translation}).
9460 @item --enable-usg-echo-default
9461 A synonym for @code{--enable-xpg-echo-default}.
9463 @item --enable-xpg-echo-default
9464 Make the @code{echo} builtin expand backslash-escaped characters by default,
9465 without requiring the @option{-e} option.
9466 This sets the default value of the @code{xpg_echo} shell option to @code{on},
9467 which makes the Bash @code{echo} behave more like the version specified in
9468 the Single Unix Specification, version 3.
9469 @xref{Bash Builtins}, for a description of the escape sequences that
9470 @code{echo} recognizes.
9473 The file @file{config-top.h} contains C Preprocessor
9474 @samp{#define} statements for options which are not settable from
9476 Some of these are not meant to be changed; beware of the consequences if
9478 Read the comments associated with each definition for more
9479 information about its effect.
9481 @node Reporting Bugs
9482 @appendix Reporting Bugs
9484 Please report all bugs you find in Bash.
9485 But first, you should
9486 make sure that it really is a bug, and that it appears in the latest
9488 The latest version of Bash is always available for FTP from
9489 @uref{ftp://ftp.gnu.org/pub/gnu/bash/} and from
9490 @uref{http://git.savannah.gnu.org/cgit/bash.git/snapshot/bash-master.tar.gz}.
9492 Once you have determined that a bug actually exists, use the
9493 @code{bashbug} command to submit a bug report.
9494 If you have a fix, you are encouraged to mail that as well!
9495 Suggestions and `philosophical' bug reports may be mailed
9496 to @email{bug-bash@@gnu.org} or posted to the Usenet
9497 newsgroup @code{gnu.bash.bug}.
9499 All bug reports should include:
9502 The version number of Bash.
9504 The hardware and operating system.
9506 The compiler used to compile Bash.
9508 A description of the bug behaviour.
9510 A short script or `recipe' which exercises the bug and may be used
9515 @code{bashbug} inserts the first three items automatically into
9516 the template it provides for filing a bug report.
9518 Please send all reports concerning this manual to
9519 @email{bug-bash@@gnu.org}.
9521 @node Major Differences From The Bourne Shell
9522 @appendix Major Differences From The Bourne Shell
9524 Bash implements essentially the same grammar, parameter and
9525 variable expansion, redirection, and quoting as the Bourne Shell.
9526 Bash uses the @sc{posix} standard as the specification of
9527 how these features are to be implemented. There are some
9528 differences between the traditional Bourne shell and Bash; this
9529 section quickly details the differences of significance. A
9530 number of these differences are explained in greater depth in
9532 This section uses the version of @code{sh} included in SVR4.2 (the
9533 last version of the historical Bourne shell) as the baseline reference.
9538 Bash is @sc{posix}-conformant, even where the @sc{posix} specification
9539 differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}).
9542 Bash has multi-character invocation options (@pxref{Invoking Bash}).
9545 Bash has command-line editing (@pxref{Command Line Editing}) and
9546 the @code{bind} builtin.
9549 Bash provides a programmable word completion mechanism
9550 (@pxref{Programmable Completion}), and builtin commands
9551 @code{complete}, @code{compgen}, and @code{compopt}, to
9555 Bash has command history (@pxref{Bash History Facilities}) and the
9556 @code{history} and @code{fc} builtins to manipulate it.
9557 The Bash history list maintains timestamp information and uses the
9558 value of the @code{HISTTIMEFORMAT} variable to display it.
9561 Bash implements @code{csh}-like history expansion
9562 (@pxref{History Interaction}).
9565 Bash has one-dimensional array variables (@pxref{Arrays}), and the
9566 appropriate variable expansions and assignment syntax to use them.
9567 Several of the Bash builtins take options to act on arrays.
9568 Bash provides a number of built-in array variables.
9571 The @code{$'@dots{}'} quoting syntax, which expands ANSI-C
9572 backslash-escaped characters in the text between the single quotes,
9573 is supported (@pxref{ANSI-C Quoting}).
9576 Bash supports the @code{$"@dots{}"} quoting syntax to do
9577 locale-specific translation of the characters between the double
9578 quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings}
9579 invocation options list the translatable strings found in a script
9580 (@pxref{Locale Translation}).
9583 Bash implements the @code{!} keyword to negate the return value of
9584 a pipeline (@pxref{Pipelines}).
9585 Very useful when an @code{if} statement needs to act only if a test fails.
9586 The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to
9587 return a failure status if any command fails.
9590 Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}).
9591 The display of the timing statistics may be controlled with the
9592 @env{TIMEFORMAT} variable.
9595 Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))}
9596 arithmetic for command, similar to the C language (@pxref{Looping Constructs}).
9599 Bash includes the @code{select} compound command, which allows the
9600 generation of simple menus (@pxref{Conditional Constructs}).
9603 Bash includes the @code{[[} compound command, which makes conditional
9604 testing part of the shell grammar (@pxref{Conditional Constructs}), including
9605 optional regular expression matching.
9608 Bash provides optional case-insensitive matching for the @code{case} and
9609 @code{[[} constructs.
9612 Bash includes brace expansion (@pxref{Brace Expansion}) and tilde
9613 expansion (@pxref{Tilde Expansion}).
9616 Bash implements command aliases and the @code{alias} and @code{unalias}
9617 builtins (@pxref{Aliases}).
9620 Bash provides shell arithmetic, the @code{((} compound command
9621 (@pxref{Conditional Constructs}),
9622 and arithmetic expansion (@pxref{Shell Arithmetic}).
9625 Variables present in the shell's initial environment are automatically
9626 exported to child processes. The Bourne shell does not normally do
9627 this unless the variables are explicitly marked using the @code{export}
9631 Bash supports the @samp{+=} assignment operator, which appends to the value
9632 of the variable named on the left hand side.
9635 Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%}
9636 and @samp{##} expansions to remove leading or trailing substrings from
9637 variable values (@pxref{Shell Parameter Expansion}).
9640 The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}},
9641 is supported (@pxref{Shell Parameter Expansion}).
9644 The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}},
9645 which expands to the substring of @code{var}'s value of length
9646 @var{length}, beginning at @var{offset}, is present
9647 (@pxref{Shell Parameter Expansion}).
9651 @code{$@{@var{var}/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}},
9652 which matches @var{pattern} and replaces it with @var{replacement} in
9653 the value of @var{var}, is available (@pxref{Shell Parameter Expansion}).
9656 The expansion @code{$@{!@var{prefix}*@}} expansion, which expands to
9657 the names of all shell variables whose names begin with @var{prefix},
9658 is available (@pxref{Shell Parameter Expansion}).
9661 Bash has indirect variable expansion using @code{$@{!word@}}
9662 (@pxref{Shell Parameter Expansion}).
9665 Bash can expand positional parameters beyond @code{$9} using
9666 @code{$@{@var{num}@}}.
9669 The @sc{posix} @code{$()} form of command substitution
9670 is implemented (@pxref{Command Substitution}),
9671 and preferred to the Bourne shell's @code{``} (which
9672 is also implemented for backwards compatibility).
9675 Bash has process substitution (@pxref{Process Substitution}).
9678 Bash automatically assigns variables that provide information about the
9679 current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host
9680 (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}),
9681 and the instance of Bash that is running (@env{BASH},
9682 @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables},
9686 The @env{IFS} variable is used to split only the results of expansion,
9687 not all words (@pxref{Word Splitting}).
9688 This closes a longstanding shell security hole.
9691 The filename expansion bracket expression code uses @samp{!} and @samp{^}
9692 to negate the set of characters between the brackets.
9693 The Bourne shell uses only @samp{!}.
9696 Bash implements the full set of @sc{posix} filename expansion operators,
9697 including character classes, equivalence classes, and
9698 collating symbols (@pxref{Filename Expansion}).
9701 Bash implements extended pattern matching features when the @code{extglob}
9702 shell option is enabled (@pxref{Pattern Matching}).
9705 It is possible to have a variable and a function with the same name;
9706 @code{sh} does not separate the two name spaces.
9709 Bash functions are permitted to have local variables using the
9710 @code{local} builtin, and thus useful recursive functions may be written
9711 (@pxref{Bash Builtins}).
9714 Variable assignments preceding commands affect only that command, even
9715 builtins and functions (@pxref{Environment}).
9716 In @code{sh}, all variable assignments
9717 preceding commands are global unless the command is executed from the
9721 Bash performs filename expansion on filenames specified as operands
9722 to input and output redirection operators (@pxref{Redirections}).
9725 Bash contains the @samp{<>} redirection operator, allowing a file to be
9726 opened for both reading and writing, and the @samp{&>} redirection
9727 operator, for directing standard output and standard error to the same
9728 file (@pxref{Redirections}).
9731 Bash includes the @samp{<<<} redirection operator, allowing a string to
9732 be used as the standard input to a command.
9735 Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}}
9736 redirection operators, which move one file descriptor to another.
9739 Bash treats a number of filenames specially when they are
9740 used in redirection operators (@pxref{Redirections}).
9743 Bash can open network connections to arbitrary machines and services
9744 with the redirection operators (@pxref{Redirections}).
9747 The @code{noclobber} option is available to avoid overwriting existing
9748 files with output redirection (@pxref{The Set Builtin}).
9749 The @samp{>|} redirection operator may be used to override @code{noclobber}.
9752 The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins})
9753 each take @option{-L} and @option{-P} options to switch between logical and
9757 Bash allows a function to override a builtin with the same name, and provides
9758 access to that builtin's functionality within the function via the
9759 @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}).
9762 The @code{command} builtin allows selective disabling of functions
9763 when command lookup is performed (@pxref{Bash Builtins}).
9766 Individual builtins may be enabled or disabled using the @code{enable}
9767 builtin (@pxref{Bash Builtins}).
9770 The Bash @code{exec} builtin takes additional options that allow users
9771 to control the contents of the environment passed to the executed
9772 command, and what the zeroth argument to the command is to be
9773 (@pxref{Bourne Shell Builtins}).
9776 Shell functions may be exported to children via the environment
9777 using @code{export -f} (@pxref{Shell Functions}).
9780 The Bash @code{export}, @code{readonly}, and @code{declare} builtins can
9781 take a @option{-f} option to act on shell functions, a @option{-p} option to
9782 display variables with various attributes set in a format that can be
9783 used as shell input, a @option{-n} option to remove various variable
9784 attributes, and @samp{name=value} arguments to set variable attributes
9785 and values simultaneously.
9788 The Bash @code{hash} builtin allows a name to be associated with
9789 an arbitrary filename, even when that filename cannot be found by
9790 searching the @env{$PATH}, using @samp{hash -p}
9791 (@pxref{Bourne Shell Builtins}).
9794 Bash includes a @code{help} builtin for quick reference to shell
9795 facilities (@pxref{Bash Builtins}).
9798 The @code{printf} builtin is available to display formatted output
9799 (@pxref{Bash Builtins}).
9802 The Bash @code{read} builtin (@pxref{Bash Builtins})
9803 will read a line ending in @samp{\} with
9804 the @option{-r} option, and will use the @env{REPLY} variable as a
9805 default if no non-option arguments are supplied.
9806 The Bash @code{read} builtin
9807 also accepts a prompt string with the @option{-p} option and will use
9808 Readline to obtain the line when given the @option{-e} option.
9809 The @code{read} builtin also has additional options to control input:
9810 the @option{-s} option will turn off echoing of input characters as
9811 they are read, the @option{-t} option will allow @code{read} to time out
9812 if input does not arrive within a specified number of seconds, the
9813 @option{-n} option will allow reading only a specified number of
9814 characters rather than a full line, and the @option{-d} option will read
9815 until a particular character rather than newline.
9818 The @code{return} builtin may be used to abort execution of scripts
9819 executed with the @code{.} or @code{source} builtins
9820 (@pxref{Bourne Shell Builtins}).
9823 Bash includes the @code{shopt} builtin, for finer control of shell
9824 optional capabilities (@pxref{The Shopt Builtin}), and allows these options
9825 to be set and unset at shell invocation (@pxref{Invoking Bash}).
9828 Bash has much more optional behavior controllable with the @code{set}
9829 builtin (@pxref{The Set Builtin}).
9832 The @samp{-x} (@option{xtrace}) option displays commands other than
9833 simple commands when performing an execution trace
9834 (@pxref{The Set Builtin}).
9837 The @code{test} builtin (@pxref{Bourne Shell Builtins})
9838 is slightly different, as it implements the @sc{posix} algorithm,
9839 which specifies the behavior based on the number of arguments.
9842 Bash includes the @code{caller} builtin, which displays the context of
9843 any active subroutine call (a shell function or a script executed with
9844 the @code{.} or @code{source} builtins). This supports the Bash
9848 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9849 @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}.
9850 Commands specified with a @code{DEBUG} trap are executed before every
9851 simple command, @code{for} command, @code{case} command,
9852 @code{select} command, every arithmetic @code{for} command, and before
9853 the first command executes in a shell function.
9854 The @code{DEBUG} trap is not inherited by shell functions unless the
9855 function has been given the @code{trace} attribute or the
9856 @code{functrace} option has been enabled using the @code{shopt} builtin.
9857 The @code{extdebug} shell option has additional effects on the
9860 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an
9861 @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}.
9862 Commands specified with an @code{ERR} trap are executed after a simple
9863 command fails, with a few exceptions.
9864 The @code{ERR} trap is not inherited by shell functions unless the
9865 @code{-o errtrace} option to the @code{set} builtin is enabled.
9867 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9868 @code{RETURN} pseudo-signal specification, similar to
9869 @code{EXIT} and @code{DEBUG}.
9870 Commands specified with an @code{RETURN} trap are executed before
9871 execution resumes after a shell function or a shell script executed with
9872 @code{.} or @code{source} returns.
9873 The @code{RETURN} trap is not inherited by shell functions unless the
9874 function has been given the @code{trace} attribute or the
9875 @code{functrace} option has been enabled using the @code{shopt} builtin.
9878 The Bash @code{type} builtin is more extensive and gives more information
9879 about the names it finds (@pxref{Bash Builtins}).
9882 The Bash @code{umask} builtin permits a @option{-p} option to cause
9883 the output to be displayed in the form of a @code{umask} command
9884 that may be reused as input (@pxref{Bourne Shell Builtins}).
9887 Bash implements a @code{csh}-like directory stack, and provides the
9888 @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it
9889 (@pxref{The Directory Stack}).
9890 Bash also makes the directory stack visible as the value of the
9891 @env{DIRSTACK} shell variable.
9894 Bash interprets special backslash-escaped characters in the prompt
9895 strings when interactive (@pxref{Controlling the Prompt}).
9898 The Bash restricted mode is more useful (@pxref{The Restricted Shell});
9899 the SVR4.2 shell restricted mode is too limited.
9902 The @code{disown} builtin can remove a job from the internal shell
9903 job table (@pxref{Job Control Builtins}) or suppress the sending
9904 of @code{SIGHUP} to a job when the shell exits as the result of a
9908 Bash includes a number of features to support a separate debugger for
9912 The SVR4.2 shell has two privilege-related builtins
9913 (@code{mldmode} and @code{priv}) not present in Bash.
9916 Bash does not have the @code{stop} or @code{newgrp} builtins.
9919 Bash does not use the @env{SHACCT} variable or perform shell accounting.
9922 The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses
9928 More features unique to Bash may be found in @ref{Bash Features}.
9931 @appendixsec Implementation Differences From The SVR4.2 Shell
9933 Since Bash is a completely new implementation, it does not suffer from
9934 many of the limitations of the SVR4.2 shell. For instance:
9939 Bash does not fork a subshell when redirecting into or out of
9940 a shell control structure such as an @code{if} or @code{while}
9944 Bash does not allow unbalanced quotes. The SVR4.2 shell will silently
9945 insert a needed closing quote at @code{EOF} under certain circumstances.
9946 This can be the cause of some hard-to-find errors.
9949 The SVR4.2 shell uses a baroque memory management scheme based on
9950 trapping @code{SIGSEGV}. If the shell is started from a process with
9951 @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library
9952 function call), it misbehaves badly.
9955 In a questionable attempt at security, the SVR4.2 shell,
9956 when invoked without the @option{-p} option, will alter its real
9957 and effective @sc{uid} and @sc{gid} if they are less than some
9958 magic threshold value, commonly 100.
9959 This can lead to unexpected results.
9962 The SVR4.2 shell does not allow users to trap @code{SIGSEGV},
9963 @code{SIGALRM}, or @code{SIGCHLD}.
9966 The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK},
9967 @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset.
9970 The SVR4.2 shell treats @samp{^} as the undocumented equivalent of
9974 Bash allows multiple option arguments when it is invoked (@code{-x -v});
9975 the SVR4.2 shell allows only one option argument (@code{-xv}). In
9976 fact, some versions of the shell dump core if the second argument begins
9980 The SVR4.2 shell exits a script if any builtin fails; Bash exits
9981 a script only if one of the @sc{posix} special builtins fails, and
9982 only for certain failures, as enumerated in the @sc{posix} standard.
9985 The SVR4.2 shell behaves differently when invoked as @code{jsh}
9986 (it turns on job control).
9989 @node GNU Free Documentation License
9990 @appendix GNU Free Documentation License
9998 * Builtin Index:: Index of Bash builtin commands.
9999 * Reserved Word Index:: Index of Bash reserved words.
10000 * Variable Index:: Quick reference helps you find the
10002 * Function Index:: Index of bindable Readline functions.
10003 * Concept Index:: General index for concepts described in
10007 @node Builtin Index
10008 @appendixsec Index of Shell Builtin Commands
10011 @node Reserved Word Index
10012 @appendixsec Index of Shell Reserved Words
10015 @node Variable Index
10016 @appendixsec Parameter and Variable Index
10019 @node Function Index
10020 @appendixsec Function Index
10023 @node Concept Index
10024 @appendixsec Concept Index