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