1 \input texinfo.tex @c -*- texinfo -*-
3 @setfilename bashref.info
4 @settitle Bash Reference Manual
12 This text is a brief description of the features that are present in
13 the Bash shell (version @value{VERSION}, @value{UPDATED}).
15 This is Edition @value{EDITION}, last updated @value{UPDATED},
16 of @cite{The GNU Bash Reference Manual},
17 for @code{Bash}, Version @value{VERSION}.
19 Copyright @copyright{} 1988--2011 Free Software Foundation, Inc.
21 Permission is granted to make and distribute verbatim copies of
22 this manual provided the copyright notice and this permission notice
23 are preserved on all copies.
26 Permission is granted to copy, distribute and/or modify this document
27 under the terms of the GNU Free Documentation License, Version 1.3 or
28 any later version published by the Free Software Foundation; with no
29 Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
30 and with the Back-Cover Texts as in (a) below. A copy of the license is
31 included in the section entitled ``GNU Free Documentation License''.
33 (a) The FSF's Back-Cover Text is: You are free to copy and modify
34 this GNU manual. Buying copies from GNU Press supports the FSF in
35 developing GNU and promoting software freedom.''
46 * Bash: (bash). The GNU Bourne-Again SHell.
52 @title Bash Reference Manual
53 @subtitle Reference Documentation for Bash
54 @subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}.
55 @subtitle @value{UPDATED-MONTH}
56 @author Chet Ramey, Case Western Reserve University
57 @author Brian Fox, Free Software Foundation
60 @vskip 0pt plus 1filll
64 Published by the Free Software Foundation @*
65 59 Temple Place, Suite 330, @*
66 Boston, MA 02111-1307 @*
74 @node Top, Introduction, (dir), (dir)
77 This text is a brief description of the features that are present in
78 the Bash shell (version @value{VERSION}, @value{UPDATED}).
80 This is Edition @value{EDITION}, last updated @value{UPDATED},
81 of @cite{The GNU Bash Reference Manual},
82 for @code{Bash}, Version @value{VERSION}.
84 Bash contains features that appear in other popular shells, and some
85 features that only appear in Bash. Some of the shells that Bash has
86 borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell
87 (@file{ksh}), and the C-shell (@file{csh} and its successor,
88 @file{tcsh}). The following menu breaks the features up into
89 categories based upon which one of these other shells inspired the
92 This manual is meant as a brief introduction to features found in
93 Bash. The Bash manual page should be used as the definitive
94 reference on shell behavior.
97 * Introduction:: An introduction to the shell.
98 * Definitions:: Some definitions used in the rest of this
100 * Basic Shell Features:: The shell "building blocks".
101 * Shell Builtin Commands:: Commands that are a part of the shell.
102 * Shell Variables:: Variables used or set by Bash.
103 * Bash Features:: Features found only in Bash.
104 * Job Control:: What job control is and how Bash allows you
106 * Command Line Editing:: Chapter describing the command line
108 * Using History Interactively:: Command History Expansion
109 * Installing Bash:: How to build and install Bash on your system.
110 * Reporting Bugs:: How to report bugs in Bash.
111 * Major Differences From The Bourne Shell:: A terse list of the differences
112 between Bash and historical
114 * GNU Free Documentation License:: Copying and sharing this documentation.
115 * Indexes:: Various indexes for this manual.
120 @chapter Introduction
122 * What is Bash?:: A short description of Bash.
123 * What is a shell?:: A brief introduction to shells.
127 @section What is Bash?
129 Bash is the shell, or command language interpreter,
130 for the @sc{gnu} operating system.
131 The name is an acronym for the @samp{Bourne-Again SHell},
132 a pun on Stephen Bourne, the author of the direct ancestor of
133 the current Unix shell @code{sh},
134 which appeared in the Seventh Edition Bell Labs Research version
137 Bash is largely compatible with @code{sh} and incorporates useful
138 features from the Korn shell @code{ksh} and the C shell @code{csh}.
139 It is intended to be a conformant implementation of the @sc{ieee}
140 @sc{posix} Shell and Tools portion of the @sc{ieee} @sc{posix}
141 specification (@sc{ieee} Standard 1003.1).
142 It offers functional improvements over @code{sh} for both interactive and
145 While the @sc{gnu} operating system provides other shells, including
146 a version of @code{csh}, Bash is the default shell.
147 Like other @sc{gnu} software, Bash is quite portable. It currently runs
148 on nearly every version of Unix and a few other operating systems @minus{}
149 independently-supported ports exist for @sc{ms-dos}, @sc{os/2},
150 and Windows platforms.
152 @node What is a shell?
153 @section What is a shell?
155 At its base, a shell is simply a macro processor that executes
156 commands. The term macro processor means functionality where text
157 and symbols are expanded to create larger expressions.
159 A Unix shell is both a command interpreter and a programming
160 language. As a command interpreter, the shell provides the user
161 interface to the rich set of @sc{gnu} utilities. The programming
162 language features allow these utilities to be combined.
163 Files containing commands can be created, and become
164 commands themselves. These new commands have the same status as
165 system commands in directories such as @file{/bin}, allowing users
166 or groups to establish custom environments to automate their common
169 Shells may be used interactively or non-interactively. In
170 interactive mode, they accept input typed from the keyboard.
171 When executing non-interactively, shells execute commands read
174 A shell allows execution of @sc{gnu} commands, both synchronously and
176 The shell waits for synchronous commands to complete before accepting
177 more input; asynchronous commands continue to execute in parallel
178 with the shell while it reads and executes additional commands.
179 The @dfn{redirection} constructs permit
180 fine-grained control of the input and output of those commands.
181 Moreover, the shell allows control over the contents of commands'
184 Shells also provide a small set of built-in
185 commands (@dfn{builtins}) implementing functionality impossible
186 or inconvenient to obtain via separate utilities.
187 For example, @code{cd}, @code{break}, @code{continue}, and
188 @code{exec} cannot be implemented outside of the shell because
189 they directly manipulate the shell itself.
190 The @code{history}, @code{getopts}, @code{kill}, or @code{pwd}
191 builtins, among others, could be implemented in separate utilities,
192 but they are more convenient to use as builtin commands.
193 All of the shell builtins are described in
196 While executing commands is essential, most of the power (and
197 complexity) of shells is due to their embedded programming
198 languages. Like any high-level language, the shell provides
199 variables, flow control constructs, quoting, and functions.
201 Shells offer features geared specifically for
202 interactive use rather than to augment the programming language.
203 These interactive features include job control, command line
204 editing, command history and aliases. Each of these features is
205 described in this manual.
209 These definitions are used throughout the remainder of this manual.
215 A family of open system standards based on Unix. Bash
216 is primarily concerned with the Shell and Utilities portion of the
217 @sc{posix} 1003.1 standard.
220 A space or tab character.
224 A command that is implemented internally by the shell itself, rather
225 than by an executable program somewhere in the file system.
227 @item control operator
228 @cindex control operator
229 A @code{token} that performs a control function. It is a @code{newline}
230 or one of the following:
231 @samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;},
232 @samp{|}, @samp{|&}, @samp{(}, or @samp{)}.
236 The value returned by a command to its caller. The value is restricted
237 to eight bits, so the maximum value is 255.
241 A unit of text that is the result of one of the shell expansions. After
242 expansion, when executing a command, the resulting fields are used as
243 the command name and arguments.
247 A string of characters used to identify a file.
251 A set of processes comprising a pipeline, and any processes descended
252 from it, that are all in the same process group.
256 A mechanism by which users can selectively stop (suspend) and restart
257 (resume) execution of processes.
260 @cindex metacharacter
261 A character that, when unquoted, separates words. A metacharacter is
262 a @code{blank} or one of the following characters:
263 @samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or
269 A @code{word} consisting solely of letters, numbers, and underscores,
270 and beginning with a letter or underscore. @code{Name}s are used as
271 shell variable and function names.
272 Also referred to as an @code{identifier}.
275 @cindex operator, shell
276 A @code{control operator} or a @code{redirection operator}.
277 @xref{Redirections}, for a list of redirection operators.
278 Operators contain at least one unquoted @code{metacharacter}.
281 @cindex process group
282 A collection of related processes each having the same process
285 @item process group ID
286 @cindex process group ID
287 A unique identifier that represents a @code{process group}
291 @cindex reserved word
292 A @code{word} that has a special meaning to the shell. Most reserved
293 words introduce shell flow control constructs, such as @code{for} and
297 @cindex return status
298 A synonym for @code{exit status}.
302 A mechanism by which a process may be notified by the kernel
303 of an event occurring in the system.
305 @item special builtin
306 @cindex special builtin
307 A shell builtin command that has been classified as special by the
312 A sequence of characters considered a single unit by the shell.
313 It is either a @code{word} or an @code{operator}.
317 A sequence of characters treated as a unit by the shell.
318 Words may not include unquoted @code{metacharacters}.
321 @node Basic Shell Features
322 @chapter Basic Shell Features
325 Bash is an acronym for @samp{Bourne-Again SHell}.
327 the traditional Unix shell originally written by Stephen Bourne.
328 All of the Bourne shell builtin commands are available in Bash,
329 The rules for evaluation and quoting are taken from the @sc{posix}
330 specification for the `standard' Unix shell.
332 This chapter briefly summarizes the shell's `building blocks':
333 commands, control structures, shell functions, shell @i{parameters},
335 @i{redirections}, which are a way to direct input and output from
336 and to named files, and how the shell executes commands.
339 * Shell Syntax:: What your input means to the shell.
340 * Shell Commands:: The types of commands you can use.
341 * Shell Functions:: Grouping commands by name.
342 * Shell Parameters:: How the shell stores values.
343 * Shell Expansions:: How Bash expands parameters and the various
344 expansions available.
345 * Redirections:: A way to control where input and output go.
346 * Executing Commands:: What happens when you run a command.
347 * Shell Scripts:: Executing files of shell commands.
351 @section Shell Syntax
353 * Shell Operation:: The basic operation of the shell.
354 * Quoting:: How to remove the special meaning from characters.
355 * Comments:: How to specify comments.
358 When the shell reads input, it proceeds through a
359 sequence of operations. If the input indicates the beginning of a
360 comment, the shell ignores the comment symbol (@samp{#}), and the rest
363 Otherwise, roughly speaking, the shell reads its input and
364 divides the input into words and operators, employing the quoting rules
365 to select which meanings to assign various words and characters.
367 The shell then parses these tokens into commands and other constructs,
368 removes the special meaning of certain words or characters, expands
369 others, redirects input and output as needed, executes the specified
370 command, waits for the command's exit status, and makes that exit status
371 available for further inspection or processing.
373 @node Shell Operation
374 @subsection Shell Operation
376 The following is a brief description of the shell's operation when it
377 reads and executes a command. Basically, the shell does the
382 Reads its input from a file (@pxref{Shell Scripts}), from a string
383 supplied as an argument to the @option{-c} invocation option
384 (@pxref{Invoking Bash}), or from the user's terminal.
387 Breaks the input into words and operators, obeying the quoting rules
388 described in @ref{Quoting}. These tokens are separated by
389 @code{metacharacters}. Alias expansion is performed by this step
393 Parses the tokens into simple and compound commands
394 (@pxref{Shell Commands}).
397 Performs the various shell expansions (@pxref{Shell Expansions}), breaking
398 the expanded tokens into lists of filenames (@pxref{Filename Expansion})
399 and commands and arguments.
402 Performs any necessary redirections (@pxref{Redirections}) and removes
403 the redirection operators and their operands from the argument list.
406 Executes the command (@pxref{Executing Commands}).
409 Optionally waits for the command to complete and collects its exit
410 status (@pxref{Exit Status}).
418 * Escape Character:: How to remove the special meaning from a single
420 * Single Quotes:: How to inhibit all interpretation of a sequence
422 * Double Quotes:: How to suppress most of the interpretation of a
423 sequence of characters.
424 * ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings.
425 * Locale Translation:: How to translate strings into different languages.
428 Quoting is used to remove the special meaning of certain
429 characters or words to the shell. Quoting can be used to
430 disable special treatment for special characters, to prevent
431 reserved words from being recognized as such, and to prevent
434 Each of the shell metacharacters (@pxref{Definitions})
435 has special meaning to the shell and must be quoted if it is to
437 When the command history expansion facilities are being used
438 (@pxref{History Interaction}), the
439 @var{history expansion} character, usually @samp{!}, must be quoted
440 to prevent history expansion. @xref{Bash History Facilities}, for
441 more details concerning history expansion.
443 There are three quoting mechanisms: the
444 @var{escape character}, single quotes, and double quotes.
446 @node Escape Character
447 @subsubsection Escape Character
448 A non-quoted backslash @samp{\} is the Bash escape character.
449 It preserves the literal value of the next character that follows,
450 with the exception of @code{newline}. If a @code{\newline} pair
451 appears, and the backslash itself is not quoted, the @code{\newline}
452 is treated as a line continuation (that is, it is removed from
453 the input stream and effectively ignored).
456 @subsubsection Single Quotes
458 Enclosing characters in single quotes (@samp{'}) preserves the literal value
459 of each character within the quotes. A single quote may not occur
460 between single quotes, even when preceded by a backslash.
463 @subsubsection Double Quotes
465 Enclosing characters in double quotes (@samp{"}) preserves the literal value
466 of all characters within the quotes, with the exception of
467 @samp{$}, @samp{`}, @samp{\},
468 and, when history expansion is enabled, @samp{!}.
469 The characters @samp{$} and @samp{`}
470 retain their special meaning within double quotes (@pxref{Shell Expansions}).
471 The backslash retains its special meaning only when followed by one of
472 the following characters:
473 @samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}.
474 Within double quotes, backslashes that are followed by one of these
475 characters are removed. Backslashes preceding characters without a
476 special meaning are left unmodified.
477 A double quote may be quoted within double quotes by preceding it with
479 If enabled, history expansion will be performed unless an @samp{!}
480 appearing in double quotes is escaped using a backslash.
481 The backslash preceding the @samp{!} is not removed.
483 The special parameters @samp{*} and @samp{@@} have special meaning
484 when in double quotes (@pxref{Shell Parameter Expansion}).
487 @subsubsection ANSI-C Quoting
488 @cindex quoting, ANSI
490 Words of the form @code{$'@var{string}'} are treated specially. The
491 word expands to @var{string}, with backslash-escaped characters replaced
492 as specified by the ANSI C standard. Backslash escape sequences, if
493 present, are decoded as follows:
502 an escape character (not ANSI C)
520 the eight-bit character whose value is the octal value @var{nnn}
521 (one to three digits)
523 the eight-bit character whose value is the hexadecimal value @var{HH}
524 (one or two hex digits)
526 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
527 @var{HHHH} (one to four hex digits)
528 @item \U@var{HHHHHHHH}
529 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
530 @var{HHHHHHHH} (one to eight hex digits)
532 a control-@var{x} character
536 The expanded result is single-quoted, as if the dollar sign had not
539 @node Locale Translation
540 @subsubsection Locale-Specific Translation
542 @cindex internationalization
543 @cindex native languages
544 @cindex translation, native languages
546 A double-quoted string preceded by a dollar sign (@samp{$}) will cause
547 the string to be translated according to the current locale.
548 If the current locale is @code{C} or @code{POSIX}, the dollar sign
550 If the string is translated and replaced, the replacement is
555 @vindex TEXTDOMAINDIR
556 Some systems use the message catalog selected by the @env{LC_MESSAGES}
557 shell variable. Others create the name of the message catalog from the
558 value of the @env{TEXTDOMAIN} shell variable, possibly adding a
559 suffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, you
560 may need to set the @env{TEXTDOMAINDIR} variable to the location of
561 the message catalog files. Still others use both variables in this
563 @env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo.
567 @cindex comments, shell
569 In a non-interactive shell, or an interactive shell in which the
570 @code{interactive_comments} option to the @code{shopt}
571 builtin is enabled (@pxref{The Shopt Builtin}),
572 a word beginning with @samp{#}
573 causes that word and all remaining characters on that line to
574 be ignored. An interactive shell without the @code{interactive_comments}
575 option enabled does not allow comments. The @code{interactive_comments}
576 option is on by default in interactive shells.
577 @xref{Interactive Shells}, for a description of what makes
581 @section Shell Commands
582 @cindex commands, shell
584 A simple shell command such as @code{echo a b c} consists of the command
585 itself followed by arguments, separated by spaces.
587 More complex shell commands are composed of simple commands arranged together
588 in a variety of ways: in a pipeline in which the output of one command
589 becomes the input of a second, in a loop or conditional construct, or in
593 * Simple Commands:: The most common type of command.
594 * Pipelines:: Connecting the input and output of several
596 * Lists:: How to execute commands sequentially.
597 * Compound Commands:: Shell commands for control flow.
598 * Coprocesses:: Two-way communication between commands.
599 * GNU Parallel:: Running commands in parallel.
602 @node Simple Commands
603 @subsection Simple Commands
604 @cindex commands, simple
606 A simple command is the kind of command encountered most often.
607 It's just a sequence of words separated by @code{blank}s, terminated
608 by one of the shell's control operators (@pxref{Definitions}). The
609 first word generally specifies a command to be executed, with the
610 rest of the words being that command's arguments.
612 The return status (@pxref{Exit Status}) of a simple command is
613 its exit status as provided
614 by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
615 the command was terminated by signal @var{n}.
618 @subsection Pipelines
620 @cindex commands, pipelines
622 A @code{pipeline} is a sequence of simple commands separated by one of
623 the control operators @samp{|} or @samp{|&}.
627 @cindex command timing
628 The format for a pipeline is
630 [time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{}
634 The output of each command in the pipeline is connected via a pipe
635 to the input of the next command.
636 That is, each command reads the previous command's output. This
637 connection is performed before any redirections specified by the
640 If @samp{|&} is used, @var{command1}'s standard output and standard error
642 @var{command2}'s standard input through the pipe;
643 it is shorthand for @code{2>&1 |}.
644 This implicit redirection of the standard error is
645 performed after any redirections specified by the command.
647 The reserved word @code{time} causes timing statistics
648 to be printed for the pipeline once it finishes.
649 The statistics currently consist of elapsed (wall-clock) time and
650 user and system time consumed by the command's execution.
651 The @option{-p} option changes the output format to that specified
653 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
654 it does not recognize @code{time} as a reserved word if the next
655 token begins with a @samp{-}.
656 The @env{TIMEFORMAT} variable may be set to a format string that
657 specifies how the timing information should be displayed.
658 @xref{Bash Variables}, for a description of the available formats.
659 The use of @code{time} as a reserved word permits the timing of
660 shell builtins, shell functions, and pipelines. An external
661 @code{time} command cannot time these easily.
663 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}
664 may be followed by a newline. In this case, the shell displays the
665 total user and system time consumed by the shell and its children.
666 The @env{TIMEFORMAT} variable may be used to specify the format of
667 the time information.
669 If the pipeline is not executed asynchronously (@pxref{Lists}), the
670 shell waits for all commands in the pipeline to complete.
672 Each command in a pipeline is executed in its own subshell
673 (@pxref{Command Execution Environment}). The exit
674 status of a pipeline is the exit status of the last command in the
675 pipeline, unless the @code{pipefail} option is enabled
676 (@pxref{The Set Builtin}).
677 If @code{pipefail} is enabled, the pipeline's return status is the
678 value of the last (rightmost) command to exit with a non-zero status,
679 or zero if all commands exit successfully.
680 If the reserved word @samp{!} precedes the pipeline, the
681 exit status is the logical negation of the exit status as described
683 The shell waits for all commands in the pipeline to terminate before
687 @subsection Lists of Commands
688 @cindex commands, lists
690 A @code{list} is a sequence of one or more pipelines separated by one
691 of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
692 and optionally terminated by one of @samp{;}, @samp{&}, or a
695 Of these list operators, @samp{&&} and @samp{||}
696 have equal precedence, followed by @samp{;} and @samp{&},
697 which have equal precedence.
699 A sequence of one or more newlines may appear in a @code{list}
700 to delimit commands, equivalent to a semicolon.
702 If a command is terminated by the control operator @samp{&},
703 the shell executes the command asynchronously in a subshell.
704 This is known as executing the command in the @var{background}.
705 The shell does not wait for the command to finish, and the return
707 When job control is not active (@pxref{Job Control}),
708 the standard input for asynchronous commands, in the absence of any
709 explicit redirections, is redirected from @code{/dev/null}.
711 Commands separated by a @samp{;} are executed sequentially; the shell
712 waits for each command to terminate in turn. The return status is the
713 exit status of the last command executed.
715 @sc{and} and @sc{or} lists are sequences of one or more pipelines
716 separated by the control operators @samp{&&} and @samp{||},
717 respectively. @sc{and} and @sc{or} lists are executed with left
720 An @sc{and} list has the form
722 @var{command1} && @var{command2}
726 @var{command2} is executed if, and only if, @var{command1}
727 returns an exit status of zero.
729 An @sc{or} list has the form
731 @var{command1} || @var{command2}
735 @var{command2} is executed if, and only if, @var{command1}
736 returns a non-zero exit status.
739 @sc{and} and @sc{or} lists is the exit status of the last command
740 executed in the list.
742 @node Compound Commands
743 @subsection Compound Commands
744 @cindex commands, compound
747 * Looping Constructs:: Shell commands for iterative action.
748 * Conditional Constructs:: Shell commands for conditional execution.
749 * Command Grouping:: Ways to group commands.
752 Compound commands are the shell programming constructs.
753 Each construct begins with a reserved word or control operator and is
754 terminated by a corresponding reserved word or operator.
755 Any redirections (@pxref{Redirections}) associated with a compound command
756 apply to all commands within that compound command unless explicitly overridden.
758 In most cases a list of commands in a compound command's description may be
759 separated from the rest of the command by one or more newlines, and may be
760 followed by a newline in place of a semicolon.
762 Bash provides looping constructs, conditional commands, and mechanisms
763 to group commands and execute them as a unit.
765 @node Looping Constructs
766 @subsubsection Looping Constructs
767 @cindex commands, looping
769 Bash supports the following looping constructs.
771 Note that wherever a @samp{;} appears in the description of a
772 command's syntax, it may be replaced with one or more newlines.
779 The syntax of the @code{until} command is:
782 until @var{test-commands}; do @var{consequent-commands}; done
785 Execute @var{consequent-commands} as long as
786 @var{test-commands} has an exit status which is not zero.
787 The return status is the exit status of the last command executed
788 in @var{consequent-commands}, or zero if none was executed.
792 The syntax of the @code{while} command is:
795 while @var{test-commands}; do @var{consequent-commands}; done
798 Execute @var{consequent-commands} as long as
799 @var{test-commands} has an exit status of zero.
800 The return status is the exit status of the last command executed
801 in @var{consequent-commands}, or zero if none was executed.
805 The syntax of the @code{for} command is:
808 for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done
811 Expand @var{words}, and execute @var{commands} once for each member
812 in the resultant list, with @var{name} bound to the current member.
813 If @samp{in @var{words}} is not present, the @code{for} command
814 executes the @var{commands} once for each positional parameter that is
815 set, as if @samp{in "$@@"} had been specified
816 (@pxref{Special Parameters}).
817 The return status is the exit status of the last command that executes.
818 If there are no items in the expansion of @var{words}, no commands are
819 executed, and the return status is zero.
821 An alternate form of the @code{for} command is also supported:
824 for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
827 First, the arithmetic expression @var{expr1} is evaluated according
828 to the rules described below (@pxref{Shell Arithmetic}).
829 The arithmetic expression @var{expr2} is then evaluated repeatedly
830 until it evaluates to zero.
831 Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
832 executed and the arithmetic expression @var{expr3} is evaluated.
833 If any expression is omitted, it behaves as if it evaluates to 1.
834 The return value is the exit status of the last command in @var{commands}
835 that is executed, or false if any of the expressions is invalid.
838 The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
839 may be used to control loop execution.
841 @node Conditional Constructs
842 @subsubsection Conditional Constructs
843 @cindex commands, conditional
852 The syntax of the @code{if} command is:
855 if @var{test-commands}; then
856 @var{consequent-commands};
857 [elif @var{more-test-commands}; then
858 @var{more-consequents};]
859 [else @var{alternate-consequents};]
863 The @var{test-commands} list is executed, and if its return status is zero,
864 the @var{consequent-commands} list is executed.
865 If @var{test-commands} returns a non-zero status, each @code{elif} list
866 is executed in turn, and if its exit status is zero,
867 the corresponding @var{more-consequents} is executed and the
869 If @samp{else @var{alternate-consequents}} is present, and
870 the final command in the final @code{if} or @code{elif} clause
871 has a non-zero exit status, then @var{alternate-consequents} is executed.
872 The return status is the exit status of the last command executed, or
873 zero if no condition tested true.
879 The syntax of the @code{case} command is:
882 case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac
885 @code{case} will selectively execute the @var{command-list} corresponding to
886 the first @var{pattern} that matches @var{word}.
887 If the shell option @code{nocasematch}
888 (see the description of @code{shopt} in @ref{The Shopt Builtin})
889 is enabled, the match is performed without regard to the case
890 of alphabetic characters.
891 The @samp{|} is used to separate multiple patterns, and the @samp{)}
892 operator terminates a pattern list.
893 A list of patterns and an associated command-list is known
896 Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.
897 The @var{word} undergoes tilde expansion, parameter expansion, command
898 substitution, arithmetic expansion, and quote removal before matching is
899 attempted. Each @var{pattern} undergoes tilde expansion, parameter
900 expansion, command substitution, and arithmetic expansion.
902 There may be an arbitrary number of @code{case} clauses, each terminated
903 by a @samp{;;}, @samp{;&}, or @samp{;;&}.
904 The first pattern that matches determines the
905 command-list that is executed.
907 Here is an example using @code{case} in a script that could be used to
908 describe one interesting feature of an animal:
911 echo -n "Enter the name of an animal: "
913 echo -n "The $ANIMAL has "
915 horse | dog | cat) echo -n "four";;
916 man | kangaroo ) echo -n "two";;
917 *) echo -n "an unknown number of";;
924 If the @samp{;;} operator is used, no subsequent matches are attempted after
925 the first pattern match.
926 Using @samp{;&} in place of @samp{;;} causes execution to continue with
927 the @var{command-list} associated with the next clause, if any.
928 Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns
929 in the next clause, if any, and execute any associated @var{command-list}
930 on a successful match.
932 The return status is zero if no @var{pattern} is matched. Otherwise, the
933 return status is the exit status of the @var{command-list} executed.
938 The @code{select} construct allows the easy generation of menus.
939 It has almost the same syntax as the @code{for} command:
942 select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
945 The list of words following @code{in} is expanded, generating a list
946 of items. The set of expanded words is printed on the standard
947 error output stream, each preceded by a number. If the
948 @samp{in @var{words}} is omitted, the positional parameters are printed,
949 as if @samp{in "$@@"} had been specified.
950 The @env{PS3} prompt is then displayed and a line is read from the
952 If the line consists of a number corresponding to one of the displayed
953 words, then the value of @var{name} is set to that word.
954 If the line is empty, the words and prompt are displayed again.
955 If @code{EOF} is read, the @code{select} command completes.
956 Any other value read causes @var{name} to be set to null.
957 The line read is saved in the variable @env{REPLY}.
959 The @var{commands} are executed after each selection until a
960 @code{break} command is executed, at which
961 point the @code{select} command completes.
963 Here is an example that allows the user to pick a filename from the
964 current directory, and displays the name and index of the file
970 echo you picked $fname \($REPLY\)
977 (( @var{expression} ))
980 The arithmetic @var{expression} is evaluated according to the rules
981 described below (@pxref{Shell Arithmetic}).
982 If the value of the expression is non-zero, the return status is 0;
983 otherwise the return status is 1. This is exactly equivalent to
985 let "@var{expression}"
988 @xref{Bash Builtins}, for a full description of the @code{let} builtin.
994 [[ @var{expression} ]]
997 Return a status of 0 or 1 depending on the evaluation of
998 the conditional expression @var{expression}.
999 Expressions are composed of the primaries described below in
1000 @ref{Bash Conditional Expressions}.
1001 Word splitting and filename expansion are not performed on the words
1002 between the @samp{[[} and @samp{]]}; tilde expansion, parameter and
1003 variable expansion, arithmetic expansion, command substitution, process
1004 substitution, and quote removal are performed.
1005 Conditional operators such as @samp{-f} must be unquoted to be recognized
1008 When used with @samp{[[}, the @samp{<} and @samp{>} operators sort
1009 lexicographically using the current locale.
1011 When the @samp{==} and @samp{!=} operators are used, the string to the
1012 right of the operator is considered a pattern and matched according
1013 to the rules described below in @ref{Pattern Matching}.
1014 If the shell option @code{nocasematch}
1015 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1016 is enabled, the match is performed without regard to the case
1017 of alphabetic characters.
1018 The return value is 0 if the string matches (@samp{==}) or does not
1019 match (@samp{!=})the pattern, and 1 otherwise.
1020 Any part of the pattern may be quoted to force the quoted portion
1021 to be matched as a string.
1023 An additional binary operator, @samp{=~}, is available, with the same
1024 precedence as @samp{==} and @samp{!=}.
1025 When it is used, the string to the right of the operator is considered
1026 an extended regular expression and matched accordingly (as in @i{regex}3)).
1027 The return value is 0 if the string matches
1028 the pattern, and 1 otherwise.
1029 If the regular expression is syntactically incorrect, the conditional
1030 expression's return value is 2.
1031 If the shell option @code{nocasematch}
1032 (see the description of @code{shopt} in @ref{The Shopt Builtin})
1033 is enabled, the match is performed without regard to the case
1034 of alphabetic characters.
1035 Any part of the pattern may be quoted to force the quoted portion
1036 to be matched as a string.
1037 Bracket expressions in regular expressions must be treated carefully,
1038 since normal quoting characters lose their meanings between brackets.
1039 If the pattern is stored in a shell variable, quoting the variable
1040 expansion forces the entire pattern to be matched as a string.
1041 Substrings matched by parenthesized subexpressions within the regular
1042 expression are saved in the array variable @code{BASH_REMATCH}.
1043 The element of @code{BASH_REMATCH} with index 0 is the portion of the string
1044 matching the entire regular expression.
1045 The element of @code{BASH_REMATCH} with index @var{n} is the portion of the
1046 string matching the @var{n}th parenthesized subexpression.
1048 Expressions may be combined using the following operators, listed
1049 in decreasing order of precedence:
1052 @item ( @var{expression} )
1053 Returns the value of @var{expression}.
1054 This may be used to override the normal precedence of operators.
1056 @item ! @var{expression}
1057 True if @var{expression} is false.
1059 @item @var{expression1} && @var{expression2}
1060 True if both @var{expression1} and @var{expression2} are true.
1062 @item @var{expression1} || @var{expression2}
1063 True if either @var{expression1} or @var{expression2} is true.
1067 The @code{&&} and @code{||} operators do not evaluate @var{[Bexpression2} if the
1068 value of @var{expression1} is sufficient to determine the return
1069 value of the entire conditional expression.
1072 @node Command Grouping
1073 @subsubsection Grouping Commands
1074 @cindex commands, grouping
1076 Bash provides two ways to group a list of commands to be executed
1077 as a unit. When commands are grouped, redirections may be applied
1078 to the entire command list. For example, the output of all the
1079 commands in the list may be redirected to a single stream.
1087 Placing a list of commands between parentheses causes a subshell
1088 environment to be created (@pxref{Command Execution Environment}), and each
1089 of the commands in @var{list} to be executed in that subshell. Since the
1090 @var{list} is executed in a subshell, variable assignments do not remain in
1091 effect after the subshell completes.
1100 Placing a list of commands between curly braces causes the list to
1101 be executed in the current shell context. No subshell is created.
1102 The semicolon (or newline) following @var{list} is required.
1105 In addition to the creation of a subshell, there is a subtle difference
1106 between these two constructs due to historical reasons. The braces
1107 are @code{reserved words}, so they must be separated from the @var{list}
1108 by @code{blank}s or other shell metacharacters.
1109 The parentheses are @code{operators}, and are
1110 recognized as separate tokens by the shell even if they are not separated
1111 from the @var{list} by whitespace.
1113 The exit status of both of these constructs is the exit status of
1117 @subsection Coprocesses
1120 A @code{coprocess} is a shell command preceded by the @code{coproc}
1122 A coprocess is executed asynchronously in a subshell, as if the command
1123 had been terminated with the @samp{&} control operator, with a two-way pipe
1124 established between the executing shell and the coprocess.
1126 The format for a coprocess is:
1128 coproc [@var{NAME}] @var{command} [@var{redirections}]
1132 This creates a coprocess named @var{NAME}.
1133 If @var{NAME} is not supplied, the default name is @var{COPROC}.
1134 @var{NAME} must not be supplied if @var{command} is a simple
1135 command (@pxref{Simple Commands}); otherwise, it is interpreted as
1136 the first word of the simple command.
1138 When the coprocess is executed, the shell creates an array variable
1140 named @env{NAME} in the context of the executing shell.
1141 The standard output of @var{command}
1142 is connected via a pipe to a file descriptor in the executing shell,
1143 and that file descriptor is assigned to @env{NAME}[0].
1144 The standard input of @var{command}
1145 is connected via a pipe to a file descriptor in the executing shell,
1146 and that file descriptor is assigned to @env{NAME}[1].
1147 This pipe is established before any redirections specified by the
1148 command (@pxref{Redirections}).
1149 The file descriptors can be utilized as arguments to shell commands
1150 and redirections using standard word expansions.
1152 The process ID of the shell spawned to execute the coprocess is
1153 available as the value of the variable @env{NAME}_PID.
1155 builtin command may be used to wait for the coprocess to terminate.
1157 The return status of a coprocess is the exit status of @var{command}.
1160 @subsection GNU Parallel
1162 GNU Parallel, as its name suggests, can be used to build and run commands
1163 in parallel. You may run the same command with different arguments, whether
1164 they are filenames, usernames, hostnames, or lines read from files.
1166 For a complete description, refer to the GNU Parallel documentation. A few
1167 examples should provide a brief introduction to its use.
1169 For example, it is easy to prefix each line in a text file with a specified
1172 cat file | parallel -k echo prefix_string
1175 The @option{-k} option is required to preserve the lines' order.
1177 Similarly, you can append a specified string to each line in a text file:
1179 cat file | parallel -k echo @{@} append_string
1182 You can use Parallel to move files from the current directory when the
1183 number of files is too large to process with one @code{mv} invocation:
1185 ls | parallel mv @{@} destdir
1188 As you can see, the @{@} is replaced with each line read from standard input.
1189 This will run as many @code{mv} commands as there are files in the current
1190 directory. You can emulate a parallel @code{xargs} by adding the @option{-X}
1193 ls | parallel -X mv @{@} destdir
1196 GNU Parallel can replace certain common idioms that operate on lines read
1197 from a file (in this case, filenames):
1199 for x in $(cat list); do
1200 do-something1 $x config-$x
1202 done | process-output
1206 with a more compact syntax reminiscent of lambdas:
1208 cat list | parallel "do-something1 @{@} config-@{@} ; do-something2 < @{@}" | process-output
1211 Parallel provides a built-in mechanism to remove filename extensions, which
1212 lends itself to batch file transformations or renaming:
1214 ls *.gz | parallel -j+0 "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}"
1217 This will recompress all files in the current directory with names ending
1218 in .gz using bzip2, running one job per CPU (-j+0) in parallel.
1220 If a command generates output, you may want to preserve the input order in
1221 the output. For instance, the following command
1223 @{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel traceroute
1226 will display as output the traceroute invocation that finishes first. Using
1227 the @option{-k} option, as we saw above
1229 @{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel -k traceroute
1232 will ensure that the output of @code{traceroute foss.org.my} is displayed first.
1234 @node Shell Functions
1235 @section Shell Functions
1236 @cindex shell function
1237 @cindex functions, shell
1239 Shell functions are a way to group commands for later execution
1240 using a single name for the group. They are executed just like
1241 a "regular" command.
1242 When the name of a shell function is used as a simple command name,
1243 the list of commands associated with that function name is executed.
1244 Shell functions are executed in the current
1245 shell context; no new process is created to interpret them.
1247 Functions are declared using this syntax:
1250 @var{name} () @var{compound-command} [ @var{redirections} ]
1256 function @var{name} [()] @var{compound-command} [ @var{redirections} ]
1259 This defines a shell function named @var{name}. The reserved
1260 word @code{function} is optional.
1261 If the @code{function} reserved
1262 word is supplied, the parentheses are optional.
1263 The @var{body} of the function is the compound command
1264 @var{compound-command} (@pxref{Compound Commands}).
1265 That command is usually a @var{list} enclosed between @{ and @}, but
1266 may be any compound command listed above.
1267 @var{compound-command} is executed whenever @var{name} is specified as the
1269 When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
1270 @var{name} may not be the same as one of the special builtins
1271 (@pxref{Special Builtins}).
1272 Any redirections (@pxref{Redirections}) associated with the shell function
1273 are performed when the function is executed.
1275 A function definition may be deleted using the @option{-f} option to the
1276 @code{unset} builtin (@pxref{Bourne Shell Builtins}).
1278 The exit status of a function definition is zero unless a syntax error
1279 occurs or a readonly function with the same name already exists.
1280 When executed, the exit status of a function is the exit status of the
1281 last command executed in the body.
1283 Note that for historical reasons, in the most common usage the curly braces
1284 that surround the body of the function must be separated from the body by
1285 @code{blank}s or newlines.
1286 This is because the braces are reserved words and are only recognized
1287 as such when they are separated from the command list
1288 by whitespace or another shell metacharacter.
1289 Also, when using the braces, the @var{list} must be terminated by a semicolon,
1290 a @samp{&}, or a newline.
1292 When a function is executed, the arguments to the
1293 function become the positional parameters
1294 during its execution (@pxref{Positional Parameters}).
1295 The special parameter @samp{#} that expands to the number of
1296 positional parameters is updated to reflect the change.
1297 Special parameter @code{0} is unchanged.
1298 The first element of the @env{FUNCNAME} variable is set to the
1299 name of the function while the function is executing.
1301 All other aspects of the shell execution
1302 environment are identical between a function and its caller
1303 with these exceptions:
1304 the @env{DEBUG} and @env{RETURN} traps
1305 are not inherited unless the function has been given the
1306 @code{trace} attribute using the @code{declare} builtin or
1307 the @code{-o functrace} option has been enabled with
1308 the @code{set} builtin,
1309 (in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps),
1310 and the @env{ERR} trap is not inherited unless the @code{-o errtrace}
1311 shell option has been enabled.
1312 @xref{Bourne Shell Builtins}, for the description of the
1313 @code{trap} builtin.
1315 The @env{FUNCNEST} variable, if set to a numeric value greater
1316 than 0, defines a maximum function nesting level. Function
1317 invocations that exceed the limit cause the entire command to
1320 If the builtin command @code{return}
1321 is executed in a function, the function completes and
1322 execution resumes with the next command after the function
1324 Any command associated with the @code{RETURN} trap is executed
1325 before execution resumes.
1326 When a function completes, the values of the
1327 positional parameters and the special parameter @samp{#}
1328 are restored to the values they had prior to the function's
1329 execution. If a numeric argument is given to @code{return},
1330 that is the function's return status; otherwise the function's
1331 return status is the exit status of the last command executed
1332 before the @code{return}.
1334 Variables local to the function may be declared with the
1335 @code{local} builtin. These variables are visible only to
1336 the function and the commands it invokes.
1338 Function names and definitions may be listed with the
1339 @option{-f} option to the @code{declare} (@code{typeset})
1340 builtin command (@pxref{Bash Builtins}).
1341 The @option{-F} option to @code{declare} or @code{typeset}
1342 will list the function names only
1343 (and optionally the source file and line number, if the @code{extdebug}
1344 shell option is enabled).
1345 Functions may be exported so that subshells
1346 automatically have them defined with the
1347 @option{-f} option to the @code{export} builtin
1348 (@pxref{Bourne Shell Builtins}).
1349 Note that shell functions and variables with the same name may result
1350 in multiple identically-named entries in the environment passed to the
1352 Care should be taken in cases where this may cause a problem.
1354 Functions may be recursive.
1355 The @code{FUNCNEST} variable may be used to limit the depth of the
1356 function call stack and restrict the number of function invocations.
1357 By default, no limit is placed on the number of recursive calls.
1359 @node Shell Parameters
1360 @section Shell Parameters
1362 @cindex variable, shell
1363 @cindex shell variable
1366 * Positional Parameters:: The shell's command-line arguments.
1367 * Special Parameters:: Parameters denoted by special characters.
1370 A @var{parameter} is an entity that stores values.
1371 It can be a @code{name}, a number, or one of the special characters
1373 A @var{variable} is a parameter denoted by a @code{name}.
1374 A variable has a @var{value} and zero or more @var{attributes}.
1375 Attributes are assigned using the @code{declare} builtin command
1376 (see the description of the @code{declare} builtin in @ref{Bash Builtins}).
1378 A parameter is set if it has been assigned a value. The null string is
1379 a valid value. Once a variable is set, it may be unset only by using
1380 the @code{unset} builtin command.
1382 A variable may be assigned to by a statement of the form
1384 @var{name}=[@var{value}]
1388 is not given, the variable is assigned the null string. All
1389 @var{value}s undergo tilde expansion, parameter and variable expansion,
1390 command substitution, arithmetic expansion, and quote
1391 removal (detailed below). If the variable has its @code{integer}
1392 attribute set, then @var{value}
1393 is evaluated as an arithmetic expression even if the @code{$((@dots{}))}
1394 expansion is not used (@pxref{Arithmetic Expansion}).
1395 Word splitting is not performed, with the exception
1396 of @code{"$@@"} as explained below.
1397 Filename expansion is not performed.
1398 Assignment statements may also appear as arguments to the
1400 @code{declare}, @code{typeset}, @code{export}, @code{readonly},
1401 and @code{local} builtin commands.
1402 When in @sc{posix} mode (@pxref{Bash POSIX Mode}), these builtins may appear
1403 in a command after one or more instances of the @code{command} builtin
1404 and retain these assignment statement properties.
1406 In the context where an assignment statement is assigning a value
1407 to a shell variable or array index (@pxref{Arrays}), the @samp{+=}
1408 operator can be used to
1409 append to or add to the variable's previous value.
1410 When @samp{+=} is applied to a variable for which the @var{integer} attribute
1411 has been set, @var{value} is evaluated as an arithmetic expression and
1412 added to the variable's current value, which is also evaluated.
1413 When @samp{+=} is applied to an array variable using compound assignment
1414 (@pxref{Arrays}), the
1415 variable's value is not unset (as it is when using @samp{=}), and new
1416 values are appended to the array beginning at one greater than the array's
1417 maximum index (for indexed arrays), or added as additional key-value pairs
1418 in an associative array.
1419 When applied to a string-valued variable, @var{value} is expanded and
1420 appended to the variable's value.
1422 @node Positional Parameters
1423 @subsection Positional Parameters
1424 @cindex parameters, positional
1426 A @var{positional parameter} is a parameter denoted by one or more
1427 digits, other than the single digit @code{0}. Positional parameters are
1428 assigned from the shell's arguments when it is invoked,
1429 and may be reassigned using the @code{set} builtin command.
1430 Positional parameter @code{N} may be referenced as @code{$@{N@}}, or
1431 as @code{$N} when @code{N} consists of a single digit.
1432 Positional parameters may not be assigned to with assignment statements.
1433 The @code{set} and @code{shift} builtins are used to set and
1434 unset them (@pxref{Shell Builtin Commands}).
1435 The positional parameters are
1436 temporarily replaced when a shell function is executed
1437 (@pxref{Shell Functions}).
1439 When a positional parameter consisting of more than a single
1440 digit is expanded, it must be enclosed in braces.
1442 @node Special Parameters
1443 @subsection Special Parameters
1444 @cindex parameters, special
1446 The shell treats several parameters specially. These parameters may
1447 only be referenced; assignment to them is not allowed.
1452 Expands to the positional parameters, starting from one. When the
1453 expansion occurs within double quotes, it expands to a single word
1454 with the value of each parameter separated by the first character
1456 special variable. That is, @code{"$*"} is equivalent
1457 to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c}
1458 is the first character of the value of the @code{IFS}
1460 If @env{IFS} is unset, the parameters are separated by spaces.
1461 If @env{IFS} is null, the parameters are joined without intervening
1466 Expands to the positional parameters, starting from one. When the
1467 expansion occurs within double quotes, each parameter expands to a
1468 separate word. That is, @code{"$@@"} is equivalent to
1469 @code{"$1" "$2" @dots{}}.
1470 If the double-quoted expansion occurs within a word, the expansion of
1471 the first parameter is joined with the beginning part of the original
1472 word, and the expansion of the last parameter is joined with the last
1473 part of the original word.
1474 When there are no positional parameters, @code{"$@@"} and
1476 expand to nothing (i.e., they are removed).
1479 Expands to the number of positional parameters in decimal.
1482 Expands to the exit status of the most recently executed foreground
1486 (A hyphen.) Expands to the current option flags as specified upon
1487 invocation, by the @code{set}
1488 builtin command, or those set by the shell itself
1489 (such as the @option{-i} option).
1492 Expands to the process @sc{id} of the shell. In a @code{()} subshell, it
1493 expands to the process @sc{id} of the invoking shell, not the subshell.
1496 Expands to the process @sc{id} of the most recently executed background
1497 (asynchronous) command.
1500 Expands to the name of the shell or shell script. This is set at
1501 shell initialization. If Bash is invoked with a file of commands
1502 (@pxref{Shell Scripts}), @code{$0} is set to the name of that file.
1503 If Bash is started with the @option{-c} option (@pxref{Invoking Bash}),
1504 then @code{$0} is set to the first argument after the string to be
1505 executed, if one is present. Otherwise, it is set
1506 to the filename used to invoke Bash, as given by argument zero.
1510 At shell startup, set to the absolute pathname used to invoke the
1511 shell or shell script being executed as passed in the environment
1513 Subsequently, expands to the last argument to the previous command,
1515 Also set to the full pathname used to invoke each command executed
1516 and placed in the environment exported to that command.
1517 When checking mail, this parameter holds the name of the mail file.
1520 @node Shell Expansions
1521 @section Shell Expansions
1524 Expansion is performed on the command line after it has been split into
1525 @code{token}s. There are seven kinds of expansion performed:
1528 @item brace expansion
1529 @item tilde expansion
1530 @item parameter and variable expansion
1531 @item command substitution
1532 @item arithmetic expansion
1533 @item word splitting
1534 @item filename expansion
1538 * Brace Expansion:: Expansion of expressions within braces.
1539 * Tilde Expansion:: Expansion of the ~ character.
1540 * Shell Parameter Expansion:: How Bash expands variables to their values.
1541 * Command Substitution:: Using the output of a command as an argument.
1542 * Arithmetic Expansion:: How to use arithmetic in shell expansions.
1543 * Process Substitution:: A way to write and read to and from a
1545 * Word Splitting:: How the results of expansion are split into separate
1547 * Filename Expansion:: A shorthand for specifying filenames matching patterns.
1548 * Quote Removal:: How and when quote characters are removed from
1552 The order of expansions is: brace expansion, tilde expansion,
1553 parameter, variable, and arithmetic expansion and
1554 command substitution
1555 (done in a left-to-right fashion), word splitting, and filename
1558 On systems that can support it, there is an additional expansion
1559 available: @var{process substitution}. This is performed at the
1560 same time as parameter, variable, and arithmetic expansion and
1561 command substitution.
1563 Only brace expansion, word splitting, and filename expansion
1564 can change the number of words of the expansion; other expansions
1565 expand a single word to a single word.
1566 The only exceptions to this are the expansions of
1567 @code{"$@@"} (@pxref{Special Parameters}) and @code{"$@{@var{name}[@@]@}"}
1570 After all expansions, @code{quote removal} (@pxref{Quote Removal})
1573 @node Brace Expansion
1574 @subsection Brace Expansion
1575 @cindex brace expansion
1576 @cindex expansion, brace
1578 Brace expansion is a mechanism by which arbitrary strings may be generated.
1579 This mechanism is similar to
1580 @var{filename expansion} (@pxref{Filename Expansion}),
1581 but the filenames generated need not exist.
1582 Patterns to be brace expanded take the form of an optional @var{preamble},
1583 followed by either a series of comma-separated strings or a sequence expression
1584 between a pair of braces,
1585 followed by an optional @var{postscript}.
1586 The preamble is prefixed to each string contained within the braces, and
1587 the postscript is then appended to each resulting string, expanding left
1590 Brace expansions may be nested.
1591 The results of each expanded string are not sorted; left to right order
1595 bash$ echo a@{d,c,b@}e
1599 A sequence expression takes the form @code{@{@var{x}..@var{y}[..@var{incr}]@}},
1600 where @var{x} and @var{y} are either integers or single characters,
1601 and @var{incr}, an optional increment, is an integer.
1602 When integers are supplied, the expression expands to each number between
1603 @var{x} and @var{y}, inclusive.
1604 Supplied integers may be prefixed with @samp{0} to force each term to have the
1605 same width. When either @var{x} or @var{y} begins with a zero, the shell
1606 attempts to force all generated terms to contain the same number of digits,
1607 zero-padding where necessary.
1608 When characters are supplied, the expression expands to each character
1609 lexicographically between @var{x} and @var{y}, inclusive. Note that
1610 both @var{x} and @var{y} must be of the same type.
1611 When the increment is supplied, it is used as the difference between
1612 each term. The default increment is 1 or -1 as appropriate.
1614 Brace expansion is performed before any other expansions,
1615 and any characters special to other expansions are preserved
1616 in the result. It is strictly textual. Bash
1617 does not apply any syntactic interpretation to the context of the
1618 expansion or the text between the braces.
1619 To avoid conflicts with parameter expansion, the string @samp{$@{}
1620 is not considered eligible for brace expansion.
1622 A correctly-formed brace expansion must contain unquoted opening
1623 and closing braces, and at least one unquoted comma or a valid
1624 sequence expression.
1625 Any incorrectly formed brace expansion is left unchanged.
1627 A @{ or @samp{,} may be quoted with a backslash to prevent its
1628 being considered part of a brace expression.
1629 To avoid conflicts with parameter expansion, the string @samp{$@{}
1630 is not considered eligible for brace expansion.
1632 This construct is typically used as shorthand when the common
1633 prefix of the strings to be generated is longer than in the
1636 mkdir /usr/local/src/bash/@{old,new,dist,bugs@}
1640 chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@}
1643 @node Tilde Expansion
1644 @subsection Tilde Expansion
1645 @cindex tilde expansion
1646 @cindex expansion, tilde
1648 If a word begins with an unquoted tilde character (@samp{~}), all of the
1649 characters up to the first unquoted slash (or all characters,
1650 if there is no unquoted slash) are considered a @var{tilde-prefix}.
1651 If none of the characters in the tilde-prefix are quoted, the
1652 characters in the tilde-prefix following the tilde are treated as a
1653 possible @var{login name}.
1654 If this login name is the null string, the tilde is replaced with the
1655 value of the @env{HOME} shell variable.
1656 If @env{HOME} is unset, the home directory of the user executing the
1657 shell is substituted instead.
1658 Otherwise, the tilde-prefix is replaced with the home directory
1659 associated with the specified login name.
1661 If the tilde-prefix is @samp{~+}, the value of
1662 the shell variable @env{PWD} replaces the tilde-prefix.
1663 If the tilde-prefix is @samp{~-}, the value of the shell variable
1664 @env{OLDPWD}, if it is set, is substituted.
1666 If the characters following the tilde in the tilde-prefix consist of a
1667 number @var{N}, optionally prefixed by a @samp{+} or a @samp{-},
1668 the tilde-prefix is replaced with the
1669 corresponding element from the directory stack, as it would be displayed
1670 by the @code{dirs} builtin invoked with the characters following tilde
1671 in the tilde-prefix as an argument (@pxref{The Directory Stack}).
1672 If the tilde-prefix, sans the tilde, consists of a number without a
1673 leading @samp{+} or @samp{-}, @samp{+} is assumed.
1675 If the login name is invalid, or the tilde expansion fails, the word is
1678 Each variable assignment is checked for unquoted tilde-prefixes immediately
1679 following a @samp{:} or the first @samp{=}.
1680 In these cases, tilde expansion is also performed.
1681 Consequently, one may use filenames with tildes in assignments to
1682 @env{PATH}, @env{MAILPATH}, and @env{CDPATH},
1683 and the shell assigns the expanded value.
1685 The following table shows how Bash treats unquoted tilde-prefixes:
1689 The value of @code{$HOME}
1694 The subdirectory @code{foo} of the home directory of the user
1701 @file{$@{OLDPWD-'~-'@}/foo}
1704 The string that would be displayed by @samp{dirs +@var{N}}
1707 The string that would be displayed by @samp{dirs +@var{N}}
1710 The string that would be displayed by @samp{dirs -@var{N}}
1713 @node Shell Parameter Expansion
1714 @subsection Shell Parameter Expansion
1715 @cindex parameter expansion
1716 @cindex expansion, parameter
1718 The @samp{$} character introduces parameter expansion,
1719 command substitution, or arithmetic expansion. The parameter name
1720 or symbol to be expanded may be enclosed in braces, which
1721 are optional but serve to protect the variable to be expanded from
1722 characters immediately following it which could be
1723 interpreted as part of the name.
1725 When braces are used, the matching ending brace is the first @samp{@}}
1726 not escaped by a backslash or within a quoted string, and not within an
1727 embedded arithmetic expansion, command substitution, or parameter
1730 The basic form of parameter expansion is $@{@var{parameter}@}.
1731 The value of @var{parameter} is substituted. The braces are required
1732 when @var{parameter}
1733 is a positional parameter with more than one digit,
1734 or when @var{parameter}
1735 is followed by a character that is not to be
1736 interpreted as part of its name.
1738 If the first character of @var{parameter} is an exclamation point (!),
1739 a level of variable indirection is introduced.
1740 Bash uses the value of the variable formed from the rest of
1741 @var{parameter} as the name of the variable; this variable is then
1742 expanded and that value is used in the rest of the substitution, rather
1743 than the value of @var{parameter} itself.
1744 This is known as @code{indirect expansion}.
1745 The exceptions to this are the expansions of $@{!@var{prefix}@*@}
1746 and $@{!@var{name}[@@]@}
1748 The exclamation point must immediately follow the left brace in order to
1749 introduce indirection.
1751 In each of the cases below, @var{word} is subject to tilde expansion,
1752 parameter expansion, command substitution, and arithmetic expansion.
1754 When not performing substring expansion, using the form described
1755 below, Bash tests for a parameter that is unset or null.
1756 Omitting the colon results in a test only for a parameter that is unset.
1757 Put another way, if the colon is included,
1758 the operator tests for both @var{parameter}'s existence and that its value
1759 is not null; if the colon is omitted, the operator tests only for existence.
1763 @item $@{@var{parameter}:@minus{}@var{word}@}
1764 If @var{parameter} is unset or null, the expansion of
1765 @var{word} is substituted. Otherwise, the value of
1766 @var{parameter} is substituted.
1768 @item $@{@var{parameter}:=@var{word}@}
1770 is unset or null, the expansion of @var{word}
1771 is assigned to @var{parameter}.
1772 The value of @var{parameter} is then substituted.
1773 Positional parameters and special parameters may not be assigned to
1776 @item $@{@var{parameter}:?@var{word}@}
1778 is null or unset, the expansion of @var{word} (or a message
1779 to that effect if @var{word}
1780 is not present) is written to the standard error and the shell, if it
1781 is not interactive, exits. Otherwise, the value of @var{parameter} is
1784 @item $@{@var{parameter}:+@var{word}@}
1786 is null or unset, nothing is substituted, otherwise the expansion of
1787 @var{word} is substituted.
1789 @item $@{@var{parameter}:@var{offset}@}
1790 @itemx $@{@var{parameter}:@var{offset}:@var{length}@}
1791 Expands to up to @var{length} characters of @var{parameter}
1792 starting at the character specified by @var{offset}.
1793 If @var{length} is omitted, expands to the substring of
1794 @var{parameter} starting at the character specified by @var{offset}.
1795 @var{length} and @var{offset} are arithmetic expressions
1796 (@pxref{Shell Arithmetic}).
1797 This is referred to as Substring Expansion.
1799 If @var{offset} evaluates to a number less than zero, the value
1800 is used as an offset from the end of the value of @var{parameter}.
1801 If @var{length} evaluates to a number less than zero, and @var{parameter}
1802 is not @samp{@@} and not an indexed or associative array, it is interpreted
1803 as an offset from the end of the value of @var{parameter} rather than
1804 a number of characters, and the expansion is the characters between the
1806 If @var{parameter} is @samp{@@}, the result is @var{length} positional
1807 parameters beginning at @var{offset}.
1808 If @var{parameter} is an indexed array name subscripted
1809 by @samp{@@} or @samp{*}, the result is the @var{length}
1810 members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}.
1811 A negative @var{offset} is taken relative to one greater than the maximum
1812 index of the specified array.
1813 Substring expansion applied to an associative array produces undefined
1816 Note that a negative offset must be separated from the colon by at least
1817 one space to avoid being confused with the @samp{:-} expansion.
1818 Substring indexing is zero-based unless the positional parameters
1819 are used, in which case the indexing starts at 1 by default.
1820 If @var{offset} is 0, and the positional parameters are used, @code{$@@} is
1821 prefixed to the list.
1823 @item $@{!@var{prefix}*@}
1824 @itemx $@{!@var{prefix}@@@}
1825 Expands to the names of variables whose names begin with @var{prefix},
1826 separated by the first character of the @env{IFS} special variable.
1827 When @samp{@@} is used and the expansion appears within double quotes, each
1828 variable name expands to a separate word.
1830 @item $@{!@var{name}[@@]@}
1831 @itemx $@{!@var{name}[*]@}
1832 If @var{name} is an array variable, expands to the list of array indices
1833 (keys) assigned in @var{name}.
1834 If @var{name} is not an array, expands to 0 if @var{name} is set and null
1836 When @samp{@@} is used and the expansion appears within double quotes, each
1837 key expands to a separate word.
1839 @item $@{#@var{parameter}@}
1840 The length in characters of the expanded value of @var{parameter} is
1842 If @var{parameter} is @samp{*} or @samp{@@}, the value substituted
1843 is the number of positional parameters.
1844 If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@},
1845 the value substituted is the number of elements in the array.
1847 @item $@{@var{parameter}#@var{word}@}
1848 @itemx $@{@var{parameter}##@var{word}@}
1850 is expanded to produce a pattern just as in filename
1851 expansion (@pxref{Filename Expansion}). If the pattern matches
1852 the beginning of the expanded value of @var{parameter},
1853 then the result of the expansion is the expanded value of @var{parameter}
1854 with the shortest matching pattern (the @samp{#} case) or the
1855 longest matching pattern (the @samp{##} case) deleted.
1856 If @var{parameter} is @samp{@@} or @samp{*},
1857 the pattern removal operation is applied to each positional
1858 parameter in turn, and the expansion is the resultant list.
1859 If @var{parameter} is an array variable subscripted with
1860 @samp{@@} or @samp{*},
1861 the pattern removal operation is applied to each member of the
1862 array in turn, and the expansion is the resultant list.
1864 @item $@{@var{parameter}%@var{word}@}
1865 @itemx $@{@var{parameter}%%@var{word}@}
1866 The @var{word} is expanded to produce a pattern just as in
1868 If the pattern matches a trailing portion of the expanded value of
1869 @var{parameter}, then the result of the expansion is the value of
1870 @var{parameter} with the shortest matching pattern (the @samp{%} case)
1871 or the longest matching pattern (the @samp{%%} case) deleted.
1872 If @var{parameter} is @samp{@@} or @samp{*},
1873 the pattern removal operation is applied to each positional
1874 parameter in turn, and the expansion is the resultant list.
1876 is an array variable subscripted with @samp{@@} or @samp{*},
1877 the pattern removal operation is applied to each member of the
1878 array in turn, and the expansion is the resultant list.
1880 @item $@{@var{parameter}/@var{pattern}/@var{string}@}
1882 The @var{pattern} is expanded to produce a pattern just as in
1884 @var{Parameter} is expanded and the longest match of @var{pattern}
1885 against its value is replaced with @var{string}.
1886 If @var{pattern} begins with @samp{/}, all matches of @var{pattern} are
1887 replaced with @var{string}. Normally only the first match is replaced.
1888 If @var{pattern} begins with @samp{#}, it must match at the beginning
1889 of the expanded value of @var{parameter}.
1890 If @var{pattern} begins with @samp{%}, it must match at the end
1891 of the expanded value of @var{parameter}.
1892 If @var{string} is null, matches of @var{pattern} are deleted
1893 and the @code{/} following @var{pattern} may be omitted.
1894 If @var{parameter} is @samp{@@} or @samp{*},
1895 the substitution operation is applied to each positional
1896 parameter in turn, and the expansion is the resultant list.
1898 is an array variable subscripted with @samp{@@} or @samp{*},
1899 the substitution operation is applied to each member of the
1900 array in turn, and the expansion is the resultant list.
1902 @item $@{@var{parameter}^@var{pattern}@}
1903 @itemx $@{@var{parameter}^^@var{pattern}@}
1904 @itemx $@{@var{parameter},@var{pattern}@}
1905 @itemx $@{@var{parameter},,@var{pattern}@}
1906 This expansion modifies the case of alphabetic characters in @var{parameter}.
1907 The @var{pattern} is expanded to produce a pattern just as in
1909 The @samp{^} operator converts lowercase letters matching @var{pattern}
1910 to uppercase; the @samp{,} operator converts matching uppercase letters
1912 The @samp{^^} and @samp{,,} expansions convert each matched character in the
1913 expanded value; the @samp{^} and @samp{,} expansions match and convert only
1914 the first character in the expanded value.
1915 If @var{pattern} is omitted, it is treated like a @samp{?}, which matches
1917 If @var{parameter} is @samp{@@} or @samp{*},
1918 the case modification operation is applied to each positional
1919 parameter in turn, and the expansion is the resultant list.
1921 is an array variable subscripted with @samp{@@} or @samp{*},
1922 the case modification operation is applied to each member of the
1923 array in turn, and the expansion is the resultant list.
1926 @node Command Substitution
1927 @subsection Command Substitution
1928 @cindex command substitution
1930 Command substitution allows the output of a command to replace
1932 Command substitution occurs when a command is enclosed as follows:
1943 Bash performs the expansion by executing @var{command} and
1944 replacing the command substitution with the standard output of the
1945 command, with any trailing newlines deleted.
1946 Embedded newlines are not deleted, but they may be removed during
1948 The command substitution @code{$(cat @var{file})} can be
1949 replaced by the equivalent but faster @code{$(< @var{file})}.
1951 When the old-style backquote form of substitution is used,
1952 backslash retains its literal meaning except when followed by
1953 @samp{$}, @samp{`}, or @samp{\}.
1954 The first backquote not preceded by a backslash terminates the
1955 command substitution.
1956 When using the @code{$(@var{command})} form, all characters between
1957 the parentheses make up the command; none are treated specially.
1959 Command substitutions may be nested. To nest when using the backquoted
1960 form, escape the inner backquotes with backslashes.
1962 If the substitution appears within double quotes, word splitting and
1963 filename expansion are not performed on the results.
1965 @node Arithmetic Expansion
1966 @subsection Arithmetic Expansion
1967 @cindex expansion, arithmetic
1968 @cindex arithmetic expansion
1970 Arithmetic expansion allows the evaluation of an arithmetic expression
1971 and the substitution of the result. The format for arithmetic expansion is:
1974 $(( @var{expression} ))
1977 The expression is treated as if it were within double quotes, but
1978 a double quote inside the parentheses is not treated specially.
1979 All tokens in the expression undergo parameter expansion, command
1980 substitution, and quote removal.
1981 Arithmetic expansions may be nested.
1983 The evaluation is performed according to the rules listed below
1984 (@pxref{Shell Arithmetic}).
1985 If the expression is invalid, Bash prints a message indicating
1986 failure to the standard error and no substitution occurs.
1988 @node Process Substitution
1989 @subsection Process Substitution
1990 @cindex process substitution
1992 Process substitution is supported on systems that support named
1993 pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files.
1994 It takes the form of
2004 The process @var{list} is run with its input or output connected to a
2005 @sc{fifo} or some file in @file{/dev/fd}. The name of this file is
2006 passed as an argument to the current command as the result of the
2007 expansion. If the @code{>(@var{list})} form is used, writing to
2008 the file will provide input for @var{list}. If the
2009 @code{<(@var{list})} form is used, the file passed as an
2010 argument should be read to obtain the output of @var{list}.
2011 Note that no space may appear between the @code{<} or @code{>}
2012 and the left parenthesis, otherwise the construct would be interpreted
2015 When available, process substitution is performed simultaneously with
2016 parameter and variable expansion, command substitution, and arithmetic
2019 @node Word Splitting
2020 @subsection Word Splitting
2021 @cindex word splitting
2023 The shell scans the results of parameter expansion, command substitution,
2024 and arithmetic expansion that did not occur within double quotes for
2027 The shell treats each character of @env{$IFS} as a delimiter, and splits
2028 the results of the other expansions into words on these characters.
2029 If @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>},
2030 the default, then sequences of
2031 @code{ <space>}, @code{<tab>}, and @code{<newline>}
2032 at the beginning and end of the results of the previous
2033 expansions are ignored, and any sequence of @env{IFS}
2034 characters not at the beginning or end serves to delimit words.
2035 If @env{IFS} has a value other than the default, then sequences of
2036 the whitespace characters @code{space} and @code{tab}
2037 are ignored at the beginning and end of the
2038 word, as long as the whitespace character is in the
2039 value of @env{IFS} (an @env{IFS} whitespace character).
2040 Any character in @env{IFS} that is not @env{IFS}
2041 whitespace, along with any adjacent @env{IFS}
2042 whitespace characters, delimits a field. A sequence of @env{IFS}
2043 whitespace characters is also treated as a delimiter.
2044 If the value of @env{IFS} is null, no word splitting occurs.
2046 Explicit null arguments (@code{""} or @code{''}) are retained.
2047 Unquoted implicit null arguments, resulting from the expansion of
2048 parameters that have no values, are removed.
2049 If a parameter with no value is expanded within double quotes, a
2050 null argument results and is retained.
2052 Note that if no expansion occurs, no splitting
2055 @node Filename Expansion
2056 @subsection Filename Expansion
2058 * Pattern Matching:: How the shell matches patterns.
2060 @cindex expansion, filename
2061 @cindex expansion, pathname
2062 @cindex filename expansion
2063 @cindex pathname expansion
2065 After word splitting, unless the @option{-f} option has been set
2066 (@pxref{The Set Builtin}), Bash scans each word for the characters
2067 @samp{*}, @samp{?}, and @samp{[}.
2068 If one of these characters appears, then the word is
2069 regarded as a @var{pattern},
2070 and replaced with an alphabetically sorted list of
2071 filenames matching the pattern (@pxref{Pattern Matching}).
2072 If no matching filenames are found,
2073 and the shell option @code{nullglob} is disabled, the word is left
2075 If the @code{nullglob} option is set, and no matches are found, the word
2077 If the @code{failglob} shell option is set, and no matches are found,
2078 an error message is printed and the command is not executed.
2079 If the shell option @code{nocaseglob} is enabled, the match is performed
2080 without regard to the case of alphabetic characters.
2082 When a pattern is used for filename expansion, the character @samp{.}
2083 at the start of a filename or immediately following a slash
2084 must be matched explicitly, unless the shell option @code{dotglob} is set.
2085 When matching a filename, the slash character must always be
2087 In other cases, the @samp{.} character is not treated specially.
2089 See the description of @code{shopt} in @ref{The Shopt Builtin},
2090 for a description of the @code{nocaseglob}, @code{nullglob},
2091 @code{failglob}, and @code{dotglob} options.
2093 The @env{GLOBIGNORE}
2094 shell variable may be used to restrict the set of filenames matching a
2095 pattern. If @env{GLOBIGNORE}
2096 is set, each matching filename that also matches one of the patterns in
2097 @env{GLOBIGNORE} is removed from the list of matches. The filenames
2098 @file{.} and @file{..}
2099 are always ignored when @env{GLOBIGNORE}
2100 is set and not null.
2101 However, setting @env{GLOBIGNORE} to a non-null value has the effect of
2102 enabling the @code{dotglob}
2103 shell option, so all other filenames beginning with a
2104 @samp{.} will match.
2105 To get the old behavior of ignoring filenames beginning with a
2106 @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}.
2107 The @code{dotglob} option is disabled when @env{GLOBIGNORE}
2110 @node Pattern Matching
2111 @subsubsection Pattern Matching
2112 @cindex pattern matching
2113 @cindex matching, pattern
2115 Any character that appears in a pattern, other than the special pattern
2116 characters described below, matches itself.
2117 The @sc{nul} character may not occur in a pattern.
2118 A backslash escapes the following character; the
2119 escaping backslash is discarded when matching.
2120 The special pattern characters must be quoted if they are to be matched
2123 The special pattern characters have the following meanings:
2126 Matches any string, including the null string.
2127 When the @code{globstar} shell option is enabled, and @samp{*} is used in
2128 a filename expansion context, two adjacent @samp{*}s used as a single
2129 pattern will match all files and zero or more directories and
2131 If followed by a @samp{/}, two adjacent @samp{*}s will match only
2132 directories and subdirectories.
2134 Matches any single character.
2136 Matches any one of the enclosed characters. A pair of characters
2137 separated by a hyphen denotes a @var{range expression};
2138 any character that sorts between those two characters, inclusive,
2139 using the current locale's collating sequence and character set,
2140 is matched. If the first character following the
2141 @samp{[} is a @samp{!} or a @samp{^}
2142 then any character not enclosed is matched. A @samp{@minus{}}
2143 may be matched by including it as the first or last character
2144 in the set. A @samp{]} may be matched by including it as the first
2145 character in the set.
2146 The sorting order of characters in range expressions is determined by
2147 the current locale and the values of the
2148 @env{LC_COLLATE} and @env{LC_ALL} shell variables, if set.
2150 For example, in the default C locale, @samp{[a-dx-z]} is equivalent to
2151 @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in
2152 these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]};
2153 it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain
2154 the traditional interpretation of ranges in bracket expressions, you can
2155 force the use of the C locale by setting the @env{LC_COLLATE} or
2156 @env{LC_ALL} environment variable to the value @samp{C}, or enable the
2157 @code{globasciiranges} shell option.
2159 Within @samp{[} and @samp{]}, @var{character classes} can be specified
2161 @code{[:}@var{class}@code{:]}, where @var{class} is one of the
2162 following classes defined in the @sc{posix} standard:
2164 alnum alpha ascii blank cntrl digit graph lower
2165 print punct space upper word xdigit
2168 A character class matches any character belonging to that class.
2169 The @code{word} character class matches letters, digits, and the character
2172 Within @samp{[} and @samp{]}, an @var{equivalence class} can be
2173 specified using the syntax @code{[=}@var{c}@code{=]}, which
2174 matches all characters with the same collation weight (as defined
2175 by the current locale) as the character @var{c}.
2177 Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]}
2178 matches the collating symbol @var{symbol}.
2181 If the @code{extglob} shell option is enabled using the @code{shopt}
2182 builtin, several extended pattern matching operators are recognized.
2183 In the following description, a @var{pattern-list} is a list of one
2184 or more patterns separated by a @samp{|}.
2185 Composite patterns may be formed using one or more of the following
2189 @item ?(@var{pattern-list})
2190 Matches zero or one occurrence of the given patterns.
2192 @item *(@var{pattern-list})
2193 Matches zero or more occurrences of the given patterns.
2195 @item +(@var{pattern-list})
2196 Matches one or more occurrences of the given patterns.
2198 @item @@(@var{pattern-list})
2199 Matches one of the given patterns.
2201 @item !(@var{pattern-list})
2202 Matches anything except one of the given patterns.
2206 @subsection Quote Removal
2208 After the preceding expansions, all unquoted occurrences of the
2209 characters @samp{\}, @samp{'}, and @samp{"} that did not
2210 result from one of the above expansions are removed.
2213 @section Redirections
2216 Before a command is executed, its input and output
2217 may be @var{redirected}
2218 using a special notation interpreted by the shell.
2219 Redirection may also be used to open and close files for the
2220 current shell execution environment. The following redirection
2221 operators may precede or appear anywhere within a
2222 simple command or may follow a command.
2223 Redirections are processed in the order they appear, from
2226 Each redirection that may be preceded by a file descriptor number
2227 may instead be preceded by a word of the form @{@var{varname}@}.
2228 In this case, for each redirection operator except
2229 >&- and <&-, the shell will allocate a file descriptor greater
2230 than 10 and assign it to @{@var{varname}@}. If >&- or <&- is preceded
2231 by @{@var{varname}@}, the value of @var{varname} defines the file
2232 descriptor to close.
2234 In the following descriptions, if the file descriptor number is
2235 omitted, and the first character of the redirection operator is
2236 @samp{<}, the redirection refers to the standard input (file
2237 descriptor 0). If the first character of the redirection operator
2238 is @samp{>}, the redirection refers to the standard output (file
2241 The word following the redirection operator in the following
2242 descriptions, unless otherwise noted, is subjected to brace expansion,
2243 tilde expansion, parameter expansion, command substitution, arithmetic
2244 expansion, quote removal, filename expansion, and word splitting.
2245 If it expands to more than one word, Bash reports an error.
2247 Note that the order of redirections is significant. For example,
2250 ls > @var{dirlist} 2>&1
2253 directs both standard output (file descriptor 1) and standard error
2254 (file descriptor 2) to the file @var{dirlist}, while the command
2256 ls 2>&1 > @var{dirlist}
2259 directs only the standard output to file @var{dirlist},
2260 because the standard error was made a copy of the standard output
2261 before the standard output was redirected to @var{dirlist}.
2263 Bash handles several filenames specially when they are used in
2264 redirections, as described in the following table:
2267 @item /dev/fd/@var{fd}
2268 If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated.
2271 File descriptor 0 is duplicated.
2274 File descriptor 1 is duplicated.
2277 File descriptor 2 is duplicated.
2279 @item /dev/tcp/@var{host}/@var{port}
2280 If @var{host} is a valid hostname or Internet address, and @var{port}
2281 is an integer port number or service name, Bash attempts to open a TCP
2282 connection to the corresponding socket.
2284 @item /dev/udp/@var{host}/@var{port}
2285 If @var{host} is a valid hostname or Internet address, and @var{port}
2286 is an integer port number or service name, Bash attempts to open a UDP
2287 connection to the corresponding socket.
2290 A failure to open or create a file causes the redirection to fail.
2292 Redirections using file descriptors greater than 9 should be used with
2293 care, as they may conflict with file descriptors the shell uses
2296 @subsection Redirecting Input
2297 Redirection of input causes the file whose name results from
2298 the expansion of @var{word}
2299 to be opened for reading on file descriptor @code{n},
2300 or the standard input (file descriptor 0) if @code{n}
2303 The general format for redirecting input is:
2305 [@var{n}]<@var{word}
2308 @subsection Redirecting Output
2309 Redirection of output causes the file whose name results from
2310 the expansion of @var{word}
2311 to be opened for writing on file descriptor @var{n},
2312 or the standard output (file descriptor 1) if @var{n}
2313 is not specified. If the file does not exist it is created;
2314 if it does exist it is truncated to zero size.
2316 The general format for redirecting output is:
2318 [@var{n}]>[|]@var{word}
2321 If the redirection operator is @samp{>}, and the @code{noclobber}
2322 option to the @code{set} builtin has been enabled, the redirection
2323 will fail if the file whose name results from the expansion of
2324 @var{word} exists and is a regular file.
2325 If the redirection operator is @samp{>|}, or the redirection operator is
2326 @samp{>} and the @code{noclobber} option is not enabled, the redirection
2327 is attempted even if the file named by @var{word} exists.
2329 @subsection Appending Redirected Output
2330 Redirection of output in this fashion
2331 causes the file whose name results from
2332 the expansion of @var{word}
2333 to be opened for appending on file descriptor @var{n},
2334 or the standard output (file descriptor 1) if @var{n}
2335 is not specified. If the file does not exist it is created.
2337 The general format for appending output is:
2339 [@var{n}]>>@var{word}
2342 @subsection Redirecting Standard Output and Standard Error
2343 This construct allows both the
2344 standard output (file descriptor 1) and
2345 the standard error output (file descriptor 2)
2346 to be redirected to the file whose name is the
2347 expansion of @var{word}.
2349 There are two formats for redirecting standard output and
2360 Of the two forms, the first is preferred.
2361 This is semantically equivalent to
2365 When using the second form, @var{word} may not expand to a number or
2366 @samp{-}. If it does, other redirection operators apply
2367 (see Duplicating File Descriptors below) for compatibility reasons.
2369 @subsection Appending Standard Output and Standard Error
2370 This construct allows both the
2371 standard output (file descriptor 1) and
2372 the standard error output (file descriptor 2)
2373 to be appended to the file whose name is the
2374 expansion of @var{word}.
2376 The format for appending standard output and standard error is:
2381 This is semantically equivalent to
2385 (see Duplicating File Descriptors below).
2387 @subsection Here Documents
2388 This type of redirection instructs the shell to read input from the
2389 current source until a line containing only @var{word}
2390 (with no trailing blanks) is seen. All of
2391 the lines read up to that point are then used as the standard
2392 input for a command.
2394 The format of here-documents is:
2396 <<[@minus{}]@var{word}
2401 No parameter and variable expansion, command substitution,
2402 arithmetic expansion, or filename expansion is performed on
2403 @var{word}. If any characters in @var{word} are quoted, the
2404 @var{delimiter} is the result of quote removal on @var{word},
2405 and the lines in the here-document are not expanded.
2406 If @var{word} is unquoted,
2407 all lines of the here-document are subjected to parameter expansion,
2408 command substitution, and arithmetic expansion. In the latter
2409 case, the character sequence @code{\newline} is ignored, and @samp{\}
2410 must be used to quote the characters
2411 @samp{\}, @samp{$}, and @samp{`}.
2413 If the redirection operator is @samp{<<-},
2414 then all leading tab characters are stripped from input lines and the
2415 line containing @var{delimiter}.
2416 This allows here-documents within shell scripts to be indented in a
2419 @subsection Here Strings
2420 A variant of here documents, the format is:
2425 The @var{word} undergoes
2426 brace expansion, tilde expansion, parameter and variable expansion,
2427 command substitution, arithmetic expansion, and quote removal.
2428 Pathname expansion word splitting are not performed.
2429 The result is supplied as a single string to the command on its
2432 @subsection Duplicating File Descriptors
2433 The redirection operator
2435 [@var{n}]<&@var{word}
2438 is used to duplicate input file descriptors.
2440 expands to one or more digits, the file descriptor denoted by @var{n}
2441 is made to be a copy of that file descriptor.
2442 If the digits in @var{word} do not specify a file descriptor open for
2443 input, a redirection error occurs.
2445 evaluates to @samp{-}, file descriptor @var{n} is closed.
2446 If @var{n} is not specified, the standard input (file descriptor 0) is used.
2450 [@var{n}]>&@var{word}
2453 is used similarly to duplicate output file descriptors. If
2454 @var{n} is not specified, the standard output (file descriptor 1) is used.
2455 If the digits in @var{word} do not specify a file descriptor open for
2456 output, a redirection error occurs.
2458 evaluates to @samp{-}, file descriptor @var{n} is closed.
2459 As a special case, if @var{n} is omitted, and @var{word} does not
2460 expand to one or more digits or @samp{-}, the standard output and standard
2461 error are redirected as described previously.
2463 @subsection Moving File Descriptors
2464 The redirection operator
2466 [@var{n}]<&@var{digit}-
2469 moves the file descriptor @var{digit} to file descriptor @var{n},
2470 or the standard input (file descriptor 0) if @var{n} is not specified.
2471 @var{digit} is closed after being duplicated to @var{n}.
2473 Similarly, the redirection operator
2475 [@var{n}]>&@var{digit}-
2478 moves the file descriptor @var{digit} to file descriptor @var{n},
2479 or the standard output (file descriptor 1) if @var{n} is not specified.
2481 @subsection Opening File Descriptors for Reading and Writing
2482 The redirection operator
2484 [@var{n}]<>@var{word}
2487 causes the file whose name is the expansion of @var{word}
2488 to be opened for both reading and writing on file descriptor
2489 @var{n}, or on file descriptor 0 if @var{n}
2490 is not specified. If the file does not exist, it is created.
2492 @node Executing Commands
2493 @section Executing Commands
2496 * Simple Command Expansion:: How Bash expands simple commands before
2498 * Command Search and Execution:: How Bash finds commands and runs them.
2499 * Command Execution Environment:: The environment in which Bash
2500 executes commands that are not
2502 * Environment:: The environment given to a command.
2503 * Exit Status:: The status returned by commands and how Bash
2505 * Signals:: What happens when Bash or a command it runs
2509 @node Simple Command Expansion
2510 @subsection Simple Command Expansion
2511 @cindex command expansion
2513 When a simple command is executed, the shell performs the following
2514 expansions, assignments, and redirections, from left to right.
2518 The words that the parser has marked as variable assignments (those
2519 preceding the command name) and redirections are saved for later
2523 The words that are not variable assignments or redirections are
2524 expanded (@pxref{Shell Expansions}).
2525 If any words remain after expansion, the first word
2526 is taken to be the name of the command and the remaining words are
2530 Redirections are performed as described above (@pxref{Redirections}).
2533 The text after the @samp{=} in each variable assignment undergoes tilde
2534 expansion, parameter expansion, command substitution, arithmetic expansion,
2535 and quote removal before being assigned to the variable.
2538 If no command name results, the variable assignments affect the current
2539 shell environment. Otherwise, the variables are added to the environment
2540 of the executed command and do not affect the current shell environment.
2541 If any of the assignments attempts to assign a value to a readonly variable,
2542 an error occurs, and the command exits with a non-zero status.
2544 If no command name results, redirections are performed, but do not
2545 affect the current shell environment. A redirection error causes the
2546 command to exit with a non-zero status.
2548 If there is a command name left after expansion, execution proceeds as
2549 described below. Otherwise, the command exits. If one of the expansions
2550 contained a command substitution, the exit status of the command is
2551 the exit status of the last command substitution performed. If there
2552 were no command substitutions, the command exits with a status of zero.
2554 @node Command Search and Execution
2555 @subsection Command Search and Execution
2556 @cindex command execution
2557 @cindex command search
2559 After a command has been split into words, if it results in a
2560 simple command and an optional list of arguments, the following
2565 If the command name contains no slashes, the shell attempts to
2566 locate it. If there exists a shell function by that name, that
2567 function is invoked as described in @ref{Shell Functions}.
2570 If the name does not match a function, the shell searches for
2571 it in the list of shell builtins. If a match is found, that
2575 If the name is neither a shell function nor a builtin,
2576 and contains no slashes, Bash searches each element of
2577 @env{$PATH} for a directory containing an executable file
2578 by that name. Bash uses a hash table to remember the full
2579 pathnames of executable files to avoid multiple @env{PATH} searches
2580 (see the description of @code{hash} in @ref{Bourne Shell Builtins}).
2581 A full search of the directories in @env{$PATH}
2582 is performed only if the command is not found in the hash table.
2583 If the search is unsuccessful, the shell searches for a defined shell
2584 function named @code{command_not_found_handle}.
2585 If that function exists, it is invoked with the original command and
2586 the original command's arguments as its arguments, and the function's
2587 exit status becomes the exit status of the shell.
2588 If that function is not defined, the shell prints an error
2589 message and returns an exit status of 127.
2592 If the search is successful, or if the command name contains
2593 one or more slashes, the shell executes the named program in
2594 a separate execution environment.
2595 Argument 0 is set to the name given, and the remaining arguments
2596 to the command are set to the arguments supplied, if any.
2599 If this execution fails because the file is not in executable
2600 format, and the file is not a directory, it is assumed to be a
2601 @var{shell script} and the shell executes it as described in
2602 @ref{Shell Scripts}.
2605 If the command was not begun asynchronously, the shell waits for
2606 the command to complete and collects its exit status.
2610 @node Command Execution Environment
2611 @subsection Command Execution Environment
2612 @cindex execution environment
2614 The shell has an @var{execution environment}, which consists of the
2619 open files inherited by the shell at invocation, as modified by
2620 redirections supplied to the @code{exec} builtin
2623 the current working directory as set by @code{cd}, @code{pushd}, or
2624 @code{popd}, or inherited by the shell at invocation
2627 the file creation mode mask as set by @code{umask} or inherited from
2631 current traps set by @code{trap}
2634 shell parameters that are set by variable assignment or with @code{set}
2635 or inherited from the shell's parent in the environment
2638 shell functions defined during execution or inherited from the shell's
2639 parent in the environment
2642 options enabled at invocation (either by default or with command-line
2643 arguments) or by @code{set}
2646 options enabled by @code{shopt} (@pxref{The Shopt Builtin})
2649 shell aliases defined with @code{alias} (@pxref{Aliases})
2652 various process @sc{id}s, including those of background jobs
2653 (@pxref{Lists}), the value of @code{$$}, and the value of
2658 When a simple command other than a builtin or shell function
2659 is to be executed, it
2660 is invoked in a separate execution environment that consists of
2661 the following. Unless otherwise noted, the values are inherited
2666 the shell's open files, plus any modifications and additions specified
2667 by redirections to the command
2670 the current working directory
2673 the file creation mode mask
2676 shell variables and functions marked for export, along with variables
2677 exported for the command, passed in the environment (@pxref{Environment})
2680 traps caught by the shell are reset to the values inherited from the
2681 shell's parent, and traps ignored by the shell are ignored
2685 A command invoked in this separate environment cannot affect the
2686 shell's execution environment.
2688 Command substitution, commands grouped with parentheses,
2689 and asynchronous commands are invoked in a
2690 subshell environment that is a duplicate of the shell environment,
2691 except that traps caught by the shell are reset to the values
2692 that the shell inherited from its parent at invocation. Builtin
2693 commands that are invoked as part of a pipeline are also executed
2694 in a subshell environment. Changes made to the subshell environment
2695 cannot affect the shell's execution environment.
2697 Subshells spawned to execute command substitutions inherit the value of
2698 the @option{-e} option from the parent shell. When not in @sc{posix} mode,
2699 Bash clears the @option{-e} option in such subshells.
2701 If a command is followed by a @samp{&} and job control is not active, the
2702 default standard input for the command is the empty file @file{/dev/null}.
2703 Otherwise, the invoked command inherits the file descriptors of the calling
2704 shell as modified by redirections.
2707 @subsection Environment
2710 When a program is invoked it is given an array of strings
2711 called the @var{environment}.
2712 This is a list of name-value pairs, of the form @code{name=value}.
2714 Bash provides several ways to manipulate the environment.
2715 On invocation, the shell scans its own environment and
2716 creates a parameter for each name found, automatically marking
2718 to child processes. Executed commands inherit the environment.
2719 The @code{export} and @samp{declare -x}
2720 commands allow parameters and functions to be added to and
2721 deleted from the environment. If the value of a parameter
2722 in the environment is modified, the new value becomes part
2723 of the environment, replacing the old. The environment
2724 inherited by any executed command consists of the shell's
2725 initial environment, whose values may be modified in the shell,
2726 less any pairs removed by the @code{unset} and @samp{export -n}
2727 commands, plus any additions via the @code{export} and
2728 @samp{declare -x} commands.
2730 The environment for any simple command
2731 or function may be augmented temporarily by prefixing it with
2732 parameter assignments, as described in @ref{Shell Parameters}.
2733 These assignment statements affect only the environment seen
2736 If the @option{-k} option is set (@pxref{The Set Builtin}), then all
2737 parameter assignments are placed in the environment for a command,
2738 not just those that precede the command name.
2740 When Bash invokes an external command, the variable @samp{$_}
2741 is set to the full pathname of the command and passed to that
2742 command in its environment.
2745 @subsection Exit Status
2748 The exit status of an executed command is the value returned by the
2749 @var{waitpid} system call or equivalent function. Exit statuses
2750 fall between 0 and 255, though, as explained below, the shell may
2751 use values above 125 specially. Exit statuses from shell builtins and
2752 compound commands are also limited to this range. Under certain
2753 circumstances, the shell will use special values to indicate specific
2756 For the shell's purposes, a command which exits with a
2757 zero exit status has succeeded.
2758 A non-zero exit status indicates failure.
2759 This seemingly counter-intuitive scheme is used so there
2760 is one well-defined way to indicate success and a variety of
2761 ways to indicate various failure modes.
2762 When a command terminates on a fatal signal whose number is @var{N},
2763 Bash uses the value 128+@var{N} as the exit status.
2765 If a command is not found, the child process created to
2766 execute it returns a status of 127. If a command is found
2767 but is not executable, the return status is 126.
2769 If a command fails because of an error during expansion or redirection,
2770 the exit status is greater than zero.
2772 The exit status is used by the Bash conditional commands
2773 (@pxref{Conditional Constructs}) and some of the list
2774 constructs (@pxref{Lists}).
2776 All of the Bash builtins return an exit status of zero if they succeed
2777 and a non-zero status on failure, so they may be used by the
2778 conditional and list constructs.
2779 All builtins return an exit status of 2 to indicate incorrect usage.
2783 @cindex signal handling
2785 When Bash is interactive, in the absence of any traps, it ignores
2786 @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell),
2788 is caught and handled (so that the @code{wait} builtin is interruptible).
2789 When Bash receives a @code{SIGINT}, it breaks out of any executing loops.
2790 In all cases, Bash ignores @code{SIGQUIT}.
2791 If job control is in effect (@pxref{Job Control}), Bash
2792 ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
2794 Non-builtin commands started by Bash have signal handlers set to the
2795 values inherited by the shell from its parent.
2796 When job control is not in effect, asynchronous commands
2797 ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited
2799 Commands run as a result of
2800 command substitution ignore the keyboard-generated job control signals
2801 @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
2803 The shell exits by default upon receipt of a @code{SIGHUP}.
2804 Before exiting, an interactive shell resends the @code{SIGHUP} to
2805 all jobs, running or stopped.
2806 Stopped jobs are sent @code{SIGCONT} to ensure that they receive
2808 To prevent the shell from sending the @code{SIGHUP} signal to a
2809 particular job, it should be removed
2810 from the jobs table with the @code{disown}
2811 builtin (@pxref{Job Control Builtins}) or marked
2812 to not receive @code{SIGHUP} using @code{disown -h}.
2814 If the @code{huponexit} shell option has been set with @code{shopt}
2815 (@pxref{The Shopt Builtin}), Bash sends a @code{SIGHUP} to all jobs when
2816 an interactive login shell exits.
2818 If Bash is waiting for a command to complete and receives a signal
2819 for which a trap has been set, the trap will not be executed until
2820 the command completes.
2821 When Bash is waiting for an asynchronous
2822 command via the @code{wait} builtin, the reception of a signal for
2823 which a trap has been set will cause the @code{wait} builtin to return
2824 immediately with an exit status greater than 128, immediately after
2825 which the trap is executed.
2828 @section Shell Scripts
2829 @cindex shell script
2831 A shell script is a text file containing shell commands. When such
2832 a file is used as the first non-option argument when invoking Bash,
2833 and neither the @option{-c} nor @option{-s} option is supplied
2834 (@pxref{Invoking Bash}),
2835 Bash reads and executes commands from the file, then exits. This
2836 mode of operation creates a non-interactive shell. The shell first
2837 searches for the file in the current directory, and looks in the
2838 directories in @env{$PATH} if not found there.
2841 a shell script, it sets the special parameter @code{0} to the name
2842 of the file, rather than the name of the shell, and the positional
2843 parameters are set to the remaining arguments, if any are given.
2844 If no additional arguments are supplied, the positional parameters
2847 A shell script may be made executable by using the @code{chmod} command
2848 to turn on the execute bit. When Bash finds such a file while
2849 searching the @env{$PATH} for a command, it spawns a subshell to
2850 execute it. In other words, executing
2852 filename @var{arguments}
2855 is equivalent to executing
2857 bash filename @var{arguments}
2861 if @code{filename} is an executable shell script.
2862 This subshell reinitializes itself, so that the effect is as if a
2863 new shell had been invoked to interpret the script, with the
2864 exception that the locations of commands remembered by the parent
2865 (see the description of @code{hash} in @ref{Bourne Shell Builtins})
2866 are retained by the child.
2868 Most versions of Unix make this a part of the operating system's command
2869 execution mechanism. If the first line of a script begins with
2870 the two characters @samp{#!}, the remainder of the line specifies
2871 an interpreter for the program.
2872 Thus, you can specify Bash, @code{awk}, Perl, or some other
2873 interpreter and write the rest of the script file in that language.
2875 The arguments to the interpreter
2876 consist of a single optional argument following the interpreter
2877 name on the first line of the script file, followed by the name of
2878 the script file, followed by the rest of the arguments. Bash
2879 will perform this action on operating systems that do not handle it
2880 themselves. Note that some older versions of Unix limit the interpreter
2881 name and argument to a maximum of 32 characters.
2883 Bash scripts often begin with @code{#! /bin/bash} (assuming that
2884 Bash has been installed in @file{/bin}), since this ensures that
2885 Bash will be used to interpret the script, even if it is executed
2886 under another shell.
2888 @node Shell Builtin Commands
2889 @chapter Shell Builtin Commands
2892 * Bourne Shell Builtins:: Builtin commands inherited from the Bourne
2894 * Bash Builtins:: Table of builtins specific to Bash.
2895 * Modifying Shell Behavior:: Builtins to modify shell attributes and
2897 * Special Builtins:: Builtin commands classified specially by
2901 Builtin commands are contained within the shell itself.
2902 When the name of a builtin command is used as the first word of
2903 a simple command (@pxref{Simple Commands}), the shell executes
2904 the command directly, without invoking another program.
2905 Builtin commands are necessary to implement functionality impossible
2906 or inconvenient to obtain with separate utilities.
2908 This section briefly describes the builtins which Bash inherits from
2909 the Bourne Shell, as well as the builtin commands which are unique
2910 to or have been extended in Bash.
2912 Several builtin commands are described in other chapters: builtin
2913 commands which provide the Bash interface to the job control
2914 facilities (@pxref{Job Control Builtins}), the directory stack
2915 (@pxref{Directory Stack Builtins}), the command history
2916 (@pxref{Bash History Builtins}), and the programmable completion
2917 facilities (@pxref{Programmable Completion Builtins}).
2919 Many of the builtins have been extended by @sc{posix} or Bash.
2921 Unless otherwise noted, each builtin command documented as accepting
2922 options preceded by @samp{-} accepts @samp{--}
2923 to signify the end of the options.
2924 The @code{:}, @code{true}, @code{false}, and @code{test}
2925 builtins do not accept options and do not treat @samp{--} specially.
2926 The @code{exit}, @code{logout}, @code{break}, @code{continue}, @code{let},
2927 and @code{shift} builtins accept and process arguments beginning
2928 with @samp{-} without requiring @samp{--}.
2929 Other builtins that accept arguments but are not specified as accepting
2930 options interpret arguments beginning with @samp{-} as invalid options and
2931 require @samp{--} to prevent this interpretation.
2933 @node Bourne Shell Builtins
2934 @section Bourne Shell Builtins
2936 The following shell builtin commands are inherited from the Bourne Shell.
2937 These commands are implemented as specified by the @sc{posix} standard.
2940 @item : @r{(a colon)}
2946 Do nothing beyond expanding @var{arguments} and performing redirections.
2947 The return status is zero.
2949 @item . @r{(a period)}
2952 . @var{filename} [@var{arguments}]
2955 Read and execute commands from the @var{filename} argument in the
2956 current shell context. If @var{filename} does not contain a slash,
2957 the @env{PATH} variable is used to find @var{filename}.
2958 When Bash is not in @sc{posix} mode, the current directory is searched
2959 if @var{filename} is not found in @env{$PATH}.
2960 If any @var{arguments} are supplied, they become the positional
2961 parameters when @var{filename} is executed. Otherwise the positional
2962 parameters are unchanged.
2963 The return status is the exit status of the last command executed, or
2964 zero if no commands are executed. If @var{filename} is not found, or
2965 cannot be read, the return status is non-zero.
2966 This builtin is equivalent to @code{source}.
2974 Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop.
2975 If @var{n} is supplied, the @var{n}th enclosing loop is exited.
2976 @var{n} must be greater than or equal to 1.
2977 The return status is zero unless @var{n} is not greater than or equal to 1.
2982 cd [-L|[-P [-e]]] [@var{directory}]
2985 Change the current working directory to @var{directory}.
2986 If @var{directory} is not given, the value of the @env{HOME} shell
2988 If the shell variable @env{CDPATH} exists, it is used as a search path.
2989 If @var{directory} begins with a slash, @env{CDPATH} is not used.
2991 The @option{-P} option means to not follow symbolic links; symbolic
2992 links are followed by default or with the @option{-L} option.
2993 If the @option{-e} option is supplied with @option{-P}
2994 and the current working directory cannot be successfully determined
2995 after a successful directory change, @code{cd} will return an unsuccessful
2997 If @var{directory} is @samp{-}, it is converted to @env{$OLDPWD}
2998 before the directory change is attempted.
3000 If a non-empty directory name from @env{CDPATH} is used, or if
3001 @samp{-} is the first argument, and the directory change is
3002 successful, the absolute pathname of the new working directory is
3003 written to the standard output.
3005 The return status is zero if the directory is successfully changed,
3014 Resume the next iteration of an enclosing @code{for}, @code{while},
3015 @code{until}, or @code{select} loop.
3016 If @var{n} is supplied, the execution of the @var{n}th enclosing loop
3018 @var{n} must be greater than or equal to 1.
3019 The return status is zero unless @var{n} is not greater than or equal to 1.
3024 eval [@var{arguments}]
3027 The arguments are concatenated together into a single command, which is
3028 then read and executed, and its exit status returned as the exit status
3030 If there are no arguments or only empty arguments, the return status is
3036 exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]]
3040 is supplied, it replaces the shell without creating a new process.
3041 If the @option{-l} option is supplied, the shell places a dash at the
3042 beginning of the zeroth argument passed to @var{command}.
3043 This is what the @code{login} program does.
3044 The @option{-c} option causes @var{command} to be executed with an empty
3046 If @option{-a} is supplied, the shell passes @var{name} as the zeroth
3047 argument to @var{command}.
3049 cannot be executed for some reason, a non-interactive shell exits,
3050 unless the @code{execfail} shell option
3051 is enabled. In that case, it returns failure.
3052 An interactive shell returns failure if the file cannot be executed.
3053 If no @var{command} is specified, redirections may be used to affect
3054 the current shell environment. If there are no redirection errors, the
3055 return status is zero; otherwise the return status is non-zero.
3063 Exit the shell, returning a status of @var{n} to the shell's parent.
3064 If @var{n} is omitted, the exit status is that of the last command executed.
3065 Any trap on @code{EXIT} is executed before the shell terminates.
3070 export [-fn] [-p] [@var{name}[=@var{value}]]
3073 Mark each @var{name} to be passed to child processes
3074 in the environment. If the @option{-f} option is supplied, the @var{name}s
3075 refer to shell functions; otherwise the names refer to shell variables.
3076 The @option{-n} option means to no longer mark each @var{name} for export.
3077 If no @var{names} are supplied, or if the @option{-p} option is given, a
3078 list of names of all exported variables is displayed.
3079 The @option{-p} option displays output in a form that may be reused as input.
3080 If a variable name is followed by =@var{value}, the value of
3081 the variable is set to @var{value}.
3083 The return status is zero unless an invalid option is supplied, one of
3084 the names is not a valid shell variable name, or @option{-f} is supplied
3085 with a name that is not a shell function.
3090 getopts @var{optstring} @var{name} [@var{args}]
3093 @code{getopts} is used by shell scripts to parse positional parameters.
3094 @var{optstring} contains the option characters to be recognized; if a
3095 character is followed by a colon, the option is expected to have an
3096 argument, which should be separated from it by whitespace.
3097 The colon (@samp{:}) and question mark (@samp{?}) may not be
3098 used as option characters.
3099 Each time it is invoked, @code{getopts}
3100 places the next option in the shell variable @var{name}, initializing
3101 @var{name} if it does not exist,
3102 and the index of the next argument to be processed into the
3103 variable @env{OPTIND}.
3104 @env{OPTIND} is initialized to 1 each time the shell or a shell script
3106 When an option requires an argument,
3107 @code{getopts} places that argument into the variable @env{OPTARG}.
3108 The shell does not reset @env{OPTIND} automatically; it must be manually
3109 reset between multiple calls to @code{getopts} within the same shell
3110 invocation if a new set of parameters is to be used.
3112 When the end of options is encountered, @code{getopts} exits with a
3113 return value greater than zero.
3114 @env{OPTIND} is set to the index of the first non-option argument,
3115 and @var{name} is set to @samp{?}.
3118 normally parses the positional parameters, but if more arguments are
3119 given in @var{args}, @code{getopts} parses those instead.
3121 @code{getopts} can report errors in two ways. If the first character of
3122 @var{optstring} is a colon, @var{silent}
3123 error reporting is used. In normal operation, diagnostic messages
3124 are printed when invalid options or missing option arguments are
3126 If the variable @env{OPTERR}
3127 is set to 0, no error messages will be displayed, even if the first
3128 character of @code{optstring} is not a colon.
3130 If an invalid option is seen,
3131 @code{getopts} places @samp{?} into @var{name} and, if not silent,
3132 prints an error message and unsets @env{OPTARG}.
3133 If @code{getopts} is silent, the option character found is placed in
3134 @env{OPTARG} and no diagnostic message is printed.
3136 If a required argument is not found, and @code{getopts}
3137 is not silent, a question mark (@samp{?}) is placed in @var{name},
3138 @code{OPTARG} is unset, and a diagnostic message is printed.
3139 If @code{getopts} is silent, then a colon (@samp{:}) is placed in
3140 @var{name} and @env{OPTARG} is set to the option character found.
3145 hash [-r] [-p @var{filename}] [-dt] [@var{name}]
3148 Each time @code{hash} is invoked, it remembers the full pathnames of the
3149 commands specified as @var{name} arguments,
3150 so they need not be searched for on subsequent invocations.
3151 The commands are found by searching through the directories listed in
3153 Any previously-remembered pathname is discarded.
3154 The @option{-p} option inhibits the path search, and @var{filename} is
3155 used as the location of @var{name}.
3156 The @option{-r} option causes the shell to forget all remembered locations.
3157 The @option{-d} option causes the shell to forget the remembered location
3159 If the @option{-t} option is supplied, the full pathname to which each
3160 @var{name} corresponds is printed. If multiple @var{name} arguments are
3161 supplied with @option{-t} the @var{name} is printed before the hashed
3163 The @option{-l} option causes output to be displayed in a format
3164 that may be reused as input.
3165 If no arguments are given, or if only @option{-l} is supplied,
3166 information about remembered commands is printed.
3167 The return status is zero unless a @var{name} is not found or an invalid
3176 Print the absolute pathname of the current working directory.
3177 If the @option{-P} option is supplied, the pathname printed will not
3178 contain symbolic links.
3179 If the @option{-L} option is supplied, the pathname printed may contain
3181 The return status is zero unless an error is encountered while
3182 determining the name of the current directory or an invalid option
3188 readonly [-aAf] [-p] [@var{name}[=@var{value}]] @dots{}
3191 Mark each @var{name} as readonly.
3192 The values of these names may not be changed by subsequent assignment.
3193 If the @option{-f} option is supplied, each @var{name} refers to a shell
3195 The @option{-a} option means each @var{name} refers to an indexed
3196 array variable; the @option{-A} option means each @var{name} refers
3197 to an associative array variable.
3198 If both options are supplied, @option{-A} takes precedence.
3199 If no @var{name} arguments are given, or if the @option{-p}
3200 option is supplied, a list of all readonly names is printed.
3201 The other options may be used to restrict the output to a subset of
3202 the set of readonly names.
3203 The @option{-p} option causes output to be displayed in a format that
3204 may be reused as input.
3205 If a variable name is followed by =@var{value}, the value of
3206 the variable is set to @var{value}.
3207 The return status is zero unless an invalid option is supplied, one of
3208 the @var{name} arguments is not a valid shell variable or function name,
3209 or the @option{-f} option is supplied with a name that is not a shell function.
3217 Cause a shell function to stop executing and return the value @var{n}
3219 If @var{n} is not supplied, the return value is the exit status of the
3220 last command executed in the function.
3221 @code{return} may also be used to terminate execution of a script
3222 being executed with the @code{.} (@code{source}) builtin,
3223 returning either @var{n} or
3224 the exit status of the last command executed within the script as the exit
3225 status of the script.
3226 If @var{n} is supplied, the return value is its least significant
3228 Any command associated with the @code{RETURN} trap is executed
3229 before execution resumes after the function or script.
3230 The return status is non-zero if @code{return} is supplied a non-numeric
3231 argument or is used outside a function
3232 and not during the execution of a script by @code{.} or @code{source}.
3240 Shift the positional parameters to the left by @var{n}.
3241 The positional parameters from @var{n}+1 @dots{} @code{$#} are
3242 renamed to @code{$1} @dots{} @code{$#}-@var{n}.
3243 Parameters represented by the numbers @code{$#} to @code{$#}-@var{n}+1
3245 @var{n} must be a non-negative number less than or equal to @code{$#}.
3246 If @var{n} is zero or greater than @code{$#}, the positional parameters
3248 If @var{n} is not supplied, it is assumed to be 1.
3249 The return status is zero unless @var{n} is greater than @code{$#} or
3250 less than zero, non-zero otherwise.
3260 Evaluate a conditional expression @var{expr} and return a status of 0
3261 (true) or 1 (false).
3262 Each operator and operand must be a separate argument.
3263 Expressions are composed of the primaries described below in
3264 @ref{Bash Conditional Expressions}.
3265 @code{test} does not accept any options, nor does it accept and ignore
3266 an argument of @option{--} as signifying the end of options.
3268 When the @code{[} form is used, the last argument to the command must
3271 Expressions may be combined using the following operators, listed in
3272 decreasing order of precedence.
3273 The evaluation depends on the number of arguments; see below.
3274 Operator precedence is used when there are five or more arguments.
3278 True if @var{expr} is false.
3280 @item ( @var{expr} )
3281 Returns the value of @var{expr}.
3282 This may be used to override the normal precedence of operators.
3284 @item @var{expr1} -a @var{expr2}
3285 True if both @var{expr1} and @var{expr2} are true.
3287 @item @var{expr1} -o @var{expr2}
3288 True if either @var{expr1} or @var{expr2} is true.
3291 The @code{test} and @code{[} builtins evaluate conditional
3292 expressions using a set of rules based on the number of arguments.
3296 The expression is false.
3299 The expression is true if and only if the argument is not null.
3302 If the first argument is @samp{!}, the expression is true if and
3303 only if the second argument is null.
3304 If the first argument is one of the unary conditional operators
3305 (@pxref{Bash Conditional Expressions}), the expression
3306 is true if the unary test is true.
3307 If the first argument is not a valid unary operator, the expression is
3311 The following conditions are applied in the order listed.
3312 If the second argument is one of the binary conditional
3313 operators (@pxref{Bash Conditional Expressions}), the
3314 result of the expression is the result of the binary test using the
3315 first and third arguments as operands.
3316 The @samp{-a} and @samp{-o} operators are considered binary operators
3317 when there are three arguments.
3318 If the first argument is @samp{!}, the value is the negation of
3319 the two-argument test using the second and third arguments.
3320 If the first argument is exactly @samp{(} and the third argument is
3321 exactly @samp{)}, the result is the one-argument test of the second
3323 Otherwise, the expression is false.
3326 If the first argument is @samp{!}, the result is the negation of
3327 the three-argument expression composed of the remaining arguments.
3328 Otherwise, the expression is parsed and evaluated according to
3329 precedence using the rules listed above.
3331 @item 5 or more arguments
3332 The expression is parsed and evaluated according to precedence
3333 using the rules listed above.
3336 When used with @code{test} or @samp{[}, the @samp{<} and @samp{>}
3337 operators sort lexicographically using ASCII ordering.
3345 Print out the user and system times used by the shell and its children.
3346 The return status is zero.
3351 trap [-lp] [@var{arg}] [@var{sigspec} @dots{}]
3354 The commands in @var{arg} are to be read and executed when the
3355 shell receives signal @var{sigspec}. If @var{arg} is absent (and
3356 there is a single @var{sigspec}) or
3357 equal to @samp{-}, each specified signal's disposition is reset
3358 to the value it had when the shell was started.
3359 If @var{arg} is the null string, then the signal specified by
3360 each @var{sigspec} is ignored by the shell and commands it invokes.
3361 If @var{arg} is not present and @option{-p} has been supplied,
3362 the shell displays the trap commands associated with each @var{sigspec}.
3363 If no arguments are supplied, or
3364 only @option{-p} is given, @code{trap} prints the list of commands
3365 associated with each signal number in a form that may be reused as
3367 The @option{-l} option causes the shell to print a list of signal names
3368 and their corresponding numbers.
3369 Each @var{sigspec} is either a signal name or a signal number.
3370 Signal names are case insensitive and the @code{SIG} prefix is optional.
3373 is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits.
3374 If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed
3375 before every simple command, @code{for} command, @code{case} command,
3376 @code{select} command, every arithmetic @code{for} command, and before
3377 the first command executes in a shell function.
3378 Refer to the description of the @code{extdebug} option to the
3379 @code{shopt} builtin (@pxref{The Shopt Builtin}) for details of its
3380 effect on the @code{DEBUG} trap.
3381 If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed
3382 each time a shell function or a script executed with the @code{.} or
3383 @code{source} builtins finishes executing.
3385 If a @var{sigspec} is @code{ERR}, the command @var{arg}
3386 is executed whenever a simple command has a non-zero exit status,
3387 subject to the following conditions.
3388 The @code{ERR} trap is not executed if the failed command is part of the
3389 command list immediately following an @code{until} or @code{while} keyword,
3390 part of the test following the @code{if} or @code{elif} reserved words,
3391 part of a command executed in a @code{&&} or @code{||} list,
3392 or if the command's return
3393 status is being inverted using @code{!}.
3394 These are the same conditions obeyed by the @code{errexit} option.
3396 Signals ignored upon entry to the shell cannot be trapped or reset.
3397 Trapped signals that are not being ignored are reset to their original
3398 values in a subshell or subshell environment when one is created.
3400 The return status is zero unless a @var{sigspec} does not specify a
3406 umask [-p] [-S] [@var{mode}]
3409 Set the shell process's file creation mask to @var{mode}. If
3410 @var{mode} begins with a digit, it is interpreted as an octal number;
3411 if not, it is interpreted as a symbolic mode mask similar
3412 to that accepted by the @code{chmod} command. If @var{mode} is
3413 omitted, the current value of the mask is printed. If the @option{-S}
3414 option is supplied without a @var{mode} argument, the mask is printed
3415 in a symbolic format.
3416 If the @option{-p} option is supplied, and @var{mode}
3417 is omitted, the output is in a form that may be reused as input.
3418 The return status is zero if the mode is successfully changed or if
3419 no @var{mode} argument is supplied, and non-zero otherwise.
3421 Note that when the mode is interpreted as an octal number, each number
3422 of the umask is subtracted from @code{7}. Thus, a umask of @code{022}
3423 results in permissions of @code{755}.
3428 unset [-fv] [@var{name}]
3431 Remove each variable or function @var{name}.
3432 If the @option{-v} option is given, each
3433 @var{name} refers to a shell variable and that variable is remvoved.
3434 If the @option{-f} option is given, the @var{name}s refer to shell
3435 functions, and the function definition is removed.
3436 If no options are supplied, each @var{name} refers to a variable; if
3437 there is no variable by that name, any function with that name is
3439 Readonly variables and functions may not be unset.
3440 The return status is zero unless a @var{name} is readonly.
3444 @section Bash Builtin Commands
3446 This section describes builtin commands which are unique to
3447 or have been extended in Bash.
3448 Some of these commands are specified in the @sc{posix} standard.
3455 alias [-p] [@var{name}[=@var{value}] @dots{}]
3458 Without arguments or with the @option{-p} option, @code{alias} prints
3459 the list of aliases on the standard output in a form that allows
3460 them to be reused as input.
3461 If arguments are supplied, an alias is defined for each @var{name}
3462 whose @var{value} is given. If no @var{value} is given, the name
3463 and value of the alias is printed.
3464 Aliases are described in @ref{Aliases}.
3469 bind [-m @var{keymap}] [-lpsvPSV]
3470 bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}]
3471 bind [-m @var{keymap}] -f @var{filename}
3472 bind [-m @var{keymap}] -x @var{keyseq:shell-command}
3473 bind [-m @var{keymap}] @var{keyseq:function-name}
3474 bind @var{readline-command}
3477 Display current Readline (@pxref{Command Line Editing})
3478 key and function bindings,
3479 bind a key sequence to a Readline function or macro,
3480 or set a Readline variable.
3481 Each non-option argument is a command as it would appear in a
3482 Readline initialization file (@pxref{Readline Init File}),
3483 but each binding or command must be passed as a separate argument; e.g.,
3484 @samp{"\C-x\C-r":re-read-init-file}.
3486 Options, if supplied, have the following meanings:
3489 @item -m @var{keymap}
3490 Use @var{keymap} as the keymap to be affected by
3491 the subsequent bindings. Acceptable @var{keymap}
3494 @code{emacs-standard},
3499 @code{vi-command}, and
3501 @code{vi} is equivalent to @code{vi-command};
3502 @code{emacs} is equivalent to @code{emacs-standard}.
3505 List the names of all Readline functions.
3508 Display Readline function names and bindings in such a way that they
3509 can be used as input or in a Readline initialization file.
3512 List current Readline function names and bindings.
3515 Display Readline variable names and values in such a way that they
3516 can be used as input or in a Readline initialization file.
3519 List current Readline variable names and values.
3522 Display Readline key sequences bound to macros and the strings they output
3523 in such a way that they can be used as input or in a Readline
3524 initialization file.
3527 Display Readline key sequences bound to macros and the strings they output.
3529 @item -f @var{filename}
3530 Read key bindings from @var{filename}.
3532 @item -q @var{function}
3533 Query about which keys invoke the named @var{function}.
3535 @item -u @var{function}
3536 Unbind all keys bound to the named @var{function}.
3538 @item -r @var{keyseq}
3539 Remove any current binding for @var{keyseq}.
3541 @item -x @var{keyseq:shell-command}
3542 Cause @var{shell-command} to be executed whenever @var{keyseq} is
3544 When @var{shell-command} is executed, the shell sets the
3545 @code{READLINE_LINE} variable to the contents of the Readline line
3546 buffer and the @code{READLINE_POINT} variable to the current location
3547 of the insertion point.
3548 If the executed command changes the value of @code{READLINE_LINE} or
3549 @code{READLINE_POINT}, those new values will be reflected in the
3554 The return status is zero unless an invalid option is supplied or an
3560 builtin [@var{shell-builtin} [@var{args}]]
3563 Run a shell builtin, passing it @var{args}, and return its exit status.
3564 This is useful when defining a shell function with the same
3565 name as a shell builtin, retaining the functionality of the builtin within
3567 The return status is non-zero if @var{shell-builtin} is not a shell
3576 Returns the context of any active subroutine call (a shell function or
3577 a script executed with the @code{.} or @code{source} builtins).
3579 Without @var{expr}, @code{caller} displays the line number and source
3580 filename of the current subroutine call.
3581 If a non-negative integer is supplied as @var{expr}, @code{caller}
3582 displays the line number, subroutine name, and source file corresponding
3583 to that position in the current execution call stack. This extra
3584 information may be used, for example, to print a stack trace. The
3585 current frame is frame 0.
3587 The return value is 0 unless the shell is not executing a subroutine
3588 call or @var{expr} does not correspond to a valid position in the
3594 command [-pVv] @var{command} [@var{arguments} @dots{}]
3597 Runs @var{command} with @var{arguments} ignoring any shell function
3598 named @var{command}.
3599 Only shell builtin commands or commands found by searching the
3600 @env{PATH} are executed.
3601 If there is a shell function named @code{ls}, running @samp{command ls}
3602 within the function will execute the external command @code{ls}
3603 instead of calling the function recursively.
3604 The @option{-p} option means to use a default value for @env{PATH}
3605 that is guaranteed to find all of the standard utilities.
3606 The return status in this case is 127 if @var{command} cannot be
3607 found or an error occurred, and the exit status of @var{command}
3610 If either the @option{-V} or @option{-v} option is supplied, a
3611 description of @var{command} is printed. The @option{-v} option
3612 causes a single word indicating the command or file name used to
3613 invoke @var{command} to be displayed; the @option{-V} option produces
3614 a more verbose description. In this case, the return status is
3615 zero if @var{command} is found, and non-zero if not.
3620 declare [-aAfFgilrtux] [-p] [@var{name}[=@var{value}] @dots{}]
3623 Declare variables and give them attributes. If no @var{name}s
3624 are given, then display the values of variables instead.
3626 The @option{-p} option will display the attributes and values of each
3628 When @option{-p} is used with @var{name} arguments, additional options
3631 When @option{-p} is supplied without @var{name} arguments, @code{declare}
3632 will display the attributes and values of all variables having the
3633 attributes specified by the additional options.
3634 If no other options are supplied with @option{-p}, @code{declare} will
3635 display the attributes and values of all shell variables. The @option{-f}
3636 option will restrict the display to shell functions.
3638 The @option{-F} option inhibits the display of function definitions;
3639 only the function name and attributes are printed.
3640 If the @code{extdebug} shell option is enabled using @code{shopt}
3641 (@pxref{The Shopt Builtin}), the source file name and line number where
3642 the function is defined are displayed as well.
3643 @option{-F} implies @option{-f}.
3645 The @option{-g} option forces variables to be created or modified at
3646 the global scope, even when @code{declare} is executed in a shell function.
3647 It is ignored in all other cases.
3649 The following options can be used to restrict output to variables with
3650 the specified attributes or to give variables attributes:
3654 Each @var{name} is an indexed array variable (@pxref{Arrays}).
3657 Each @var{name} is an associative array variable (@pxref{Arrays}).
3660 Use function names only.
3663 The variable is to be treated as
3664 an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is
3665 performed when the variable is assigned a value.
3668 When the variable is assigned a value, all upper-case characters are
3669 converted to lower-case.
3670 The upper-case attribute is disabled.
3673 Make @var{name}s readonly. These names cannot then be assigned values
3674 by subsequent assignment statements or unset.
3677 Give each @var{name} the @code{trace} attribute.
3678 Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from
3680 The trace attribute has no special meaning for variables.
3683 When the variable is assigned a value, all lower-case characters are
3684 converted to upper-case.
3685 The lower-case attribute is disabled.
3688 Mark each @var{name} for export to subsequent commands via
3692 Using @samp{+} instead of @samp{-} turns off the attribute instead,
3693 with the exceptions that @samp{+a}
3694 may not be used to destroy an array variable and @samp{+r} will not
3695 remove the readonly attribute.
3696 When used in a function, @code{declare} makes each @var{name} local,
3697 as with the @code{local} command, unless the @option{-g} option is used.
3698 If a variable name is followed by =@var{value}, the value of the variable
3699 is set to @var{value}.
3701 The return status is zero unless an invalid option is encountered,
3702 an attempt is made to define a function using @samp{-f foo=bar},
3703 an attempt is made to assign a value to a readonly variable,
3704 an attempt is made to assign a value to an array variable without
3705 using the compound assignment syntax (@pxref{Arrays}),
3706 one of the @var{names} is not a valid shell variable name,
3707 an attempt is made to turn off readonly status for a readonly variable,
3708 an attempt is made to turn off array status for an array variable,
3709 or an attempt is made to display a non-existent function with @option{-f}.
3714 echo [-neE] [@var{arg} @dots{}]
3717 Output the @var{arg}s, separated by spaces, terminated with a
3719 The return status is 0 unless a write error occurs.
3720 If @option{-n} is specified, the trailing newline is suppressed.
3721 If the @option{-e} option is given, interpretation of the following
3722 backslash-escaped characters is enabled.
3723 The @option{-E} option disables the interpretation of these escape characters,
3724 even on systems where they are interpreted by default.
3725 The @code{xpg_echo} shell option may be used to
3726 dynamically determine whether or not @code{echo} expands these
3727 escape characters by default.
3728 @code{echo} does not interpret @option{--} to mean the end of options.
3730 @code{echo} interprets the following escape sequences:
3737 suppress further output
3754 the eight-bit character whose value is the octal value @var{nnn}
3755 (zero to three octal digits)
3757 the eight-bit character whose value is the hexadecimal value @var{HH}
3758 (one or two hex digits)
3760 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
3761 @var{HHHH} (one to four hex digits)
3762 @item \U@var{HHHHHHHH}
3763 the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
3764 @var{HHHHHHHH} (one to eight hex digits)
3770 enable [-a] [-dnps] [-f @var{filename}] [@var{name} @dots{}]
3773 Enable and disable builtin shell commands.
3774 Disabling a builtin allows a disk command which has the same name
3775 as a shell builtin to be executed without specifying a full pathname,
3776 even though the shell normally searches for builtins before disk commands.
3777 If @option{-n} is used, the @var{name}s become disabled. Otherwise
3778 @var{name}s are enabled. For example, to use the @code{test} binary
3779 found via @env{$PATH} instead of the shell builtin version, type
3780 @samp{enable -n test}.
3782 If the @option{-p} option is supplied, or no @var{name} arguments appear,
3783 a list of shell builtins is printed. With no other arguments, the list
3784 consists of all enabled shell builtins.
3785 The @option{-a} option means to list
3786 each builtin with an indication of whether or not it is enabled.
3788 The @option{-f} option means to load the new builtin command @var{name}
3789 from shared object @var{filename}, on systems that support dynamic loading.
3790 The @option{-d} option will delete a builtin loaded with @option{-f}.
3792 If there are no options, a list of the shell builtins is displayed.
3793 The @option{-s} option restricts @code{enable} to the @sc{posix} special
3794 builtins. If @option{-s} is used with @option{-f}, the new builtin becomes
3795 a special builtin (@pxref{Special Builtins}).
3797 The return status is zero unless a @var{name} is not a shell builtin
3798 or there is an error loading a new builtin from a shared object.
3803 help [-dms] [@var{pattern}]
3806 Display helpful information about builtin commands.
3807 If @var{pattern} is specified, @code{help} gives detailed help
3808 on all commands matching @var{pattern}, otherwise a list of
3809 the builtins is printed.
3811 Options, if supplied, have the following meanings:
3815 Display a short description of each @var{pattern}
3817 Display the description of each @var{pattern} in a manpage-like format
3819 Display only a short usage synopsis for each @var{pattern}
3822 The return status is zero unless no command matches @var{pattern}.
3827 let @var{expression} [@var{expression} @dots{}]
3830 The @code{let} builtin allows arithmetic to be performed on shell
3831 variables. Each @var{expression} is evaluated according to the
3832 rules given below in @ref{Shell Arithmetic}. If the
3833 last @var{expression} evaluates to 0, @code{let} returns 1;
3834 otherwise 0 is returned.
3839 local [@var{option}] @var{name}[=@var{value}] @dots{}
3842 For each argument, a local variable named @var{name} is created,
3843 and assigned @var{value}.
3844 The @var{option} can be any of the options accepted by @code{declare}.
3845 @code{local} can only be used within a function; it makes the variable
3846 @var{name} have a visible scope restricted to that function and its
3847 children. The return status is zero unless @code{local} is used outside
3848 a function, an invalid @var{name} is supplied, or @var{name} is a
3857 Exit a login shell, returning a status of @var{n} to the shell's
3863 mapfile [-n @var{count}] [-O @var{origin}] [-s @var{count}] [-t] [-u @var{fd}]
3864 [-C @var{callback}] [-c @var{quantum}] [@var{array}]
3867 Read lines from the standard input into the indexed array variable @var{array},
3868 or from file descriptor @var{fd}
3869 if the @option{-u} option is supplied.
3870 The variable @code{MAPFILE} is the default @var{array}.
3871 Options, if supplied, have the following meanings:
3876 Copy at most @var{count} lines. If @var{count} is 0, all lines are copied.
3878 Begin assigning to @var{array} at index @var{origin}.
3879 The default index is 0.
3881 Discard the first @var{count} lines read.
3883 Remove a trailing newline from each line read.
3885 Read lines from file descriptor @var{fd} instead of the standard input.
3887 Evaluate @var{callback} each time @var{quantum}P lines are read.
3888 The @option{-c} option specifies @var{quantum}.
3890 Specify the number of lines read between each call to @var{callback}.
3893 If @option{-C} is specified without @option{-c},
3894 the default quantum is 5000.
3895 When @var{callback} is evaluated, it is supplied the index of the next
3896 array element to be assigned and the line to be assigned to that element
3897 as additional arguments.
3898 @var{callback} is evaluated after the line is read but before the
3899 array element is assigned.
3901 If not supplied with an explicit origin, @code{mapfile} will clear @var{array}
3902 before assigning to it.
3904 @code{mapfile} returns successfully unless an invalid option or option
3905 argument is supplied, @var{array} is invalid or unassignable, or @var{array}
3906 is not an indexed array.
3911 printf [-v @var{var}] @var{format} [@var{arguments}]
3914 Write the formatted @var{arguments} to the standard output under the
3915 control of the @var{format}.
3916 The @option{-v} option causes the output to be assigned to the variable
3917 @var{var} rather than being printed to the standard output.
3919 The @var{format} is a character string which contains three types of objects:
3920 plain characters, which are simply copied to standard output, character
3921 escape sequences, which are converted and copied to the standard output, and
3922 format specifications, each of which causes printing of the next successive
3924 In addition to the standard @code{printf(1)} formats, @code{printf}
3925 interprets the following extensions:
3929 Causes @code{printf} to expand backslash escape sequences in the
3930 corresponding @var{argument},
3931 except that @samp{\c} terminates output, backslashes in
3932 @samp{\'}, @samp{\"}, and @samp{\?} are not removed, and octal escapes
3933 beginning with @samp{\0} may contain up to four digits.
3935 Causes @code{printf} to output the
3936 corresponding @var{argument} in a format that can be reused as shell input.
3937 @item %(@var{datefmt})T
3938 Causes @code{printf} to output the date-time string resulting from using
3939 @var{datefmt} as a format string for @code{strftime}(3). The corresponding
3940 @var{argument} is an integer representing the number of seconds since the
3941 epoch. Two special argument values may be used: -1 represents the current
3942 time, and -2 represents the time the shell was invoked.
3946 Arguments to non-string format specifiers are treated as C language constants,
3947 except that a leading plus or minus sign is allowed, and if the leading
3948 character is a single or double quote, the value is the ASCII value of
3949 the following character.
3951 The @var{format} is reused as necessary to consume all of the @var{arguments}.
3952 If the @var{format} requires more @var{arguments} than are supplied, the
3953 extra format specifications behave as if a zero value or null string, as
3954 appropriate, had been supplied. The return value is zero on success,
3955 non-zero on failure.
3960 read [-ers] [-a @var{aname}] [-d @var{delim}] [-i @var{text}] [-n @var{nchars}]
3961 [-N @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}]
3964 One line is read from the standard input, or from the file descriptor
3965 @var{fd} supplied as an argument to the @option{-u} option, and the first word
3966 is assigned to the first @var{name}, the second word to the second @var{name},
3967 and so on, with leftover words and their intervening separators assigned
3968 to the last @var{name}.
3969 If there are fewer words read from the input stream than names,
3970 the remaining names are assigned empty values.
3971 The characters in the value of the @env{IFS} variable
3972 are used to split the line into words.
3973 The backslash character @samp{\} may be used to remove any special
3974 meaning for the next character read and for line continuation.
3975 If no names are supplied, the line read is assigned to the
3976 variable @env{REPLY}.
3977 The return code is zero, unless end-of-file is encountered, @code{read}
3978 times out (in which case the return code is greater than 128), or an
3979 invalid file descriptor is supplied as the argument to @option{-u}.
3981 Options, if supplied, have the following meanings:
3984 @item -a @var{aname}
3985 The words are assigned to sequential indices of the array variable
3986 @var{aname}, starting at 0.
3987 All elements are removed from @var{aname} before the assignment.
3988 Other @var{name} arguments are ignored.
3990 @item -d @var{delim}
3991 The first character of @var{delim} is used to terminate the input line,
3992 rather than newline.
3995 Readline (@pxref{Command Line Editing}) is used to obtain the line.
3996 Readline uses the current (or default, if line editing was not previously
3997 active) editing settings.
4000 If Readline is being used to read the line, @var{text} is placed into
4001 the editing buffer before editing begins.
4003 @item -n @var{nchars}
4004 @code{read} returns after reading @var{nchars} characters rather than
4005 waiting for a complete line of input, but honor a delimiter if fewer
4006 than @var{nchars} characters are read before the delimiter.
4008 @item -N @var{nchars}
4009 @code{read} returns after reading exactly @var{nchars} characters rather
4010 than waiting for a complete line of input, unless EOF is encountered or
4011 @code{read} times out.
4012 Delimiter characters encountered in the input are
4013 not treated specially and do not cause @code{read} to return until
4014 @var{nchars} characters are read.
4016 @item -p @var{prompt}
4017 Display @var{prompt}, without a trailing newline, before attempting
4019 The prompt is displayed only if input is coming from a terminal.
4022 If this option is given, backslash does not act as an escape character.
4023 The backslash is considered to be part of the line.
4024 In particular, a backslash-newline pair may not be used as a line
4028 Silent mode. If input is coming from a terminal, characters are
4031 @item -t @var{timeout}
4032 Cause @code{read} to time out and return failure if a complete line of
4033 input is not read within @var{timeout} seconds.
4034 @var{timeout} may be a decimal number with a fractional portion following
4036 This option is only effective if @code{read} is reading input from a
4037 terminal, pipe, or other special file; it has no effect when reading
4039 If @var{timeout} is 0, @code{read} returns immediately, without trying to
4040 read and data. The exit status is 0 if input is available on
4041 the specified file descriptor, non-zero otherwise.
4042 The exit status is greater than 128 if the timeout is exceeded.
4045 Read input from file descriptor @var{fd}.
4051 readarray [-n @var{count}] [-O @var{origin}] [-s @var{count}] [-t] [-u @var{fd}]
4052 [-C @var{callback}] [-c @var{quantum}] [@var{array}]
4055 Read lines from the standard input into the indexed array variable @var{array},
4056 or from file descriptor @var{fd}
4057 if the @option{-u} option is supplied.
4059 A synonym for @code{mapfile}.
4064 source @var{filename}
4067 A synonym for @code{.} (@pxref{Bourne Shell Builtins}).
4072 type [-afptP] [@var{name} @dots{}]
4075 For each @var{name}, indicate how it would be interpreted if used as a
4078 If the @option{-t} option is used, @code{type} prints a single word
4079 which is one of @samp{alias}, @samp{function}, @samp{builtin},
4080 @samp{file} or @samp{keyword},
4081 if @var{name} is an alias, shell function, shell builtin,
4082 disk file, or shell reserved word, respectively.
4083 If the @var{name} is not found, then nothing is printed, and
4084 @code{type} returns a failure status.
4086 If the @option{-p} option is used, @code{type} either returns the name
4087 of the disk file that would be executed, or nothing if @option{-t}
4088 would not return @samp{file}.
4090 The @option{-P} option forces a path search for each @var{name}, even if
4091 @option{-t} would not return @samp{file}.
4093 If a command is hashed, @option{-p} and @option{-P} print the hashed value,
4094 which is not necessarily the file that appears first in @code{$PATH}.
4096 If the @option{-a} option is used, @code{type} returns all of the places
4097 that contain an executable named @var{file}.
4098 This includes aliases and functions, if and only if the @option{-p} option
4101 If the @option{-f} option is used, @code{type} does not attempt to find
4102 shell functions, as with the @code{command} builtin.
4104 The return status is zero if all of the @var{names} are found, non-zero
4105 if any are not found.
4110 typeset [-afFgrxilrtux] [-p] [@var{name}[=@var{value}] @dots{}]
4113 The @code{typeset} command is supplied for compatibility with the Korn
4115 It is a synonym for the @code{declare} builtin command.
4120 ulimit [-abcdefilmnpqrstuvxHST] [@var{limit}]
4123 @code{ulimit} provides control over the resources available to processes
4124 started by the shell, on systems that allow such control. If an
4125 option is given, it is interpreted as follows:
4129 Change and report the soft limit associated with a resource.
4132 Change and report the hard limit associated with a resource.
4135 All current limits are reported.
4138 The maximum socket buffer size.
4141 The maximum size of core files created.
4144 The maximum size of a process's data segment.
4147 The maximum scheduling priority ("nice").
4150 The maximum size of files written by the shell and its children.
4153 The maximum number of pending signals.
4156 The maximum size that may be locked into memory.
4159 The maximum resident set size (many systems do not honor this limit).
4162 The maximum number of open file descriptors (most systems do not
4163 allow this value to be set).
4166 The pipe buffer size.
4169 The maximum number of bytes in POSIX message queues.
4172 The maximum real-time scheduling priority.
4175 The maximum stack size.
4178 The maximum amount of cpu time in seconds.
4181 The maximum number of processes available to a single user.
4184 The maximum amount of virtual memory available to the shell, and, on
4185 some systems, to its children.
4188 The maximum number of file locks.
4191 The maximum number of threads.
4194 If @var{limit} is given, and the @option{-a} option is not used,
4195 @var{limit} is the new value of the specified resource.
4196 The special @var{limit} values @code{hard}, @code{soft}, and
4197 @code{unlimited} stand for the current hard limit, the current soft limit,
4198 and no limit, respectively.
4199 A hard limit cannot be increased by a non-root user once it is set;
4200 a soft limit may be increased up to the value of the hard limit.
4201 Otherwise, the current value of the soft limit for the specified resource
4202 is printed, unless the @option{-H} option is supplied.
4203 When setting new limits, if neither @option{-H} nor @option{-S} is supplied,
4204 both the hard and soft limits are set.
4205 If no option is given, then @option{-f} is assumed. Values are in 1024-byte
4206 increments, except for @option{-t}, which is in seconds; @option{-p},
4207 which is in units of 512-byte blocks; and @option{-T}, @option{-b},
4208 @option{-n} and @option{-u}, which are unscaled values.
4210 The return status is zero unless an invalid option or argument is supplied,
4211 or an error occurs while setting a new limit.
4216 unalias [-a] [@var{name} @dots{} ]
4219 Remove each @var{name} from the list of aliases. If @option{-a} is
4220 supplied, all aliases are removed.
4221 Aliases are described in @ref{Aliases}.
4224 @node Modifying Shell Behavior
4225 @section Modifying Shell Behavior
4228 * The Set Builtin:: Change the values of shell attributes and
4229 positional parameters.
4230 * The Shopt Builtin:: Modify shell optional behavior.
4233 @node The Set Builtin
4234 @subsection The Set Builtin
4236 This builtin is so complicated that it deserves its own section. @code{set}
4237 allows you to change the values of shell options and set the positional
4238 parameters, or to display the names and values of shell variables.
4244 set [--abefhkmnptuvxBCEHPT] [-o @var{option-name}] [@var{argument} @dots{}]
4245 set [+abefhkmnptuvxBCEHPT] [+o @var{option-name}] [@var{argument} @dots{}]
4248 If no options or arguments are supplied, @code{set} displays the names
4249 and values of all shell variables and functions, sorted according to the
4250 current locale, in a format that may be reused as input
4251 for setting or resetting the currently-set variables.
4252 Read-only variables cannot be reset.
4253 In @sc{posix} mode, only shell variables are listed.
4255 When options are supplied, they set or unset shell attributes.
4256 Options, if specified, have the following meanings:
4260 Mark variables and function which are modified or created for export
4261 to the environment of subsequent commands.
4264 Cause the status of terminated background jobs to be reported
4265 immediately, rather than before printing the next primary prompt.
4269 a pipeline (@pxref{Pipelines}), which may consist of a single simple command
4270 (@pxref{Simple Commands}),
4271 a list (@pxref{Lists}),
4272 or a compound command (@pxref{Compound Commands})
4273 returns a non-zero status.
4274 The shell does not exit if the command that fails is part of the
4275 command list immediately following a @code{while} or @code{until} keyword,
4276 part of the test in an @code{if} statement,
4277 part of any command executed in a @code{&&} or @code{||} list except
4278 the command following the final @code{&&} or @code{||},
4279 any command in a pipeline but the last,
4280 or if the command's return status is being inverted with @code{!}.
4281 If a compound command other than a subshell
4282 returns a non-zero status because a command failed
4283 while @option{-e} was being ignored, the shell does not exit.
4284 A trap on @code{ERR}, if set, is executed before the shell exits.
4286 This option applies to the shell environment and each subshell environment
4287 separately (@pxref{Command Execution Environment}), and may cause
4288 subshells to exit before executing all the commands in the subshell.
4291 Disable filename expansion (globbing).
4294 Locate and remember (hash) commands as they are looked up for execution.
4295 This option is enabled by default.
4298 All arguments in the form of assignment statements are placed
4299 in the environment for a command, not just those that precede
4303 Job control is enabled (@pxref{Job Control}).
4304 All processes run in a separate process group.
4305 When a background job completes, the shell prints a line
4306 containing its exit status.
4309 Read commands but do not execute them; this may be used to check a
4310 script for syntax errors.
4311 This option is ignored by interactive shells.
4313 @item -o @var{option-name}
4315 Set the option corresponding to @var{option-name}:
4325 Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}).
4326 This also affects the editing interface used for @code{read -e}.
4344 Enable command history, as described in @ref{Bash History Facilities}.
4345 This option is on by default in interactive shells.
4348 An interactive shell will not exit upon reading EOF.
4381 If set, the return value of a pipeline is the value of the last
4382 (rightmost) command to exit with a non-zero status, or zero if all
4383 commands in the pipeline exit successfully.
4384 This option is disabled by default.
4387 Change the behavior of Bash where the default operation differs
4388 from the @sc{posix} standard to match the standard
4389 (@pxref{Bash POSIX Mode}).
4390 This is intended to make Bash behave as a strict superset of that
4400 Use a @code{vi}-style line editing interface.
4401 This also affects the editing interface used for @code{read -e}.
4408 Turn on privileged mode.
4409 In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not
4410 processed, shell functions are not inherited from the environment,
4411 and the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH} and @env{GLOBIGNORE}
4412 variables, if they appear in the environment, are ignored.
4413 If the shell is started with the effective user (group) id not equal to the
4414 real user (group) id, and the @option{-p} option is not supplied, these actions
4415 are taken and the effective user id is set to the real user id.
4416 If the @option{-p} option is supplied at startup, the effective user id is
4418 Turning this option off causes the effective user
4419 and group ids to be set to the real user and group ids.
4422 Exit after reading and executing one command.
4425 Treat unset variables and parameters other than the special parameters
4426 @samp{@@} or @samp{*} as an error when performing parameter expansion.
4427 An error message will be written to the standard error, and a non-interactive
4431 Print shell input lines as they are read.
4434 Print a trace of simple commands, @code{for} commands, @code{case}
4435 commands, @code{select} commands, and arithmetic @code{for} commands
4436 and their arguments or associated word lists after they are
4437 expanded and before they are executed. The value of the @env{PS4}
4438 variable is expanded and the resultant value is printed before
4439 the command and its expanded arguments.
4442 The shell will perform brace expansion (@pxref{Brace Expansion}).
4443 This option is on by default.
4446 Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>}
4447 from overwriting existing files.
4450 If set, any trap on @code{ERR} is inherited by shell functions, command
4451 substitutions, and commands executed in a subshell environment.
4452 The @code{ERR} trap is normally not inherited in such cases.
4455 Enable @samp{!} style history substitution (@pxref{History Interaction}).
4456 This option is on by default for interactive shells.
4459 If set, do not follow symbolic links when performing commands such as
4460 @code{cd} which change the current directory. The physical directory
4461 is used instead. By default, Bash follows
4462 the logical chain of directories when performing commands
4463 which change the current directory.
4465 For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys}
4468 $ cd /usr/sys; echo $PWD
4475 If @code{set -P} is on, then:
4477 $ cd /usr/sys; echo $PWD
4484 If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by
4485 shell functions, command substitutions, and commands executed
4486 in a subshell environment.
4487 The @code{DEBUG} and @code{RETURN} traps are normally not inherited
4491 If no arguments follow this option, then the positional parameters are
4492 unset. Otherwise, the positional parameters are set to the
4493 @var{arguments}, even if some of them begin with a @samp{-}.
4496 Signal the end of options, cause all remaining @var{arguments}
4497 to be assigned to the positional parameters. The @option{-x}
4498 and @option{-v} options are turned off.
4499 If there are no arguments, the positional parameters remain unchanged.
4502 Using @samp{+} rather than @samp{-} causes these options to be
4503 turned off. The options can also be used upon invocation of the
4504 shell. The current set of options may be found in @code{$-}.
4506 The remaining N @var{arguments} are positional parameters and are
4507 assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}.
4508 The special parameter @code{#} is set to N.
4510 The return status is always zero unless an invalid option is supplied.
4513 @node The Shopt Builtin
4514 @subsection The Shopt Builtin
4516 This builtin allows you to change additional shell optional behavior.
4523 shopt [-pqsu] [-o] [@var{optname} @dots{}]
4526 Toggle the values of variables controlling optional shell behavior.
4527 With no options, or with the @option{-p} option, a list of all settable
4528 options is displayed, with an indication of whether or not each is set.
4529 The @option{-p} option causes output to be displayed in a form that
4530 may be reused as input.
4531 Other options have the following meanings:
4535 Enable (set) each @var{optname}.
4538 Disable (unset) each @var{optname}.
4541 Suppresses normal output; the return status
4542 indicates whether the @var{optname} is set or unset.
4543 If multiple @var{optname} arguments are given with @option{-q},
4544 the return status is zero if all @var{optnames} are enabled;
4548 Restricts the values of
4549 @var{optname} to be those defined for the @option{-o} option to the
4550 @code{set} builtin (@pxref{The Set Builtin}).
4553 If either @option{-s} or @option{-u}
4554 is used with no @var{optname} arguments, @code{shopt} shows only
4555 those options which are set or unset, respectively.
4557 Unless otherwise noted, the @code{shopt} options are disabled (off)
4560 The return status when listing options is zero if all @var{optnames}
4561 are enabled, non-zero otherwise. When setting or unsetting options,
4562 the return status is zero unless an @var{optname} is not a valid shell
4565 The list of @code{shopt} options is:
4569 If set, a command name that is the name of a directory is executed as if
4570 it were the argument to the @code{cd} command.
4571 This option is only used by interactive shells.
4574 If this is set, an argument to the @code{cd} builtin command that
4575 is not a directory is assumed to be the name of a variable whose
4576 value is the directory to change to.
4579 If set, minor errors in the spelling of a directory component in a
4580 @code{cd} command will be corrected.
4581 The errors checked for are transposed characters,
4582 a missing character, and a character too many.
4583 If a correction is found, the corrected path is printed,
4584 and the command proceeds.
4585 This option is only used by interactive shells.
4588 If this is set, Bash checks that a command found in the hash
4589 table exists before trying to execute it. If a hashed command no
4590 longer exists, a normal path search is performed.
4593 If set, Bash lists the status of any stopped and running jobs before
4594 exiting an interactive shell. If any jobs are running, this causes
4595 the exit to be deferred until a second exit is attempted without an
4596 intervening command (@pxref{Job Control}).
4597 The shell always postpones exiting if any jobs are stopped.
4600 If set, Bash checks the window size after each command
4601 and, if necessary, updates the values of
4602 @env{LINES} and @env{COLUMNS}.
4606 attempts to save all lines of a multiple-line
4607 command in the same history entry. This allows
4608 easy re-editing of multi-line commands.
4612 changes its behavior to that of version 3.1 with respect to quoted
4613 arguments to the conditional command's @samp{=~} operator.
4617 changes its behavior to that of version 3.2 with respect to locale-specific
4618 string comparison when using the @samp{[[}
4619 conditional command's @samp{<} and @samp{>} operators.
4620 Bash versions prior to bash-4.0 use ASCII collation and strcmp(3);
4621 bash-4.1 and later use the current locale's collation sequence and strcoll(3).
4625 changes its behavior to that of version 4.0 with respect to locale-specific
4626 string comparison when using the @samp{[[}
4627 conditional command's @samp{<} and @samp{>} operators (see previous item)
4628 and the effect of interrupting a command list.
4631 If set, Bash, when in posix mode, treats a single quote in a double-quoted
4632 parameter expansion as a special character. The single quotes must match
4633 (an even number) and the characters between the single quotes are considered
4634 quoted. This is the behavior of @sc{posix} mode through version 4.1.
4635 The default Bash behavior remains as in previous versions.
4637 @item complete_fullquote
4639 quotes all shell metacharacters in filenames and directory names when
4640 performing completion.
4642 removes metacharacters such as the dollar sign from the set of
4643 characters that will be quoted in completed filenames
4644 when these metacharacters appear in shell variable references in words to be
4646 This means that dollar signs in variable names that expand to directories
4648 however, any dollar signs appearing in filenames will not be quoted, either.
4649 This is active only when bash is using backslashes to quote completed
4651 This variable is set by default, which is the default Bash behavior in
4652 versions through 4.2.
4656 replaces directory names with the results of word expansion when performing
4657 filename completion. This changes the contents of the readline editing
4659 If not set, Bash attempts to preserve what the user typed.
4663 attempts spelling correction on directory names during word completion
4664 if the directory name initially supplied does not exist.
4667 If set, Bash includes filenames beginning with a `.' in
4668 the results of filename expansion.
4671 If this is set, a non-interactive shell will not exit if
4672 it cannot execute the file specified as an argument to the @code{exec}
4673 builtin command. An interactive shell does not exit if @code{exec}
4676 @item expand_aliases
4677 If set, aliases are expanded as described below under Aliases,
4679 This option is enabled by default for interactive shells.
4682 If set, behavior intended for use by debuggers is enabled:
4686 The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins})
4687 displays the source file name and line number corresponding to each function
4688 name supplied as an argument.
4691 If the command run by the @code{DEBUG} trap returns a non-zero value, the
4692 next command is skipped and not executed.
4695 If the command run by the @code{DEBUG} trap returns a value of 2, and the
4696 shell is executing in a subroutine (a shell function or a shell script
4697 executed by the @code{.} or @code{source} builtins), a call to
4698 @code{return} is simulated.
4701 @code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their
4702 descriptions (@pxref{Bash Variables}).
4705 Function tracing is enabled: command substitution, shell functions, and
4706 subshells invoked with @code{( @var{command} )} inherit the
4707 @code{DEBUG} and @code{RETURN} traps.
4710 Error tracing is enabled: command substitution, shell functions, and
4711 subshells invoked with @code{( @var{command} )} inherit the
4716 If set, the extended pattern matching features described above
4717 (@pxref{Pattern Matching}) are enabled.
4720 If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is
4721 performed within @code{$@{@var{parameter}@}} expansions
4722 enclosed in double quotes. This option is enabled by default.
4725 If set, patterns which fail to match filenames during filename expansion
4726 result in an expansion error.
4729 If set, the suffixes specified by the @env{FIGNORE} shell variable
4730 cause words to be ignored when performing word completion even if
4731 the ignored words are the only possible completions.
4732 @xref{Bash Variables}, for a description of @env{FIGNORE}.
4733 This option is enabled by default.
4735 @item globasciiranges
4736 If set, range expressions used in pattern matching (@pxref{Pattern Matching})
4737 behave as if in the traditional C locale when performing
4738 comparisons. That is, the current locale's collating sequence
4739 is not taken into account, so
4740 @samp{b} will not collate between @samp{A} and @samp{B},
4741 and upper-case and lower-case ASCII characters will collate together.
4744 If set, the pattern @samp{**} used in a filename expansion context will
4745 match all files and zero or more directories and subdirectories.
4746 If the pattern is followed by a @samp{/}, only directories and
4747 subdirectories match.
4750 If set, shell error messages are written in the standard @sc{gnu} error
4754 If set, the history list is appended to the file named by the value
4755 of the @env{HISTFILE}
4756 variable when the shell exits, rather than overwriting the file.
4759 If set, and Readline
4760 is being used, a user is given the opportunity to re-edit a
4761 failed history substitution.
4764 If set, and Readline
4765 is being used, the results of history substitution are not immediately
4766 passed to the shell parser. Instead, the resulting line is loaded into
4767 the Readline editing buffer, allowing further modification.
4770 If set, and Readline is being used, Bash will attempt to perform
4771 hostname completion when a word containing a @samp{@@} is being
4772 completed (@pxref{Commands For Completion}). This option is enabled
4776 If set, Bash will send @code{SIGHUP} to all jobs when an interactive
4777 login shell exits (@pxref{Signals}).
4779 @item interactive_comments
4780 Allow a word beginning with @samp{#}
4781 to cause that word and all remaining characters on that
4782 line to be ignored in an interactive shell.
4783 This option is enabled by default.
4786 If set, and job control is not active, the shell runs the last command of
4787 a pipeline not executed in the background in the current shell environment.
4790 If enabled, and the @code{cmdhist}
4791 option is enabled, multi-line commands are saved to the history with
4792 embedded newlines rather than using semicolon separators where possible.
4795 The shell sets this option if it is started as a login shell
4796 (@pxref{Invoking Bash}).
4797 The value may not be changed.
4800 If set, and a file that Bash is checking for mail has been
4801 accessed since the last time it was checked, the message
4802 @code{"The mail in @var{mailfile} has been read"} is displayed.
4804 @item no_empty_cmd_completion
4805 If set, and Readline is being used, Bash will not attempt to search
4806 the @env{PATH} for possible completions when completion is attempted
4810 If set, Bash matches filenames in a case-insensitive fashion when
4811 performing filename expansion.
4814 If set, Bash matches patterns in a case-insensitive fashion when
4815 performing matching while executing @code{case} or @code{[[}
4816 conditional commands.
4819 If set, Bash allows filename patterns which match no
4820 files to expand to a null string, rather than themselves.
4823 If set, the programmable completion facilities
4824 (@pxref{Programmable Completion}) are enabled.
4825 This option is enabled by default.
4828 If set, prompt strings undergo
4829 parameter expansion, command substitution, arithmetic
4830 expansion, and quote removal after being expanded
4831 as described below (@pxref{Controlling the Prompt}).
4832 This option is enabled by default.
4834 @item restricted_shell
4835 The shell sets this option if it is started in restricted mode
4836 (@pxref{The Restricted Shell}).
4837 The value may not be changed.
4838 This is not reset when the startup files are executed, allowing
4839 the startup files to discover whether or not a shell is restricted.
4842 If this is set, the @code{shift}
4843 builtin prints an error message when the shift count exceeds the
4844 number of positional parameters.
4847 If set, the @code{source} builtin uses the value of @env{PATH}
4848 to find the directory containing the file supplied as an argument.
4849 This option is enabled by default.
4852 If set, the @code{echo} builtin expands backslash-escape sequences
4858 The return status when listing options is zero if all @var{optnames}
4859 are enabled, non-zero otherwise.
4860 When setting or unsetting options, the return status is zero unless an
4861 @var{optname} is not a valid shell option.
4864 @node Special Builtins
4865 @section Special Builtins
4866 @cindex special builtin
4868 For historical reasons, the @sc{posix} standard has classified
4869 several builtin commands as @emph{special}.
4870 When Bash is executing in @sc{posix} mode, the special builtins
4871 differ from other builtin commands in three respects:
4875 Special builtins are found before shell functions during command lookup.
4878 If a special builtin returns an error status, a non-interactive shell exits.
4881 Assignment statements preceding the command stay in effect in the shell
4882 environment after the command completes.
4885 When Bash is not executing in @sc{posix} mode, these builtins behave no
4886 differently than the rest of the Bash builtin commands.
4887 The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}.
4889 These are the @sc{posix} special builtins:
4891 @w{break : . continue eval exec exit export readonly return set}
4892 @w{shift trap unset}
4895 @node Shell Variables
4896 @chapter Shell Variables
4899 * Bourne Shell Variables:: Variables which Bash uses in the same way
4900 as the Bourne Shell.
4901 * Bash Variables:: List of variables that exist in Bash.
4904 This chapter describes the shell variables that Bash uses.
4905 Bash automatically assigns default values to a number of variables.
4907 @node Bourne Shell Variables
4908 @section Bourne Shell Variables
4910 Bash uses certain shell variables in the same way as the Bourne shell.
4911 In some cases, Bash assigns a default value to the variable.
4916 A colon-separated list of directories used as a search path for
4917 the @code{cd} builtin command.
4920 The current user's home directory; the default for the @code{cd} builtin
4922 The value of this variable is also used by tilde expansion
4923 (@pxref{Tilde Expansion}).
4926 A list of characters that separate fields; used when the shell splits
4927 words as part of expansion.
4930 If this parameter is set to a filename or directory name
4931 and the @env{MAILPATH} variable
4932 is not set, Bash informs the user of the arrival of mail in
4933 the specified file or Maildir-format directory.
4936 A colon-separated list of filenames which the shell periodically checks
4938 Each list entry can specify the message that is printed when new mail
4939 arrives in the mail file by separating the filename from the message with
4941 When used in the text of the message, @code{$_} expands to the name of
4942 the current mail file.
4945 The value of the last option argument processed by the @code{getopts} builtin.
4948 The index of the last option argument processed by the @code{getopts} builtin.
4951 A colon-separated list of directories in which the shell looks for
4953 A zero-length (null) directory name in the value of @code{PATH} indicates the
4955 A null directory name may appear as two adjacent colons, or as an initial
4960 The primary prompt string. The default value is @samp{\s-\v\$ }.
4961 @xref{Controlling the Prompt}, for the complete list of escape
4962 sequences that are expanded before @env{PS1} is displayed.
4965 The secondary prompt string. The default value is @samp{> }.
4969 @node Bash Variables
4970 @section Bash Variables
4972 These variables are set or used by Bash, but other shells
4973 do not normally treat them specially.
4975 A few variables used by Bash are described in different chapters:
4976 variables for controlling the job control facilities
4977 (@pxref{Job Control Variables}).
4982 The full pathname used to execute the current instance of Bash.
4985 A colon-separated list of enabled shell options. Each word in
4986 the list is a valid argument for the @option{-s} option to the
4987 @code{shopt} builtin command (@pxref{The Shopt Builtin}).
4988 The options appearing in @env{BASHOPTS} are those reported
4989 as @samp{on} by @samp{shopt}.
4990 If this variable is in the environment when Bash
4991 starts up, each shell option in the list will be enabled before
4992 reading any startup files. This variable is readonly.
4995 Expands to the process ID of the current Bash process.
4996 This differs from @code{$$} under certain circumstances, such as subshells
4997 that do not require Bash to be re-initialized.
5000 An associative array variable whose members correspond to the internal
5001 list of aliases as maintained by the @code{alias} builtin.
5002 (@pxref{Bourne Shell Builtins}).
5003 Elements added to this array appear in the alias list; unsetting array
5004 elements cause aliases to be removed from the alias list.
5007 An array variable whose values are the number of parameters in each
5008 frame of the current bash execution call stack. The number of
5009 parameters to the current subroutine (shell function or script executed
5010 with @code{.} or @code{source}) is at the top of the stack. When a
5011 subroutine is executed, the number of parameters passed is pushed onto
5013 The shell sets @code{BASH_ARGC} only when in extended debugging mode
5014 (see @ref{The Shopt Builtin}
5015 for a description of the @code{extdebug} option to the @code{shopt}
5019 An array variable containing all of the parameters in the current bash
5020 execution call stack. The final parameter of the last subroutine call
5021 is at the top of the stack; the first parameter of the initial call is
5022 at the bottom. When a subroutine is executed, the parameters supplied
5023 are pushed onto @code{BASH_ARGV}.
5024 The shell sets @code{BASH_ARGV} only when in extended debugging mode
5025 (see @ref{The Shopt Builtin}
5026 for a description of the @code{extdebug} option to the @code{shopt}
5030 An associative array variable whose members correspond to the internal
5031 hash table of commands as maintained by the @code{hash} builtin
5032 (@pxref{Bourne Shell Builtins}).
5033 Elements added to this array appear in the hash table; unsetting array
5034 elements cause commands to be removed from the hash table.
5037 The command currently being executed or about to be executed, unless the
5038 shell is executing a command as the result of a trap,
5039 in which case it is the command executing at the time of the trap.
5042 If this variable is set when Bash is invoked to execute a shell
5043 script, its value is expanded and used as the name of a startup file
5044 to read before executing the script. @xref{Bash Startup Files}.
5046 @item BASH_EXECUTION_STRING
5047 The command argument to the @option{-c} invocation option.
5050 An array variable whose members are the line numbers in source files
5051 where each corresponding member of @var{FUNCNAME} was invoked.
5052 @code{$@{BASH_LINENO[$i]@}} is the line number in the source file
5053 (@code{$@{BASH_SOURCE[$i+1]@}}) where
5054 @code{$@{FUNCNAME[$i]@}} was called (or @code{$@{BASH_LINENO[$i-1]@}} if
5055 referenced within another shell function).
5056 Use @code{LINENO} to obtain the current line number.
5059 An array variable whose members are assigned by the @samp{=~} binary
5060 operator to the @code{[[} conditional command
5061 (@pxref{Conditional Constructs}).
5062 The element with index 0 is the portion of the string
5063 matching the entire regular expression.
5064 The element with index @var{n} is the portion of the
5065 string matching the @var{n}th parenthesized subexpression.
5066 This variable is read-only.
5069 An array variable whose members are the source filenames where the
5070 corresponding shell function names in the @code{FUNCNAME} array
5071 variable are defined.
5072 The shell function @code{$@{FUNCNAME[$i]@}} is defined in the file
5073 @code{$@{BASH_SOURCE[$i]@}} and called from @code{$@{BASH_SOURCE[$i+1]@}}
5076 Incremented by one within each subshell or subshell environment when
5077 the shell begins executing in that environment.
5078 The initial value is 0.
5081 A readonly array variable (@pxref{Arrays})
5082 whose members hold version information for this instance of Bash.
5083 The values assigned to the array members are as follows:
5087 @item BASH_VERSINFO[0]
5088 The major version number (the @var{release}).
5090 @item BASH_VERSINFO[1]
5091 The minor version number (the @var{version}).
5093 @item BASH_VERSINFO[2]
5096 @item BASH_VERSINFO[3]
5099 @item BASH_VERSINFO[4]
5100 The release status (e.g., @var{beta1}).
5102 @item BASH_VERSINFO[5]
5103 The value of @env{MACHTYPE}.
5107 The version number of the current instance of Bash.
5110 If set to an integer corresponding to a valid file descriptor, Bash
5111 will write the trace output generated when @samp{set -x}
5112 is enabled to that file descriptor.
5113 This allows tracing output to be separated from diagnostic and error
5115 The file descriptor is closed when @code{BASH_XTRACEFD} is unset or assigned
5117 Unsetting @code{BASH_XTRACEFD} or assigning it the empty string causes the
5118 trace output to be sent to the standard error.
5119 Note that setting @code{BASH_XTRACEFD} to 2 (the standard error file
5120 descriptor) and then unsetting it will result in the standard error
5124 Used by the @code{select} command to determine the terminal width
5125 when printing selection lists. Automatically set by an interactive shell
5130 An index into @env{$@{COMP_WORDS@}} of the word containing the current
5132 This variable is available only in shell functions invoked by the
5133 programmable completion facilities (@pxref{Programmable Completion}).
5136 The current command line.
5137 This variable is available only in shell functions and external
5138 commands invoked by the
5139 programmable completion facilities (@pxref{Programmable Completion}).
5142 The index of the current cursor position relative to the beginning of
5143 the current command.
5144 If the current cursor position is at the end of the current command,
5145 the value of this variable is equal to @code{$@{#COMP_LINE@}}.
5146 This variable is available only in shell functions and external
5147 commands invoked by the
5148 programmable completion facilities (@pxref{Programmable Completion}).
5151 Set to an integer value corresponding to the type of completion attempted
5152 that caused a completion function to be called:
5153 @var{TAB}, for normal completion,
5154 @samp{?}, for listing completions after successive tabs,
5155 @samp{!}, for listing alternatives on partial word completion,
5156 @samp{@@}, to list completions if the word is not unmodified,
5158 @samp{%}, for menu completion.
5159 This variable is available only in shell functions and external
5160 commands invoked by the
5161 programmable completion facilities (@pxref{Programmable Completion}).
5164 The key (or final key of a key sequence) used to invoke the current
5165 completion function.
5167 @item COMP_WORDBREAKS
5168 The set of characters that the Readline library treats as word
5169 separators when performing word completion.
5170 If @code{COMP_WORDBREAKS} is unset, it loses its special properties,
5171 even if it is subsequently reset.
5174 An array variable consisting of the individual
5175 words in the current command line.
5176 The line is split into words as Readline would split it, using
5177 @code{COMP_WORDBREAKS} as described above.
5178 This variable is available only in shell functions invoked by the
5179 programmable completion facilities (@pxref{Programmable Completion}).
5182 An array variable from which Bash reads the possible completions
5183 generated by a shell function invoked by the programmable completion
5184 facility (@pxref{Programmable Completion}).
5187 An array variable created to hold the file descriptors
5188 for output from and input to an unnamed coprocess (@pxref{Coprocesses}).
5191 An array variable containing the current contents of the directory stack.
5192 Directories appear in the stack in the order they are displayed by the
5193 @code{dirs} builtin.
5194 Assigning to members of this array variable may be used to modify
5195 directories already in the stack, but the @code{pushd} and @code{popd}
5196 builtins must be used to add and remove directories.
5197 Assignment to this variable will not change the current directory.
5198 If @env{DIRSTACK} is unset, it loses its special properties, even if
5199 it is subsequently reset.
5202 If Bash finds this variable in the environment when the shell
5203 starts with value @samp{t}, it assumes that the shell is running in an
5204 Emacs shell buffer and disables line editing.
5207 Similar to @code{BASH_ENV}; used when the shell is invoked in
5208 @sc{posix} Mode (@pxref{Bash POSIX Mode}).
5211 The numeric effective user id of the current user. This variable
5215 The editor used as a default by the @option{-e} option to the @code{fc}
5219 A colon-separated list of suffixes to ignore when performing
5220 filename completion.
5221 A filename whose suffix matches one of the entries in
5223 is excluded from the list of matched filenames. A sample
5224 value is @samp{.o:~}
5227 An array variable containing the names of all shell functions
5228 currently in the execution call stack.
5229 The element with index 0 is the name of any currently-executing
5231 The bottom-most element (the one with the highest index)
5233 This variable exists only when a shell function is executing.
5234 Assignments to @env{FUNCNAME} have no effect and return an error status.
5235 If @env{FUNCNAME} is unset, it loses its special properties, even if
5236 it is subsequently reset.
5238 This variable can be used with @code{BASH_LINENO} and @code{BASH_SOURCE}.
5239 Each element of @code{FUNCNAME} has corresponding elements in
5240 @code{BASH_LINENO} and @code{BASH_SOURCE} to describe the call stack.
5241 For instance, @code{$@{FUNCNAME[$i]@}} was called from the file
5242 @code{$@{BASH_SOURCE[$i+1]@}} at line number @code{$@{BASH_LINENO[$i]@}}.
5243 The @code{caller} builtin displays the current call stack using this
5247 If set to a numeric value greater than 0, defines a maximum function
5248 nesting level. Function invocations that exceed this nesting level
5249 will cause the current command to abort.
5252 A colon-separated list of patterns defining the set of filenames to
5253 be ignored by filename expansion.
5254 If a filename matched by a filename expansion pattern also matches one
5255 of the patterns in @env{GLOBIGNORE}, it is removed from the list
5259 An array variable containing the list of groups of which the current
5261 Assignments to @env{GROUPS} have no effect and return an error status.
5262 If @env{GROUPS} is unset, it loses its special properties, even if it is
5266 Up to three characters which control history expansion, quick
5267 substitution, and tokenization (@pxref{History Interaction}).
5268 The first character is the
5269 @var{history expansion} character, that is, the character which signifies the
5270 start of a history expansion, normally @samp{!}. The second character is the
5271 character which signifies `quick substitution' when seen as the first
5272 character on a line, normally @samp{^}. The optional third character is the
5273 character which indicates that the remainder of the line is a comment when
5274 found as the first character of a word, usually @samp{#}. The history
5275 comment character causes history substitution to be skipped for the
5276 remaining words on the line. It does not necessarily cause the shell
5277 parser to treat the rest of the line as a comment.
5280 The history number, or index in the history list, of the current
5281 command. If @env{HISTCMD} is unset, it loses its special properties,
5282 even if it is subsequently reset.
5285 A colon-separated list of values controlling how commands are saved on
5287 If the list of values includes @samp{ignorespace}, lines which begin
5288 with a space character are not saved in the history list.
5289 A value of @samp{ignoredups} causes lines which match the previous
5290 history entry to not be saved.
5291 A value of @samp{ignoreboth} is shorthand for
5292 @samp{ignorespace} and @samp{ignoredups}.
5293 A value of @samp{erasedups} causes all previous lines matching the
5294 current line to be removed from the history list before that line
5296 Any value not in the above list is ignored.
5297 If @env{HISTCONTROL} is unset, or does not include a valid value,
5298 all lines read by the shell parser are saved on the history list,
5299 subject to the value of @env{HISTIGNORE}.
5300 The second and subsequent lines of a multi-line compound command are
5301 not tested, and are added to the history regardless of the value of
5305 The name of the file to which the command history is saved. The
5306 default value is @file{~/.bash_history}.
5309 The maximum number of lines contained in the history file.
5310 When this variable is assigned a value, the history file is truncated,
5311 if necessary, to contain no more than that number of lines
5312 by removing the oldest entries.
5313 The history file is also truncated to this size after
5314 writing it when an interactive shell exits.
5315 If the value is 0, the history file is truncated to zero size.
5316 Numeric values less than zero inhibit truncation.
5317 The shell sets the default value to the value of @env{HISTSIZE}
5318 after reading any startup files.
5321 A colon-separated list of patterns used to decide which command
5322 lines should be saved on the history list. Each pattern is
5323 anchored at the beginning of the line and must match the complete
5324 line (no implicit @samp{*} is appended). Each pattern is tested
5325 against the line after the checks specified by @env{HISTCONTROL}
5326 are applied. In addition to the normal shell pattern matching
5327 characters, @samp{&} matches the previous history line. @samp{&}
5328 may be escaped using a backslash; the backslash is removed
5329 before attempting a match.
5330 The second and subsequent lines of a multi-line compound command are
5331 not tested, and are added to the history regardless of the value of
5334 @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A
5335 pattern of @samp{&} is identical to @code{ignoredups}, and a
5336 pattern of @samp{[ ]*} is identical to @code{ignorespace}.
5337 Combining these two patterns, separating them with a colon,
5338 provides the functionality of @code{ignoreboth}.
5341 The maximum number of commands to remember on the history list.
5342 If the value is 0, commands are not saved in the history list.
5343 Numeric values less than zero result in every command being saved
5344 on the history list (there is no limit).
5345 The shell sets the default value to 500 after reading any startup files.
5347 @item HISTTIMEFORMAT
5348 If this variable is set and not null, its value is used as a format string
5349 for @var{strftime} to print the time stamp associated with each history
5350 entry displayed by the @code{history} builtin.
5351 If this variable is set, time stamps are written to the history file so
5352 they may be preserved across shell sessions.
5353 This uses the history comment character to distinguish timestamps from
5354 other history lines.
5357 Contains the name of a file in the same format as @file{/etc/hosts} that
5358 should be read when the shell needs to complete a hostname.
5359 The list of possible hostname completions may be changed while the shell
5361 the next time hostname completion is attempted after the
5362 value is changed, Bash adds the contents of the new file to the
5364 If @env{HOSTFILE} is set, but has no value, or does not name a readable file,
5365 Bash attempts to read
5366 @file{/etc/hosts} to obtain the list of possible hostname completions.
5367 When @env{HOSTFILE} is unset, the hostname list is cleared.
5370 The name of the current host.
5373 A string describing the machine Bash is running on.
5376 Controls the action of the shell on receipt of an @code{EOF} character
5377 as the sole input. If set, the value denotes the number
5378 of consecutive @code{EOF} characters that can be read as the
5379 first character on an input line
5380 before the shell will exit. If the variable exists but does not
5381 have a numeric value (or has no value) then the default is 10.
5382 If the variable does not exist, then @code{EOF} signifies the end of
5383 input to the shell. This is only in effect for interactive shells.
5386 The name of the Readline initialization file, overriding the default
5387 of @file{~/.inputrc}.
5390 Used to determine the locale category for any category not specifically
5391 selected with a variable starting with @code{LC_}.
5394 This variable overrides the value of @env{LANG} and any other
5395 @code{LC_} variable specifying a locale category.
5398 This variable determines the collation order used when sorting the
5399 results of filename expansion, and
5400 determines the behavior of range expressions, equivalence classes,
5401 and collating sequences within filename expansion and pattern matching
5402 (@pxref{Filename Expansion}).
5405 This variable determines the interpretation of characters and the
5406 behavior of character classes within filename expansion and pattern
5407 matching (@pxref{Filename Expansion}).
5410 This variable determines the locale used to translate double-quoted
5411 strings preceded by a @samp{$} (@pxref{Locale Translation}).
5414 This variable determines the locale category used for number formatting.
5417 The line number in the script or shell function currently executing.
5420 Used by the @code{select} command to determine the column length
5421 for printing selection lists. Automatically set by an interactive shell
5426 A string that fully describes the system type on which Bash
5427 is executing, in the standard @sc{gnu} @var{cpu-company-system} format.
5430 How often (in seconds) that the shell should check for mail in the
5431 files specified in the @env{MAILPATH} or @env{MAIL} variables.
5432 The default is 60 seconds. When it is time to check
5433 for mail, the shell does so before displaying the primary prompt.
5434 If this variable is unset, or set to a value that is not a number
5435 greater than or equal to zero, the shell disables mail checking.
5438 An array variable created to hold the text read by the
5439 @code{mapfile} builtin when no variable name is supplied.
5442 The previous working directory as set by the @code{cd} builtin.
5445 If set to the value 1, Bash displays error messages
5446 generated by the @code{getopts} builtin command.
5449 A string describing the operating system Bash is running on.
5452 An array variable (@pxref{Arrays})
5453 containing a list of exit status values from the processes
5454 in the most-recently-executed foreground pipeline (which may
5455 contain only a single command).
5457 @item POSIXLY_CORRECT
5458 If this variable is in the environment when Bash starts, the shell
5459 enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the
5460 startup files, as if the @option{--posix} invocation option had been supplied.
5461 If it is set while the shell is running, Bash enables @sc{posix} mode,
5470 The process @sc{id} of the shell's parent process. This variable
5473 @item PROMPT_COMMAND
5474 If set, the value is interpreted as a command to execute
5475 before the printing of each primary prompt (@env{$PS1}).
5477 @item PROMPT_DIRTRIM
5478 If set to a number greater than zero, the value is used as the number of
5479 trailing directory components to retain when expanding the @code{\w} and
5480 @code{\W} prompt string escapes (@pxref{Controlling the Prompt}).
5481 Characters removed are replaced with an ellipsis.
5484 The value of this variable is used as the prompt for the
5485 @code{select} command. If this variable is not set, the
5486 @code{select} command prompts with @samp{#? }
5489 The value is the prompt printed before the command line is echoed
5490 when the @option{-x} option is set (@pxref{The Set Builtin}).
5491 The first character of @env{PS4} is replicated multiple times, as
5492 necessary, to indicate multiple levels of indirection.
5493 The default is @samp{+ }.
5496 The current working directory as set by the @code{cd} builtin.
5499 Each time this parameter is referenced, a random integer
5500 between 0 and 32767 is generated. Assigning a value to this
5501 variable seeds the random number generator.
5504 The contents of the Readline line buffer, for use
5505 with @samp{bind -x} (@pxref{Bash Builtins}).
5507 @item READLINE_POINT
5508 The position of the insertion point in the Readline line buffer, for use
5509 with @samp{bind -x} (@pxref{Bash Builtins}).
5512 The default variable for the @code{read} builtin.
5515 This variable expands to the number of seconds since the
5516 shell was started. Assignment to this variable resets
5517 the count to the value assigned, and the expanded value
5518 becomes the value assigned plus the number of seconds
5519 since the assignment.
5522 The full pathname to the shell is kept in this environment variable.
5523 If it is not set when the shell starts,
5524 Bash assigns to it the full pathname of the current user's login shell.
5527 A colon-separated list of enabled shell options. Each word in
5528 the list is a valid argument for the @option{-o} option to the
5529 @code{set} builtin command (@pxref{The Set Builtin}).
5530 The options appearing in @env{SHELLOPTS} are those reported
5531 as @samp{on} by @samp{set -o}.
5532 If this variable is in the environment when Bash
5533 starts up, each shell option in the list will be enabled before
5534 reading any startup files. This variable is readonly.
5537 Incremented by one each time a new instance of Bash is started. This is
5538 intended to be a count of how deeply your Bash shells are nested.
5541 The value of this parameter is used as a format string specifying
5542 how the timing information for pipelines prefixed with the @code{time}
5543 reserved word should be displayed.
5544 The @samp{%} character introduces an
5545 escape sequence that is expanded to a time value or other
5547 The escape sequences and their meanings are as
5548 follows; the braces denote optional portions.
5555 @item %[@var{p}][l]R
5556 The elapsed time in seconds.
5558 @item %[@var{p}][l]U
5559 The number of CPU seconds spent in user mode.
5561 @item %[@var{p}][l]S
5562 The number of CPU seconds spent in system mode.
5565 The CPU percentage, computed as (%U + %S) / %R.
5568 The optional @var{p} is a digit specifying the precision, the number of
5569 fractional digits after a decimal point.
5570 A value of 0 causes no decimal point or fraction to be output.
5571 At most three places after the decimal point may be specified; values
5572 of @var{p} greater than 3 are changed to 3.
5573 If @var{p} is not specified, the value 3 is used.
5575 The optional @code{l} specifies a longer format, including minutes, of
5576 the form @var{MM}m@var{SS}.@var{FF}s.
5577 The value of @var{p} determines whether or not the fraction is included.
5579 If this variable is not set, Bash acts as if it had the value
5581 @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'}
5583 If the value is null, no timing information is displayed.
5584 A trailing newline is added when the format string is displayed.
5587 If set to a value greater than zero, @code{TMOUT} is treated as the
5588 default timeout for the @code{read} builtin (@pxref{Bash Builtins}).
5589 The @code{select} command (@pxref{Conditional Constructs}) terminates
5590 if input does not arrive after @code{TMOUT} seconds when input is coming
5593 In an interactive shell, the value is interpreted as
5594 the number of seconds to wait for input after issuing the primary
5595 prompt when the shell is interactive.
5596 Bash terminates after that number of seconds if input does
5600 If set, Bash uses its value as the name of a directory in which
5601 Bash creates temporary files for the shell's use.
5604 The numeric real user id of the current user. This variable is readonly.
5609 @chapter Bash Features
5611 This chapter describes features unique to Bash.
5614 * Invoking Bash:: Command line options that you can give
5616 * Bash Startup Files:: When and how Bash executes scripts.
5617 * Interactive Shells:: What an interactive shell is.
5618 * Bash Conditional Expressions:: Primitives used in composing expressions for
5619 the @code{test} builtin.
5620 * Shell Arithmetic:: Arithmetic on shell variables.
5621 * Aliases:: Substituting one command for another.
5622 * Arrays:: Array Variables.
5623 * The Directory Stack:: History of visited directories.
5624 * Controlling the Prompt:: Customizing the various prompt strings.
5625 * The Restricted Shell:: A more controlled mode of shell execution.
5626 * Bash POSIX Mode:: Making Bash behave more closely to what
5627 the POSIX standard specifies.
5631 @section Invoking Bash
5634 bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}]
5635 bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}]
5636 bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}]
5639 All of the single-character options used with the @code{set} builtin
5640 (@pxref{The Set Builtin}) can be used as options when the shell is invoked.
5641 In addition, there are several multi-character
5642 options that you can use. These options must appear on the command
5643 line before the single-character options to be recognized.
5647 Arrange for the debugger profile to be executed before the shell
5648 starts. Turns on extended debugging mode (see @ref{The Shopt Builtin}
5649 for a description of the @code{extdebug} option to the @code{shopt}
5652 @item --dump-po-strings
5653 A list of all double-quoted strings preceded by @samp{$}
5654 is printed on the standard output
5655 in the @sc{gnu} @code{gettext} PO (portable object) file format.
5656 Equivalent to @option{-D} except for the output format.
5658 @item --dump-strings
5659 Equivalent to @option{-D}.
5662 Display a usage message on standard output and exit successfully.
5664 @item --init-file @var{filename}
5665 @itemx --rcfile @var{filename}
5666 Execute commands from @var{filename} (instead of @file{~/.bashrc})
5667 in an interactive shell.
5670 Equivalent to @option{-l}.
5673 Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing})
5674 to read command lines when the shell is interactive.
5677 Don't load the system-wide startup file @file{/etc/profile}
5678 or any of the personal initialization files
5679 @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile}
5680 when Bash is invoked as a login shell.
5683 Don't read the @file{~/.bashrc} initialization file in an
5684 interactive shell. This is on by default if the shell is
5685 invoked as @code{sh}.
5688 Change the behavior of Bash where the default operation differs
5689 from the @sc{posix} standard to match the standard. This
5690 is intended to make Bash behave as a strict superset of that
5691 standard. @xref{Bash POSIX Mode}, for a description of the Bash
5695 Make the shell a restricted shell (@pxref{The Restricted Shell}).
5698 Equivalent to @option{-v}. Print shell input lines as they're read.
5701 Show version information for this instance of
5702 Bash on the standard output and exit successfully.
5705 There are several single-character options that may be supplied at
5706 invocation which are not available with the @code{set} builtin.
5709 @item -c @var{string}
5710 Read and execute commands from @var{string} after processing the
5711 options, then exit. Any remaining arguments are assigned to the
5712 positional parameters, starting with @code{$0}.
5715 Force the shell to run interactively. Interactive shells are
5716 described in @ref{Interactive Shells}.
5719 Make this shell act as if it had been directly invoked by login.
5720 When the shell is interactive, this is equivalent to starting a
5721 login shell with @samp{exec -l bash}.
5722 When the shell is not interactive, the login shell startup files will
5724 @samp{exec bash -l} or @samp{exec bash --login}
5725 will replace the current shell with a Bash login shell.
5726 @xref{Bash Startup Files}, for a description of the special behavior
5730 Make the shell a restricted shell (@pxref{The Restricted Shell}).
5733 If this option is present, or if no arguments remain after option
5734 processing, then commands are read from the standard input.
5735 This option allows the positional parameters to be set
5736 when invoking an interactive shell.
5739 A list of all double-quoted strings preceded by @samp{$}
5740 is printed on the standard output.
5741 These are the strings that
5742 are subject to language translation when the current locale
5743 is not @code{C} or @code{POSIX} (@pxref{Locale Translation}).
5744 This implies the @option{-n} option; no commands will be executed.
5746 @item [-+]O [@var{shopt_option}]
5747 @var{shopt_option} is one of the shell options accepted by the
5748 @code{shopt} builtin (@pxref{The Shopt Builtin}).
5749 If @var{shopt_option} is present, @option{-O} sets the value of that option;
5750 @option{+O} unsets it.
5751 If @var{shopt_option} is not supplied, the names and values of the shell
5752 options accepted by @code{shopt} are printed on the standard output.
5753 If the invocation option is @option{+O}, the output is displayed in a format
5754 that may be reused as input.
5757 A @code{--} signals the end of options and disables further option
5759 Any arguments after the @code{--} are treated as filenames and arguments.
5763 A @emph{login} shell is one whose first character of argument zero is
5764 @samp{-}, or one invoked with the @option{--login} option.
5766 @cindex interactive shell
5767 An @emph{interactive} shell is one started without non-option arguments,
5768 unless @option{-s} is specified,
5769 without specifying the @option{-c} option, and whose input and output are both
5770 connected to terminals (as determined by @code{isatty(3)}), or one
5771 started with the @option{-i} option. @xref{Interactive Shells}, for more
5774 If arguments remain after option processing, and neither the
5775 @option{-c} nor the @option{-s}
5776 option has been supplied, the first argument is assumed to
5777 be the name of a file containing shell commands (@pxref{Shell Scripts}).
5778 When Bash is invoked in this fashion, @code{$0}
5779 is set to the name of the file, and the positional parameters
5780 are set to the remaining arguments.
5781 Bash reads and executes commands from this file, then exits.
5782 Bash's exit status is the exit status of the last command executed
5783 in the script. If no commands are executed, the exit status is 0.
5785 @node Bash Startup Files
5786 @section Bash Startup Files
5787 @cindex startup files
5789 This section describes how Bash executes its startup files.
5790 If any of the files exist but cannot be read, Bash reports an error.
5791 Tildes are expanded in filenames as described above under
5792 Tilde Expansion (@pxref{Tilde Expansion}).
5794 Interactive shells are described in @ref{Interactive Shells}.
5796 @subsubheading Invoked as an interactive login shell, or with @option{--login}
5798 When Bash is invoked as an interactive login shell, or as a
5799 non-interactive shell with the @option{--login} option, it first reads and
5800 executes commands from the file @file{/etc/profile}, if that file exists.
5801 After reading that file, it looks for @file{~/.bash_profile},
5802 @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads
5803 and executes commands from the first one that exists and is readable.
5804 The @option{--noprofile} option may be used when the shell is started to
5805 inhibit this behavior.
5807 When a login shell exits, Bash reads and executes commands from
5808 the file @file{~/.bash_logout}, if it exists.
5810 @subsubheading Invoked as an interactive non-login shell
5812 When an interactive shell that is not a login shell is started, Bash
5813 reads and executes commands from @file{~/.bashrc}, if that file exists.
5814 This may be inhibited by using the @option{--norc} option.
5815 The @option{--rcfile @var{file}} option will force Bash to read and
5816 execute commands from @var{file} instead of @file{~/.bashrc}.
5818 So, typically, your @file{~/.bash_profile} contains the line
5820 @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi}
5823 after (or before) any login-specific initializations.
5825 @subsubheading Invoked non-interactively
5827 When Bash is started non-interactively, to run a shell script,
5828 for example, it looks for the variable @env{BASH_ENV} in the environment,
5829 expands its value if it appears there, and uses the expanded value as
5830 the name of a file to read and execute. Bash behaves as if the
5831 following command were executed:
5833 @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi}
5836 but the value of the @env{PATH} variable is not used to search for the
5839 As noted above, if a non-interactive shell is invoked with the
5840 @option{--login} option, Bash attempts to read and execute commands from the
5841 login shell startup files.
5843 @subsubheading Invoked with name @code{sh}
5845 If Bash is invoked with the name @code{sh}, it tries to mimic the
5846 startup behavior of historical versions of @code{sh} as closely as
5847 possible, while conforming to the @sc{posix} standard as well.
5849 When invoked as an interactive login shell, or as a non-interactive
5850 shell with the @option{--login} option, it first attempts to read
5851 and execute commands from @file{/etc/profile} and @file{~/.profile}, in
5853 The @option{--noprofile} option may be used to inhibit this behavior.
5854 When invoked as an interactive shell with the name @code{sh}, Bash
5855 looks for the variable @env{ENV}, expands its value if it is defined,
5856 and uses the expanded value as the name of a file to read and execute.
5857 Since a shell invoked as @code{sh} does not attempt to read and execute
5858 commands from any other startup files, the @option{--rcfile} option has
5860 A non-interactive shell invoked with the name @code{sh} does not attempt
5861 to read any other startup files.
5863 When invoked as @code{sh}, Bash enters @sc{posix} mode after
5864 the startup files are read.
5866 @subsubheading Invoked in @sc{posix} mode
5868 When Bash is started in @sc{posix} mode, as with the
5869 @option{--posix} command line option, it follows the @sc{posix} standard
5871 In this mode, interactive shells expand the @env{ENV} variable
5872 and commands are read and executed from the file whose name is the
5874 No other startup files are read.
5876 @subsubheading Invoked by remote shell daemon
5878 Bash attempts to determine when it is being run with its standard input
5879 connected to a network connection, as when executed by the remote shell
5880 daemon, usually @code{rshd}, or the secure shell daemon @code{sshd}.
5881 If Bash determines it is being run in
5882 this fashion, it reads and executes commands from @file{~/.bashrc}, if that
5883 file exists and is readable.
5884 It will not do this if invoked as @code{sh}.
5885 The @option{--norc} option may be used to inhibit this behavior, and the
5886 @option{--rcfile} option may be used to force another file to be read, but
5887 @code{rshd} does not generally invoke the shell with those options or
5888 allow them to be specified.
5890 @subsubheading Invoked with unequal effective and real @sc{uid/gid}s
5892 If Bash is started with the effective user (group) id not equal to the
5893 real user (group) id, and the @option{-p} option is not supplied, no startup
5894 files are read, shell functions are not inherited from the environment,
5895 the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH}, and @env{GLOBIGNORE}
5896 variables, if they appear in the environment, are ignored, and the effective
5897 user id is set to the real user id.
5898 If the @option{-p} option is supplied at invocation, the startup behavior is
5899 the same, but the effective user id is not reset.
5901 @node Interactive Shells
5902 @section Interactive Shells
5903 @cindex interactive shell
5904 @cindex shell, interactive
5907 * What is an Interactive Shell?:: What determines whether a shell is Interactive.
5908 * Is this Shell Interactive?:: How to tell if a shell is interactive.
5909 * Interactive Shell Behavior:: What changes in a interactive shell?
5912 @node What is an Interactive Shell?
5913 @subsection What is an Interactive Shell?
5915 An interactive shell
5916 is one started without non-option arguments, unless @option{-s} is
5917 specified, without specifying the @option{-c} option, and
5918 whose input and error output are both
5919 connected to terminals (as determined by @code{isatty(3)}),
5920 or one started with the @option{-i} option.
5922 An interactive shell generally reads from and writes to a user's
5925 The @option{-s} invocation option may be used to set the positional parameters
5926 when an interactive shell is started.
5928 @node Is this Shell Interactive?
5929 @subsection Is this Shell Interactive?
5931 To determine within a startup script whether or not Bash is
5932 running interactively,
5933 test the value of the @samp{-} special parameter.
5934 It contains @code{i} when the shell is interactive. For example:
5938 *i*) echo This shell is interactive ;;
5939 *) echo This shell is not interactive ;;
5943 Alternatively, startup scripts may examine the variable
5944 @env{PS1}; it is unset in non-interactive shells, and set in
5945 interactive shells. Thus:
5948 if [ -z "$PS1" ]; then
5949 echo This shell is not interactive
5951 echo This shell is interactive
5955 @node Interactive Shell Behavior
5956 @subsection Interactive Shell Behavior
5958 When the shell is running interactively, it changes its behavior in
5963 Startup files are read and executed as described in @ref{Bash Startup Files}.
5966 Job Control (@pxref{Job Control}) is enabled by default. When job
5967 control is in effect, Bash ignores the keyboard-generated job control
5968 signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
5971 Bash expands and displays @env{PS1} before reading the first line
5972 of a command, and expands and displays @env{PS2} before reading the
5973 second and subsequent lines of a multi-line command.
5976 Bash executes the value of the @env{PROMPT_COMMAND} variable as a command
5977 before printing the primary prompt, @env{$PS1}
5978 (@pxref{Bash Variables}).
5981 Readline (@pxref{Command Line Editing}) is used to read commands from
5982 the user's terminal.
5985 Bash inspects the value of the @code{ignoreeof} option to @code{set -o}
5986 instead of exiting immediately when it receives an @code{EOF} on its
5987 standard input when reading a command (@pxref{The Set Builtin}).
5990 Command history (@pxref{Bash History Facilities})
5991 and history expansion (@pxref{History Interaction})
5992 are enabled by default.
5993 Bash will save the command history to the file named by @env{$HISTFILE}
5994 when an interactive shell exits.
5997 Alias expansion (@pxref{Aliases}) is performed by default.
6000 In the absence of any traps, Bash ignores @code{SIGTERM}
6004 In the absence of any traps, @code{SIGINT} is caught and handled
6006 @code{SIGINT} will interrupt some shell builtins.
6009 An interactive login shell sends a @code{SIGHUP} to all jobs on exit
6010 if the @code{huponexit} shell option has been enabled (@pxref{Signals}).
6013 The @option{-n} invocation option is ignored, and @samp{set -n} has
6014 no effect (@pxref{The Set Builtin}).
6017 Bash will check for mail periodically, depending on the values of the
6018 @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables
6019 (@pxref{Bash Variables}).
6022 Expansion errors due to references to unbound shell variables after
6023 @samp{set -u} has been enabled will not cause the shell to exit
6024 (@pxref{The Set Builtin}).
6027 The shell will not exit on expansion errors caused by @var{var} being unset
6028 or null in @code{$@{@var{var}:?@var{word}@}} expansions
6029 (@pxref{Shell Parameter Expansion}).
6032 Redirection errors encountered by shell builtins will not cause the
6036 When running in @sc{posix} mode, a special builtin returning an error
6037 status will not cause the shell to exit (@pxref{Bash POSIX Mode}).
6040 A failed @code{exec} will not cause the shell to exit
6041 (@pxref{Bourne Shell Builtins}).
6044 Parser syntax errors will not cause the shell to exit.
6047 Simple spelling correction for directory arguments to the @code{cd}
6048 builtin is enabled by default (see the description of the @code{cdspell}
6049 option to the @code{shopt} builtin in @ref{The Shopt Builtin}).
6052 The shell will check the value of the @env{TMOUT} variable and exit
6053 if a command is not read within the specified number of seconds after
6054 printing @env{$PS1} (@pxref{Bash Variables}).
6058 @node Bash Conditional Expressions
6059 @section Bash Conditional Expressions
6060 @cindex expressions, conditional
6062 Conditional expressions are used by the @code{[[} compound command
6063 and the @code{test} and @code{[} builtin commands.
6065 Expressions may be unary or binary.
6066 Unary expressions are often used to examine the status of a file.
6067 There are string operators and numeric comparison operators as well.
6068 If the @var{file} argument to one of the primaries is of the form
6069 @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked.
6070 If the @var{file} argument to one of the primaries is one of
6071 @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file
6072 descriptor 0, 1, or 2, respectively, is checked.
6074 When used with @samp{[[}, the @samp{<} and @samp{>} operators sort
6075 lexicographically using the current locale.
6076 The @code{test} command uses ASCII ordering.
6078 Unless otherwise specified, primaries that operate on files follow symbolic
6079 links and operate on the target of the link, rather than the link itself.
6083 True if @var{file} exists.
6086 True if @var{file} exists and is a block special file.
6089 True if @var{file} exists and is a character special file.
6092 True if @var{file} exists and is a directory.
6095 True if @var{file} exists.
6098 True if @var{file} exists and is a regular file.
6101 True if @var{file} exists and its set-group-id bit is set.
6104 True if @var{file} exists and is a symbolic link.
6107 True if @var{file} exists and its "sticky" bit is set.
6110 True if @var{file} exists and is a named pipe (FIFO).
6113 True if @var{file} exists and is readable.
6116 True if @var{file} exists and has a size greater than zero.
6119 True if file descriptor @var{fd} is open and refers to a terminal.
6122 True if @var{file} exists and its set-user-id bit is set.
6125 True if @var{file} exists and is writable.
6128 True if @var{file} exists and is executable.
6131 True if @var{file} exists and is owned by the effective group id.
6134 True if @var{file} exists and is a symbolic link.
6137 True if @var{file} exists and has been modified since it was last read.
6140 True if @var{file} exists and is owned by the effective user id.
6143 True if @var{file} exists and is a socket.
6145 @item @var{file1} -ef @var{file2}
6146 True if @var{file1} and @var{file2} refer to the same device and
6149 @item @var{file1} -nt @var{file2}
6150 True if @var{file1} is newer (according to modification date)
6151 than @var{file2}, or if @var{file1} exists and @var{file2} does not.
6153 @item @var{file1} -ot @var{file2}
6154 True if @var{file1} is older than @var{file2},
6155 or if @var{file2} exists and @var{file1} does not.
6157 @item -o @var{optname}
6158 True if the shell option @var{optname} is enabled.
6159 The list of options appears in the description of the @option{-o}
6160 option to the @code{set} builtin (@pxref{The Set Builtin}).
6162 @item -v @var{varname}
6163 True if the shell variable @var{varname} is set (has been assigned a value).
6165 @item -z @var{string}
6166 True if the length of @var{string} is zero.
6168 @item -n @var{string}
6170 True if the length of @var{string} is non-zero.
6172 @item @var{string1} == @var{string2}
6173 @itemx @var{string1} = @var{string2}
6174 True if the strings are equal.
6175 @samp{=} should be used with the @code{test} command for @sc{posix} conformance.
6177 @item @var{string1} != @var{string2}
6178 True if the strings are not equal.
6180 @item @var{string1} < @var{string2}
6181 True if @var{string1} sorts before @var{string2} lexicographically.
6183 @item @var{string1} > @var{string2}
6184 True if @var{string1} sorts after @var{string2} lexicographically.
6186 @item @var{arg1} OP @var{arg2}
6188 @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}.
6189 These arithmetic binary operators return true if @var{arg1}
6190 is equal to, not equal to, less than, less than or equal to,
6191 greater than, or greater than or equal to @var{arg2},
6192 respectively. @var{Arg1} and @var{arg2}
6193 may be positive or negative integers.
6196 @node Shell Arithmetic
6197 @section Shell Arithmetic
6198 @cindex arithmetic, shell
6199 @cindex shell arithmetic
6200 @cindex expressions, arithmetic
6201 @cindex evaluation, arithmetic
6202 @cindex arithmetic evaluation
6204 The shell allows arithmetic expressions to be evaluated, as one of
6205 the shell expansions or by the @code{let} and the @option{-i} option
6206 to the @code{declare} builtins.
6208 Evaluation is done in fixed-width integers with no check for overflow,
6209 though division by 0 is trapped and flagged as an error.
6210 The operators and their precedence, associativity, and values
6211 are the same as in the C language.
6212 The following list of operators is grouped into levels of
6213 equal-precedence operators.
6214 The levels are listed in order of decreasing precedence.
6218 @item @var{id}++ @var{id}--
6219 variable post-increment and post-decrement
6221 @item ++@var{id} --@var{id}
6222 variable pre-increment and pre-decrement
6225 unary minus and plus
6228 logical and bitwise negation
6234 multiplication, division, remainder
6237 addition, subtraction
6240 left and right bitwise shifts
6246 equality and inequality
6252 bitwise exclusive OR
6263 @item expr ? expr : expr
6264 conditional operator
6266 @item = *= /= %= += -= <<= >>= &= ^= |=
6273 Shell variables are allowed as operands; parameter expansion is
6274 performed before the expression is evaluated.
6275 Within an expression, shell variables may also be referenced by name
6276 without using the parameter expansion syntax.
6277 A shell variable that is null or unset evaluates to 0 when referenced
6278 by name without using the parameter expansion syntax.
6279 The value of a variable is evaluated as an arithmetic expression
6280 when it is referenced, or when a variable which has been given the
6281 @var{integer} attribute using @samp{declare -i} is assigned a value.
6282 A null value evaluates to 0.
6283 A shell variable need not have its @var{integer} attribute turned on
6284 to be used in an expression.
6286 Constants with a leading 0 are interpreted as octal numbers.
6287 A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise,
6288 numbers take the form [@var{base}@code{#}]@var{n}, where the optional @var{base}
6289 is a decimal number between 2 and 64 representing the arithmetic
6290 base, and @var{n} is a number in that base. If @var{base}@code{#} is
6291 omitted, then base 10 is used.
6292 The digits greater than 9 are represented by the lowercase letters,
6293 the uppercase letters, @samp{@@}, and @samp{_}, in that order.
6294 If @var{base} is less than or equal to 36, lowercase and uppercase
6295 letters may be used interchangeably to represent numbers between 10
6298 Operators are evaluated in order of precedence. Sub-expressions in
6299 parentheses are evaluated first and may override the precedence
6304 @cindex alias expansion
6306 @var{Aliases} allow a string to be substituted for a word when it is used
6307 as the first word of a simple command.
6308 The shell maintains a list of aliases that may be set and unset with
6309 the @code{alias} and @code{unalias} builtin commands.
6311 The first word of each simple command, if unquoted, is checked to see
6313 If so, that word is replaced by the text of the alias.
6314 The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the
6315 shell metacharacters or quoting characters listed above may not appear
6317 The replacement text may contain any valid
6318 shell input, including shell metacharacters.
6319 The first word of the replacement text is tested for
6320 aliases, but a word that is identical to an alias being expanded
6321 is not expanded a second time.
6322 This means that one may alias @code{ls} to @code{"ls -F"},
6323 for instance, and Bash does not try to recursively expand the
6325 If the last character of the alias value is a
6326 @var{blank}, then the next command word following the
6327 alias is also checked for alias expansion.
6329 Aliases are created and listed with the @code{alias}
6330 command, and removed with the @code{unalias} command.
6332 There is no mechanism for using arguments in the replacement text,
6334 If arguments are needed, a shell function should be used
6335 (@pxref{Shell Functions}).
6337 Aliases are not expanded when the shell is not interactive,
6338 unless the @code{expand_aliases} shell option is set using
6339 @code{shopt} (@pxref{The Shopt Builtin}).
6341 The rules concerning the definition and use of aliases are
6342 somewhat confusing. Bash
6343 always reads at least one complete line
6344 of input before executing any
6345 of the commands on that line. Aliases are expanded when a
6346 command is read, not when it is executed. Therefore, an
6347 alias definition appearing on the same line as another
6348 command does not take effect until the next line of input is read.
6349 The commands following the alias definition
6350 on that line are not affected by the new alias.
6351 This behavior is also an issue when functions are executed.
6352 Aliases are expanded when a function definition is read,
6353 not when the function is executed, because a function definition
6354 is itself a compound command. As a consequence, aliases
6355 defined in a function are not available until after that
6356 function is executed. To be safe, always put
6357 alias definitions on a separate line, and do not use @code{alias}
6358 in compound commands.
6360 For almost every purpose, shell functions are preferred over aliases.
6366 Bash provides one-dimensional indexed and associative array variables.
6367 Any variable may be used as an indexed array;
6368 the @code{declare} builtin will explicitly declare an array.
6370 limit on the size of an array, nor any requirement that members
6371 be indexed or assigned contiguously.
6372 Indexed arrays are referenced using integers (including arithmetic
6373 expressions (@pxref{Shell Arithmetic})) and are zero-based;
6374 associative arrays use arbitrary strings.
6376 An indexed array is created automatically if any variable is assigned to
6379 @var{name}[@var{subscript}]=@var{value}
6384 is treated as an arithmetic expression that must evaluate to a number.
6385 To explicitly declare an array, use
6387 declare -a @var{name}
6392 declare -a @var{name}[@var{subscript}]
6395 is also accepted; the @var{subscript} is ignored.
6398 Associative arrays are created using
6400 declare -A @var{name}.
6404 specified for an array variable using the @code{declare} and
6405 @code{readonly} builtins. Each attribute applies to all members of
6408 Arrays are assigned to using compound assignments of the form
6410 @var{name}=(@var{value1} @var{value2} @dots{} )
6414 @var{value} is of the form @code{[@var{subscript}]=}@var{string}.
6415 Indexed array assignments do not require anything but @var{string}.
6416 When assigning to indexed arrays, if
6417 the optional subscript is supplied, that index is assigned to;
6418 otherwise the index of the element assigned is the last index assigned
6419 to by the statement plus one. Indexing starts at zero.
6421 When assigning to an associative array, the subscript is required.
6423 This syntax is also accepted by the @code{declare}
6424 builtin. Individual array elements may be assigned to using the
6425 @code{@var{name}[@var{subscript}]=@var{value}} syntax introduced above.
6427 Any element of an array may be referenced using
6428 @code{$@{@var{name}[@var{subscript}]@}}.
6429 The braces are required to avoid
6430 conflicts with the shell's filename expansion operators. If the
6431 @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members
6432 of the array @var{name}. These subscripts differ only when the word
6433 appears within double quotes.
6434 If the word is double-quoted,
6435 @code{$@{@var{name}[*]@}} expands to a single word with
6436 the value of each array member separated by the first character of the
6437 @env{IFS} variable, and @code{$@{@var{name}[@@]@}} expands each element of
6438 @var{name} to a separate word. When there are no array members,
6439 @code{$@{@var{name}[@@]@}} expands to nothing.
6440 If the double-quoted expansion occurs within a word, the expansion of
6441 the first parameter is joined with the beginning part of the original
6442 word, and the expansion of the last parameter is joined with the last
6443 part of the original word.
6444 This is analogous to the
6445 expansion of the special parameters @samp{@@} and @samp{*}.
6446 @code{$@{#@var{name}[@var{subscript}]@}} expands to the length of
6447 @code{$@{@var{name}[@var{subscript}]@}}.
6448 If @var{subscript} is @samp{@@} or
6449 @samp{*}, the expansion is the number of elements in the array.
6450 Referencing an array variable without a subscript is equivalent to
6451 referencing with a subscript of 0.
6452 If the @var{subscript}
6453 used to reference an element of an indexed array
6454 evaluates to a number less than zero, it is used as
6455 an offset from one greater than the array's maximum index (so a subcript
6456 of -1 refers to the last element of the array).
6458 An array variable is considered set if a subscript has been assigned a
6459 value. The null string is a valid value.
6461 The @code{unset} builtin is used to destroy arrays.
6462 @code{unset @var{name}[@var{subscript}]}
6463 destroys the array element at index @var{subscript}.
6464 Care must be taken to avoid unwanted side effects caused by filename
6466 @code{unset @var{name}}, where @var{name} is an array, removes the
6467 entire array. A subscript of @samp{*} or @samp{@@} also removes the
6470 The @code{declare}, @code{local}, and @code{readonly}
6471 builtins each accept a @option{-a} option to specify an indexed
6472 array and a @option{-A} option to specify an associative array.
6473 If both options are supplied, @option{-A} takes precedence.
6474 The @code{read} builtin accepts a @option{-a}
6475 option to assign a list of words read from the standard input
6476 to an array, and can read values from the standard input into
6477 individual array elements. The @code{set} and @code{declare}
6478 builtins display array values in a way that allows them to be
6481 @node The Directory Stack
6482 @section The Directory Stack
6483 @cindex directory stack
6486 * Directory Stack Builtins:: Bash builtin commands to manipulate
6487 the directory stack.
6490 The directory stack is a list of recently-visited directories. The
6491 @code{pushd} builtin adds directories to the stack as it changes
6492 the current directory, and the @code{popd} builtin removes specified
6493 directories from the stack and changes the current directory to
6494 the directory removed. The @code{dirs} builtin displays the contents
6495 of the directory stack.
6497 The contents of the directory stack are also visible
6498 as the value of the @env{DIRSTACK} shell variable.
6500 @node Directory Stack Builtins
6501 @subsection Directory Stack Builtins
6508 dirs [-clpv] [+@var{N} | -@var{N}]
6511 Display the list of currently remembered directories. Directories
6512 are added to the list with the @code{pushd} command; the
6513 @code{popd} command removes directories from the list.
6517 Clears the directory stack by deleting all of the elements.
6519 Produces a listing using full pathnames;
6520 the default listing format uses a tilde to denote the home directory.
6522 Causes @code{dirs} to print the directory stack with one entry per
6525 Causes @code{dirs} to print the directory stack with one entry per
6526 line, prefixing each entry with its index in the stack.
6528 Displays the @var{N}th directory (counting from the left of the
6529 list printed by @code{dirs} when invoked without options), starting
6532 Displays the @var{N}th directory (counting from the right of the
6533 list printed by @code{dirs} when invoked without options), starting
6540 popd [-n] [+@var{N} | -@var{N}]
6543 Remove the top entry from the directory stack, and @code{cd}
6544 to the new top directory.
6545 When no arguments are given, @code{popd}
6546 removes the top directory from the stack and
6547 performs a @code{cd} to the new top directory. The
6548 elements are numbered from 0 starting at the first directory listed with
6549 @code{dirs}; that is, @code{popd} is equivalent to @code{popd +0}.
6553 Suppresses the normal change of directory when removing directories
6554 from the stack, so that only the stack is manipulated.
6556 Removes the @var{N}th directory (counting from the left of the
6557 list printed by @code{dirs}), starting with zero.
6559 Removes the @var{N}th directory (counting from the right of the
6560 list printed by @code{dirs}), starting with zero.
6566 pushd [-n] [@var{+N} | @var{-N} | @var{dir}]
6569 Save the current directory on the top of the directory stack
6570 and then @code{cd} to @var{dir}.
6571 With no arguments, @code{pushd} exchanges the top two directories.
6575 Suppresses the normal change of directory when adding directories
6576 to the stack, so that only the stack is manipulated.
6578 Brings the @var{N}th directory (counting from the left of the
6579 list printed by @code{dirs}, starting with zero) to the top of
6580 the list by rotating the stack.
6582 Brings the @var{N}th directory (counting from the right of the
6583 list printed by @code{dirs}, starting with zero) to the top of
6584 the list by rotating the stack.
6586 Makes the current working directory be the top of the stack, making
6587 it the new current directory as if it had been supplied as an argument
6588 to the @code{cd} builtin.
6592 @node Controlling the Prompt
6593 @section Controlling the Prompt
6596 The value of the variable @env{PROMPT_COMMAND} is examined just before
6597 Bash prints each primary prompt. If @env{PROMPT_COMMAND} is set and
6598 has a non-null value, then the
6599 value is executed just as if it had been typed on the command line.
6601 In addition, the following table describes the special characters which
6602 can appear in the prompt variables @env{PS1} to @env{PS4}:
6608 The date, in "Weekday Month Date" format (e.g., "Tue May 26").
6609 @item \D@{@var{format}@}
6610 The @var{format} is passed to @code{strftime}(3) and the result is inserted
6611 into the prompt string; an empty @var{format} results in a locale-specific
6612 time representation. The braces are required.
6614 An escape character.
6616 The hostname, up to the first `.'.
6620 The number of jobs currently managed by the shell.
6622 The basename of the shell's terminal device name.
6628 The name of the shell, the basename of @code{$0} (the portion
6629 following the final slash).
6631 The time, in 24-hour HH:MM:SS format.
6633 The time, in 12-hour HH:MM:SS format.
6635 The time, in 12-hour am/pm format.
6637 The time, in 24-hour HH:MM format.
6639 The username of the current user.
6641 The version of Bash (e.g., 2.00)
6643 The release of Bash, version + patchlevel (e.g., 2.00.0)
6645 The current working directory, with @env{$HOME} abbreviated with a tilde
6646 (uses the @env{$PROMPT_DIRTRIM} variable).
6648 The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde.
6650 The history number of this command.
6652 The command number of this command.
6654 If the effective uid is 0, @code{#}, otherwise @code{$}.
6656 The character whose ASCII code is the octal value @var{nnn}.
6660 Begin a sequence of non-printing characters. This could be used to
6661 embed a terminal control sequence into the prompt.
6663 End a sequence of non-printing characters.
6666 The command number and the history number are usually different:
6667 the history number of a command is its position in the history
6668 list, which may include commands restored from the history file
6669 (@pxref{Bash History Facilities}), while the command number is
6670 the position in the sequence of commands executed during the current
6673 After the string is decoded, it is expanded via
6674 parameter expansion, command substitution, arithmetic
6675 expansion, and quote removal, subject to the value of the
6676 @code{promptvars} shell option (@pxref{Bash Builtins}).
6678 @node The Restricted Shell
6679 @section The Restricted Shell
6680 @cindex restricted shell
6682 If Bash is started with the name @code{rbash}, or the
6683 @option{--restricted}
6686 option is supplied at invocation, the shell becomes restricted.
6687 A restricted shell is used to
6688 set up an environment more controlled than the standard shell.
6689 A restricted shell behaves identically to @code{bash}
6690 with the exception that the following are disallowed or not performed:
6694 Changing directories with the @code{cd} builtin.
6696 Setting or unsetting the values of the @env{SHELL}, @env{PATH},
6697 @env{ENV}, or @env{BASH_ENV} variables.
6699 Specifying command names containing slashes.
6701 Specifying a filename containing a slash as an argument to the @code{.}
6704 Specifying a filename containing a slash as an argument to the @option{-p}
6705 option to the @code{hash} builtin command.
6707 Importing function definitions from the shell environment at startup.
6709 Parsing the value of @env{SHELLOPTS} from the shell environment at startup.
6711 Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&},
6712 @samp{&>}, and @samp{>>} redirection operators.
6714 Using the @code{exec} builtin to replace the shell with another command.
6716 Adding or deleting builtin commands with the
6717 @option{-f} and @option{-d} options to the @code{enable} builtin.
6719 Using the @code{enable} builtin command to enable disabled shell builtins.
6721 Specifying the @option{-p} option to the @code{command} builtin.
6723 Turning off restricted mode with @samp{set +r} or @samp{set +o restricted}.
6726 These restrictions are enforced after any startup files are read.
6728 When a command that is found to be a shell script is executed
6729 (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in
6730 the shell spawned to execute the script.
6732 @node Bash POSIX Mode
6733 @section Bash POSIX Mode
6736 Starting Bash with the @option{--posix} command-line option or executing
6737 @samp{set -o posix} while Bash is running will cause Bash to conform more
6738 closely to the @sc{posix} standard by changing the behavior to
6739 match that specified by @sc{posix} in areas where the Bash default differs.
6741 When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the
6744 The following list is what's changed when `@sc{posix} mode' is in effect:
6748 When a command in the hash table no longer exists, Bash will re-search
6749 @env{$PATH} to find the new location. This is also available with
6750 @samp{shopt -s checkhash}.
6753 The message printed by the job control code and builtins when a job
6754 exits with a non-zero status is `Done(status)'.
6757 The message printed by the job control code and builtins when a job
6758 is stopped is `Stopped(@var{signame})', where @var{signame} is, for
6759 example, @code{SIGTSTP}.
6762 The @code{bg} builtin uses the required format to describe each job placed
6763 in the background, which does not include an indication of whether the job
6764 is the current or previous job.
6767 Reserved words appearing in a context where reserved words are recognized
6768 do not undergo alias expansion.
6771 The @sc{posix} @env{PS1} and @env{PS2} expansions of @samp{!} to
6772 the history number and @samp{!!} to @samp{!} are enabled,
6773 and parameter expansion is performed on the values of @env{PS1} and
6774 @env{PS2} regardless of the setting of the @code{promptvars} option.
6777 The @sc{posix} startup files are executed (@env{$ENV}) rather than
6778 the normal Bash files.
6781 Tilde expansion is only performed on assignments preceding a command
6782 name, rather than on all assignment statements on the line.
6785 The @code{command} builtin does not prevent builtins that take assignment
6786 statements as arguments from expanding them as assignment statements;
6787 when not in POSIX mode, assignment builtins lose their assignment
6788 statement expansion properties when preceded by @code{command}.
6791 The default history file is @file{~/.sh_history} (this is the
6792 default value of @env{$HISTFILE}).
6795 The output of @samp{kill -l} prints all the signal names on a single line,
6796 separated by spaces, without the @samp{SIG} prefix.
6799 The @code{kill} builtin does not accept signal names with a @samp{SIG}
6803 Non-interactive shells exit if @var{filename} in @code{.} @var{filename}
6807 Non-interactive shells exit if a syntax error in an arithmetic expansion
6808 results in an invalid expression.
6811 Non-interactive shells exit if there is a syntax error in a script read
6812 with the @code{.} or @code{source} builtins, or in a string processed by
6813 the @code{eval} builtin.
6816 Redirection operators do not perform filename expansion on the word
6817 in the redirection unless the shell is interactive.
6820 Redirection operators do not perform word splitting on the word in the
6824 Function names must be valid shell @code{name}s. That is, they may not
6825 contain characters other than letters, digits, and underscores, and
6826 may not start with a digit. Declaring a function with an invalid name
6827 causes a fatal syntax error in non-interactive shells.
6830 Function names may not be the same as one of the @sc{posix} special
6834 @sc{posix} special builtins are found before shell functions
6835 during command lookup.
6838 The @code{time} reserved word may be used by itself as a command. When
6839 used in this way, it displays timing statistics for the shell and its
6840 completed children. The @env{TIMEFORMAT} variable controls the format
6841 of the timing information.
6844 When parsing and expanding a $@{@dots{}@} expansion that appears within
6845 double quotes, single quotes are no longer special and cannot be used to
6846 quote a closing brace or other special character, unless the operator is
6847 one of those defined to perform pattern removal. In this case, they do
6848 not have to appear as matched pairs.
6851 The parser does not recognize @code{time} as a reserved word if the next
6852 token begins with a @samp{-}.
6855 If a @sc{posix} special builtin returns an error status, a
6856 non-interactive shell exits. The fatal errors are those listed in
6857 the @sc{posix} standard, and include things like passing incorrect options,
6858 redirection errors, variable assignment errors for assignments preceding
6859 the command name, and so on.
6862 A non-interactive shell exits with an error status if a variable
6863 assignment error occurs when no command name follows the assignment
6865 A variable assignment error occurs, for example, when trying to assign
6866 a value to a readonly variable.
6869 A non-interactive shell exists with an error status if a variable
6870 assignment error occurs in an assignment statement preceding a special
6871 builtin, but not with any other simple command.
6874 A non-interactive shell exits with an error status if the iteration
6875 variable in a @code{for} statement or the selection variable in a
6876 @code{select} statement is a readonly variable.
6879 Process substitution is not available.
6882 While variable indirection is available, it may not be applied to the
6883 @samp{#} and @samp{?} special parameters.
6886 Assignment statements preceding @sc{posix} special builtins
6887 persist in the shell environment after the builtin completes.
6890 Assignment statements preceding shell function calls persist in the
6891 shell environment after the function returns, as if a @sc{posix}
6892 special builtin command had been executed.
6895 The @code{export} and @code{readonly} builtin commands display their
6896 output in the format required by @sc{posix}.
6899 The @code{trap} builtin displays signal names without the leading
6903 The @code{trap} builtin doesn't check the first argument for a possible
6904 signal specification and revert the signal handling to the original
6905 disposition if it is, unless that argument consists solely of digits and
6906 is a valid signal number. If users want to reset the handler for a given
6907 signal to the original disposition, they should use @samp{-} as the
6911 The @code{.} and @code{source} builtins do not search the current directory
6912 for the filename argument if it is not found by searching @env{PATH}.
6915 Subshells spawned to execute command substitutions inherit the value of
6916 the @option{-e} option from the parent shell. When not in @sc{posix} mode,
6917 Bash clears the @option{-e} option in such subshells.
6920 Alias expansion is always enabled, even in non-interactive shells.
6923 When the @code{alias} builtin displays alias definitions, it does not
6924 display them with a leading @samp{alias } unless the @option{-p} option
6928 When the @code{set} builtin is invoked without options, it does not display
6929 shell function names and definitions.
6932 When the @code{set} builtin is invoked without options, it displays
6933 variable values without quotes, unless they contain shell metacharacters,
6934 even if the result contains nonprinting characters.
6937 When the @code{cd} builtin is invoked in @var{logical} mode, and the pathname
6938 constructed from @code{$PWD} and the directory name supplied as an argument
6939 does not refer to an existing directory, @code{cd} will fail instead of
6940 falling back to @var{physical} mode.
6943 The @code{pwd} builtin verifies that the value it prints is the same as the
6944 current directory, even if it is not asked to check the file system with the
6948 When listing the history, the @code{fc} builtin does not include an
6949 indication of whether or not a history entry has been modified.
6952 The default editor used by @code{fc} is @code{ed}.
6955 The @code{type} and @code{command} builtins will not report a non-executable
6956 file as having been found, though the shell will attempt to execute such a
6957 file if it is the only so-named file found in @code{$PATH}.
6960 The @code{vi} editing mode will invoke the @code{vi} editor directly when
6961 the @samp{v} command is run, instead of checking @code{$VISUAL} and
6965 When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret
6966 any arguments to @code{echo} as options. Each argument is displayed, after
6967 escape characters are converted.
6970 The @code{ulimit} builtin uses a block size of 512 bytes for the @option{-c}
6971 and @option{-f} options.
6974 The arrival of @code{SIGCHLD} when a trap is set on @code{SIGCHLD} does
6975 not interrupt the @code{wait} builtin and cause it to return immediately.
6976 The trap command is run once for each child that exits.
6980 There is other @sc{posix} behavior that Bash does not implement by
6981 default even when in @sc{posix} mode.
6987 The @code{fc} builtin checks @code{$EDITOR} as a program to edit history
6988 entries if @code{FCEDIT} is unset, rather than defaulting directly to
6989 @code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset.
6992 As noted above, Bash requires the @code{xpg_echo} option to be enabled for
6993 the @code{echo} builtin to be fully conformant.
6997 Bash can be configured to be @sc{posix}-conformant by default, by specifying
6998 the @option{--enable-strict-posix-default} to @code{configure} when building
6999 (@pxref{Optional Features}).
7002 @chapter Job Control
7004 This chapter discusses what job control is, how it works, and how
7005 Bash allows you to access its facilities.
7008 * Job Control Basics:: How job control works.
7009 * Job Control Builtins:: Bash builtin commands used to interact
7011 * Job Control Variables:: Variables Bash uses to customize job
7015 @node Job Control Basics
7016 @section Job Control Basics
7020 @cindex suspending jobs
7023 refers to the ability to selectively stop (suspend)
7024 the execution of processes and continue (resume)
7025 their execution at a later point. A user typically employs
7026 this facility via an interactive interface supplied jointly
7027 by the operating system kernel's terminal driver and Bash.
7029 The shell associates a @var{job} with each pipeline. It keeps a
7030 table of currently executing jobs, which may be listed with the
7031 @code{jobs} command. When Bash starts a job
7032 asynchronously, it prints a line that looks
7038 indicating that this job is job number 1 and that the process @sc{id}
7039 of the last process in the pipeline associated with this job is
7040 25647. All of the processes in a single pipeline are members of
7041 the same job. Bash uses the @var{job} abstraction as the
7042 basis for job control.
7044 To facilitate the implementation of the user interface to job
7045 control, the operating system maintains the notion of a current terminal
7046 process group @sc{id}. Members of this process group (processes whose
7047 process group @sc{id} is equal to the current terminal process group
7048 @sc{id}) receive keyboard-generated signals such as @code{SIGINT}.
7049 These processes are said to be in the foreground. Background
7050 processes are those whose process group @sc{id} differs from the
7051 terminal's; such processes are immune to keyboard-generated
7052 signals. Only foreground processes are allowed to read from or, if
7053 the user so specifies with @code{stty tostop}, write to the terminal.
7054 Background processes which attempt to
7055 read from (write to when @code{stty tostop} is in effect) the
7056 terminal are sent a @code{SIGTTIN} (@code{SIGTTOU})
7057 signal by the kernel's terminal driver,
7058 which, unless caught, suspends the process.
7060 If the operating system on which Bash is running supports
7061 job control, Bash contains facilities to use it. Typing the
7062 @var{suspend} character (typically @samp{^Z}, Control-Z) while a
7063 process is running causes that process to be stopped and returns
7064 control to Bash. Typing the @var{delayed suspend} character
7065 (typically @samp{^Y}, Control-Y) causes the process to be stopped
7066 when it attempts to read input from the terminal, and control to
7067 be returned to Bash. The user then manipulates the state of
7068 this job, using the @code{bg} command to continue it in the
7069 background, the @code{fg} command to continue it in the
7070 foreground, or the @code{kill} command to kill it. A @samp{^Z}
7071 takes effect immediately, and has the additional side effect of
7072 causing pending output and typeahead to be discarded.
7074 There are a number of ways to refer to a job in the shell. The
7075 character @samp{%} introduces a job specification (@var{jobspec}).
7077 Job number @code{n} may be referred to as @samp{%n}.
7078 The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the
7079 current job, which is the last job stopped while it was in the foreground
7080 or started in the background.
7081 A single @samp{%} (with no accompanying job specification) also refers
7083 The previous job may be referenced using @samp{%-}.
7084 If there is only a single job, @samp{%+} and @samp{%-} can both be used
7085 to refer to that job.
7086 In output pertaining to jobs (e.g., the output of the @code{jobs}
7087 command), the current job is always flagged with a @samp{+}, and the
7088 previous job with a @samp{-}.
7090 A job may also be referred to
7091 using a prefix of the name used to start it, or using a substring
7092 that appears in its command line. For example, @samp{%ce} refers
7093 to a stopped @code{ce} job. Using @samp{%?ce}, on the
7094 other hand, refers to any job containing the string @samp{ce} in
7095 its command line. If the prefix or substring matches more than one job,
7096 Bash reports an error.
7098 Simply naming a job can be used to bring it into the foreground:
7099 @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the
7100 background into the foreground. Similarly, @samp{%1 &} resumes
7101 job 1 in the background, equivalent to @samp{bg %1}
7103 The shell learns immediately whenever a job changes state.
7104 Normally, Bash waits until it is about to print a prompt
7105 before reporting changes in a job's status so as to not interrupt
7107 If the @option{-b} option to the @code{set} builtin is enabled,
7108 Bash reports such changes immediately (@pxref{The Set Builtin}).
7109 Any trap on @code{SIGCHLD} is executed for each child process
7112 If an attempt to exit Bash is made while jobs are stopped, (or running, if
7113 the @code{checkjobs} option is enabled -- see @ref{The Shopt Builtin}), the
7114 shell prints a warning message, and if the @code{checkjobs} option is
7115 enabled, lists the jobs and their statuses.
7116 The @code{jobs} command may then be used to inspect their status.
7117 If a second attempt to exit is made without an intervening command,
7118 Bash does not print another warning, and any stopped jobs are terminated.
7120 @node Job Control Builtins
7121 @section Job Control Builtins
7128 bg [@var{jobspec} @dots{}]
7131 Resume each suspended job @var{jobspec} in the background, as if it
7132 had been started with @samp{&}.
7133 If @var{jobspec} is not supplied, the current job is used.
7134 The return status is zero unless it is run when job control is not
7135 enabled, or, when run with job control enabled, any
7136 @var{jobspec} was not found or specifies a job
7137 that was started without job control.
7145 Resume the job @var{jobspec} in the foreground and make it the current job.
7146 If @var{jobspec} is not supplied, the current job is used.
7147 The return status is that of the command placed into the foreground,
7148 or non-zero if run when job control is disabled or, when run with
7149 job control enabled, @var{jobspec} does not specify a valid job or
7150 @var{jobspec} specifies a job that was started without job control.
7155 jobs [-lnprs] [@var{jobspec}]
7156 jobs -x @var{command} [@var{arguments}]
7159 The first form lists the active jobs. The options have the
7164 List process @sc{id}s in addition to the normal information.
7167 Display information only about jobs that have changed status since
7168 the user was last notified of their status.
7171 List only the process @sc{id} of the job's process group leader.
7174 Display only running jobs.
7177 Display only stopped jobs.
7180 If @var{jobspec} is given,
7181 output is restricted to information about that job.
7182 If @var{jobspec} is not supplied, the status of all jobs is
7185 If the @option{-x} option is supplied, @code{jobs} replaces any
7186 @var{jobspec} found in @var{command} or @var{arguments} with the
7187 corresponding process group @sc{id}, and executes @var{command},
7188 passing it @var{argument}s, returning its exit status.
7193 kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid}
7194 kill -l [@var{exit_status}]
7197 Send a signal specified by @var{sigspec} or @var{signum} to the process
7198 named by job specification @var{jobspec} or process @sc{id} @var{pid}.
7199 @var{sigspec} is either a case-insensitive signal name such as
7200 @code{SIGINT} (with or without the @code{SIG} prefix)
7201 or a signal number; @var{signum} is a signal number.
7202 If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used.
7203 The @option{-l} option lists the signal names.
7204 If any arguments are supplied when @option{-l} is given, the names of the
7205 signals corresponding to the arguments are listed, and the return status
7207 @var{exit_status} is a number specifying a signal number or the exit
7208 status of a process terminated by a signal.
7209 The return status is zero if at least one signal was successfully sent,
7210 or non-zero if an error occurs or an invalid option is encountered.
7215 wait [@var{jobspec} or @var{pid} @dots{}]
7218 Wait until the child process specified by each process @sc{id} @var{pid}
7219 or job specification @var{jobspec} exits and return the exit status of the
7220 last command waited for.
7221 If a job spec is given, all processes in the job are waited for.
7222 If no arguments are given, all currently active child processes are
7223 waited for, and the return status is zero.
7224 If neither @var{jobspec} nor @var{pid} specifies an active child process
7225 of the shell, the return status is 127.
7230 disown [-ar] [-h] [@var{jobspec} @dots{}]
7233 Without options, remove each @var{jobspec} from the table of
7235 If the @option{-h} option is given, the job is not removed from the table,
7236 but is marked so that @code{SIGHUP} is not sent to the job if the shell
7237 receives a @code{SIGHUP}.
7238 If @var{jobspec} is not present, and neither the @option{-a} nor @option{-r}
7239 option is supplied, the current job is used.
7240 If no @var{jobspec} is supplied, the @option{-a} option means to remove or
7241 mark all jobs; the @option{-r} option without a @var{jobspec}
7242 argument restricts operation to running jobs.
7250 Suspend the execution of this shell until it receives a
7251 @code{SIGCONT} signal.
7252 A login shell cannot be suspended; the @option{-f}
7253 option can be used to override this and force the suspension.
7256 When job control is not active, the @code{kill} and @code{wait}
7257 builtins do not accept @var{jobspec} arguments. They must be
7258 supplied process @sc{id}s.
7260 @node Job Control Variables
7261 @section Job Control Variables
7266 This variable controls how the shell interacts with the user and
7267 job control. If this variable exists then single word simple
7268 commands without redirections are treated as candidates for resumption
7269 of an existing job. There is no ambiguity allowed; if there is
7270 more than one job beginning with the string typed, then
7271 the most recently accessed job will be selected.
7272 The name of a stopped job, in this context, is the command line
7273 used to start it. If this variable is set to the value @samp{exact},
7274 the string supplied must match the name of a stopped job exactly;
7275 if set to @samp{substring},
7276 the string supplied needs to match a substring of the name of a
7277 stopped job. The @samp{substring} value provides functionality
7278 analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}).
7279 If set to any other value, the supplied string must
7280 be a prefix of a stopped job's name; this provides functionality
7281 analogous to the @samp{%} job @sc{id}.
7285 @set readline-appendix
7286 @set history-appendix
7287 @cindex Readline, how to use
7288 @include rluser.texi
7289 @cindex History, how to use
7290 @include hsuser.texi
7291 @clear readline-appendix
7292 @clear history-appendix
7294 @node Installing Bash
7295 @chapter Installing Bash
7297 This chapter provides basic instructions for installing Bash on
7298 the various supported platforms. The distribution supports the
7299 @sc{gnu} operating systems, nearly every version of Unix, and several
7300 non-Unix systems such as BeOS and Interix.
7301 Other independent ports exist for
7302 @sc{ms-dos}, @sc{os/2}, and Windows platforms.
7305 * Basic Installation:: Installation instructions.
7306 * Compilers and Options:: How to set special options for various
7308 * Compiling For Multiple Architectures:: How to compile Bash for more
7309 than one kind of system from
7310 the same source tree.
7311 * Installation Names:: How to set the various paths used by the installation.
7312 * Specifying the System Type:: How to configure Bash for a particular system.
7313 * Sharing Defaults:: How to share default configuration values among GNU
7315 * Operation Controls:: Options recognized by the configuration program.
7316 * Optional Features:: How to enable and disable optional features when
7320 @node Basic Installation
7321 @section Basic Installation
7322 @cindex installation
7323 @cindex configuration
7324 @cindex Bash installation
7325 @cindex Bash configuration
7327 These are installation instructions for Bash.
7329 The simplest way to compile Bash is:
7333 @code{cd} to the directory containing the source code and type
7334 @samp{./configure} to configure Bash for your system. If you're
7335 using @code{csh} on an old version of System V, you might need to
7336 type @samp{sh ./configure} instead to prevent @code{csh} from trying
7337 to execute @code{configure} itself.
7339 Running @code{configure} takes some time.
7340 While running, it prints messages telling which features it is
7344 Type @samp{make} to compile Bash and build the @code{bashbug} bug
7348 Optionally, type @samp{make tests} to run the Bash test suite.
7351 Type @samp{make install} to install @code{bash} and @code{bashbug}.
7352 This will also install the manual pages and Info file.
7356 The @code{configure} shell script attempts to guess correct
7357 values for various system-dependent variables used during
7358 compilation. It uses those values to create a @file{Makefile} in
7359 each directory of the package (the top directory, the
7360 @file{builtins}, @file{doc}, and @file{support} directories,
7361 each directory under @file{lib}, and several others). It also creates a
7362 @file{config.h} file containing system-dependent definitions.
7363 Finally, it creates a shell script named @code{config.status} that you
7364 can run in the future to recreate the current configuration, a
7365 file @file{config.cache} that saves the results of its tests to
7366 speed up reconfiguring, and a file @file{config.log} containing
7367 compiler output (useful mainly for debugging @code{configure}).
7369 @file{config.cache} contains results you don't want to keep, you
7370 may remove or edit it.
7372 To find out more about the options and arguments that the
7373 @code{configure} script understands, type
7376 bash-2.04$ ./configure --help
7380 at the Bash prompt in your Bash source directory.
7382 If you need to do unusual things to compile Bash, please
7383 try to figure out how @code{configure} could check whether or not
7384 to do them, and mail diffs or instructions to
7385 @email{bash-maintainers@@gnu.org} so they can be
7386 considered for the next release.
7388 The file @file{configure.in} is used to create @code{configure}
7389 by a program called Autoconf. You only need
7390 @file{configure.in} if you want to change it or regenerate
7391 @code{configure} using a newer version of Autoconf. If
7392 you do this, make sure you are using Autoconf version 2.50 or
7395 You can remove the program binaries and object files from the
7396 source code directory by typing @samp{make clean}. To also remove the
7397 files that @code{configure} created (so you can compile Bash for
7398 a different kind of computer), type @samp{make distclean}.
7400 @node Compilers and Options
7401 @section Compilers and Options
7403 Some systems require unusual options for compilation or linking
7404 that the @code{configure} script does not know about. You can
7405 give @code{configure} initial values for variables by setting
7406 them in the environment. Using a Bourne-compatible shell, you
7407 can do that on the command line like this:
7410 CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
7413 On systems that have the @code{env} program, you can do it like this:
7416 env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
7419 The configuration process uses GCC to build Bash if it
7422 @node Compiling For Multiple Architectures
7423 @section Compiling For Multiple Architectures
7425 You can compile Bash for more than one kind of computer at the
7426 same time, by placing the object files for each architecture in their
7427 own directory. To do this, you must use a version of @code{make} that
7428 supports the @code{VPATH} variable, such as GNU @code{make}.
7430 directory where you want the object files and executables to go and run
7431 the @code{configure} script from the source directory. You may need to
7432 supply the @option{--srcdir=PATH} argument to tell @code{configure} where the
7433 source files are. @code{configure} automatically checks for the
7434 source code in the directory that @code{configure} is in and in `..'.
7436 If you have to use a @code{make} that does not supports the @code{VPATH}
7437 variable, you can compile Bash for one architecture at a
7438 time in the source code directory. After you have installed
7439 Bash for one architecture, use @samp{make distclean} before
7440 reconfiguring for another architecture.
7442 Alternatively, if your system supports symbolic links, you can use the
7443 @file{support/mkclone} script to create a build tree which has
7444 symbolic links back to each file in the source directory. Here's an
7445 example that creates a build directory in the current directory from a
7446 source directory @file{/usr/gnu/src/bash-2.0}:
7449 bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 .
7453 The @code{mkclone} script requires Bash, so you must have already built
7454 Bash for at least one architecture before you can create build
7455 directories for other architectures.
7457 @node Installation Names
7458 @section Installation Names
7460 By default, @samp{make install} will install into
7461 @file{/usr/local/bin}, @file{/usr/local/man}, etc. You can
7462 specify an installation prefix other than @file{/usr/local} by
7463 giving @code{configure} the option @option{--prefix=@var{PATH}},
7464 or by specifying a value for the @code{DESTDIR} @samp{make}
7465 variable when running @samp{make install}.
7467 You can specify separate installation prefixes for
7468 architecture-specific files and architecture-independent files.
7469 If you give @code{configure} the option
7470 @option{--exec-prefix=@var{PATH}}, @samp{make install} will use
7471 @var{PATH} as the prefix for installing programs and libraries.
7472 Documentation and other data files will still use the regular prefix.
7474 @node Specifying the System Type
7475 @section Specifying the System Type
7477 There may be some features @code{configure} can not figure out
7478 automatically, but need to determine by the type of host Bash
7479 will run on. Usually @code{configure} can figure that
7480 out, but if it prints a message saying it can not guess the host
7481 type, give it the @option{--host=TYPE} option. @samp{TYPE} can
7482 either be a short name for the system type, such as @samp{sun4},
7483 or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM}
7484 (e.g., @samp{i386-unknown-freebsd4.2}).
7486 See the file @file{support/config.sub} for the possible
7487 values of each field.
7489 @node Sharing Defaults
7490 @section Sharing Defaults
7492 If you want to set default values for @code{configure} scripts to
7493 share, you can create a site shell script called
7494 @code{config.site} that gives default values for variables like
7495 @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure}
7496 looks for @file{PREFIX/share/config.site} if it exists, then
7497 @file{PREFIX/etc/config.site} if it exists. Or, you can set the
7498 @code{CONFIG_SITE} environment variable to the location of the site
7499 script. A warning: the Bash @code{configure} looks for a site script,
7500 but not all @code{configure} scripts do.
7502 @node Operation Controls
7503 @section Operation Controls
7505 @code{configure} recognizes the following options to control how it
7510 @item --cache-file=@var{file}
7511 Use and save the results of the tests in
7512 @var{file} instead of @file{./config.cache}. Set @var{file} to
7513 @file{/dev/null} to disable caching, for debugging
7517 Print a summary of the options to @code{configure}, and exit.
7522 Do not print messages saying which checks are being made.
7524 @item --srcdir=@var{dir}
7525 Look for the Bash source code in directory @var{dir}. Usually
7526 @code{configure} can determine that directory automatically.
7529 Print the version of Autoconf used to generate the @code{configure}
7533 @code{configure} also accepts some other, not widely used, boilerplate
7534 options. @samp{configure --help} prints the complete list.
7536 @node Optional Features
7537 @section Optional Features
7539 The Bash @code{configure} has a number of @option{--enable-@var{feature}}
7540 options, where @var{feature} indicates an optional part of Bash.
7541 There are also several @option{--with-@var{package}} options,
7542 where @var{package} is something like @samp{bash-malloc} or @samp{purify}.
7543 To turn off the default use of a package, use
7544 @option{--without-@var{package}}. To configure Bash without a feature
7545 that is enabled by default, use @option{--disable-@var{feature}}.
7547 Here is a complete list of the @option{--enable-} and
7548 @option{--with-} options that the Bash @code{configure} recognizes.
7552 Define if you are using the Andrew File System from Transarc.
7554 @item --with-bash-malloc
7555 Use the Bash version of
7556 @code{malloc} in the directory @file{lib/malloc}. This is not the same
7557 @code{malloc} that appears in @sc{gnu} libc, but an older version
7558 originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc}
7559 is very fast, but wastes some space on each allocation.
7560 This option is enabled by default.
7561 The @file{NOTES} file contains a list of systems for
7562 which this should be turned off, and @code{configure} disables this
7563 option automatically for a number of systems.
7566 Use the curses library instead of the termcap library. This should
7567 be supplied if your system has an inadequate or incomplete termcap
7570 @item --with-gnu-malloc
7571 A synonym for @code{--with-bash-malloc}.
7573 @item --with-installed-readline[=@var{PREFIX}]
7574 Define this to make Bash link with a locally-installed version of Readline
7575 rather than the version in @file{lib/readline}. This works only with
7576 Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not
7577 supplied, @code{configure} uses the values of the make variables
7578 @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix}
7579 by default, to find the installed version of Readline if it is not in
7580 the standard system include and library directories.
7581 If @var{PREFIX} is @code{no}, Bash links with the version in
7582 @file{lib/readline}.
7583 If @var{PREFIX} is set to any other value, @code{configure} treats it as
7584 a directory pathname and looks for
7585 the installed version of Readline in subdirectories of that directory
7586 (include files in @var{PREFIX}/@code{include} and the library in
7587 @var{PREFIX}/@code{lib}).
7590 Define this to use the Purify memory allocation checker from Rational
7593 @item --enable-minimal-config
7594 This produces a shell with minimal features, close to the historical
7598 There are several @option{--enable-} options that alter how Bash is
7599 compiled and linked, rather than changing run-time features.
7602 @item --enable-largefile
7603 Enable support for @uref{http://www.sas.com/standards/large_file/x_open.20Mar96.html,
7604 large files} if the operating system requires special compiler options
7605 to build programs which can access large files. This is enabled by
7606 default, if the operating system provides large file support.
7608 @item --enable-profiling
7609 This builds a Bash binary that produces profiling information to be
7610 processed by @code{gprof} each time it is executed.
7612 @item --enable-static-link
7613 This causes Bash to be linked statically, if @code{gcc} is being used.
7614 This could be used to build a version to use as root's shell.
7617 The @samp{minimal-config} option can be used to disable all of
7618 the following options, but it is processed first, so individual
7619 options may be enabled using @samp{enable-@var{feature}}.
7621 All of the following options except for @samp{disabled-builtins} and
7622 @samp{xpg-echo-default} are
7623 enabled by default, unless the operating system does not provide the
7627 @item --enable-alias
7628 Allow alias expansion and include the @code{alias} and @code{unalias}
7629 builtins (@pxref{Aliases}).
7631 @item --enable-arith-for-command
7632 Include support for the alternate form of the @code{for} command
7633 that behaves like the C language @code{for} statement
7634 (@pxref{Looping Constructs}).
7636 @item --enable-array-variables
7637 Include support for one-dimensional array shell variables
7640 @item --enable-bang-history
7641 Include support for @code{csh}-like history substitution
7642 (@pxref{History Interaction}).
7644 @item --enable-brace-expansion
7645 Include @code{csh}-like brace expansion
7646 ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ).
7647 See @ref{Brace Expansion}, for a complete description.
7649 @item --enable-casemod-attributes
7650 Include support for case-modifying attributes in the @code{declare} builtin
7651 and assignment statements. Variables with the @var{uppercase} attribute,
7652 for example, will have their values converted to uppercase upon assignment.
7654 @item --enable-casemod-expansion
7655 Include support for case-modifying word expansions.
7657 @item --enable-command-timing
7658 Include support for recognizing @code{time} as a reserved word and for
7659 displaying timing statistics for the pipeline following @code{time}
7660 (@pxref{Pipelines}).
7661 This allows pipelines as well as shell builtins and functions to be timed.
7663 @item --enable-cond-command
7664 Include support for the @code{[[} conditional command.
7665 (@pxref{Conditional Constructs}).
7667 @item --enable-cond-regexp
7668 Include support for matching @sc{posix} regular expressions using the
7669 @samp{=~} binary operator in the @code{[[} conditional command.
7670 (@pxref{Conditional Constructs}).
7672 @item --enable-coprocesses
7673 Include support for coprocesses and the @code{coproc} reserved word
7674 (@pxref{Pipelines}).
7676 @item --enable-debugger
7677 Include support for the bash debugger (distributed separately).
7679 @item --enable-directory-stack
7680 Include support for a @code{csh}-like directory stack and the
7681 @code{pushd}, @code{popd}, and @code{dirs} builtins
7682 (@pxref{The Directory Stack}).
7684 @item --enable-disabled-builtins
7685 Allow builtin commands to be invoked via @samp{builtin xxx}
7686 even after @code{xxx} has been disabled using @samp{enable -n xxx}.
7687 See @ref{Bash Builtins}, for details of the @code{builtin} and
7688 @code{enable} builtin commands.
7690 @item --enable-dparen-arithmetic
7691 Include support for the @code{((@dots{}))} command
7692 (@pxref{Conditional Constructs}).
7694 @item --enable-extended-glob
7695 Include support for the extended pattern matching features described
7696 above under @ref{Pattern Matching}.
7698 @item --enable-extended-glob-default
7699 Set the default value of the @var{extglob} shell option described
7700 above under @ref{The Shopt Builtin} to be enabled.
7702 @item --enable-help-builtin
7703 Include the @code{help} builtin, which displays help on shell builtins and
7704 variables (@pxref{Bash Builtins}).
7706 @item --enable-history
7707 Include command history and the @code{fc} and @code{history}
7708 builtin commands (@pxref{Bash History Facilities}).
7710 @item --enable-job-control
7711 This enables the job control features (@pxref{Job Control}),
7712 if the operating system supports them.
7714 @item --enable-multibyte
7715 This enables support for multibyte characters if the operating
7716 system provides the necessary support.
7718 @item --enable-net-redirections
7719 This enables the special handling of filenames of the form
7720 @code{/dev/tcp/@var{host}/@var{port}} and
7721 @code{/dev/udp/@var{host}/@var{port}}
7722 when used in redirections (@pxref{Redirections}).
7724 @item --enable-process-substitution
7725 This enables process substitution (@pxref{Process Substitution}) if
7726 the operating system provides the necessary support.
7728 @item --enable-progcomp
7729 Enable the programmable completion facilities
7730 (@pxref{Programmable Completion}).
7731 If Readline is not enabled, this option has no effect.
7733 @item --enable-prompt-string-decoding
7734 Turn on the interpretation of a number of backslash-escaped characters
7735 in the @env{$PS1}, @env{$PS2}, @env{$PS3}, and @env{$PS4} prompt
7736 strings. See @ref{Controlling the Prompt}, for a complete list of prompt
7737 string escape sequences.
7739 @item --enable-readline
7740 Include support for command-line editing and history with the Bash
7741 version of the Readline library (@pxref{Command Line Editing}).
7743 @item --enable-restricted
7744 Include support for a @dfn{restricted shell}. If this is enabled, Bash,
7745 when called as @code{rbash}, enters a restricted mode. See
7746 @ref{The Restricted Shell}, for a description of restricted mode.
7748 @item --enable-select
7749 Include the @code{select} compound command, which allows the generation of
7750 simple menus (@pxref{Conditional Constructs}).
7752 @item --enable-separate-helpfiles
7753 Use external files for the documentation displayed by the @code{help} builtin
7754 instead of storing the text internally.
7756 @item --enable-single-help-strings
7757 Store the text displayed by the @code{help} builtin as a single string for
7758 each help topic. This aids in translating the text to different languages.
7759 You may need to disable this if your compiler cannot handle very long string
7762 @item --enable-strict-posix-default
7763 Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}).
7765 @item --enable-usg-echo-default
7766 A synonym for @code{--enable-xpg-echo-default}.
7768 @item --enable-xpg-echo-default
7769 Make the @code{echo} builtin expand backslash-escaped characters by default,
7770 without requiring the @option{-e} option.
7771 This sets the default value of the @code{xpg_echo} shell option to @code{on},
7772 which makes the Bash @code{echo} behave more like the version specified in
7773 the Single Unix Specification, version 3.
7774 @xref{Bash Builtins}, for a description of the escape sequences that
7775 @code{echo} recognizes.
7778 The file @file{config-top.h} contains C Preprocessor
7779 @samp{#define} statements for options which are not settable from
7781 Some of these are not meant to be changed; beware of the consequences if
7783 Read the comments associated with each definition for more
7784 information about its effect.
7786 @node Reporting Bugs
7787 @appendix Reporting Bugs
7789 Please report all bugs you find in Bash.
7790 But first, you should
7791 make sure that it really is a bug, and that it appears in the latest
7793 The latest version of Bash is always available for FTP from
7794 @uref{ftp://ftp.gnu.org/pub/gnu/bash/}.
7796 Once you have determined that a bug actually exists, use the
7797 @code{bashbug} command to submit a bug report.
7798 If you have a fix, you are encouraged to mail that as well!
7799 Suggestions and `philosophical' bug reports may be mailed
7800 to @email{bug-bash@@gnu.org} or posted to the Usenet
7801 newsgroup @code{gnu.bash.bug}.
7803 All bug reports should include:
7806 The version number of Bash.
7808 The hardware and operating system.
7810 The compiler used to compile Bash.
7812 A description of the bug behaviour.
7814 A short script or `recipe' which exercises the bug and may be used
7819 @code{bashbug} inserts the first three items automatically into
7820 the template it provides for filing a bug report.
7822 Please send all reports concerning this manual to
7823 @email{chet.ramey@@case.edu}.
7825 @node Major Differences From The Bourne Shell
7826 @appendix Major Differences From The Bourne Shell
7828 Bash implements essentially the same grammar, parameter and
7829 variable expansion, redirection, and quoting as the Bourne Shell.
7830 Bash uses the @sc{posix} standard as the specification of
7831 how these features are to be implemented. There are some
7832 differences between the traditional Bourne shell and Bash; this
7833 section quickly details the differences of significance. A
7834 number of these differences are explained in greater depth in
7836 This section uses the version of @code{sh} included in SVR4.2 (the
7837 last version of the historical Bourne shell) as the baseline reference.
7842 Bash is @sc{posix}-conformant, even where the @sc{posix} specification
7843 differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}).
7846 Bash has multi-character invocation options (@pxref{Invoking Bash}).
7849 Bash has command-line editing (@pxref{Command Line Editing}) and
7850 the @code{bind} builtin.
7853 Bash provides a programmable word completion mechanism
7854 (@pxref{Programmable Completion}), and builtin commands
7855 @code{complete}, @code{compgen}, and @code{compopt}, to
7859 Bash has command history (@pxref{Bash History Facilities}) and the
7860 @code{history} and @code{fc} builtins to manipulate it.
7861 The Bash history list maintains timestamp information and uses the
7862 value of the @code{HISTTIMEFORMAT} variable to display it.
7865 Bash implements @code{csh}-like history expansion
7866 (@pxref{History Interaction}).
7869 Bash has one-dimensional array variables (@pxref{Arrays}), and the
7870 appropriate variable expansions and assignment syntax to use them.
7871 Several of the Bash builtins take options to act on arrays.
7872 Bash provides a number of built-in array variables.
7875 The @code{$'@dots{}'} quoting syntax, which expands ANSI-C
7876 backslash-escaped characters in the text between the single quotes,
7877 is supported (@pxref{ANSI-C Quoting}).
7880 Bash supports the @code{$"@dots{}"} quoting syntax to do
7881 locale-specific translation of the characters between the double
7882 quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings}
7883 invocation options list the translatable strings found in a script
7884 (@pxref{Locale Translation}).
7887 Bash implements the @code{!} keyword to negate the return value of
7888 a pipeline (@pxref{Pipelines}).
7889 Very useful when an @code{if} statement needs to act only if a test fails.
7890 The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to
7891 return a failure status if any command fails.
7894 Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}).
7895 The display of the timing statistics may be controlled with the
7896 @env{TIMEFORMAT} variable.
7899 Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))}
7900 arithmetic for command, similar to the C language (@pxref{Looping Constructs}).
7903 Bash includes the @code{select} compound command, which allows the
7904 generation of simple menus (@pxref{Conditional Constructs}).
7907 Bash includes the @code{[[} compound command, which makes conditional
7908 testing part of the shell grammar (@pxref{Conditional Constructs}), including
7909 optional regular expression matching.
7912 Bash provides optional case-insensitive matching for the @code{case} and
7913 @code{[[} constructs.
7916 Bash includes brace expansion (@pxref{Brace Expansion}) and tilde
7917 expansion (@pxref{Tilde Expansion}).
7920 Bash implements command aliases and the @code{alias} and @code{unalias}
7921 builtins (@pxref{Aliases}).
7924 Bash provides shell arithmetic, the @code{((} compound command
7925 (@pxref{Conditional Constructs}),
7926 and arithmetic expansion (@pxref{Shell Arithmetic}).
7929 Variables present in the shell's initial environment are automatically
7930 exported to child processes. The Bourne shell does not normally do
7931 this unless the variables are explicitly marked using the @code{export}
7935 Bash supports the @samp{+=} assignment operator, which appends to the value
7936 of the variable named on the left hand side.
7939 Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%}
7940 and @samp{##} expansions to remove leading or trailing substrings from
7941 variable values (@pxref{Shell Parameter Expansion}).
7944 The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}},
7945 is supported (@pxref{Shell Parameter Expansion}).
7948 The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}},
7949 which expands to the substring of @code{var}'s value of length
7950 @var{length}, beginning at @var{offset}, is present
7951 (@pxref{Shell Parameter Expansion}).
7955 @code{$@{var/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}},
7956 which matches @var{pattern} and replaces it with @var{replacement} in
7957 the value of @code{var}, is available (@pxref{Shell Parameter Expansion}).
7960 The expansion @code{$@{!@var{prefix@}*}} expansion, which expands to
7961 the names of all shell variables whose names begin with @var{prefix},
7962 is available (@pxref{Shell Parameter Expansion}).
7965 Bash has @var{indirect} variable expansion using @code{$@{!word@}}
7966 (@pxref{Shell Parameter Expansion}).
7969 Bash can expand positional parameters beyond @code{$9} using
7970 @code{$@{@var{num}@}}.
7973 The @sc{posix} @code{$()} form of command substitution
7974 is implemented (@pxref{Command Substitution}),
7975 and preferred to the Bourne shell's @code{``} (which
7976 is also implemented for backwards compatibility).
7979 Bash has process substitution (@pxref{Process Substitution}).
7982 Bash automatically assigns variables that provide information about the
7983 current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host
7984 (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}),
7985 and the instance of Bash that is running (@env{BASH},
7986 @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables},
7990 The @env{IFS} variable is used to split only the results of expansion,
7991 not all words (@pxref{Word Splitting}).
7992 This closes a longstanding shell security hole.
7995 Bash implements the full set of @sc{posix} filename expansion operators,
7996 including @var{character classes}, @var{equivalence classes}, and
7997 @var{collating symbols} (@pxref{Filename Expansion}).
8000 Bash implements extended pattern matching features when the @code{extglob}
8001 shell option is enabled (@pxref{Pattern Matching}).
8004 It is possible to have a variable and a function with the same name;
8005 @code{sh} does not separate the two name spaces.
8008 Bash functions are permitted to have local variables using the
8009 @code{local} builtin, and thus useful recursive functions may be written
8010 (@pxref{Bash Builtins}).
8013 Variable assignments preceding commands affect only that command, even
8014 builtins and functions (@pxref{Environment}).
8015 In @code{sh}, all variable assignments
8016 preceding commands are global unless the command is executed from the
8020 Bash performs filename expansion on filenames specified as operands
8021 to input and output redirection operators (@pxref{Redirections}).
8024 Bash contains the @samp{<>} redirection operator, allowing a file to be
8025 opened for both reading and writing, and the @samp{&>} redirection
8026 operator, for directing standard output and standard error to the same
8027 file (@pxref{Redirections}).
8030 Bash includes the @samp{<<<} redirection operator, allowing a string to
8031 be used as the standard input to a command.
8034 Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}}
8035 redirection operators, which move one file descriptor to another.
8038 Bash treats a number of filenames specially when they are
8039 used in redirection operators (@pxref{Redirections}).
8042 Bash can open network connections to arbitrary machines and services
8043 with the redirection operators (@pxref{Redirections}).
8046 The @code{noclobber} option is available to avoid overwriting existing
8047 files with output redirection (@pxref{The Set Builtin}).
8048 The @samp{>|} redirection operator may be used to override @code{noclobber}.
8051 The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins})
8052 each take @option{-L} and @option{-P} options to switch between logical and
8056 Bash allows a function to override a builtin with the same name, and provides
8057 access to that builtin's functionality within the function via the
8058 @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}).
8061 The @code{command} builtin allows selective disabling of functions
8062 when command lookup is performed (@pxref{Bash Builtins}).
8065 Individual builtins may be enabled or disabled using the @code{enable}
8066 builtin (@pxref{Bash Builtins}).
8069 The Bash @code{exec} builtin takes additional options that allow users
8070 to control the contents of the environment passed to the executed
8071 command, and what the zeroth argument to the command is to be
8072 (@pxref{Bourne Shell Builtins}).
8075 Shell functions may be exported to children via the environment
8076 using @code{export -f} (@pxref{Shell Functions}).
8079 The Bash @code{export}, @code{readonly}, and @code{declare} builtins can
8080 take a @option{-f} option to act on shell functions, a @option{-p} option to
8081 display variables with various attributes set in a format that can be
8082 used as shell input, a @option{-n} option to remove various variable
8083 attributes, and @samp{name=value} arguments to set variable attributes
8084 and values simultaneously.
8087 The Bash @code{hash} builtin allows a name to be associated with
8088 an arbitrary filename, even when that filename cannot be found by
8089 searching the @env{$PATH}, using @samp{hash -p}
8090 (@pxref{Bourne Shell Builtins}).
8093 Bash includes a @code{help} builtin for quick reference to shell
8094 facilities (@pxref{Bash Builtins}).
8097 The @code{printf} builtin is available to display formatted output
8098 (@pxref{Bash Builtins}).
8101 The Bash @code{read} builtin (@pxref{Bash Builtins})
8102 will read a line ending in @samp{\} with
8103 the @option{-r} option, and will use the @env{REPLY} variable as a
8104 default if no non-option arguments are supplied.
8105 The Bash @code{read} builtin
8106 also accepts a prompt string with the @option{-p} option and will use
8107 Readline to obtain the line when given the @option{-e} option.
8108 The @code{read} builtin also has additional options to control input:
8109 the @option{-s} option will turn off echoing of input characters as
8110 they are read, the @option{-t} option will allow @code{read} to time out
8111 if input does not arrive within a specified number of seconds, the
8112 @option{-n} option will allow reading only a specified number of
8113 characters rather than a full line, and the @option{-d} option will read
8114 until a particular character rather than newline.
8117 The @code{return} builtin may be used to abort execution of scripts
8118 executed with the @code{.} or @code{source} builtins
8119 (@pxref{Bourne Shell Builtins}).
8122 Bash includes the @code{shopt} builtin, for finer control of shell
8123 optional capabilities (@pxref{The Shopt Builtin}), and allows these options
8124 to be set and unset at shell invocation (@pxref{Invoking Bash}).
8127 Bash has much more optional behavior controllable with the @code{set}
8128 builtin (@pxref{The Set Builtin}).
8131 The @samp{-x} (@option{xtrace}) option displays commands other than
8132 simple commands when performing an execution trace
8133 (@pxref{The Set Builtin}).
8136 The @code{test} builtin (@pxref{Bourne Shell Builtins})
8137 is slightly different, as it implements the @sc{posix} algorithm,
8138 which specifies the behavior based on the number of arguments.
8141 Bash includes the @code{caller} builtin, which displays the context of
8142 any active subroutine call (a shell function or a script executed with
8143 the @code{.} or @code{source} builtins). This supports the bash
8147 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
8148 @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}.
8149 Commands specified with a @code{DEBUG} trap are executed before every
8150 simple command, @code{for} command, @code{case} command,
8151 @code{select} command, every arithmetic @code{for} command, and before
8152 the first command executes in a shell function.
8153 The @code{DEBUG} trap is not inherited by shell functions unless the
8154 function has been given the @code{trace} attribute or the
8155 @code{functrace} option has been enabled using the @code{shopt} builtin.
8156 The @code{extdebug} shell option has additional effects on the
8159 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an
8160 @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}.
8161 Commands specified with an @code{ERR} trap are executed after a simple
8162 command fails, with a few exceptions.
8163 The @code{ERR} trap is not inherited by shell functions unless the
8164 @code{-o errtrace} option to the @code{set} builtin is enabled.
8166 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
8167 @code{RETURN} pseudo-signal specification, similar to
8168 @code{EXIT} and @code{DEBUG}.
8169 Commands specified with an @code{RETURN} trap are executed before
8170 execution resumes after a shell function or a shell script executed with
8171 @code{.} or @code{source} returns.
8172 The @code{RETURN} trap is not inherited by shell functions unless the
8173 function has been given the @code{trace} attribute or the
8174 @code{functrace} option has been enabled using the @code{shopt} builtin.
8177 The Bash @code{type} builtin is more extensive and gives more information
8178 about the names it finds (@pxref{Bash Builtins}).
8181 The Bash @code{umask} builtin permits a @option{-p} option to cause
8182 the output to be displayed in the form of a @code{umask} command
8183 that may be reused as input (@pxref{Bourne Shell Builtins}).
8186 Bash implements a @code{csh}-like directory stack, and provides the
8187 @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it
8188 (@pxref{The Directory Stack}).
8189 Bash also makes the directory stack visible as the value of the
8190 @env{DIRSTACK} shell variable.
8193 Bash interprets special backslash-escaped characters in the prompt
8194 strings when interactive (@pxref{Controlling the Prompt}).
8197 The Bash restricted mode is more useful (@pxref{The Restricted Shell});
8198 the SVR4.2 shell restricted mode is too limited.
8201 The @code{disown} builtin can remove a job from the internal shell
8202 job table (@pxref{Job Control Builtins}) or suppress the sending
8203 of @code{SIGHUP} to a job when the shell exits as the result of a
8207 Bash includes a number of features to support a separate debugger for
8211 The SVR4.2 shell has two privilege-related builtins
8212 (@code{mldmode} and @code{priv}) not present in Bash.
8215 Bash does not have the @code{stop} or @code{newgrp} builtins.
8218 Bash does not use the @env{SHACCT} variable or perform shell accounting.
8221 The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses
8227 More features unique to Bash may be found in @ref{Bash Features}.
8230 @appendixsec Implementation Differences From The SVR4.2 Shell
8232 Since Bash is a completely new implementation, it does not suffer from
8233 many of the limitations of the SVR4.2 shell. For instance:
8238 Bash does not fork a subshell when redirecting into or out of
8239 a shell control structure such as an @code{if} or @code{while}
8243 Bash does not allow unbalanced quotes. The SVR4.2 shell will silently
8244 insert a needed closing quote at @code{EOF} under certain circumstances.
8245 This can be the cause of some hard-to-find errors.
8248 The SVR4.2 shell uses a baroque memory management scheme based on
8249 trapping @code{SIGSEGV}. If the shell is started from a process with
8250 @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library
8251 function call), it misbehaves badly.
8254 In a questionable attempt at security, the SVR4.2 shell,
8255 when invoked without the @option{-p} option, will alter its real
8256 and effective @sc{uid} and @sc{gid} if they are less than some
8257 magic threshold value, commonly 100.
8258 This can lead to unexpected results.
8261 The SVR4.2 shell does not allow users to trap @code{SIGSEGV},
8262 @code{SIGALRM}, or @code{SIGCHLD}.
8265 The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK},
8266 @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset.
8269 The SVR4.2 shell treats @samp{^} as the undocumented equivalent of
8273 Bash allows multiple option arguments when it is invoked (@code{-x -v});
8274 the SVR4.2 shell allows only one option argument (@code{-xv}). In
8275 fact, some versions of the shell dump core if the second argument begins
8279 The SVR4.2 shell exits a script if any builtin fails; Bash exits
8280 a script only if one of the @sc{posix} special builtins fails, and
8281 only for certain failures, as enumerated in the @sc{posix} standard.
8284 The SVR4.2 shell behaves differently when invoked as @code{jsh}
8285 (it turns on job control).
8288 @node GNU Free Documentation License
8289 @appendix GNU Free Documentation License
8297 * Builtin Index:: Index of Bash builtin commands.
8298 * Reserved Word Index:: Index of Bash reserved words.
8299 * Variable Index:: Quick reference helps you find the
8301 * Function Index:: Index of bindable Readline functions.
8302 * Concept Index:: General index for concepts described in
8307 @appendixsec Index of Shell Builtin Commands
8310 @node Reserved Word Index
8311 @appendixsec Index of Shell Reserved Words
8314 @node Variable Index
8315 @appendixsec Parameter and Variable Index
8318 @node Function Index
8319 @appendixsec Function Index
8323 @appendixsec Concept Index