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-2004 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.1 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 have freedom to copy and modify
34 this GNU Manual, like GNU software. Copies published by the Free
35 Software Foundation raise funds for GNU development.''
45 * Bash: (bash). The GNU Bourne-Again SHell.
51 @title Bash Reference Manual
52 @subtitle Reference Documentation for Bash
53 @subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}.
54 @subtitle @value{UPDATED-MONTH}
55 @author Chet Ramey, Case Western Reserve University
56 @author Brian Fox, Free Software Foundation
59 @vskip 0pt plus 1filll
63 Published by the Free Software Foundation @*
64 59 Temple Place, Suite 330, @*
65 Boston, MA 02111-1307 @*
73 @node Top, Introduction, (dir), (dir)
76 This text is a brief description of the features that are present in
77 the Bash shell (version @value{VERSION}, @value{UPDATED})..
79 This is Edition @value{EDITION}, last updated @value{UPDATED},
80 of @cite{The GNU Bash Reference Manual},
81 for @code{Bash}, Version @value{VERSION}.
83 Bash contains features that appear in other popular shells, and some
84 features that only appear in Bash. Some of the shells that Bash has
85 borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell
86 (@file{ksh}), and the C-shell (@file{csh} and its successor,
87 @file{tcsh}). The following menu breaks the features up into
88 categories based upon which one of these other shells inspired the
91 This manual is meant as a brief introduction to features found in
92 Bash. The Bash manual page should be used as the definitive
93 reference on shell behavior.
96 * Introduction:: An introduction to the shell.
98 * Definitions:: Some definitions used in the rest of this
101 * Basic Shell Features:: The shell "building blocks".
103 * Shell Builtin Commands:: Commands that are a part of the shell.
105 * Shell Variables:: Variables used or set by Bash.
107 * Bash Features:: Features found only in Bash.
109 * Job Control:: A chapter describing what job control is
110 and how Bash allows you to use it.
112 * Using History Interactively:: Chapter dealing with history expansion
115 * Command Line Editing:: Chapter describing the command line
118 * Installing Bash:: How to build and install Bash on your system.
120 * Reporting Bugs:: How to report bugs in Bash.
122 * Major Differences From The Bourne Shell:: A terse list of the differences
123 between Bash and historical
126 * Copying This Manual:: Copying this manual.
128 * Builtin Index:: Index of Bash builtin commands.
130 * Reserved Word Index:: Index of Bash reserved words.
132 * Variable Index:: Quick reference helps you find the
135 * Function Index:: Index of bindable Readline functions.
137 * Concept Index:: General index for concepts described in
143 @chapter Introduction
145 * What is Bash?:: A short description of Bash.
147 * What is a shell?:: A brief introduction to shells.
151 @section What is Bash?
153 Bash is the shell, or command language interpreter,
154 for the @sc{gnu} operating system.
155 The name is an acronym for the @samp{Bourne-Again SHell},
156 a pun on Stephen Bourne, the author of the direct ancestor of
157 the current Unix shell @code{sh},
158 which appeared in the Seventh Edition Bell Labs Research version
161 Bash is largely compatible with @code{sh} and incorporates useful
162 features from the Korn shell @code{ksh} and the C shell @code{csh}.
163 It is intended to be a conformant implementation of the @sc{ieee}
164 @sc{posix} Shell and Tools specification (@sc{ieee} Working Group 1003.2).
165 It offers functional improvements over @code{sh} for both interactive and
168 While the @sc{gnu} operating system provides other shells, including
169 a version of @code{csh}, Bash is the default shell.
170 Like other @sc{gnu} software, Bash is quite portable. It currently runs
171 on nearly every version of Unix and a few other operating systems @minus{}
172 independently-supported ports exist for @sc{ms-dos}, @sc{os/2},
173 and Windows platforms.
175 @node What is a shell?
176 @section What is a shell?
178 At its base, a shell is simply a macro processor that executes
179 commands. The term macro processor means functionality where text
180 and symbols are expanded to create larger expressions.
182 A Unix shell is both a command interpreter and a programming
183 language. As a command interpreter, the shell provides the user
184 interface to the rich set of @sc{gnu} utilities. The programming
185 language features allow these utilitites to be combined.
186 Files containing commands can be created, and become
187 commands themselves. These new commands have the same status as
188 system commands in directories such as @file{/bin}, allowing users
189 or groups to establish custom environments to automate their common
192 Shells may be used interactively or non-interactively. In
193 interactive mode, they accept input typed from the keyboard.
194 When executing non-interactively, shells execute commands read
197 A shell allows execution of @sc{gnu} commands, both synchronously and
199 The shell waits for synchronous commands to complete before accepting
200 more input; asynchronous commands continue to execute in parallel
201 with the shell while it reads and executes additional commands.
202 The @dfn{redirection} constructs permit
203 fine-grained control of the input and output of those commands.
204 Moreover, the shell allows control over the contents of commands'
207 Shells also provide a small set of built-in
208 commands (@dfn{builtins}) implementing functionality impossible
209 or inconvenient to obtain via separate utilities.
210 For example, @code{cd}, @code{break}, @code{continue}, and
211 @code{exec}) cannot be implemented outside of the shell because
212 they directly manipulate the shell itself.
213 The @code{history}, @code{getopts}, @code{kill}, or @code{pwd}
214 builtins, among others, could be implemented in separate utilities,
215 but they are more convenient to use as builtin commands.
216 All of the shell builtins are described in
219 While executing commands is essential, most of the power (and
220 complexity) of shells is due to their embedded programming
221 languages. Like any high-level language, the shell provides
222 variables, flow control constructs, quoting, and functions.
224 Shells offer features geared specifically for
225 interactive use rather than to augment the programming language.
226 These interactive features include job control, command line
227 editing, command history and aliases. Each of these features is
228 described in this manual.
232 These definitions are used throughout the remainder of this manual.
238 A family of open system standards based on Unix. Bash
239 is concerned with @sc{posix} 1003.2, the Shell and Tools Standard.
242 A space or tab character.
246 A command that is implemented internally by the shell itself, rather
247 than by an executable program somewhere in the file system.
249 @item control operator
250 @cindex control operator
251 A @code{word} that performs a control function. It is a @code{newline}
252 or one of the following:
253 @samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;},
254 @samp{|}, @samp{(}, or @samp{)}.
258 The value returned by a command to its caller. The value is restricted
259 to eight bits, so the maximum value is 255.
263 A unit of text that is the result of one of the shell expansions. After
264 expansion, when executing a command, the resulting fields are used as
265 the command name and arguments.
269 A string of characters used to identify a file.
273 A set of processes comprising a pipeline, and any processes descended
274 from it, that are all in the same process group.
278 A mechanism by which users can selectively stop (suspend) and restart
279 (resume) execution of processes.
282 @cindex metacharacter
283 A character that, when unquoted, separates words. A metacharacter is
284 a @code{blank} or one of the following characters:
285 @samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or
291 A @code{word} consisting solely of letters, numbers, and underscores,
292 and beginning with a letter or underscore. @code{Name}s are used as
293 shell variable and function names.
294 Also referred to as an @code{identifier}.
297 @cindex operator, shell
298 A @code{control operator} or a @code{redirection operator}.
299 @xref{Redirections}, for a list of redirection operators.
302 @cindex process group
303 A collection of related processes each having the same process
306 @item process group ID
307 @cindex process group ID
308 A unique identifer that represents a @code{process group}
312 @cindex reserved word
313 A @code{word} that has a special meaning to the shell. Most reserved
314 words introduce shell flow control constructs, such as @code{for} and
318 @cindex return status
319 A synonym for @code{exit status}.
323 A mechanism by which a process may be notified by the kernel
324 of an event occurring in the system.
326 @item special builtin
327 @cindex special builtin
328 A shell builtin command that has been classified as special by the
329 @sc{posix} 1003.2 standard.
333 A sequence of characters considered a single unit by the shell. It is
334 either a @code{word} or an @code{operator}.
338 A @code{token} that is not an @code{operator}.
341 @node Basic Shell Features
342 @chapter Basic Shell Features
345 Bash is an acronym for @samp{Bourne-Again SHell}.
347 the traditional Unix shell originally written by Stephen Bourne.
348 All of the Bourne shell builtin commands are available in Bash,
349 The rules for evaluation and quoting are taken from the @sc{posix}
350 specification for the `standard' Unix shell.
352 This chapter briefly summarizes the shell's `building blocks':
353 commands, control structures, shell functions, shell @i{parameters},
355 @i{redirections}, which are a way to direct input and output from
356 and to named files, and how the shell executes commands.
359 * Shell Syntax:: What your input means to the shell.
360 * Shell Commands:: The types of commands you can use.
361 * Shell Functions:: Grouping commands by name.
362 * Shell Parameters:: How the shell stores values.
363 * Shell Expansions:: How Bash expands parameters and the various
364 expansions available.
365 * Redirections:: A way to control where input and output go.
366 * Executing Commands:: What happens when you run a command.
367 * Shell Scripts:: Executing files of shell commands.
371 @section Shell Syntax
373 * Shell Operation:: The basic operation of the shell.
375 * Quoting:: How to remove the special meaning from characters.
377 * Comments:: How to specify comments.
380 When the shell reads input, it proceeds through a
381 sequence of operations. If the input indicates the beginning of a
382 comment, the shell ignores the comment symbol (@samp{#}), and the rest
385 Otherwise, roughly speaking, the shell reads its input and
386 divides the input into words and operators, employing the quoting rules
387 to select which meanings to assign various words and characters.
389 The shell then parses these tokens into commands and other constructs,
390 removes the special meaning of certain words or characters, expands
391 others, redirects input and output as needed, executes the specified
392 command, waits for the command's exit status, and makes that exit status
393 available for further inspection or processing.
395 @node Shell Operation
396 @subsection Shell Operation
398 The following is a brief description of the shell's operation when it
399 reads and executes a command. Basically, the shell does the
404 Reads its input from a file (@pxref{Shell Scripts}), from a string
405 supplied as an argument to the @option{-c} invocation option
406 (@pxref{Invoking Bash}), or from the user's terminal.
409 Breaks the input into words and operators, obeying the quoting rules
410 described in @ref{Quoting}. These tokens are separated by
411 @code{metacharacters}. Alias expansion is performed by this step
415 Parses the tokens into simple and compound commands
416 (@pxref{Shell Commands}).
419 Performs the various shell expansions (@pxref{Shell Expansions}), breaking
420 the expanded tokens into lists of filenames (@pxref{Filename Expansion})
421 and commands and arguments.
424 Performs any necessary redirections (@pxref{Redirections}) and removes
425 the redirection operators and their operands from the argument list.
428 Executes the command (@pxref{Executing Commands}).
431 Optionally waits for the command to complete and collects its exit
432 status (@pxref{Exit Status}).
440 * Escape Character:: How to remove the special meaning from a single
442 * Single Quotes:: How to inhibit all interpretation of a sequence
444 * Double Quotes:: How to suppress most of the interpretation of a
445 sequence of characters.
446 * ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings.
448 * Locale Translation:: How to translate strings into different languages.
451 Quoting is used to remove the special meaning of certain
452 characters or words to the shell. Quoting can be used to
453 disable special treatment for special characters, to prevent
454 reserved words from being recognized as such, and to prevent
457 Each of the shell metacharacters (@pxref{Definitions})
458 has special meaning to the shell and must be quoted if it is to
460 When the command history expansion facilities are being used, the
461 @var{history expansion} character, usually @samp{!}, must be quoted
462 to prevent history expansion. @xref{Bash History Facilities}, for
463 more details concerning history expansion.
465 There are three quoting mechanisms: the
466 @var{escape character}, single quotes, and double quotes.
468 @node Escape Character
469 @subsubsection Escape Character
470 A non-quoted backslash @samp{\} is the Bash escape character.
471 It preserves the literal value of the next character that follows,
472 with the exception of @code{newline}. If a @code{\newline} pair
473 appears, and the backslash itself is not quoted, the @code{\newline}
474 is treated as a line continuation (that is, it is removed from
475 the input stream and effectively ignored).
478 @subsubsection Single Quotes
480 Enclosing characters in single quotes (@samp{'}) preserves the literal value
481 of each character within the quotes. A single quote may not occur
482 between single quotes, even when preceded by a backslash.
485 @subsubsection Double Quotes
487 Enclosing characters in double quotes (@samp{"}) preserves the literal value
488 of all characters within the quotes, with the exception of
489 @samp{$}, @samp{`}, and @samp{\}.
490 The characters @samp{$} and @samp{`}
491 retain their special meaning within double quotes (@pxref{Shell Expansions}).
492 The backslash retains its special meaning only when followed by one of
493 the following characters:
494 @samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}.
495 Within double quotes, backslashes that are followed by one of these
496 characters are removed. Backslashes preceding characters without a
497 special meaning are left unmodified.
498 A double quote may be quoted within double quotes by preceding it with
500 When command history is being used, the double quote may not be used to
501 quote the history expansion character.
503 The special parameters @samp{*} and @samp{@@} have special meaning
504 when in double quotes (@pxref{Shell Parameter Expansion}).
507 @subsubsection ANSI-C Quoting
508 @cindex quoting, ANSI
510 Words of the form @code{$'@var{string}'} are treated specially. The
511 word expands to @var{string}, with backslash-escaped characters replaced
512 as specified by the ANSI C standard. Backslash escape sequences, if
513 present, are decoded as follows:
521 an escape character (not ANSI C)
537 the eight-bit character whose value is the octal value @var{nnn}
538 (one to three digits)
540 the eight-bit character whose value is the hexadecimal value @var{HH}
541 (one or two hex digits)
543 a control-@var{x} character
547 The expanded result is single-quoted, as if the dollar sign had not
550 @node Locale Translation
551 @subsubsection Locale-Specific Translation
553 @cindex internationalization
554 @cindex native languages
555 @cindex translation, native languages
557 A double-quoted string preceded by a dollar sign (@samp{$}) will cause
558 the string to be translated according to the current locale.
559 If the current locale is @code{C} or @code{POSIX}, the dollar sign
561 If the string is translated and replaced, the replacement is
566 @vindex TEXTDOMAINDIR
567 Some systems use the message catalog selected by the @env{LC_MESSAGES}
568 shell variable. Others create the name of the message catalog from the
569 value of the @env{TEXTDOMAIN} shell variable, possibly adding a
570 suffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, you
571 may need to set the @env{TEXTDOMAINDIR} variable to the location of
572 the message catalog files. Still others use both variables in this
574 @env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo.
578 @cindex comments, shell
580 In a non-interactive shell, or an interactive shell in which the
581 @code{interactive_comments} option to the @code{shopt}
582 builtin is enabled (@pxref{Bash Builtins}),
583 a word beginning with @samp{#}
584 causes that word and all remaining characters on that line to
585 be ignored. An interactive shell without the @code{interactive_comments}
586 option enabled does not allow comments. The @code{interactive_comments}
587 option is on by default in interactive shells.
588 @xref{Interactive Shells}, for a description of what makes
592 @section Shell Commands
593 @cindex commands, shell
595 A simple shell command such as @code{echo a b c} consists of the command
596 itself followed by arguments, separated by spaces.
598 More complex shell commands are composed of simple commands arranged together
599 in a variety of ways: in a pipeline in which the output of one command
600 becomes the input of a second, in a loop or conditional construct, or in
604 * Simple Commands:: The most common type of command.
605 * Pipelines:: Connecting the input and output of several
607 * Lists:: How to execute commands sequentially.
608 * Compound Commands:: Shell commands for control flow.
611 @node Simple Commands
612 @subsection Simple Commands
613 @cindex commands, simple
615 A simple command is the kind of command encountered most often.
616 It's just a sequence of words separated by @code{blank}s, terminated
617 by one of the shell's control operators (@pxref{Definitions}). The
618 first word generally specifies a command to be executed, with the
619 rest of the words being that command's arguments.
621 The return status (@pxref{Exit Status}) of a simple command is
622 its exit status as provided
623 by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
624 the command was terminated by signal @var{n}.
627 @subsection Pipelines
629 @cindex commands, pipelines
631 A @code{pipeline} is a sequence of simple commands separated by
636 @cindex command timing
637 The format for a pipeline is
639 [@code{time} [@code{-p}]] [@code{!}] @var{command1} [@code{|} @var{command2} @dots{}]
643 The output of each command in the pipeline is connected via a pipe
644 to the input of the next command.
645 That is, each command reads the previous command's output.
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 The @env{TIMEFORMAT} variable may be set to a format string that
654 specifies how the timing information should be displayed.
655 @xref{Bash Variables}, for a description of the available formats.
656 The use of @code{time} as a reserved word permits the timing of
657 shell builtins, shell functions, and pipelines. An external
658 @code{time} command cannot time these easily.
660 If the pipeline is not executed asynchronously (@pxref{Lists}), the
661 shell waits for all commands in the pipeline to complete.
663 Each command in a pipeline is executed in its own subshell
664 (@pxref{Command Execution Environment}). The exit
665 status of a pipeline is the exit status of the last command in the
666 pipeline, unless the @code{pipefail} option is enabled
667 (@pxref{The Set Builtin}).
668 If @code{pipefail} is enabled, the pipeline's return status is the
669 value of the last (rightmost) command to exit with a non-zero status,
670 or zero if all commands exit successfully.
671 If the reserved word @samp{!} precedes the pipeline, the
672 exit status is the logical negation of the exit status as described
674 The shell waits for all commands in the pipeline to terminate before
678 @subsection Lists of Commands
679 @cindex commands, lists
681 A @code{list} is a sequence of one or more pipelines separated by one
682 of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
683 and optionally terminated by one of @samp{;}, @samp{&}, or a
686 Of these list operators, @samp{&&} and @samp{||}
687 have equal precedence, followed by @samp{;} and @samp{&},
688 which have equal precedence.
690 A sequence of one or more newlines may appear in a @code{list}
691 to delimit commands, equivalent to a semicolon.
693 If a command is terminated by the control operator @samp{&},
694 the shell executes the command asynchronously in a subshell.
695 This is known as executing the command in the @var{background}.
696 The shell does not wait for the command to finish, and the return
698 When job control is not active (@pxref{Job Control}),
699 the standard input for asynchronous commands, in the absence of any
700 explicit redirections, is redirected from @code{/dev/null}.
702 Commands separated by a @samp{;} are executed sequentially; the shell
703 waits for each command to terminate in turn. The return status is the
704 exit status of the last command executed.
706 The control operators @samp{&&} and @samp{||}
707 denote @sc{and} lists and @sc{or} lists, respectively.
708 An @sc{and} list has the form
710 @var{command1} && @var{command2}
714 @var{command2} is executed if, and only if, @var{command1}
715 returns an exit status of zero.
717 An @sc{or} list has the form
719 @var{command1} || @var{command2}
723 @var{command2} is executed if, and only if, @var{command1}
724 returns a non-zero exit status.
727 @sc{and} and @sc{or} lists is the exit status of the last command
728 executed in the list.
730 @node Compound Commands
731 @subsection Compound Commands
732 @cindex commands, compound
735 * Looping Constructs:: Shell commands for iterative action.
736 * Conditional Constructs:: Shell commands for conditional execution.
737 * Command Grouping:: Ways to group commands.
740 Compound commands are the shell programming constructs.
741 Each construct begins with a reserved word or control operator and is
742 terminated by a corresponding reserved word or operator.
743 Any redirections (@pxref{Redirections}) associated with a compound command
744 apply to all commands within that compound command unless explicitly overridden.
746 Bash provides looping constructs, conditional commands, and mechanisms
747 to group commands and execute them as a unit.
749 @node Looping Constructs
750 @subsubsection Looping Constructs
751 @cindex commands, looping
753 Bash supports the following looping constructs.
755 Note that wherever a @samp{;} appears in the description of a
756 command's syntax, it may be replaced with one or more newlines.
763 The syntax of the @code{until} command is:
765 until @var{test-commands}; do @var{consequent-commands}; done
767 Execute @var{consequent-commands} as long as
768 @var{test-commands} has an exit status which is not zero.
769 The return status is the exit status of the last command executed
770 in @var{consequent-commands}, or zero if none was executed.
774 The syntax of the @code{while} command is:
776 while @var{test-commands}; do @var{consequent-commands}; done
779 Execute @var{consequent-commands} as long as
780 @var{test-commands} has an exit status of zero.
781 The return status is the exit status of the last command executed
782 in @var{consequent-commands}, or zero if none was executed.
786 The syntax of the @code{for} command is:
789 for @var{name} [in @var{words} @dots{}]; do @var{commands}; done
791 Expand @var{words}, and execute @var{commands} once for each member
792 in the resultant list, with @var{name} bound to the current member.
793 If @samp{in @var{words}} is not present, the @code{for} command
794 executes the @var{commands} once for each positional parameter that is
795 set, as if @samp{in "$@@"} had been specified
796 (@pxref{Special Parameters}).
797 The return status is the exit status of the last command that executes.
798 If there are no items in the expansion of @var{words}, no commands are
799 executed, and the return status is zero.
801 An alternate form of the @code{for} command is also supported:
804 for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
806 First, the arithmetic expression @var{expr1} is evaluated according
807 to the rules described below (@pxref{Shell Arithmetic}).
808 The arithmetic expression @var{expr2} is then evaluated repeatedly
809 until it evaluates to zero.
810 Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
811 executed and the arithmetic expression @var{expr3} is evaluated.
812 If any expression is omitted, it behaves as if it evaluates to 1.
813 The return value is the exit status of the last command in @var{list}
814 that is executed, or false if any of the expressions is invalid.
818 The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
819 may be used to control loop execution.
821 @node Conditional Constructs
822 @subsubsection Conditional Constructs
823 @cindex commands, conditional
832 The syntax of the @code{if} command is:
835 if @var{test-commands}; then
836 @var{consequent-commands};
837 [elif @var{more-test-commands}; then
838 @var{more-consequents};]
839 [else @var{alternate-consequents};]
843 The @var{test-commands} list is executed, and if its return status is zero,
844 the @var{consequent-commands} list is executed.
845 If @var{test-commands} returns a non-zero status, each @code{elif} list
846 is executed in turn, and if its exit status is zero,
847 the corresponding @var{more-consequents} is executed and the
849 If @samp{else @var{alternate-consequents}} is present, and
850 the final command in the final @code{if} or @code{elif} clause
851 has a non-zero exit status, then @var{alternate-consequents} is executed.
852 The return status is the exit status of the last command executed, or
853 zero if no condition tested true.
859 The syntax of the @code{case} command is:
862 @code{case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac}
865 @code{case} will selectively execute the @var{command-list} corresponding to
866 the first @var{pattern} that matches @var{word}.
867 The @samp{|} is used to separate multiple patterns, and the @samp{)}
868 operator terminates a pattern list.
869 A list of patterns and an associated command-list is known
870 as a @var{clause}. Each clause must be terminated with @samp{;;}.
871 The @var{word} undergoes tilde expansion, parameter expansion, command
872 substitution, arithmetic expansion, and quote removal before matching is
873 attempted. Each @var{pattern} undergoes tilde expansion, parameter
874 expansion, command substitution, and arithmetic expansion.
876 There may be an arbitrary number of @code{case} clauses, each terminated
877 by a @samp{;;}. The first pattern that matches determines the
878 command-list that is executed.
880 Here is an example using @code{case} in a script that could be used to
881 describe one interesting feature of an animal:
884 echo -n "Enter the name of an animal: "
886 echo -n "The $ANIMAL has "
888 horse | dog | cat) echo -n "four";;
889 man | kangaroo ) echo -n "two";;
890 *) echo -n "an unknown number of";;
896 The return status is zero if no @var{pattern} is matched. Otherwise, the
897 return status is the exit status of the @var{command-list} executed.
902 The @code{select} construct allows the easy generation of menus.
903 It has almost the same syntax as the @code{for} command:
906 select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
909 The list of words following @code{in} is expanded, generating a list
910 of items. The set of expanded words is printed on the standard
911 error output stream, each preceded by a number. If the
912 @samp{in @var{words}} is omitted, the positional parameters are printed,
913 as if @samp{in "$@@"} had been specifed.
914 The @env{PS3} prompt is then displayed and a line is read from the
916 If the line consists of a number corresponding to one of the displayed
917 words, then the value of @var{name} is set to that word.
918 If the line is empty, the words and prompt are displayed again.
919 If @code{EOF} is read, the @code{select} command completes.
920 Any other value read causes @var{name} to be set to null.
921 The line read is saved in the variable @env{REPLY}.
923 The @var{commands} are executed after each selection until a
924 @code{break} command is executed, at which
925 point the @code{select} command completes.
927 Here is an example that allows the user to pick a filename from the
928 current directory, and displays the name and index of the file
934 echo you picked $fname \($REPLY\)
941 (( @var{expression} ))
944 The arithmetic @var{expression} is evaluated according to the rules
945 described below (@pxref{Shell Arithmetic}).
946 If the value of the expression is non-zero, the return status is 0;
947 otherwise the return status is 1. This is exactly equivalent to
949 let "@var{expression}"
952 @xref{Bash Builtins}, for a full description of the @code{let} builtin.
958 [[ @var{expression} ]]
961 Return a status of 0 or 1 depending on the evaluation of
962 the conditional expression @var{expression}.
963 Expressions are composed of the primaries described below in
964 @ref{Bash Conditional Expressions}.
965 Word splitting and filename expansion are not performed on the words
966 between the @samp{[[} and @samp{]]}; tilde expansion, parameter and
967 variable expansion, arithmetic expansion, command substitution, process
968 substitution, and quote removal are performed.
969 Conditional operators such as @samp{-f} must be unquoted to be recognized
972 When the @samp{==} and @samp{!=} operators are used, the string to the
973 right of the operator is considered a pattern and matched according
974 to the rules described below in @ref{Pattern Matching}.
975 The return value is 0 if the string matches or does not match
976 the pattern, respectively, and 1 otherwise.
977 Any part of the pattern may be quoted to force it to be matched as a
980 An additional binary operator, @samp{=~}, is available, with the same
981 precedence as @samp{==} and @samp{!=}.
982 When it is used, the string to the right of the operator is considered
983 an extended regular expression and matched accordingly (as in @i{regex}3)).
984 The return value is 0 if the string matches
985 the pattern, and 1 otherwise.
986 If the regular expression is syntactically incorrect, the conditional
987 expression's return value is 2.
988 If the shell option @code{nocaseglob}
989 (see the description of @code{shopt} in @ref{Bash Builtins})
990 is enabled, the match is performed without regard to the case
991 of alphabetic characters.
992 Substrings matched by parenthesized subexpressions within the regular
993 expression are saved in the array variable @code{BASH_REMATCH}.
994 The element of @code{BASH_REMATCH} with index 0 is the portion of the string
995 matching the entire regular expression.
996 The element of @code{BASH_REMATCH} with index @var{n} is the portion of the
997 string matching the @var{n}th parenthesized subexpression.
999 Expressions may be combined using the following operators, listed
1000 in decreasing order of precedence:
1003 @item ( @var{expression} )
1004 Returns the value of @var{expression}.
1005 This may be used to override the normal precedence of operators.
1007 @item ! @var{expression}
1008 True if @var{expression} is false.
1010 @item @var{expression1} && @var{expression2}
1011 True if both @var{expression1} and @var{expression2} are true.
1013 @item @var{expression1} || @var{expression2}
1014 True if either @var{expression1} or @var{expression2} is true.
1017 The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the
1018 value of @var{expression1} is sufficient to determine the return
1019 value of the entire conditional expression.
1023 @node Command Grouping
1024 @subsubsection Grouping Commands
1025 @cindex commands, grouping
1027 Bash provides two ways to group a list of commands to be executed
1028 as a unit. When commands are grouped, redirections may be applied
1029 to the entire command list. For example, the output of all the
1030 commands in the list may be redirected to a single stream.
1038 Placing a list of commands between parentheses causes a subshell
1039 environment to be created (@pxref{Command Execution Environment}), and each
1040 of the commands in @var{list} to be executed in that subshell. Since the
1041 @var{list} is executed in a subshell, variable assignments do not remain in
1042 effect after the subshell completes.
1051 Placing a list of commands between curly braces causes the list to
1052 be executed in the current shell context. No subshell is created.
1053 The semicolon (or newline) following @var{list} is required.
1056 In addition to the creation of a subshell, there is a subtle difference
1057 between these two constructs due to historical reasons. The braces
1058 are @code{reserved words}, so they must be separated from the @var{list}
1059 by @code{blank}s. The parentheses are @code{operators}, and are
1060 recognized as separate tokens by the shell even if they are not separated
1061 from the @var{list} by whitespace.
1063 The exit status of both of these constructs is the exit status of
1066 @node Shell Functions
1067 @section Shell Functions
1068 @cindex shell function
1069 @cindex functions, shell
1071 Shell functions are a way to group commands for later execution
1072 using a single name for the group. They are executed just like
1073 a "regular" command.
1074 When the name of a shell function is used as a simple command name,
1075 the list of commands associated with that function name is executed.
1076 Shell functions are executed in the current
1077 shell context; no new process is created to interpret them.
1079 Functions are declared using this syntax:
1082 [ @code{function} ] @var{name} () @var{compound-command} [ @var{redirections} ]
1085 This defines a shell function named @var{name}. The reserved
1086 word @code{function} is optional.
1087 If the @code{function} reserved
1088 word is supplied, the parentheses are optional.
1089 The @var{body} of the function is the compound command
1090 @var{compound-command} (@pxref{Compound Commands}).
1091 That command is usually a @var{list} enclosed between @{ and @}, but
1092 may be any compound command listed above.
1093 @var{compound-command} is executed whenever @var{name} is specified as the
1095 Any redirections (@pxref{Redirections}) associated with the shell function
1096 are performed when the function is executed.
1098 The exit status of a function definition is zero unless a syntax error
1099 occurs or a readonly function with the same name already exists.
1100 When executed, the exit status of a function is the exit status of the
1101 last command executed in the body.
1103 Note that for historical reasons, in the most common usage the curly braces
1104 that surround the body of the function must be separated from the body by
1105 @code{blank}s or newlines.
1106 This is because the braces are reserved words and are only recognized
1107 as such when they are separated by whitespace.
1108 Also, when using the braces, the @var{list} must be terminated by a semicolon,
1109 a @samp{&}, or a newline.
1111 When a function is executed, the arguments to the
1112 function become the positional parameters
1113 during its execution (@pxref{Positional Parameters}).
1114 The special parameter @samp{#} that expands to the number of
1115 positional parameters is updated to reflect the change.
1116 Special parameter @code{0} is unchanged.
1117 The first element of the @env{FUNCNAME} variable is set to the
1118 name of the function while the function is executing.
1119 All other aspects of the shell execution
1120 environment are identical between a function and its caller
1121 with the exception that the @env{DEBUG} trap
1122 below) is not inherited unless the function has been given the
1123 @code{trace} attribute using the @code{declare} builtin or
1124 the @code{-o functrace} option has been enabled with
1125 the @code{set} builtin,
1126 (in which case all functions inherit the @code{DEBUG} trap).
1127 @xref{Bourne Shell Builtins}, for the description of the
1128 @code{trap} builtin.
1130 If the builtin command @code{return}
1131 is executed in a function, the function completes and
1132 execution resumes with the next command after the function
1134 Any command associated with the @code{RETURN} trap is executed
1135 before execution resumes.
1136 When a function completes, the values of the
1137 positional parameters and the special parameter @samp{#}
1138 are restored to the values they had prior to the function's
1139 execution. If a numeric argument is given to @code{return},
1140 that is the function's return status; otherwise the function's
1141 return status is the exit status of the last command executed
1142 before the @code{return}.
1144 Variables local to the function may be declared with the
1145 @code{local} builtin. These variables are visible only to
1146 the function and the commands it invokes.
1148 Function names and definitions may be listed with the
1149 @option{-f} option to the @code{declare} or @code{typeset}
1150 builtin commands (@pxref{Bash Builtins}).
1151 The @option{-F} option to @code{declare} or @code{typeset}
1152 will list the function names only
1153 (and optionally the source file and line number, if the @code{extdebug}
1154 shell option is enabled).
1155 Functions may be exported so that subshells
1156 automatically have them defined with the
1157 @option{-f} option to the @code{export} builtin
1158 (@pxref{Bourne Shell Builtins}).
1159 Note that shell functions and variables with the same name may result
1160 in multiple identically-named entries in the environment passed to the
1162 Care should be taken in cases where this may cause a problem.
1164 Functions may be recursive. No limit is placed on the number of
1167 @node Shell Parameters
1168 @section Shell Parameters
1170 @cindex variable, shell
1171 @cindex shell variable
1174 * Positional Parameters:: The shell's command-line arguments.
1175 * Special Parameters:: Parameters denoted by special characters.
1178 A @var{parameter} is an entity that stores values.
1179 It can be a @code{name}, a number, or one of the special characters
1181 A @var{variable} is a parameter denoted by a @code{name}.
1182 A variable has a @var{value} and zero or more @var{attributes}.
1183 Attributes are assigned using the @code{declare} builtin command
1184 (see the description of the @code{declare} builtin in @ref{Bash Builtins}).
1186 A parameter is set if it has been assigned a value. The null string is
1187 a valid value. Once a variable is set, it may be unset only by using
1188 the @code{unset} builtin command.
1190 A variable may be assigned to by a statement of the form
1192 @var{name}=[@var{value}]
1196 is not given, the variable is assigned the null string. All
1197 @var{value}s undergo tilde expansion, parameter and variable expansion,
1198 command substitution, arithmetic expansion, and quote
1199 removal (detailed below). If the variable has its @code{integer}
1200 attribute set, then @var{value}
1201 is evaluated as an arithmetic expression even if the @code{$((@dots{}))}
1202 expansion is not used (@pxref{Arithmetic Expansion}).
1203 Word splitting is not performed, with the exception
1204 of @code{"$@@"} as explained below.
1205 Filename expansion is not performed.
1206 Assignment statements may also appear as arguments to the
1208 @code{declare}, @code{typeset}, @code{export}, @code{readonly},
1209 and @code{local} builtin commands.
1211 In the context where an assignment statement is assigning a value
1212 to a shell variable or array index (@pxref{Arrays}), the @samp{+=}
1213 operator can be used to
1214 append to or add to the variable's previous value.
1215 When @samp{+=} is applied to a variable for which the integer attribute
1216 has been set, @var{value} is evaluated as an arithmetic expression and
1217 added to the variable's current value, which is also evaluated.
1218 When @samp{+=} is applied to an array variable using compound assignment
1219 (@pxref{Arrays}), the
1220 variable's value is not unset (as it is when using @samp{=}), and new
1221 values are appended to the array beginning at one greater than the array's
1223 When applied to a string-valued variable, @var{value} is expanded and
1224 appended to the variable's value.
1226 @node Positional Parameters
1227 @subsection Positional Parameters
1228 @cindex parameters, positional
1230 A @var{positional parameter} is a parameter denoted by one or more
1231 digits, other than the single digit @code{0}. Positional parameters are
1232 assigned from the shell's arguments when it is invoked,
1233 and may be reassigned using the @code{set} builtin command.
1234 Positional parameter @code{N} may be referenced as @code{$@{N@}}, or
1235 as @code{$N} when @code{N} consists of a single digit.
1236 Positional parameters may not be assigned to with assignment statements.
1237 The @code{set} and @code{shift} builtins are used to set and
1238 unset them (@pxref{Shell Builtin Commands}).
1239 The positional parameters are
1240 temporarily replaced when a shell function is executed
1241 (@pxref{Shell Functions}).
1243 When a positional parameter consisting of more than a single
1244 digit is expanded, it must be enclosed in braces.
1246 @node Special Parameters
1247 @subsection Special Parameters
1248 @cindex parameters, special
1250 The shell treats several parameters specially. These parameters may
1251 only be referenced; assignment to them is not allowed.
1256 Expands to the positional parameters, starting from one. When the
1257 expansion occurs within double quotes, it expands to a single word
1258 with the value of each parameter separated by the first character
1260 special variable. That is, @code{"$*"} is equivalent
1261 to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c}
1262 is the first character of the value of the @code{IFS}
1264 If @env{IFS} is unset, the parameters are separated by spaces.
1265 If @env{IFS} is null, the parameters are joined without intervening
1270 Expands to the positional parameters, starting from one. When the
1271 expansion occurs within double quotes, each parameter expands to a
1272 separate word. That is, @code{"$@@"} is equivalent to
1273 @code{"$1" "$2" @dots{}}.
1274 If the double-quoted expansion occurs within a word, the expansion of
1275 the first parameter is joined with the beginning part of the original
1276 word, and the expansion of the last parameter is joined with the last
1277 part of the original word.
1278 When there are no positional parameters, @code{"$@@"} and
1280 expand to nothing (i.e., they are removed).
1283 Expands to the number of positional parameters in decimal.
1286 Expands to the exit status of the most recently executed foreground
1290 (A hyphen.) Expands to the current option flags as specified upon
1291 invocation, by the @code{set}
1292 builtin command, or those set by the shell itself
1293 (such as the @option{-i} option).
1296 Expands to the process @sc{id} of the shell. In a @code{()} subshell, it
1297 expands to the process @sc{id} of the invoking shell, not the subshell.
1300 Expands to the process @sc{id} of the most recently executed background
1301 (asynchronous) command.
1304 Expands to the name of the shell or shell script. This is set at
1305 shell initialization. If Bash is invoked with a file of commands
1306 (@pxref{Shell Scripts}), @code{$0} is set to the name of that file.
1307 If Bash is started with the @option{-c} option (@pxref{Invoking Bash}),
1308 then @code{$0} is set to the first argument after the string to be
1309 executed, if one is present. Otherwise, it is set
1310 to the filename used to invoke Bash, as given by argument zero.
1314 At shell startup, set to the absolute filename of the shell or shell
1315 script being executed as passed in the argument list.
1316 Subsequently, expands to the last argument to the previous command,
1318 Also set to the full pathname of each command executed and placed in
1319 the environment exported to that command.
1320 When checking mail, this parameter holds the name of the mail file.
1323 @node Shell Expansions
1324 @section Shell Expansions
1327 Expansion is performed on the command line after it has been split into
1328 @code{token}s. There are seven kinds of expansion performed:
1330 @item brace expansion
1331 @item tilde expansion
1332 @item parameter and variable expansion
1333 @item command substitution
1334 @item arithmetic expansion
1335 @item word splitting
1336 @item filename expansion
1340 * Brace Expansion:: Expansion of expressions within braces.
1341 * Tilde Expansion:: Expansion of the ~ character.
1342 * Shell Parameter Expansion:: How Bash expands variables to their values.
1343 * Command Substitution:: Using the output of a command as an argument.
1344 * Arithmetic Expansion:: How to use arithmetic in shell expansions.
1345 * Process Substitution:: A way to write and read to and from a
1347 * Word Splitting:: How the results of expansion are split into separate
1349 * Filename Expansion:: A shorthand for specifying filenames matching patterns.
1350 * Quote Removal:: How and when quote characters are removed from
1354 The order of expansions is: brace expansion, tilde expansion,
1355 parameter, variable, and arithmetic expansion and
1356 command substitution
1357 (done in a left-to-right fashion), word splitting, and filename
1360 On systems that can support it, there is an additional expansion
1361 available: @var{process substitution}. This is performed at the
1362 same time as parameter, variable, and arithmetic expansion and
1363 command substitution.
1365 Only brace expansion, word splitting, and filename expansion
1366 can change the number of words of the expansion; other expansions
1367 expand a single word to a single word.
1368 The only exceptions to this are the expansions of
1369 @code{"$@@"} (@pxref{Special Parameters}) and @code{"$@{@var{name}[@@]@}"}
1372 After all expansions, @code{quote removal} (@pxref{Quote Removal})
1375 @node Brace Expansion
1376 @subsection Brace Expansion
1377 @cindex brace expansion
1378 @cindex expansion, brace
1380 Brace expansion is a mechanism by which arbitrary strings may be generated.
1381 This mechanism is similar to
1382 @var{filename expansion} (@pxref{Filename Expansion}),
1383 but the file names generated need not exist.
1384 Patterns to be brace expanded take the form of an optional @var{preamble},
1385 followed by either a series of comma-separated strings or a sequnce expression
1386 between a pair of braces,
1387 followed by an optional @var{postscript}.
1388 The preamble is prefixed to each string contained within the braces, and
1389 the postscript is then appended to each resulting string, expanding left
1392 Brace expansions may be nested.
1393 The results of each expanded string are not sorted; left to right order
1397 bash$ echo a@{d,c,b@}e
1401 A sequence expression takes the form @code{@{@var{x}..@var{y}@}},
1402 where @var{x} and @var{y} are either integers or single characters.
1403 When integers are supplied, the expression expands to each number between
1404 @var{x} and @var{y}, inclusive.
1405 When characters are supplied, the expression expands to each character
1406 lexicographically between @var{x} and @var{y}, inclusive. Note that
1407 both @var{x} and @var{y} must be of the same type.
1409 Brace expansion is performed before any other expansions,
1410 and any characters special to other expansions are preserved
1411 in the result. It is strictly textual. Bash
1412 does not apply any syntactic interpretation to the context of the
1413 expansion or the text between the braces.
1414 To avoid conflicts with parameter expansion, the string @samp{$@{}
1415 is not considered eligible for brace expansion.
1417 A correctly-formed brace expansion must contain unquoted opening
1418 and closing braces, and at least one unquoted comma or a valid
1419 sequence expression.
1420 Any incorrectly formed brace expansion is left unchanged.
1422 A @{ or @samp{,} may be quoted with a backslash to prevent its
1423 being considered part of a brace expression.
1424 To avoid conflicts with parameter expansion, the string @samp{$@{}
1425 is not considered eligible for brace expansion.
1427 This construct is typically used as shorthand when the common
1428 prefix of the strings to be generated is longer than in the
1431 mkdir /usr/local/src/bash/@{old,new,dist,bugs@}
1435 chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@}
1438 @node Tilde Expansion
1439 @subsection Tilde Expansion
1440 @cindex tilde expansion
1441 @cindex expansion, tilde
1443 If a word begins with an unquoted tilde character (@samp{~}), all of the
1444 characters up to the first unquoted slash (or all characters,
1445 if there is no unquoted slash) are considered a @var{tilde-prefix}.
1446 If none of the characters in the tilde-prefix are quoted, the
1447 characters in the tilde-prefix following the tilde are treated as a
1448 possible @var{login name}.
1449 If this login name is the null string, the tilde is replaced with the
1450 value of the @env{HOME} shell variable.
1451 If @env{HOME} is unset, the home directory of the user executing the
1452 shell is substituted instead.
1453 Otherwise, the tilde-prefix is replaced with the home directory
1454 associated with the specified login name.
1456 If the tilde-prefix is @samp{~+}, the value of
1457 the shell variable @env{PWD} replaces the tilde-prefix.
1458 If the tilde-prefix is @samp{~-}, the value of the shell variable
1459 @env{OLDPWD}, if it is set, is substituted.
1461 If the characters following the tilde in the tilde-prefix consist of a
1462 number @var{N}, optionally prefixed by a @samp{+} or a @samp{-},
1463 the tilde-prefix is replaced with the
1464 corresponding element from the directory stack, as it would be displayed
1465 by the @code{dirs} builtin invoked with the characters following tilde
1466 in the tilde-prefix as an argument (@pxref{The Directory Stack}).
1467 If the tilde-prefix, sans the tilde, consists of a number without a
1468 leading @samp{+} or @samp{-}, @samp{+} is assumed.
1470 If the login name is invalid, or the tilde expansion fails, the word is
1473 Each variable assignment is checked for unquoted tilde-prefixes immediately
1474 following a @samp{:} or the first @samp{=}.
1475 In these cases, tilde expansion is also performed.
1476 Consequently, one may use file names with tildes in assignments to
1477 @env{PATH}, @env{MAILPATH}, and @env{CDPATH},
1478 and the shell assigns the expanded value.
1480 The following table shows how Bash treats unquoted tilde-prefixes:
1484 The value of @code{$HOME}
1489 The subdirectory @code{foo} of the home directory of the user
1496 @file{$@{OLDPWD-'~-'@}/foo}
1499 The string that would be displayed by @samp{dirs +@var{N}}
1502 The string that would be displayed by @samp{dirs +@var{N}}
1505 The string that would be displayed by @samp{dirs -@var{N}}
1509 @node Shell Parameter Expansion
1510 @subsection Shell Parameter Expansion
1511 @cindex parameter expansion
1512 @cindex expansion, parameter
1514 The @samp{$} character introduces parameter expansion,
1515 command substitution, or arithmetic expansion. The parameter name
1516 or symbol to be expanded may be enclosed in braces, which
1517 are optional but serve to protect the variable to be expanded from
1518 characters immediately following it which could be
1519 interpreted as part of the name.
1521 When braces are used, the matching ending brace is the first @samp{@}}
1522 not escaped by a backslash or within a quoted string, and not within an
1523 embedded arithmetic expansion, command substitution, or parameter
1526 The basic form of parameter expansion is $@{@var{parameter}@}.
1527 The value of @var{parameter} is substituted. The braces are required
1528 when @var{parameter}
1529 is a positional parameter with more than one digit,
1530 or when @var{parameter}
1531 is followed by a character that is not to be
1532 interpreted as part of its name.
1534 If the first character of @var{parameter} is an exclamation point,
1535 a level of variable indirection is introduced.
1536 Bash uses the value of the variable formed from the rest of
1537 @var{parameter} as the name of the variable; this variable is then
1538 expanded and that value is used in the rest of the substitution, rather
1539 than the value of @var{parameter} itself.
1540 This is known as @code{indirect expansion}.
1541 The exceptions to this are the expansions of $@{!@var{prefix*}@}
1542 and $@{!@var{name}[@@]@}
1544 The exclamation point must immediately follow the left brace in order to
1545 introduce indirection.
1547 In each of the cases below, @var{word} is subject to tilde expansion,
1548 parameter expansion, command substitution, and arithmetic expansion.
1550 When not performing substring expansion, Bash tests for a parameter
1551 that is unset or null; omitting the colon results in a test only for a
1552 parameter that is unset. Put another way, if the colon is included,
1553 the operator tests for both existence and that the value is not null;
1554 if the colon is omitted, the operator tests only for existence.
1558 @item $@{@var{parameter}:@minus{}@var{word}@}
1559 If @var{parameter} is unset or null, the expansion of
1560 @var{word} is substituted. Otherwise, the value of
1561 @var{parameter} is substituted.
1563 @item $@{@var{parameter}:=@var{word}@}
1565 is unset or null, the expansion of @var{word}
1566 is assigned to @var{parameter}.
1567 The value of @var{parameter} is then substituted.
1568 Positional parameters and special parameters may not be assigned to
1571 @item $@{@var{parameter}:?@var{word}@}
1573 is null or unset, the expansion of @var{word} (or a message
1574 to that effect if @var{word}
1575 is not present) is written to the standard error and the shell, if it
1576 is not interactive, exits. Otherwise, the value of @var{parameter} is
1579 @item $@{@var{parameter}:+@var{word}@}
1581 is null or unset, nothing is substituted, otherwise the expansion of
1582 @var{word} is substituted.
1584 @item $@{@var{parameter}:@var{offset}@}
1585 @itemx $@{@var{parameter}:@var{offset}:@var{length}@}
1586 Expands to up to @var{length} characters of @var{parameter}
1587 starting at the character specified by @var{offset}.
1588 If @var{length} is omitted, expands to the substring of
1589 @var{parameter} starting at the character specified by @var{offset}.
1590 @var{length} and @var{offset} are arithmetic expressions
1591 (@pxref{Shell Arithmetic}).
1592 This is referred to as Substring Expansion.
1594 @var{length} must evaluate to a number greater than or equal to zero.
1595 If @var{offset} evaluates to a number less than zero, the value
1596 is used as an offset from the end of the value of @var{parameter}.
1597 If @var{parameter} is @samp{@@}, the result is @var{length} positional
1598 parameters beginning at @var{offset}.
1599 If @var{parameter} is an array name indexed by @samp{@@} or @samp{*},
1600 the result is the @var{length}
1601 members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}.
1602 A negative @var{offset} is taken relative to one greater than the maximum
1603 index of the specified array.
1604 Note that a negative offset must be separated from the colon by at least
1605 one space to avoid being confused with the @samp{:-} expansion.
1606 Substring indexing is zero-based unless the positional parameters
1607 are used, in which case the indexing starts at 1.
1609 @item $@{!@var{prefix}*@}
1610 @itemx $@{!@var{prefix}@@@}
1611 Expands to the names of variables whose names begin with @var{prefix},
1612 separated by the first character of the @env{IFS} special variable.
1614 @item $@{!@var{name}[@@]@}
1615 @itemx $@{!@var{name}[*]@}
1616 If @var{name} is an array variable, expands to the list of array indices
1617 (keys) assigned in @var{name}.
1618 If @var{name} is not an array, expands to 0 if @var{name} is set and null
1620 When @samp{@@} is used and the expansion appears within double quotes, each
1621 key expands to a separate word.
1623 @item $@{#@var{parameter}@}
1624 The length in characters of the expanded value of @var{parameter} is
1626 If @var{parameter} is @samp{*} or @samp{@@}, the value substituted
1627 is the number of positional parameters.
1628 If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@},
1629 the value substituted is the number of elements in the array.
1631 @item $@{@var{parameter}#@var{word}@}
1632 @itemx $@{@var{parameter}##@var{word}@}
1634 is expanded to produce a pattern just as in filename
1635 expansion (@pxref{Filename Expansion}). If the pattern matches
1636 the beginning of the expanded value of @var{parameter},
1637 then the result of the expansion is the expanded value of @var{parameter}
1638 with the shortest matching pattern (the @samp{#} case) or the
1639 longest matching pattern (the @samp{##} case) deleted.
1640 If @var{parameter} is @samp{@@} or @samp{*},
1641 the pattern removal operation is applied to each positional
1642 parameter in turn, and the expansion is the resultant list.
1643 If @var{parameter} is an array variable subscripted with
1644 @samp{@@} or @samp{*},
1645 the pattern removal operation is applied to each member of the
1646 array in turn, and the expansion is the resultant list.
1648 @item $@{@var{parameter}%@var{word}@}
1649 @itemx $@{@var{parameter}%%@var{word}@}
1650 The @var{word} is expanded to produce a pattern just as in
1652 If the pattern matches a trailing portion of the expanded value of
1653 @var{parameter}, then the result of the expansion is the value of
1654 @var{parameter} with the shortest matching pattern (the @samp{%} case)
1655 or the longest matching pattern (the @samp{%%} case) deleted.
1656 If @var{parameter} is @samp{@@} or @samp{*},
1657 the pattern removal operation is applied to each positional
1658 parameter in turn, and the expansion is the resultant list.
1660 is an array variable subscripted with @samp{@@} or @samp{*},
1661 the pattern removal operation is applied to each member of the
1662 array in turn, and the expansion is the resultant list.
1664 @item $@{@var{parameter}/@var{pattern}/@var{string}@}
1665 @itemx $@{@var{parameter}//@var{pattern}/@var{string}@}
1667 The @var{pattern} is expanded to produce a pattern just as in
1669 @var{Parameter} is expanded and the longest match of @var{pattern}
1670 against its value is replaced with @var{string}.
1671 In the first form, only the first match is replaced.
1672 The second form causes all matches of @var{pattern} to be
1673 replaced with @var{string}.
1674 If @var{pattern} begins with @samp{#}, it must match at the beginning
1675 of the expanded value of @var{parameter}.
1676 If @var{pattern} begins with @samp{%}, it must match at the end
1677 of the expanded value of @var{parameter}.
1678 If @var{string} is null, matches of @var{pattern} are deleted
1679 and the @code{/} following @var{pattern} may be omitted.
1680 If @var{parameter} is @samp{@@} or @samp{*},
1681 the substitution operation is applied to each positional
1682 parameter in turn, and the expansion is the resultant list.
1684 is an array variable subscripted with @samp{@@} or @samp{*},
1685 the substitution operation is applied to each member of the
1686 array in turn, and the expansion is the resultant list.
1690 @node Command Substitution
1691 @subsection Command Substitution
1692 @cindex command substitution
1694 Command substitution allows the output of a command to replace
1696 Command substitution occurs when a command is enclosed as follows:
1707 Bash performs the expansion by executing @var{command} and
1708 replacing the command substitution with the standard output of the
1709 command, with any trailing newlines deleted.
1710 Embedded newlines are not deleted, but they may be removed during
1712 The command substitution @code{$(cat @var{file})} can be
1713 replaced by the equivalent but faster @code{$(< @var{file})}.
1715 When the old-style backquote form of substitution is used,
1716 backslash retains its literal meaning except when followed by
1717 @samp{$}, @samp{`}, or @samp{\}.
1718 The first backquote not preceded by a backslash terminates the
1719 command substitution.
1720 When using the @code{$(@var{command})} form, all characters between
1721 the parentheses make up the command; none are treated specially.
1723 Command substitutions may be nested. To nest when using the backquoted
1724 form, escape the inner backquotes with backslashes.
1726 If the substitution appears within double quotes, word splitting and
1727 filename expansion are not performed on the results.
1729 @node Arithmetic Expansion
1730 @subsection Arithmetic Expansion
1731 @cindex expansion, arithmetic
1732 @cindex arithmetic expansion
1734 Arithmetic expansion allows the evaluation of an arithmetic expression
1735 and the substitution of the result. The format for arithmetic expansion is:
1738 $(( @var{expression} ))
1741 The expression is treated as if it were within double quotes, but
1742 a double quote inside the parentheses is not treated specially.
1743 All tokens in the expression undergo parameter expansion, command
1744 substitution, and quote removal.
1745 Arithmetic expansions may be nested.
1747 The evaluation is performed according to the rules listed below
1748 (@pxref{Shell Arithmetic}).
1749 If the expression is invalid, Bash prints a message indicating
1750 failure to the standard error and no substitution occurs.
1752 @node Process Substitution
1753 @subsection Process Substitution
1754 @cindex process substitution
1756 Process substitution is supported on systems that support named
1757 pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files.
1758 It takes the form of
1768 The process @var{list} is run with its input or output connected to a
1769 @sc{fifo} or some file in @file{/dev/fd}. The name of this file is
1770 passed as an argument to the current command as the result of the
1771 expansion. If the @code{>(@var{list})} form is used, writing to
1772 the file will provide input for @var{list}. If the
1773 @code{<(@var{list})} form is used, the file passed as an
1774 argument should be read to obtain the output of @var{list}.
1775 Note that no space may appear between the @code{<} or @code{>}
1776 and the left parenthesis, otherwise the construct would be interpreted
1779 When available, process substitution is performed simultaneously with
1780 parameter and variable expansion, command substitution, and arithmetic
1783 @node Word Splitting
1784 @subsection Word Splitting
1785 @cindex word splitting
1787 The shell scans the results of parameter expansion, command substitution,
1788 and arithmetic expansion that did not occur within double quotes for
1791 The shell treats each character of @env{$IFS}
1792 as a delimiter, and splits the results of the other
1793 expansions into words on these characters. If
1794 @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>},
1795 the default, then any sequence of @env{IFS}
1796 characters serves to delimit words. If @env{IFS}
1797 has a value other than the default, then sequences of
1798 the whitespace characters @code{space} and @code{tab}
1799 are ignored at the beginning and end of the
1800 word, as long as the whitespace character is in the
1801 value of @env{IFS} (an @env{IFS} whitespace character).
1802 Any character in @env{IFS} that is not @env{IFS}
1803 whitespace, along with any adjacent @env{IFS}
1804 whitespace characters, delimits a field. A sequence of @env{IFS}
1805 whitespace characters is also treated as a delimiter.
1806 If the value of @env{IFS} is null, no word splitting occurs.
1808 Explicit null arguments (@code{""} or @code{''}) are retained.
1809 Unquoted implicit null arguments, resulting from the expansion of
1810 parameters that have no values, are removed.
1811 If a parameter with no value is expanded within double quotes, a
1812 null argument results and is retained.
1814 Note that if no expansion occurs, no splitting
1817 @node Filename Expansion
1818 @subsection Filename Expansion
1820 * Pattern Matching:: How the shell matches patterns.
1822 @cindex expansion, filename
1823 @cindex expansion, pathname
1824 @cindex filename expansion
1825 @cindex pathname expansion
1827 After word splitting, unless the @option{-f} option has been set
1828 (@pxref{The Set Builtin}), Bash scans each word for the characters
1829 @samp{*}, @samp{?}, and @samp{[}.
1830 If one of these characters appears, then the word is
1831 regarded as a @var{pattern},
1832 and replaced with an alphabetically sorted list of
1833 file names matching the pattern. If no matching file names are found,
1834 and the shell option @code{nullglob} is disabled, the word is left
1836 If the @code{nullglob} option is set, and no matches are found, the word
1838 If the @code{failglob} shell option is set, and no matches are found,
1839 an error message is printed and the command is not executed.
1840 If the shell option @code{nocaseglob} is enabled, the match is performed
1841 without regard to the case of alphabetic characters.
1843 When a pattern is used for filename generation, the character @samp{.}
1844 at the start of a filename or immediately following a slash
1845 must be matched explicitly, unless the shell option @code{dotglob} is set.
1846 When matching a file name, the slash character must always be
1848 In other cases, the @samp{.} character is not treated specially.
1850 See the description of @code{shopt} in @ref{Bash Builtins},
1851 for a description of the @code{nocaseglob}, @code{nullglob},
1852 @code{failglob}, and @code{dotglob} options.
1854 The @env{GLOBIGNORE}
1855 shell variable may be used to restrict the set of filenames matching a
1856 pattern. If @env{GLOBIGNORE}
1857 is set, each matching filename that also matches one of the patterns in
1858 @env{GLOBIGNORE} is removed from the list of matches. The filenames
1859 @file{.} and @file{..}
1860 are always ignored when @env{GLOBIGNORE}
1861 is set and not null.
1862 However, setting @env{GLOBIGNORE} to a non-null value has the effect of
1863 enabling the @code{dotglob}
1864 shell option, so all other filenames beginning with a
1865 @samp{.} will match.
1866 To get the old behavior of ignoring filenames beginning with a
1867 @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}.
1868 The @code{dotglob} option is disabled when @env{GLOBIGNORE}
1871 @node Pattern Matching
1872 @subsubsection Pattern Matching
1873 @cindex pattern matching
1874 @cindex matching, pattern
1876 Any character that appears in a pattern, other than the special pattern
1877 characters described below, matches itself.
1878 The @sc{nul} character may not occur in a pattern.
1879 A backslash escapes the following character; the
1880 escaping backslash is discarded when matching.
1881 The special pattern characters must be quoted if they are to be matched
1884 The special pattern characters have the following meanings:
1887 Matches any string, including the null string.
1889 Matches any single character.
1891 Matches any one of the enclosed characters. A pair of characters
1892 separated by a hyphen denotes a @var{range expression};
1893 any character that sorts between those two characters, inclusive,
1894 using the current locale's collating sequence and character set,
1895 is matched. If the first character following the
1896 @samp{[} is a @samp{!} or a @samp{^}
1897 then any character not enclosed is matched. A @samp{@minus{}}
1898 may be matched by including it as the first or last character
1899 in the set. A @samp{]} may be matched by including it as the first
1900 character in the set.
1901 The sorting order of characters in range expressions is determined by
1902 the current locale and the value of the @env{LC_COLLATE} shell variable,
1905 For example, in the default C locale, @samp{[a-dx-z]} is equivalent to
1906 @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in
1907 these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]};
1908 it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain
1909 the traditional interpretation of ranges in bracket expressions, you can
1910 force the use of the C locale by setting the @env{LC_COLLATE} or
1911 @env{LC_ALL} environment variable to the value @samp{C}.
1913 Within @samp{[} and @samp{]}, @var{character classes} can be specified
1915 @code{[:}@var{class}@code{:]}, where @var{class} is one of the
1916 following classes defined in the @sc{posix} 1003.2 standard:
1918 alnum alpha ascii blank cntrl digit graph lower
1919 print punct space upper word xdigit
1922 A character class matches any character belonging to that class.
1923 The @code{word} character class matches letters, digits, and the character
1926 Within @samp{[} and @samp{]}, an @var{equivalence class} can be
1927 specified using the syntax @code{[=}@var{c}@code{=]}, which
1928 matches all characters with the same collation weight (as defined
1929 by the current locale) as the character @var{c}.
1931 Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]}
1932 matches the collating symbol @var{symbol}.
1935 If the @code{extglob} shell option is enabled using the @code{shopt}
1936 builtin, several extended pattern matching operators are recognized.
1937 In the following description, a @var{pattern-list} is a list of one
1938 or more patterns separated by a @samp{|}.
1939 Composite patterns may be formed using one or more of the following
1943 @item ?(@var{pattern-list})
1944 Matches zero or one occurrence of the given patterns.
1946 @item *(@var{pattern-list})
1947 Matches zero or more occurrences of the given patterns.
1949 @item +(@var{pattern-list})
1950 Matches one or more occurrences of the given patterns.
1952 @item @@(@var{pattern-list})
1953 Matches one of the given patterns.
1955 @item !(@var{pattern-list})
1956 Matches anything except one of the given patterns.
1960 @subsection Quote Removal
1962 After the preceding expansions, all unquoted occurrences of the
1963 characters @samp{\}, @samp{'}, and @samp{"} that did not
1964 result from one of the above expansions are removed.
1967 @section Redirections
1970 Before a command is executed, its input and output
1971 may be @var{redirected}
1972 using a special notation interpreted by the shell.
1973 Redirection may also be used to open and close files for the
1974 current shell execution environment. The following redirection
1975 operators may precede or appear anywhere within a
1976 simple command or may follow a command.
1977 Redirections are processed in the order they appear, from
1980 In the following descriptions, if the file descriptor number is
1981 omitted, and the first character of the redirection operator is
1982 @samp{<}, the redirection refers to the standard input (file
1983 descriptor 0). If the first character of the redirection operator
1984 is @samp{>}, the redirection refers to the standard output (file
1987 The word following the redirection operator in the following
1988 descriptions, unless otherwise noted, is subjected to brace expansion,
1989 tilde expansion, parameter expansion, command substitution, arithmetic
1990 expansion, quote removal, filename expansion, and word splitting.
1991 If it expands to more than one word, Bash reports an error.
1993 Note that the order of redirections is significant. For example,
1996 ls > @var{dirlist} 2>&1
1999 directs both standard output (file descriptor 1) and standard error
2000 (file descriptor 2) to the file @var{dirlist}, while the command
2002 ls 2>&1 > @var{dirlist}
2005 directs only the standard output to file @var{dirlist},
2006 because the standard error was duplicated as standard output
2007 before the standard output was redirected to @var{dirlist}.
2009 Bash handles several filenames specially when they are used in
2010 redirections, as described in the following table:
2013 @item /dev/fd/@var{fd}
2014 If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated.
2017 File descriptor 0 is duplicated.
2020 File descriptor 1 is duplicated.
2023 File descriptor 2 is duplicated.
2025 @item /dev/tcp/@var{host}/@var{port}
2026 If @var{host} is a valid hostname or Internet address, and @var{port}
2027 is an integer port number or service name, Bash attempts to open a TCP
2028 connection to the corresponding socket.
2030 @item /dev/udp/@var{host}/@var{port}
2031 If @var{host} is a valid hostname or Internet address, and @var{port}
2032 is an integer port number or service name, Bash attempts to open a UDP
2033 connection to the corresponding socket.
2037 A failure to open or create a file causes the redirection to fail.
2039 Redirections using file descriptors greater than 9 should be used with
2040 care, as they may conflict with file descriptors the shell uses
2043 @subsection Redirecting Input
2044 Redirection of input causes the file whose name results from
2045 the expansion of @var{word}
2046 to be opened for reading on file descriptor @code{n},
2047 or the standard input (file descriptor 0) if @code{n}
2050 The general format for redirecting input is:
2052 [@var{n}]<@var{word}
2055 @subsection Redirecting Output
2056 Redirection of output causes the file whose name results from
2057 the expansion of @var{word}
2058 to be opened for writing on file descriptor @var{n},
2059 or the standard output (file descriptor 1) if @var{n}
2060 is not specified. If the file does not exist it is created;
2061 if it does exist it is truncated to zero size.
2063 The general format for redirecting output is:
2065 [@var{n}]>[|]@var{word}
2068 If the redirection operator is @samp{>}, and the @code{noclobber}
2069 option to the @code{set} builtin has been enabled, the redirection
2070 will fail if the file whose name results from the expansion of
2071 @var{word} exists and is a regular file.
2072 If the redirection operator is @samp{>|}, or the redirection operator is
2073 @samp{>} and the @code{noclobber} option is not enabled, the redirection
2074 is attempted even if the file named by @var{word} exists.
2076 @subsection Appending Redirected Output
2077 Redirection of output in this fashion
2078 causes the file whose name results from
2079 the expansion of @var{word}
2080 to be opened for appending on file descriptor @var{n},
2081 or the standard output (file descriptor 1) if @var{n}
2082 is not specified. If the file does not exist it is created.
2084 The general format for appending output is:
2086 [@var{n}]>>@var{word}
2089 @subsection Redirecting Standard Output and Standard Error
2090 Bash allows both the
2091 standard output (file descriptor 1) and
2092 the standard error output (file descriptor 2)
2093 to be redirected to the file whose name is the
2094 expansion of @var{word} with this construct.
2096 There are two formats for redirecting standard output and
2107 Of the two forms, the first is preferred.
2108 This is semantically equivalent to
2113 @subsection Here Documents
2114 This type of redirection instructs the shell to read input from the
2115 current source until a line containing only @var{word}
2116 (with no trailing blanks) is seen. All of
2117 the lines read up to that point are then used as the standard
2118 input for a command.
2120 The format of here-documents is:
2122 <<[@minus{}]@var{word}
2127 No parameter expansion, command substitution, arithmetic expansion,
2128 or filename expansion is performed on
2129 @var{word}. If any characters in @var{word} are quoted, the
2130 @var{delimiter} is the result of quote removal on @var{word},
2131 and the lines in the here-document are not expanded.
2132 If @var{word} is unquoted,
2133 all lines of the here-document are subjected to parameter expansion,
2134 command substitution, and arithmetic expansion. In the latter
2135 case, the character sequence @code{\newline} is ignored, and @samp{\}
2136 must be used to quote the characters
2137 @samp{\}, @samp{$}, and @samp{`}.
2139 If the redirection operator is @samp{<<-},
2140 then all leading tab characters are stripped from input lines and the
2141 line containing @var{delimiter}.
2142 This allows here-documents within shell scripts to be indented in a
2145 @subsection Here Strings
2146 A variant of here documents, the format is:
2151 The @var{word} is expanded and supplied to the command on its standard
2154 @subsection Duplicating File Descriptors
2155 The redirection operator
2157 [@var{n}]<&@var{word}
2160 is used to duplicate input file descriptors.
2162 expands to one or more digits, the file descriptor denoted by @var{n}
2163 is made to be a copy of that file descriptor.
2164 If the digits in @var{word} do not specify a file descriptor open for
2165 input, a redirection error occurs.
2167 evaluates to @samp{-}, file descriptor @var{n} is closed. If
2168 @var{n} is not specified, the standard input (file descriptor 0) is used.
2172 [@var{n}]>&@var{word}
2175 is used similarly to duplicate output file descriptors. If
2176 @var{n} is not specified, the standard output (file descriptor 1) is used.
2177 If the digits in @var{word} do not specify a file descriptor open for
2178 output, a redirection error occurs.
2179 As a special case, if @var{n} is omitted, and @var{word} does not
2180 expand to one or more digits, the standard output and standard
2181 error are redirected as described previously.
2183 @subsection Moving File Descriptors
2184 The redirection operator
2186 [@var{n}]<&@var{digit}-
2189 moves the file descriptor @var{digit} to file descriptor @var{n},
2190 or the standard input (file descriptor 0) if @var{n} is not specified.
2191 @var{digit} is closed after being duplicated to @var{n}.
2193 Similarly, the redirection operator
2195 [@var{n}]>&@var{digit}-
2198 moves the file descriptor @var{digit} to file descriptor @var{n},
2199 or the standard output (file descriptor 1) if @var{n} is not specified.
2201 @subsection Opening File Descriptors for Reading and Writing
2202 The redirection operator
2204 [@var{n}]<>@var{word}
2207 causes the file whose name is the expansion of @var{word}
2208 to be opened for both reading and writing on file descriptor
2209 @var{n}, or on file descriptor 0 if @var{n}
2210 is not specified. If the file does not exist, it is created.
2212 @node Executing Commands
2213 @section Executing Commands
2216 * Simple Command Expansion:: How Bash expands simple commands before
2219 * Command Search and Execution:: How Bash finds commands and runs them.
2221 * Command Execution Environment:: The environment in which Bash
2222 executes commands that are not
2225 * Environment:: The environment given to a command.
2227 * Exit Status:: The status returned by commands and how Bash
2230 * Signals:: What happens when Bash or a command it runs
2235 @node Simple Command Expansion
2236 @subsection Simple Command Expansion
2237 @cindex command expansion
2239 When a simple command is executed, the shell performs the following
2240 expansions, assignments, and redirections, from left to right.
2244 The words that the parser has marked as variable assignments (those
2245 preceding the command name) and redirections are saved for later
2249 The words that are not variable assignments or redirections are
2250 expanded (@pxref{Shell Expansions}).
2251 If any words remain after expansion, the first word
2252 is taken to be the name of the command and the remaining words are
2256 Redirections are performed as described above (@pxref{Redirections}).
2259 The text after the @samp{=} in each variable assignment undergoes tilde
2260 expansion, parameter expansion, command substitution, arithmetic expansion,
2261 and quote removal before being assigned to the variable.
2264 If no command name results, the variable assignments affect the current
2265 shell environment. Otherwise, the variables are added to the environment
2266 of the executed command and do not affect the current shell environment.
2267 If any of the assignments attempts to assign a value to a readonly variable,
2268 an error occurs, and the command exits with a non-zero status.
2270 If no command name results, redirections are performed, but do not
2271 affect the current shell environment. A redirection error causes the
2272 command to exit with a non-zero status.
2274 If there is a command name left after expansion, execution proceeds as
2275 described below. Otherwise, the command exits. If one of the expansions
2276 contained a command substitution, the exit status of the command is
2277 the exit status of the last command substitution performed. If there
2278 were no command substitutions, the command exits with a status of zero.
2280 @node Command Search and Execution
2281 @subsection Command Search and Execution
2282 @cindex command execution
2283 @cindex command search
2285 After a command has been split into words, if it results in a
2286 simple command and an optional list of arguments, the following
2291 If the command name contains no slashes, the shell attempts to
2292 locate it. If there exists a shell function by that name, that
2293 function is invoked as described in @ref{Shell Functions}.
2296 If the name does not match a function, the shell searches for
2297 it in the list of shell builtins. If a match is found, that
2301 If the name is neither a shell function nor a builtin,
2302 and contains no slashes, Bash searches each element of
2303 @env{$PATH} for a directory containing an executable file
2304 by that name. Bash uses a hash table to remember the full
2305 pathnames of executable files to avoid multiple @env{PATH} searches
2306 (see the description of @code{hash} in @ref{Bourne Shell Builtins}).
2307 A full search of the directories in @env{$PATH}
2308 is performed only if the command is not found in the hash table.
2309 If the search is unsuccessful, the shell prints an error
2310 message and returns an exit status of 127.
2313 If the search is successful, or if the command name contains
2314 one or more slashes, the shell executes the named program in
2315 a separate execution environment.
2316 Argument 0 is set to the name given, and the remaining arguments
2317 to the command are set to the arguments supplied, if any.
2320 If this execution fails because the file is not in executable
2321 format, and the file is not a directory, it is assumed to be a
2322 @var{shell script} and the shell executes it as described in
2323 @ref{Shell Scripts}.
2326 If the command was not begun asynchronously, the shell waits for
2327 the command to complete and collects its exit status.
2331 @node Command Execution Environment
2332 @subsection Command Execution Environment
2333 @cindex execution environment
2335 The shell has an @var{execution environment}, which consists of the
2340 open files inherited by the shell at invocation, as modified by
2341 redirections supplied to the @code{exec} builtin
2344 the current working directory as set by @code{cd}, @code{pushd}, or
2345 @code{popd}, or inherited by the shell at invocation
2348 the file creation mode mask as set by @code{umask} or inherited from
2352 current traps set by @code{trap}
2355 shell parameters that are set by variable assignment or with @code{set}
2356 or inherited from the shell's parent in the environment
2359 shell functions defined during execution or inherited from the shell's
2360 parent in the environment
2363 options enabled at invocation (either by default or with command-line
2364 arguments) or by @code{set}
2367 options enabled by @code{shopt}
2370 shell aliases defined with @code{alias} (@pxref{Aliases})
2373 various process @sc{id}s, including those of background jobs
2374 (@pxref{Lists}), the value of @code{$$}, and the value of
2379 When a simple command other than a builtin or shell function
2380 is to be executed, it
2381 is invoked in a separate execution environment that consists of
2382 the following. Unless otherwise noted, the values are inherited
2387 the shell's open files, plus any modifications and additions specified
2388 by redirections to the command
2391 the current working directory
2394 the file creation mode mask
2397 shell variables and functions marked for export, along with variables
2398 exported for the command, passed in the environment (@pxref{Environment})
2401 traps caught by the shell are reset to the values inherited from the
2402 shell's parent, and traps ignored by the shell are ignored
2406 A command invoked in this separate environment cannot affect the
2407 shell's execution environment.
2409 Command substitution, commands grouped with parentheses,
2410 and asynchronous commands are invoked in a
2411 subshell environment that is a duplicate of the shell environment,
2412 except that traps caught by the shell are reset to the values
2413 that the shell inherited from its parent at invocation. Builtin
2414 commands that are invoked as part of a pipeline are also executed
2415 in a subshell environment. Changes made to the subshell environment
2416 cannot affect the shell's execution environment.
2418 If a command is followed by a @samp{&} and job control is not active, the
2419 default standard input for the command is the empty file @file{/dev/null}.
2420 Otherwise, the invoked command inherits the file descriptors of the calling
2421 shell as modified by redirections.
2424 @subsection Environment
2427 When a program is invoked it is given an array of strings
2428 called the @var{environment}.
2429 This is a list of name-value pairs, of the form @code{name=value}.
2431 Bash provides several ways to manipulate the environment.
2432 On invocation, the shell scans its own environment and
2433 creates a parameter for each name found, automatically marking
2435 to child processes. Executed commands inherit the environment.
2436 The @code{export} and @samp{declare -x}
2437 commands allow parameters and functions to be added to and
2438 deleted from the environment. If the value of a parameter
2439 in the environment is modified, the new value becomes part
2440 of the environment, replacing the old. The environment
2441 inherited by any executed command consists of the shell's
2442 initial environment, whose values may be modified in the shell,
2443 less any pairs removed by the @code{unset} and @samp{export -n}
2444 commands, plus any additions via the @code{export} and
2445 @samp{declare -x} commands.
2447 The environment for any simple command
2448 or function may be augmented temporarily by prefixing it with
2449 parameter assignments, as described in @ref{Shell Parameters}.
2450 These assignment statements affect only the environment seen
2453 If the @option{-k} option is set (@pxref{The Set Builtin}), then all
2454 parameter assignments are placed in the environment for a command,
2455 not just those that precede the command name.
2457 When Bash invokes an external command, the variable @samp{$_}
2458 is set to the full path name of the command and passed to that
2459 command in its environment.
2462 @subsection Exit Status
2465 For the shell's purposes, a command which exits with a
2466 zero exit status has succeeded.
2467 A non-zero exit status indicates failure.
2468 This seemingly counter-intuitive scheme is used so there
2469 is one well-defined way to indicate success and a variety of
2470 ways to indicate various failure modes.
2471 When a command terminates on a fatal signal whose number is @var{N},
2472 Bash uses the value 128+@var{N} as the exit status.
2474 If a command is not found, the child process created to
2475 execute it returns a status of 127. If a command is found
2476 but is not executable, the return status is 126.
2478 If a command fails because of an error during expansion or redirection,
2479 the exit status is greater than zero.
2481 The exit status is used by the Bash conditional commands
2482 (@pxref{Conditional Constructs}) and some of the list
2483 constructs (@pxref{Lists}).
2485 All of the Bash builtins return an exit status of zero if they succeed
2486 and a non-zero status on failure, so they may be used by the
2487 conditional and list constructs.
2488 All builtins return an exit status of 2 to indicate incorrect usage.
2492 @cindex signal handling
2494 When Bash is interactive, in the absence of any traps, it ignores
2495 @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell),
2497 is caught and handled (so that the @code{wait} builtin is interruptible).
2498 When Bash receives a @code{SIGINT}, it breaks out of any executing loops.
2499 In all cases, Bash ignores @code{SIGQUIT}.
2500 If job control is in effect (@pxref{Job Control}), Bash
2501 ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
2503 Non-builtin commands started by Bash have signal handlers set to the
2504 values inherited by the shell from its parent.
2505 When job control is not in effect, asynchronous commands
2506 ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited
2508 Commands run as a result of
2509 command substitution ignore the keyboard-generated job control signals
2510 @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
2512 The shell exits by default upon receipt of a @code{SIGHUP}.
2513 Before exiting, an interactive shell resends the @code{SIGHUP} to
2514 all jobs, running or stopped.
2515 Stopped jobs are sent @code{SIGCONT} to ensure that they receive
2517 To prevent the shell from sending the @code{SIGHUP} signal to a
2518 particular job, it should be removed
2519 from the jobs table with the @code{disown}
2520 builtin (@pxref{Job Control Builtins}) or marked
2521 to not receive @code{SIGHUP} using @code{disown -h}.
2523 If the @code{huponexit} shell option has been set with @code{shopt}
2524 (@pxref{Bash Builtins}), Bash sends a @code{SIGHUP} to all jobs when
2525 an interactive login shell exits.
2527 If Bash is waiting for a command to complete and receives a signal
2528 for which a trap has been set, the trap will not be executed until
2529 the command completes.
2530 When Bash is waiting for an asynchronous
2531 command via the @code{wait} builtin, the reception of a signal for
2532 which a trap has been set will cause the @code{wait} builtin to return
2533 immediately with an exit status greater than 128, immediately after
2534 which the trap is executed.
2537 @section Shell Scripts
2538 @cindex shell script
2540 A shell script is a text file containing shell commands. When such
2541 a file is used as the first non-option argument when invoking Bash,
2542 and neither the @option{-c} nor @option{-s} option is supplied
2543 (@pxref{Invoking Bash}),
2544 Bash reads and executes commands from the file, then exits. This
2545 mode of operation creates a non-interactive shell. The shell first
2546 searches for the file in the current directory, and looks in the
2547 directories in @env{$PATH} if not found there.
2550 a shell script, it sets the special parameter @code{0} to the name
2551 of the file, rather than the name of the shell, and the positional
2552 parameters are set to the remaining arguments, if any are given.
2553 If no additional arguments are supplied, the positional parameters
2556 A shell script may be made executable by using the @code{chmod} command
2557 to turn on the execute bit. When Bash finds such a file while
2558 searching the @env{$PATH} for a command, it spawns a subshell to
2559 execute it. In other words, executing
2561 filename @var{arguments}
2564 is equivalent to executing
2566 bash filename @var{arguments}
2570 if @code{filename} is an executable shell script.
2571 This subshell reinitializes itself, so that the effect is as if a
2572 new shell had been invoked to interpret the script, with the
2573 exception that the locations of commands remembered by the parent
2574 (see the description of @code{hash} in @ref{Bourne Shell Builtins})
2575 are retained by the child.
2577 Most versions of Unix make this a part of the operating system's command
2578 execution mechanism. If the first line of a script begins with
2579 the two characters @samp{#!}, the remainder of the line specifies
2580 an interpreter for the program.
2581 Thus, you can specify Bash, @code{awk}, Perl, or some other
2582 interpreter and write the rest of the script file in that language.
2584 The arguments to the interpreter
2585 consist of a single optional argument following the interpreter
2586 name on the first line of the script file, followed by the name of
2587 the script file, followed by the rest of the arguments. Bash
2588 will perform this action on operating systems that do not handle it
2589 themselves. Note that some older versions of Unix limit the interpreter
2590 name and argument to a maximum of 32 characters.
2592 Bash scripts often begin with @code{#! /bin/bash} (assuming that
2593 Bash has been installed in @file{/bin}), since this ensures that
2594 Bash will be used to interpret the script, even if it is executed
2595 under another shell.
2597 @node Shell Builtin Commands
2598 @chapter Shell Builtin Commands
2601 * Bourne Shell Builtins:: Builtin commands inherited from the Bourne
2603 * Bash Builtins:: Table of builtins specific to Bash.
2604 * The Set Builtin:: This builtin is so overloaded it
2605 deserves its own section.
2606 * Special Builtins:: Builtin commands classified specially by
2610 Builtin commands are contained within the shell itself.
2611 When the name of a builtin command is used as the first word of
2612 a simple command (@pxref{Simple Commands}), the shell executes
2613 the command directly, without invoking another program.
2614 Builtin commands are necessary to implement functionality impossible
2615 or inconvenient to obtain with separate utilities.
2617 This section briefly the builtins which Bash inherits from
2618 the Bourne Shell, as well as the builtin commands which are unique
2619 to or have been extended in Bash.
2621 Several builtin commands are described in other chapters: builtin
2622 commands which provide the Bash interface to the job control
2623 facilities (@pxref{Job Control Builtins}), the directory stack
2624 (@pxref{Directory Stack Builtins}), the command history
2625 (@pxref{Bash History Builtins}), and the programmable completion
2626 facilities (@pxref{Programmable Completion Builtins}).
2628 Many of the builtins have been extended by @sc{posix} or Bash.
2630 Unless otherwise noted, each builtin command documented as accepting
2631 options preceded by @samp{-} accepts @samp{--}
2632 to signify the end of the options.
2634 @node Bourne Shell Builtins
2635 @section Bourne Shell Builtins
2637 The following shell builtin commands are inherited from the Bourne Shell.
2638 These commands are implemented as specified by the @sc{posix} 1003.2 standard.
2641 @item : @r{(a colon)}
2646 Do nothing beyond expanding @var{arguments} and performing redirections.
2647 The return status is zero.
2649 @item . @r{(a period)}
2652 . @var{filename} [@var{arguments}]
2654 Read and execute commands from the @var{filename} argument in the
2655 current shell context. If @var{filename} does not contain a slash,
2656 the @env{PATH} variable is used to find @var{filename}.
2657 When Bash is not in @sc{posix} mode, the current directory is searched
2658 if @var{filename} is not found in @env{$PATH}.
2659 If any @var{arguments} are supplied, they become the positional
2660 parameters when @var{filename} is executed. Otherwise the positional
2661 parameters are unchanged.
2662 The return status is the exit status of the last command executed, or
2663 zero if no commands are executed. If @var{filename} is not found, or
2664 cannot be read, the return status is non-zero.
2665 This builtin is equivalent to @code{source}.
2672 Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop.
2673 If @var{n} is supplied, the @var{n}th enclosing loop is exited.
2674 @var{n} must be greater than or equal to 1.
2675 The return status is zero unless @var{n} is not greater than or equal to 1.
2680 cd [-L|-P] [@var{directory}]
2682 Change the current working directory to @var{directory}.
2683 If @var{directory} is not given, the value of the @env{HOME} shell
2685 If the shell variable @env{CDPATH} exists, it is used as a search path.
2686 If @var{directory} begins with a slash, @env{CDPATH} is not used.
2688 The @option{-P} option means to not follow symbolic links; symbolic
2689 links are followed by default or with the @option{-L} option.
2690 If @var{directory} is @samp{-}, it is equivalent to @env{$OLDPWD}.
2692 If a non-empty directory name from @env{CDPATH} is used, or if
2693 @samp{-} is the first argument, and the directory change is
2694 successful, the absolute pathname of the new working directory is
2695 written to the standard output.
2697 The return status is zero if the directory is successfully changed,
2705 Resume the next iteration of an enclosing @code{for}, @code{while},
2706 @code{until}, or @code{select} loop.
2707 If @var{n} is supplied, the execution of the @var{n}th enclosing loop
2709 @var{n} must be greater than or equal to 1.
2710 The return status is zero unless @var{n} is not greater than or equal to 1.
2715 eval [@var{arguments}]
2717 The arguments are concatenated together into a single command, which is
2718 then read and executed, and its exit status returned as the exit status
2720 If there are no arguments or only empty arguments, the return status is
2726 exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]]
2729 is supplied, it replaces the shell without creating a new process.
2730 If the @option{-l} option is supplied, the shell places a dash at the
2731 beginning of the zeroth arg passed to @var{command}.
2732 This is what the @code{login} program does.
2733 The @option{-c} option causes @var{command} to be executed with an empty
2735 If @option{-a} is supplied, the shell passes @var{name} as the zeroth
2736 argument to @var{command}.
2737 If no @var{command} is specified, redirections may be used to affect
2738 the current shell environment. If there are no redirection errors, the
2739 return status is zero; otherwise the return status is non-zero.
2746 Exit the shell, returning a status of @var{n} to the shell's parent.
2747 If @var{n} is omitted, the exit status is that of the last command executed.
2748 Any trap on @code{EXIT} is executed before the shell terminates.
2753 export [-fn] [-p] [@var{name}[=@var{value}]]
2755 Mark each @var{name} to be passed to child processes
2756 in the environment. If the @option{-f} option is supplied, the @var{name}s
2757 refer to shell functions; otherwise the names refer to shell variables.
2758 The @option{-n} option means to no longer mark each @var{name} for export.
2759 If no @var{names} are supplied, or if the @option{-p} option is given, a
2760 list of exported names is displayed.
2761 The @option{-p} option displays output in a form that may be reused as input.
2762 If a variable name is followed by =@var{value}, the value of
2763 the variable is set to @var{value}.
2765 The return status is zero unless an invalid option is supplied, one of
2766 the names is not a valid shell variable name, or @option{-f} is supplied
2767 with a name that is not a shell function.
2772 getopts @var{optstring} @var{name} [@var{args}]
2774 @code{getopts} is used by shell scripts to parse positional parameters.
2775 @var{optstring} contains the option characters to be recognized; if a
2776 character is followed by a colon, the option is expected to have an
2777 argument, which should be separated from it by white space.
2778 The colon (@samp{:}) and question mark (@samp{?}) may not be
2779 used as option characters.
2780 Each time it is invoked, @code{getopts}
2781 places the next option in the shell variable @var{name}, initializing
2782 @var{name} if it does not exist,
2783 and the index of the next argument to be processed into the
2784 variable @env{OPTIND}.
2785 @env{OPTIND} is initialized to 1 each time the shell or a shell script
2787 When an option requires an argument,
2788 @code{getopts} places that argument into the variable @env{OPTARG}.
2789 The shell does not reset @env{OPTIND} automatically; it must be manually
2790 reset between multiple calls to @code{getopts} within the same shell
2791 invocation if a new set of parameters is to be used.
2793 When the end of options is encountered, @code{getopts} exits with a
2794 return value greater than zero.
2795 @env{OPTIND} is set to the index of the first non-option argument,
2796 and @code{name} is set to @samp{?}.
2799 normally parses the positional parameters, but if more arguments are
2800 given in @var{args}, @code{getopts} parses those instead.
2802 @code{getopts} can report errors in two ways. If the first character of
2803 @var{optstring} is a colon, @var{silent}
2804 error reporting is used. In normal operation diagnostic messages
2805 are printed when invalid options or missing option arguments are
2807 If the variable @env{OPTERR}
2808 is set to 0, no error messages will be displayed, even if the first
2809 character of @code{optstring} is not a colon.
2811 If an invalid option is seen,
2812 @code{getopts} places @samp{?} into @var{name} and, if not silent,
2813 prints an error message and unsets @env{OPTARG}.
2814 If @code{getopts} is silent, the option character found is placed in
2815 @env{OPTARG} and no diagnostic message is printed.
2817 If a required argument is not found, and @code{getopts}
2818 is not silent, a question mark (@samp{?}) is placed in @var{name},
2819 @code{OPTARG} is unset, and a diagnostic message is printed.
2820 If @code{getopts} is silent, then a colon (@samp{:}) is placed in
2821 @var{name} and @env{OPTARG} is set to the option character found.
2826 hash [-'r] [-p @var{filename}] [-dt] [@var{name}]
2828 Remember the full pathnames of commands specified as @var{name} arguments,
2829 so they need not be searched for on subsequent invocations.
2830 The commands are found by searching through the directories listed in
2832 The @option{-p} option inhibits the path search, and @var{filename} is
2833 used as the location of @var{name}.
2834 The @option{-r} option causes the shell to forget all remembered locations.
2835 The @option{-d} option causes the shell to forget the remembered location
2837 If the @option{-t} option is supplied, the full pathname to which each
2838 @var{name} corresponds is printed. If multiple @var{name} arguments are
2839 supplied with @option{-t} the @var{name} is printed before the hashed
2841 The @option{-l} option causes output to be displayed in a format
2842 that may be reused as input.
2843 If no arguments are given, or if only @option{-l} is supplied,
2844 information about remembered commands is printed.
2845 The return status is zero unless a @var{name} is not found or an invalid
2853 Print the absolute pathname of the current working directory.
2854 If the @option{-P} option is supplied, the pathname printed will not
2855 contain symbolic links.
2856 If the @option{-L} option is supplied, the pathname printed may contain
2858 The return status is zero unless an error is encountered while
2859 determining the name of the current directory or an invalid option
2865 readonly [-apf] [@var{name}[=@var{value}]] @dots{}
2867 Mark each @var{name} as readonly.
2868 The values of these names may not be changed by subsequent assignment.
2869 If the @option{-f} option is supplied, each @var{name} refers to a shell
2871 The @option{-a} option means each @var{name} refers to an array variable.
2872 If no @var{name} arguments are given, or if the @option{-p}
2873 option is supplied, a list of all readonly names is printed.
2874 The @option{-p} option causes output to be displayed in a format that
2875 may be reused as input.
2876 If a variable name is followed by =@var{value}, the value of
2877 the variable is set to @var{value}.
2878 The return status is zero unless an invalid option is supplied, one of
2879 the @var{name} arguments is not a valid shell variable or function name,
2880 or the @option{-f} option is supplied with a name that is not a shell function.
2887 Cause a shell function to exit with the return value @var{n}.
2888 If @var{n} is not supplied, the return value is the exit status of the
2889 last command executed in the function.
2890 This may also be used to terminate execution of a script being executed
2891 with the @code{.} (or @code{source}) builtin, returning either @var{n} or
2892 the exit status of the last command executed within the script as the exit
2893 status of the script.
2894 Any command associated with the @code{RETURN} trap is executed
2895 before execution resumes after the function or script.
2896 The return status is non-zero if @code{return} is used outside a function
2897 and not during the execution of a script by @code{.} or @code{source}.
2904 Shift the positional parameters to the left by @var{n}.
2905 The positional parameters from @var{n}+1 @dots{} @code{$#} are
2906 renamed to @code{$1} @dots{} @code{$#}-@var{n}+1.
2907 Parameters represented by the numbers @code{$#} to @var{n}+1 are unset.
2908 @var{n} must be a non-negative number less than or equal to @code{$#}.
2909 If @var{n} is zero or greater than @code{$#}, the positional parameters
2911 If @var{n} is not supplied, it is assumed to be 1.
2912 The return status is zero unless @var{n} is greater than @code{$#} or
2913 less than zero, non-zero otherwise.
2919 Evaluate a conditional expression @var{expr}.
2920 Each operator and operand must be a separate argument.
2921 Expressions are composed of the primaries described below in
2922 @ref{Bash Conditional Expressions}.
2924 When the @code{[} form is used, the last argument to the command must
2927 Expressions may be combined using the following operators, listed in
2928 decreasing order of precedence.
2932 True if @var{expr} is false.
2934 @item ( @var{expr} )
2935 Returns the value of @var{expr}.
2936 This may be used to override the normal precedence of operators.
2938 @item @var{expr1} -a @var{expr2}
2939 True if both @var{expr1} and @var{expr2} are true.
2941 @item @var{expr1} -o @var{expr2}
2942 True if either @var{expr1} or @var{expr2} is true.
2945 The @code{test} and @code{[} builtins evaluate conditional
2946 expressions using a set of rules based on the number of arguments.
2950 The expression is false.
2953 The expression is true if and only if the argument is not null.
2956 If the first argument is @samp{!}, the expression is true if and
2957 only if the second argument is null.
2958 If the first argument is one of the unary conditional operators
2959 (@pxref{Bash Conditional Expressions}), the expression
2960 is true if the unary test is true.
2961 If the first argument is not a valid unary operator, the expression is
2965 If the second argument is one of the binary conditional
2966 operators (@pxref{Bash Conditional Expressions}), the
2967 result of the expression is the result of the binary test using the
2968 first and third arguments as operands.
2969 If the first argument is @samp{!}, the value is the negation of
2970 the two-argument test using the second and third arguments.
2971 If the first argument is exactly @samp{(} and the third argument is
2972 exactly @samp{)}, the result is the one-argument test of the second
2974 Otherwise, the expression is false.
2975 The @samp{-a} and @samp{-o} operators are considered binary operators
2979 If the first argument is @samp{!}, the result is the negation of
2980 the three-argument expression composed of the remaining arguments.
2981 Otherwise, the expression is parsed and evaluated according to
2982 precedence using the rules listed above.
2984 @item 5 or more arguments
2985 The expression is parsed and evaluated according to precedence
2986 using the rules listed above.
2994 Print out the user and system times used by the shell and its children.
2995 The return status is zero.
3000 trap [-lp] [@var{arg}] [@var{sigspec} @dots{}]
3002 The commands in @var{arg} are to be read and executed when the
3003 shell receives signal @var{sigspec}. If @var{arg} is absent (and
3004 there is a single @var{sigspec}) or
3005 equal to @samp{-}, each specified signal's disposition is reset
3006 to the value it had when the shell was started.
3007 If @var{arg} is the null string, then the signal specified by
3008 each @var{sigspec} is ignored by the shell and commands it invokes.
3009 If @var{arg} is not present and @option{-p} has been supplied,
3010 the shell displays the trap commands associated with each @var{sigspec}.
3011 If no arguments are supplied, or
3012 only @option{-p} is given, @code{trap} prints the list of commands
3013 associated with each signal number in a form that may be reused as
3015 The @option{-l} option causes the shell to print a list of signal names
3016 and their corresponding numbers.
3017 Each @var{sigspec} is either a signal name or a signal number.
3018 Signal names are case insensitive and the @code{SIG} prefix is optional.
3020 is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits.
3021 If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed
3022 before every simple command, @code{for} command, @code{case} command,
3023 @code{select} command, every arithmetic @code{for} command, and before
3024 the first command executes in a shell function.
3025 Refer to the description of the @code{extglob} option to the
3026 @code{shopt} builtin (@pxref{Bash Builtins}) for details of its
3027 effect on the @code{DEBUG} trap.
3028 If a @var{sigspec} is @code{ERR}, the command @var{arg}
3029 is executed whenever a simple command has a non-zero exit status,
3030 subject to the following conditions.
3031 The @code{ERR} trap is not executed if the failed command is part of the
3032 command list immediately following an @code{until} or @code{while} keyword,
3033 part of the test in an @code{if} statement,
3034 part of a @code{&&} or @code{||} list, or if the command's return
3035 status is being inverted using @code{!}.
3036 These are the same conditions obeyed by the @code{errexit} option.
3037 If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed
3038 each time a shell function or a script executed with the @code{.} or
3039 @code{source} builtins finishes executing.
3041 Signals ignored upon entry to the shell cannot be trapped or reset.
3042 Trapped signals are reset to their original values in a child
3043 process when it is created.
3045 The return status is zero unless a @var{sigspec} does not specify a
3051 umask [-p] [-S] [@var{mode}]
3053 Set the shell process's file creation mask to @var{mode}. If
3054 @var{mode} begins with a digit, it is interpreted as an octal number;
3055 if not, it is interpreted as a symbolic mode mask similar
3056 to that accepted by the @code{chmod} command. If @var{mode} is
3057 omitted, the current value of the mask is printed. If the @option{-S}
3058 option is supplied without a @var{mode} argument, the mask is printed
3059 in a symbolic format.
3060 If the @option{-p} option is supplied, and @var{mode}
3061 is omitted, the output is in a form that may be reused as input.
3062 The return status is zero if the mode is successfully changed or if
3063 no @var{mode} argument is supplied, and non-zero otherwise.
3065 Note that when the mode is interpreted as an octal number, each number
3066 of the umask is subtracted from @code{7}. Thus, a umask of @code{022}
3067 results in permissions of @code{755}.
3072 unset [-fv] [@var{name}]
3074 Each variable or function @var{name} is removed.
3075 If no options are supplied, or the @option{-v} option is given, each
3076 @var{name} refers to a shell variable.
3077 If the @option{-f} option is given, the @var{name}s refer to shell
3078 functions, and the function definition is removed.
3079 Readonly variables and functions may not be unset.
3080 The return status is zero unless a @var{name} is readonly.
3084 @section Bash Builtin Commands
3086 This section describes builtin commands which are unique to
3087 or have been extended in Bash.
3088 Some of these commands are specified in the @sc{posix} 1003.2 standard.
3095 alias [@code{-p}] [@var{name}[=@var{value}] @dots{}]
3098 Without arguments or with the @option{-p} option, @code{alias} prints
3099 the list of aliases on the standard output in a form that allows
3100 them to be reused as input.
3101 If arguments are supplied, an alias is defined for each @var{name}
3102 whose @var{value} is given. If no @var{value} is given, the name
3103 and value of the alias is printed.
3104 Aliases are described in @ref{Aliases}.
3109 bind [-m @var{keymap}] [-lpsvPSV]
3110 bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}]
3111 bind [-m @var{keymap}] -f @var{filename}
3112 bind [-m @var{keymap}] -x @var{keyseq:shell-command}
3113 bind [-m @var{keymap}] @var{keyseq:function-name}
3114 bind @var{readline-command}
3117 Display current Readline (@pxref{Command Line Editing})
3118 key and function bindings,
3119 bind a key sequence to a Readline function or macro,
3120 or set a Readline variable.
3121 Each non-option argument is a command as it would appear in a
3122 a Readline initialization file (@pxref{Readline Init File}),
3123 but each binding or command must be passed as a separate argument; e.g.,
3124 @samp{"\C-x\C-r":re-read-init-file}.
3125 Options, if supplied, have the following meanings:
3128 @item -m @var{keymap}
3129 Use @var{keymap} as the keymap to be affected by
3130 the subsequent bindings. Acceptable @var{keymap}
3133 @code{emacs-standard},
3138 @code{vi-command}, and
3140 @code{vi} is equivalent to @code{vi-command};
3141 @code{emacs} is equivalent to @code{emacs-standard}.
3144 List the names of all Readline functions.
3147 Display Readline function names and bindings in such a way that they
3148 can be used as input or in a Readline initialization file.
3151 List current Readline function names and bindings.
3154 Display Readline variable names and values in such a way that they
3155 can be used as input or in a Readline initialization file.
3158 List current Readline variable names and values.
3161 Display Readline key sequences bound to macros and the strings they output
3162 in such a way that they can be used as input or in a Readline
3163 initialization file.
3166 Display Readline key sequences bound to macros and the strings they output.
3168 @item -f @var{filename}
3169 Read key bindings from @var{filename}.
3171 @item -q @var{function}
3172 Query about which keys invoke the named @var{function}.
3174 @item -u @var{function}
3175 Unbind all keys bound to the named @var{function}.
3177 @item -r @var{keyseq}
3178 Remove any current binding for @var{keyseq}.
3180 @item -x @var{keyseq:shell-command}
3181 Cause @var{shell-command} to be executed whenever @var{keyseq} is
3187 The return status is zero unless an invalid option is supplied or an
3193 builtin [@var{shell-builtin} [@var{args}]]
3195 Run a shell builtin, passing it @var{args}, and return its exit status.
3196 This is useful when defining a shell function with the same
3197 name as a shell builtin, retaining the functionality of the builtin within
3199 The return status is non-zero if @var{shell-builtin} is not a shell
3207 Returns the context of any active subroutine call (a shell function or
3208 a script executed with the @code{.} or @code{source} builtins).
3210 Without @var{expr}, @code{caller} displays the line number and source
3211 filename of the current subroutine call.
3212 If a non-negative integer is supplied as @var{expr}, @code{caller}
3213 displays the line number, subroutine name, and source file corresponding
3214 to that position in the current execution call stack. This extra
3215 information may be used, for example, to print a stack trace. The
3216 current frame is frame 0.
3218 The return value is 0 unless the shell is not executing a subroutine
3219 call or @var{expr} does not correspond to a valid position in the
3225 command [-pVv] @var{command} [@var{arguments} @dots{}]
3227 Runs @var{command} with @var{arguments} ignoring any shell function
3228 named @var{command}.
3229 Only shell builtin commands or commands found by searching the
3230 @env{PATH} are executed.
3231 If there is a shell function named @code{ls}, running @samp{command ls}
3232 within the function will execute the external command @code{ls}
3233 instead of calling the function recursively.
3234 The @option{-p} option means to use a default value for @env{PATH}
3235 that is guaranteed to find all of the standard utilities.
3236 The return status in this case is 127 if @var{command} cannot be
3237 found or an error occurred, and the exit status of @var{command}
3240 If either the @option{-V} or @option{-v} option is supplied, a
3241 description of @var{command} is printed. The @option{-v} option
3242 causes a single word indicating the command or file name used to
3243 invoke @var{command} to be displayed; the @option{-V} option produces
3244 a more verbose description. In this case, the return status is
3245 zero if @var{command} is found, and non-zero if not.
3250 declare [-afFirtx] [-p] [@var{name}[=@var{value}] @dots{}]
3253 Declare variables and give them attributes. If no @var{name}s
3254 are given, then display the values of variables instead.
3256 The @option{-p} option will display the attributes and values of each
3258 When @option{-p} is used, additional options are ignored.
3259 The @option{-F} option inhibits the display of function definitions;
3260 only the function name and attributes are printed.
3261 If the @code{extdebug} shell option is enabled using @code{shopt}
3262 (@pxref{Bash Builtins}), the source file name and line number where
3263 the function is defined are displayed as well.
3264 @option{-F} implies @option{-f}.
3265 The following options can be used to restrict output to variables with
3266 the specified attributes or to give variables attributes:
3270 Each @var{name} is an array variable (@pxref{Arrays}).
3273 Use function names only.
3276 The variable is to be treated as
3277 an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is
3278 performed when the variable is assigned a value.
3281 Make @var{name}s readonly. These names cannot then be assigned values
3282 by subsequent assignment statements or unset.
3285 Give each @var{name} the @code{trace} attribute.
3286 Traced functions inherit the @code{DEBUG} trap from the calling shell.
3287 The trace attribute has no special meaning for variables.
3290 Mark each @var{name} for export to subsequent commands via
3294 Using @samp{+} instead of @samp{-} turns off the attribute instead.
3295 When used in a function, @code{declare} makes each @var{name} local,
3296 as with the @code{local} command. If a variable name is followed by
3297 =@var{value}, the value of the variable is set to @var{value}.
3299 The return status is zero unless an invalid option is encountered,
3300 an attempt is made to define a function using @samp{-f foo=bar},
3301 an attempt is made to assign a value to a readonly variable,
3302 an attempt is made to assign a value to an array variable without
3303 using the compound assignment syntax (@pxref{Arrays}),
3304 one of the @var{names} is not a valid shell variable name,
3305 an attempt is made to turn off readonly status for a readonly variable,
3306 an attempt is made to turn off array status for an array variable,
3307 or an attempt is made to display a non-existent function with @option{-f}.
3312 echo [-neE] [@var{arg} @dots{}]
3314 Output the @var{arg}s, separated by spaces, terminated with a
3316 The return status is always 0.
3317 If @option{-n} is specified, the trailing newline is suppressed.
3318 If the @option{-e} option is given, interpretation of the following
3319 backslash-escaped characters is enabled.
3320 The @option{-E} option disables the interpretation of these escape characters,
3321 even on systems where they are interpreted by default.
3322 The @code{xpg_echo} shell option may be used to
3323 dynamically determine whether or not @code{echo} expands these
3324 escape characters by default.
3325 @code{echo} interprets the following escape sequences:
3332 suppress trailing newline
3348 the eight-bit character whose value is the octal value @var{nnn}
3349 (zero to three octal digits)
3351 the eight-bit character whose value is the octal value @var{nnn}
3352 (one to three octal digits)
3354 the eight-bit character whose value is the hexadecimal value @var{HH}
3355 (one or two hex digits)
3361 enable [-n] [-p] [-f @var{filename}] [-ads] [@var{name} @dots{}]
3363 Enable and disable builtin shell commands.
3364 Disabling a builtin allows a disk command which has the same name
3365 as a shell builtin to be executed without specifying a full pathname,
3366 even though the shell normally searches for builtins before disk commands.
3367 If @option{-n} is used, the @var{name}s become disabled. Otherwise
3368 @var{name}s are enabled. For example, to use the @code{test} binary
3369 found via @env{$PATH} instead of the shell builtin version, type
3370 @samp{enable -n test}.
3372 If the @option{-p} option is supplied, or no @var{name} arguments appear,
3373 a list of shell builtins is printed. With no other arguments, the list
3374 consists of all enabled shell builtins.
3375 The @option{-a} option means to list
3376 each builtin with an indication of whether or not it is enabled.
3378 The @option{-f} option means to load the new builtin command @var{name}
3379 from shared object @var{filename}, on systems that support dynamic loading.
3380 The @option{-d} option will delete a builtin loaded with @option{-f}.
3382 If there are no options, a list of the shell builtins is displayed.
3383 The @option{-s} option restricts @code{enable} to the @sc{posix} special
3384 builtins. If @option{-s} is used with @option{-f}, the new builtin becomes
3385 a special builtin (@pxref{Special Builtins}).
3387 The return status is zero unless a @var{name} is not a shell builtin
3388 or there is an error loading a new builtin from a shared object.
3393 help [-s] [@var{pattern}]
3395 Display helpful information about builtin commands.
3396 If @var{pattern} is specified, @code{help} gives detailed help
3397 on all commands matching @var{pattern}, otherwise a list of
3398 the builtins is printed.
3399 The @option{-s} option restricts the information displayed to a short
3401 The return status is zero unless no command matches @var{pattern}.
3406 let @var{expression} [@var{expression}]
3408 The @code{let} builtin allows arithmetic to be performed on shell
3409 variables. Each @var{expression} is evaluated according to the
3410 rules given below in @ref{Shell Arithmetic}. If the
3411 last @var{expression} evaluates to 0, @code{let} returns 1;
3412 otherwise 0 is returned.
3417 local [@var{option}] @var{name}[=@var{value}] @dots{}
3419 For each argument, a local variable named @var{name} is created,
3420 and assigned @var{value}.
3421 The @var{option} can be any of the options accepted by @code{declare}.
3422 @code{local} can only be used within a function; it makes the variable
3423 @var{name} have a visible scope restricted to that function and its
3424 children. The return status is zero unless @code{local} is used outside
3425 a function, an invalid @var{name} is supplied, or @var{name} is a
3433 Exit a login shell, returning a status of @var{n} to the shell's
3439 @code{printf} @var{format} [@var{arguments}]
3441 Write the formatted @var{arguments} to the standard output under the
3442 control of the @var{format}.
3443 The @var{format} is a character string which contains three types of objects:
3444 plain characters, which are simply copied to standard output, character
3445 escape sequences, which are converted and copied to the standard output, and
3446 format specifications, each of which causes printing of the next successive
3448 In addition to the standard @code{printf(1)} formats, @samp{%b} causes
3449 @code{printf} to expand backslash escape sequences in the corresponding
3451 (except that @samp{\c} terminates output, backslashes in
3452 @samp{\'}, @samp{\"}, and @samp{\?} are not removed, and octal escapes
3453 beginning with @samp{\0} may contain up to four digits),
3454 and @samp{%q} causes @code{printf} to output the
3455 corresponding @var{argument} in a format that can be reused as shell input.
3457 The @var{format} is reused as necessary to consume all of the @var{arguments}.
3458 If the @var{format} requires more @var{arguments} than are supplied, the
3459 extra format specifications behave as if a zero value or null string, as
3460 appropriate, had been supplied. The return value is zero on success,
3461 non-zero on failure.
3466 read [-ers] [-a @var{aname}] [-d @var{delim}] [-n @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}]
3468 One line is read from the standard input, or from the file descriptor
3469 @var{fd} supplied as an argument to the @option{-u} option, and the first word
3470 is assigned to the first @var{name}, the second word to the second @var{name},
3471 and so on, with leftover words and their intervening separators assigned
3472 to the last @var{name}.
3473 If there are fewer words read from the input stream than names,
3474 the remaining names are assigned empty values.
3475 The characters in the value of the @env{IFS} variable
3476 are used to split the line into words.
3477 The backslash character @samp{\} may be used to remove any special
3478 meaning for the next character read and for line continuation.
3479 If no names are supplied, the line read is assigned to the
3480 variable @env{REPLY}.
3481 The return code is zero, unless end-of-file is encountered, @code{read}
3482 times out, or an invalid file descriptor is supplied as the argument to
3484 Options, if supplied, have the following meanings:
3487 @item -a @var{aname}
3488 The words are assigned to sequential indices of the array variable
3489 @var{aname}, starting at 0.
3490 All elements are removed from @var{aname} before the assignment.
3491 Other @var{name} arguments are ignored.
3493 @item -d @var{delim}
3494 The first character of @var{delim} is used to terminate the input line,
3495 rather than newline.
3498 Readline (@pxref{Command Line Editing}) is used to obtain the line.
3500 @item -n @var{nchars}
3501 @code{read} returns after reading @var{nchars} characters rather than
3502 waiting for a complete line of input.
3504 @item -p @var{prompt}
3505 Display @var{prompt}, without a trailing newline, before attempting
3507 The prompt is displayed only if input is coming from a terminal.
3510 If this option is given, backslash does not act as an escape character.
3511 The backslash is considered to be part of the line.
3512 In particular, a backslash-newline pair may not be used as a line
3516 Silent mode. If input is coming from a terminal, characters are
3519 @item -t @var{timeout}
3520 Cause @code{read} to time out and return failure if a complete line of
3521 input is not read within @var{timeout} seconds.
3522 This option has no effect if @code{read} is not reading input from the
3526 Read input from file descriptor @var{fd}.
3533 shopt [-pqsu] [-o] [@var{optname} @dots{}]
3535 Toggle the values of variables controlling optional shell behavior.
3536 With no options, or with the @option{-p} option, a list of all settable
3537 options is displayed, with an indication of whether or not each is set.
3538 The @option{-p} option causes output to be displayed in a form that
3539 may be reused as input.
3540 Other options have the following meanings:
3544 Enable (set) each @var{optname}.
3547 Disable (unset) each @var{optname}.
3550 Suppresses normal output; the return status
3551 indicates whether the @var{optname} is set or unset.
3552 If multiple @var{optname} arguments are given with @option{-q},
3553 the return status is zero if all @var{optnames} are enabled;
3557 Restricts the values of
3558 @var{optname} to be those defined for the @option{-o} option to the
3559 @code{set} builtin (@pxref{The Set Builtin}).
3562 If either @option{-s} or @option{-u}
3563 is used with no @var{optname} arguments, the display is limited to
3564 those options which are set or unset, respectively.
3566 Unless otherwise noted, the @code{shopt} options are disabled (off)
3569 The return status when listing options is zero if all @var{optnames}
3570 are enabled, non-zero otherwise. When setting or unsetting options,
3571 the return status is zero unless an @var{optname} is not a valid shell
3574 The list of @code{shopt} options is:
3577 If this is set, an argument to the @code{cd}
3578 builtin command that
3579 is not a directory is assumed to be the name of a variable whose
3580 value is the directory to change to.
3583 If set, minor errors in the spelling of a directory component in a
3584 @code{cd} command will be corrected.
3585 The errors checked for are transposed characters,
3586 a missing character, and a character too many.
3587 If a correction is found, the corrected path is printed,
3588 and the command proceeds.
3589 This option is only used by interactive shells.
3592 If this is set, Bash checks that a command found in the hash
3593 table exists before trying to execute it. If a hashed command no
3594 longer exists, a normal path search is performed.
3597 If set, Bash checks the window size after each command
3598 and, if necessary, updates the values of
3599 @env{LINES} and @env{COLUMNS}.
3603 attempts to save all lines of a multiple-line
3604 command in the same history entry. This allows
3605 easy re-editing of multi-line commands.
3608 If set, Bash includes filenames beginning with a `.' in
3609 the results of filename expansion.
3612 If this is set, a non-interactive shell will not exit if
3613 it cannot execute the file specified as an argument to the @code{exec}
3614 builtin command. An interactive shell does not exit if @code{exec}
3617 @item expand_aliases
3618 If set, aliases are expanded as described below under Aliases,
3620 This option is enabled by default for interactive shells.
3623 If set, behavior intended for use by debuggers is enabled:
3627 The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins})
3628 displays the source file name and line number corresponding to each function
3629 name supplied as an argument.
3632 If the command run by the @code{DEBUG} trap returns a non-zero value, the
3633 next command is skipped and not executed.
3636 If the command run by the @code{DEBUG} trap returns a value of 2, and the
3637 shell is executing in a subroutine (a shell function or a shell script
3638 executed by the @code{.} or @code{source} builtins), a call to
3639 @code{return} is simulated.
3643 If set, the extended pattern matching features described above
3644 (@pxref{Pattern Matching}) are enabled.
3647 If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is
3648 performed within @code{$@{@var{parameter}@}} expansions
3649 enclosed in double quotes. This option is enabled by default.
3652 If set, patterns which fail to match filenames during pathname expansion
3653 result in an expansion error.
3656 If set, the suffixes specified by the @env{FIGNORE} shell variable
3657 cause words to be ignored when performing word completion even if
3658 the ignored words are the only possible completions.
3659 @xref{Bash Variables}, for a description of @env{FIGNORE}.
3660 This option is enabled by default.
3663 If set, shell error messages are written in the standard @sc{gnu} error
3667 If set, the history list is appended to the file named by the value
3668 of the @env{HISTFILE}
3669 variable when the shell exits, rather than overwriting the file.
3672 If set, and Readline
3673 is being used, a user is given the opportunity to re-edit a
3674 failed history substitution.
3677 If set, and Readline
3678 is being used, the results of history substitution are not immediately
3679 passed to the shell parser. Instead, the resulting line is loaded into
3680 the Readline editing buffer, allowing further modification.
3683 If set, and Readline is being used, Bash will attempt to perform
3684 hostname completion when a word containing a @samp{@@} is being
3685 completed (@pxref{Commands For Completion}). This option is enabled
3689 If set, Bash will send @code{SIGHUP} to all jobs when an interactive
3690 login shell exits (@pxref{Signals}).
3692 @item interactive_comments
3693 Allow a word beginning with @samp{#}
3694 to cause that word and all remaining characters on that
3695 line to be ignored in an interactive shell.
3696 This option is enabled by default.
3699 If enabled, and the @code{cmdhist}
3700 option is enabled, multi-line commands are saved to the history with
3701 embedded newlines rather than using semicolon separators where possible.
3704 The shell sets this option if it is started as a login shell
3705 (@pxref{Invoking Bash}).
3706 The value may not be changed.
3709 If set, and a file that Bash is checking for mail has been
3710 accessed since the last time it was checked, the message
3711 @code{"The mail in @var{mailfile} has been read"} is displayed.
3713 @item no_empty_cmd_completion
3714 If set, and Readline is being used, Bash will not attempt to search
3715 the @env{PATH} for possible completions when completion is attempted
3719 If set, Bash matches filenames in a case-insensitive fashion when
3720 performing filename expansion.
3723 If set, Bash allows filename patterns which match no
3724 files to expand to a null string, rather than themselves.
3727 If set, the programmable completion facilities
3728 (@pxref{Programmable Completion}) are enabled.
3729 This option is enabled by default.
3732 If set, prompt strings undergo
3733 parameter expansion, command substitution, arithmetic
3734 expansion, and quote removal after being expanded
3735 as described below (@pxref{Printing a Prompt}).
3736 This option is enabled by default.
3738 @item restricted_shell
3739 The shell sets this option if it is started in restricted mode
3740 (@pxref{The Restricted Shell}).
3741 The value may not be changed.
3742 This is not reset when the startup files are executed, allowing
3743 the startup files to discover whether or not a shell is restricted.
3746 If this is set, the @code{shift}
3747 builtin prints an error message when the shift count exceeds the
3748 number of positional parameters.
3751 If set, the @code{source} builtin uses the value of @env{PATH}
3752 to find the directory containing the file supplied as an argument.
3753 This option is enabled by default.
3756 If set, the @code{echo} builtin expands backslash-escape sequences
3762 The return status when listing options is zero if all @var{optnames}
3763 are enabled, non-zero otherwise.
3764 When setting or unsetting options, the return status is zero unless an
3765 @var{optname} is not a valid shell option.
3770 source @var{filename}
3772 A synonym for @code{.} (@pxref{Bourne Shell Builtins}).
3777 type [-afptP] [@var{name} @dots{}]
3779 For each @var{name}, indicate how it would be interpreted if used as a
3782 If the @option{-t} option is used, @code{type} prints a single word
3783 which is one of @samp{alias}, @samp{function}, @samp{builtin},
3784 @samp{file} or @samp{keyword},
3785 if @var{name} is an alias, shell function, shell builtin,
3786 disk file, or shell reserved word, respectively.
3787 If the @var{name} is not found, then nothing is printed, and
3788 @code{type} returns a failure status.
3790 If the @option{-p} option is used, @code{type} either returns the name
3791 of the disk file that would be executed, or nothing if @option{-t}
3792 would not return @samp{file}.
3794 The @option{-P} option forces a path search for each @var{name}, even if
3795 @option{-t} would not return @samp{file}.
3797 If a command is hashed, @option{-p} and @option{-P} print the hashed value,
3798 not necessarily the file that appears first in @code{$PATH}.
3800 If the @option{-a} option is used, @code{type} returns all of the places
3801 that contain an executable named @var{file}.
3802 This includes aliases and functions, if and only if the @option{-p} option
3805 If the @option{-f} option is used, @code{type} does not attempt to find
3806 shell functions, as with the @code{command} builtin.
3808 The return status is zero if any of the @var{names} are found, non-zero
3814 typeset [-afFrxi] [-p] [@var{name}[=@var{value}] @dots{}]
3816 The @code{typeset} command is supplied for compatibility with the Korn
3817 shell; however, it has been deprecated in favor of the @code{declare}
3823 ulimit [-acdflmnpstuvSH] [@var{limit}]
3825 @code{ulimit} provides control over the resources available to processes
3826 started by the shell, on systems that allow such control. If an
3827 option is given, it is interpreted as follows:
3830 Change and report the soft limit associated with a resource.
3833 Change and report the hard limit associated with a resource.
3836 All current limits are reported.
3839 The maximum size of core files created.
3842 The maximum size of a process's data segment.
3845 The maximum size of files created by the shell.
3848 The maximum size that may be locked into memory.
3851 The maximum resident set size.
3854 The maximum number of open file descriptors.
3857 The pipe buffer size.
3860 The maximum stack size.
3863 The maximum amount of cpu time in seconds.
3866 The maximum number of processes available to a single user.
3869 The maximum amount of virtual memory available to the process.
3873 If @var{limit} is given, it is the new value of the specified resource;
3874 the special @var{limit} values @code{hard}, @code{soft}, and
3875 @code{unlimited} stand for the current hard limit, the current soft limit,
3876 and no limit, respectively.
3877 Otherwise, the current value of the soft limit for the specified resource
3878 is printed, unless the @option{-H} option is supplied.
3879 When setting new limits, if neither @option{-H} nor @option{-S} is supplied,
3880 both the hard and soft limits are set.
3881 If no option is given, then @option{-f} is assumed. Values are in 1024-byte
3882 increments, except for @option{-t}, which is in seconds, @option{-p},
3883 which is in units of 512-byte blocks, and @option{-n} and @option{-u}, which
3884 are unscaled values.
3886 The return status is zero unless an invalid option or argument is supplied,
3887 or an error occurs while setting a new limit.
3892 unalias [-a] [@var{name} @dots{} ]
3895 Remove each @var{name} from the list of aliases. If @option{-a} is
3896 supplied, all aliases are removed.
3897 Aliases are described in @ref{Aliases}.
3901 @node The Set Builtin
3902 @section The Set Builtin
3904 This builtin is so complicated that it deserves its own section.
3910 set [--abefhkmnptuvxBCHP] [-o @var{option}] [@var{argument} @dots{}]
3913 If no options or arguments are supplied, @code{set} displays the names
3914 and values of all shell variables and functions, sorted according to the
3915 current locale, in a format that may be reused as input.
3917 When options are supplied, they set or unset shell attributes.
3918 Options, if specified, have the following meanings:
3922 Mark variables and function which are modified or created for export
3923 to the environment of subsequent commands.
3926 Cause the status of terminated background jobs to be reported
3927 immediately, rather than before printing the next primary prompt.
3930 Exit immediately if a simple command (@pxref{Simple Commands}) exits
3931 with a non-zero status, unless the command that fails is part of the
3932 command list immediately following a @code{while} or @code{until}
3933 keyword, part of the test in an @code{if} statement,
3934 part of a @code{&&} or @code{||} list, or if the command's return
3935 status is being inverted using @code{!}.
3936 A trap on @code{ERR}, if set, is executed before the shell exits.
3939 Disable file name generation (globbing).
3942 Locate and remember (hash) commands as they are looked up for execution.
3943 This option is enabled by default.
3946 All arguments in the form of assignment statements are placed
3947 in the environment for a command, not just those that precede
3951 Job control is enabled (@pxref{Job Control}).
3954 Read commands but do not execute them; this may be used to check a
3955 script for syntax errors.
3956 This option is ignored by interactive shells.
3958 @item -o @var{option-name}
3960 Set the option corresponding to @var{option-name}:
3970 Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}).
3988 Enable command history, as described in @ref{Bash History Facilities}.
3989 This option is on by default in interactive shells.
3992 An interactive shell will not exit upon reading EOF.
4025 If set, the return value of a pipeline is the value of the last
4026 (rightmost) command to exit with a non-zero status, or zero if all
4027 commands in the pipeline exit successfully.
4028 This option is disabled by default.
4031 Change the behavior of Bash where the default operation differs
4032 from the @sc{posix} 1003.2 standard to match the standard
4033 (@pxref{Bash POSIX Mode}).
4034 This is intended to make Bash behave as a strict superset of that
4044 Use a @code{vi}-style line editing interface.
4051 Turn on privileged mode.
4052 In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not
4053 processed, shell functions are not inherited from the environment,
4054 and the @env{SHELLOPTS} variable, if it appears in the environment,
4056 If the shell is started with the effective user (group) id not equal to the
4057 real user (group) id, and the @code{-p} option is not supplied, these actions
4058 are taken and the effective user id is set to the real user id.
4059 If the @code{-p} option is supplied at startup, the effective user id is
4061 Turning this option off causes the effective user
4062 and group ids to be set to the real user and group ids.
4065 Exit after reading and executing one command.
4068 Treat unset variables as an error when performing parameter expansion.
4069 An error message will be written to the standard error, and a non-interactive
4073 Print shell input lines as they are read.
4076 Print a trace of simple commands, \fBfor\fP commands, \fBcase\fP
4077 commands, \fBselect\fP commands, and arithmetic \fBfor\fP commands
4078 and their arguments or associated word lists after they are
4079 expanded and before they are executed. The value of the @env{PS4}
4080 variable is expanded and the resultant value is printed before
4081 the command and its expanded arguments.
4084 The shell will perform brace expansion (@pxref{Brace Expansion}).
4085 This option is on by default.
4088 Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>}
4089 from overwriting existing files.
4092 If set, any trap on @code{ERR} is inherited by shell functions, command
4093 substitutions, and commands executed in a subshell environment.
4094 The @code{ERR} trap is normally not inherited in such cases.
4097 Enable @samp{!} style history substitution (@pxref{History Interaction}).
4098 This option is on by default for interactive shells.
4101 If set, do not follow symbolic links when performing commands such as
4102 @code{cd} which change the current directory. The physical directory
4103 is used instead. By default, Bash follows
4104 the logical chain of directories when performing commands
4105 which change the current directory.
4107 For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys}
4110 $ cd /usr/sys; echo $PWD
4117 If @code{set -P} is on, then:
4119 $ cd /usr/sys; echo $PWD
4126 If set, any trap on @code{DEBUG} is inherited by shell functions, command
4127 substitutions, and commands executed in a subshell environment.
4128 The @code{DEBUG} trap is normally not inherited in such cases.
4131 If no arguments follow this option, then the positional parameters are
4132 unset. Otherwise, the positional parameters are set to the
4133 @var{arguments}, even if some of them begin with a @samp{-}.
4136 Signal the end of options, cause all remaining @var{arguments}
4137 to be assigned to the positional parameters. The @option{-x}
4138 and @option{-v} options are turned off.
4139 If there are no arguments, the positional parameters remain unchanged.
4142 Using @samp{+} rather than @samp{-} causes these options to be
4143 turned off. The options can also be used upon invocation of the
4144 shell. The current set of options may be found in @code{$-}.
4146 The remaining N @var{arguments} are positional parameters and are
4147 assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}.
4148 The special parameter @code{#} is set to N.
4150 The return status is always zero unless an invalid option is supplied.
4153 @node Special Builtins
4154 @section Special Builtins
4155 @cindex special builtin
4157 For historical reasons, the @sc{posix} 1003.2 standard has classified
4158 several builtin commands as @emph{special}.
4159 When Bash is executing in @sc{posix} mode, the special builtins
4160 differ from other builtin commands in three respects:
4164 Special builtins are found before shell functions during command lookup.
4167 If a special builtin returns an error status, a non-interactive shell exits.
4170 Assignment statements preceding the command stay in effect in the shell
4171 environment after the command completes.
4174 When Bash is not executing in @sc{posix} mode, these builtins behave no
4175 differently than the rest of the Bash builtin commands.
4176 The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}.
4178 These are the @sc{posix} special builtins:
4180 @w{break : . continue eval exec exit export readonly return set}
4181 @w{shift trap unset}
4184 @node Shell Variables
4185 @chapter Shell Variables
4188 * Bourne Shell Variables:: Variables which Bash uses in the same way
4189 as the Bourne Shell.
4190 * Bash Variables:: List of variables that exist in Bash.
4193 This chapter describes the shell variables that Bash uses.
4194 Bash automatically assigns default values to a number of variables.
4196 @node Bourne Shell Variables
4197 @section Bourne Shell Variables
4199 Bash uses certain shell variables in the same way as the Bourne shell.
4200 In some cases, Bash assigns a default value to the variable.
4205 A colon-separated list of directories used as a search path for
4206 the @code{cd} builtin command.
4209 The current user's home directory; the default for the @code{cd} builtin
4211 The value of this variable is also used by tilde expansion
4212 (@pxref{Tilde Expansion}).
4215 A list of characters that separate fields; used when the shell splits
4216 words as part of expansion.
4219 If this parameter is set to a filename and the @env{MAILPATH} variable
4220 is not set, Bash informs the user of the arrival of mail in
4224 A colon-separated list of filenames which the shell periodically checks
4226 Each list entry can specify the message that is printed when new mail
4227 arrives in the mail file by separating the file name from the message with
4229 When used in the text of the message, @code{$_} expands to the name of
4230 the current mail file.
4233 The value of the last option argument processed by the @code{getopts} builtin.
4236 The index of the last option argument processed by the @code{getopts} builtin.
4239 A colon-separated list of directories in which the shell looks for
4241 A zero-length (null) directory name in the value of @code{PATH} indicates the
4243 A null directory name may appear as two adjacent colons, or as an initial
4248 The primary prompt string. The default value is @samp{\s-\v\$ }.
4249 @xref{Printing a Prompt}, for the complete list of escape
4250 sequences that are expanded before @env{PS1} is displayed.
4253 The secondary prompt string. The default value is @samp{> }.
4257 @node Bash Variables
4258 @section Bash Variables
4260 These variables are set or used by Bash, but other shells
4261 do not normally treat them specially.
4263 A few variables used by Bash are described in different chapters:
4264 variables for controlling the job control facilities
4265 (@pxref{Job Control Variables}).
4270 The full pathname used to execute the current instance of Bash.
4273 An array variable whose values are the number of parameters in each
4274 frame of the current bash execution call stack. The number of
4275 parameters to the current subroutine (shell function or script executed
4276 with @code{.} or @code{source}) is at the top of the stack. When a
4277 subroutine is executed, the number of parameters passed is pushed onto
4281 An array variable containing all of the parameters in the current bash
4282 execution call stack. The final parameter of the last subroutine call
4283 is at the top of the stack; the first parameter of the initial call is
4284 at the bottom. When a subroutine is executed, the parameters supplied
4285 are pushed onto @code{BASH_ARGV}.
4288 The command currently being executed or about to be executed, unless the
4289 shell is executing a command as the result of a trap,
4290 in which case it is the command executing at the time of the trap.
4293 If this variable is set when Bash is invoked to execute a shell
4294 script, its value is expanded and used as the name of a startup file
4295 to read before executing the script. @xref{Bash Startup Files}.
4297 @item BASH_EXECUTION_STRING
4298 The command argument to the @option{-c} invocation option.
4301 An array variable whose members are the line numbers in source files
4302 corresponding to each member of @var{FUNCNAME}.
4303 @code{$@{BASH_LINENO[$i]@}} is the line number in the source file where
4304 @code{$@{FUNCNAME[$i]@}} was called.
4305 The corresponding source file name is @code{$@{BASH_SOURCE[$i]@}}.
4306 Use @code{LINENO} to obtain the current line number.
4309 An array variable whose members are assigned by the @samp{=~} binary
4310 operator to the @code{[[} conditional command
4311 (@pxref{Conditional Constructs}).
4312 The element with index 0 is the portion of the string
4313 matching the entire regular expression.
4314 The element with index @var{n} is the portion of the
4315 string matching the @var{n}th parenthesized subexpression.
4316 This variable is read-only.
4319 An array variable whose members are the source filenames corresponding
4320 to the elements in the @code{FUNCNAME} array variable.
4323 Incremented by one each time a subshell or subshell environment is spawned.
4324 The initial value is 0.
4327 A readonly array variable (@pxref{Arrays})
4328 whose members hold version information for this instance of Bash.
4329 The values assigned to the array members are as follows:
4333 @item BASH_VERSINFO[0]
4334 The major version number (the @var{release}).
4336 @item BASH_VERSINFO[1]
4337 The minor version number (the @var{version}).
4339 @item BASH_VERSINFO[2]
4342 @item BASH_VERSINFO[3]
4345 @item BASH_VERSINFO[4]
4346 The release status (e.g., @var{beta1}).
4348 @item BASH_VERSINFO[5]
4349 The value of @env{MACHTYPE}.
4354 The version number of the current instance of Bash.
4357 Used by the @code{select} builtin command to determine the terminal width
4358 when printing selection lists. Automatically set upon receipt of a
4362 An index into @env{$@{COMP_WORDS@}} of the word containing the current
4364 This variable is available only in shell functions invoked by the
4365 programmable completion facilities (@pxref{Programmable Completion}).
4368 The current command line.
4369 This variable is available only in shell functions and external
4370 commands invoked by the
4371 programmable completion facilities (@pxref{Programmable Completion}).
4374 The index of the current cursor position relative to the beginning of
4375 the current command.
4376 If the current cursor position is at the end of the current command,
4377 the value of this variable is equal to @code{$@{#COMP_LINE@}}.
4378 This variable is available only in shell functions and external
4379 commands invoked by the
4380 programmable completion facilities (@pxref{Programmable Completion}).
4382 @item COMP_WORDBREAKS
4383 The set of characters that the Readline library treats as word
4384 separators when performing word completion.
4385 If @code{COMP_WORDBREAKS} is unset, it loses its special properties,
4386 even if it is subsequently reset.
4389 An array variable consisting of the individual
4390 words in the current command line.
4391 This variable is available only in shell functions invoked by the
4392 programmable completion facilities (@pxref{Programmable Completion}).
4395 An array variable from which Bash reads the possible completions
4396 generated by a shell function invoked by the programmable completion
4397 facility (@pxref{Programmable Completion}).
4400 An array variable containing the current contents of the directory stack.
4401 Directories appear in the stack in the order they are displayed by the
4402 @code{dirs} builtin.
4403 Assigning to members of this array variable may be used to modify
4404 directories already in the stack, but the @code{pushd} and @code{popd}
4405 builtins must be used to add and remove directories.
4406 Assignment to this variable will not change the current directory.
4407 If @env{DIRSTACK} is unset, it loses its special properties, even if
4408 it is subsequently reset.
4411 If Bash finds this variable in the environment when the shell
4412 starts with value @samp{t}, it assumes that the shell is running in an
4413 emacs shell buffer and disables line editing.
4416 The numeric effective user id of the current user. This variable
4420 The editor used as a default by the @option{-e} option to the @code{fc}
4424 A colon-separated list of suffixes to ignore when performing
4425 filename completion.
4426 A file name whose suffix matches one of the entries in
4428 is excluded from the list of matched file names. A sample
4429 value is @samp{.o:~}
4432 An array variable containing the names of all shell functions
4433 currently in the execution call stack.
4434 The element with index 0 is the name of any currently-executing
4436 The bottom-most element is "main".
4437 This variable exists only when a shell function is executing.
4438 Assignments to @env{FUNCNAME} have no effect and return an error status.
4439 If @env{FUNCNAME} is unset, it loses its special properties, even if
4440 it is subsequently reset.
4443 A colon-separated list of patterns defining the set of filenames to
4444 be ignored by filename expansion.
4445 If a filename matched by a filename expansion pattern also matches one
4446 of the patterns in @env{GLOBIGNORE}, it is removed from the list
4450 An array variable containing the list of groups of which the current
4452 Assignments to @env{GROUPS} have no effect and return an error status.
4453 If @env{GROUPS} is unset, it loses its special properties, even if it is
4457 Up to three characters which control history expansion, quick
4458 substitution, and tokenization (@pxref{History Interaction}).
4459 The first character is the
4460 @var{history expansion} character, that is, the character which signifies the
4461 start of a history expansion, normally @samp{!}. The second character is the
4462 character which signifies `quick substitution' when seen as the first
4463 character on a line, normally @samp{^}. The optional third character is the
4464 character which indicates that the remainder of the line is a comment when
4465 found as the first character of a word, usually @samp{#}. The history
4466 comment character causes history substitution to be skipped for the
4467 remaining words on the line. It does not necessarily cause the shell
4468 parser to treat the rest of the line as a comment.
4471 The history number, or index in the history list, of the current
4472 command. If @env{HISTCMD} is unset, it loses its special properties,
4473 even if it is subsequently reset.
4476 A colon-separated list of values controlling how commands are saved on
4478 If the list of values includes @samp{ignorespace}, lines which begin
4479 with a space character are not saved in the history list.
4480 A value of @samp{ignoredups} causes lines which match the previous
4481 history entry to not be saved.
4482 A value of @samp{ignoreboth} is shorthand for
4483 @samp{ignorespace} and @samp{ignoredups}.
4484 A value of @samp{erasedups} causes all previous lines matching the
4485 current line to be removed from the history list before that line
4487 Any value not in the above list is ignored.
4488 If @env{HISTCONTROL} is unset, or does not include a valid value,
4489 all lines read by the shell parser are saved on the history list,
4490 subject to the value of @env{HISTIGNORE}.
4491 The second and subsequent lines of a multi-line compound command are
4492 not tested, and are added to the history regardless of the value of
4496 The name of the file to which the command history is saved. The
4497 default value is @file{~/.bash_history}.
4500 The maximum number of lines contained in the history file. When this
4501 variable is assigned a value, the history file is truncated, if
4502 necessary, to contain no more than that number of lines.
4503 The history file is also truncated to this size after
4504 writing it when an interactive shell exits.
4505 The default value is 500.
4508 A colon-separated list of patterns used to decide which command
4509 lines should be saved on the history list. Each pattern is
4510 anchored at the beginning of the line and must match the complete
4511 line (no implicit @samp{*} is appended). Each pattern is tested
4512 against the line after the checks specified by @env{HISTCONTROL}
4513 are applied. In addition to the normal shell pattern matching
4514 characters, @samp{&} matches the previous history line. @samp{&}
4515 may be escaped using a backslash; the backslash is removed
4516 before attempting a match.
4517 The second and subsequent lines of a multi-line compound command are
4518 not tested, and are added to the history regardless of the value of
4521 @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A
4522 pattern of @samp{&} is identical to @code{ignoredups}, and a
4523 pattern of @samp{[ ]*} is identical to @code{ignorespace}.
4524 Combining these two patterns, separating them with a colon,
4525 provides the functionality of @code{ignoreboth}.
4528 The maximum number of commands to remember on the history list.
4529 The default value is 500.
4531 @item HISTTIMEFORMAT
4532 If this variable is set and not null, its value is used as a format string
4533 for @var{strftime} to print the time stamp associated with each history
4534 entry displayed by the @code{history} builtin.
4535 If this variable is set, time stamps are written to the history file so
4536 they may be preserved across shell sessions.
4539 Contains the name of a file in the same format as @file{/etc/hosts} that
4540 should be read when the shell needs to complete a hostname.
4541 The list of possible hostname completions may be changed while the shell
4543 the next time hostname completion is attempted after the
4544 value is changed, Bash adds the contents of the new file to the
4546 If @env{HOSTFILE} is set, but has no value, Bash attempts to read
4547 @file{/etc/hosts} to obtain the list of possible hostname completions.
4548 When @env{HOSTFILE} is unset, the hostname list is cleared.
4551 The name of the current host.
4554 A string describing the machine Bash is running on.
4557 Controls the action of the shell on receipt of an @code{EOF} character
4558 as the sole input. If set, the value denotes the number
4559 of consecutive @code{EOF} characters that can be read as the
4560 first character on an input line
4561 before the shell will exit. If the variable exists but does not
4562 have a numeric value (or has no value) then the default is 10.
4563 If the variable does not exist, then @code{EOF} signifies the end of
4564 input to the shell. This is only in effect for interactive shells.
4567 The name of the Readline initialization file, overriding the default
4568 of @file{~/.inputrc}.
4571 Used to determine the locale category for any category not specifically
4572 selected with a variable starting with @code{LC_}.
4575 This variable overrides the value of @env{LANG} and any other
4576 @code{LC_} variable specifying a locale category.
4579 This variable determines the collation order used when sorting the
4580 results of filename expansion, and
4581 determines the behavior of range expressions, equivalence classes,
4582 and collating sequences within filename expansion and pattern matching
4583 (@pxref{Filename Expansion}).
4586 This variable determines the interpretation of characters and the
4587 behavior of character classes within filename expansion and pattern
4588 matching (@pxref{Filename Expansion}).
4591 This variable determines the locale used to translate double-quoted
4592 strings preceded by a @samp{$} (@pxref{Locale Translation}).
4595 This variable determines the locale category used for number formatting.
4598 The line number in the script or shell function currently executing.
4601 Used by the @code{select} builtin command to determine the column length
4602 for printing selection lists. Automatically set upon receipt of a
4606 A string that fully describes the system type on which Bash
4607 is executing, in the standard @sc{gnu} @var{cpu-company-system} format.
4610 How often (in seconds) that the shell should check for mail in the
4611 files specified in the @env{MAILPATH} or @env{MAIL} variables.
4612 The default is 60 seconds. When it is time to check
4613 for mail, the shell does so before displaying the primary prompt.
4614 If this variable is unset, or set to a value that is not a number
4615 greater than or equal to zero, the shell disables mail checking.
4618 The previous working directory as set by the @code{cd} builtin.
4621 If set to the value 1, Bash displays error messages
4622 generated by the @code{getopts} builtin command.
4625 A string describing the operating system Bash is running on.
4628 An array variable (@pxref{Arrays})
4629 containing a list of exit status values from the processes
4630 in the most-recently-executed foreground pipeline (which may
4631 contain only a single command).
4633 @item POSIXLY_CORRECT
4634 If this variable is in the environment when @code{bash} starts, the shell
4635 enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the
4636 startup files, as if the @option{--posix} invocation option had been supplied.
4637 If it is set while the shell is running, @code{bash} enables @sc{posix} mode,
4646 The process @sc{id} of the shell's parent process. This variable
4649 @item PROMPT_COMMAND
4650 If set, the value is interpreted as a command to execute
4651 before the printing of each primary prompt (@env{$PS1}).
4654 The value of this variable is used as the prompt for the
4655 @code{select} command. If this variable is not set, the
4656 @code{select} command prompts with @samp{#? }
4659 The value is the prompt printed before the command line is echoed
4660 when the @option{-x} option is set (@pxref{The Set Builtin}).
4661 The first character of @env{PS4} is replicated multiple times, as
4662 necessary, to indicate multiple levels of indirection.
4663 The default is @samp{+ }.
4666 The current working directory as set by the @code{cd} builtin.
4669 Each time this parameter is referenced, a random integer
4670 between 0 and 32767 is generated. Assigning a value to this
4671 variable seeds the random number generator.
4674 The default variable for the @code{read} builtin.
4677 This variable expands to the number of seconds since the
4678 shell was started. Assignment to this variable resets
4679 the count to the value assigned, and the expanded value
4680 becomes the value assigned plus the number of seconds
4681 since the assignment.
4684 The full pathname to the shell is kept in this environment variable.
4685 If it is not set when the shell starts,
4686 Bash assigns to it the full pathname of the current user's login shell.
4689 A colon-separated list of enabled shell options. Each word in
4690 the list is a valid argument for the @option{-o} option to the
4691 @code{set} builtin command (@pxref{The Set Builtin}).
4692 The options appearing in @env{SHELLOPTS} are those reported
4693 as @samp{on} by @samp{set -o}.
4694 If this variable is in the environment when Bash
4695 starts up, each shell option in the list will be enabled before
4696 reading any startup files. This variable is readonly.
4699 Incremented by one each time a new instance of Bash is started. This is
4700 intended to be a count of how deeply your Bash shells are nested.
4703 The value of this parameter is used as a format string specifying
4704 how the timing information for pipelines prefixed with the @code{time}
4705 reserved word should be displayed.
4706 The @samp{%} character introduces an
4707 escape sequence that is expanded to a time value or other
4709 The escape sequences and their meanings are as
4710 follows; the braces denote optional portions.
4717 @item %[@var{p}][l]R
4718 The elapsed time in seconds.
4720 @item %[@var{p}][l]U
4721 The number of CPU seconds spent in user mode.
4723 @item %[@var{p}][l]S
4724 The number of CPU seconds spent in system mode.
4727 The CPU percentage, computed as (%U + %S) / %R.
4730 The optional @var{p} is a digit specifying the precision, the number of
4731 fractional digits after a decimal point.
4732 A value of 0 causes no decimal point or fraction to be output.
4733 At most three places after the decimal point may be specified; values
4734 of @var{p} greater than 3 are changed to 3.
4735 If @var{p} is not specified, the value 3 is used.
4737 The optional @code{l} specifies a longer format, including minutes, of
4738 the form @var{MM}m@var{SS}.@var{FF}s.
4739 The value of @var{p} determines whether or not the fraction is included.
4741 If this variable is not set, Bash acts as if it had the value
4743 @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'}
4745 If the value is null, no timing information is displayed.
4746 A trailing newline is added when the format string is displayed.
4749 If set to a value greater than zero, @code{TMOUT} is treated as the
4750 default timeout for the @code{read} builtin (@pxref{Bash Builtins}).
4751 The @code{select} command (@pxref{Conditional Constructs}) terminates
4752 if input does not arrive after @code{TMOUT} seconds when input is coming
4755 In an interative shell, the value is interpreted as
4756 the number of seconds to wait for input after issuing the primary
4757 prompt when the shell is interactive.
4758 Bash terminates after that number of seconds if input does
4762 The numeric real user id of the current user. This variable is readonly.
4767 @chapter Bash Features
4769 This section describes features unique to Bash.
4772 * Invoking Bash:: Command line options that you can give
4774 * Bash Startup Files:: When and how Bash executes scripts.
4775 * Interactive Shells:: What an interactive shell is.
4776 * Bash Conditional Expressions:: Primitives used in composing expressions for
4777 the @code{test} builtin.
4778 * Shell Arithmetic:: Arithmetic on shell variables.
4779 * Aliases:: Substituting one command for another.
4780 * Arrays:: Array Variables.
4781 * The Directory Stack:: History of visited directories.
4782 * Printing a Prompt:: Controlling the PS1 string.
4783 * The Restricted Shell:: A more controlled mode of shell execution.
4784 * Bash POSIX Mode:: Making Bash behave more closely to what
4785 the POSIX standard specifies.
4789 @section Invoking Bash
4792 bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}]
4793 bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}]
4794 bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}]
4797 In addition to the single-character shell command-line options
4798 (@pxref{The Set Builtin}), there are several multi-character
4799 options that you can use. These options must appear on the command
4800 line before the single-character options to be recognized.
4804 Arrange for the debugger profile to be executed before the shell
4805 starts. Turns on extended debugging mode (see @ref{Bash Builtins}
4806 for a description of the @code{extdebug} option to the @code{shopt}
4807 builtin) and shell function tracing
4808 (see @ref{The Set Builtin} for a description of the @code{-o functrace}
4811 @item --dump-po-strings
4812 A list of all double-quoted strings preceded by @samp{$}
4813 is printed on the standard output
4814 in the @sc{gnu} @code{gettext} PO (portable object) file format.
4815 Equivalent to @option{-D} except for the output format.
4817 @item --dump-strings
4818 Equivalent to @option{-D}.
4821 Display a usage message on standard output and exit sucessfully.
4823 @item --init-file @var{filename}
4824 @itemx --rcfile @var{filename}
4825 Execute commands from @var{filename} (instead of @file{~/.bashrc})
4826 in an interactive shell.
4829 Equivalent to @option{-l}.
4832 Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing})
4833 to read command lines when the shell is interactive.
4836 Don't load the system-wide startup file @file{/etc/profile}
4837 or any of the personal initialization files
4838 @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile}
4839 when Bash is invoked as a login shell.
4842 Don't read the @file{~/.bashrc} initialization file in an
4843 interactive shell. This is on by default if the shell is
4844 invoked as @code{sh}.
4847 Change the behavior of Bash where the default operation differs
4848 from the @sc{posix} 1003.2 standard to match the standard. This
4849 is intended to make Bash behave as a strict superset of that
4850 standard. @xref{Bash POSIX Mode}, for a description of the Bash
4854 Make the shell a restricted shell (@pxref{The Restricted Shell}).
4857 Equivalent to @option{-v}. Print shell input lines as they're read.
4860 Show version information for this instance of
4861 Bash on the standard output and exit successfully.
4865 There are several single-character options that may be supplied at
4866 invocation which are not available with the @code{set} builtin.
4869 @item -c @var{string}
4870 Read and execute commands from @var{string} after processing the
4871 options, then exit. Any remaining arguments are assigned to the
4872 positional parameters, starting with @code{$0}.
4875 Force the shell to run interactively. Interactive shells are
4876 described in @ref{Interactive Shells}.
4879 Make this shell act as if it had been directly invoked by login.
4880 When the shell is interactive, this is equivalent to starting a
4881 login shell with @samp{exec -l bash}.
4882 When the shell is not interactive, the login shell startup files will
4884 @samp{exec bash -l} or @samp{exec bash --login}
4885 will replace the current shell with a Bash login shell.
4886 @xref{Bash Startup Files}, for a description of the special behavior
4890 Make the shell a restricted shell (@pxref{The Restricted Shell}).
4893 If this option is present, or if no arguments remain after option
4894 processing, then commands are read from the standard input.
4895 This option allows the positional parameters to be set
4896 when invoking an interactive shell.
4899 A list of all double-quoted strings preceded by @samp{$}
4900 is printed on the standard output.
4901 These are the strings that
4902 are subject to language translation when the current locale
4903 is not @code{C} or @code{POSIX} (@pxref{Locale Translation}).
4904 This implies the @option{-n} option; no commands will be executed.
4906 @item [-+]O [@var{shopt_option}]
4907 @var{shopt_option} is one of the shell options accepted by the
4908 @code{shopt} builtin (@pxref{Shell Builtin Commands}).
4909 If @var{shopt_option} is present, @option{-O} sets the value of that option;
4910 @option{+O} unsets it.
4911 If @var{shopt_option} is not supplied, the names and values of the shell
4912 options accepted by @code{shopt} are printed on the standard output.
4913 If the invocation option is @option{+O}, the output is displayed in a format
4914 that may be reused as input.
4917 A @code{--} signals the end of options and disables further option
4919 Any arguments after the @code{--} are treated as filenames and arguments.
4924 A @emph{login} shell is one whose first character of argument zero is
4925 @samp{-}, or one invoked with the @option{--login} option.
4927 @cindex interactive shell
4928 An @emph{interactive} shell is one started without non-option arguments,
4929 unless @option{-s} is specified,
4930 without specifying the @option{-c} option, and whose input and output are both
4931 connected to terminals (as determined by @code{isatty(3)}), or one
4932 started with the @option{-i} option. @xref{Interactive Shells}, for more
4935 If arguments remain after option processing, and neither the
4936 @option{-c} nor the @option{-s}
4937 option has been supplied, the first argument is assumed to
4938 be the name of a file containing shell commands (@pxref{Shell Scripts}).
4939 When Bash is invoked in this fashion, @code{$0}
4940 is set to the name of the file, and the positional parameters
4941 are set to the remaining arguments.
4942 Bash reads and executes commands from this file, then exits.
4943 Bash's exit status is the exit status of the last command executed
4944 in the script. If no commands are executed, the exit status is 0.
4946 @node Bash Startup Files
4947 @section Bash Startup Files
4948 @cindex startup files
4950 This section describs how Bash executes its startup files.
4951 If any of the files exist but cannot be read, Bash reports an error.
4952 Tildes are expanded in file names as described above under
4953 Tilde Expansion (@pxref{Tilde Expansion}).
4955 Interactive shells are described in @ref{Interactive Shells}.
4957 @subsubheading Invoked as an interactive login shell, or with @option{--login}
4959 When Bash is invoked as an interactive login shell, or as a
4960 non-interactive shell with the @option{--login} option, it first reads and
4961 executes commands from the file @file{/etc/profile}, if that file exists.
4962 After reading that file, it looks for @file{~/.bash_profile},
4963 @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads
4964 and executes commands from the first one that exists and is readable.
4965 The @option{--noprofile} option may be used when the shell is started to
4966 inhibit this behavior.
4968 When a login shell exits, Bash reads and executes commands from
4969 the file @file{~/.bash_logout}, if it exists.
4971 @subsubheading Invoked as an interactive non-login shell
4973 When an interactive shell that is not a login shell is started, Bash
4974 reads and executes commands from @file{~/.bashrc}, if that file exists.
4975 This may be inhibited by using the @option{--norc} option.
4976 The @option{--rcfile @var{file}} option will force Bash to read and
4977 execute commands from @var{file} instead of @file{~/.bashrc}.
4979 So, typically, your @file{~/.bash_profile} contains the line
4981 @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi}
4984 after (or before) any login-specific initializations.
4986 @subsubheading Invoked non-interactively
4988 When Bash is started non-interactively, to run a shell script,
4989 for example, it looks for the variable @env{BASH_ENV} in the environment,
4990 expands its value if it appears there, and uses the expanded value as
4991 the name of a file to read and execute. Bash behaves as if the
4992 following command were executed:
4994 @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi}
4997 but the value of the @env{PATH} variable is not used to search for the
5000 As noted above, if a non-interactive shell is invoked with the
5001 @option{--login} option, Bash attempts to read and execute commands from the
5002 login shell startup files.
5004 @subsubheading Invoked with name @code{sh}
5006 If Bash is invoked with the name @code{sh}, it tries to mimic the
5007 startup behavior of historical versions of @code{sh} as closely as
5008 possible, while conforming to the @sc{posix} standard as well.
5010 When invoked as an interactive login shell, or as a non-interactive
5011 shell with the @option{--login} option, it first attempts to read
5012 and execute commands from @file{/etc/profile} and @file{~/.profile}, in
5014 The @option{--noprofile} option may be used to inhibit this behavior.
5015 When invoked as an interactive shell with the name @code{sh}, Bash
5016 looks for the variable @env{ENV}, expands its value if it is defined,
5017 and uses the expanded value as the name of a file to read and execute.
5018 Since a shell invoked as @code{sh} does not attempt to read and execute
5019 commands from any other startup files, the @option{--rcfile} option has
5021 A non-interactive shell invoked with the name @code{sh} does not attempt
5022 to read any other startup files.
5024 When invoked as @code{sh}, Bash enters @sc{posix} mode after
5025 the startup files are read.
5027 @subsubheading Invoked in @sc{posix} mode
5029 When Bash is started in @sc{posix} mode, as with the
5030 @option{--posix} command line option, it follows the @sc{posix} standard
5032 In this mode, interactive shells expand the @env{ENV} variable
5033 and commands are read and executed from the file whose name is the
5035 No other startup files are read.
5037 @subsubheading Invoked by remote shell daemon
5039 Bash attempts to determine when it is being run by the remote shell
5040 daemon, usually @code{rshd}. If Bash determines it is being run by
5041 rshd, it reads and executes commands from @file{~/.bashrc}, if that
5042 file exists and is readable.
5043 It will not do this if invoked as @code{sh}.
5044 The @option{--norc} option may be used to inhibit this behavior, and the
5045 @option{--rcfile} option may be used to force another file to be read, but
5046 @code{rshd} does not generally invoke the shell with those options or
5047 allow them to be specified.
5049 @subsubheading Invoked with unequal effective and real @sc{uid/gid}s
5051 If Bash is started with the effective user (group) id not equal to the
5052 real user (group) id, and the @code{-p} option is not supplied, no startup
5053 files are read, shell functions are not inherited from the environment,
5054 the @env{SHELLOPTS} variable, if it appears in the environment, is ignored,
5055 and the effective user id is set to the real user id.
5056 If the @code{-p} option is supplied at invocation, the startup behavior is
5057 the same, but the effective user id is not reset.
5059 @node Interactive Shells
5060 @section Interactive Shells
5061 @cindex interactive shell
5062 @cindex shell, interactive
5065 * What is an Interactive Shell?:: What determines whether a shell is Interactive.
5066 * Is this Shell Interactive?:: How to tell if a shell is interactive.
5067 * Interactive Shell Behavior:: What changes in a interactive shell?
5070 @node What is an Interactive Shell?
5071 @subsection What is an Interactive Shell?
5073 An interactive shell
5074 is one started without non-option arguments, unless @option{-s} is
5075 specified, without specifiying the @option{-c} option, and
5076 whose input and error output are both
5077 connected to terminals (as determined by @code{isatty(3)}),
5078 or one started with the @option{-i} option.
5080 An interactive shell generally reads from and writes to a user's
5083 The @option{-s} invocation option may be used to set the positional parameters
5084 when an interactive shell is started.
5086 @node Is this Shell Interactive?
5087 @subsection Is this Shell Interactive?
5089 To determine within a startup script whether or not Bash is
5090 running interactively,
5091 test the value of the @samp{-} special parameter.
5092 It contains @code{i} when the shell is interactive. For example:
5096 *i*) echo This shell is interactive ;;
5097 *) echo This shell is not interactive ;;
5101 Alternatively, startup scripts may examine the variable
5102 @env{PS1}; it is unset in non-interactive shells, and set in
5103 interactive shells. Thus:
5106 if [ -z "$PS1" ]; then
5107 echo This shell is not interactive
5109 echo This shell is interactive
5113 @node Interactive Shell Behavior
5114 @subsection Interactive Shell Behavior
5116 When the shell is running interactively, it changes its behavior in
5121 Startup files are read and executed as described in @ref{Bash Startup Files}.
5124 Job Control (@pxref{Job Control}) is enabled by default. When job
5125 control is in effect, Bash ignores the keyboard-generated job control
5126 signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
5129 Bash expands and displays @env{PS1} before reading the first line
5130 of a command, and expands and displays @env{PS2} before reading the
5131 second and subsequent lines of a multi-line command.
5134 Bash executes the value of the @env{PROMPT_COMMAND} variable as a command
5135 before printing the primary prompt, @env{$PS1}
5136 (@pxref{Bash Variables}).
5139 Readline (@pxref{Command Line Editing}) is used to read commands from
5140 the user's terminal.
5143 Bash inspects the value of the @code{ignoreeof} option to @code{set -o}
5144 instead of exiting immediately when it receives an @code{EOF} on its
5145 standard input when reading a command (@pxref{The Set Builtin}).
5148 Command history (@pxref{Bash History Facilities})
5149 and history expansion (@pxref{History Interaction})
5150 are enabled by default.
5151 Bash will save the command history to the file named by @env{$HISTFILE}
5152 when an interactive shell exits.
5155 Alias expansion (@pxref{Aliases}) is performed by default.
5158 In the absence of any traps, Bash ignores @code{SIGTERM}
5162 In the absence of any traps, @code{SIGINT} is caught and handled
5164 @code{SIGINT} will interrupt some shell builtins.
5167 An interactive login shell sends a @code{SIGHUP} to all jobs on exit
5168 if the @code{hupoxexit} shell option has been enabled (@pxref{Signals}).
5171 The @option{-n} invocation option is ignored, and @samp{set -n} has
5172 no effect (@pxref{The Set Builtin}).
5175 Bash will check for mail periodically, depending on the values of the
5176 @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables
5177 (@pxref{Bash Variables}).
5180 Expansion errors due to references to unbound shell variables after
5181 @samp{set -u} has been enabled will not cause the shell to exit
5182 (@pxref{The Set Builtin}).
5185 The shell will not exit on expansion errors caused by @var{var} being unset
5186 or null in @code{$@{@var{var}:?@var{word}@}} expansions
5187 (@pxref{Shell Parameter Expansion}).
5190 Redirection errors encountered by shell builtins will not cause the
5194 When running in @sc{posix} mode, a special builtin returning an error
5195 status will not cause the shell to exit (@pxref{Bash POSIX Mode}).
5197 A failed @code{exec} will not cause the shell to exit
5198 (@pxref{Bourne Shell Builtins}).
5201 Parser syntax errors will not cause the shell to exit.
5204 Simple spelling correction for directory arguments to the @code{cd}
5205 builtin is enabled by default (see the description of the @code{cdspell}
5206 option to the @code{shopt} builtin in @ref{Bash Builtins}).
5209 The shell will check the value of the @env{TMOUT} variable and exit
5210 if a command is not read within the specified number of seconds after
5211 printing @env{$PS1} (@pxref{Bash Variables}).
5215 @node Bash Conditional Expressions
5216 @section Bash Conditional Expressions
5217 @cindex expressions, conditional
5219 Conditional expressions are used by the @code{[[} compound command
5220 and the @code{test} and @code{[} builtin commands.
5222 Expressions may be unary or binary.
5223 Unary expressions are often used to examine the status of a file.
5224 There are string operators and numeric comparison operators as well.
5225 If the @var{file} argument to one of the primaries is of the form
5226 @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked.
5227 If the @var{file} argument to one of the primaries is one of
5228 @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file
5229 descriptor 0, 1, or 2, respectively, is checked.
5231 Unless otherwise specified, primaries that operate on files follow symbolic
5232 links and operate on the target of the link, rather than the link itself.
5236 True if @var{file} exists.
5239 True if @var{file} exists and is a block special file.
5242 True if @var{file} exists and is a character special file.
5245 True if @var{file} exists and is a directory.
5248 True if @var{file} exists.
5251 True if @var{file} exists and is a regular file.
5254 True if @var{file} exists and its set-group-id bit is set.
5257 True if @var{file} exists and is a symbolic link.
5260 True if @var{file} exists and its "sticky" bit is set.
5263 True if @var{file} exists and is a named pipe (FIFO).
5266 True if @var{file} exists and is readable.
5269 True if @var{file} exists and has a size greater than zero.
5272 True if file descriptor @var{fd} is open and refers to a terminal.
5275 True if @var{file} exists and its set-user-id bit is set.
5278 True if @var{file} exists and is writable.
5281 True if @var{file} exists and is executable.
5284 True if @var{file} exists and is owned by the effective user id.
5287 True if @var{file} exists and is owned by the effective group id.
5290 True if @var{file} exists and is a symbolic link.
5293 True if @var{file} exists and is a socket.
5296 True if @var{file} exists and has been modified since it was last read.
5298 @item @var{file1} -nt @var{file2}
5299 True if @var{file1} is newer (according to modification date)
5300 than @var{file2}, or if @var{file1} exists and @var{file2} does not.
5302 @item @var{file1} -ot @var{file2}
5303 True if @var{file1} is older than @var{file2},
5304 or if @var{file2} exists and @var{file1} does not.
5306 @item @var{file1} -ef @var{file2}
5307 True if @var{file1} and @var{file2} refer to the same device and
5310 @item -o @var{optname}
5311 True if shell option @var{optname} is enabled.
5312 The list of options appears in the description of the @option{-o}
5313 option to the @code{set} builtin (@pxref{The Set Builtin}).
5315 @item -z @var{string}
5316 True if the length of @var{string} is zero.
5318 @item -n @var{string}
5320 True if the length of @var{string} is non-zero.
5322 @item @var{string1} == @var{string2}
5323 True if the strings are equal.
5324 @samp{=} may be used in place of @samp{==} for strict @sc{posix} compliance.
5326 @item @var{string1} != @var{string2}
5327 True if the strings are not equal.
5329 @item @var{string1} < @var{string2}
5330 True if @var{string1} sorts before @var{string2} lexicographically
5331 in the current locale.
5333 @item @var{string1} > @var{string2}
5334 True if @var{string1} sorts after @var{string2} lexicographically
5335 in the current locale.
5337 @item @var{arg1} OP @var{arg2}
5339 @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}.
5340 These arithmetic binary operators return true if @var{arg1}
5341 is equal to, not equal to, less than, less than or equal to,
5342 greater than, or greater than or equal to @var{arg2},
5343 respectively. @var{Arg1} and @var{arg2}
5344 may be positive or negative integers.
5348 @node Shell Arithmetic
5349 @section Shell Arithmetic
5350 @cindex arithmetic, shell
5351 @cindex shell arithmetic
5352 @cindex expressions, arithmetic
5353 @cindex evaluation, arithmetic
5354 @cindex arithmetic evaluation
5356 The shell allows arithmetic expressions to be evaluated, as one of
5357 the shell expansions or by the @code{let} and the @option{-i} option
5358 to the @code{declare} builtins.
5360 Evaluation is done in fixed-width integers with no check for overflow,
5361 though division by 0 is trapped and flagged as an error.
5362 The operators and their precedence, associativity, and values
5363 are the same as in the C language.
5364 The following list of operators is grouped into levels of
5365 equal-precedence operators.
5366 The levels are listed in order of decreasing precedence.
5370 @item @var{id}++ @var{id}--
5371 variable post-increment and post-decrement
5373 @item ++@var{id} --@var{id}
5374 variable pre-increment and pre-decrement
5377 unary minus and plus
5380 logical and bitwise negation
5386 multiplication, division, remainder
5389 addition, subtraction
5392 left and right bitwise shifts
5398 equality and inequality
5404 bitwise exclusive OR
5415 @item expr ? expr : expr
5416 conditional operator
5418 @item = *= /= %= += -= <<= >>= &= ^= |=
5425 Shell variables are allowed as operands; parameter expansion is
5426 performed before the expression is evaluated.
5427 Within an expression, shell variables may also be referenced by name
5428 without using the parameter expansion syntax.
5429 A shell variable that is null or unset evaluates to 0 when referenced
5430 by name without using the parameter expansion syntax.
5431 The value of a variable is evaluated as an arithmetic expression
5432 when it is referenced, or when a variable which has been given the
5433 @var{integer} attribute using @samp{declare -i} is assigned a value.
5434 A null value evaluates to 0.
5435 A shell variable need not have its integer attribute turned on
5436 to be used in an expression.
5438 Constants with a leading 0 are interpreted as octal numbers.
5439 A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise,
5440 numbers take the form [@var{base}@code{#}]@var{n}, where @var{base}
5441 is a decimal number between 2 and 64 representing the arithmetic
5442 base, and @var{n} is a number in that base. If @var{base}@code{#} is
5443 omitted, then base 10 is used.
5444 The digits greater than 9 are represented by the lowercase letters,
5445 the uppercase letters, @samp{@@}, and @samp{_}, in that order.
5446 If @var{base} is less than or equal to 36, lowercase and uppercase
5447 letters may be used interchangeably to represent numbers between 10
5450 Operators are evaluated in order of precedence. Sub-expressions in
5451 parentheses are evaluated first and may override the precedence
5456 @cindex alias expansion
5458 @var{Aliases} allow a string to be substituted for a word when it is used
5459 as the first word of a simple command.
5460 The shell maintains a list of aliases that may be set and unset with
5461 the @code{alias} and @code{unalias} builtin commands.
5463 The first word of each simple command, if unquoted, is checked to see
5465 If so, that word is replaced by the text of the alias.
5466 The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the
5467 shell metacharacters or quoting characters listed above may not appear
5469 The replacement text may contain any valid
5470 shell input, including shell metacharacters.
5471 The first word of the replacement text is tested for
5472 aliases, but a word that is identical to an alias being expanded
5473 is not expanded a second time.
5474 This means that one may alias @code{ls} to @code{"ls -F"},
5475 for instance, and Bash does not try to recursively expand the
5476 replacement text. If the last character of the alias value is a
5477 space or tab character, then the next command word following the
5478 alias is also checked for alias expansion.
5480 Aliases are created and listed with the @code{alias}
5481 command, and removed with the @code{unalias} command.
5483 There is no mechanism for using arguments in the replacement text,
5485 If arguments are needed, a shell function should be used
5486 (@pxref{Shell Functions}).
5488 Aliases are not expanded when the shell is not interactive,
5489 unless the @code{expand_aliases} shell option is set using
5490 @code{shopt} (@pxref{Bash Builtins}).
5492 The rules concerning the definition and use of aliases are
5493 somewhat confusing. Bash
5494 always reads at least one complete line
5495 of input before executing any
5496 of the commands on that line. Aliases are expanded when a
5497 command is read, not when it is executed. Therefore, an
5498 alias definition appearing on the same line as another
5499 command does not take effect until the next line of input is read.
5500 The commands following the alias definition
5501 on that line are not affected by the new alias.
5502 This behavior is also an issue when functions are executed.
5503 Aliases are expanded when a function definition is read,
5504 not when the function is executed, because a function definition
5505 is itself a compound command. As a consequence, aliases
5506 defined in a function are not available until after that
5507 function is executed. To be safe, always put
5508 alias definitions on a separate line, and do not use @code{alias}
5509 in compound commands.
5511 For almost every purpose, shell functions are preferred over aliases.
5517 Bash provides one-dimensional array variables. Any variable may be used as
5518 an array; the @code{declare} builtin will explicitly declare an array.
5520 limit on the size of an array, nor any requirement that members
5521 be indexed or assigned contiguously. Arrays are zero-based.
5523 An array is created automatically if any variable is assigned to using
5526 name[@var{subscript}]=@var{value}
5531 is treated as an arithmetic expression that must evaluate to a number
5532 greater than or equal to zero. To explicitly declare an array, use
5534 declare -a @var{name}
5539 declare -a @var{name}[@var{subscript}]
5542 is also accepted; the @var{subscript} is ignored. Attributes may be
5543 specified for an array variable using the @code{declare} and
5544 @code{readonly} builtins. Each attribute applies to all members of
5547 Arrays are assigned to using compound assignments of the form
5549 name=(value@var{1} @dots{} value@var{n})
5553 @var{value} is of the form @code{[[@var{subscript}]=]}@var{string}. If
5554 the optional subscript is supplied, that index is assigned to;
5555 otherwise the index of the element assigned is the last index assigned
5556 to by the statement plus one. Indexing starts at zero.
5557 This syntax is also accepted by the @code{declare}
5558 builtin. Individual array elements may be assigned to using the
5559 @code{name[}@var{subscript}@code{]=}@var{value} syntax introduced above.
5561 Any element of an array may be referenced using
5562 @code{$@{name[}@var{subscript}@code{]@}}.
5563 The braces are required to avoid
5564 conflicts with the shell's filename expansion operators. If the
5565 @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members
5566 of the array @var{name}. These subscripts differ only when the word
5567 appears within double quotes.
5568 If the word is double-quoted,
5569 @code{$@{name[*]@}} expands to a single word with
5570 the value of each array member separated by the first character of the
5571 @env{IFS} variable, and @code{$@{name[@@]@}} expands each element of
5572 @var{name} to a separate word. When there are no array members,
5573 @code{$@{name[@@]@}} expands to nothing.
5574 If the double-quoted expansion occurs within a word, the expansion of
5575 the first parameter is joined with the beginning part of the original
5576 word, and the expansion of the last parameter is joined with the last
5577 part of the original word.
5578 This is analogous to the
5579 expansion of the special parameters @samp{@@} and @samp{*}.
5580 @code{$@{#name[}@var{subscript}@code{]@}} expands to the length of
5581 @code{$@{name[}@var{subscript}@code{]@}}.
5582 If @var{subscript} is @samp{@@} or
5583 @samp{*}, the expansion is the number of elements in the array.
5584 Referencing an array variable without a subscript is equivalent to
5585 referencing element zero.
5587 The @code{unset} builtin is used to destroy arrays.
5588 @code{unset} @var{name}[@var{subscript}]
5589 destroys the array element at index @var{subscript}.
5590 @code{unset} @var{name}, where @var{name} is an array, removes the
5591 entire array. A subscript of @samp{*} or @samp{@@} also removes the
5594 The @code{declare}, @code{local}, and @code{readonly}
5595 builtins each accept a @option{-a}
5596 option to specify an array. The @code{read}
5597 builtin accepts a @option{-a}
5598 option to assign a list of words read from the standard input
5599 to an array, and can read values from the standard input into
5600 individual array elements. The @code{set} and @code{declare}
5601 builtins display array values in a way that allows them to be
5604 @node The Directory Stack
5605 @section The Directory Stack
5606 @cindex directory stack
5609 * Directory Stack Builtins:: Bash builtin commands to manipulate
5610 the directory stack.
5613 The directory stack is a list of recently-visited directories. The
5614 @code{pushd} builtin adds directories to the stack as it changes
5615 the current directory, and the @code{popd} builtin removes specified
5616 directories from the stack and changes the current directory to
5617 the directory removed. The @code{dirs} builtin displays the contents
5618 of the directory stack.
5620 The contents of the directory stack are also visible
5621 as the value of the @env{DIRSTACK} shell variable.
5623 @node Directory Stack Builtins
5624 @subsection Directory Stack Builtins
5631 dirs [+@var{N} | -@var{N}] [-clpv]
5633 Display the list of currently remembered directories. Directories
5634 are added to the list with the @code{pushd} command; the
5635 @code{popd} command removes directories from the list.
5638 Displays the @var{N}th directory (counting from the left of the
5639 list printed by @code{dirs} when invoked without options), starting
5642 Displays the @var{N}th directory (counting from the right of the
5643 list printed by @code{dirs} when invoked without options), starting
5646 Clears the directory stack by deleting all of the elements.
5648 Produces a longer listing; the default listing format uses a
5649 tilde to denote the home directory.
5651 Causes @code{dirs} to print the directory stack with one entry per
5654 Causes @code{dirs} to print the directory stack with one entry per
5655 line, prefixing each entry with its index in the stack.
5661 popd [+@var{N} | -@var{N}] [-n]
5664 Remove the top entry from the directory stack, and @code{cd}
5665 to the new top directory.
5666 When no arguments are given, @code{popd}
5667 removes the top directory from the stack and
5668 performs a @code{cd} to the new top directory. The
5669 elements are numbered from 0 starting at the first directory listed with
5670 @code{dirs}; i.e., @code{popd} is equivalent to @code{popd +0}.
5673 Removes the @var{N}th directory (counting from the left of the
5674 list printed by @code{dirs}), starting with zero.
5676 Removes the @var{N}th directory (counting from the right of the
5677 list printed by @code{dirs}), starting with zero.
5679 Suppresses the normal change of directory when removing directories
5680 from the stack, so that only the stack is manipulated.
5686 pushd [@var{dir} | @var{+N} | @var{-N}] [-n]
5689 Save the current directory on the top of the directory stack
5690 and then @code{cd} to @var{dir}.
5691 With no arguments, @code{pushd} exchanges the top two directories.
5695 Brings the @var{N}th directory (counting from the left of the
5696 list printed by @code{dirs}, starting with zero) to the top of
5697 the list by rotating the stack.
5699 Brings the @var{N}th directory (counting from the right of the
5700 list printed by @code{dirs}, starting with zero) to the top of
5701 the list by rotating the stack.
5703 Suppresses the normal change of directory when adding directories
5704 to the stack, so that only the stack is manipulated.
5706 Makes the current working directory be the top of the stack, and then
5707 executes the equivalent of `@code{cd} @var{dir}'.
5708 @code{cd}s to @var{dir}.
5713 @node Printing a Prompt
5714 @section Controlling the Prompt
5717 The value of the variable @env{PROMPT_COMMAND} is examined just before
5718 Bash prints each primary prompt. If @env{PROMPT_COMMAND} is set and
5719 has a non-null value, then the
5720 value is executed just as if it had been typed on the command line.
5722 In addition, the following table describes the special characters which
5723 can appear in the prompt variables:
5729 The date, in "Weekday Month Date" format (e.g., "Tue May 26").
5730 @item \D@{@var{format}@}
5731 The @var{format} is passed to @code{strftime}(3) and the result is inserted
5732 into the prompt string; an empty @var{format} results in a locale-specific
5733 time representation. The braces are required.
5735 An escape character.
5737 The hostname, up to the first `.'.
5741 The number of jobs currently managed by the shell.
5743 The basename of the shell's terminal device name.
5749 The name of the shell, the basename of @code{$0} (the portion
5750 following the final slash).
5752 The time, in 24-hour HH:MM:SS format.
5754 The time, in 12-hour HH:MM:SS format.
5756 The time, in 12-hour am/pm format.
5758 The time, in 24-hour HH:MM format.
5760 The username of the current user.
5762 The version of Bash (e.g., 2.00)
5764 The release of Bash, version + patchlevel (e.g., 2.00.0)
5766 The current working directory, with @env{$HOME} abbreviated with a tilde.
5768 The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde.
5770 The history number of this command.
5772 The command number of this command.
5774 If the effective uid is 0, @code{#}, otherwise @code{$}.
5776 The character whose ASCII code is the octal value @var{nnn}.
5780 Begin a sequence of non-printing characters. This could be used to
5781 embed a terminal control sequence into the prompt.
5783 End a sequence of non-printing characters.
5786 The command number and the history number are usually different:
5787 the history number of a command is its position in the history
5788 list, which may include commands restored from the history file
5789 (@pxref{Bash History Facilities}), while the command number is
5790 the position in the sequence of commands executed during the current
5793 After the string is decoded, it is expanded via
5794 parameter expansion, command substitution, arithmetic
5795 expansion, and quote removal, subject to the value of the
5796 @code{promptvars} shell option (@pxref{Bash Builtins}).
5798 @node The Restricted Shell
5799 @section The Restricted Shell
5800 @cindex restricted shell
5802 If Bash is started with the name @code{rbash}, or the
5803 @option{--restricted}
5806 option is supplied at invocation, the shell becomes restricted.
5807 A restricted shell is used to
5808 set up an environment more controlled than the standard shell.
5809 A restricted shell behaves identically to @code{bash}
5810 with the exception that the following are disallowed or not performed:
5814 Changing directories with the @code{cd} builtin.
5816 Setting or unsetting the values of the @env{SHELL}, @env{PATH},
5817 @env{ENV}, or @env{BASH_ENV} variables.
5819 Specifying command names containing slashes.
5821 Specifying a filename containing a slash as an argument to the @code{.}
5824 Specifying a filename containing a slash as an argument to the @option{-p}
5825 option to the @code{hash} builtin command.
5827 Importing function definitions from the shell environment at startup.
5829 Parsing the value of @env{SHELLOPTS} from the shell environment at startup.
5831 Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&},
5832 @samp{&>}, and @samp{>>} redirection operators.
5834 Using the @code{exec} builtin to replace the shell with another command.
5836 Adding or deleting builtin commands with the
5837 @option{-f} and @option{-d} options to the @code{enable} builtin.
5839 Using the @code{enable} builtin command to enable disabled shell builtins.
5841 Specifying the @option{-p} option to the @code{command} builtin.
5843 Turning off restricted mode with @samp{set +r} or @samp{set +o restricted}.
5846 These restrictions are enforced after any startup files are read.
5848 When a command that is found to be a shell script is executed
5849 (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in
5850 the shell spawned to execute the script.
5852 @node Bash POSIX Mode
5853 @section Bash POSIX Mode
5856 Starting Bash with the @option{--posix} command-line option or executing
5857 @samp{set -o posix} while Bash is running will cause Bash to conform more
5858 closely to the @sc{posix} 1003.2 standard by changing the behavior to
5859 match that specified by @sc{posix} in areas where the Bash default differs.
5861 When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the
5864 The following list is what's changed when `@sc{posix} mode' is in effect:
5868 When a command in the hash table no longer exists, Bash will re-search
5869 @env{$PATH} to find the new location. This is also available with
5870 @samp{shopt -s checkhash}.
5873 The message printed by the job control code and builtins when a job
5874 exits with a non-zero status is `Done(status)'.
5877 The message printed by the job control code and builtins when a job
5878 is stopped is `Stopped(@var{signame})', where @var{signame} is, for
5879 example, @code{SIGTSTP}.
5882 Reserved words may not be aliased.
5885 The @sc{posix} 1003.2 @env{PS1} and @env{PS2} expansions of @samp{!} to
5886 the history number and @samp{!!} to @samp{!} are enabled,
5887 and parameter expansion is performed on the values of @env{PS1} and
5888 @env{PS2} regardless of the setting of the @code{promptvars} option.
5891 The @sc{posix} 1003.2 startup files are executed (@env{$ENV}) rather than
5892 the normal Bash files.
5895 Tilde expansion is only performed on assignments preceding a command
5896 name, rather than on all assignment statements on the line.
5899 The default history file is @file{~/.sh_history} (this is the
5900 default value of @env{$HISTFILE}).
5903 The output of @samp{kill -l} prints all the signal names on a single line,
5904 separated by spaces, without the @samp{SIG} prefix.
5907 The @code{kill} builtin does not accept signal names with a @samp{SIG}
5911 Non-interactive shells exit if @var{filename} in @code{.} @var{filename}
5915 Non-interactive shells exit if a syntax error in an arithmetic expansion
5916 results in an invalid expression.
5919 Redirection operators do not perform filename expansion on the word
5920 in the redirection unless the shell is interactive.
5923 Redirection operators do not perform word splitting on the word in the
5927 Function names must be valid shell @code{name}s. That is, they may not
5928 contain characters other than letters, digits, and underscores, and
5929 may not start with a digit. Declaring a function with an invalid name
5930 causes a fatal syntax error in non-interactive shells.
5933 @sc{posix} 1003.2 `special' builtins are found before shell functions
5934 during command lookup.
5937 If a @sc{posix} 1003.2 special builtin returns an error status, a
5938 non-interactive shell exits. The fatal errors are those listed in
5939 the POSIX.2 standard, and include things like passing incorrect options,
5940 redirection errors, variable assignment errors for assignments preceding
5941 the command name, and so on.
5944 If the @code{cd} builtin finds a directory to change to
5945 using @env{$CDPATH}, the
5946 value it assigns to the @env{PWD} variable does not contain any
5947 symbolic links, as if @samp{cd -P} had been executed.
5950 If @env{CDPATH} is set, the @code{cd} builtin will not implicitly
5951 append the current directory to it. This means that @code{cd} will
5952 fail if no valid directory name can be constructed from
5953 any of the entries in @env{$CDPATH}, even if the a directory with
5954 the same name as the name given as an argument to @code{cd} exists
5955 in the current directory.
5958 A non-interactive shell exits with an error status if a variable
5959 assignment error occurs when no command name follows the assignment
5961 A variable assignment error occurs, for example, when trying to assign
5962 a value to a readonly variable.
5965 A non-interactive shell exits with an error status if the iteration
5966 variable in a @code{for} statement or the selection variable in a
5967 @code{select} statement is a readonly variable.
5970 Process substitution is not available.
5973 Assignment statements preceding @sc{posix} 1003.2 special builtins
5974 persist in the shell environment after the builtin completes.
5977 Assignment statements preceding shell function calls persist in the
5978 shell environment after the function returns, as if a @sc{posix}
5979 special builtin command had been executed.
5982 The @code{export} and @code{readonly} builtin commands display their
5983 output in the format required by @sc{posix} 1003.2.
5986 The @code{trap} builtin displays signal names without the leading
5990 The @code{trap} builtin doesn't check the first argument for a possible
5991 signal specification and revert the signal handling to the original
5992 disposition if it is, unless that argument consists solely of digits and
5993 is a valid signal number. If users want to reset the handler for a given
5994 signal to the original disposition, they should use @samp{-} as the
5998 The @code{.} and @code{source} builtins do not search the current directory
5999 for the filename argument if it is not found by searching @env{PATH}.
6002 Subshells spawned to execute command substitutions inherit the value of
6003 the @option{-e} option from the parent shell. When not in @sc{posix} mode,
6004 Bash clears the @option{-e} option in such subshells.
6007 Alias expansion is always enabled, even in non-interactive shells.
6010 When the @code{alias} builtin displays alias definitions, it does not
6011 display them with a leading @samp{alias } unless the @option{-p} option
6015 When the @code{set} builtin is invoked without options, it does not display
6016 shell function names and definitions.
6019 When the @code{set} builtin is invoked without options, it displays
6020 variable values without quotes, unless they contain shell metacharacters,
6021 even if the result contains nonprinting characters.
6024 When the @code{cd} builtin is invoked in @var{logical} mode, and the pathname
6025 constructed from @code{$PWD} and the directory name supplied as an argument
6026 does not refer to an existing directory, @code{cd} will fail instead of
6027 falling back to @var{physical} mode.
6030 When listing the history, the @code{fc} builtin does not include an
6031 indication of whether or not a history entry has been modified.
6034 The default editor used by @code{fc} is @code{ed}.
6037 The @code{type} and @code{command} builtins will not report a non-executable
6038 file as having been found, though the shell will attempt to execute such a
6039 file if it is the only so-named file found in @code{$PATH}.
6042 When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret
6043 any arguments to @code{echo} as options. Each argument is displayed, after
6044 escape characters are converted.
6048 There is other @sc{posix} 1003.2 behavior that Bash does not implement.
6053 Assignment statements affect the execution environment of all
6054 builtins, not just special ones.
6057 When a subshell is created to execute a shell script with execute permission,
6058 but without a leading @samp{#!}, Bash sets @code{$0} to the full pathname of
6059 the script as found by searching @code{$PATH}, rather than the command as
6063 When using @samp{.} to source a shell script found in @code{$PATH}, bash
6064 checks execute permission bits rather than read permission bits, just as
6065 if it were searching for a command.
6070 @chapter Job Control
6072 This chapter discusses what job control is, how it works, and how
6073 Bash allows you to access its facilities.
6076 * Job Control Basics:: How job control works.
6077 * Job Control Builtins:: Bash builtin commands used to interact
6079 * Job Control Variables:: Variables Bash uses to customize job
6083 @node Job Control Basics
6084 @section Job Control Basics
6088 @cindex suspending jobs
6091 refers to the ability to selectively stop (suspend)
6092 the execution of processes and continue (resume)
6093 their execution at a later point. A user typically employs
6094 this facility via an interactive interface supplied jointly
6095 by the system's terminal driver and Bash.
6097 The shell associates a @var{job} with each pipeline. It keeps a
6098 table of currently executing jobs, which may be listed with the
6099 @code{jobs} command. When Bash starts a job
6100 asynchronously, it prints a line that looks
6106 indicating that this job is job number 1 and that the process @sc{id}
6107 of the last process in the pipeline associated with this job is
6108 25647. All of the processes in a single pipeline are members of
6109 the same job. Bash uses the @var{job} abstraction as the
6110 basis for job control.
6112 To facilitate the implementation of the user interface to job
6113 control, the operating system maintains the notion of a current terminal
6114 process group @sc{id}. Members of this process group (processes whose
6115 process group @sc{id} is equal to the current terminal process group
6116 @sc{id}) receive keyboard-generated signals such as @code{SIGINT}.
6117 These processes are said to be in the foreground. Background
6118 processes are those whose process group @sc{id} differs from the
6119 terminal's; such processes are immune to keyboard-generated
6120 signals. Only foreground processes are allowed to read from or
6121 write to the terminal. Background processes which attempt to
6122 read from (write to) the terminal are sent a @code{SIGTTIN}
6123 (@code{SIGTTOU}) signal by the terminal driver, which, unless
6124 caught, suspends the process.
6126 If the operating system on which Bash is running supports
6127 job control, Bash contains facilities to use it. Typing the
6128 @var{suspend} character (typically @samp{^Z}, Control-Z) while a
6129 process is running causes that process to be stopped and returns
6130 control to Bash. Typing the @var{delayed suspend} character
6131 (typically @samp{^Y}, Control-Y) causes the process to be stopped
6132 when it attempts to read input from the terminal, and control to
6133 be returned to Bash. The user then manipulates the state of
6134 this job, using the @code{bg} command to continue it in the
6135 background, the @code{fg} command to continue it in the
6136 foreground, or the @code{kill} command to kill it. A @samp{^Z}
6137 takes effect immediately, and has the additional side effect of
6138 causing pending output and typeahead to be discarded.
6140 There are a number of ways to refer to a job in the shell. The
6141 character @samp{%} introduces a job name.
6143 Job number @code{n} may be referred to as @samp{%n}.
6144 The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the
6145 current job, which is the last job stopped while it was in the foreground
6146 or started in the background.
6147 A single @samp{%} (with no accompanying job specification) also refers
6149 The previous job may be referenced using @samp{%-}. In output
6150 pertaining to jobs (e.g., the output of the @code{jobs} command),
6151 the current job is always flagged with a @samp{+}, and the
6152 previous job with a @samp{-}.
6154 A job may also be referred to
6155 using a prefix of the name used to start it, or using a substring
6156 that appears in its command line. For example, @samp{%ce} refers
6157 to a stopped @code{ce} job. Using @samp{%?ce}, on the
6158 other hand, refers to any job containing the string @samp{ce} in
6159 its command line. If the prefix or substring matches more than one job,
6160 Bash reports an error.
6162 Simply naming a job can be used to bring it into the foreground:
6163 @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the
6164 background into the foreground. Similarly, @samp{%1 &} resumes
6165 job 1 in the background, equivalent to @samp{bg %1}
6167 The shell learns immediately whenever a job changes state.
6168 Normally, Bash waits until it is about to print a prompt
6169 before reporting changes in a job's status so as to not interrupt
6171 If the @option{-b} option to the @code{set} builtin is enabled,
6172 Bash reports such changes immediately (@pxref{The Set Builtin}).
6173 Any trap on @code{SIGCHLD} is executed for each child process
6176 If an attempt to exit Bash is made while jobs are stopped, the
6177 shell prints a message warning that there are stopped jobs.
6178 The @code{jobs} command may then be used to inspect their status.
6179 If a second attempt to exit is made without an intervening command,
6180 Bash does not print another warning, and the stopped jobs are terminated.
6182 @node Job Control Builtins
6183 @section Job Control Builtins
6190 bg [@var{jobspec} @dots{}]
6192 Resume each suspended job @var{jobspec} in the background, as if it
6193 had been started with @samp{&}.
6194 If @var{jobspec} is not supplied, the current job is used.
6195 The return status is zero unless it is run when job control is not
6196 enabled, or, when run with job control enabled, if the last
6197 @var{jobspec} was not found or the last @var{jobspec} specifies a job
6198 that was started without job control.
6205 Resume the job @var{jobspec} in the foreground and make it the current job.
6206 If @var{jobspec} is not supplied, the current job is used.
6207 The return status is that of the command placed into the foreground,
6208 or non-zero if run when job control is disabled or, when run with
6209 job control enabled, @var{jobspec} does not specify a valid job or
6210 @var{jobspec} specifies a job that was started without job control.
6215 jobs [-lnprs] [@var{jobspec}]
6216 jobs -x @var{command} [@var{arguments}]
6219 The first form lists the active jobs. The options have the
6224 List process @sc{id}s in addition to the normal information.
6227 Display information only about jobs that have changed status since
6228 the user was last notified of their status.
6231 List only the process @sc{id} of the job's process group leader.
6234 Restrict output to running jobs.
6237 Restrict output to stopped jobs.
6240 If @var{jobspec} is given,
6241 output is restricted to information about that job.
6242 If @var{jobspec} is not supplied, the status of all jobs is
6245 If the @option{-x} option is supplied, @code{jobs} replaces any
6246 @var{jobspec} found in @var{command} or @var{arguments} with the
6247 corresponding process group @sc{id}, and executes @var{command},
6248 passing it @var{argument}s, returning its exit status.
6253 kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid}
6254 kill -l [@var{exit_status}]
6256 Send a signal specified by @var{sigspec} or @var{signum} to the process
6257 named by job specification @var{jobspec} or process @sc{id} @var{pid}.
6258 @var{sigspec} is either a case-insensitive signal name such as
6259 @code{SIGINT} (with or without the @code{SIG} prefix)
6260 or a signal number; @var{signum} is a signal number.
6261 If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used.
6262 The @option{-l} option lists the signal names.
6263 If any arguments are supplied when @option{-l} is given, the names of the
6264 signals corresponding to the arguments are listed, and the return status
6266 @var{exit_status} is a number specifying a signal number or the exit
6267 status of a process terminated by a signal.
6268 The return status is zero if at least one signal was successfully sent,
6269 or non-zero if an error occurs or an invalid option is encountered.
6274 wait [@var{jobspec} or @var{pid} ...]
6276 Wait until the child process specified by each process @sc{id} @var{pid}
6277 or job specification @var{jobspec} exits and return the exit status of the
6278 last command waited for.
6279 If a job spec is given, all processes in the job are waited for.
6280 If no arguments are given, all currently active child processes are
6281 waited for, and the return status is zero.
6282 If neither @var{jobspec} nor @var{pid} specifies an active child process
6283 of the shell, the return status is 127.
6288 disown [-ar] [-h] [@var{jobspec} @dots{}]
6290 Without options, each @var{jobspec} is removed from the table of
6292 If the @option{-h} option is given, the job is not removed from the table,
6293 but is marked so that @code{SIGHUP} is not sent to the job if the shell
6294 receives a @code{SIGHUP}.
6295 If @var{jobspec} is not present, and neither the @option{-a} nor @option{-r}
6296 option is supplied, the current job is used.
6297 If no @var{jobspec} is supplied, the @option{-a} option means to remove or
6298 mark all jobs; the @option{-r} option without a @var{jobspec}
6299 argument restricts operation to running jobs.
6306 Suspend the execution of this shell until it receives a
6307 @code{SIGCONT} signal. The @option{-f} option means to suspend
6308 even if the shell is a login shell.
6312 When job control is not active, the @code{kill} and @code{wait}
6313 builtins do not accept @var{jobspec} arguments. They must be
6314 supplied process @sc{id}s.
6316 @node Job Control Variables
6317 @section Job Control Variables
6322 This variable controls how the shell interacts with the user and
6323 job control. If this variable exists then single word simple
6324 commands without redirections are treated as candidates for resumption
6325 of an existing job. There is no ambiguity allowed; if there is
6326 more than one job beginning with the string typed, then
6327 the most recently accessed job will be selected.
6328 The name of a stopped job, in this context, is the command line
6329 used to start it. If this variable is set to the value @samp{exact},
6330 the string supplied must match the name of a stopped job exactly;
6331 if set to @samp{substring},
6332 the string supplied needs to match a substring of the name of a
6333 stopped job. The @samp{substring} value provides functionality
6334 analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}).
6335 If set to any other value, the supplied string must
6336 be a prefix of a stopped job's name; this provides functionality
6337 analogous to the @samp{%} job @sc{id}.
6341 @set readline-appendix
6342 @set history-appendix
6343 @cindex Readline, how to use
6344 @include rluser.texi
6345 @cindex History, how to use
6346 @include hsuser.texi
6347 @clear readline-appendix
6348 @clear history-appendix
6350 @node Installing Bash
6351 @chapter Installing Bash
6353 This chapter provides basic instructions for installing Bash on
6354 the various supported platforms. The distribution supports the
6355 @sc{gnu} operating systems, nearly every version of Unix, and several
6356 non-Unix systems such as BeOS and Interix.
6357 Other independent ports exist for
6358 @sc{ms-dos}, @sc{os/2}, and Windows platforms.
6361 * Basic Installation:: Installation instructions.
6363 * Compilers and Options:: How to set special options for various
6366 * Compiling For Multiple Architectures:: How to compile Bash for more
6367 than one kind of system from
6368 the same source tree.
6370 * Installation Names:: How to set the various paths used by the installation.
6372 * Specifying the System Type:: How to configure Bash for a particular system.
6374 * Sharing Defaults:: How to share default configuration values among GNU
6377 * Operation Controls:: Options recognized by the configuration program.
6379 * Optional Features:: How to enable and disable optional features when
6383 @node Basic Installation
6384 @section Basic Installation
6385 @cindex installation
6386 @cindex configuration
6387 @cindex Bash installation
6388 @cindex Bash configuration
6390 These are installation instructions for Bash.
6392 The simplest way to compile Bash is:
6396 @code{cd} to the directory containing the source code and type
6397 @samp{./configure} to configure Bash for your system. If you're
6398 using @code{csh} on an old version of System V, you might need to
6399 type @samp{sh ./configure} instead to prevent @code{csh} from trying
6400 to execute @code{configure} itself.
6402 Running @code{configure} takes some time.
6403 While running, it prints messages telling which features it is
6407 Type @samp{make} to compile Bash and build the @code{bashbug} bug
6411 Optionally, type @samp{make tests} to run the Bash test suite.
6414 Type @samp{make install} to install @code{bash} and @code{bashbug}.
6415 This will also install the manual pages and Info file.
6419 The @code{configure} shell script attempts to guess correct
6420 values for various system-dependent variables used during
6421 compilation. It uses those values to create a @file{Makefile} in
6422 each directory of the package (the top directory, the
6423 @file{builtins}, @file{doc}, and @file{support} directories,
6424 each directory under @file{lib}, and several others). It also creates a
6425 @file{config.h} file containing system-dependent definitions.
6426 Finally, it creates a shell script named @code{config.status} that you
6427 can run in the future to recreate the current configuration, a
6428 file @file{config.cache} that saves the results of its tests to
6429 speed up reconfiguring, and a file @file{config.log} containing
6430 compiler output (useful mainly for debugging @code{configure}).
6432 @file{config.cache} contains results you don't want to keep, you
6433 may remove or edit it.
6435 To find out more about the options and arguments that the
6436 @code{configure} script understands, type
6439 bash-2.04$ ./configure --help
6443 at the Bash prompt in your Bash source directory.
6445 If you need to do unusual things to compile Bash, please
6446 try to figure out how @code{configure} could check whether or not
6447 to do them, and mail diffs or instructions to
6448 @email{bash-maintainers@@gnu.org} so they can be
6449 considered for the next release.
6451 The file @file{configure.in} is used to create @code{configure}
6452 by a program called Autoconf. You only need
6453 @file{configure.in} if you want to change it or regenerate
6454 @code{configure} using a newer version of Autoconf. If
6455 you do this, make sure you are using Autoconf version 2.50 or
6458 You can remove the program binaries and object files from the
6459 source code directory by typing @samp{make clean}. To also remove the
6460 files that @code{configure} created (so you can compile Bash for
6461 a different kind of computer), type @samp{make distclean}.
6463 @node Compilers and Options
6464 @section Compilers and Options
6466 Some systems require unusual options for compilation or linking
6467 that the @code{configure} script does not know about. You can
6468 give @code{configure} initial values for variables by setting
6469 them in the environment. Using a Bourne-compatible shell, you
6470 can do that on the command line like this:
6473 CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
6476 On systems that have the @code{env} program, you can do it like this:
6479 env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
6482 The configuration process uses GCC to build Bash if it
6485 @node Compiling For Multiple Architectures
6486 @section Compiling For Multiple Architectures
6488 You can compile Bash for more than one kind of computer at the
6489 same time, by placing the object files for each architecture in their
6490 own directory. To do this, you must use a version of @code{make} that
6491 supports the @code{VPATH} variable, such as GNU @code{make}.
6493 directory where you want the object files and executables to go and run
6494 the @code{configure} script from the source directory. You may need to
6495 supply the @option{--srcdir=PATH} argument to tell @code{configure} where the
6496 source files are. @code{configure} automatically checks for the
6497 source code in the directory that @code{configure} is in and in `..'.
6499 If you have to use a @code{make} that does not supports the @code{VPATH}
6500 variable, you can compile Bash for one architecture at a
6501 time in the source code directory. After you have installed
6502 Bash for one architecture, use @samp{make distclean} before
6503 reconfiguring for another architecture.
6505 Alternatively, if your system supports symbolic links, you can use the
6506 @file{support/mkclone} script to create a build tree which has
6507 symbolic links back to each file in the source directory. Here's an
6508 example that creates a build directory in the current directory from a
6509 source directory @file{/usr/gnu/src/bash-2.0}:
6512 bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 .
6516 The @code{mkclone} script requires Bash, so you must have already built
6517 Bash for at least one architecture before you can create build
6518 directories for other architectures.
6520 @node Installation Names
6521 @section Installation Names
6523 By default, @samp{make install} will install into
6524 @file{/usr/local/bin}, @file{/usr/local/man}, etc. You can
6525 specify an installation prefix other than @file{/usr/local} by
6526 giving @code{configure} the option @option{--prefix=@var{PATH}},
6527 or by specifying a value for the @code{DESTDIR} @samp{make}
6528 variable when running @samp{make install}.
6530 You can specify separate installation prefixes for
6531 architecture-specific files and architecture-independent files.
6532 If you give @code{configure} the option
6533 @option{--exec-prefix=@var{PATH}}, @samp{make install} will use
6534 @var{PATH} as the prefix for installing programs and libraries.
6535 Documentation and other data files will still use the regular prefix.
6537 @node Specifying the System Type
6538 @section Specifying the System Type
6540 There may be some features @code{configure} can not figure out
6541 automatically, but need to determine by the type of host Bash
6542 will run on. Usually @code{configure} can figure that
6543 out, but if it prints a message saying it can not guess the host
6544 type, give it the @option{--host=TYPE} option. @samp{TYPE} can
6545 either be a short name for the system type, such as @samp{sun4},
6546 or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM}
6547 (e.g., @samp{i386-unknown-freebsd4.2}).
6549 See the file @file{support/config.sub} for the possible
6550 values of each field.
6552 @node Sharing Defaults
6553 @section Sharing Defaults
6555 If you want to set default values for @code{configure} scripts to
6556 share, you can create a site shell script called
6557 @code{config.site} that gives default values for variables like
6558 @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure}
6559 looks for @file{PREFIX/share/config.site} if it exists, then
6560 @file{PREFIX/etc/config.site} if it exists. Or, you can set the
6561 @code{CONFIG_SITE} environment variable to the location of the site
6562 script. A warning: the Bash @code{configure} looks for a site script,
6563 but not all @code{configure} scripts do.
6565 @node Operation Controls
6566 @section Operation Controls
6568 @code{configure} recognizes the following options to control how it
6573 @item --cache-file=@var{file}
6574 Use and save the results of the tests in
6575 @var{file} instead of @file{./config.cache}. Set @var{file} to
6576 @file{/dev/null} to disable caching, for debugging
6580 Print a summary of the options to @code{configure}, and exit.
6585 Do not print messages saying which checks are being made.
6587 @item --srcdir=@var{dir}
6588 Look for the Bash source code in directory @var{dir}. Usually
6589 @code{configure} can determine that directory automatically.
6592 Print the version of Autoconf used to generate the @code{configure}
6596 @code{configure} also accepts some other, not widely used, boilerplate
6597 options. @samp{configure --help} prints the complete list.
6599 @node Optional Features
6600 @section Optional Features
6602 The Bash @code{configure} has a number of @option{--enable-@var{feature}}
6603 options, where @var{feature} indicates an optional part of Bash.
6604 There are also several @option{--with-@var{package}} options,
6605 where @var{package} is something like @samp{bash-malloc} or @samp{purify}.
6606 To turn off the default use of a package, use
6607 @option{--without-@var{package}}. To configure Bash without a feature
6608 that is enabled by default, use @option{--disable-@var{feature}}.
6610 Here is a complete list of the @option{--enable-} and
6611 @option{--with-} options that the Bash @code{configure} recognizes.
6615 Define if you are using the Andrew File System from Transarc.
6617 @item --with-bash-malloc
6618 Use the Bash version of
6619 @code{malloc} in @file{lib/malloc/malloc.c}. This is not the same
6620 @code{malloc} that appears in @sc{gnu} libc, but an older version
6621 derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc} is
6622 very fast, but wastes some space on each allocation.
6623 This option is enabled by default.
6624 The @file{NOTES} file contains a list of systems for
6625 which this should be turned off, and @code{configure} disables this
6626 option automatically for a number of systems.
6629 Use the curses library instead of the termcap library. This should
6630 be supplied if your system has an inadequate or incomplete termcap
6633 @item --with-gnu-malloc
6634 A synonym for @code{--with-bash-malloc}.
6636 @item --with-installed-readline[=@var{PREFIX}]
6637 Define this to make Bash link with a locally-installed version of Readline
6638 rather than the version in @file{lib/readline}. This works only with
6639 Readline 4.3 and later versions. If @var{PREFIX} is @code{yes} or not
6640 supplied, @code{configure} uses the values of the make variables
6641 @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix}
6642 by default, to find the installed version of Readline if it is not in
6643 the standard system include and library directories.
6644 If @var{PREFIX} is @code{no}, Bash links with the version in
6645 @file{lib/readline}.
6646 If @var{PREFIX} is set to any other value, @code{configure} treats it as
6647 a directory pathname and looks for
6648 the installed version of Readline in subdirectories of that directory
6649 (include files in @var{PREFIX}/@code{include} and the library in
6650 @var{PREFIX}/@code{lib}).
6653 Define this to use the Purify memory allocation checker from Rational
6656 @item --enable-minimal-config
6657 This produces a shell with minimal features, close to the historical
6661 There are several @option{--enable-} options that alter how Bash is
6662 compiled and linked, rather than changing run-time features.
6665 @item --enable-largefile
6666 Enable support for @uref{http://www.sas.com/standards/large_file/x_open.20Mar96.html,
6667 large files} if the operating system requires special compiler options
6668 to build programs which can access large files. This is enabled by
6669 default, if the operating system provides large file support.
6671 @item --enable-profiling
6672 This builds a Bash binary that produces profiling information to be
6673 processed by @code{gprof} each time it is executed.
6675 @item --enable-static-link
6676 This causes Bash to be linked statically, if @code{gcc} is being used.
6677 This could be used to build a version to use as root's shell.
6680 The @samp{minimal-config} option can be used to disable all of
6681 the following options, but it is processed first, so individual
6682 options may be enabled using @samp{enable-@var{feature}}.
6684 All of the following options except for @samp{disabled-builtins} and
6685 @samp{xpg-echo-default} are
6686 enabled by default, unless the operating system does not provide the
6690 @item --enable-alias
6691 Allow alias expansion and include the @code{alias} and @code{unalias}
6692 builtins (@pxref{Aliases}).
6694 @item --enable-arith-for-command
6695 Include support for the alternate form of the @code{for} command
6696 that behaves like the C language @code{for} statement
6697 (@pxref{Looping Constructs}).
6699 @item --enable-array-variables
6700 Include support for one-dimensional array shell variables
6703 @item --enable-bang-history
6704 Include support for @code{csh}-like history substitution
6705 (@pxref{History Interaction}).
6707 @item --enable-brace-expansion
6708 Include @code{csh}-like brace expansion
6709 ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ).
6710 See @ref{Brace Expansion}, for a complete description.
6712 @item --enable-command-timing
6713 Include support for recognizing @code{time} as a reserved word and for
6714 displaying timing statistics for the pipeline following @code{time}
6715 (@pxref{Pipelines}).
6716 This allows pipelines as well as shell builtins and functions to be timed.
6718 @item --enable-cond-command
6719 Include support for the @code{[[} conditional command.
6720 (@pxref{Conditional Constructs}).
6722 @item --enable-cond-regexp
6723 Include support for matching POSIX regular expressions using the
6724 @samp{=~} binary operator in the @code{[[} conditional command.
6725 (@pxref{Conditional Constructs}).
6727 @item --enable-directory-stack
6728 Include support for a @code{csh}-like directory stack and the
6729 @code{pushd}, @code{popd}, and @code{dirs} builtins
6730 (@pxref{The Directory Stack}).
6732 @item --enable-disabled-builtins
6733 Allow builtin commands to be invoked via @samp{builtin xxx}
6734 even after @code{xxx} has been disabled using @samp{enable -n xxx}.
6735 See @ref{Bash Builtins}, for details of the @code{builtin} and
6736 @code{enable} builtin commands.
6738 @item --enable-dparen-arithmetic
6739 Include support for the @code{((@dots{}))} command
6740 (@pxref{Conditional Constructs}).
6742 @item --enable-extended-glob
6743 Include support for the extended pattern matching features described
6744 above under @ref{Pattern Matching}.
6746 @item --enable-help-builtin
6747 Include the @code{help} builtin, which displays help on shell builtins and
6748 variables (@pxref{Bash Builtins}).
6750 @item --enable-history
6751 Include command history and the @code{fc} and @code{history}
6752 builtin commands (@pxref{Bash History Facilities}).
6754 @item --enable-job-control
6755 This enables the job control features (@pxref{Job Control}),
6756 if the operating system supports them.
6758 @item --enable-multibyte
6759 This enables support for multibyte characters if the operating
6760 system provides the necessary support.
6762 @item --enable-net-redirections
6763 This enables the special handling of filenames of the form
6764 @code{/dev/tcp/@var{host}/@var{port}} and
6765 @code{/dev/udp/@var{host}/@var{port}}
6766 when used in redirections (@pxref{Redirections}).
6768 @item --enable-process-substitution
6769 This enables process substitution (@pxref{Process Substitution}) if
6770 the operating system provides the necessary support.
6772 @item --enable-prompt-string-decoding
6773 Turn on the interpretation of a number of backslash-escaped characters
6774 in the @env{$PS1}, @env{$PS2}, @env{$PS3}, and @env{$PS4} prompt
6775 strings. See @ref{Printing a Prompt}, for a complete list of prompt
6776 string escape sequences.
6778 @item --enable-progcomp
6779 Enable the programmable completion facilities
6780 (@pxref{Programmable Completion}).
6781 If Readline is not enabled, this option has no effect.
6783 @item --enable-readline
6784 Include support for command-line editing and history with the Bash
6785 version of the Readline library (@pxref{Command Line Editing}).
6787 @item --enable-restricted
6788 Include support for a @dfn{restricted shell}. If this is enabled, Bash,
6789 when called as @code{rbash}, enters a restricted mode. See
6790 @ref{The Restricted Shell}, for a description of restricted mode.
6792 @item --enable-select
6793 Include the @code{select} builtin, which allows the generation of simple
6794 menus (@pxref{Conditional Constructs}).
6796 @item --enable-usg-echo-default
6797 A synonym for @code{--enable-xpg-echo-default}.
6799 @item --enable-xpg-echo-default
6800 Make the @code{echo} builtin expand backslash-escaped characters by default,
6801 without requiring the @option{-e} option.
6802 This sets the default value of the @code{xpg_echo} shell option to @code{on},
6803 which makes the Bash @code{echo} behave more like the version specified in
6804 the Single Unix Specification, version 2.
6805 @xref{Bash Builtins}, for a description of the escape sequences that
6806 @code{echo} recognizes.
6810 The file @file{config-top.h} contains C Preprocessor
6811 @samp{#define} statements for options which are not settable from
6813 Some of these are not meant to be changed; beware of the consequences if
6815 Read the comments associated with each definition for more
6816 information about its effect.
6818 @node Reporting Bugs
6819 @appendix Reporting Bugs
6821 Please report all bugs you find in Bash.
6822 But first, you should
6823 make sure that it really is a bug, and that it appears in the latest
6825 The latest version of Bash is always available for FTP from
6826 @uref{ftp://ftp.gnu.org/pub/bash/}.
6828 Once you have determined that a bug actually exists, use the
6829 @code{bashbug} command to submit a bug report.
6830 If you have a fix, you are encouraged to mail that as well!
6831 Suggestions and `philosophical' bug reports may be mailed
6832 to @email{bug-bash@@gnu.org} or posted to the Usenet
6833 newsgroup @code{gnu.bash.bug}.
6835 All bug reports should include:
6838 The version number of Bash.
6840 The hardware and operating system.
6842 The compiler used to compile Bash.
6844 A description of the bug behaviour.
6846 A short script or `recipe' which exercises the bug and may be used
6851 @code{bashbug} inserts the first three items automatically into
6852 the template it provides for filing a bug report.
6854 Please send all reports concerning this manual to
6855 @email{chet@@po.CWRU.Edu}.
6857 @node Major Differences From The Bourne Shell
6858 @appendix Major Differences From The Bourne Shell
6860 Bash implements essentially the same grammar, parameter and
6861 variable expansion, redirection, and quoting as the Bourne Shell.
6862 Bash uses the @sc{posix} 1003.2 standard as the specification of
6863 how these features are to be implemented. There are some
6864 differences between the traditional Bourne shell and Bash; this
6865 section quickly details the differences of significance. A
6866 number of these differences are explained in greater depth in
6868 This section uses the version of @code{sh} included in SVR4.2 as
6869 the baseline reference.
6874 Bash is @sc{posix}-conformant, even where the @sc{posix} specification
6875 differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}).
6878 Bash has multi-character invocation options (@pxref{Invoking Bash}).
6881 Bash has command-line editing (@pxref{Command Line Editing}) and
6882 the @code{bind} builtin.
6885 Bash provides a programmable word completion mechanism
6886 (@pxref{Programmable Completion}), and two builtin commands,
6887 @code{complete} and @code{compgen}, to manipulate it.
6890 Bash has command history (@pxref{Bash History Facilities}) and the
6891 @code{history} and @code{fc} builtins to manipulate it.
6892 The Bash history list maintains timestamp information and uses the
6893 value of the @code{HISTTIMEFORMAT} variable to display it.
6896 Bash implements @code{csh}-like history expansion
6897 (@pxref{History Interaction}).
6900 Bash has one-dimensional array variables (@pxref{Arrays}), and the
6901 appropriate variable expansions and assignment syntax to use them.
6902 Several of the Bash builtins take options to act on arrays.
6903 Bash provides a number of built-in array variables.
6906 The @code{$'@dots{}'} quoting syntax, which expands ANSI-C
6907 backslash-escaped characters in the text between the single quotes,
6908 is supported (@pxref{ANSI-C Quoting}).
6911 Bash supports the @code{$"@dots{}"} quoting syntax to do
6912 locale-specific translation of the characters between the double
6913 quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings}
6914 invocation options list the translatable strings found in a script
6915 (@pxref{Locale Translation}).
6918 Bash implements the @code{!} keyword to negate the return value of
6919 a pipeline (@pxref{Pipelines}).
6920 Very useful when an @code{if} statement needs to act only if a test fails.
6923 Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}).
6924 The display of the timing statistics may be controlled with the
6925 @env{TIMEFORMAT} variable.
6928 Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))}
6929 arithmetic for command, similar to the C language (@pxref{Looping Constructs}).
6932 Bash includes the @code{select} compound command, which allows the
6933 generation of simple menus (@pxref{Conditional Constructs}).
6936 Bash includes the @code{[[} compound command, which makes conditional
6937 testing part of the shell grammar (@pxref{Conditional Constructs}).
6940 Bash includes brace expansion (@pxref{Brace Expansion}) and tilde
6941 expansion (@pxref{Tilde Expansion}).
6944 Bash implements command aliases and the @code{alias} and @code{unalias}
6945 builtins (@pxref{Aliases}).
6948 Bash provides shell arithmetic, the @code{((} compound command
6949 (@pxref{Conditional Constructs}),
6950 and arithmetic expansion (@pxref{Shell Arithmetic}).
6953 Variables present in the shell's initial environment are automatically
6954 exported to child processes. The Bourne shell does not normally do
6955 this unless the variables are explicitly marked using the @code{export}
6959 Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%}
6960 and @samp{##} expansions to remove leading or trailing substrings from
6961 variable values (@pxref{Shell Parameter Expansion}).
6964 The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}},
6965 is supported (@pxref{Shell Parameter Expansion}).
6968 The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}},
6969 which expands to the substring of @code{var}'s value of length
6970 @var{length}, beginning at @var{offset}, is present
6971 (@pxref{Shell Parameter Expansion}).
6975 @code{$@{var/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}},
6976 which matches @var{pattern} and replaces it with @var{replacement} in
6977 the value of @code{var}, is available (@pxref{Shell Parameter Expansion}).
6980 The expansion @code{$@{!@var{prefix@}*}} expansion, which expands to
6981 the names of all shell variables whose names begin with @var{prefix},
6982 is available (@pxref{Shell Parameter Expansion}).
6985 Bash has @var{indirect} variable expansion using @code{$@{!word@}}
6986 (@pxref{Shell Parameter Expansion}).
6989 Bash can expand positional parameters beyond @code{$9} using
6990 @code{$@{@var{num}@}}.
6993 The @sc{posix} @code{$()} form of command substitution
6994 is implemented (@pxref{Command Substitution}),
6995 and preferred to the Bourne shell's @code{``} (which
6996 is also implemented for backwards compatibility).
6999 Bash has process substitution (@pxref{Process Substitution}).
7002 Bash automatically assigns variables that provide information about the
7003 current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host
7004 (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}),
7005 and the instance of Bash that is running (@env{BASH},
7006 @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables},
7010 The @env{IFS} variable is used to split only the results of expansion,
7011 not all words (@pxref{Word Splitting}).
7012 This closes a longstanding shell security hole.
7015 Bash implements the full set of @sc{posix} 1003.2 filename expansion operators,
7016 including @var{character classes}, @var{equivalence classes}, and
7017 @var{collating symbols} (@pxref{Filename Expansion}).
7020 Bash implements extended pattern matching features when the @code{extglob}
7021 shell option is enabled (@pxref{Pattern Matching}).
7024 It is possible to have a variable and a function with the same name;
7025 @code{sh} does not separate the two name spaces.
7028 Bash functions are permitted to have local variables using the
7029 @code{local} builtin, and thus useful recursive functions may be written
7030 (@pxref{Bash Builtins}).
7033 Variable assignments preceding commands affect only that command, even
7034 builtins and functions (@pxref{Environment}).
7035 In @code{sh}, all variable assignments
7036 preceding commands are global unless the command is executed from the
7040 Bash performs filename expansion on filenames specified as operands
7041 to input and output redirection operators (@pxref{Redirections}).
7044 Bash contains the @samp{<>} redirection operator, allowing a file to be
7045 opened for both reading and writing, and the @samp{&>} redirection
7046 operator, for directing standard output and standard error to the same
7047 file (@pxref{Redirections}).
7050 Bash treats a number of filenames specially when they are
7051 used in redirection operators (@pxref{Redirections}).
7054 Bash can open network connections to arbitrary machines and services
7055 with the redirection operators (@pxref{Redirections}).
7058 The @code{noclobber} option is available to avoid overwriting existing
7059 files with output redirection (@pxref{The Set Builtin}).
7060 The @samp{>|} redirection operator may be used to override @code{noclobber}.
7063 The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins})
7064 each take @option{-L} and @option{-P} options to switch between logical and
7068 Bash allows a function to override a builtin with the same name, and provides
7069 access to that builtin's functionality within the function via the
7070 @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}).
7073 The @code{command} builtin allows selective disabling of functions
7074 when command lookup is performed (@pxref{Bash Builtins}).
7077 Individual builtins may be enabled or disabled using the @code{enable}
7078 builtin (@pxref{Bash Builtins}).
7081 The Bash @code{exec} builtin takes additional options that allow users
7082 to control the contents of the environment passed to the executed
7083 command, and what the zeroth argument to the command is to be
7084 (@pxref{Bourne Shell Builtins}).
7087 Shell functions may be exported to children via the environment
7088 using @code{export -f} (@pxref{Shell Functions}).
7091 The Bash @code{export}, @code{readonly}, and @code{declare} builtins can
7092 take a @option{-f} option to act on shell functions, a @option{-p} option to
7093 display variables with various attributes set in a format that can be
7094 used as shell input, a @option{-n} option to remove various variable
7095 attributes, and @samp{name=value} arguments to set variable attributes
7096 and values simultaneously.
7099 The Bash @code{hash} builtin allows a name to be associated with
7100 an arbitrary filename, even when that filename cannot be found by
7101 searching the @env{$PATH}, using @samp{hash -p}
7102 (@pxref{Bourne Shell Builtins}).
7105 Bash includes a @code{help} builtin for quick reference to shell
7106 facilities (@pxref{Bash Builtins}).
7109 The @code{printf} builtin is available to display formatted output
7110 (@pxref{Bash Builtins}).
7113 The Bash @code{read} builtin (@pxref{Bash Builtins})
7114 will read a line ending in @samp{\} with
7115 the @option{-r} option, and will use the @env{REPLY} variable as a
7116 default if no non-option arguments are supplied.
7117 The Bash @code{read} builtin
7118 also accepts a prompt string with the @option{-p} option and will use
7119 Readline to obtain the line when given the @option{-e} option.
7120 The @code{read} builtin also has additional options to control input:
7121 the @option{-s} option will turn off echoing of input characters as
7122 they are read, the @option{-t} option will allow @code{read} to time out
7123 if input does not arrive within a specified number of seconds, the
7124 @option{-n} option will allow reading only a specified number of
7125 characters rather than a full line, and the @option{-d} option will read
7126 until a particular character rather than newline.
7129 The @code{return} builtin may be used to abort execution of scripts
7130 executed with the @code{.} or @code{source} builtins
7131 (@pxref{Bourne Shell Builtins}).
7134 Bash includes the @code{shopt} builtin, for finer control of shell
7135 optional capabilities (@pxref{Bash Builtins}), and allows these options
7136 to be set and unset at shell invocation (@pxref{Invoking Bash}).
7139 Bash has much more optional behavior controllable with the @code{set}
7140 builtin (@pxref{The Set Builtin}).
7143 The @samp{-x} (@code{xtrace}) option displays commands other than
7144 simple commands when performing an execution trace
7145 (@pxref{The Set Builtin}).
7148 The @code{test} builtin (@pxref{Bourne Shell Builtins})
7149 is slightly different, as it implements the @sc{posix} algorithm,
7150 which specifies the behavior based on the number of arguments.
7153 Bash includes the @code{caller} builtin, which displays the context of
7154 any active subroutine call (a shell function or a script executed with
7155 the @code{.} or @code{source} builtins). This supports the bash
7159 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
7160 @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}.
7161 Commands specified with a @code{DEBUG} trap are executed before every
7162 simple command, @code{for} command, @code{case} command,
7163 @code{select} command, every arithmetic @code{for} command, and before
7164 the first command executes in a shell function.
7165 The @code{DEBUG} trap is not inherited by shell functions unless the
7166 function has been given the @code{trace} attribute or the
7167 @code{functrace} option has been enabled using the @code{shopt} builtin.
7168 The @code{extdebug} shell option has additional effects on the
7171 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an
7172 @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}.
7173 Commands specified with an @code{ERR} trap are executed after a simple
7174 command fails, with a few exceptions.
7175 The @code{ERR} trap is not inherited by shell functions unless the
7176 @code{-o errtrace} option to the @code{set} builtin is enabled.
7178 The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
7179 @code{RETURN} pseudo-signal specification, similar to
7180 @code{EXIT} and @code{DEBUG}.
7181 Commands specified with an @code{RETURN} trap are executed before
7182 execution resumes after a shell function or a shell script executed with
7183 @code{.} or @code{source} returns.
7184 The @code{RETURN} trap is not inherited by shell functions.
7187 The Bash @code{type} builtin is more extensive and gives more information
7188 about the names it finds (@pxref{Bash Builtins}).
7191 The Bash @code{umask} builtin permits a @option{-p} option to cause
7192 the output to be displayed in the form of a @code{umask} command
7193 that may be reused as input (@pxref{Bourne Shell Builtins}).
7196 Bash implements a @code{csh}-like directory stack, and provides the
7197 @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it
7198 (@pxref{The Directory Stack}).
7199 Bash also makes the directory stack visible as the value of the
7200 @env{DIRSTACK} shell variable.
7203 Bash interprets special backslash-escaped characters in the prompt
7204 strings when interactive (@pxref{Printing a Prompt}).
7207 The Bash restricted mode is more useful (@pxref{The Restricted Shell});
7208 the SVR4.2 shell restricted mode is too limited.
7211 The @code{disown} builtin can remove a job from the internal shell
7212 job table (@pxref{Job Control Builtins}) or suppress the sending
7213 of @code{SIGHUP} to a job when the shell exits as the result of a
7217 The SVR4.2 shell has two privilege-related builtins
7218 (@code{mldmode} and @code{priv}) not present in Bash.
7221 Bash does not have the @code{stop} or @code{newgrp} builtins.
7224 Bash does not use the @env{SHACCT} variable or perform shell accounting.
7227 The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses
7233 More features unique to Bash may be found in @ref{Bash Features}.
7236 @appendixsec Implementation Differences From The SVR4.2 Shell
7238 Since Bash is a completely new implementation, it does not suffer from
7239 many of the limitations of the SVR4.2 shell. For instance:
7244 Bash does not fork a subshell when redirecting into or out of
7245 a shell control structure such as an @code{if} or @code{while}
7249 Bash does not allow unbalanced quotes. The SVR4.2 shell will silently
7250 insert a needed closing quote at @code{EOF} under certain circumstances.
7251 This can be the cause of some hard-to-find errors.
7254 The SVR4.2 shell uses a baroque memory management scheme based on
7255 trapping @code{SIGSEGV}. If the shell is started from a process with
7256 @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library
7257 function call), it misbehaves badly.
7260 In a questionable attempt at security, the SVR4.2 shell,
7261 when invoked without the @option{-p} option, will alter its real
7262 and effective @sc{uid} and @sc{gid} if they are less than some
7263 magic threshold value, commonly 100.
7264 This can lead to unexpected results.
7267 The SVR4.2 shell does not allow users to trap @code{SIGSEGV},
7268 @code{SIGALRM}, or @code{SIGCHLD}.
7271 The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK},
7272 @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset.
7275 The SVR4.2 shell treats @samp{^} as the undocumented equivalent of
7279 Bash allows multiple option arguments when it is invoked (@code{-x -v});
7280 the SVR4.2 shell allows only one option argument (@code{-xv}). In
7281 fact, some versions of the shell dump core if the second argument begins
7285 The SVR4.2 shell exits a script if any builtin fails; Bash exits
7286 a script only if one of the @sc{posix} 1003.2 special builtins fails, and
7287 only for certain failures, as enumerated in the @sc{posix} 1003.2 standard.
7290 The SVR4.2 shell behaves differently when invoked as @code{jsh}
7291 (it turns on job control).
7294 @node Copying This Manual
7295 @appendix Copying This Manual
7298 * GNU Free Documentation License:: License for copying this manual.
7304 @unnumbered Index of Shell Builtin Commands
7307 @node Reserved Word Index
7308 @unnumbered Index of Shell Reserved Words
7311 @node Variable Index
7312 @unnumbered Parameter and Variable Index
7315 @node Function Index
7316 @unnumbered Function Index
7320 @unnumbered Concept Index