]> git.ipfire.org Git - thirdparty/bash.git/blame - doc/bashref.texi
updated translations; remove unneeded files
[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
74091dd4 17Copyright @copyright{} 1988--2022 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
74091dd4 423@dfn{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
74091dd4 428@dfn{escape character}, single quotes, and double quotes.
ccc6cda3
JA
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
74091dd4
CR
478Character sequences of the form $'@var{string}' are treated as a special
479kind of single quotes.
480The sequence expands to @var{string}, with backslash-escaped characters
481in @var{string} replaced as specified by the ANSI C standard.
482Backslash escape sequences, if present, are decoded as follows:
ccc6cda3
JA
483
484@table @code
485@item \a
486alert (bell)
487@item \b
488backspace
489@item \e
0001803f 490@itemx \E
ccc6cda3
JA
491an escape character (not ANSI C)
492@item \f
493form feed
494@item \n
495newline
496@item \r
497carriage return
498@item \t
499horizontal tab
500@item \v
501vertical tab
502@item \\
503backslash
bb70624e
JA
504@item \'
505single quote
0001803f
CR
506@item \"
507double quote
a0c0a00f
CR
508@item \?
509question mark
ccc6cda3 510@item \@var{nnn}
f73dda09 511the eight-bit character whose value is the octal value @var{nnn}
d233b485 512(one to three octal digits)
f73dda09
JA
513@item \x@var{HH}
514the eight-bit character whose value is the hexadecimal value @var{HH}
515(one or two hex digits)
495aee44
CR
516@item \u@var{HHHH}
517the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
518@var{HHHH} (one to four hex digits)
519@item \U@var{HHHHHHHH}
520the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
521@var{HHHHHHHH} (one to eight hex digits)
7117c2d2
JA
522@item \c@var{x}
523a control-@var{x} character
ccc6cda3
JA
524@end table
525
526@noindent
bb70624e
JA
527The expanded result is single-quoted, as if the dollar sign had not
528been present.
ccc6cda3
JA
529
530@node Locale Translation
531@subsubsection Locale-Specific Translation
532@cindex localization
f73dda09
JA
533@cindex internationalization
534@cindex native languages
535@cindex translation, native languages
74091dd4
CR
536@menu
537* Creating Internationalized Scripts:: How to use translations and different
538 languages in your scripts.
539@end menu
ccc6cda3 540
74091dd4
CR
541Prefixing a double-quoted string with a dollar sign (@samp{$}), such
542as @verb{|$"hello, world"|},
8868edaf 543will cause the string to be translated according to the current locale.
74091dd4
CR
544The @code{gettext} infrastructure performs the lookup and
545translation, using the @code{LC_MESSAGES}, @code{TEXTDOMAINDIR},
546and @code{TEXTDOMAIN} shell variables, as explained below.
547See the gettext documentation for additional details not covered here.
8868edaf 548If the current locale is @code{C} or @code{POSIX},
74091dd4
CR
549if there are no translations available,
550of if the string is not translated,
8868edaf 551the dollar sign is ignored.
74091dd4
CR
552Since this is a form of double quoting, the string remains double-quoted
553by default, whether or not it is translated and replaced.
554If the @code{noexpand_translation} option is enabled
555using the @code{shopt} builtin (@pxref{The Shopt Builtin}),
556translated strings are single-quoted instead of double-quoted.
557
558The rest of this section is a brief overview of how you use gettext to
559create translations for strings in a shell script named @var{scriptname}.
560There are more details in the gettext documentation.
561
562@node Creating Internationalized Scripts
563@cindex internationalized scripts
564@cindex string translations
565Once you've marked the strings in your script
566that you want to translate using $"...",
567you create a gettext "template" file using the command
568
569@example
570bash --dump-po-strings @var{scriptname} > @var{domain}.pot
571@end example
572
573@noindent
574The @var{domain} is your @dfn{message domain}.
575It's just an arbitrary string that's used to identify the files gettext
576needs, like a package or script name.
577It needs to be unique among all
578the message domains on systems where you install the translations, so
579gettext knows which translations correspond to your script.
580You'll use the template file to create translations for each target language.
581The template file conventionally has the suffix @samp{.pot}.
582
583You copy this template file to a separate file for each target language
584you want to support (called "PO" files, which use the suffix @samp{.po}).
585PO files use various naming conventions, but
586when you are working to translate a template file into a particular
587language, you first copy the template file to a file whose name is the
588language you want to target, with the @samp{.po} suffix.
589For instance, the Spanish translations of your strings would be
590in a file named @samp{es.po}, and to get started using a message
591domain named "example," you would run
592
593@example
594cp example.pot es.po
595@end example
596
597@noindent
598Ultimately, PO files are often named @var{domain}.po and installed in
599directories that contain multiple translation files for a particular language.
600
601Whichever naming convention you choose, you will need to translate the
602strings in the PO files into the appropriate languages.
603This has to be done manually.
604
605When you have the translations and PO files complete, you'll use the
606gettext tools to produce what are called "MO" files, which are compiled
607versions of the PO files the gettext tools use to look up translations
608efficiently.
609MO files are also called "message catalog" files.
610You use the @command{msgfmt} program to do this.
611For instance, if you had a file with Spanish translations, you could run
ccc6cda3 612
74091dd4
CR
613@example
614msgfmt -o es.mo es.po
615@end example
616
617@noindent
618to produce the corresponding MO file.
619
620Once you have the MO files, you decide where to install them and use the
621@code{TEXTDOMAINDIR} shell variable to tell the gettext tools where they are.
622Make sure to use the same message domain to name the MO files
623as you did for the PO files when you install them.
624
625@vindex LANG
f73dda09
JA
626@vindex LC_MESSAGES
627@vindex TEXTDOMAIN
628@vindex TEXTDOMAINDIR
74091dd4
CR
629Your users will use the @env{LANG} or @env{LC_MESSAGES} shell variables to
630select the desired language.
631
632You set the @env{TEXTDOMAIN} variable to the script's message domain.
633As above, you use the message domain to name your translation files.
634
635You, or possibly your users, set the @env{TEXTDOMAINDIR} variable to the
636name of a directory where the message catalog files are stored.
637If you install the message files into the system's standard message catalog
638directory, you don't need to worry about this variable.
639
640The directory where the message catalog files are stored varies between
641systems.
642Some use the message catalog selected by the @env{LC_MESSAGES}
643shell variable.
644Others create the name of the message catalog from the value of the
645@env{TEXTDOMAIN} shell variable, possibly adding the @samp{.mo} suffix.
646If you use the @env{TEXTDOMAIN} variable, you may need to set the
647@env{TEXTDOMAINDIR} variable to the location of the message catalog files,
648as above.
649It's common to use both variables in this fashion:
650@env{$TEXTDOMAINDIR}/@env{$LC_MESSAGES}/LC_MESSAGES/@env{$TEXTDOMAIN}.mo.
651
652If you used that last convention, and you wanted to store the message
653catalog files with Spanish (es) and Esperanto (eo) translations into a
654local directory you use for custom translation files, you could run
655
656@example
657TEXTDOMAIN=example
658TEXTDOMAINDIR=/usr/local/share/locale
659
660cp es.mo $@{TEXTDOMAINDIR@}/es/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
661cp eo.mo $@{TEXTDOMAINDIR@}/eo/LC_MESSAGES/$@{TEXTDOMAIN@}.mo
662@end example
663
664When all of this is done, and the message catalog files containing the
665compiled translations are installed in the correct location,
666your users will be able to see translated strings
667in any of the supported languages by setting the @env{LANG} or
668@env{LC_MESSAGES} environment variables before running your script.
28ef6c31 669
ccc6cda3
JA
670@node Comments
671@subsection Comments
672@cindex comments, shell
673
674In a non-interactive shell, or an interactive shell in which the
675@code{interactive_comments} option to the @code{shopt}
3185942a 676builtin is enabled (@pxref{The Shopt Builtin}),
ccc6cda3
JA
677a word beginning with @samp{#}
678causes that word and all remaining characters on that line to
679be ignored. An interactive shell without the @code{interactive_comments}
680option enabled does not allow comments. The @code{interactive_comments}
681option is on by default in interactive shells.
bb70624e 682@xref{Interactive Shells}, for a description of what makes
cce855bc
JA
683a shell interactive.
684
685@node Shell Commands
686@section Shell Commands
687@cindex commands, shell
bb70624e
JA
688
689A simple shell command such as @code{echo a b c} consists of the command
690itself followed by arguments, separated by spaces.
691
692More complex shell commands are composed of simple commands arranged together
693in a variety of ways: in a pipeline in which the output of one command
694becomes the input of a second, in a loop or conditional construct, or in
695some other grouping.
696
cce855bc 697@menu
8868edaf 698* Reserved Words:: Words that have special meaning to the shell.
cce855bc
JA
699* Simple Commands:: The most common type of command.
700* Pipelines:: Connecting the input and output of several
701 commands.
702* Lists:: How to execute commands sequentially.
b80f6443 703* Compound Commands:: Shell commands for control flow.
3185942a 704* Coprocesses:: Two-way communication between commands.
495aee44 705* GNU Parallel:: Running commands in parallel.
cce855bc 706@end menu
ccc6cda3 707
8868edaf
CR
708@node Reserved Words
709@subsection Reserved Words
710@cindex reserved words
711
712Reserved words are words that have special meaning to the shell.
713They are used to begin and end the shell's compound commands.
714
715The following words are recognized as reserved when unquoted and
716the first word of a command (see below for exceptions):
717
718@multitable @columnfractions .1 .1 .1 .1 .12 .1
719@item @code{if} @tab @code{then} @tab @code{elif}
720@tab @code{else} @tab @code{fi} @tab @code{time}
721@item @code{for} @tab @code{in} @tab @code{until}
722@tab @code{while} @tab @code{do} @tab @code{done}
723@item @code{case} @tab @code{esac} @tab @code{coproc}
724@tab @code{select} @tab @code{function}
725@item @code{@{} @tab @code{@}} @tab @code{[[} @tab @code{]]} @tab @code{!}
726@end multitable
727
728@noindent
729@code{in} is recognized as a reserved word if it is the third word of a
730@code{case} or @code{select} command.
731@code{in} and @code{do} are recognized as reserved
732words if they are the third word in a @code{for} command.
733
ccc6cda3 734@node Simple Commands
cce855bc 735@subsection Simple Commands
ccc6cda3
JA
736@cindex commands, simple
737
cce855bc 738A simple command is the kind of command encountered most often.
ccc6cda3 739It's just a sequence of words separated by @code{blank}s, terminated
cce855bc 740by one of the shell's control operators (@pxref{Definitions}). The
bb70624e
JA
741first word generally specifies a command to be executed, with the
742rest of the words being that command's arguments.
ccc6cda3
JA
743
744The return status (@pxref{Exit Status}) of a simple command is
745its exit status as provided
bb70624e
JA
746by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if
747the command was terminated by signal @var{n}.
ccc6cda3
JA
748
749@node Pipelines
cce855bc 750@subsection Pipelines
ccc6cda3
JA
751@cindex pipeline
752@cindex commands, pipelines
753
a0c0a00f
CR
754A @code{pipeline} is a sequence of one or more commands separated by
755one of the control operators @samp{|} or @samp{|&}.
ccc6cda3
JA
756
757@rwindex time
758@rwindex !
759@cindex command timing
760The format for a pipeline is
761@example
ac50fbac 762[time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{}
ccc6cda3
JA
763@end example
764
765@noindent
f73dda09
JA
766The output of each command in the pipeline is connected via a pipe
767to the input of the next command.
3185942a 768That is, each command reads the previous command's output. This
74091dd4
CR
769connection is performed before any redirections specified by
770@var{command1}.
3185942a 771
ac50fbac
CR
772If @samp{|&} is used, @var{command1}'s standard error, in addition to
773its standard output, is connected to
774@var{command2}'s standard input through the pipe;
775it is shorthand for @code{2>&1 |}.
776This implicit redirection of the standard error to the standard output is
74091dd4 777performed after any redirections specified by @var{command1}.
ccc6cda3
JA
778
779The reserved word @code{time} causes timing statistics
780to be printed for the pipeline once it finishes.
cce855bc
JA
781The statistics currently consist of elapsed (wall-clock) time and
782user and system time consumed by the command's execution.
28ef6c31 783The @option{-p} option changes the output format to that specified
bb70624e 784by @sc{posix}.
495aee44
CR
785When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
786it does not recognize @code{time} as a reserved word if the next
787token begins with a @samp{-}.
28ef6c31 788The @env{TIMEFORMAT} variable may be set to a format string that
ccc6cda3
JA
789specifies how the timing information should be displayed.
790@xref{Bash Variables}, for a description of the available formats.
cce855bc
JA
791The use of @code{time} as a reserved word permits the timing of
792shell builtins, shell functions, and pipelines. An external
793@code{time} command cannot time these easily.
794
495aee44
CR
795When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time}
796may be followed by a newline. In this case, the shell displays the
797total user and system time consumed by the shell and its children.
798The @env{TIMEFORMAT} variable may be used to specify the format of
799the time information.
800
cce855bc
JA
801If the pipeline is not executed asynchronously (@pxref{Lists}), the
802shell waits for all commands in the pipeline to complete.
ccc6cda3 803
74091dd4
CR
804Each command in a multi-command pipeline,
805where pipes are created,
806is executed in its own @dfn{subshell}, which is a
d233b485
CR
807separate process (@pxref{Command Execution Environment}).
808If the @code{lastpipe} option is enabled using the @code{shopt} builtin
809(@pxref{The Shopt Builtin}),
74091dd4
CR
810the last element of a pipeline may be run by the shell process
811when job control is not active.
d233b485
CR
812
813The exit
ccc6cda3 814status of a pipeline is the exit status of the last command in the
b80f6443
JA
815pipeline, unless the @code{pipefail} option is enabled
816(@pxref{The Set Builtin}).
817If @code{pipefail} is enabled, the pipeline's return status is the
818value of the last (rightmost) command to exit with a non-zero status,
819or zero if all commands exit successfully.
820If the reserved word @samp{!} precedes the pipeline, the
821exit status is the logical negation of the exit status as described
822above.
823The shell waits for all commands in the pipeline to terminate before
824returning a value.
ccc6cda3
JA
825
826@node Lists
cce855bc 827@subsection Lists of Commands
ccc6cda3
JA
828@cindex commands, lists
829
830A @code{list} is a sequence of one or more pipelines separated by one
831of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||},
832and optionally terminated by one of @samp{;}, @samp{&}, or a
833@code{newline}.
834
835Of these list operators, @samp{&&} and @samp{||}
836have equal precedence, followed by @samp{;} and @samp{&},
837which have equal precedence.
838
7117c2d2
JA
839A sequence of one or more newlines may appear in a @code{list}
840to delimit commands, equivalent to a semicolon.
841
ccc6cda3 842If a command is terminated by the control operator @samp{&},
cce855bc 843the shell executes the command asynchronously in a subshell.
74091dd4
CR
844This is known as executing the command in the @dfn{background},
845and these are referred to as @dfn{asynchronous} commands.
cce855bc
JA
846The shell does not wait for the command to finish, and the return
847status is 0 (true).
bb70624e
JA
848When job control is not active (@pxref{Job Control}),
849the standard input for asynchronous commands, in the absence of any
cce855bc
JA
850explicit redirections, is redirected from @code{/dev/null}.
851
852Commands separated by a @samp{;} are executed sequentially; the shell
853waits for each command to terminate in turn. The return status is the
ccc6cda3
JA
854exit status of the last command executed.
855
3185942a
JA
856@sc{and} and @sc{or} lists are sequences of one or more pipelines
857separated by the control operators @samp{&&} and @samp{||},
858respectively. @sc{and} and @sc{or} lists are executed with left
859associativity.
860
bb70624e 861An @sc{and} list has the form
ccc6cda3 862@example
bb70624e 863@var{command1} && @var{command2}
ccc6cda3
JA
864@end example
865
866@noindent
bb70624e 867@var{command2} is executed if, and only if, @var{command1}
d233b485 868returns an exit status of zero (success).
ccc6cda3 869
bb70624e 870An @sc{or} list has the form
ccc6cda3 871@example
bb70624e 872@var{command1} || @var{command2}
ccc6cda3
JA
873@end example
874
875@noindent
bb70624e 876@var{command2} is executed if, and only if, @var{command1}
ccc6cda3
JA
877returns a non-zero exit status.
878
879The return status of
bb70624e 880@sc{and} and @sc{or} lists is the exit status of the last command
ccc6cda3
JA
881executed in the list.
882
b80f6443
JA
883@node Compound Commands
884@subsection Compound Commands
885@cindex commands, compound
886
887@menu
888* Looping Constructs:: Shell commands for iterative action.
889* Conditional Constructs:: Shell commands for conditional execution.
890* Command Grouping:: Ways to group commands.
891@end menu
892
d233b485 893Compound commands are the shell programming language constructs.
b80f6443
JA
894Each construct begins with a reserved word or control operator and is
895terminated by a corresponding reserved word or operator.
896Any redirections (@pxref{Redirections}) associated with a compound command
897apply to all commands within that compound command unless explicitly overridden.
898
ac50fbac
CR
899In most cases a list of commands in a compound command's description may be
900separated from the rest of the command by one or more newlines, and may be
901followed by a newline in place of a semicolon.
902
b80f6443
JA
903Bash provides looping constructs, conditional commands, and mechanisms
904to group commands and execute them as a unit.
905
ccc6cda3 906@node Looping Constructs
b80f6443 907@subsubsection Looping Constructs
ccc6cda3
JA
908@cindex commands, looping
909
ccc6cda3
JA
910Bash supports the following looping constructs.
911
bb70624e 912Note that wherever a @samp{;} appears in the description of a
cce855bc
JA
913command's syntax, it may be replaced with one or more newlines.
914
ccc6cda3
JA
915@table @code
916@item until
917@rwindex until
918@rwindex do
919@rwindex done
920The syntax of the @code{until} command is:
ac50fbac 921
ccc6cda3
JA
922@example
923until @var{test-commands}; do @var{consequent-commands}; done
924@end example
ac50fbac 925
cce855bc 926Execute @var{consequent-commands} as long as
ccc6cda3 927@var{test-commands} has an exit status which is not zero.
cce855bc
JA
928The return status is the exit status of the last command executed
929in @var{consequent-commands}, or zero if none was executed.
ccc6cda3
JA
930
931@item while
932@rwindex while
933The syntax of the @code{while} command is:
ac50fbac 934
ccc6cda3
JA
935@example
936while @var{test-commands}; do @var{consequent-commands}; done
937@end example
938
cce855bc 939Execute @var{consequent-commands} as long as
ccc6cda3 940@var{test-commands} has an exit status of zero.
cce855bc
JA
941The return status is the exit status of the last command executed
942in @var{consequent-commands}, or zero if none was executed.
ccc6cda3
JA
943
944@item for
945@rwindex for
946The syntax of the @code{for} command is:
947
948@example
0001803f 949for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done
ccc6cda3 950@end example
ac50fbac 951
d233b485
CR
952Expand @var{words} (@pxref{Shell Expansions}), and execute @var{commands}
953once for each member
cce855bc 954in the resultant list, with @var{name} bound to the current member.
bb70624e
JA
955If @samp{in @var{words}} is not present, the @code{for} command
956executes the @var{commands} once for each positional parameter that is
957set, as if @samp{in "$@@"} had been specified
958(@pxref{Special Parameters}).
d233b485 959
cce855bc
JA
960The return status is the exit status of the last command that executes.
961If there are no items in the expansion of @var{words}, no commands are
962executed, and the return status is zero.
bb70624e
JA
963
964An alternate form of the @code{for} command is also supported:
965
966@example
967for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done
968@end example
ac50fbac 969
bb70624e
JA
970First, the arithmetic expression @var{expr1} is evaluated according
971to the rules described below (@pxref{Shell Arithmetic}).
972The arithmetic expression @var{expr2} is then evaluated repeatedly
973until it evaluates to zero.
974Each time @var{expr2} evaluates to a non-zero value, @var{commands} are
975executed and the arithmetic expression @var{expr3} is evaluated.
976If any expression is omitted, it behaves as if it evaluates to 1.
495aee44 977The return value is the exit status of the last command in @var{commands}
bb70624e 978that is executed, or false if any of the expressions is invalid.
ccc6cda3
JA
979@end table
980
981The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins})
982may be used to control loop execution.
983
984@node Conditional Constructs
b80f6443 985@subsubsection Conditional Constructs
ccc6cda3
JA
986@cindex commands, conditional
987
988@table @code
989@item if
990@rwindex if
991@rwindex then
992@rwindex else
993@rwindex elif
994@rwindex fi
995The syntax of the @code{if} command is:
996
997@example
998if @var{test-commands}; then
999 @var{consequent-commands};
1000[elif @var{more-test-commands}; then
1001 @var{more-consequents};]
1002[else @var{alternate-consequents};]
1003fi
1004@end example
1005
cce855bc
JA
1006The @var{test-commands} list is executed, and if its return status is zero,
1007the @var{consequent-commands} list is executed.
1008If @var{test-commands} returns a non-zero status, each @code{elif} list
1009is executed in turn, and if its exit status is zero,
ccc6cda3
JA
1010the corresponding @var{more-consequents} is executed and the
1011command completes.
1012If @samp{else @var{alternate-consequents}} is present, and
1013the final command in the final @code{if} or @code{elif} clause
cce855bc
JA
1014has a non-zero exit status, then @var{alternate-consequents} is executed.
1015The return status is the exit status of the last command executed, or
1016zero if no condition tested true.
ccc6cda3
JA
1017
1018@item case
1019@rwindex case
1020@rwindex in
1021@rwindex esac
1022The syntax of the @code{case} command is:
1023
1024@example
d233b485
CR
1025case @var{word} in
1026 [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{}
1027esac
ccc6cda3
JA
1028@end example
1029
cce855bc
JA
1030@code{case} will selectively execute the @var{command-list} corresponding to
1031the first @var{pattern} that matches @var{word}.
d233b485
CR
1032The match is performed according
1033to the rules described below in @ref{Pattern Matching}.
a0c0a00f 1034If the @code{nocasematch} shell option
3185942a 1035(see the description of @code{shopt} in @ref{The Shopt Builtin})
95732b49
JA
1036is enabled, the match is performed without regard to the case
1037of alphabetic characters.
cce855bc
JA
1038The @samp{|} is used to separate multiple patterns, and the @samp{)}
1039operator terminates a pattern list.
1040A list of patterns and an associated command-list is known
3185942a
JA
1041as a @var{clause}.
1042
0001803f 1043Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}.
cce855bc 1044The @var{word} undergoes tilde expansion, parameter expansion, command
d233b485
CR
1045substitution, arithmetic expansion, and quote removal
1046(@pxref{Shell Parameter Expansion})
74091dd4
CR
1047before matching is attempted.
1048Each @var{pattern} undergoes tilde expansion, parameter expansion,
1049command substitution, arithmetic expansion, process substitution, and
1050quote removal.
cce855bc
JA
1051
1052There may be an arbitrary number of @code{case} clauses, each terminated
3185942a
JA
1053by a @samp{;;}, @samp{;&}, or @samp{;;&}.
1054The first pattern that matches determines the
cce855bc 1055command-list that is executed.
ac50fbac
CR
1056It's a common idiom to use @samp{*} as the final pattern to define the
1057default case, since that pattern will always match.
ccc6cda3
JA
1058
1059Here is an example using @code{case} in a script that could be used to
1060describe one interesting feature of an animal:
1061
1062@example
1063echo -n "Enter the name of an animal: "
1064read ANIMAL
1065echo -n "The $ANIMAL has "
1066case $ANIMAL in
1067 horse | dog | cat) echo -n "four";;
1068 man | kangaroo ) echo -n "two";;
1069 *) echo -n "an unknown number of";;
1070esac
1071echo " legs."
1072@end example
1073
cce855bc 1074@noindent
3185942a
JA
1075
1076If the @samp{;;} operator is used, no subsequent matches are attempted after
1077the first pattern match.
1078Using @samp{;&} in place of @samp{;;} causes execution to continue with
1079the @var{command-list} associated with the next clause, if any.
1080Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns
1081in the next clause, if any, and execute any associated @var{command-list}
8868edaf
CR
1082on a successful match,
1083continuing the case statement execution as if the pattern list had not matched.
3185942a 1084
cce855bc
JA
1085The return status is zero if no @var{pattern} is matched. Otherwise, the
1086return status is the exit status of the @var{command-list} executed.
1087
1088@item select
1089@rwindex select
1090
1091The @code{select} construct allows the easy generation of menus.
1092It has almost the same syntax as the @code{for} command:
1093
1094@example
1095select @var{name} [in @var{words} @dots{}]; do @var{commands}; done
1096@end example
1097
1098The list of words following @code{in} is expanded, generating a list
74091dd4 1099of items, and the set of expanded words is printed on the standard
cce855bc
JA
1100error output stream, each preceded by a number. If the
1101@samp{in @var{words}} is omitted, the positional parameters are printed,
3185942a 1102as if @samp{in "$@@"} had been specified.
74091dd4
CR
1103@code{select} then displays the @env{PS3}
1104prompt and reads a line from the standard input.
cce855bc
JA
1105If the line consists of a number corresponding to one of the displayed
1106words, then the value of @var{name} is set to that word.
1107If the line is empty, the words and prompt are displayed again.
74091dd4 1108If @code{EOF} is read, the @code{select} command completes and returns 1.
cce855bc 1109Any other value read causes @var{name} to be set to null.
28ef6c31 1110The line read is saved in the variable @env{REPLY}.
cce855bc
JA
1111
1112The @var{commands} are executed after each selection until a
f73dda09 1113@code{break} command is executed, at which
cce855bc
JA
1114point the @code{select} command completes.
1115
1116Here is an example that allows the user to pick a filename from the
1117current directory, and displays the name and index of the file
1118selected.
1119
1120@example
1121select fname in *;
1122do
1123 echo you picked $fname \($REPLY\)
1124 break;
1125done
1126@end example
1127
ccc6cda3
JA
1128@item ((@dots{}))
1129@example
1130(( @var{expression} ))
1131@end example
1132
cce855bc
JA
1133The arithmetic @var{expression} is evaluated according to the rules
1134described below (@pxref{Shell Arithmetic}).
74091dd4
CR
1135The @var{expression} undergoes the same expansions
1136as if it were within double quotes,
1137but double quote characters in @var{expression} are not treated specially
1138are removed.
ccc6cda3 1139If the value of the expression is non-zero, the return status is 0;
74091dd4
CR
1140otherwise the return status is 1.
1141
cce855bc
JA
1142
1143@item [[@dots{}]]
1144@rwindex [[
1145@rwindex ]]
1146@example
1147[[ @var{expression} ]]
1148@end example
1149
1150Return a status of 0 or 1 depending on the evaluation of
1151the conditional expression @var{expression}.
1152Expressions are composed of the primaries described below in
1153@ref{Bash Conditional Expressions}.
74091dd4
CR
1154The words between the @code{[[} and @code{]]} do not undergo word splitting
1155and filename expansion.
1156The shell performs tilde expansion, parameter and
cce855bc 1157variable expansion, arithmetic expansion, command substitution, process
74091dd4
CR
1158substitution, and quote removal on those words
1159(the expansions that would occur if the words were enclosed in double quotes).
b80f6443
JA
1160Conditional operators such as @samp{-f} must be unquoted to be recognized
1161as primaries.
cce855bc 1162
ac50fbac 1163When used with @code{[[}, the @samp{<} and @samp{>} operators sort
0001803f
CR
1164lexicographically using the current locale.
1165
cce855bc
JA
1166When the @samp{==} and @samp{!=} operators are used, the string to the
1167right of the operator is considered a pattern and matched according
ac50fbac
CR
1168to the rules described below in @ref{Pattern Matching},
1169as if the @code{extglob} shell option were enabled.
1170The @samp{=} operator is identical to @samp{==}.
a0c0a00f 1171If the @code{nocasematch} shell option
3185942a 1172(see the description of @code{shopt} in @ref{The Shopt Builtin})
95732b49
JA
1173is enabled, the match is performed without regard to the case
1174of alphabetic characters.
0628567a 1175The return value is 0 if the string matches (@samp{==}) or does not
d233b485 1176match (@samp{!=}) the pattern, and 1 otherwise.
74091dd4
CR
1177
1178If you quote any part of the pattern,
1179using any of the shell's quoting mechanisms,
1180the quoted portion is matched literally.
1181This means every character in the quoted portion matches itself,
1182instead of having any special pattern matching meaning.
cce855bc 1183
b80f6443
JA
1184An additional binary operator, @samp{=~}, is available, with the same
1185precedence as @samp{==} and @samp{!=}.
74091dd4
CR
1186When you use @samp{=~}, the string to the right of the operator is considered
1187a @sc{posix} extended regular expression pattern and matched accordingly
8868edaf
CR
1188(using the @sc{posix} @code{regcomp} and @code{regexec} interfaces
1189usually described in @i{regex}(3)).
74091dd4 1190The return value is 0 if the string matches the pattern, and 1 if it does not.
b80f6443 1191If the regular expression is syntactically incorrect, the conditional
74091dd4 1192expression returns 2.
a0c0a00f 1193If the @code{nocasematch} shell option
3185942a 1194(see the description of @code{shopt} in @ref{The Shopt Builtin})
b80f6443
JA
1195is enabled, the match is performed without regard to the case
1196of alphabetic characters.
74091dd4
CR
1197
1198You can quote any part of the pattern
1199to force the quoted portion to be matched literally
1200instead of as a regular expression (see above).
ac50fbac 1201If the pattern is stored in a shell variable, quoting the variable
74091dd4 1202expansion forces the entire pattern to be matched literally.
8868edaf
CR
1203
1204The pattern will match if it matches any part of the string.
74091dd4
CR
1205If you want to force the pattern to match the entire string,
1206anchor the pattern using the @samp{^} and @samp{$} regular expression
1207operators.
b80f6443 1208
ac50fbac 1209For example, the following will match a line
74091dd4 1210(stored in the shell variable @code{line})
8868edaf 1211if there is a sequence of characters anywhere in the value consisting of
ac50fbac 1212any number, including zero, of
8868edaf 1213characters in the @code{space} character class,
74091dd4
CR
1214immediately followed by zero or one instances of @samp{a},
1215then a @samp{b}:
1216
ac50fbac 1217@example
8868edaf 1218[[ $line =~ [[:space:]]*(a)?b ]]
ac50fbac
CR
1219@end example
1220
1221@noindent
74091dd4
CR
1222That means values for @code{line} like
1223@samp{aab}, @samp{ aaaaaab}, @samp{xaby}, and @samp{ ab}
1224will all match,
1225as will a line containing a @samp{b} anywhere in its value.
1226
1227If you want to match a character that's special to the regular expression
1228grammar (@samp{^$|[]()\.*+?}), it has to be quoted to remove its special
1229meaning.
1230This means that in the pattern @samp{xxx.txt}, the @samp{.} matches any
1231character in the string (its usual regular expression meaning), but in the
1232pattern @samp{"xxx.txt"}, it can only match a literal @samp{.}.
1233
1234Likewise, if you want to include a character in your pattern that has a
1235special meaning to the regular expression grammar, you must make sure it's
1236not quoted.
1237If you want to anchor a pattern at the beginning or end of the string,
1238for instance, you cannot quote the @samp{^} or @samp{$}
1239characters using any form of shell quoting.
1240
1241If you want to match @samp{initial string} at the start of a line,
1242the following will work:
1243@example
1244[[ $line =~ ^"initial string" ]]
1245@end example
1246@noindent
1247but this will not:
1248@example
1249[[ $line =~ "^initial string" ]]
1250@end example
1251@noindent
1252because in the second example the @samp{^} is quoted and doesn't have its
1253usual special meaning.
ac50fbac 1254
74091dd4
CR
1255It is sometimes difficult to specify a regular expression properly
1256without using quotes, or to keep track of the quoting used by regular
1257expressions while paying attention to
1258shell quoting and the shell's quote removal.
ac50fbac
CR
1259Storing the regular expression in a shell variable is often a useful
1260way to avoid problems with quoting characters that are special to the
1261shell.
74091dd4
CR
1262For example, the following is equivalent to the pattern used above:
1263
ac50fbac 1264@example
8868edaf 1265pattern='[[:space:]]*(a)?b'
ac50fbac
CR
1266[[ $line =~ $pattern ]]
1267@end example
1268
74091dd4
CR
1269Shell programmers should take special care with backslashes, since
1270backslashes are used by both the shell and regular expressions to remove
1271the special meaning from the following character.
1272This means that after the shell's word expansions complete
1273(@pxref{Shell Expansions}),
1274any backslashes remaining in parts of the pattern
1275that were originally not quoted can remove the
1276special meaning of pattern characters.
1277If any part of the pattern is quoted, the shell does its best to ensure that
1278the regular expression treats those remaining backslashes as literal,
1279if they appeared in a quoted portion.
1280
ac50fbac 1281The following two sets of commands are @emph{not} equivalent:
74091dd4 1282
ac50fbac
CR
1283@example
1284pattern='\.'
1285
1286[[ . =~ $pattern ]]
1287[[ . =~ \. ]]
1288
1289[[ . =~ "$pattern" ]]
1290[[ . =~ '\.' ]]
1291@end example
1292
1293@noindent
1294The first two matches will succeed, but the second two will not, because
1295in the second two the backslash will be part of the pattern to be matched.
74091dd4
CR
1296In the first two examples, the pattern passed to the regular expression
1297parser is @samp{\.}. The backslash removes the special meaning from
ac50fbac 1298@samp{.}, so the literal @samp{.} matches.
74091dd4
CR
1299In the second two examples, the pattern passed to the regular expression
1300parser has the backslash quoted (e.g., @samp{\\\.}), which will not match
1301the string, since it does not contain a backslash.
ac50fbac
CR
1302If the string in the first examples were anything other than @samp{.}, say
1303@samp{a}, the pattern would not match, because the quoted @samp{.} in the
1304pattern loses its special meaning of matching any single character.
1305
74091dd4
CR
1306Bracket expressions in regular expressions can be sources of errors as well,
1307since characters that are normally special in regular expressions
1308lose their special meanings between brackets.
1309However, you can use bracket expressions to match special pattern characters
1310without quoting them, so they are sometimes useful for this purpose.
1311
1312Though it might seem like a strange way to write it, the following pattern
1313will match a @samp{.} in the string:
1314
1315@example
1316[[ . =~ [.] ]]
1317@end example
1318
1319The shell performs any word expansions before passing the pattern
1320to the regular expression functions,
1321so you can assume that the shell's quoting takes precedence.
1322As noted above, the regular expression parser will interpret any
1323unquoted backslashes remaining in the pattern after shell expansion
1324according to its own rules.
1325The intention is to avoid making shell programmers quote things twice
1326as much as possible, so shell quoting should be sufficient to quote
1327special pattern characters where that's necessary.
1328
1329The array variable @code{BASH_REMATCH} records which parts of the string
1330matched the pattern.
1331The element of @code{BASH_REMATCH} with index 0 contains the portion of
1332the string matching the entire regular expression.
1333Substrings matched by parenthesized subexpressions within the regular
1334expression are saved in the remaining @code{BASH_REMATCH} indices.
1335The element of @code{BASH_REMATCH} with index @var{n} is the portion of the
1336string matching the @var{n}th parenthesized subexpression.
1337
1338Bash sets
1339@code{BASH_REMATCH}
1340in the global scope; declaring it as a local variable will lead to
1341unexpected results.
1342
cce855bc
JA
1343Expressions may be combined using the following operators, listed
1344in decreasing order of precedence:
1345
1346@table @code
1347@item ( @var{expression} )
1348Returns the value of @var{expression}.
1349This may be used to override the normal precedence of operators.
ccc6cda3 1350
cce855bc
JA
1351@item ! @var{expression}
1352True if @var{expression} is false.
1353
1354@item @var{expression1} && @var{expression2}
1355True if both @var{expression1} and @var{expression2} are true.
1356
1357@item @var{expression1} || @var{expression2}
1358True if either @var{expression1} or @var{expression2} is true.
ccc6cda3 1359@end table
ac50fbac 1360
cce855bc 1361@noindent
7117c2d2 1362The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the
cce855bc
JA
1363value of @var{expression1} is sufficient to determine the return
1364value of the entire conditional expression.
cce855bc 1365@end table
ccc6cda3
JA
1366
1367@node Command Grouping
b80f6443 1368@subsubsection Grouping Commands
ccc6cda3
JA
1369@cindex commands, grouping
1370
1371Bash provides two ways to group a list of commands to be executed
1372as a unit. When commands are grouped, redirections may be applied
1373to the entire command list. For example, the output of all the
1374commands in the list may be redirected to a single stream.
1375
1376@table @code
1377@item ()
1378@example
1379( @var{list} )
1380@end example
1381
74091dd4
CR
1382Placing a list of commands between parentheses forces the shell to create
1383a subshell (@pxref{Command Execution Environment}), and each
1384of the commands in @var{list} is executed in that subshell environment.
1385Since the @var{list} is executed in a subshell, variable assignments do not
1386remain in effect after the subshell completes.
ccc6cda3
JA
1387
1388@item @{@}
1389@rwindex @{
1390@rwindex @}
1391@example
1392@{ @var{list}; @}
1393@end example
1394
1395Placing a list of commands between curly braces causes the list to
1396be executed in the current shell context. No subshell is created.
cce855bc 1397The semicolon (or newline) following @var{list} is required.
ccc6cda3
JA
1398@end table
1399
1400In addition to the creation of a subshell, there is a subtle difference
1401between these two constructs due to historical reasons. The braces
74091dd4 1402are reserved words, so they must be separated from the @var{list}
3185942a 1403by @code{blank}s or other shell metacharacters.
74091dd4 1404The parentheses are operators, and are
ccc6cda3 1405recognized as separate tokens by the shell even if they are not separated
d166f048 1406from the @var{list} by whitespace.
ccc6cda3
JA
1407
1408The exit status of both of these constructs is the exit status of
1409@var{list}.
1410
3185942a
JA
1411@node Coprocesses
1412@subsection Coprocesses
1413@cindex coprocess
1414
1415A @code{coprocess} is a shell command preceded by the @code{coproc}
1416reserved word.
1417A coprocess is executed asynchronously in a subshell, as if the command
1418had been terminated with the @samp{&} control operator, with a two-way pipe
1419established between the executing shell and the coprocess.
1420
74091dd4
CR
1421The syntax for a coprocess is:
1422
3185942a 1423@example
ac50fbac 1424coproc [@var{NAME}] @var{command} [@var{redirections}]
3185942a
JA
1425@end example
1426
1427@noindent
1428This creates a coprocess named @var{NAME}.
74091dd4
CR
1429@var{command} may be either a simple command (@pxref{Simple Commands})
1430or a compound command (@pxref{Compound Commands}).
1431@var{NAME} is a shell variable name.
1432If @var{NAME} is not supplied, the default name is @code{COPROC}.
1433
1434The recommended form to use for a coprocess is
1435
1436@example
1437coproc @var{NAME} @{ @var{command}; @}
1438@end example
1439
1440@noindent
1441This form is recommended because simple commands result in the coprocess
1442always being named @code{COPROC}, and it is simpler to use and more complete
1443than the other compound commands.
1444
1445There are other forms of coprocesses:
1446
1447@example
1448coproc @var{NAME} @var{compound-command}
1449coproc @var{compound-command}
1450coproc @var{simple-command}
1451@end example
1452
1453@noindent
1454If @var{command} is a compound command, @var{NAME} is optional. The
1455word following @code{coproc} determines whether that word is interpreted
1456as a variable name: it is interpreted as @var{NAME} if it is not a
1457reserved word that introduces a compound command.
1458If @var{command} is a simple command, @var{NAME} is not allowed; this
1459is to avoid confusion between @var{NAME} and the first word of the simple
1460command.
3185942a 1461
ac50fbac 1462When the coprocess is executed, the shell creates an array variable
3185942a 1463(@pxref{Arrays})
74091dd4 1464named @var{NAME} in the context of the executing shell.
3185942a
JA
1465The standard output of @var{command}
1466is connected via a pipe to a file descriptor in the executing shell,
74091dd4 1467and that file descriptor is assigned to @var{NAME}[0].
3185942a
JA
1468The standard input of @var{command}
1469is connected via a pipe to a file descriptor in the executing shell,
74091dd4 1470and that file descriptor is assigned to @var{NAME}[1].
3185942a
JA
1471This pipe is established before any redirections specified by the
1472command (@pxref{Redirections}).
1473The file descriptors can be utilized as arguments to shell commands
1474and redirections using standard word expansions.
d233b485
CR
1475Other than those created to execute command and process substitutions,
1476the file descriptors are not available in subshells.
3185942a 1477
495aee44 1478The process ID of the shell spawned to execute the coprocess is
74091dd4 1479available as the value of the variable @env{@var{NAME}_PID}.
3185942a
JA
1480The @code{wait}
1481builtin command may be used to wait for the coprocess to terminate.
1482
ac50fbac
CR
1483Since the coprocess is created as an asynchronous command,
1484the @code{coproc} command always returns success.
3185942a
JA
1485The return status of a coprocess is the exit status of @var{command}.
1486
495aee44
CR
1487@node GNU Parallel
1488@subsection GNU Parallel
1489
ac50fbac
CR
1490There are ways to run commands in parallel that are not built into Bash.
1491GNU Parallel is a tool to do just that.
1492
495aee44
CR
1493GNU Parallel, as its name suggests, can be used to build and run commands
1494in parallel. You may run the same command with different arguments, whether
ac50fbac
CR
1495they are filenames, usernames, hostnames, or lines read from files. GNU
1496Parallel provides shorthand references to many of the most common operations
1497(input lines, various portions of the input line, different ways to specify
1498the input source, and so on). Parallel can replace @code{xargs} or feed
1499commands from its input sources to several different instances of Bash.
495aee44 1500
74091dd4
CR
1501For a complete description, refer to the GNU Parallel documentation, which
1502is available at
1503@url{https://www.gnu.org/software/parallel/parallel_tutorial.html}.
ac50fbac 1504
ccc6cda3
JA
1505@node Shell Functions
1506@section Shell Functions
1507@cindex shell function
1508@cindex functions, shell
1509
1510Shell functions are a way to group commands for later execution
1511using a single name for the group. They are executed just like
bb70624e
JA
1512a "regular" command.
1513When the name of a shell function is used as a simple command name,
1514the list of commands associated with that function name is executed.
1515Shell functions are executed in the current
ccc6cda3
JA
1516shell context; no new process is created to interpret them.
1517
1518Functions are declared using this syntax:
1519@rwindex function
1520@example
8868edaf 1521@var{fname} () @var{compound-command} [ @var{redirections} ]
ac50fbac
CR
1522@end example
1523
1524or
1525
1526@example
8868edaf 1527function @var{fname} [()] @var{compound-command} [ @var{redirections} ]
ccc6cda3
JA
1528@end example
1529
8868edaf 1530This defines a shell function named @var{fname}. The reserved
cce855bc
JA
1531word @code{function} is optional.
1532If the @code{function} reserved
1533word is supplied, the parentheses are optional.
74091dd4 1534The @dfn{body} of the function is the compound command
b80f6443
JA
1535@var{compound-command} (@pxref{Compound Commands}).
1536That command is usually a @var{list} enclosed between @{ and @}, but
74091dd4
CR
1537may be any compound command listed above.
1538If the @code{function} reserved word is used, but the
1539parentheses are not supplied, the braces are recommended.
8868edaf 1540@var{compound-command} is executed whenever @var{fname} is specified as the
74091dd4 1541name of a simple command.
ac50fbac 1542When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}),
74091dd4 1543@var{fname} must be a valid shell name and
8868edaf 1544may not be the same as one of the special builtins
ac50fbac 1545(@pxref{Special Builtins}).
8868edaf
CR
1546In default mode, a function name can be any unquoted shell word that does
1547not contain @samp{$}.
b80f6443
JA
1548Any redirections (@pxref{Redirections}) associated with the shell function
1549are performed when the function is executed.
0628567a
JA
1550A function definition may be deleted using the @option{-f} option to the
1551@code{unset} builtin (@pxref{Bourne Shell Builtins}).
1552
b80f6443
JA
1553The exit status of a function definition is zero unless a syntax error
1554occurs or a readonly function with the same name already exists.
1555When executed, the exit status of a function is the exit status of the
1556last command executed in the body.
1557
1558Note that for historical reasons, in the most common usage the curly braces
1559that surround the body of the function must be separated from the body by
bb70624e
JA
1560@code{blank}s or newlines.
1561This is because the braces are reserved words and are only recognized
3185942a
JA
1562as such when they are separated from the command list
1563by whitespace or another shell metacharacter.
b80f6443
JA
1564Also, when using the braces, the @var{list} must be terminated by a semicolon,
1565a @samp{&}, or a newline.
bb70624e 1566
ccc6cda3
JA
1567When a function is executed, the arguments to the
1568function become the positional parameters
1569during its execution (@pxref{Positional Parameters}).
d166f048
JA
1570The special parameter @samp{#} that expands to the number of
1571positional parameters is updated to reflect the change.
b80f6443
JA
1572Special parameter @code{0} is unchanged.
1573The first element of the @env{FUNCNAME} variable is set to the
1574name of the function while the function is executing.
0001803f 1575
b80f6443
JA
1576All other aspects of the shell execution
1577environment are identical between a function and its caller
0001803f
CR
1578with these exceptions:
1579the @env{DEBUG} and @env{RETURN} traps
95732b49 1580are not inherited unless the function has been given the
b80f6443
JA
1581@code{trace} attribute using the @code{declare} builtin or
1582the @code{-o functrace} option has been enabled with
1583the @code{set} builtin,
0001803f
CR
1584(in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps),
1585and the @env{ERR} trap is not inherited unless the @code{-o errtrace}
1586shell option has been enabled.
b80f6443
JA
1587@xref{Bourne Shell Builtins}, for the description of the
1588@code{trap} builtin.
ccc6cda3 1589
495aee44
CR
1590The @env{FUNCNEST} variable, if set to a numeric value greater
1591than 0, defines a maximum function nesting level. Function
1592invocations that exceed the limit cause the entire command to
1593abort.
1594
ccc6cda3
JA
1595If the builtin command @code{return}
1596is executed in a function, the function completes and
1597execution resumes with the next command after the function
b80f6443
JA
1598call.
1599Any command associated with the @code{RETURN} trap is executed
1600before execution resumes.
1601When a function completes, the values of the
ccc6cda3 1602positional parameters and the special parameter @samp{#}
cce855bc 1603are restored to the values they had prior to the function's
ccc6cda3 1604execution. If a numeric argument is given to @code{return},
f73dda09 1605that is the function's return status; otherwise the function's
cce855bc
JA
1606return status is the exit status of the last command executed
1607before the @code{return}.
ccc6cda3
JA
1608
1609Variables local to the function may be declared with the
74091dd4
CR
1610@code{local} builtin (@dfn{local variables}).
1611Ordinarily, variables and their values
1612are shared between a function and its caller.
1613These variables are visible only to
d233b485
CR
1614the function and the commands it invokes. This is particularly
1615important when a shell function calls other functions.
1616
74091dd4
CR
1617In the following description, the @dfn{current scope} is a currently-
1618executing function.
1619Previous scopes consist of that function's caller and so on,
1620back to the "global" scope, where the shell is not executing
1621any shell function.
1622Consequently, a local variable at the current local scope is a variable
1623declared using the @code{local} or @code{declare} builtins in the
1624function that is currently executing.
1625
d233b485
CR
1626Local variables "shadow" variables with the same name declared at
1627previous scopes. For instance, a local variable declared in a function
1628hides a global variable of the same name: references and assignments
1629refer to the local variable, leaving the global variable unmodified.
1630When the function returns, the global variable is once again visible.
1631
74091dd4 1632The shell uses @dfn{dynamic scoping} to control a variable's visibility
d233b485
CR
1633within functions.
1634With dynamic scoping, visible variables and their values
1635are a result of the sequence of function calls that caused execution
1636to reach the current function.
1637The value of a variable that a function sees depends
1638on its value within its caller, if any, whether that caller is
1639the "global" scope or another shell function.
1640This is also the value that a local variable
1641declaration "shadows", and the value that is restored when the function
1642returns.
1643
74091dd4
CR
1644For example, if a variable @env{var} is declared as local in function
1645@code{func1}, and @code{func1} calls another function @code{func2},
1646references to @env{var} made from within @code{func2} will resolve to the
1647local variable @env{var} from @code{func1}, shadowing any global variable
1648named @env{var}.
d233b485
CR
1649
1650The following script demonstrates this behavior.
1651When executed, the script displays
1652
1653@example
1654In func2, var = func1 local
1655@end example
1656
1657@example
1658func1()
1659@{
1660 local var='func1 local'
1661 func2
1662@}
1663
1664func2()
1665@{
1666 echo "In func2, var = $var"
1667@}
1668
1669var=global
1670func1
1671@end example
1672
1673The @code{unset} builtin also acts using the same dynamic scope: if a
1674variable is local to the current scope, @code{unset} will unset it;
1675otherwise the unset will refer to the variable found in any calling scope
1676as described above.
1677If a variable at the current local scope is unset, it will remain so
74091dd4 1678(appearing as unset)
d233b485
CR
1679until it is reset in that scope or until the function returns.
1680Once the function returns, any instance of the variable at a previous
1681scope will become visible.
1682If the unset acts on a variable at a previous scope, any instance of a
74091dd4
CR
1683variable with that name that had been shadowed will become visible
1684(see below how @code{localvar_unset}shell option changes this behavior).
ccc6cda3 1685
b80f6443 1686Function names and definitions may be listed with the
ac50fbac
CR
1687@option{-f} option to the @code{declare} (@code{typeset})
1688builtin command (@pxref{Bash Builtins}).
b80f6443
JA
1689The @option{-F} option to @code{declare} or @code{typeset}
1690will list the function names only
1691(and optionally the source file and line number, if the @code{extdebug}
1692shell option is enabled).
74091dd4
CR
1693Functions may be exported so that child shell processes
1694(those created when executing a separate shell invocation)
b80f6443
JA
1695automatically have them defined with the
1696@option{-f} option to the @code{export} builtin
1697(@pxref{Bourne Shell Builtins}).
b80f6443 1698
495aee44
CR
1699Functions may be recursive.
1700The @code{FUNCNEST} variable may be used to limit the depth of the
1701function call stack and restrict the number of function invocations.
1702By default, no limit is placed on the number of recursive calls.
ccc6cda3
JA
1703
1704@node Shell Parameters
1705@section Shell Parameters
1706@cindex parameters
1707@cindex variable, shell
1708@cindex shell variable
1709
1710@menu
1711* Positional Parameters:: The shell's command-line arguments.
b80f6443 1712* Special Parameters:: Parameters denoted by special characters.
ccc6cda3
JA
1713@end menu
1714
74091dd4 1715A @dfn{parameter} is an entity that stores values.
ccc6cda3
JA
1716It can be a @code{name}, a number, or one of the special characters
1717listed below.
74091dd4
CR
1718A @dfn{variable} is a parameter denoted by a @code{name}.
1719A variable has a @code{value} and zero or more @code{attributes}.
f73dda09
JA
1720Attributes are assigned using the @code{declare} builtin command
1721(see the description of the @code{declare} builtin in @ref{Bash Builtins}).
ccc6cda3
JA
1722
1723A parameter is set if it has been assigned a value. The null string is
1724a valid value. Once a variable is set, it may be unset only by using
1725the @code{unset} builtin command.
1726
1727A variable may be assigned to by a statement of the form
1728@example
1729@var{name}=[@var{value}]
1730@end example
1731@noindent
1732If @var{value}
1733is not given, the variable is assigned the null string. All
1734@var{value}s undergo tilde expansion, parameter and variable expansion,
1735command substitution, arithmetic expansion, and quote
74091dd4
CR
1736removal (@pxref{Shell Parameter Expansion}).
1737If the variable has its @code{integer}
f73dda09 1738attribute set, then @var{value}
b80f6443 1739is evaluated as an arithmetic expression even if the @code{$((@dots{}))}
cce855bc 1740expansion is not used (@pxref{Arithmetic Expansion}).
74091dd4 1741Word splitting and filename expansion are not performed.
f73dda09 1742Assignment statements may also appear as arguments to the
b80f6443 1743@code{alias},
f73dda09 1744@code{declare}, @code{typeset}, @code{export}, @code{readonly},
74091dd4 1745and @code{local} builtin commands (@dfn{declaration} commands).
ac50fbac
CR
1746When in @sc{posix} mode (@pxref{Bash POSIX Mode}), these builtins may appear
1747in a command after one or more instances of the @code{command} builtin
1748and retain these assignment statement properties.
ccc6cda3 1749
95732b49
JA
1750In the context where an assignment statement is assigning a value
1751to a shell variable or array index (@pxref{Arrays}), the @samp{+=}
1752operator can be used to
1753append to or add to the variable's previous value.
a0c0a00f 1754This includes arguments to builtin commands such as @code{declare} that
74091dd4
CR
1755accept assignment statements (declaration commands).
1756When @samp{+=} is applied to a variable for which the @code{integer} attribute
95732b49
JA
1757has been set, @var{value} is evaluated as an arithmetic expression and
1758added to the variable's current value, which is also evaluated.
1759When @samp{+=} is applied to an array variable using compound assignment
1760(@pxref{Arrays}), the
1761variable's value is not unset (as it is when using @samp{=}), and new
1762values are appended to the array beginning at one greater than the array's
3185942a
JA
1763maximum index (for indexed arrays), or added as additional key-value pairs
1764in an associative array.
95732b49
JA
1765When applied to a string-valued variable, @var{value} is expanded and
1766appended to the variable's value.
1767
74091dd4 1768A variable can be assigned the @code{nameref} attribute using the
a0c0a00f 1769@option{-n} option to the @code{declare} or @code{local} builtin commands
ac50fbac 1770(@pxref{Bash Builtins})
74091dd4 1771to create a @dfn{nameref}, or a reference to another variable.
ac50fbac 1772This allows variables to be manipulated indirectly.
a0c0a00f
CR
1773Whenever the nameref variable is referenced, assigned to, unset, or has
1774its attributes modified (other than using or changing the nameref
1775attribute itself), the
1776operation is actually performed on the variable specified by the nameref
1777variable's value.
ac50fbac
CR
1778A nameref is commonly used within shell functions to refer to a variable
1779whose name is passed as an argument to the function.
1780For instance, if a variable name is passed to a shell function as its first
1781argument, running
1782@example
1783declare -n ref=$1
1784@end example
1785@noindent
74091dd4 1786inside the function creates a nameref variable @env{ref} whose value is
ac50fbac 1787the variable name passed as the first argument.
74091dd4 1788References and assignments to @env{ref}, and changes to its attributes,
a0c0a00f
CR
1789are treated as references, assignments, and attribute modifications
1790to the variable whose name was passed as @code{$1}.
ac50fbac
CR
1791
1792If the control variable in a @code{for} loop has the nameref attribute,
1793the list of words can be a list of shell variables, and a name reference
1794will be established for each word in the list, in turn, when the loop is
1795executed.
a0c0a00f 1796Array variables cannot be given the nameref attribute.
ac50fbac
CR
1797However, nameref variables can reference array variables and subscripted
1798array variables.
1799Namerefs can be unset using the @option{-n} option to the @code{unset} builtin
1800(@pxref{Bourne Shell Builtins}).
1801Otherwise, if @code{unset} is executed with the name of a nameref variable
1802as an argument, the variable referenced by the nameref variable will be unset.
1803
ccc6cda3
JA
1804@node Positional Parameters
1805@subsection Positional Parameters
1806@cindex parameters, positional
1807
74091dd4 1808A @dfn{positional parameter} is a parameter denoted by one or more
ccc6cda3
JA
1809digits, other than the single digit @code{0}. Positional parameters are
1810assigned from the shell's arguments when it is invoked,
cce855bc 1811and may be reassigned using the @code{set} builtin command.
bb70624e
JA
1812Positional parameter @code{N} may be referenced as @code{$@{N@}}, or
1813as @code{$N} when @code{N} consists of a single digit.
1814Positional parameters may not be assigned to with assignment statements.
1815The @code{set} and @code{shift} builtins are used to set and
1816unset them (@pxref{Shell Builtin Commands}).
1817The positional parameters are
ccc6cda3
JA
1818temporarily replaced when a shell function is executed
1819(@pxref{Shell Functions}).
1820
1821When a positional parameter consisting of more than a single
1822digit is expanded, it must be enclosed in braces.
1823
1824@node Special Parameters
1825@subsection Special Parameters
1826@cindex parameters, special
1827
1828The shell treats several parameters specially. These parameters may
1829only be referenced; assignment to them is not allowed.
1830
1831@vtable @code
1832
1833@item *
ac50fbac
CR
1834@vindex $*
1835($*) Expands to the positional parameters, starting from one.
1836When the expansion is not within double quotes, each positional parameter
1837expands to a separate word.
1838In contexts where it is performed, those words
8868edaf 1839are subject to further word splitting and filename expansion.
ac50fbac 1840When the expansion occurs within double quotes, it expands to a single word
a0c0a00f
CR
1841with the value of each parameter separated by the first character of the
1842@env{IFS} special variable. That is, @code{"$*"} is equivalent
ccc6cda3
JA
1843to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c}
1844is the first character of the value of the @code{IFS}
d166f048 1845variable.
28ef6c31
JA
1846If @env{IFS} is unset, the parameters are separated by spaces.
1847If @env{IFS} is null, the parameters are joined without intervening
d166f048
JA
1848separators.
1849
ccc6cda3 1850@item @@
ac50fbac 1851@vindex $@@
d233b485
CR
1852($@@) Expands to the positional parameters, starting from one.
1853In contexts where word splitting is performed, this expands each
1854positional parameter to a separate word; if not within double
1855quotes, these words are subject to word splitting.
1856In contexts where word splitting is not performed,
1857this expands to a single word
1858with each positional parameter separated by a space.
1859When the
1860expansion occurs within double quotes, and word splitting is performed,
1861each parameter expands to a
ccc6cda3
JA
1862separate word. That is, @code{"$@@"} is equivalent to
1863@code{"$1" "$2" @dots{}}.
95732b49
JA
1864If the double-quoted expansion occurs within a word, the expansion of
1865the first parameter is joined with the beginning part of the original
1866word, and the expansion of the last parameter is joined with the last
1867part of the original word.
ccc6cda3
JA
1868When there are no positional parameters, @code{"$@@"} and
1869@code{$@@}
1870expand to nothing (i.e., they are removed).
1871
1872@item #
ac50fbac
CR
1873@vindex $#
1874($#) Expands to the number of positional parameters in decimal.
ccc6cda3
JA
1875
1876@item ?
ac50fbac
CR
1877@vindex $?
1878($?) Expands to the exit status of the most recently executed foreground
ccc6cda3
JA
1879pipeline.
1880
1881@item -
ac50fbac
CR
1882@vindex $-
1883($-, a hyphen.) Expands to the current option flags as specified upon
bb70624e 1884invocation, by the @code{set}
ccc6cda3 1885builtin command, or those set by the shell itself
28ef6c31 1886(such as the @option{-i} option).
ccc6cda3
JA
1887
1888@item $
ac50fbac 1889@vindex $$
74091dd4 1890($$) Expands to the process @sc{id} of the shell. In a subshell, it
bb70624e 1891expands to the process @sc{id} of the invoking shell, not the subshell.
ccc6cda3
JA
1892
1893@item !
ac50fbac
CR
1894@vindex $!
1895($!) Expands to the process @sc{id} of the job most recently placed into the
1896background, whether executed as an asynchronous command or using
1897the @code{bg} builtin (@pxref{Job Control Builtins}).
ccc6cda3
JA
1898
1899@item 0
ac50fbac
CR
1900@vindex $0
1901($0) Expands to the name of the shell or shell script. This is set at
cce855bc
JA
1902shell initialization. If Bash is invoked with a file of commands
1903(@pxref{Shell Scripts}), @code{$0} is set to the name of that file.
28ef6c31 1904If Bash is started with the @option{-c} option (@pxref{Invoking Bash}),
cce855bc 1905then @code{$0} is set to the first argument after the string to be
ccc6cda3
JA
1906executed, if one is present. Otherwise, it is set
1907to the filename used to invoke Bash, as given by argument zero.
ccc6cda3
JA
1908@end vtable
1909
1910@node Shell Expansions
1911@section Shell Expansions
1912@cindex expansion
1913
1914Expansion is performed on the command line after it has been split into
1915@code{token}s. There are seven kinds of expansion performed:
ac50fbac 1916
ccc6cda3
JA
1917@itemize @bullet
1918@item brace expansion
1919@item tilde expansion
1920@item parameter and variable expansion
1921@item command substitution
1922@item arithmetic expansion
1923@item word splitting
1924@item filename expansion
1925@end itemize
1926
1927@menu
cce855bc
JA
1928* Brace Expansion:: Expansion of expressions within braces.
1929* Tilde Expansion:: Expansion of the ~ character.
ccc6cda3
JA
1930* Shell Parameter Expansion:: How Bash expands variables to their values.
1931* Command Substitution:: Using the output of a command as an argument.
cce855bc 1932* Arithmetic Expansion:: How to use arithmetic in shell expansions.
ccc6cda3
JA
1933* Process Substitution:: A way to write and read to and from a
1934 command.
1935* Word Splitting:: How the results of expansion are split into separate
1936 arguments.
1937* Filename Expansion:: A shorthand for specifying filenames matching patterns.
1938* Quote Removal:: How and when quote characters are removed from
1939 words.
1940@end menu
1941
ac50fbac
CR
1942The order of expansions is:
1943brace expansion;
1944tilde expansion, parameter and variable expansion, arithmetic expansion,
1945and command substitution (done in a left-to-right fashion);
1946word splitting;
1947and filename expansion.
ccc6cda3
JA
1948
1949On systems that can support it, there is an additional expansion
74091dd4 1950available: @dfn{process substitution}.
ac50fbac
CR
1951This is performed at the
1952same time as tilde, parameter, variable, and arithmetic expansion and
ccc6cda3
JA
1953command substitution.
1954
a0c0a00f
CR
1955After these expansions are performed, quote characters present in the
1956original word are removed unless they have been quoted themselves
74091dd4 1957(@dfn{quote removal}).
a0c0a00f 1958
ccc6cda3 1959Only brace expansion, word splitting, and filename expansion
d233b485 1960can increase the number of words of the expansion; other expansions
ccc6cda3
JA
1961expand a single word to a single word.
1962The only exceptions to this are the expansions of
d233b485
CR
1963@code{"$@@"} and @code{$*} (@pxref{Special Parameters}), and
1964@code{"$@{@var{name}[@@]@}"} and @code{$@{@var{name}[*]@}}
ccc6cda3
JA
1965(@pxref{Arrays}).
1966
1967After all expansions, @code{quote removal} (@pxref{Quote Removal})
1968is performed.
1969
cce855bc
JA
1970@node Brace Expansion
1971@subsection Brace Expansion
1972@cindex brace expansion
1973@cindex expansion, brace
1974
bb70624e
JA
1975Brace expansion is a mechanism by which arbitrary strings may be generated.
1976This mechanism is similar to
74091dd4 1977@dfn{filename expansion} (@pxref{Filename Expansion}),
ac50fbac 1978but the filenames generated need not exist.
bb70624e 1979Patterns to be brace expanded take the form of an optional @var{preamble},
ac50fbac 1980followed by either a series of comma-separated strings or a sequence expression
b80f6443 1981between a pair of braces,
bb70624e
JA
1982followed by an optional @var{postscript}.
1983The preamble is prefixed to each string contained within the braces, and
1984the postscript is then appended to each resulting string, expanding left
1985to right.
1986
1987Brace expansions may be nested.
1988The results of each expanded string are not sorted; left to right order
1989is preserved.
cce855bc
JA
1990For example,
1991@example
1992bash$ echo a@{d,c,b@}e
1993ade ace abe
1994@end example
1995
0001803f 1996A sequence expression takes the form @code{@{@var{x}..@var{y}[..@var{incr}]@}},
74091dd4 1997where @var{x} and @var{y} are either integers or letters,
3185942a 1998and @var{incr}, an optional increment, is an integer.
b80f6443
JA
1999When integers are supplied, the expression expands to each number between
2000@var{x} and @var{y}, inclusive.
3185942a 2001Supplied integers may be prefixed with @samp{0} to force each term to have the
ac50fbac
CR
2002same width.
2003When either @var{x} or @var{y} begins with a zero, the shell
3185942a
JA
2004attempts to force all generated terms to contain the same number of digits,
2005zero-padding where necessary.
74091dd4 2006When letters are supplied, the expression expands to each character
ac50fbac
CR
2007lexicographically between @var{x} and @var{y}, inclusive,
2008using the default C locale.
74091dd4
CR
2009Note that both @var{x} and @var{y} must be of the same type
2010(integer or letter).
3185942a
JA
2011When the increment is supplied, it is used as the difference between
2012each term. The default increment is 1 or -1 as appropriate.
b80f6443 2013
cce855bc
JA
2014Brace expansion is performed before any other expansions,
2015and any characters special to other expansions are preserved
2016in the result. It is strictly textual. Bash
2017does not apply any syntactic interpretation to the context of the
2018expansion or the text between the braces.
2019
2020A correctly-formed brace expansion must contain unquoted opening
b80f6443
JA
2021and closing braces, and at least one unquoted comma or a valid
2022sequence expression.
cce855bc
JA
2023Any incorrectly formed brace expansion is left unchanged.
2024
b80f6443
JA
2025A @{ or @samp{,} may be quoted with a backslash to prevent its
2026being considered part of a brace expression.
2027To avoid conflicts with parameter expansion, the string @samp{$@{}
d233b485 2028is not considered eligible for brace expansion,
8868edaf 2029and inhibits brace expansion until the closing @samp{@}}.
b80f6443 2030
cce855bc
JA
2031This construct is typically used as shorthand when the common
2032prefix of the strings to be generated is longer than in the
2033above example:
2034@example
2035mkdir /usr/local/src/bash/@{old,new,dist,bugs@}
2036@end example
2037or
2038@example
2039chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@}
2040@end example
2041
2042@node Tilde Expansion
2043@subsection Tilde Expansion
2044@cindex tilde expansion
2045@cindex expansion, tilde
2046
2047If a word begins with an unquoted tilde character (@samp{~}), all of the
2048characters up to the first unquoted slash (or all characters,
74091dd4 2049if there is no unquoted slash) are considered a @dfn{tilde-prefix}.
cce855bc
JA
2050If none of the characters in the tilde-prefix are quoted, the
2051characters in the tilde-prefix following the tilde are treated as a
74091dd4 2052possible @dfn{login name}.
cce855bc 2053If this login name is the null string, the tilde is replaced with the
28ef6c31
JA
2054value of the @env{HOME} shell variable.
2055If @env{HOME} is unset, the home directory of the user executing the
cce855bc
JA
2056shell is substituted instead.
2057Otherwise, the tilde-prefix is replaced with the home directory
2058associated with the specified login name.
2059
2060If the tilde-prefix is @samp{~+}, the value of
28ef6c31 2061the shell variable @env{PWD} replaces the tilde-prefix.
cce855bc 2062If the tilde-prefix is @samp{~-}, the value of the shell variable
28ef6c31 2063@env{OLDPWD}, if it is set, is substituted.
cce855bc
JA
2064
2065If the characters following the tilde in the tilde-prefix consist of a
2066number @var{N}, optionally prefixed by a @samp{+} or a @samp{-},
2067the tilde-prefix is replaced with the
2068corresponding element from the directory stack, as it would be displayed
2069by the @code{dirs} builtin invoked with the characters following tilde
2070in the tilde-prefix as an argument (@pxref{The Directory Stack}).
2071If the tilde-prefix, sans the tilde, consists of a number without a
2072leading @samp{+} or @samp{-}, @samp{+} is assumed.
2073
2074If the login name is invalid, or the tilde expansion fails, the word is
2075left unchanged.
2076
2077Each variable assignment is checked for unquoted tilde-prefixes immediately
95732b49 2078following a @samp{:} or the first @samp{=}.
cce855bc 2079In these cases, tilde expansion is also performed.
ac50fbac 2080Consequently, one may use filenames with tildes in assignments to
28ef6c31 2081@env{PATH}, @env{MAILPATH}, and @env{CDPATH},
cce855bc
JA
2082and the shell assigns the expanded value.
2083
2084The following table shows how Bash treats unquoted tilde-prefixes:
2085
2086@table @code
2087@item ~
2088The value of @code{$HOME}
2089@item ~/foo
2090@file{$HOME/foo}
2091
2092@item ~fred/foo
2093The subdirectory @code{foo} of the home directory of the user
2094@code{fred}
2095
2096@item ~+/foo
2097@file{$PWD/foo}
2098
2099@item ~-/foo
2100@file{$@{OLDPWD-'~-'@}/foo}
2101
2102@item ~@var{N}
2103The string that would be displayed by @samp{dirs +@var{N}}
2104
2105@item ~+@var{N}
2106The string that would be displayed by @samp{dirs +@var{N}}
2107
2108@item ~-@var{N}
2109The string that would be displayed by @samp{dirs -@var{N}}
cce855bc
JA
2110@end table
2111
d233b485
CR
2112Bash also performs tilde expansion on words satisfying the conditions of
2113variable assignments (@pxref{Shell Parameters})
2114when they appear as arguments to simple commands.
74091dd4 2115Bash does not do this, except for the declaration commands listed
d233b485
CR
2116above, when in @sc{posix} mode.
2117
ccc6cda3
JA
2118@node Shell Parameter Expansion
2119@subsection Shell Parameter Expansion
2120@cindex parameter expansion
2121@cindex expansion, parameter
2122
2123The @samp{$} character introduces parameter expansion,
2124command substitution, or arithmetic expansion. The parameter name
2125or symbol to be expanded may be enclosed in braces, which
2126are optional but serve to protect the variable to be expanded from
2127characters immediately following it which could be
2128interpreted as part of the name.
2129
cce855bc
JA
2130When braces are used, the matching ending brace is the first @samp{@}}
2131not escaped by a backslash or within a quoted string, and not within an
2132embedded arithmetic expansion, command substitution, or parameter
2133expansion.
2134
ccc6cda3 2135The basic form of parameter expansion is $@{@var{parameter}@}.
ac50fbac
CR
2136The value of @var{parameter} is substituted.
2137The @var{parameter} is a shell parameter as described above
2138(@pxref{Shell Parameters}) or an array reference (@pxref{Arrays}).
2139The braces are required when @var{parameter}
ccc6cda3 2140is a positional parameter with more than one digit,
ac50fbac 2141or when @var{parameter} is followed by a character that is not to be
ccc6cda3
JA
2142interpreted as part of its name.
2143
0001803f 2144If the first character of @var{parameter} is an exclamation point (!),
74091dd4 2145and @var{parameter} is not a nameref,
d233b485
CR
2146it introduces a level of indirection.
2147Bash uses the value formed by expanding the rest of
2148@var{parameter} as the new @var{parameter}; this is then
2149expanded and that value is used in the rest of the expansion, rather
2150than the expansion of the original @var{parameter}.
ccc6cda3 2151This is known as @code{indirect expansion}.
d233b485
CR
2152The value is subject to tilde expansion,
2153parameter expansion, command substitution, and arithmetic expansion.
a0c0a00f
CR
2154If @var{parameter} is a nameref, this expands to the name of the
2155variable referenced by @var{parameter} instead of performing the
2156complete indirect expansion.
ac50fbac 2157The exceptions to this are the expansions of $@{!@var{prefix}*@}
b80f6443 2158and $@{!@var{name}[@@]@}
bb70624e 2159described below.
b80f6443
JA
2160The exclamation point must immediately follow the left brace in order to
2161introduce indirection.
ccc6cda3
JA
2162
2163In each of the cases below, @var{word} is subject to tilde expansion,
2164parameter expansion, command substitution, and arithmetic expansion.
bb70624e 2165
17345e5a 2166When not performing substring expansion, using the form described
ac50fbac 2167below (e.g., @samp{:-}), Bash tests for a parameter that is unset or null.
17345e5a
JA
2168Omitting the colon results in a test only for a parameter that is unset.
2169Put another way, if the colon is included,
2170the operator tests for both @var{parameter}'s existence and that its value
2171is not null; if the colon is omitted, the operator tests only for existence.
ccc6cda3
JA
2172
2173@table @code
2174
2175@item $@{@var{parameter}:@minus{}@var{word}@}
2176If @var{parameter} is unset or null, the expansion of
2177@var{word} is substituted. Otherwise, the value of
2178@var{parameter} is substituted.
2179
74091dd4
CR
2180@example
2181$ v=123
2182$ echo $@{v-unset@}
2183123
2184@end example
2185
ccc6cda3
JA
2186@item $@{@var{parameter}:=@var{word}@}
2187If @var{parameter}
2188is unset or null, the expansion of @var{word}
2189is assigned to @var{parameter}.
b80f6443
JA
2190The value of @var{parameter} is then substituted.
2191Positional parameters and special parameters may not be assigned to
2192in this way.
ccc6cda3 2193
74091dd4
CR
2194@example
2195$ var=
2196$ : $@{var:=DEFAULT@}
2197$ echo $var
2198DEFAULT
2199@end example
2200
ccc6cda3
JA
2201@item $@{@var{parameter}:?@var{word}@}
2202If @var{parameter}
2203is null or unset, the expansion of @var{word} (or a message
2204to that effect if @var{word}
2205is not present) is written to the standard error and the shell, if it
2206is not interactive, exits. Otherwise, the value of @var{parameter} is
2207substituted.
2208
74091dd4
CR
2209@example
2210$ var=
2211$ : $@{var:?var is unset or null@}
2212bash: var: var is unset or null
2213@end example
2214
ccc6cda3
JA
2215@item $@{@var{parameter}:+@var{word}@}
2216If @var{parameter}
2217is null or unset, nothing is substituted, otherwise the expansion of
2218@var{word} is substituted.
2219
74091dd4
CR
2220@example
2221$ var=123
2222$ echo $@{var:+var is set and not null@}
2223var is set and not null
2224@end example
2225
ccc6cda3
JA
2226@item $@{@var{parameter}:@var{offset}@}
2227@itemx $@{@var{parameter}:@var{offset}:@var{length}@}
ac50fbac
CR
2228This is referred to as Substring Expansion.
2229It expands to up to @var{length} characters of the value of @var{parameter}
cce855bc 2230starting at the character specified by @var{offset}.
74091dd4 2231If @var{parameter} is @samp{@@} or @samp{*}, an indexed array subscripted by
ac50fbac
CR
2232@samp{@@} or @samp{*}, or an associative array name, the results differ as
2233described below.
2234If @var{length} is omitted, it expands to the substring of the value of
2235@var{parameter} starting at the character specified by @var{offset}
2236and extending to the end of the value.
ccc6cda3 2237@var{length} and @var{offset} are arithmetic expressions
cce855bc 2238(@pxref{Shell Arithmetic}).
ccc6cda3 2239
ccc6cda3 2240If @var{offset} evaluates to a number less than zero, the value
ac50fbac
CR
2241is used as an offset in characters
2242from the end of the value of @var{parameter}.
2243If @var{length} evaluates to a number less than zero,
2244it is interpreted as an offset in characters
2245from the end of the value of @var{parameter} rather than
2246a number of characters, and the expansion is the characters between
2247@var{offset} and that result.
2248Note that a negative offset must be separated from the colon by at least
2249one space to avoid being confused with the @samp{:-} expansion.
2250
2251Here are some examples illustrating substring expansion on parameters and
2252subscripted arrays:
2253
2254@verbatim
2255$ string=01234567890abcdefgh
2256$ echo ${string:7}
22577890abcdefgh
2258$ echo ${string:7:0}
2259
2260$ echo ${string:7:2}
226178
2262$ echo ${string:7:-2}
22637890abcdef
2264$ echo ${string: -7}
2265bcdefgh
2266$ echo ${string: -7:0}
2267
2268$ echo ${string: -7:2}
2269bc
2270$ echo ${string: -7:-2}
2271bcdef
2272$ set -- 01234567890abcdefgh
2273$ echo ${1:7}
22747890abcdefgh
2275$ echo ${1:7:0}
2276
2277$ echo ${1:7:2}
227878
2279$ echo ${1:7:-2}
22807890abcdef
2281$ echo ${1: -7}
2282bcdefgh
2283$ echo ${1: -7:0}
2284
2285$ echo ${1: -7:2}
2286bc
2287$ echo ${1: -7:-2}
2288bcdef
2289$ array[0]=01234567890abcdefgh
2290$ echo ${array[0]:7}
22917890abcdefgh
2292$ echo ${array[0]:7:0}
2293
2294$ echo ${array[0]:7:2}
229578
2296$ echo ${array[0]:7:-2}
22977890abcdef
2298$ echo ${array[0]: -7}
2299bcdefgh
2300$ echo ${array[0]: -7:0}
2301
2302$ echo ${array[0]: -7:2}
2303bc
2304$ echo ${array[0]: -7:-2}
2305bcdef
2306@end verbatim
2307
74091dd4
CR
2308If @var{parameter} is @samp{@@} or @samp{*}, the result is @var{length}
2309positional parameters beginning at @var{offset}.
ac50fbac
CR
2310A negative @var{offset} is taken relative to one greater than the greatest
2311positional parameter, so an offset of -1 evaluates to the last positional
2312parameter.
2313It is an expansion error if @var{length} evaluates to a number less than zero.
2314
2315The following examples illustrate substring expansion using positional
2316parameters:
2317
2318@verbatim
2319$ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2320$ echo ${@:7}
23217 8 9 0 a b c d e f g h
2322$ echo ${@:7:0}
2323
2324$ echo ${@:7:2}
23257 8
2326$ echo ${@:7:-2}
2327bash: -2: substring expression < 0
2328$ echo ${@: -7:2}
2329b c
2330$ echo ${@:0}
2331./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2332$ echo ${@:0:2}
2333./bash 1
2334$ echo ${@: -7:0}
2335
2336@end verbatim
2337
3185942a
JA
2338If @var{parameter} is an indexed array name subscripted
2339by @samp{@@} or @samp{*}, the result is the @var{length}
cce855bc 2340members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}.
95732b49
JA
2341A negative @var{offset} is taken relative to one greater than the maximum
2342index of the specified array.
ac50fbac
CR
2343It is an expansion error if @var{length} evaluates to a number less than zero.
2344
2345These examples show how you can use substring expansion with indexed
2346arrays:
2347
2348@verbatim
2349$ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h)
2350$ echo ${array[@]:7}
23517 8 9 0 a b c d e f g h
2352$ echo ${array[@]:7:2}
23537 8
2354$ echo ${array[@]: -7:2}
2355b c
2356$ echo ${array[@]: -7:-2}
2357bash: -2: substring expression < 0
2358$ echo ${array[@]:0}
23590 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
2360$ echo ${array[@]:0:2}
23610 1
2362$ echo ${array[@]: -7:0}
2363
2364@end verbatim
2365
3185942a
JA
2366Substring expansion applied to an associative array produces undefined
2367results.
2368
cce855bc 2369Substring indexing is zero-based unless the positional parameters
3185942a 2370are used, in which case the indexing starts at 1 by default.
8868edaf 2371If @var{offset} is 0, and the positional parameters are used, @code{$0} is
3185942a 2372prefixed to the list.
ccc6cda3 2373
bb70624e 2374@item $@{!@var{prefix}*@}
b80f6443 2375@itemx $@{!@var{prefix}@@@}
bb70624e 2376Expands to the names of variables whose names begin with @var{prefix},
28ef6c31 2377separated by the first character of the @env{IFS} special variable.
3185942a
JA
2378When @samp{@@} is used and the expansion appears within double quotes, each
2379variable name expands to a separate word.
bb70624e 2380
b80f6443
JA
2381@item $@{!@var{name}[@@]@}
2382@itemx $@{!@var{name}[*]@}
2383If @var{name} is an array variable, expands to the list of array indices
2384(keys) assigned in @var{name}.
2385If @var{name} is not an array, expands to 0 if @var{name} is set and null
2386otherwise.
2387When @samp{@@} is used and the expansion appears within double quotes, each
2388key expands to a separate word.
2389
ccc6cda3 2390@item $@{#@var{parameter}@}
cce855bc
JA
2391The length in characters of the expanded value of @var{parameter} is
2392substituted.
2393If @var{parameter} is @samp{*} or @samp{@@}, the value substituted
2394is the number of positional parameters.
2395If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@},
2396the value substituted is the number of elements in the array.
ac50fbac
CR
2397If @var{parameter}
2398is an indexed array name subscripted by a negative number, that number is
2399interpreted as relative to one greater than the maximum index of
2400@var{parameter}, so negative indices count back from the end of the
2401array, and an index of -1 references the last element.
ccc6cda3
JA
2402
2403@item $@{@var{parameter}#@var{word}@}
2404@itemx $@{@var{parameter}##@var{word}@}
2405The @var{word}
d233b485
CR
2406is expanded to produce a pattern and matched according to the rules
2407described below (@pxref{Pattern Matching}). If the pattern matches
cce855bc
JA
2408the beginning of the expanded value of @var{parameter},
2409then the result of the expansion is the expanded value of @var{parameter}
ccc6cda3
JA
2410with the shortest matching pattern (the @samp{#} case) or the
2411longest matching pattern (the @samp{##} case) deleted.
2412If @var{parameter} is @samp{@@} or @samp{*},
2413the pattern removal operation is applied to each positional
2414parameter in turn, and the expansion is the resultant list.
2415If @var{parameter} is an array variable subscripted with
2416@samp{@@} or @samp{*},
2417the pattern removal operation is applied to each member of the
2418array in turn, and the expansion is the resultant list.
2419
2420@item $@{@var{parameter}%@var{word}@}
2421@itemx $@{@var{parameter}%%@var{word}@}
d233b485
CR
2422The @var{word}
2423is expanded to produce a pattern and matched according to the rules
8868edaf 2424described below (@pxref{Pattern Matching}).
cce855bc
JA
2425If the pattern matches a trailing portion of the expanded value of
2426@var{parameter}, then the result of the expansion is the value of
2427@var{parameter} with the shortest matching pattern (the @samp{%} case)
2428or the longest matching pattern (the @samp{%%} case) deleted.
ccc6cda3
JA
2429If @var{parameter} is @samp{@@} or @samp{*},
2430the pattern removal operation is applied to each positional
2431parameter in turn, and the expansion is the resultant list.
2432If @var{parameter}
2433is an array variable subscripted with @samp{@@} or @samp{*},
2434the pattern removal operation is applied to each member of the
2435array in turn, and the expansion is the resultant list.
2436
2437@item $@{@var{parameter}/@var{pattern}/@var{string}@}
74091dd4
CR
2438@itemx $@{@var{parameter}//@var{pattern}/@var{string}@}
2439@itemx $@{@var{parameter}/#@var{pattern}/@var{string}@}
2440@itemx $@{@var{parameter}/%@var{pattern}/@var{string}@}
ccc6cda3
JA
2441The @var{pattern} is expanded to produce a pattern just as in
2442filename expansion.
2443@var{Parameter} is expanded and the longest match of @var{pattern}
2444against its value is replaced with @var{string}.
74091dd4
CR
2445@var{string} undergoes tilde expansion, parameter and variable expansion,
2446arithmetic expansion, command and process substitution, and quote removal.
d233b485
CR
2447The match is performed according to the rules described below
2448(@pxref{Pattern Matching}).
74091dd4
CR
2449
2450In the first form above, only the first match is replaced.
2451If there are two slashes separating @var{parameter} and @var{pattern}
2452(the second form above), all matches of @var{pattern} are
2453replaced with @var{string}.
2454If @var{pattern} is preceded by @samp{#} (the third form above),
2455it must match at the beginning of the expanded value of @var{parameter}.
2456If @var{pattern} is preceded by @samp{%} (the fourth form above),
2457it must match at the end of the expanded value of @var{parameter}.
2458If the expansion of @var{string} is null,
2459matches of @var{pattern} are deleted.
2460If @var{string} is null,
2461matches of @var{pattern} are deleted
2462and the @samp{/} following @var{pattern} may be omitted.
2463
2464If the @code{patsub_replacement} shell option is enabled using @code{shopt},
2465any unquoted instances of @samp{&} in @var{string} are replaced with the
2466matching portion of @var{pattern}.
2467This is intended to duplicate a common @code{sed} idiom.
2468
2469Quoting any part of @var{string} inhibits replacement in the
2470expansion of the quoted portion, including replacement strings stored
2471in shell variables.
2472Backslash will escape @samp{&} in @var{string}; the backslash is removed
2473in order to permit a literal @samp{&} in the replacement string.
2474Users should take care if @var{string} is double-quoted to avoid
2475unwanted interactions between the backslash and double-quoting, since
2476backslash has special meaning within double quotes.
2477Pattern substitution performs the check for unquoted @samp{&} after
2478expanding @var{string},
2479so users should ensure to properly quote any occurrences of @samp{&}
2480they want to be taken literally in the replacement
2481and ensure any instances of @samp{&} they want to be replaced are unquoted.
2482
2483For instance,
2484
2485@example
2486var=abcdef
2487rep='& '
2488echo $@{var/abc/& @}
2489echo "$@{var/abc/& @}"
2490echo $@{var/abc/$rep@}
2491echo "$@{var/abc/$rep@}"
2492@end example
2493
2494@noindent
2495will display four lines of "abc def", while
2496
2497@example
2498var=abcdef
2499rep='& '
2500echo $@{var/abc/\& @}
2501echo "$@{var/abc/\& @}"
2502echo $@{var/abc/"& "@}
2503echo $@{var/abc/"$rep"@}
2504@end example
2505
2506@noindent
2507will display four lines of "& def".
2508Like the pattern removal operators, double quotes surrounding the
2509replacement string quote the expanded characters, while double quotes
2510enclosing the entire parameter substitution do not, since
2511the expansion is performed in a
2512context that doesn't take any enclosing double quotes into account.
2513
2514Since backslash can escape @samp{&}, it can also escape a backslash in
2515the replacement string.
2516This means that @samp{\\} will insert a literal
2517backslash into the replacement, so these two @code{echo} commands
2518
2519@example
2520var=abcdef
2521rep='\\&xyz'
2522echo $@{var/abc/\\&xyz@}
2523echo $@{var/abc/$rep@}
2524@end example
2525
2526@noindent
2527will both output @samp{\abcxyzdef}.
2528
2529It should rarely be necessary to enclose only @var{string} in double
2530quotes.
2531
a0c0a00f
CR
2532If the @code{nocasematch} shell option
2533(see the description of @code{shopt} in @ref{The Shopt Builtin})
2534is enabled, the match is performed without regard to the case
2535of alphabetic characters.
ccc6cda3
JA
2536If @var{parameter} is @samp{@@} or @samp{*},
2537the substitution operation is applied to each positional
2538parameter in turn, and the expansion is the resultant list.
2539If @var{parameter}
2540is an array variable subscripted with @samp{@@} or @samp{*},
2541the substitution operation is applied to each member of the
2542array in turn, and the expansion is the resultant list.
2543
3185942a
JA
2544@item $@{@var{parameter}^@var{pattern}@}
2545@itemx $@{@var{parameter}^^@var{pattern}@}
2546@itemx $@{@var{parameter},@var{pattern}@}
2547@itemx $@{@var{parameter},,@var{pattern}@}
2548This expansion modifies the case of alphabetic characters in @var{parameter}.
2549The @var{pattern} is expanded to produce a pattern just as in
0001803f 2550filename expansion.
ac50fbac
CR
2551Each character in the expanded value of @var{parameter} is tested against
2552@var{pattern}, and, if it matches the pattern, its case is converted.
2553The pattern should not attempt to match more than one character.
74091dd4 2554
3185942a
JA
2555The @samp{^} operator converts lowercase letters matching @var{pattern}
2556to uppercase; the @samp{,} operator converts matching uppercase letters
2557to lowercase.
2558The @samp{^^} and @samp{,,} expansions convert each matched character in the
2559expanded value; the @samp{^} and @samp{,} expansions match and convert only
17345e5a 2560the first character in the expanded value.
3185942a
JA
2561If @var{pattern} is omitted, it is treated like a @samp{?}, which matches
2562every character.
74091dd4 2563
3185942a
JA
2564If @var{parameter} is @samp{@@} or @samp{*},
2565the case modification operation is applied to each positional
2566parameter in turn, and the expansion is the resultant list.
2567If @var{parameter}
2568is an array variable subscripted with @samp{@@} or @samp{*},
2569the case modification operation is applied to each member of the
2570array in turn, and the expansion is the resultant list.
a0c0a00f
CR
2571
2572@item $@{@var{parameter}@@@var{operator}@}
2573The expansion is either a transformation of the value of @var{parameter}
2574or information about @var{parameter} itself, depending on the value of
2575@var{operator}. Each @var{operator} is a single letter:
2576
2577@table @code
8868edaf
CR
2578@item U
2579The expansion is a string that is the value of @var{parameter} with lowercase
2580alphabetic characters converted to uppercase.
2581@item u
2582The expansion is a string that is the value of @var{parameter} with the first
2583character converted to uppercase, if it is alphabetic.
2584@item L
2585The expansion is a string that is the value of @var{parameter} with uppercase
2586alphabetic characters converted to lowercase.
a0c0a00f
CR
2587@item Q
2588The expansion is a string that is the value of @var{parameter} quoted in a
2589format that can be reused as input.
2590@item E
2591The expansion is a string that is the value of @var{parameter} with backslash
d233b485 2592escape sequences expanded as with the @code{$'@dots{}'} quoting mechanism.
a0c0a00f
CR
2593@item P
2594The expansion is a string that is the result of expanding the value of
2595@var{parameter} as if it were a prompt string (@pxref{Controlling the Prompt}).
2596@item A
2597The expansion is a string in the form of
2598an assignment statement or @code{declare} command that, if
2599evaluated, will recreate @var{parameter} with its attributes and value.
8868edaf
CR
2600@item K
2601Produces a possibly-quoted version of the value of @var{parameter},
2602except that it prints the values of
2603indexed and associative arrays as a sequence of quoted key-value pairs
2604(@pxref{Arrays}).
a0c0a00f
CR
2605@item a
2606The expansion is a string consisting of flag values representing
2607@var{parameter}'s attributes.
74091dd4
CR
2608@item k
2609Like the @samp{K} transformation, but expands the keys and values of
2610indexed and associative arrays to separate words after word splitting.
a0c0a00f
CR
2611@end table
2612
2613If @var{parameter} is @samp{@@} or @samp{*},
2614the operation is applied to each positional
2615parameter in turn, and the expansion is the resultant list.
2616If @var{parameter}
2617is an array variable subscripted with @samp{@@} or @samp{*},
2618the operation is applied to each member of the
2619array in turn, and the expansion is the resultant list.
2620
8868edaf 2621The result of the expansion is subject to word splitting and filename
a0c0a00f 2622expansion as described below.
ccc6cda3
JA
2623@end table
2624
2625@node Command Substitution
2626@subsection Command Substitution
2627@cindex command substitution
2628
2629Command substitution allows the output of a command to replace
bb70624e
JA
2630the command itself.
2631Command substitution occurs when a command is enclosed as follows:
ccc6cda3
JA
2632@example
2633$(@var{command})
2634@end example
2635@noindent
2636or
2637@example
2638`@var{command}`
2639@end example
2640
2641@noindent
a0c0a00f
CR
2642Bash performs the expansion by executing @var{command} in a subshell environment
2643and replacing the command substitution with the standard output of the
ccc6cda3 2644command, with any trailing newlines deleted.
cce855bc
JA
2645Embedded newlines are not deleted, but they may be removed during
2646word splitting.
2647The command substitution @code{$(cat @var{file})} can be
2648replaced by the equivalent but faster @code{$(< @var{file})}.
ccc6cda3
JA
2649
2650When the old-style backquote form of substitution is used,
2651backslash retains its literal meaning except when followed by
cce855bc
JA
2652@samp{$}, @samp{`}, or @samp{\}.
2653The first backquote not preceded by a backslash terminates the
2654command substitution.
ccc6cda3
JA
2655When using the @code{$(@var{command})} form, all characters between
2656the parentheses make up the command; none are treated specially.
2657
cce855bc
JA
2658Command substitutions may be nested. To nest when using the backquoted
2659form, escape the inner backquotes with backslashes.
ccc6cda3
JA
2660
2661If the substitution appears within double quotes, word splitting and
2662filename expansion are not performed on the results.
2663
cce855bc
JA
2664@node Arithmetic Expansion
2665@subsection Arithmetic Expansion
2666@cindex expansion, arithmetic
2667@cindex arithmetic expansion
2668
2669Arithmetic expansion allows the evaluation of an arithmetic expression
2670and the substitution of the result. The format for arithmetic expansion is:
2671
2672@example
2673$(( @var{expression} ))
2674@end example
2675
74091dd4
CR
2676The @var{expression} undergoes the same expansions
2677as if it were within double quotes,
2678but double quote characters in @var{expression} are not treated specially
2679and are removed.
ac50fbac
CR
2680All tokens in the expression undergo parameter and variable expansion,
2681command substitution, and quote removal.
2682The result is treated as the arithmetic expression to be evaluated.
b80f6443 2683Arithmetic expansions may be nested.
cce855bc
JA
2684
2685The evaluation is performed according to the rules listed below
2686(@pxref{Shell Arithmetic}).
2687If the expression is invalid, Bash prints a message indicating
2688failure to the standard error and no substitution occurs.
2689
ccc6cda3
JA
2690@node Process Substitution
2691@subsection Process Substitution
2692@cindex process substitution
2693
a0c0a00f
CR
2694Process substitution allows a process's input or output to be
2695referred to using a filename.
ccc6cda3
JA
2696It takes the form of
2697@example
2698<(@var{list})
2699@end example
2700@noindent
2701or
2702@example
2703>(@var{list})
2704@end example
2705@noindent
a0c0a00f
CR
2706The process @var{list} is run asynchronously, and its input or output
2707appears as a filename.
2708This filename is
ccc6cda3 2709passed as an argument to the current command as the result of the
a0c0a00f
CR
2710expansion.
2711If the @code{>(@var{list})} form is used, writing to
ccc6cda3
JA
2712the file will provide input for @var{list}. If the
2713@code{<(@var{list})} form is used, the file passed as an
2714argument should be read to obtain the output of @var{list}.
bb70624e
JA
2715Note that no space may appear between the @code{<} or @code{>}
2716and the left parenthesis, otherwise the construct would be interpreted
2717as a redirection.
a0c0a00f
CR
2718Process substitution is supported on systems that support named
2719pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files.
ccc6cda3 2720
cce855bc
JA
2721When available, process substitution is performed simultaneously with
2722parameter and variable expansion, command substitution, and arithmetic
2723expansion.
ccc6cda3
JA
2724
2725@node Word Splitting
2726@subsection Word Splitting
2727@cindex word splitting
2728
2729The shell scans the results of parameter expansion, command substitution,
2730and arithmetic expansion that did not occur within double quotes for
2731word splitting.
2732
3185942a 2733The shell treats each character of @env{$IFS} as a delimiter, and splits
ac50fbac
CR
2734the results of the other expansions into words using these characters
2735as field terminators.
3185942a
JA
2736If @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>},
2737the default, then sequences of
2738@code{ <space>}, @code{<tab>}, and @code{<newline>}
2739at the beginning and end of the results of the previous
2740expansions are ignored, and any sequence of @env{IFS}
2741characters not at the beginning or end serves to delimit words.
2742If @env{IFS} has a value other than the default, then sequences of
a0c0a00f 2743the whitespace characters @code{space}, @code{tab}, and @code{newline}
ccc6cda3
JA
2744are ignored at the beginning and end of the
2745word, as long as the whitespace character is in the
28ef6c31
JA
2746value of @env{IFS} (an @env{IFS} whitespace character).
2747Any character in @env{IFS} that is not @env{IFS}
2748whitespace, along with any adjacent @env{IFS}
2749whitespace characters, delimits a field. A sequence of @env{IFS}
ccc6cda3 2750whitespace characters is also treated as a delimiter.
28ef6c31 2751If the value of @env{IFS} is null, no word splitting occurs.
ccc6cda3 2752
a0c0a00f
CR
2753Explicit null arguments (@code{""} or @code{''}) are retained
2754and passed to commands as empty strings.
ccc6cda3 2755Unquoted implicit null arguments, resulting from the expansion of
bb70624e 2756parameters that have no values, are removed.
ccc6cda3 2757If a parameter with no value is expanded within double quotes, a
a0c0a00f
CR
2758null argument results and is retained
2759and passed to a command as an empty string.
2760When a quoted null argument appears as part of a word whose expansion is
2761non-null, the null argument is removed.
2762That is, the word
2763@code{-d''} becomes @code{-d} after word splitting and
2764null argument removal.
ccc6cda3
JA
2765
2766Note that if no expansion occurs, no splitting
2767is performed.
2768
2769@node Filename Expansion
2770@subsection Filename Expansion
cce855bc
JA
2771@menu
2772* Pattern Matching:: How the shell matches patterns.
2773@end menu
ccc6cda3
JA
2774@cindex expansion, filename
2775@cindex expansion, pathname
2776@cindex filename expansion
2777@cindex pathname expansion
2778
28ef6c31 2779After word splitting, unless the @option{-f} option has been set
cce855bc 2780(@pxref{The Set Builtin}), Bash scans each word for the characters
bb70624e 2781@samp{*}, @samp{?}, and @samp{[}.
8868edaf 2782If one of these characters appears, and is not quoted, then the word is
ccc6cda3
JA
2783regarded as a @var{pattern},
2784and replaced with an alphabetically sorted list of
ac50fbac
CR
2785filenames matching the pattern (@pxref{Pattern Matching}).
2786If no matching filenames are found,
ccc6cda3 2787and the shell option @code{nullglob} is disabled, the word is left
cce855bc
JA
2788unchanged.
2789If the @code{nullglob} option is set, and no matches are found, the word
2790is removed.
b80f6443
JA
2791If the @code{failglob} shell option is set, and no matches are found,
2792an error message is printed and the command is not executed.
cce855bc
JA
2793If the shell option @code{nocaseglob} is enabled, the match is performed
2794without regard to the case of alphabetic characters.
2795
0001803f 2796When a pattern is used for filename expansion, the character @samp{.}
ccc6cda3 2797at the start of a filename or immediately following a slash
cce855bc 2798must be matched explicitly, unless the shell option @code{dotglob} is set.
74091dd4
CR
2799In order to match the filenames @samp{.} and @samp{..},
2800the pattern must begin with @samp{.} (for example, @samp{.?}),
d233b485 2801even if @code{dotglob} is set.
74091dd4
CR
2802If the @code{globskipdots} shell option is enabled, the filenames
2803@samp{.} and @samp{..} are never matched, even if the pattern begins
2804with a @samp{.}.
2805When not matching filenames, the @samp{.} character is not treated specially.
cce855bc 2806
d233b485
CR
2807When matching a filename, the slash character must always be
2808matched explicitly by a slash in the pattern, but in other matching
2809contexts it can be matched by a special pattern character as described
2810below (@pxref{Pattern Matching}).
2811
3185942a 2812See the description of @code{shopt} in @ref{The Shopt Builtin},
cce855bc 2813for a description of the @code{nocaseglob}, @code{nullglob},
74091dd4 2814@code{globskipdots},
b80f6443 2815@code{failglob}, and @code{dotglob} options.
ccc6cda3 2816
28ef6c31 2817The @env{GLOBIGNORE}
d233b485 2818shell variable may be used to restrict the set of file names matching a
28ef6c31 2819pattern. If @env{GLOBIGNORE}
d233b485 2820is set, each matching file name that also matches one of the patterns in
a0c0a00f
CR
2821@env{GLOBIGNORE} is removed from the list of matches.
2822If the @code{nocaseglob} option is set, the matching against the patterns in
2823@env{GLOBIGNORE} is performed without regard to case.
2824The filenames
ccc6cda3 2825@file{.} and @file{..}
b80f6443
JA
2826are always ignored when @env{GLOBIGNORE}
2827is set and not null.
2828However, setting @env{GLOBIGNORE} to a non-null value has the effect of
ccc6cda3
JA
2829enabling the @code{dotglob}
2830shell option, so all other filenames beginning with a
2831@samp{.} will match.
2832To get the old behavior of ignoring filenames beginning with a
28ef6c31
JA
2833@samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}.
2834The @code{dotglob} option is disabled when @env{GLOBIGNORE}
ccc6cda3
JA
2835is unset.
2836
cce855bc
JA
2837@node Pattern Matching
2838@subsubsection Pattern Matching
2839@cindex pattern matching
2840@cindex matching, pattern
2841
2842Any character that appears in a pattern, other than the special pattern
b80f6443
JA
2843characters described below, matches itself.
2844The @sc{nul} character may not occur in a pattern.
2845A backslash escapes the following character; the
2846escaping backslash is discarded when matching.
2847The special pattern characters must be quoted if they are to be matched
2848literally.
cce855bc 2849
ccc6cda3
JA
2850The special pattern characters have the following meanings:
2851@table @code
2852@item *
2853Matches any string, including the null string.
3185942a
JA
2854When the @code{globstar} shell option is enabled, and @samp{*} is used in
2855a filename expansion context, two adjacent @samp{*}s used as a single
2856pattern will match all files and zero or more directories and
2857subdirectories.
2858If followed by a @samp{/}, two adjacent @samp{*}s will match only
2859directories and subdirectories.
ccc6cda3
JA
2860@item ?
2861Matches any single character.
2862@item [@dots{}]
2863Matches any one of the enclosed characters. A pair of characters
28ef6c31 2864separated by a hyphen denotes a @var{range expression};
ac50fbac 2865any character that falls between those two characters, inclusive,
28ef6c31 2866using the current locale's collating sequence and character set,
ccc6cda3
JA
2867is matched. If the first character following the
2868@samp{[} is a @samp{!} or a @samp{^}
2869then any character not enclosed is matched. A @samp{@minus{}}
2870may be matched by including it as the first or last character
2871in the set. A @samp{]} may be matched by including it as the first
2872character in the set.
74091dd4
CR
2873The sorting order of characters in range expressions,
2874and the characters included in the range,
2875are determined by
ac50fbac
CR
2876the current locale and the values of the
2877@env{LC_COLLATE} and @env{LC_ALL} shell variables, if set.
28ef6c31
JA
2878
2879For example, in the default C locale, @samp{[a-dx-z]} is equivalent to
2880@samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in
2881these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]};
74091dd4 2882it might be equivalent to @samp{[aBbCcDdxYyZz]}, for example. To obtain
28ef6c31
JA
2883the traditional interpretation of ranges in bracket expressions, you can
2884force the use of the C locale by setting the @env{LC_COLLATE} or
ac50fbac
CR
2885@env{LC_ALL} environment variable to the value @samp{C}, or enable the
2886@code{globasciiranges} shell option.
cce855bc 2887
74091dd4 2888Within @samp{[} and @samp{]}, @dfn{character classes} can be specified
cce855bc
JA
2889using the syntax
2890@code{[:}@var{class}@code{:]}, where @var{class} is one of the
0628567a 2891following classes defined in the @sc{posix} standard:
cce855bc
JA
2892@example
2893alnum alpha ascii blank cntrl digit graph lower
7117c2d2 2894print punct space upper word xdigit
cce855bc
JA
2895@end example
2896@noindent
2897A character class matches any character belonging to that class.
7117c2d2
JA
2898The @code{word} character class matches letters, digits, and the character
2899@samp{_}.
cce855bc 2900
74091dd4 2901Within @samp{[} and @samp{]}, an @dfn{equivalence class} can be
cce855bc
JA
2902specified using the syntax @code{[=}@var{c}@code{=]}, which
2903matches all characters with the same collation weight (as defined
2904by the current locale) as the character @var{c}.
2905
2906Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]}
2907matches the collating symbol @var{symbol}.
2908@end table
2909
2910If the @code{extglob} shell option is enabled using the @code{shopt}
74091dd4 2911builtin, the shell recognizes several extended pattern matching operators.
cce855bc
JA
2912In the following description, a @var{pattern-list} is a list of one
2913or more patterns separated by a @samp{|}.
74091dd4
CR
2914When matching filenames, the @code{dotglob} shell option determines
2915the set of filenames that are tested, as described above.
cce855bc
JA
2916Composite patterns may be formed using one or more of the following
2917sub-patterns:
2918
2919@table @code
2920@item ?(@var{pattern-list})
2921Matches zero or one occurrence of the given patterns.
2922
2923@item *(@var{pattern-list})
2924Matches zero or more occurrences of the given patterns.
2925
2926@item +(@var{pattern-list})
2927Matches one or more occurrences of the given patterns.
2928
2929@item @@(@var{pattern-list})
95732b49 2930Matches one of the given patterns.
cce855bc
JA
2931
2932@item !(@var{pattern-list})
2933Matches anything except one of the given patterns.
ccc6cda3
JA
2934@end table
2935
74091dd4
CR
2936The @code{extglob} option changes the behavior of the parser, since the
2937parentheses are normally treated as operators with syntactic meaning.
2938To ensure that extended matching patterns are parsed correctly, make sure
2939that @code{extglob} is enabled before parsing constructs containing the
2940patterns, including shell functions and command substitutions.
2941
2942When matching filenames, the @code{dotglob} shell option determines
2943the set of filenames that are tested:
2944when @code{dotglob} is enabled, the set of filenames includes all files
2945beginning with @samp{.}, but the filenames
2946@samp{.} and @samp{..} must be matched by a
2947pattern or sub-pattern that begins with a dot;
2948when it is disabled, the set does not
2949include any filenames beginning with ``.'' unless the pattern
2950or sub-pattern begins with a @samp{.}.
2951As above, @samp{.} only has a special meaning when matching filenames.
2952
d233b485
CR
2953Complicated extended pattern matching against long strings is slow,
2954especially when the patterns contain alternations and the strings
2955contain multiple matches.
2956Using separate matches against shorter strings, or using arrays of
2957strings instead of a single long string, may be faster.
2958
ccc6cda3
JA
2959@node Quote Removal
2960@subsection Quote Removal
2961
2962After the preceding expansions, all unquoted occurrences of the
2963characters @samp{\}, @samp{'}, and @samp{"} that did not
2964result from one of the above expansions are removed.
2965
2966@node Redirections
2967@section Redirections
2968@cindex redirection
2969
2970Before a command is executed, its input and output
74091dd4 2971may be @dfn{redirected}
ccc6cda3 2972using a special notation interpreted by the shell.
74091dd4 2973@dfn{Redirection} allows commands' file handles to be
ac50fbac
CR
2974duplicated, opened, closed,
2975made to refer to different files,
2976and can change the files the command reads from and writes to.
2977Redirection may also be used to modify file handles in the
ccc6cda3
JA
2978current shell execution environment. The following redirection
2979operators may precede or appear anywhere within a
2980simple command or may follow a command.
2981Redirections are processed in the order they appear, from
2982left to right.
2983
0001803f
CR
2984Each redirection that may be preceded by a file descriptor number
2985may instead be preceded by a word of the form @{@var{varname}@}.
2986In this case, for each redirection operator except
2987>&- and <&-, the shell will allocate a file descriptor greater
2988than 10 and assign it to @{@var{varname}@}. If >&- or <&- is preceded
2989by @{@var{varname}@}, the value of @var{varname} defines the file
2990descriptor to close.
d233b485
CR
2991If @{@var{varname}@} is supplied, the redirection persists beyond
2992the scope of the command, allowing the shell programmer to manage
8868edaf 2993the file descriptor's lifetime manually.
74091dd4
CR
2994The @code{varredir_close} shell option manages this behavior
2995(@pxref{The Shopt Builtin}).
0001803f 2996
ccc6cda3
JA
2997In the following descriptions, if the file descriptor number is
2998omitted, and the first character of the redirection operator is
2999@samp{<}, the redirection refers to the standard input (file
3000descriptor 0). If the first character of the redirection operator
3001is @samp{>}, the redirection refers to the standard output (file
3002descriptor 1).
3003
cce855bc
JA
3004The word following the redirection operator in the following
3005descriptions, unless otherwise noted, is subjected to brace expansion,
3006tilde expansion, parameter expansion, command substitution, arithmetic
bb70624e 3007expansion, quote removal, filename expansion, and word splitting.
cce855bc 3008If it expands to more than one word, Bash reports an error.
ccc6cda3
JA
3009
3010Note that the order of redirections is significant. For example,
3011the command
3012@example
3013ls > @var{dirlist} 2>&1
3014@end example
3015@noindent
bb70624e
JA
3016directs both standard output (file descriptor 1) and standard error
3017(file descriptor 2) to the file @var{dirlist}, while the command
ccc6cda3
JA
3018@example
3019ls 2>&1 > @var{dirlist}
3020@end example
3021@noindent
3022directs only the standard output to file @var{dirlist},
17345e5a 3023because the standard error was made a copy of the standard output
ccc6cda3
JA
3024before the standard output was redirected to @var{dirlist}.
3025
bb70624e 3026Bash handles several filenames specially when they are used in
a0c0a00f
CR
3027redirections, as described in the following table.
3028If the operating system on which Bash is running provides these
3029special files, bash will use them; otherwise it will emulate them
3030internally with the behavior described below.
bb70624e
JA
3031
3032@table @code
3033@item /dev/fd/@var{fd}
3034If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated.
3035
3036@item /dev/stdin
3037File descriptor 0 is duplicated.
3038
3039@item /dev/stdout
3040File descriptor 1 is duplicated.
3041
3042@item /dev/stderr
3043File descriptor 2 is duplicated.
3044
3045@item /dev/tcp/@var{host}/@var{port}
3046If @var{host} is a valid hostname or Internet address, and @var{port}
ac50fbac
CR
3047is an integer port number or service name, Bash attempts to open
3048the corresponding TCP socket.
bb70624e
JA
3049
3050@item /dev/udp/@var{host}/@var{port}
3051If @var{host} is a valid hostname or Internet address, and @var{port}
ac50fbac
CR
3052is an integer port number or service name, Bash attempts to open
3053the corresponding UDP socket.
bb70624e
JA
3054@end table
3055
cce855bc
JA
3056A failure to open or create a file causes the redirection to fail.
3057
95732b49
JA
3058Redirections using file descriptors greater than 9 should be used with
3059care, as they may conflict with file descriptors the shell uses
3060internally.
3061
ccc6cda3
JA
3062@subsection Redirecting Input
3063Redirection of input causes the file whose name results from
3064the expansion of @var{word}
3065to be opened for reading on file descriptor @code{n},
3066or the standard input (file descriptor 0) if @code{n}
3067is not specified.
3068
3069The general format for redirecting input is:
3070@example
7117c2d2 3071[@var{n}]<@var{word}
ccc6cda3
JA
3072@end example
3073
3074@subsection Redirecting Output
3075Redirection of output causes the file whose name results from
3076the expansion of @var{word}
7117c2d2
JA
3077to be opened for writing on file descriptor @var{n},
3078or the standard output (file descriptor 1) if @var{n}
ccc6cda3
JA
3079is not specified. If the file does not exist it is created;
3080if it does exist it is truncated to zero size.
3081
3082The general format for redirecting output is:
3083@example
7117c2d2 3084[@var{n}]>[|]@var{word}
ccc6cda3
JA
3085@end example
3086
cce855bc
JA
3087If the redirection operator is @samp{>}, and the @code{noclobber}
3088option to the @code{set} builtin has been enabled, the redirection
bb70624e 3089will fail if the file whose name results from the expansion of
cce855bc
JA
3090@var{word} exists and is a regular file.
3091If the redirection operator is @samp{>|}, or the redirection operator is
3092@samp{>} and the @code{noclobber} option is not enabled, the redirection
3093is attempted even if the file named by @var{word} exists.
ccc6cda3
JA
3094
3095@subsection Appending Redirected Output
3096Redirection of output in this fashion
3097causes the file whose name results from
3098the expansion of @var{word}
7117c2d2
JA
3099to be opened for appending on file descriptor @var{n},
3100or the standard output (file descriptor 1) if @var{n}
ccc6cda3
JA
3101is not specified. If the file does not exist it is created.
3102
3103The general format for appending output is:
3104@example
7117c2d2 3105[@var{n}]>>@var{word}
ccc6cda3
JA
3106@end example
3107
3108@subsection Redirecting Standard Output and Standard Error
3185942a 3109This construct allows both the
ccc6cda3
JA
3110standard output (file descriptor 1) and
3111the standard error output (file descriptor 2)
3112to be redirected to the file whose name is the
3185942a 3113expansion of @var{word}.
ccc6cda3
JA
3114
3115There are two formats for redirecting standard output and
3116standard error:
3117@example
3118&>@var{word}
3119@end example
3120@noindent
3121and
3122@example
3123>&@var{word}
3124@end example
3125@noindent
3126Of the two forms, the first is preferred.
3127This is semantically equivalent to
3128@example
3129>@var{word} 2>&1
3130@end example
ac50fbac
CR
3131When using the second form, @var{word} may not expand to a number or
3132@samp{-}. If it does, other redirection operators apply
3133(see Duplicating File Descriptors below) for compatibility reasons.
ccc6cda3 3134
3185942a
JA
3135@subsection Appending Standard Output and Standard Error
3136This construct allows both the
3137standard output (file descriptor 1) and
3138the standard error output (file descriptor 2)
3139to be appended to the file whose name is the
3140expansion of @var{word}.
3141
3142The format for appending standard output and standard error is:
3143@example
3144&>>@var{word}
3145@end example
3146@noindent
3147This is semantically equivalent to
3148@example
3149>>@var{word} 2>&1
3150@end example
ac50fbac 3151(see Duplicating File Descriptors below).
3185942a 3152
ccc6cda3
JA
3153@subsection Here Documents
3154This type of redirection instructs the shell to read input from the
3155current source until a line containing only @var{word}
3156(with no trailing blanks) is seen. All of
3157the lines read up to that point are then used as the standard
a0c0a00f 3158input (or file descriptor @var{n} if @var{n} is specified) for a command.
ccc6cda3 3159
7117c2d2 3160The format of here-documents is:
ccc6cda3 3161@example
a0c0a00f 3162[@var{n}]<<[@minus{}]@var{word}
ccc6cda3
JA
3163 @var{here-document}
3164@var{delimiter}
3165@end example
3166
ac50fbac
CR
3167No parameter and variable expansion, command substitution,
3168arithmetic expansion, or filename expansion is performed on
a0c0a00f 3169@var{word}. If any part of @var{word} is quoted, the
ccc6cda3 3170@var{delimiter} is the result of quote removal on @var{word},
cce855bc
JA
3171and the lines in the here-document are not expanded.
3172If @var{word} is unquoted,
ac50fbac
CR
3173all lines of the here-document are subjected to
3174parameter expansion, command substitution, and arithmetic expansion,
3175the character sequence @code{\newline} is ignored, and @samp{\}
ccc6cda3
JA
3176must be used to quote the characters
3177@samp{\}, @samp{$}, and @samp{`}.
3178
3179If the redirection operator is @samp{<<-},
3180then all leading tab characters are stripped from input lines and the
3181line containing @var{delimiter}.
3182This allows here-documents within shell scripts to be indented in a
3183natural fashion.
3184
7117c2d2
JA
3185@subsection Here Strings
3186A variant of here documents, the format is:
3187@example
a0c0a00f 3188[@var{n}]<<< @var{word}
7117c2d2
JA
3189@end example
3190
ac50fbac 3191The @var{word} undergoes
d233b485 3192tilde expansion, parameter and variable expansion,
ac50fbac 3193command substitution, arithmetic expansion, and quote removal.
8868edaf 3194Filename expansion and word splitting are not performed.
a0c0a00f
CR
3195The result is supplied as a single string,
3196with a newline appended,
3197to the command on its
3198standard input (or file descriptor @var{n} if @var{n} is specified).
7117c2d2 3199
ccc6cda3
JA
3200@subsection Duplicating File Descriptors
3201The redirection operator
3202@example
7117c2d2 3203[@var{n}]<&@var{word}
ccc6cda3
JA
3204@end example
3205@noindent
3206is used to duplicate input file descriptors.
3207If @var{word}
7117c2d2 3208expands to one or more digits, the file descriptor denoted by @var{n}
cce855bc
JA
3209is made to be a copy of that file descriptor.
3210If the digits in @var{word} do not specify a file descriptor open for
3211input, a redirection error occurs.
3212If @var{word}
ac50fbac
CR
3213evaluates to @samp{-}, file descriptor @var{n} is closed.
3214If @var{n} is not specified, the standard input (file descriptor 0) is used.
ccc6cda3
JA
3215
3216The operator
3217@example
7117c2d2 3218[@var{n}]>&@var{word}
ccc6cda3
JA
3219@end example
3220@noindent
3221is used similarly to duplicate output file descriptors. If
7117c2d2 3222@var{n} is not specified, the standard output (file descriptor 1) is used.
cce855bc
JA
3223If the digits in @var{word} do not specify a file descriptor open for
3224output, a redirection error occurs.
ac50fbac
CR
3225If @var{word}
3226evaluates to @samp{-}, file descriptor @var{n} is closed.
7117c2d2 3227As a special case, if @var{n} is omitted, and @var{word} does not
ac50fbac 3228expand to one or more digits or @samp{-}, the standard output and standard
ccc6cda3
JA
3229error are redirected as described previously.
3230
7117c2d2
JA
3231@subsection Moving File Descriptors
3232The redirection operator
3233@example
3234[@var{n}]<&@var{digit}-
3235@end example
3236@noindent
3237moves the file descriptor @var{digit} to file descriptor @var{n},
3238or the standard input (file descriptor 0) if @var{n} is not specified.
3239@var{digit} is closed after being duplicated to @var{n}.
3240
3241Similarly, the redirection operator
3242@example
3243[@var{n}]>&@var{digit}-
3244@end example
3245@noindent
3246moves the file descriptor @var{digit} to file descriptor @var{n},
3247or the standard output (file descriptor 1) if @var{n} is not specified.
3248
ccc6cda3
JA
3249@subsection Opening File Descriptors for Reading and Writing
3250The redirection operator
3251@example
7117c2d2 3252[@var{n}]<>@var{word}
ccc6cda3
JA
3253@end example
3254@noindent
3255causes the file whose name is the expansion of @var{word}
3256to be opened for both reading and writing on file descriptor
7117c2d2 3257@var{n}, or on file descriptor 0 if @var{n}
ccc6cda3
JA
3258is not specified. If the file does not exist, it is created.
3259
3260@node Executing Commands
3261@section Executing Commands
3262
3263@menu
cce855bc
JA
3264* Simple Command Expansion:: How Bash expands simple commands before
3265 executing them.
ccc6cda3 3266* Command Search and Execution:: How Bash finds commands and runs them.
cce855bc
JA
3267* Command Execution Environment:: The environment in which Bash
3268 executes commands that are not
3269 shell builtins.
ccc6cda3 3270* Environment:: The environment given to a command.
ccc6cda3
JA
3271* Exit Status:: The status returned by commands and how Bash
3272 interprets it.
ccc6cda3
JA
3273* Signals:: What happens when Bash or a command it runs
3274 receives a signal.
3275@end menu
3276
cce855bc
JA
3277@node Simple Command Expansion
3278@subsection Simple Command Expansion
3279@cindex command expansion
3280
3281When a simple command is executed, the shell performs the following
8868edaf
CR
3282expansions, assignments, and redirections, from left to right, in
3283the following order.
cce855bc
JA
3284
3285@enumerate
3286@item
3287The words that the parser has marked as variable assignments (those
3288preceding the command name) and redirections are saved for later
3289processing.
3290
3291@item
3292The words that are not variable assignments or redirections are
3293expanded (@pxref{Shell Expansions}).
3294If any words remain after expansion, the first word
3295is taken to be the name of the command and the remaining words are
3296the arguments.
3297
3298@item
3299Redirections are performed as described above (@pxref{Redirections}).
3300
3301@item
3302The text after the @samp{=} in each variable assignment undergoes tilde
3303expansion, parameter expansion, command substitution, arithmetic expansion,
3304and quote removal before being assigned to the variable.
3305@end enumerate
3306
3307If no command name results, the variable assignments affect the current
74091dd4
CR
3308shell environment.
3309In the case of such a command (one that consists only of assignment
3310statements and redirections), assignment statements are performed before
3311redirections.
3312Otherwise, the variables are added to the environment
cce855bc
JA
3313of the executed command and do not affect the current shell environment.
3314If any of the assignments attempts to assign a value to a readonly variable,
3315an error occurs, and the command exits with a non-zero status.
3316
3317If no command name results, redirections are performed, but do not
3318affect the current shell environment. A redirection error causes the
3319command to exit with a non-zero status.
3320
3321If there is a command name left after expansion, execution proceeds as
3322described below. Otherwise, the command exits. If one of the expansions
3323contained a command substitution, the exit status of the command is
3324the exit status of the last command substitution performed. If there
3325were no command substitutions, the command exits with a status of zero.
3326
ccc6cda3
JA
3327@node Command Search and Execution
3328@subsection Command Search and Execution
3329@cindex command execution
3330@cindex command search
3331
3332After a command has been split into words, if it results in a
3333simple command and an optional list of arguments, the following
3334actions are taken.
3335
3336@enumerate
3337@item
3338If the command name contains no slashes, the shell attempts to
3339locate it. If there exists a shell function by that name, that
bb70624e 3340function is invoked as described in @ref{Shell Functions}.
ccc6cda3
JA
3341
3342@item
3343If the name does not match a function, the shell searches for
3344it in the list of shell builtins. If a match is found, that
3345builtin is invoked.
3346
3347@item
3348If the name is neither a shell function nor a builtin,
3349and contains no slashes, Bash searches each element of
28ef6c31 3350@env{$PATH} for a directory containing an executable file
ccc6cda3 3351by that name. Bash uses a hash table to remember the full
28ef6c31 3352pathnames of executable files to avoid multiple @env{PATH} searches
cce855bc 3353(see the description of @code{hash} in @ref{Bourne Shell Builtins}).
28ef6c31 3354A full search of the directories in @env{$PATH}
ccc6cda3 3355is performed only if the command is not found in the hash table.
3185942a
JA
3356If the search is unsuccessful, the shell searches for a defined shell
3357function named @code{command_not_found_handle}.
d233b485
CR
3358If that function exists, it is invoked in a separate execution environment
3359with the original command and
3185942a 3360the original command's arguments as its arguments, and the function's
d233b485 3361exit status becomes the exit status of that subshell.
3185942a 3362If that function is not defined, the shell prints an error
cce855bc 3363message and returns an exit status of 127.
ccc6cda3
JA
3364
3365@item
3366If the search is successful, or if the command name contains
cce855bc
JA
3367one or more slashes, the shell executes the named program in
3368a separate execution environment.
ccc6cda3
JA
3369Argument 0 is set to the name given, and the remaining arguments
3370to the command are set to the arguments supplied, if any.
3371
3372@item
3373If this execution fails because the file is not in executable
cce855bc 3374format, and the file is not a directory, it is assumed to be a
74091dd4 3375@dfn{shell script} and the shell executes it as described in
cce855bc
JA
3376@ref{Shell Scripts}.
3377
3378@item
3379If the command was not begun asynchronously, the shell waits for
3380the command to complete and collects its exit status.
3381
ccc6cda3
JA
3382@end enumerate
3383
cce855bc
JA
3384@node Command Execution Environment
3385@subsection Command Execution Environment
3386@cindex execution environment
3387
74091dd4 3388The shell has an @dfn{execution environment}, which consists of the
cce855bc
JA
3389following:
3390
3391@itemize @bullet
3392@item
3393open files inherited by the shell at invocation, as modified by
3394redirections supplied to the @code{exec} builtin
3395
3396@item
3397the current working directory as set by @code{cd}, @code{pushd}, or
3398@code{popd}, or inherited by the shell at invocation
3399
3400@item
3401the file creation mode mask as set by @code{umask} or inherited from
3402the shell's parent
3403
3404@item
3405current traps set by @code{trap}
3406
3407@item
3408shell parameters that are set by variable assignment or with @code{set}
3409or inherited from the shell's parent in the environment
3410
3411@item
3412shell functions defined during execution or inherited from the shell's
3413parent in the environment
3414
3415@item
3416options enabled at invocation (either by default or with command-line
3417arguments) or by @code{set}
3418
3419@item
3185942a 3420options enabled by @code{shopt} (@pxref{The Shopt Builtin})
cce855bc
JA
3421
3422@item
3423shell aliases defined with @code{alias} (@pxref{Aliases})
3424
3425@item
bb70624e 3426various process @sc{id}s, including those of background jobs
cce855bc 3427(@pxref{Lists}), the value of @code{$$}, and the value of
28ef6c31 3428@env{$PPID}
cce855bc
JA
3429
3430@end itemize
3431
3432When a simple command other than a builtin or shell function
3433is to be executed, it
3434is invoked in a separate execution environment that consists of
3435the following. Unless otherwise noted, the values are inherited
3436from the shell.
3437
3438@itemize @bullet
3439@item
3440the shell's open files, plus any modifications and additions specified
3441by redirections to the command
3442
3443@item
3444the current working directory
3445
3446@item
3447the file creation mode mask
3448
3449@item
b80f6443
JA
3450shell variables and functions marked for export, along with variables
3451exported for the command, passed in the environment (@pxref{Environment})
cce855bc
JA
3452
3453@item
3454traps caught by the shell are reset to the values inherited from the
3455shell's parent, and traps ignored by the shell are ignored
3456
3457@end itemize
3458
3459A command invoked in this separate environment cannot affect the
a0c0a00f 3460shell's execution environment.
cce855bc 3461
74091dd4
CR
3462A @dfn{subshell} is a copy of the shell process.
3463
b80f6443
JA
3464Command substitution, commands grouped with parentheses,
3465and asynchronous commands are invoked in a
cce855bc
JA
3466subshell environment that is a duplicate of the shell environment,
3467except that traps caught by the shell are reset to the values
3468that the shell inherited from its parent at invocation. Builtin
3469commands that are invoked as part of a pipeline are also executed
3470in a subshell environment. Changes made to the subshell environment
3471cannot affect the shell's execution environment.
3472
3185942a
JA
3473Subshells spawned to execute command substitutions inherit the value of
3474the @option{-e} option from the parent shell. When not in @sc{posix} mode,
3475Bash clears the @option{-e} option in such subshells.
3476
f73dda09
JA
3477If a command is followed by a @samp{&} and job control is not active, the
3478default standard input for the command is the empty file @file{/dev/null}.
3479Otherwise, the invoked command inherits the file descriptors of the calling
3480shell as modified by redirections.
3481
ccc6cda3
JA
3482@node Environment
3483@subsection Environment
3484@cindex environment
3485
3486When a program is invoked it is given an array of strings
74091dd4 3487called the @dfn{environment}.
ccc6cda3
JA
3488This is a list of name-value pairs, of the form @code{name=value}.
3489
bb70624e
JA
3490Bash provides several ways to manipulate the environment.
3491On invocation, the shell scans its own environment and
ccc6cda3 3492creates a parameter for each name found, automatically marking
74091dd4 3493it for @code{export}
ccc6cda3
JA
3494to child processes. Executed commands inherit the environment.
3495The @code{export} and @samp{declare -x}
3496commands allow parameters and functions to be added to and
3497deleted from the environment. If the value of a parameter
3498in the environment is modified, the new value becomes part
3499of the environment, replacing the old. The environment
3500inherited by any executed command consists of the shell's
3501initial environment, whose values may be modified in the shell,
cce855bc
JA
3502less any pairs removed by the @code{unset} and @samp{export -n}
3503commands, plus any additions via the @code{export} and
3504@samp{declare -x} commands.
ccc6cda3
JA
3505
3506The environment for any simple command
3507or function may be augmented temporarily by prefixing it with
3508parameter assignments, as described in @ref{Shell Parameters}.
3509These assignment statements affect only the environment seen
3510by that command.
3511
28ef6c31 3512If the @option{-k} option is set (@pxref{The Set Builtin}), then all
ccc6cda3
JA
3513parameter assignments are placed in the environment for a command,
3514not just those that precede the command name.
3515
3516When Bash invokes an external command, the variable @samp{$_}
ac50fbac 3517is set to the full pathname of the command and passed to that
ccc6cda3
JA
3518command in its environment.
3519
3520@node Exit Status
3521@subsection Exit Status
3522@cindex exit status
3523
3185942a 3524The exit status of an executed command is the value returned by the
74091dd4 3525@code{waitpid} system call or equivalent function. Exit statuses
3185942a
JA
3526fall between 0 and 255, though, as explained below, the shell may
3527use values above 125 specially. Exit statuses from shell builtins and
ac50fbac 3528compound commands are also limited to this range. Under certain
3185942a
JA
3529circumstances, the shell will use special values to indicate specific
3530failure modes.
3531
cce855bc 3532For the shell's purposes, a command which exits with a
ccc6cda3
JA
3533zero exit status has succeeded.
3534A non-zero exit status indicates failure.
3535This seemingly counter-intuitive scheme is used so there
3536is one well-defined way to indicate success and a variety of
3537ways to indicate various failure modes.
bb70624e
JA
3538When a command terminates on a fatal signal whose number is @var{N},
3539Bash uses the value 128+@var{N} as the exit status.
ccc6cda3
JA
3540
3541If a command is not found, the child process created to
3542execute it returns a status of 127. If a command is found
3543but is not executable, the return status is 126.
3544
cce855bc
JA
3545If a command fails because of an error during expansion or redirection,
3546the exit status is greater than zero.
3547
ccc6cda3
JA
3548The exit status is used by the Bash conditional commands
3549(@pxref{Conditional Constructs}) and some of the list
3550constructs (@pxref{Lists}).
3551
3552All of the Bash builtins return an exit status of zero if they succeed
3553and a non-zero status on failure, so they may be used by the
3554conditional and list constructs.
a0c0a00f
CR
3555All builtins return an exit status of 2 to indicate incorrect usage,
3556generally invalid options or missing arguments.
ccc6cda3 3557
74091dd4
CR
3558The exit status of the last command is available in the special
3559parameter $? (@pxref{Special Parameters}).
3560
ccc6cda3
JA
3561@node Signals
3562@subsection Signals
3563@cindex signal handling
3564
cce855bc 3565When Bash is interactive, in the absence of any traps, it ignores
ccc6cda3
JA
3566@code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell),
3567and @code{SIGINT}
3568is caught and handled (so that the @code{wait} builtin is interruptible).
3569When Bash receives a @code{SIGINT}, it breaks out of any executing loops.
3570In all cases, Bash ignores @code{SIGQUIT}.
3571If job control is in effect (@pxref{Job Control}), Bash
3572ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3573
b80f6443 3574Non-builtin commands started by Bash have signal handlers set to the
cce855bc
JA
3575values inherited by the shell from its parent.
3576When job control is not in effect, asynchronous commands
b80f6443
JA
3577ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited
3578handlers.
cce855bc
JA
3579Commands run as a result of
3580command substitution ignore the keyboard-generated job control signals
ccc6cda3
JA
3581@code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
3582
3583The shell exits by default upon receipt of a @code{SIGHUP}.
f73dda09
JA
3584Before exiting, an interactive shell resends the @code{SIGHUP} to
3585all jobs, running or stopped.
cce855bc
JA
3586Stopped jobs are sent @code{SIGCONT} to ensure that they receive
3587the @code{SIGHUP}.
3588To prevent the shell from sending the @code{SIGHUP} signal to a
3589particular job, it should be removed
3590from the jobs table with the @code{disown}
3591builtin (@pxref{Job Control Builtins}) or marked
3592to not receive @code{SIGHUP} using @code{disown -h}.
3593
3594If the @code{huponexit} shell option has been set with @code{shopt}
3185942a 3595(@pxref{The Shopt Builtin}), Bash sends a @code{SIGHUP} to all jobs when
cce855bc
JA
3596an interactive login shell exits.
3597
b80f6443
JA
3598If Bash is waiting for a command to complete and receives a signal
3599for which a trap has been set, the trap will not be executed until
a0c0a00f 3600the command completes.
cce855bc
JA
3601When Bash is waiting for an asynchronous
3602command via the @code{wait} builtin, the reception of a signal for
3603which a trap has been set will cause the @code{wait} builtin to return
3604immediately with an exit status greater than 128, immediately after
3605which the trap is executed.
ccc6cda3 3606
74091dd4
CR
3607When job control is not enabled, and Bash is waiting for a foreground
3608command to complete, the shell receives keyboard-generated signals
3609such as @code{SIGINT} (usually generated by @samp{^C}) that users
3610commonly intend to send to that command.
3611This happens because the shell and the command are in the same process
3612group as the terminal, and @samp{^C} sends @code{SIGINT} to all processes
3613in that process group.
3614See @ref{Job Control}, for a more in-depth discussion of process groups.
3615
3616When Bash is running without job control enabled and receives @code{SIGINT}
3617while waiting for a foreground command, it waits until that foreground
3618command terminates and then decides what to do about the @code{SIGINT}:
3619
3620@enumerate
3621@item
3622If the command terminates due to the @code{SIGINT}, Bash concludes
3623that the user meant to end the entire script, and acts on the
3624@code{SIGINT} (e.g., by running a @code{SIGINT} trap or exiting itself);
3625
3626@item
3627If the pipeline does not terminate due to @code{SIGINT}, the program
3628handled the @code{SIGINT} itself and did not treat it as a fatal signal.
3629In that case, Bash does not treat @code{SIGINT} as a fatal signal,
3630either, instead assuming that the @code{SIGINT} was used as part of the
3631program's normal operation (e.g., @command{emacs} uses it to abort editing
3632commands) or deliberately discarded. However, Bash will run any
3633trap set on @code{SIGINT}, as it does with any other trapped signal it
3634receives while it is waiting for the foreground command to
3635complete, for compatibility.
3636@end enumerate
3637
ccc6cda3
JA
3638@node Shell Scripts
3639@section Shell Scripts
3640@cindex shell script
3641
3642A shell script is a text file containing shell commands. When such
3643a file is used as the first non-option argument when invoking Bash,
28ef6c31 3644and neither the @option{-c} nor @option{-s} option is supplied
ccc6cda3
JA
3645(@pxref{Invoking Bash}),
3646Bash reads and executes commands from the file, then exits. This
f73dda09
JA
3647mode of operation creates a non-interactive shell. The shell first
3648searches for the file in the current directory, and looks in the
3649directories in @env{$PATH} if not found there.
3650
3651When Bash runs
ccc6cda3
JA
3652a shell script, it sets the special parameter @code{0} to the name
3653of the file, rather than the name of the shell, and the positional
3654parameters are set to the remaining arguments, if any are given.
3655If no additional arguments are supplied, the positional parameters
3656are unset.
3657
3658A shell script may be made executable by using the @code{chmod} command
3659to turn on the execute bit. When Bash finds such a file while
74091dd4
CR
3660searching the @env{$PATH} for a command, it creates a
3661new instance of itself
3662to execute it.
3663In other words, executing
ccc6cda3
JA
3664@example
3665filename @var{arguments}
3666@end example
3667@noindent
3668is equivalent to executing
3669@example
3670bash filename @var{arguments}
3671@end example
3672
3673@noindent
3674if @code{filename} is an executable shell script.
3675This subshell reinitializes itself, so that the effect is as if a
cce855bc
JA
3676new shell had been invoked to interpret the script, with the
3677exception that the locations of commands remembered by the parent
3678(see the description of @code{hash} in @ref{Bourne Shell Builtins})
3679are retained by the child.
ccc6cda3 3680
bb70624e 3681Most versions of Unix make this a part of the operating system's command
ccc6cda3
JA
3682execution mechanism. If the first line of a script begins with
3683the two characters @samp{#!}, the remainder of the line specifies
8868edaf
CR
3684an interpreter for the program and, depending on the operating system, one
3685or more optional arguments for that interpreter.
bb70624e
JA
3686Thus, you can specify Bash, @code{awk}, Perl, or some other
3687interpreter and write the rest of the script file in that language.
3688
3689The arguments to the interpreter
8868edaf 3690consist of one or more optional arguments following the interpreter
ccc6cda3 3691name on the first line of the script file, followed by the name of
8868edaf
CR
3692the script file, followed by the rest of the arguments supplied to the
3693script.
3694The details of how the interpreter line is split into an interpreter name
3695and a set of arguments vary across systems.
3696Bash will perform this action on operating systems that do not handle it
3697themselves.
3698Note that some older versions of Unix limit the interpreter
3699name and a single argument to a maximum of 32 characters, so it's not
3700portable to assume that using more than one argument will work.
ccc6cda3 3701
bb70624e
JA
3702Bash scripts often begin with @code{#! /bin/bash} (assuming that
3703Bash has been installed in @file{/bin}), since this ensures that
3704Bash will be used to interpret the script, even if it is executed
8868edaf
CR
3705under another shell. It's a common idiom to use @code{env} to find
3706@code{bash} even if it's been installed in another directory:
3707@code{#!/usr/bin/env bash} will find the first occurrence of @code{bash}
3708in @env{$PATH}.
bb70624e
JA
3709
3710@node Shell Builtin Commands
3711@chapter Shell Builtin Commands
ccc6cda3
JA
3712
3713@menu
3714* Bourne Shell Builtins:: Builtin commands inherited from the Bourne
3715 Shell.
bb70624e 3716* Bash Builtins:: Table of builtins specific to Bash.
3185942a
JA
3717* Modifying Shell Behavior:: Builtins to modify shell attributes and
3718 optional behavior.
bb70624e 3719* Special Builtins:: Builtin commands classified specially by
0628567a 3720 POSIX.
ccc6cda3
JA
3721@end menu
3722
bb70624e
JA
3723Builtin commands are contained within the shell itself.
3724When the name of a builtin command is used as the first word of
3725a simple command (@pxref{Simple Commands}), the shell executes
3726the command directly, without invoking another program.
3727Builtin commands are necessary to implement functionality impossible
3728or inconvenient to obtain with separate utilities.
3729
0628567a 3730This section briefly describes the builtins which Bash inherits from
bb70624e
JA
3731the Bourne Shell, as well as the builtin commands which are unique
3732to or have been extended in Bash.
3733
a0c0a00f 3734Several builtin commands are described in other chapters: builtin
bb70624e
JA
3735commands which provide the Bash interface to the job control
3736facilities (@pxref{Job Control Builtins}), the directory stack
3737(@pxref{Directory Stack Builtins}), the command history
3738(@pxref{Bash History Builtins}), and the programmable completion
3739facilities (@pxref{Programmable Completion Builtins}).
3740
3741Many of the builtins have been extended by @sc{posix} or Bash.
ccc6cda3 3742
b80f6443
JA
3743Unless otherwise noted, each builtin command documented as accepting
3744options preceded by @samp{-} accepts @samp{--}
3745to signify the end of the options.
d233b485 3746The @code{:}, @code{true}, @code{false}, and @code{test}/@code{[}
0001803f 3747builtins do not accept options and do not treat @samp{--} specially.
a0c0a00f
CR
3748The @code{exit}, @code{logout}, @code{return},
3749@code{break}, @code{continue}, @code{let},
0001803f
CR
3750and @code{shift} builtins accept and process arguments beginning
3751with @samp{-} without requiring @samp{--}.
3752Other builtins that accept arguments but are not specified as accepting
3753options interpret arguments beginning with @samp{-} as invalid options and
3754require @samp{--} to prevent this interpretation.
b80f6443 3755
ccc6cda3
JA
3756@node Bourne Shell Builtins
3757@section Bourne Shell Builtins
3758
bb70624e 3759The following shell builtin commands are inherited from the Bourne Shell.
0628567a 3760These commands are implemented as specified by the @sc{posix} standard.
ccc6cda3
JA
3761
3762@table @code
bb70624e 3763@item : @r{(a colon)}
ccc6cda3
JA
3764@btindex :
3765@example
3766: [@var{arguments}]
3767@end example
ac50fbac 3768
ccc6cda3 3769Do nothing beyond expanding @var{arguments} and performing redirections.
cce855bc 3770The return status is zero.
ccc6cda3 3771
bb70624e 3772@item . @r{(a period)}
ccc6cda3
JA
3773@btindex .
3774@example
b72432fd 3775. @var{filename} [@var{arguments}]
ccc6cda3 3776@end example
ac50fbac 3777
ccc6cda3 3778Read and execute commands from the @var{filename} argument in the
cce855bc 3779current shell context. If @var{filename} does not contain a slash,
74091dd4
CR
3780the @env{PATH} variable is used to find @var{filename},
3781but @var{filename} does not need to be executable.
3782When Bash is not in @sc{posix} mode, it searches the current directory
28ef6c31 3783if @var{filename} is not found in @env{$PATH}.
b72432fd
JA
3784If any @var{arguments} are supplied, they become the positional
3785parameters when @var{filename} is executed. Otherwise the positional
3786parameters are unchanged.
74091dd4 3787If the @option{-T} option is enabled, @code{.} inherits any trap on
a0c0a00f 3788@code{DEBUG}; if it is not, any @code{DEBUG} trap string is saved and
74091dd4 3789restored around the call to @code{.}, and @code{.} unsets the
a0c0a00f
CR
3790@code{DEBUG} trap while it executes.
3791If @option{-T} is not set, and the sourced file changes
74091dd4 3792the @code{DEBUG} trap, the new value is retained when @code{.} completes.
cce855bc
JA
3793The return status is the exit status of the last command executed, or
3794zero if no commands are executed. If @var{filename} is not found, or
3795cannot be read, the return status is non-zero.
bb70624e 3796This builtin is equivalent to @code{source}.
ccc6cda3
JA
3797
3798@item break
3799@btindex break
3800@example
3801break [@var{n}]
3802@end example
ac50fbac 3803
ccc6cda3
JA
3804Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop.
3805If @var{n} is supplied, the @var{n}th enclosing loop is exited.
cce855bc
JA
3806@var{n} must be greater than or equal to 1.
3807The return status is zero unless @var{n} is not greater than or equal to 1.
ccc6cda3
JA
3808
3809@item cd
3810@btindex cd
3811@example
ac50fbac 3812cd [-L|[-P [-e]] [-@@] [@var{directory}]
ccc6cda3 3813@end example
ac50fbac 3814
b80f6443 3815Change the current working directory to @var{directory}.
ac50fbac
CR
3816If @var{directory} is not supplied, the value of the @env{HOME}
3817shell variable is used.
ac50fbac
CR
3818If the shell variable
3819@env{CDPATH} exists, it is used as a search path:
3820each directory name in @env{CDPATH} is searched for
3821@var{directory}, with alternative directory names in @env{CDPATH}
3822separated by a colon (@samp{:}).
b80f6443
JA
3823If @var{directory} begins with a slash, @env{CDPATH} is not used.
3824
ac50fbac
CR
3825The @option{-P} option means to not follow symbolic links: symbolic links
3826are resolved while @code{cd} is traversing @var{directory} and before
3827processing an instance of @samp{..} in @var{directory}.
3828
3829By default, or when the @option{-L} option is supplied, symbolic links
3830in @var{directory} are resolved after @code{cd} processes an instance
3831of @samp{..} in @var{directory}.
3832
3833If @samp{..} appears in @var{directory}, it is processed by removing the
3834immediately preceding pathname component, back to a slash or the beginning
3835of @var{directory}.
3836
495aee44
CR
3837If the @option{-e} option is supplied with @option{-P}
3838and the current working directory cannot be successfully determined
3839after a successful directory change, @code{cd} will return an unsuccessful
3840status.
ac50fbac
CR
3841
3842On systems that support it, the @option{-@@} option presents the extended
3843attributes associated with a file as a directory.
3844
3845If @var{directory} is @samp{-}, it is converted to @env{$OLDPWD}
3846before the directory change is attempted.
b80f6443
JA
3847
3848If a non-empty directory name from @env{CDPATH} is used, or if
3849@samp{-} is the first argument, and the directory change is
3850successful, the absolute pathname of the new working directory is
3851written to the standard output.
3852
74091dd4
CR
3853If the directory change is successful, @code{cd} sets the value of the
3854@env{PWD} environment variable to the new directory name, and sets the
3855@env{OLDPWD} environment variable to the value of the current working
3856directory before the change.
3857
cce855bc
JA
3858The return status is zero if the directory is successfully changed,
3859non-zero otherwise.
ccc6cda3
JA
3860
3861@item continue
3862@btindex continue
3863@example
3864continue [@var{n}]
3865@end example
ac50fbac 3866
ccc6cda3
JA
3867Resume the next iteration of an enclosing @code{for}, @code{while},
3868@code{until}, or @code{select} loop.
cce855bc
JA
3869If @var{n} is supplied, the execution of the @var{n}th enclosing loop
3870is resumed.
3871@var{n} must be greater than or equal to 1.
3872The return status is zero unless @var{n} is not greater than or equal to 1.
ccc6cda3
JA
3873
3874@item eval
3875@btindex eval
3876@example
3877eval [@var{arguments}]
3878@end example
ac50fbac 3879
cce855bc
JA
3880The arguments are concatenated together into a single command, which is
3881then read and executed, and its exit status returned as the exit status
3882of @code{eval}.
3883If there are no arguments or only empty arguments, the return status is
3884zero.
ccc6cda3
JA
3885
3886@item exec
3887@btindex exec
3888@example
cce855bc 3889exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]]
ccc6cda3 3890@end example
ac50fbac 3891
ccc6cda3 3892If @var{command}
cce855bc 3893is supplied, it replaces the shell without creating a new process.
28ef6c31 3894If the @option{-l} option is supplied, the shell places a dash at the
3185942a 3895beginning of the zeroth argument passed to @var{command}.
ccc6cda3 3896This is what the @code{login} program does.
28ef6c31 3897The @option{-c} option causes @var{command} to be executed with an empty
cce855bc 3898environment.
28ef6c31 3899If @option{-a} is supplied, the shell passes @var{name} as the zeroth
cce855bc 3900argument to @var{command}.
ac50fbac
CR
3901If @var{command}
3902cannot be executed for some reason, a non-interactive shell exits,
3903unless the @code{execfail} shell option
3904is enabled. In that case, it returns failure.
3905An interactive shell returns failure if the file cannot be executed.
d233b485 3906A subshell exits unconditionally if @code{exec} fails.
ccc6cda3 3907If no @var{command} is specified, redirections may be used to affect
cce855bc
JA
3908the current shell environment. If there are no redirection errors, the
3909return status is zero; otherwise the return status is non-zero.
ccc6cda3
JA
3910
3911@item exit
3912@btindex exit
3913@example
3914exit [@var{n}]
3915@end example
ac50fbac 3916
ccc6cda3 3917Exit the shell, returning a status of @var{n} to the shell's parent.
bb70624e 3918If @var{n} is omitted, the exit status is that of the last command executed.
cce855bc 3919Any trap on @code{EXIT} is executed before the shell terminates.
ccc6cda3
JA
3920
3921@item export
3922@btindex export
3923@example
3924export [-fn] [-p] [@var{name}[=@var{value}]]
3925@end example
ac50fbac 3926
ccc6cda3 3927Mark each @var{name} to be passed to child processes
28ef6c31 3928in the environment. If the @option{-f} option is supplied, the @var{name}s
cce855bc 3929refer to shell functions; otherwise the names refer to shell variables.
28ef6c31 3930The @option{-n} option means to no longer mark each @var{name} for export.
74091dd4 3931If no @var{name}s are supplied, or if the @option{-p} option is given, a
ac50fbac 3932list of names of all exported variables is displayed.
28ef6c31 3933The @option{-p} option displays output in a form that may be reused as input.
b80f6443
JA
3934If a variable name is followed by =@var{value}, the value of
3935the variable is set to @var{value}.
3936
cce855bc 3937The return status is zero unless an invalid option is supplied, one of
28ef6c31 3938the names is not a valid shell variable name, or @option{-f} is supplied
cce855bc 3939with a name that is not a shell function.
ccc6cda3
JA
3940
3941@item getopts
3942@btindex getopts
3943@example
8868edaf 3944getopts @var{optstring} @var{name} [@var{arg} @dots{}]
ccc6cda3 3945@end example
ac50fbac 3946
ccc6cda3 3947@code{getopts} is used by shell scripts to parse positional parameters.
bb70624e
JA
3948@var{optstring} contains the option characters to be recognized; if a
3949character is followed by a colon, the option is expected to have an
ac50fbac 3950argument, which should be separated from it by whitespace.
bb70624e
JA
3951The colon (@samp{:}) and question mark (@samp{?}) may not be
3952used as option characters.
ccc6cda3
JA
3953Each time it is invoked, @code{getopts}
3954places the next option in the shell variable @var{name}, initializing
3955@var{name} if it does not exist,
3956and the index of the next argument to be processed into the
28ef6c31
JA
3957variable @env{OPTIND}.
3958@env{OPTIND} is initialized to 1 each time the shell or a shell script
cce855bc
JA
3959is invoked.
3960When an option requires an argument,
28ef6c31
JA
3961@code{getopts} places that argument into the variable @env{OPTARG}.
3962The shell does not reset @env{OPTIND} automatically; it must be manually
cce855bc
JA
3963reset between multiple calls to @code{getopts} within the same shell
3964invocation if a new set of parameters is to be used.
3965
3966When the end of options is encountered, @code{getopts} exits with a
3967return value greater than zero.
28ef6c31 3968@env{OPTIND} is set to the index of the first non-option argument,
495aee44 3969and @var{name} is set to @samp{?}.
cce855bc
JA
3970
3971@code{getopts}
3972normally parses the positional parameters, but if more arguments are
8868edaf 3973supplied as @var{arg} values, @code{getopts} parses those instead.
ccc6cda3
JA
3974
3975@code{getopts} can report errors in two ways. If the first character of
3976@var{optstring} is a colon, @var{silent}
ac50fbac 3977error reporting is used. In normal operation, diagnostic messages
cce855bc 3978are printed when invalid options or missing option arguments are
ccc6cda3 3979encountered.
28ef6c31 3980If the variable @env{OPTERR}
cce855bc 3981is set to 0, no error messages will be displayed, even if the first
ccc6cda3
JA
3982character of @code{optstring} is not a colon.
3983
cce855bc 3984If an invalid option is seen,
ccc6cda3 3985@code{getopts} places @samp{?} into @var{name} and, if not silent,
28ef6c31 3986prints an error message and unsets @env{OPTARG}.
ccc6cda3 3987If @code{getopts} is silent, the option character found is placed in
28ef6c31 3988@env{OPTARG} and no diagnostic message is printed.
ccc6cda3
JA
3989
3990If a required argument is not found, and @code{getopts}
3991is not silent, a question mark (@samp{?}) is placed in @var{name},
3992@code{OPTARG} is unset, and a diagnostic message is printed.
3993If @code{getopts} is silent, then a colon (@samp{:}) is placed in
28ef6c31 3994@var{name} and @env{OPTARG} is set to the option character found.
ccc6cda3 3995
ccc6cda3
JA
3996@item hash
3997@btindex hash
3998@example
0628567a 3999hash [-r] [-p @var{filename}] [-dt] [@var{name}]
ccc6cda3 4000@end example
ac50fbac 4001
495aee44
CR
4002Each time @code{hash} is invoked, it remembers the full pathnames of the
4003commands specified as @var{name} arguments,
cce855bc
JA
4004so they need not be searched for on subsequent invocations.
4005The commands are found by searching through the directories listed in
28ef6c31 4006@env{$PATH}.
495aee44 4007Any previously-remembered pathname is discarded.
28ef6c31 4008The @option{-p} option inhibits the path search, and @var{filename} is
cce855bc 4009used as the location of @var{name}.
28ef6c31 4010The @option{-r} option causes the shell to forget all remembered locations.
7117c2d2
JA
4011The @option{-d} option causes the shell to forget the remembered location
4012of each @var{name}.
f73dda09
JA
4013If the @option{-t} option is supplied, the full pathname to which each
4014@var{name} corresponds is printed. If multiple @var{name} arguments are
d233b485 4015supplied with @option{-t}, the @var{name} is printed before the hashed
f73dda09 4016full pathname.
7117c2d2
JA
4017The @option{-l} option causes output to be displayed in a format
4018that may be reused as input.
4019If no arguments are given, or if only @option{-l} is supplied,
4020information about remembered commands is printed.
cce855bc
JA
4021The return status is zero unless a @var{name} is not found or an invalid
4022option is supplied.
ccc6cda3
JA
4023
4024@item pwd
4025@btindex pwd
4026@example
4027pwd [-LP]
4028@end example
ac50fbac 4029
bb70624e 4030Print the absolute pathname of the current working directory.
28ef6c31 4031If the @option{-P} option is supplied, the pathname printed will not
cce855bc 4032contain symbolic links.
28ef6c31 4033If the @option{-L} option is supplied, the pathname printed may contain
cce855bc
JA
4034symbolic links.
4035The return status is zero unless an error is encountered while
4036determining the name of the current directory or an invalid option
4037is supplied.
ccc6cda3
JA
4038
4039@item readonly
4040@btindex readonly
4041@example
495aee44 4042readonly [-aAf] [-p] [@var{name}[=@var{value}]] @dots{}
ccc6cda3 4043@end example
ac50fbac 4044
cce855bc
JA
4045Mark each @var{name} as readonly.
4046The values of these names may not be changed by subsequent assignment.
28ef6c31 4047If the @option{-f} option is supplied, each @var{name} refers to a shell
cce855bc 4048function.
3185942a
JA
4049The @option{-a} option means each @var{name} refers to an indexed
4050array variable; the @option{-A} option means each @var{name} refers
4051to an associative array variable.
495aee44 4052If both options are supplied, @option{-A} takes precedence.
28ef6c31 4053If no @var{name} arguments are given, or if the @option{-p}
ccc6cda3 4054option is supplied, a list of all readonly names is printed.
495aee44
CR
4055The other options may be used to restrict the output to a subset of
4056the set of readonly names.
28ef6c31 4057The @option{-p} option causes output to be displayed in a format that
cce855bc 4058may be reused as input.
b80f6443
JA
4059If a variable name is followed by =@var{value}, the value of
4060the variable is set to @var{value}.
cce855bc
JA
4061The return status is zero unless an invalid option is supplied, one of
4062the @var{name} arguments is not a valid shell variable or function name,
28ef6c31 4063or the @option{-f} option is supplied with a name that is not a shell function.
ccc6cda3
JA
4064
4065@item return
4066@btindex return
4067@example
4068return [@var{n}]
4069@end example
ac50fbac
CR
4070
4071Cause a shell function to stop executing and return the value @var{n}
4072to its caller.
bb70624e
JA
4073If @var{n} is not supplied, the return value is the exit status of the
4074last command executed in the function.
a0c0a00f
CR
4075If @code{return} is executed by a trap handler, the last command used to
4076determine the status is the last command executed before the trap handler.
d233b485 4077If @code{return} is executed during a @code{DEBUG} trap, the last command
a0c0a00f
CR
4078used to determine the status is the last command executed by the trap
4079handler before @code{return} was invoked.
ac50fbac
CR
4080@code{return} may also be used to terminate execution of a script
4081being executed with the @code{.} (@code{source}) builtin,
4082returning either @var{n} or
bb70624e
JA
4083the exit status of the last command executed within the script as the exit
4084status of the script.
ac50fbac
CR
4085If @var{n} is supplied, the return value is its least significant
40868 bits.
b80f6443
JA
4087Any command associated with the @code{RETURN} trap is executed
4088before execution resumes after the function or script.
ac50fbac
CR
4089The return status is non-zero if @code{return} is supplied a non-numeric
4090argument or is used outside a function
bb70624e 4091and not during the execution of a script by @code{.} or @code{source}.
ccc6cda3
JA
4092
4093@item shift
4094@btindex shift
4095@example
4096shift [@var{n}]
4097@end example
ac50fbac 4098
cce855bc
JA
4099Shift the positional parameters to the left by @var{n}.
4100The positional parameters from @var{n}+1 @dots{} @code{$#} are
3185942a 4101renamed to @code{$1} @dots{} @code{$#}-@var{n}.
8868edaf 4102Parameters represented by the numbers @code{$#} down to @code{$#}-@var{n}+1
3185942a 4103are unset.
cce855bc
JA
4104@var{n} must be a non-negative number less than or equal to @code{$#}.
4105If @var{n} is zero or greater than @code{$#}, the positional parameters
4106are not changed.
bb70624e 4107If @var{n} is not supplied, it is assumed to be 1.
cce855bc
JA
4108The return status is zero unless @var{n} is greater than @code{$#} or
4109less than zero, non-zero otherwise.
ccc6cda3
JA
4110
4111@item test
4112@itemx [
4113@btindex test
4114@btindex [
ac50fbac
CR
4115@example
4116test @var{expr}
4117@end example
4118
a0c0a00f 4119Evaluate a conditional expression @var{expr} and return a status of 0
ac50fbac 4120(true) or 1 (false).
cce855bc
JA
4121Each operator and operand must be a separate argument.
4122Expressions are composed of the primaries described below in
4123@ref{Bash Conditional Expressions}.
95732b49
JA
4124@code{test} does not accept any options, nor does it accept and ignore
4125an argument of @option{--} as signifying the end of options.
cce855bc 4126
bb70624e
JA
4127When the @code{[} form is used, the last argument to the command must
4128be a @code{]}.
4129
cce855bc
JA
4130Expressions may be combined using the following operators, listed in
4131decreasing order of precedence.
3185942a 4132The evaluation depends on the number of arguments; see below.
495aee44 4133Operator precedence is used when there are five or more arguments.
cce855bc
JA
4134
4135@table @code
4136@item ! @var{expr}
4137True if @var{expr} is false.
4138
4139@item ( @var{expr} )
4140Returns the value of @var{expr}.
4141This may be used to override the normal precedence of operators.
4142
4143@item @var{expr1} -a @var{expr2}
4144True if both @var{expr1} and @var{expr2} are true.
4145
4146@item @var{expr1} -o @var{expr2}
4147True if either @var{expr1} or @var{expr2} is true.
4148@end table
4149
4150The @code{test} and @code{[} builtins evaluate conditional
4151expressions using a set of rules based on the number of arguments.
4152
4153@table @asis
4154@item 0 arguments
4155The expression is false.
4156
4157@item 1 argument
d233b485 4158The expression is true if, and only if, the argument is not null.
cce855bc
JA
4159
4160@item 2 arguments
4161If the first argument is @samp{!}, the expression is true if and
4162only if the second argument is null.
4163If the first argument is one of the unary conditional operators
4164(@pxref{Bash Conditional Expressions}), the expression
4165is true if the unary test is true.
4166If the first argument is not a valid unary operator, the expression is
4167false.
4168
4169@item 3 arguments
495aee44 4170The following conditions are applied in the order listed.
d233b485
CR
4171
4172@enumerate
4173@item
cce855bc
JA
4174If the second argument is one of the binary conditional
4175operators (@pxref{Bash Conditional Expressions}), the
4176result of the expression is the result of the binary test using the
4177first and third arguments as operands.
3185942a
JA
4178The @samp{-a} and @samp{-o} operators are considered binary operators
4179when there are three arguments.
d233b485 4180@item
cce855bc
JA
4181If the first argument is @samp{!}, the value is the negation of
4182the two-argument test using the second and third arguments.
d233b485 4183@item
cce855bc
JA
4184If the first argument is exactly @samp{(} and the third argument is
4185exactly @samp{)}, the result is the one-argument test of the second
4186argument.
d233b485 4187@item
cce855bc 4188Otherwise, the expression is false.
d233b485 4189@end enumerate
cce855bc
JA
4190
4191@item 4 arguments
74091dd4
CR
4192The following conditions are applied in the order listed.
4193
4194@enumerate
4195@item
cce855bc
JA
4196If the first argument is @samp{!}, the result is the negation of
4197the three-argument expression composed of the remaining arguments.
74091dd4
CR
4198@item
4199If the first argument is exactly @samp{(} and the fourth argument is
4200exactly @samp{)}, the result is the two-argument test of the second
4201and third arguments.
4202@item
a0c0a00f 4203Otherwise, the expression is parsed and evaluated according to
cce855bc 4204precedence using the rules listed above.
74091dd4 4205@end enumerate
cce855bc
JA
4206
4207@item 5 or more arguments
4208The expression is parsed and evaluated according to precedence
4209using the rules listed above.
4210@end table
ccc6cda3 4211
495aee44
CR
4212When used with @code{test} or @samp{[}, the @samp{<} and @samp{>}
4213operators sort lexicographically using ASCII ordering.
4214
ccc6cda3
JA
4215@item times
4216@btindex times
4217@example
4218times
4219@end example
ac50fbac 4220
ccc6cda3 4221Print out the user and system times used by the shell and its children.
cce855bc 4222The return status is zero.
ccc6cda3
JA
4223
4224@item trap
4225@btindex trap
4226@example
d166f048 4227trap [-lp] [@var{arg}] [@var{sigspec} @dots{}]
ccc6cda3 4228@end example
ac50fbac 4229
ccc6cda3 4230The commands in @var{arg} are to be read and executed when the
b80f6443
JA
4231shell receives signal @var{sigspec}. If @var{arg} is absent (and
4232there is a single @var{sigspec}) or
4233equal to @samp{-}, each specified signal's disposition is reset
4234to the value it had when the shell was started.
d166f048
JA
4235If @var{arg} is the null string, then the signal specified by
4236each @var{sigspec} is ignored by the shell and commands it invokes.
28ef6c31 4237If @var{arg} is not present and @option{-p} has been supplied,
bb70624e
JA
4238the shell displays the trap commands associated with each @var{sigspec}.
4239If no arguments are supplied, or
28ef6c31 4240only @option{-p} is given, @code{trap} prints the list of commands
cce855bc
JA
4241associated with each signal number in a form that may be reused as
4242shell input.
b80f6443
JA
4243The @option{-l} option causes the shell to print a list of signal names
4244and their corresponding numbers.
4245Each @var{sigspec} is either a signal name or a signal number.
4246Signal names are case insensitive and the @code{SIG} prefix is optional.
0001803f 4247
d166f048
JA
4248If a @var{sigspec}
4249is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits.
4250If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed
b80f6443
JA
4251before every simple command, @code{for} command, @code{case} command,
4252@code{select} command, every arithmetic @code{for} command, and before
4253the first command executes in a shell function.
0001803f 4254Refer to the description of the @code{extdebug} option to the
3185942a 4255@code{shopt} builtin (@pxref{The Shopt Builtin}) for details of its
b80f6443 4256effect on the @code{DEBUG} trap.
0001803f
CR
4257If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed
4258each time a shell function or a script executed with the @code{.} or
4259@code{source} builtins finishes executing.
4260
f73dda09 4261If a @var{sigspec} is @code{ERR}, the command @var{arg}
ac50fbac
CR
4262is executed whenever
4263a pipeline (which may consist of a single simple
a0c0a00f 4264command), a list, or a compound command returns a
ac50fbac 4265non-zero exit status,
b80f6443
JA
4266subject to the following conditions.
4267The @code{ERR} trap is not executed if the failed command is part of the
4268command list immediately following an @code{until} or @code{while} keyword,
17345e5a 4269part of the test following the @code{if} or @code{elif} reserved words,
ac50fbac
CR
4270part of a command executed in a @code{&&} or @code{||} list
4271except the command following the final @code{&&} or @code{||},
4272any command in a pipeline but the last,
3185942a 4273or if the command's return
f73dda09 4274status is being inverted using @code{!}.
ac50fbac
CR
4275These are the same conditions obeyed by the @code{errexit} (@option{-e})
4276option.
ccc6cda3
JA
4277
4278Signals ignored upon entry to the shell cannot be trapped or reset.
0628567a 4279Trapped signals that are not being ignored are reset to their original
0001803f 4280values in a subshell or subshell environment when one is created.
ccc6cda3 4281
cce855bc
JA
4282The return status is zero unless a @var{sigspec} does not specify a
4283valid signal.
4284
ccc6cda3
JA
4285@item umask
4286@btindex umask
4287@example
cce855bc 4288umask [-p] [-S] [@var{mode}]
ccc6cda3 4289@end example
ac50fbac 4290
ccc6cda3
JA
4291Set the shell process's file creation mask to @var{mode}. If
4292@var{mode} begins with a digit, it is interpreted as an octal number;
4293if not, it is interpreted as a symbolic mode mask similar
4294to that accepted by the @code{chmod} command. If @var{mode} is
28ef6c31 4295omitted, the current value of the mask is printed. If the @option{-S}
ccc6cda3
JA
4296option is supplied without a @var{mode} argument, the mask is printed
4297in a symbolic format.
28ef6c31 4298If the @option{-p} option is supplied, and @var{mode}
cce855bc
JA
4299is omitted, the output is in a form that may be reused as input.
4300The return status is zero if the mode is successfully changed or if
4301no @var{mode} argument is supplied, and non-zero otherwise.
ccc6cda3 4302
bb70624e
JA
4303Note that when the mode is interpreted as an octal number, each number
4304of the umask is subtracted from @code{7}. Thus, a umask of @code{022}
4305results in permissions of @code{755}.
4306
ccc6cda3
JA
4307@item unset
4308@btindex unset
4309@example
ac50fbac 4310unset [-fnv] [@var{name}]
ccc6cda3 4311@end example
ac50fbac
CR
4312
4313Remove each variable or function @var{name}.
4314If the @option{-v} option is given, each
a0c0a00f 4315@var{name} refers to a shell variable and that variable is removed.
28ef6c31 4316If the @option{-f} option is given, the @var{name}s refer to shell
ccc6cda3 4317functions, and the function definition is removed.
ac50fbac 4318If the @option{-n} option is supplied, and @var{name} is a variable with
74091dd4 4319the @code{nameref} attribute, @var{name} will be unset rather than the
ac50fbac
CR
4320variable it references.
4321@option{-n} has no effect if the @option{-f} option is supplied.
4322If no options are supplied, each @var{name} refers to a variable; if
8868edaf 4323there is no variable by that name, a function with that name, if any, is
ac50fbac 4324unset.
cce855bc 4325Readonly variables and functions may not be unset.
8868edaf
CR
4326Some shell variables lose their special behavior if they are unset; such
4327behavior is noted in the description of the individual variables.
74091dd4 4328The return status is zero unless a @var{name} is readonly or may not be unset.
ccc6cda3
JA
4329@end table
4330
bb70624e
JA
4331@node Bash Builtins
4332@section Bash Builtin Commands
ccc6cda3 4333
bb70624e
JA
4334This section describes builtin commands which are unique to
4335or have been extended in Bash.
0628567a 4336Some of these commands are specified in the @sc{posix} standard.
ccc6cda3 4337
bb70624e 4338@table @code
ccc6cda3 4339
bb70624e
JA
4340@item alias
4341@btindex alias
4342@example
ac50fbac 4343alias [-p] [@var{name}[=@var{value}] @dots{}]
bb70624e 4344@end example
ccc6cda3 4345
28ef6c31 4346Without arguments or with the @option{-p} option, @code{alias} prints
bb70624e
JA
4347the list of aliases on the standard output in a form that allows
4348them to be reused as input.
4349If arguments are supplied, an alias is defined for each @var{name}
4350whose @var{value} is given. If no @var{value} is given, the name
4351and value of the alias is printed.
4352Aliases are described in @ref{Aliases}.
ccc6cda3 4353
bb70624e
JA
4354@item bind
4355@btindex bind
4356@example
ac50fbac 4357bind [-m @var{keymap}] [-lpsvPSVX]
bb70624e
JA
4358bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}]
4359bind [-m @var{keymap}] -f @var{filename}
4360bind [-m @var{keymap}] -x @var{keyseq:shell-command}
4361bind [-m @var{keymap}] @var{keyseq:function-name}
a0c0a00f 4362bind [-m @var{keymap}] @var{keyseq:readline-command}
74091dd4 4363bind @var{readline-command-line}
bb70624e 4364@end example
ccc6cda3 4365
bb70624e 4366Display current Readline (@pxref{Command Line Editing})
7117c2d2
JA
4367key and function bindings,
4368bind a key sequence to a Readline function or macro,
4369or set a Readline variable.
4370Each non-option argument is a command as it would appear in a
3185942a 4371Readline initialization file (@pxref{Readline Init File}),
7117c2d2 4372but each binding or command must be passed as a separate argument; e.g.,
bb70624e 4373@samp{"\C-x\C-r":re-read-init-file}.
3185942a 4374
bb70624e 4375Options, if supplied, have the following meanings:
cce855bc 4376
bb70624e
JA
4377@table @code
4378@item -m @var{keymap}
4379Use @var{keymap} as the keymap to be affected by
4380the subsequent bindings. Acceptable @var{keymap}
4381names are
4382@code{emacs},
4383@code{emacs-standard},
4384@code{emacs-meta},
4385@code{emacs-ctlx},
4386@code{vi},
28ef6c31 4387@code{vi-move},
bb70624e
JA
4388@code{vi-command}, and
4389@code{vi-insert}.
a0c0a00f
CR
4390@code{vi} is equivalent to @code{vi-command} (@code{vi-move} is also a
4391synonym); @code{emacs} is equivalent to @code{emacs-standard}.
cce855bc 4392
bb70624e
JA
4393@item -l
4394List the names of all Readline functions.
cce855bc 4395
bb70624e
JA
4396@item -p
4397Display Readline function names and bindings in such a way that they
4398can be used as input or in a Readline initialization file.
cce855bc 4399
bb70624e
JA
4400@item -P
4401List current Readline function names and bindings.
ccc6cda3 4402
bb70624e
JA
4403@item -v
4404Display Readline variable names and values in such a way that they
4405can be used as input or in a Readline initialization file.
ccc6cda3 4406
bb70624e
JA
4407@item -V
4408List current Readline variable names and values.
ccc6cda3 4409
bb70624e
JA
4410@item -s
4411Display Readline key sequences bound to macros and the strings they output
4412in such a way that they can be used as input or in a Readline
4413initialization file.
ccc6cda3 4414
bb70624e
JA
4415@item -S
4416Display Readline key sequences bound to macros and the strings they output.
ccc6cda3 4417
bb70624e
JA
4418@item -f @var{filename}
4419Read key bindings from @var{filename}.
ccc6cda3 4420
bb70624e
JA
4421@item -q @var{function}
4422Query about which keys invoke the named @var{function}.
ccc6cda3 4423
bb70624e
JA
4424@item -u @var{function}
4425Unbind all keys bound to the named @var{function}.
cce855bc 4426
bb70624e
JA
4427@item -r @var{keyseq}
4428Remove any current binding for @var{keyseq}.
ccc6cda3 4429
bb70624e
JA
4430@item -x @var{keyseq:shell-command}
4431Cause @var{shell-command} to be executed whenever @var{keyseq} is
4432entered.
3185942a
JA
4433When @var{shell-command} is executed, the shell sets the
4434@code{READLINE_LINE} variable to the contents of the Readline line
8868edaf
CR
4435buffer and the @code{READLINE_POINT} and @code{READLINE_MARK} variables
4436to the current location of the insertion point and the saved insertion
4437point (the @var{mark}), respectively.
74091dd4
CR
4438The shell assigns any numeric argument the user supplied to the
4439@code{READLINE_ARGUMENT} variable.
4440If there was no argument, that variable is not set.
8868edaf
CR
4441If the executed command changes the value of any of @code{READLINE_LINE},
4442@code{READLINE_POINT}, or @code{READLINE_MARK}, those new values will be
4443reflected in the editing state.
ac50fbac
CR
4444
4445@item -X
4446List all key sequences bound to shell commands and the associated commands
4447in a format that can be reused as input.
bb70624e 4448@end table
ccc6cda3 4449
bb70624e
JA
4450@noindent
4451The return status is zero unless an invalid option is supplied or an
4452error occurs.
ccc6cda3 4453
bb70624e
JA
4454@item builtin
4455@btindex builtin
4456@example
4457builtin [@var{shell-builtin} [@var{args}]]
4458@end example
ac50fbac 4459
bb70624e
JA
4460Run a shell builtin, passing it @var{args}, and return its exit status.
4461This is useful when defining a shell function with the same
4462name as a shell builtin, retaining the functionality of the builtin within
4463the function.
4464The return status is non-zero if @var{shell-builtin} is not a shell
4465builtin command.
ccc6cda3 4466
b80f6443
JA
4467@item caller
4468@btindex caller
4469@example
4470caller [@var{expr}]
4471@end example
ac50fbac 4472
b80f6443
JA
4473Returns the context of any active subroutine call (a shell function or
4474a script executed with the @code{.} or @code{source} builtins).
4475
4476Without @var{expr}, @code{caller} displays the line number and source
4477filename of the current subroutine call.
a0c0a00f 4478If a non-negative integer is supplied as @var{expr}, @code{caller}
b80f6443
JA
4479displays the line number, subroutine name, and source file corresponding
4480to that position in the current execution call stack. This extra
4481information may be used, for example, to print a stack trace. The
4482current frame is frame 0.
4483
4484The return value is 0 unless the shell is not executing a subroutine
4485call or @var{expr} does not correspond to a valid position in the
4486call stack.
4487
bb70624e
JA
4488@item command
4489@btindex command
4490@example
4491command [-pVv] @var{command} [@var{arguments} @dots{}]
4492@end example
ac50fbac 4493
bb70624e
JA
4494Runs @var{command} with @var{arguments} ignoring any shell function
4495named @var{command}.
4496Only shell builtin commands or commands found by searching the
28ef6c31 4497@env{PATH} are executed.
bb70624e
JA
4498If there is a shell function named @code{ls}, running @samp{command ls}
4499within the function will execute the external command @code{ls}
4500instead of calling the function recursively.
28ef6c31 4501The @option{-p} option means to use a default value for @env{PATH}
bb70624e
JA
4502that is guaranteed to find all of the standard utilities.
4503The return status in this case is 127 if @var{command} cannot be
4504found or an error occurred, and the exit status of @var{command}
4505otherwise.
cce855bc 4506
28ef6c31
JA
4507If either the @option{-V} or @option{-v} option is supplied, a
4508description of @var{command} is printed. The @option{-v} option
bb70624e 4509causes a single word indicating the command or file name used to
28ef6c31 4510invoke @var{command} to be displayed; the @option{-V} option produces
bb70624e
JA
4511a more verbose description. In this case, the return status is
4512zero if @var{command} is found, and non-zero if not.
cce855bc 4513
bb70624e
JA
4514@item declare
4515@btindex declare
4516@example
8868edaf 4517declare [-aAfFgiIlnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
bb70624e 4518@end example
ccc6cda3 4519
bb70624e
JA
4520Declare variables and give them attributes. If no @var{name}s
4521are given, then display the values of variables instead.
ccc6cda3 4522
28ef6c31 4523The @option{-p} option will display the attributes and values of each
b80f6443 4524@var{name}.
ac50fbac
CR
4525When @option{-p} is used with @var{name} arguments, additional options,
4526other than @option{-f} and @option{-F}, are ignored.
3185942a
JA
4527
4528When @option{-p} is supplied without @var{name} arguments, @code{declare}
4529will display the attributes and values of all variables having the
4530attributes specified by the additional options.
4531If no other options are supplied with @option{-p}, @code{declare} will
4532display the attributes and values of all shell variables. The @option{-f}
4533option will restrict the display to shell functions.
4534
28ef6c31 4535The @option{-F} option inhibits the display of function definitions;
b80f6443
JA
4536only the function name and attributes are printed.
4537If the @code{extdebug} shell option is enabled using @code{shopt}
3185942a 4538(@pxref{The Shopt Builtin}), the source file name and line number where
a0c0a00f 4539each @var{name} is defined are displayed as well.
b80f6443 4540@option{-F} implies @option{-f}.
495aee44
CR
4541
4542The @option{-g} option forces variables to be created or modified at
4543the global scope, even when @code{declare} is executed in a shell function.
4544It is ignored in all other cases.
4545
8868edaf 4546The @option{-I} option causes local variables to inherit the attributes
74091dd4 4547(except the @code{nameref} attribute)
8868edaf
CR
4548and value of any existing variable with the same
4549@var{name} at a surrounding scope.
4550If there is no existing variable, the local variable is initially unset.
4551
b80f6443
JA
4552The following options can be used to restrict output to variables with
4553the specified attributes or to give variables attributes:
cce855bc 4554
bb70624e
JA
4555@table @code
4556@item -a
3185942a
JA
4557Each @var{name} is an indexed array variable (@pxref{Arrays}).
4558
4559@item -A
4560Each @var{name} is an associative array variable (@pxref{Arrays}).
cce855bc 4561
bb70624e
JA
4562@item -f
4563Use function names only.
ccc6cda3 4564
bb70624e
JA
4565@item -i
4566The variable is to be treated as
4567an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is
4568performed when the variable is assigned a value.
ccc6cda3 4569
3185942a
JA
4570@item -l
4571When the variable is assigned a value, all upper-case characters are
4572converted to lower-case.
4573The upper-case attribute is disabled.
4574
ac50fbac 4575@item -n
74091dd4 4576Give each @var{name} the @code{nameref} attribute, making
ac50fbac
CR
4577it a name reference to another variable.
4578That other variable is defined by the value of @var{name}.
a0c0a00f
CR
4579All references, assignments, and attribute modifications
4580to @var{name}, except for those using or changing the
ac50fbac
CR
4581@option{-n} attribute itself, are performed on the variable referenced by
4582@var{name}'s value.
a0c0a00f 4583The nameref attribute cannot be applied to array variables.
ac50fbac 4584
bb70624e
JA
4585@item -r
4586Make @var{name}s readonly. These names cannot then be assigned values
4587by subsequent assignment statements or unset.
ccc6cda3 4588
7117c2d2
JA
4589@item -t
4590Give each @var{name} the @code{trace} attribute.
95732b49
JA
4591Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from
4592the calling shell.
7117c2d2
JA
4593The trace attribute has no special meaning for variables.
4594
3185942a
JA
4595@item -u
4596When the variable is assigned a value, all lower-case characters are
4597converted to upper-case.
4598The lower-case attribute is disabled.
4599
bb70624e
JA
4600@item -x
4601Mark each @var{name} for export to subsequent commands via
4602the environment.
4603@end table
ccc6cda3 4604
3185942a 4605Using @samp{+} instead of @samp{-} turns off the attribute instead,
d233b485
CR
4606with the exceptions that @samp{+a} and @samp{+A}
4607may not be used to destroy array variables and @samp{+r} will not
3185942a 4608remove the readonly attribute.
bb70624e 4609When used in a function, @code{declare} makes each @var{name} local,
ac50fbac 4610as with the @code{local} command, unless the @option{-g} option is used.
495aee44
CR
4611If a variable name is followed by =@var{value}, the value of the variable
4612is set to @var{value}.
ccc6cda3 4613
ac50fbac
CR
4614When using @option{-a} or @option{-A} and the compound assignment syntax to
4615create array variables, additional attributes do not take effect until
4616subsequent assignments.
4617
bb70624e
JA
4618The return status is zero unless an invalid option is encountered,
4619an attempt is made to define a function using @samp{-f foo=bar},
4620an attempt is made to assign a value to a readonly variable,
4621an attempt is made to assign a value to an array variable without
4622using the compound assignment syntax (@pxref{Arrays}),
74091dd4 4623one of the @var{name}s is not a valid shell variable name,
bb70624e
JA
4624an attempt is made to turn off readonly status for a readonly variable,
4625an attempt is made to turn off array status for an array variable,
28ef6c31 4626or an attempt is made to display a non-existent function with @option{-f}.
ccc6cda3 4627
bb70624e
JA
4628@item echo
4629@btindex echo
4630@example
4631echo [-neE] [@var{arg} @dots{}]
4632@end example
ac50fbac 4633
bb70624e
JA
4634Output the @var{arg}s, separated by spaces, terminated with a
4635newline.
ac50fbac 4636The return status is 0 unless a write error occurs.
28ef6c31
JA
4637If @option{-n} is specified, the trailing newline is suppressed.
4638If the @option{-e} option is given, interpretation of the following
bb70624e 4639backslash-escaped characters is enabled.
28ef6c31 4640The @option{-E} option disables the interpretation of these escape characters,
bb70624e
JA
4641even on systems where they are interpreted by default.
4642The @code{xpg_echo} shell option may be used to
4643dynamically determine whether or not @code{echo} expands these
4644escape characters by default.
95732b49
JA
4645@code{echo} does not interpret @option{--} to mean the end of options.
4646
bb70624e
JA
4647@code{echo} interprets the following escape sequences:
4648@table @code
4649@item \a
4650alert (bell)
4651@item \b
4652backspace
4653@item \c
3185942a 4654suppress further output
bb70624e 4655@item \e
495aee44 4656@itemx \E
bb70624e
JA
4657escape
4658@item \f
4659form feed
4660@item \n
4661new line
4662@item \r
4663carriage return
4664@item \t
4665horizontal tab
4666@item \v
4667vertical tab
4668@item \\
4669backslash
7117c2d2
JA
4670@item \0@var{nnn}
4671the eight-bit character whose value is the octal value @var{nnn}
4672(zero to three octal digits)
f73dda09
JA
4673@item \x@var{HH}
4674the eight-bit character whose value is the hexadecimal value @var{HH}
4675(one or two hex digits)
495aee44
CR
4676@item \u@var{HHHH}
4677the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4678@var{HHHH} (one to four hex digits)
4679@item \U@var{HHHHHHHH}
4680the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value
4681@var{HHHHHHHH} (one to eight hex digits)
bb70624e 4682@end table
ccc6cda3 4683
bb70624e
JA
4684@item enable
4685@btindex enable
4686@example
3185942a 4687enable [-a] [-dnps] [-f @var{filename}] [@var{name} @dots{}]
bb70624e 4688@end example
ac50fbac 4689
bb70624e
JA
4690Enable and disable builtin shell commands.
4691Disabling a builtin allows a disk command which has the same name
4692as a shell builtin to be executed without specifying a full pathname,
4693even though the shell normally searches for builtins before disk commands.
28ef6c31 4694If @option{-n} is used, the @var{name}s become disabled. Otherwise
bb70624e 4695@var{name}s are enabled. For example, to use the @code{test} binary
28ef6c31 4696found via @env{$PATH} instead of the shell builtin version, type
bb70624e 4697@samp{enable -n test}.
ccc6cda3 4698
28ef6c31 4699If the @option{-p} option is supplied, or no @var{name} arguments appear,
bb70624e
JA
4700a list of shell builtins is printed. With no other arguments, the list
4701consists of all enabled shell builtins.
28ef6c31 4702The @option{-a} option means to list
bb70624e 4703each builtin with an indication of whether or not it is enabled.
ccc6cda3 4704
28ef6c31 4705The @option{-f} option means to load the new builtin command @var{name}
bb70624e 4706from shared object @var{filename}, on systems that support dynamic loading.
74091dd4
CR
4707Bash will use the value of the @env{BASH_LOADABLES_PATH} variable as a
4708colon-separated list of directories in which to search for @var{filename}.
4709The default is system-dependent.
28ef6c31 4710The @option{-d} option will delete a builtin loaded with @option{-f}.
ccc6cda3 4711
bb70624e 4712If there are no options, a list of the shell builtins is displayed.
28ef6c31
JA
4713The @option{-s} option restricts @code{enable} to the @sc{posix} special
4714builtins. If @option{-s} is used with @option{-f}, the new builtin becomes
bb70624e 4715a special builtin (@pxref{Special Builtins}).
ccc6cda3 4716
74091dd4
CR
4717If no options are supplied and a @var{name} is not a shell builtin,
4718@code{enable} will attempt to load @var{name} from a shared object named
4719@var{name}, as if the command were
4720@samp{enable -f @var{name} @var{name}}.
4721
bb70624e
JA
4722The return status is zero unless a @var{name} is not a shell builtin
4723or there is an error loading a new builtin from a shared object.
cce855bc 4724
bb70624e
JA
4725@item help
4726@btindex help
4727@example
3185942a 4728help [-dms] [@var{pattern}]
bb70624e 4729@end example
ac50fbac 4730
bb70624e
JA
4731Display helpful information about builtin commands.
4732If @var{pattern} is specified, @code{help} gives detailed help
4733on all commands matching @var{pattern}, otherwise a list of
4734the builtins is printed.
3185942a
JA
4735
4736Options, if supplied, have the following meanings:
4737
4738@table @code
4739@item -d
4740Display a short description of each @var{pattern}
4741@item -m
4742Display the description of each @var{pattern} in a manpage-like format
4743@item -s
4744Display only a short usage synopsis for each @var{pattern}
4745@end table
4746
bb70624e 4747The return status is zero unless no command matches @var{pattern}.
ccc6cda3 4748
bb70624e
JA
4749@item let
4750@btindex let
4751@example
ac50fbac 4752let @var{expression} [@var{expression} @dots{}]
bb70624e 4753@end example
ac50fbac 4754
bb70624e
JA
4755The @code{let} builtin allows arithmetic to be performed on shell
4756variables. Each @var{expression} is evaluated according to the
4757rules given below in @ref{Shell Arithmetic}. If the
4758last @var{expression} evaluates to 0, @code{let} returns 1;
4759otherwise 0 is returned.
ccc6cda3 4760
bb70624e
JA
4761@item local
4762@btindex local
4763@example
b80f6443 4764local [@var{option}] @var{name}[=@var{value}] @dots{}
bb70624e 4765@end example
ac50fbac 4766
bb70624e
JA
4767For each argument, a local variable named @var{name} is created,
4768and assigned @var{value}.
4769The @var{option} can be any of the options accepted by @code{declare}.
4770@code{local} can only be used within a function; it makes the variable
4771@var{name} have a visible scope restricted to that function and its
a0c0a00f
CR
4772children.
4773If @var{name} is @samp{-}, the set of shell options is made local to the
4774function in which @code{local} is invoked: shell options changed using
4775the @code{set} builtin inside the function are restored to their original
4776values when the function returns.
8868edaf
CR
4777The restore is effected as if a series of @code{set} commands were executed
4778to restore the values that were in place before the function.
a0c0a00f 4779The return status is zero unless @code{local} is used outside
bb70624e
JA
4780a function, an invalid @var{name} is supplied, or @var{name} is a
4781readonly variable.
ccc6cda3 4782
bb70624e
JA
4783@item logout
4784@btindex logout
4785@example
4786logout [@var{n}]
4787@end example
ac50fbac 4788
bb70624e
JA
4789Exit a login shell, returning a status of @var{n} to the shell's
4790parent.
cce855bc 4791
3185942a
JA
4792@item mapfile
4793@btindex mapfile
4794@example
d233b485
CR
4795mapfile [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
4796 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
3185942a 4797@end example
ac50fbac 4798
0001803f 4799Read lines from the standard input into the indexed array variable @var{array},
3185942a
JA
4800or from file descriptor @var{fd}
4801if the @option{-u} option is supplied.
4802The variable @code{MAPFILE} is the default @var{array}.
4803Options, if supplied, have the following meanings:
ac50fbac 4804
3185942a
JA
4805@table @code
4806
a0c0a00f
CR
4807@item -d
4808The first character of @var{delim} is used to terminate each input line,
4809rather than newline.
d233b485
CR
4810If @var{delim} is the empty string, @code{mapfile} will terminate a line
4811when it reads a NUL character.
3185942a
JA
4812@item -n
4813Copy at most @var{count} lines. If @var{count} is 0, all lines are copied.
4814@item -O
4815Begin assigning to @var{array} at index @var{origin}.
4816The default index is 0.
4817@item -s
4818Discard the first @var{count} lines read.
4819@item -t
a0c0a00f 4820Remove a trailing @var{delim} (default newline) from each line read.
3185942a
JA
4821@item -u
4822Read lines from file descriptor @var{fd} instead of the standard input.
4823@item -C
d233b485 4824Evaluate @var{callback} each time @var{quantum} lines are read.
3185942a
JA
4825The @option{-c} option specifies @var{quantum}.
4826@item -c
4827Specify the number of lines read between each call to @var{callback}.
4828@end table
4829
a0c0a00f 4830If @option{-C} is specified without @option{-c},
3185942a 4831the default quantum is 5000.
17345e5a 4832When @var{callback} is evaluated, it is supplied the index of the next
495aee44
CR
4833array element to be assigned and the line to be assigned to that element
4834as additional arguments.
a0c0a00f 4835@var{callback} is evaluated after the line is read but before the
17345e5a 4836array element is assigned.
3185942a
JA
4837
4838If not supplied with an explicit origin, @code{mapfile} will clear @var{array}
4839before assigning to it.
4840
4841@code{mapfile} returns successfully unless an invalid option or option
0001803f
CR
4842argument is supplied, @var{array} is invalid or unassignable, or @var{array}
4843is not an indexed array.
3185942a 4844
bb70624e
JA
4845@item printf
4846@btindex printf
4847@example
3185942a 4848printf [-v @var{var}] @var{format} [@var{arguments}]
bb70624e 4849@end example
ac50fbac 4850
bb70624e
JA
4851Write the formatted @var{arguments} to the standard output under the
4852control of the @var{format}.
495aee44
CR
4853The @option{-v} option causes the output to be assigned to the variable
4854@var{var} rather than being printed to the standard output.
4855
bb70624e
JA
4856The @var{format} is a character string which contains three types of objects:
4857plain characters, which are simply copied to standard output, character
4858escape sequences, which are converted and copied to the standard output, and
4859format specifications, each of which causes printing of the next successive
4860@var{argument}.
495aee44
CR
4861In addition to the standard @code{printf(1)} formats, @code{printf}
4862interprets the following extensions:
4863
4864@table @code
4865@item %b
ac50fbac 4866Causes @code{printf} to expand backslash escape sequences in the
a0c0a00f
CR
4867corresponding @var{argument} in the same way as @code{echo -e}
4868(@pxref{Bash Builtins}).
495aee44 4869@item %q
ac50fbac 4870Causes @code{printf} to output the
bb70624e 4871corresponding @var{argument} in a format that can be reused as shell input.
74091dd4
CR
4872@item %Q
4873like @code{%q}, but applies any supplied precision to the @var{argument}
4874before quoting it.
495aee44 4875@item %(@var{datefmt})T
ac50fbac
CR
4876Causes @code{printf} to output the date-time string resulting from using
4877@var{datefmt} as a format string for @code{strftime}(3).
4878The corresponding @var{argument} is an integer representing the number of
4879seconds since the epoch.
4880Two special argument values may be used: -1 represents the current
495aee44 4881time, and -2 represents the time the shell was invoked.
ac50fbac
CR
4882If no argument is specified, conversion behaves as if -1 had been given.
4883This is an exception to the usual @code{printf} behavior.
495aee44 4884@end table
cce855bc 4885
495aee44 4886@noindent
8868edaf
CR
4887The %b, %q, and %T directives all use the field width and precision
4888arguments from the format specification and write that many bytes from
4889(or use that wide a field for) the expanded argument, which usually
4890contains more characters than the original.
4891
495aee44
CR
4892Arguments to non-string format specifiers are treated as C language constants,
4893except that a leading plus or minus sign is allowed, and if the leading
4894character is a single or double quote, the value is the ASCII value of
4895the following character.
95732b49 4896
bb70624e
JA
4897The @var{format} is reused as necessary to consume all of the @var{arguments}.
4898If the @var{format} requires more @var{arguments} than are supplied, the
4899extra format specifications behave as if a zero value or null string, as
4900appropriate, had been supplied. The return value is zero on success,
4901non-zero on failure.
ccc6cda3 4902
bb70624e
JA
4903@item read
4904@btindex read
4905@example
ac50fbac
CR
4906read [-ers] [-a @var{aname}] [-d @var{delim}] [-i @var{text}] [-n @var{nchars}]
4907 [-N @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}]
bb70624e 4908@end example
ac50fbac 4909
7117c2d2 4910One line is read from the standard input, or from the file descriptor
a0c0a00f
CR
4911@var{fd} supplied as an argument to the @option{-u} option,
4912split into words as described above in @ref{Word Splitting},
4913and the first word
bb70624e 4914is assigned to the first @var{name}, the second word to the second @var{name},
a0c0a00f
CR
4915and so on.
4916If there are more words than names,
4917the remaining words and their intervening delimiters are assigned
bb70624e 4918to the last @var{name}.
7117c2d2 4919If there are fewer words read from the input stream than names,
bb70624e 4920the remaining names are assigned empty values.
28ef6c31 4921The characters in the value of the @env{IFS} variable
ac50fbac
CR
4922are used to split the line into words using the same rules the shell
4923uses for expansion (described above in @ref{Word Splitting}).
bb70624e
JA
4924The backslash character @samp{\} may be used to remove any special
4925meaning for the next character read and for line continuation.
3185942a 4926
bb70624e 4927Options, if supplied, have the following meanings:
ccc6cda3 4928
bb70624e
JA
4929@table @code
4930@item -a @var{aname}
4931The words are assigned to sequential indices of the array variable
4932@var{aname}, starting at 0.
4933All elements are removed from @var{aname} before the assignment.
4934Other @var{name} arguments are ignored.
ccc6cda3 4935
bb70624e
JA
4936@item -d @var{delim}
4937The first character of @var{delim} is used to terminate the input line,
4938rather than newline.
d233b485
CR
4939If @var{delim} is the empty string, @code{read} will terminate a line
4940when it reads a NUL character.
ccc6cda3 4941
bb70624e
JA
4942@item -e
4943Readline (@pxref{Command Line Editing}) is used to obtain the line.
3185942a 4944Readline uses the current (or default, if line editing was not previously
d233b485 4945active) editing settings, but uses Readline's default filename completion.
3185942a
JA
4946
4947@item -i @var{text}
4948If Readline is being used to read the line, @var{text} is placed into
4949the editing buffer before editing begins.
ccc6cda3 4950
bb70624e
JA
4951@item -n @var{nchars}
4952@code{read} returns after reading @var{nchars} characters rather than
a0c0a00f 4953waiting for a complete line of input, but honors a delimiter if fewer
0001803f
CR
4954than @var{nchars} characters are read before the delimiter.
4955
4956@item -N @var{nchars}
4957@code{read} returns after reading exactly @var{nchars} characters rather
4958than waiting for a complete line of input, unless EOF is encountered or
4959@code{read} times out.
4960Delimiter characters encountered in the input are
4961not treated specially and do not cause @code{read} to return until
4962@var{nchars} characters are read.
a0c0a00f
CR
4963The result is not split on the characters in @code{IFS}; the intent is
4964that the variable is assigned exactly the characters read
4965(with the exception of backslash; see the @option{-r} option below).
ccc6cda3 4966
bb70624e
JA
4967@item -p @var{prompt}
4968Display @var{prompt}, without a trailing newline, before attempting
4969to read any input.
4970The prompt is displayed only if input is coming from a terminal.
ccc6cda3 4971
bb70624e
JA
4972@item -r
4973If this option is given, backslash does not act as an escape character.
4974The backslash is considered to be part of the line.
d233b485 4975In particular, a backslash-newline pair may not then be used as a line
bb70624e 4976continuation.
ccc6cda3 4977
bb70624e
JA
4978@item -s
4979Silent mode. If input is coming from a terminal, characters are
4980not echoed.
ccc6cda3 4981
bb70624e
JA
4982@item -t @var{timeout}
4983Cause @code{read} to time out and return failure if a complete line of
ac50fbac
CR
4984input (or a specified number of characters)
4985is not read within @var{timeout} seconds.
3185942a
JA
4986@var{timeout} may be a decimal number with a fractional portion following
4987the decimal point.
4988This option is only effective if @code{read} is reading input from a
4989terminal, pipe, or other special file; it has no effect when reading
4990from regular files.
ac50fbac
CR
4991If @code{read} times out, @code{read} saves any partial input read into
4992the specified variable @var{name}.
4993If @var{timeout} is 0, @code{read} returns immediately, without trying to
74091dd4
CR
4994read any data.
4995The exit status is 0 if input is available on the specified file descriptor,
4996or the read will return EOF,
4997non-zero otherwise.
3185942a 4998The exit status is greater than 128 if the timeout is exceeded.
ccc6cda3 4999
7117c2d2
JA
5000@item -u @var{fd}
5001Read input from file descriptor @var{fd}.
bb70624e 5002@end table
ccc6cda3 5003
8868edaf
CR
5004If no @var{name}s are supplied, the line read,
5005without the ending delimiter but otherwise unmodified,
5006is assigned to the
5007variable @env{REPLY}.
5008The exit status is zero, unless end-of-file is encountered, @code{read}
5009times out (in which case the status is greater than 128),
5010a variable assignment error (such as assigning to a readonly variable) occurs,
5011or an invalid file descriptor is supplied as the argument to @option{-u}.
5012
17345e5a
JA
5013@item readarray
5014@btindex readarray
5015@example
d233b485
CR
5016readarray [-d @var{delim}] [-n @var{count}] [-O @var{origin}] [-s @var{count}]
5017 [-t] [-u @var{fd}] [-C @var{callback}] [-c @var{quantum}] [@var{array}]
17345e5a 5018@end example
ac50fbac 5019
0001803f 5020Read lines from the standard input into the indexed array variable @var{array},
17345e5a
JA
5021or from file descriptor @var{fd}
5022if the @option{-u} option is supplied.
5023
5024A synonym for @code{mapfile}.
5025
3185942a
JA
5026@item source
5027@btindex source
bb70624e 5028@example
3185942a 5029source @var{filename}
bb70624e 5030@end example
ac50fbac 5031
3185942a 5032A synonym for @code{.} (@pxref{Bourne Shell Builtins}).
cce855bc 5033
3185942a
JA
5034@item type
5035@btindex type
5036@example
5037type [-afptP] [@var{name} @dots{}]
5038@end example
ac50fbac 5039
3185942a
JA
5040For each @var{name}, indicate how it would be interpreted if used as a
5041command name.
cce855bc 5042
3185942a
JA
5043If the @option{-t} option is used, @code{type} prints a single word
5044which is one of @samp{alias}, @samp{function}, @samp{builtin},
5045@samp{file} or @samp{keyword},
5046if @var{name} is an alias, shell function, shell builtin,
5047disk file, or shell reserved word, respectively.
5048If the @var{name} is not found, then nothing is printed, and
5049@code{type} returns a failure status.
ccc6cda3 5050
3185942a
JA
5051If the @option{-p} option is used, @code{type} either returns the name
5052of the disk file that would be executed, or nothing if @option{-t}
5053would not return @samp{file}.
ccc6cda3 5054
3185942a
JA
5055The @option{-P} option forces a path search for each @var{name}, even if
5056@option{-t} would not return @samp{file}.
cce855bc 5057
3185942a 5058If a command is hashed, @option{-p} and @option{-P} print the hashed value,
ac50fbac 5059which is not necessarily the file that appears first in @code{$PATH}.
ccc6cda3 5060
3185942a
JA
5061If the @option{-a} option is used, @code{type} returns all of the places
5062that contain an executable named @var{file}.
5063This includes aliases and functions, if and only if the @option{-p} option
5064is not also used.
cce855bc 5065
3185942a
JA
5066If the @option{-f} option is used, @code{type} does not attempt to find
5067shell functions, as with the @code{command} builtin.
ccc6cda3 5068
74091dd4 5069The return status is zero if all of the @var{name}s are found, non-zero
3185942a 5070if any are not found.
ccc6cda3 5071
3185942a
JA
5072@item typeset
5073@btindex typeset
5074@example
ac50fbac 5075typeset [-afFgrxilnrtux] [-p] [@var{name}[=@var{value}] @dots{}]
3185942a 5076@end example
ac50fbac 5077
3185942a 5078The @code{typeset} command is supplied for compatibility with the Korn
ac50fbac
CR
5079shell.
5080It is a synonym for the @code{declare} builtin command.
ccc6cda3 5081
3185942a
JA
5082@item ulimit
5083@btindex ulimit
5084@example
8868edaf
CR
5085ulimit [-HS] -a
5086ulimit [-HS] [-bcdefiklmnpqrstuvxPRT] [@var{limit}]
3185942a 5087@end example
ac50fbac 5088
3185942a
JA
5089@code{ulimit} provides control over the resources available to processes
5090started by the shell, on systems that allow such control. If an
5091option is given, it is interpreted as follows:
ac50fbac 5092
3185942a
JA
5093@table @code
5094@item -S
5095Change and report the soft limit associated with a resource.
ccc6cda3 5096
3185942a
JA
5097@item -H
5098Change and report the hard limit associated with a resource.
ccc6cda3 5099
3185942a 5100@item -a
8868edaf 5101All current limits are reported; no limits are set.
cce855bc 5102
3185942a
JA
5103@item -b
5104The maximum socket buffer size.
f1be666c 5105
3185942a
JA
5106@item -c
5107The maximum size of core files created.
ccc6cda3 5108
3185942a
JA
5109@item -d
5110The maximum size of a process's data segment.
cce855bc 5111
3185942a
JA
5112@item -e
5113The maximum scheduling priority ("nice").
ccc6cda3 5114
3185942a
JA
5115@item -f
5116The maximum size of files written by the shell and its children.
b80f6443 5117
3185942a
JA
5118@item -i
5119The maximum number of pending signals.
b80f6443 5120
a0c0a00f
CR
5121@item -k
5122The maximum number of kqueues that may be allocated.
5123
3185942a
JA
5124@item -l
5125The maximum size that may be locked into memory.
b80f6443 5126
3185942a 5127@item -m
17345e5a 5128The maximum resident set size (many systems do not honor this limit).
95732b49 5129
3185942a 5130@item -n
17345e5a
JA
5131The maximum number of open file descriptors (most systems do not
5132allow this value to be set).
ccc6cda3 5133
bb70624e
JA
5134@item -p
5135The pipe buffer size.
ccc6cda3 5136
0628567a 5137@item -q
8868edaf 5138The maximum number of bytes in @sc{posix} message queues.
0628567a
JA
5139
5140@item -r
5141The maximum real-time scheduling priority.
5142
bb70624e
JA
5143@item -s
5144The maximum stack size.
ccc6cda3 5145
bb70624e
JA
5146@item -t
5147The maximum amount of cpu time in seconds.
ccc6cda3 5148
bb70624e
JA
5149@item -u
5150The maximum number of processes available to a single user.
ccc6cda3 5151
bb70624e 5152@item -v
495aee44
CR
5153The maximum amount of virtual memory available to the shell, and, on
5154some systems, to its children.
ccc6cda3 5155
0628567a
JA
5156@item -x
5157The maximum number of file locks.
5158
a0c0a00f
CR
5159@item -P
5160The maximum number of pseudoterminals.
5161
8868edaf
CR
5162@item -R
5163The maximum time a real-time process can run before blocking, in microseconds.
5164
3185942a
JA
5165@item -T
5166The maximum number of threads.
bb70624e 5167@end table
ccc6cda3 5168
ac50fbac
CR
5169If @var{limit} is given, and the @option{-a} option is not used,
5170@var{limit} is the new value of the specified resource.
5171The special @var{limit} values @code{hard}, @code{soft}, and
f73dda09
JA
5172@code{unlimited} stand for the current hard limit, the current soft limit,
5173and no limit, respectively.
3185942a
JA
5174A hard limit cannot be increased by a non-root user once it is set;
5175a soft limit may be increased up to the value of the hard limit.
bb70624e 5176Otherwise, the current value of the soft limit for the specified resource
28ef6c31 5177is printed, unless the @option{-H} option is supplied.
8868edaf
CR
5178When more than one
5179resource is specified, the limit name and unit, if appropriate,
5180are printed before the value.
28ef6c31 5181When setting new limits, if neither @option{-H} nor @option{-S} is supplied,
bb70624e 5182both the hard and soft limits are set.
28ef6c31 5183If no option is given, then @option{-f} is assumed. Values are in 1024-byte
8868edaf
CR
5184increments, except for
5185@option{-t}, which is in seconds;
5186@option{-R}, which is in microseconds;
5187@option{-p}, which is in units of 512-byte blocks;
a0c0a00f
CR
5188@option{-P},
5189@option{-T},
5190@option{-b},
5191@option{-k},
5192@option{-n} and @option{-u}, which are unscaled values;
5193and, when in @sc{posix} Mode (@pxref{Bash POSIX Mode}),
5194@option{-c} and @option{-f}, which are in 512-byte increments.
ccc6cda3 5195
f73dda09
JA
5196The return status is zero unless an invalid option or argument is supplied,
5197or an error occurs while setting a new limit.
ccc6cda3 5198
bb70624e
JA
5199@item unalias
5200@btindex unalias
5201@example
5202unalias [-a] [@var{name} @dots{} ]
5203@end example
ccc6cda3 5204
28ef6c31 5205Remove each @var{name} from the list of aliases. If @option{-a} is
bb70624e
JA
5206supplied, all aliases are removed.
5207Aliases are described in @ref{Aliases}.
ccc6cda3
JA
5208@end table
5209
3185942a
JA
5210@node Modifying Shell Behavior
5211@section Modifying Shell Behavior
5212
5213@menu
5214* The Set Builtin:: Change the values of shell attributes and
5215 positional parameters.
5216* The Shopt Builtin:: Modify shell optional behavior.
5217@end menu
5218
bb70624e 5219@node The Set Builtin
3185942a 5220@subsection The Set Builtin
ccc6cda3 5221
3185942a
JA
5222This builtin is so complicated that it deserves its own section. @code{set}
5223allows you to change the values of shell options and set the positional
5224parameters, or to display the names and values of shell variables.
ccc6cda3 5225
bb70624e
JA
5226@table @code
5227@item set
5228@btindex set
5229@example
74091dd4
CR
5230set [-abefhkmnptuvxBCEHPT] [-o @var{option-name}] [--] [-] [@var{argument} @dots{}]
5231set [+abefhkmnptuvxBCEHPT] [+o @var{option-name}] [--] [-] [@var{argument} @dots{}]
bb70624e 5232@end example
ccc6cda3 5233
bb70624e
JA
5234If no options or arguments are supplied, @code{set} displays the names
5235and values of all shell variables and functions, sorted according to the
95732b49
JA
5236current locale, in a format that may be reused as input
5237for setting or resetting the currently-set variables.
5238Read-only variables cannot be reset.
5239In @sc{posix} mode, only shell variables are listed.
ccc6cda3 5240
bb70624e
JA
5241When options are supplied, they set or unset shell attributes.
5242Options, if specified, have the following meanings:
ccc6cda3 5243
bb70624e
JA
5244@table @code
5245@item -a
a0c0a00f
CR
5246Each variable or function that is created or modified is given the
5247export attribute and marked for export to the environment of
5248subsequent commands.
ccc6cda3 5249
bb70624e
JA
5250@item -b
5251Cause the status of terminated background jobs to be reported
5252immediately, rather than before printing the next primary prompt.
ccc6cda3 5253
bb70624e 5254@item -e
ac50fbac
CR
5255Exit immediately if
5256a pipeline (@pxref{Pipelines}), which may consist of a single simple command
5257(@pxref{Simple Commands}),
5258a list (@pxref{Lists}),
5259or a compound command (@pxref{Compound Commands})
17345e5a
JA
5260returns a non-zero status.
5261The shell does not exit if the command that fails is part of the
3185942a
JA
5262command list immediately following a @code{while} or @code{until} keyword,
5263part of the test in an @code{if} statement,
17345e5a
JA
5264part of any command executed in a @code{&&} or @code{||} list except
5265the command following the final @code{&&} or @code{||},
3185942a 5266any command in a pipeline but the last,
17345e5a 5267or if the command's return status is being inverted with @code{!}.
ac50fbac
CR
5268If a compound command other than a subshell
5269returns a non-zero status because a command failed
5270while @option{-e} was being ignored, the shell does not exit.
f73dda09 5271A trap on @code{ERR}, if set, is executed before the shell exits.
ccc6cda3 5272
17345e5a
JA
5273This option applies to the shell environment and each subshell environment
5274separately (@pxref{Command Execution Environment}), and may cause
5275subshells to exit before executing all the commands in the subshell.
5276
ac50fbac
CR
5277If a compound command or shell function executes in a context where
5278@option{-e} is being ignored,
5279none of the commands executed within the compound command or function body
5280will be affected by the @option{-e} setting, even if @option{-e} is set
5281and a command returns a failure status.
5282If a compound command or shell function sets @option{-e} while executing in
5283a context where @option{-e} is ignored, that setting will not have any
5284effect until the compound command or the command containing the function
5285call completes.
5286
bb70624e 5287@item -f
0001803f 5288Disable filename expansion (globbing).
ccc6cda3 5289
bb70624e
JA
5290@item -h
5291Locate and remember (hash) commands as they are looked up for execution.
5292This option is enabled by default.
ccc6cda3 5293
bb70624e
JA
5294@item -k
5295All arguments in the form of assignment statements are placed
5296in the environment for a command, not just those that precede
5297the command name.
ccc6cda3 5298
bb70624e
JA
5299@item -m
5300Job control is enabled (@pxref{Job Control}).
ac50fbac
CR
5301All processes run in a separate process group.
5302When a background job completes, the shell prints a line
5303containing its exit status.
ccc6cda3 5304
bb70624e 5305@item -n
a0c0a00f
CR
5306Read commands but do not execute them.
5307This may be used to check a script for syntax errors.
bb70624e 5308This option is ignored by interactive shells.
b72432fd 5309
bb70624e 5310@item -o @var{option-name}
ccc6cda3 5311
bb70624e 5312Set the option corresponding to @var{option-name}:
ccc6cda3 5313
bb70624e
JA
5314@table @code
5315@item allexport
5316Same as @code{-a}.
ccc6cda3 5317
bb70624e
JA
5318@item braceexpand
5319Same as @code{-B}.
ccc6cda3 5320
bb70624e
JA
5321@item emacs
5322Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}).
3185942a 5323This also affects the editing interface used for @code{read -e}.
d166f048 5324
bb70624e
JA
5325@item errexit
5326Same as @code{-e}.
d166f048 5327
b80f6443
JA
5328@item errtrace
5329Same as @code{-E}.
5330
5331@item functrace
5332Same as @code{-T}.
5333
bb70624e
JA
5334@item hashall
5335Same as @code{-h}.
ccc6cda3 5336
bb70624e
JA
5337@item histexpand
5338Same as @code{-H}.
ccc6cda3 5339
bb70624e
JA
5340@item history
5341Enable command history, as described in @ref{Bash History Facilities}.
5342This option is on by default in interactive shells.
ccc6cda3 5343
bb70624e
JA
5344@item ignoreeof
5345An interactive shell will not exit upon reading EOF.
ccc6cda3 5346
bb70624e
JA
5347@item keyword
5348Same as @code{-k}.
ccc6cda3 5349
bb70624e
JA
5350@item monitor
5351Same as @code{-m}.
ccc6cda3 5352
bb70624e
JA
5353@item noclobber
5354Same as @code{-C}.
ccc6cda3 5355
bb70624e
JA
5356@item noexec
5357Same as @code{-n}.
ccc6cda3 5358
bb70624e
JA
5359@item noglob
5360Same as @code{-f}.
ccc6cda3 5361
f73dda09
JA
5362@item nolog
5363Currently ignored.
5364
3185942a
JA
5365@item notify
5366Same as @code{-b}.
5367
5368@item nounset
5369Same as @code{-u}.
5370
5371@item onecmd
5372Same as @code{-t}.
5373
5374@item physical
5375Same as @code{-P}.
5376
5377@item pipefail
5378If set, the return value of a pipeline is the value of the last
5379(rightmost) command to exit with a non-zero status, or zero if all
5380commands in the pipeline exit successfully.
5381This option is disabled by default.
5382
5383@item posix
5384Change the behavior of Bash where the default operation differs
5385from the @sc{posix} standard to match the standard
5386(@pxref{Bash POSIX Mode}).
5387This is intended to make Bash behave as a strict superset of that
5388standard.
5389
5390@item privileged
5391Same as @code{-p}.
5392
5393@item verbose
5394Same as @code{-v}.
5395
5396@item vi
5397Use a @code{vi}-style line editing interface.
5398This also affects the editing interface used for @code{read -e}.
5399
5400@item xtrace
5401Same as @code{-x}.
5402@end table
5403
5404@item -p
5405Turn on privileged mode.
5406In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not
5407processed, shell functions are not inherited from the environment,
0001803f
CR
5408and the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH} and @env{GLOBIGNORE}
5409variables, if they appear in the environment, are ignored.
3185942a 5410If the shell is started with the effective user (group) id not equal to the
ac50fbac 5411real user (group) id, and the @option{-p} option is not supplied, these actions
3185942a 5412are taken and the effective user id is set to the real user id.
ac50fbac 5413If the @option{-p} option is supplied at startup, the effective user id is
3185942a
JA
5414not reset.
5415Turning this option off causes the effective user
5416and group ids to be set to the real user and group ids.
5417
74091dd4
CR
5418@item -r
5419Enable restricted shell mode.
5420This option cannot be unset once it has been set.
5421
3185942a
JA
5422@item -t
5423Exit after reading and executing one command.
5424
5425@item -u
89a92869 5426Treat unset variables and parameters other than the special parameters
74091dd4
CR
5427@samp{@@} or @samp{*},
5428or array variables subscripted with @samp{@@} or @samp{*},
5429as an error when performing parameter expansion.
3185942a
JA
5430An error message will be written to the standard error, and a non-interactive
5431shell will exit.
5432
5433@item -v
5434Print shell input lines as they are read.
5435
5436@item -x
5437Print a trace of simple commands, @code{for} commands, @code{case}
5438commands, @code{select} commands, and arithmetic @code{for} commands
5439and their arguments or associated word lists after they are
5440expanded and before they are executed. The value of the @env{PS4}
5441variable is expanded and the resultant value is printed before
5442the command and its expanded arguments.
5443
5444@item -B
5445The shell will perform brace expansion (@pxref{Brace Expansion}).
5446This option is on by default.
5447
5448@item -C
5449Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>}
5450from overwriting existing files.
5451
5452@item -E
5453If set, any trap on @code{ERR} is inherited by shell functions, command
5454substitutions, and commands executed in a subshell environment.
5455The @code{ERR} trap is normally not inherited in such cases.
5456
5457@item -H
5458Enable @samp{!} style history substitution (@pxref{History Interaction}).
5459This option is on by default for interactive shells.
5460
5461@item -P
ac50fbac 5462If set, do not resolve symbolic links when performing commands such as
3185942a
JA
5463@code{cd} which change the current directory. The physical directory
5464is used instead. By default, Bash follows
5465the logical chain of directories when performing commands
5466which change the current directory.
5467
5468For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys}
5469then:
5470@example
5471$ cd /usr/sys; echo $PWD
5472/usr/sys
5473$ cd ..; pwd
5474/usr
5475@end example
5476
5477@noindent
5478If @code{set -P} is on, then:
5479@example
5480$ cd /usr/sys; echo $PWD
5481/usr/local/sys
5482$ cd ..; pwd
5483/usr/local
5484@end example
5485
5486@item -T
5487If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by
5488shell functions, command substitutions, and commands executed
5489in a subshell environment.
5490The @code{DEBUG} and @code{RETURN} traps are normally not inherited
5491in such cases.
5492
5493@item --
5494If no arguments follow this option, then the positional parameters are
5495unset. Otherwise, the positional parameters are set to the
5496@var{arguments}, even if some of them begin with a @samp{-}.
5497
5498@item -
5499Signal the end of options, cause all remaining @var{arguments}
5500to be assigned to the positional parameters. The @option{-x}
5501and @option{-v} options are turned off.
5502If there are no arguments, the positional parameters remain unchanged.
5503@end table
5504
5505Using @samp{+} rather than @samp{-} causes these options to be
5506turned off. The options can also be used upon invocation of the
5507shell. The current set of options may be found in @code{$-}.
5508
5509The remaining N @var{arguments} are positional parameters and are
5510assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}.
5511The special parameter @code{#} is set to N.
5512
5513The return status is always zero unless an invalid option is supplied.
5514@end table
5515
5516@node The Shopt Builtin
5517@subsection The Shopt Builtin
5518
5519This builtin allows you to change additional shell optional behavior.
5520
5521@table @code
5522
5523@item shopt
5524@btindex shopt
5525@example
5526shopt [-pqsu] [-o] [@var{optname} @dots{}]
5527@end example
ac50fbac
CR
5528
5529Toggle the values of settings controlling optional shell behavior.
5530The settings can be either those listed below, or, if the
5531@option{-o} option is used, those available with the @option{-o}
5532option to the @code{set} builtin command (@pxref{The Set Builtin}).
3185942a 5533With no options, or with the @option{-p} option, a list of all settable
d233b485 5534options is displayed, with an indication of whether or not each is set;
74091dd4 5535if @var{optname}s are supplied, the output is restricted to those options.
3185942a
JA
5536The @option{-p} option causes output to be displayed in a form that
5537may be reused as input.
5538Other options have the following meanings:
5539
5540@table @code
5541@item -s
5542Enable (set) each @var{optname}.
5543
5544@item -u
5545Disable (unset) each @var{optname}.
5546
5547@item -q
5548Suppresses normal output; the return status
5549indicates whether the @var{optname} is set or unset.
5550If multiple @var{optname} arguments are given with @option{-q},
74091dd4 5551the return status is zero if all @var{optname}s are enabled;
3185942a
JA
5552non-zero otherwise.
5553
5554@item -o
5555Restricts the values of
5556@var{optname} to be those defined for the @option{-o} option to the
5557@code{set} builtin (@pxref{The Set Builtin}).
5558@end table
5559
5560If either @option{-s} or @option{-u}
ac50fbac 5561is used with no @var{optname} arguments, @code{shopt} shows only
3185942a
JA
5562those options which are set or unset, respectively.
5563
5564Unless otherwise noted, the @code{shopt} options are disabled (off)
5565by default.
5566
74091dd4 5567The return status when listing options is zero if all @var{optname}s
3185942a
JA
5568are enabled, non-zero otherwise. When setting or unsetting options,
5569the return status is zero unless an @var{optname} is not a valid shell
5570option.
5571
5572The list of @code{shopt} options is:
5573@table @code
5574
d233b485
CR
5575@item assoc_expand_once
5576If set, the shell suppresses multiple evaluation of associative array
5577subscripts during arithmetic expression evaluation, while executing
5578builtins that can perform variable assignments,
5579and while executing builtins that perform array dereferencing.
5580
3185942a
JA
5581@item autocd
5582If set, a command name that is the name of a directory is executed as if
5583it were the argument to the @code{cd} command.
5584This option is only used by interactive shells.
5585
5586@item cdable_vars
5587If this is set, an argument to the @code{cd} builtin command that
5588is not a directory is assumed to be the name of a variable whose
5589value is the directory to change to.
5590
5591@item cdspell
5592If set, minor errors in the spelling of a directory component in a
5593@code{cd} command will be corrected.
5594The errors checked for are transposed characters,
5595a missing character, and a character too many.
5596If a correction is found, the corrected path is printed,
5597and the command proceeds.
5598This option is only used by interactive shells.
5599
5600@item checkhash
5601If this is set, Bash checks that a command found in the hash
5602table exists before trying to execute it. If a hashed command no
5603longer exists, a normal path search is performed.
5604
5605@item checkjobs
5606If set, Bash lists the status of any stopped and running jobs before
5607exiting an interactive shell. If any jobs are running, this causes
5608the exit to be deferred until a second exit is attempted without an
5609intervening command (@pxref{Job Control}).
5610The shell always postpones exiting if any jobs are stopped.
5611
5612@item checkwinsize
d233b485
CR
5613If set, Bash checks the window size after each external (non-builtin)
5614command and, if necessary, updates the values of
3185942a 5615@env{LINES} and @env{COLUMNS}.
d233b485 5616This option is enabled by default.
3185942a
JA
5617
5618@item cmdhist
5619If set, Bash
5620attempts to save all lines of a multiple-line
5621command in the same history entry. This allows
5622easy re-editing of multi-line commands.
d233b485
CR
5623This option is enabled by default, but only has an effect if command
5624history is enabled (@pxref{Bash History Facilities}).
3185942a
JA
5625
5626@item compat31
8868edaf
CR
5627@itemx compat32
5628@itemx compat40
5629@itemx compat41
5630@itemx compat42
5631@itemx compat43
5632@itemx compat44
5633These control aspects of the shell's compatibility mode
5634(@pxref{Shell Compatibility Mode}).
d233b485 5635
ac50fbac
CR
5636@item complete_fullquote
5637If set, Bash
5638quotes all shell metacharacters in filenames and directory names when
5639performing completion.
5640If not set, Bash
5641removes metacharacters such as the dollar sign from the set of
5642characters that will be quoted in completed filenames
5643when these metacharacters appear in shell variable references in words to be
5644completed.
5645This means that dollar signs in variable names that expand to directories
5646will not be quoted;
5647however, any dollar signs appearing in filenames will not be quoted, either.
5648This is active only when bash is using backslashes to quote completed
5649filenames.
5650This variable is set by default, which is the default Bash behavior in
5651versions through 4.2.
5652
16b2d7f4
CR
5653@item direxpand
5654If set, Bash
5655replaces directory names with the results of word expansion when performing
74091dd4 5656filename completion. This changes the contents of the Readline editing
16b2d7f4
CR
5657buffer.
5658If not set, Bash attempts to preserve what the user typed.
5659
3185942a
JA
5660@item dirspell
5661If set, Bash
5662attempts spelling correction on directory names during word completion
5663if the directory name initially supplied does not exist.
5664
5665@item dotglob
5666If set, Bash includes filenames beginning with a `.' in
5667the results of filename expansion.
d233b485
CR
5668The filenames @samp{.} and @samp{..} must always be matched explicitly,
5669even if @code{dotglob} is set.
3185942a
JA
5670
5671@item execfail
5672If this is set, a non-interactive shell will not exit if
5673it cannot execute the file specified as an argument to the @code{exec}
5674builtin command. An interactive shell does not exit if @code{exec}
5675fails.
5676
5677@item expand_aliases
5678If set, aliases are expanded as described below under Aliases,
5679@ref{Aliases}.
5680This option is enabled by default for interactive shells.
5681
5682@item extdebug
8868edaf
CR
5683If set at shell invocation,
5684or in a shell startup file,
5685arrange to execute the debugger profile
a0c0a00f
CR
5686before the shell starts, identical to the @option{--debugger} option.
5687If set after invocation, behavior intended for use by debuggers is enabled:
3185942a
JA
5688
5689@enumerate
5690@item
5691The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins})
5692displays the source file name and line number corresponding to each function
5693name supplied as an argument.
5694
5695@item
5696If the command run by the @code{DEBUG} trap returns a non-zero value, the
5697next command is skipped and not executed.
5698
5699@item
5700If the command run by the @code{DEBUG} trap returns a value of 2, and the
5701shell is executing in a subroutine (a shell function or a shell script
a0c0a00f
CR
5702executed by the @code{.} or @code{source} builtins), the shell simulates
5703a call to @code{return}.
3185942a
JA
5704
5705@item
5706@code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their
5707descriptions (@pxref{Bash Variables}).
5708
5709@item
a0c0a00f 5710Function tracing is enabled: command substitution, shell functions, and
3185942a
JA
5711subshells invoked with @code{( @var{command} )} inherit the
5712@code{DEBUG} and @code{RETURN} traps.
5713
5714@item
a0c0a00f 5715Error tracing is enabled: command substitution, shell functions, and
3185942a 5716subshells invoked with @code{( @var{command} )} inherit the
495aee44 5717@code{ERR} trap.
3185942a
JA
5718@end enumerate
5719
5720@item extglob
5721If set, the extended pattern matching features described above
5722(@pxref{Pattern Matching}) are enabled.
5723
5724@item extquote
5725If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is
5726performed within @code{$@{@var{parameter}@}} expansions
5727enclosed in double quotes. This option is enabled by default.
ccc6cda3 5728
3185942a 5729@item failglob
0001803f 5730If set, patterns which fail to match filenames during filename expansion
3185942a 5731result in an expansion error.
ccc6cda3 5732
3185942a
JA
5733@item force_fignore
5734If set, the suffixes specified by the @env{FIGNORE} shell variable
5735cause words to be ignored when performing word completion even if
5736the ignored words are the only possible completions.
5737@xref{Bash Variables}, for a description of @env{FIGNORE}.
5738This option is enabled by default.
ccc6cda3 5739
ac50fbac
CR
5740@item globasciiranges
5741If set, range expressions used in pattern matching bracket expressions
5742(@pxref{Pattern Matching})
5743behave as if in the traditional C locale when performing
5744comparisons. That is, the current locale's collating sequence
5745is not taken into account, so
5746@samp{b} will not collate between @samp{A} and @samp{B},
5747and upper-case and lower-case ASCII characters will collate together.
5748
74091dd4
CR
5749@item globskipdots
5750If set, filename expansion will never match the filenames
5751@samp{.} and @samp{..},
5752even if the pattern begins with a @samp{.}.
5753This option is enabled by default.
5754
3185942a
JA
5755@item globstar
5756If set, the pattern @samp{**} used in a filename expansion context will
495aee44 5757match all files and zero or more directories and subdirectories.
3185942a
JA
5758If the pattern is followed by a @samp{/}, only directories and
5759subdirectories match.
ccc6cda3 5760
3185942a
JA
5761@item gnu_errfmt
5762If set, shell error messages are written in the standard @sc{gnu} error
5763message format.
b80f6443 5764
3185942a
JA
5765@item histappend
5766If set, the history list is appended to the file named by the value
5767of the @env{HISTFILE}
5768variable when the shell exits, rather than overwriting the file.
ccc6cda3 5769
3185942a
JA
5770@item histreedit
5771If set, and Readline
5772is being used, a user is given the opportunity to re-edit a
5773failed history substitution.
cce855bc 5774
3185942a
JA
5775@item histverify
5776If set, and Readline
5777is being used, the results of history substitution are not immediately
5778passed to the shell parser. Instead, the resulting line is loaded into
5779the Readline editing buffer, allowing further modification.
ccc6cda3 5780
3185942a
JA
5781@item hostcomplete
5782If set, and Readline is being used, Bash will attempt to perform
5783hostname completion when a word containing a @samp{@@} is being
5784completed (@pxref{Commands For Completion}). This option is enabled
5785by default.
ccc6cda3 5786
3185942a
JA
5787@item huponexit
5788If set, Bash will send @code{SIGHUP} to all jobs when an interactive
5789login shell exits (@pxref{Signals}).
ccc6cda3 5790
a0c0a00f
CR
5791@item inherit_errexit
5792If set, command substitution inherits the value of the @code{errexit} option,
5793instead of unsetting it in the subshell environment.
5794This option is enabled when @sc{posix} mode is enabled.
5795
3185942a
JA
5796@item interactive_comments
5797Allow a word beginning with @samp{#}
5798to cause that word and all remaining characters on that
5799line to be ignored in an interactive shell.
5800This option is enabled by default.
cce855bc 5801
495aee44
CR
5802@item lastpipe
5803If set, and job control is not active, the shell runs the last command of
5804a pipeline not executed in the background in the current shell environment.
5805
3185942a
JA
5806@item lithist
5807If enabled, and the @code{cmdhist}
5808option is enabled, multi-line commands are saved to the history with
5809embedded newlines rather than using semicolon separators where possible.
ccc6cda3 5810
d233b485
CR
5811@item localvar_inherit
5812If set, local variables inherit the value and attributes of a variable of
5813the same name that exists at a previous scope before any new value is
74091dd4 5814assigned. The @code{nameref} attribute is not inherited.
d233b485
CR
5815
5816@item localvar_unset
5817If set, calling @code{unset} on local variables in previous function scopes
5818marks them so subsequent lookups find them unset until that function
5819returns. This is identical to the behavior of unsetting local variables
5820at the current function scope.
5821
3185942a
JA
5822@item login_shell
5823The shell sets this option if it is started as a login shell
5824(@pxref{Invoking Bash}).
5825The value may not be changed.
ccc6cda3 5826
3185942a 5827@item mailwarn
a0c0a00f 5828If set, and a file that Bash is checking for mail has been
3185942a
JA
5829accessed since the last time it was checked, the message
5830@code{"The mail in @var{mailfile} has been read"} is displayed.
ccc6cda3 5831
3185942a
JA
5832@item no_empty_cmd_completion
5833If set, and Readline is being used, Bash will not attempt to search
5834the @env{PATH} for possible completions when completion is attempted
5835on an empty line.
ccc6cda3 5836
3185942a
JA
5837@item nocaseglob
5838If set, Bash matches filenames in a case-insensitive fashion when
5839performing filename expansion.
ccc6cda3 5840
3185942a
JA
5841@item nocasematch
5842If set, Bash matches patterns in a case-insensitive fashion when
5843performing matching while executing @code{case} or @code{[[}
74091dd4 5844conditional commands (@pxref{Conditional Constructs},
a0c0a00f
CR
5845when performing pattern substitution word expansions,
5846or when filtering possible completions as part of programmable completion.
ccc6cda3 5847
74091dd4
CR
5848@item noexpand_translation
5849If set, Bash
5850encloses the translated results of $"..." quoting in single quotes
5851instead of double quotes.
5852If the string is not translated, this has no effect.
5853
3185942a
JA
5854@item nullglob
5855If set, Bash allows filename patterns which match no
5856files to expand to a null string, rather than themselves.
b80f6443 5857
74091dd4
CR
5858@item patsub_replacement
5859If set, Bash
5860expands occurrences of @samp{&} in the replacement string of pattern
5861substitution to the text matched by the pattern, as described
5862above (@pxref{Shell Parameter Expansion}).
5863This option is enabled by default.
5864
3185942a
JA
5865@item progcomp
5866If set, the programmable completion facilities
5867(@pxref{Programmable Completion}) are enabled.
5868This option is enabled by default.
ccc6cda3 5869
d233b485
CR
5870@item progcomp_alias
5871If set, and programmable completion is enabled, Bash treats a command
5872name that doesn't have any completions as a possible alias and attempts
5873alias expansion. If it has an alias, Bash attempts programmable
5874completion using the command word resulting from the expanded alias.
5875
3185942a
JA
5876@item promptvars
5877If set, prompt strings undergo
5878parameter expansion, command substitution, arithmetic
5879expansion, and quote removal after being expanded
ac50fbac 5880as described below (@pxref{Controlling the Prompt}).
3185942a 5881This option is enabled by default.
ccc6cda3 5882
3185942a
JA
5883@item restricted_shell
5884The shell sets this option if it is started in restricted mode
5885(@pxref{The Restricted Shell}).
5886The value may not be changed.
5887This is not reset when the startup files are executed, allowing
5888the startup files to discover whether or not a shell is restricted.
ccc6cda3 5889
3185942a
JA
5890@item shift_verbose
5891If this is set, the @code{shift}
5892builtin prints an error message when the shift count exceeds the
5893number of positional parameters.
ccc6cda3 5894
3185942a 5895@item sourcepath
74091dd4 5896If set, the @code{.} (@code{source}) builtin uses the value of @env{PATH}
3185942a
JA
5897to find the directory containing the file supplied as an argument.
5898This option is enabled by default.
b80f6443 5899
74091dd4
CR
5900@item varredir_close
5901If set, the shell automatically closes file descriptors assigned using the
5902@code{@{varname@}} redirection syntax (@pxref{Redirections}) instead of
5903leaving them open when the command completes.
5904
3185942a
JA
5905@item xpg_echo
5906If set, the @code{echo} builtin expands backslash-escape sequences
5907by default.
bb70624e 5908
ccc6cda3 5909@end table
ccc6cda3
JA
5910@end table
5911
bb70624e
JA
5912@node Special Builtins
5913@section Special Builtins
5914@cindex special builtin
cce855bc 5915
0628567a 5916For historical reasons, the @sc{posix} standard has classified
bb70624e
JA
5917several builtin commands as @emph{special}.
5918When Bash is executing in @sc{posix} mode, the special builtins
5919differ from other builtin commands in three respects:
ccc6cda3 5920
bb70624e
JA
5921@enumerate
5922@item
5923Special builtins are found before shell functions during command lookup.
cce855bc 5924
bb70624e
JA
5925@item
5926If a special builtin returns an error status, a non-interactive shell exits.
cce855bc 5927
bb70624e
JA
5928@item
5929Assignment statements preceding the command stay in effect in the shell
5930environment after the command completes.
5931@end enumerate
ccc6cda3 5932
bb70624e
JA
5933When Bash is not executing in @sc{posix} mode, these builtins behave no
5934differently than the rest of the Bash builtin commands.
5935The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}.
ccc6cda3 5936
bb70624e 5937These are the @sc{posix} special builtins:
ccc6cda3 5938@example
bb70624e
JA
5939@w{break : . continue eval exec exit export readonly return set}
5940@w{shift trap unset}
ccc6cda3 5941@end example
ccc6cda3 5942
bb70624e
JA
5943@node Shell Variables
5944@chapter Shell Variables
cce855bc 5945
bb70624e
JA
5946@menu
5947* Bourne Shell Variables:: Variables which Bash uses in the same way
5948 as the Bourne Shell.
5949* Bash Variables:: List of variables that exist in Bash.
5950@end menu
cce855bc 5951
bb70624e
JA
5952This chapter describes the shell variables that Bash uses.
5953Bash automatically assigns default values to a number of variables.
ccc6cda3 5954
bb70624e
JA
5955@node Bourne Shell Variables
5956@section Bourne Shell Variables
ccc6cda3 5957
bb70624e
JA
5958Bash uses certain shell variables in the same way as the Bourne shell.
5959In some cases, Bash assigns a default value to the variable.
ccc6cda3 5960
bb70624e 5961@vtable @code
ccc6cda3 5962
bb70624e
JA
5963@item CDPATH
5964A colon-separated list of directories used as a search path for
5965the @code{cd} builtin command.
ccc6cda3 5966
bb70624e
JA
5967@item HOME
5968The current user's home directory; the default for the @code{cd} builtin
5969command.
5970The value of this variable is also used by tilde expansion
5971(@pxref{Tilde Expansion}).
ccc6cda3 5972
bb70624e
JA
5973@item IFS
5974A list of characters that separate fields; used when the shell splits
5975words as part of expansion.
ccc6cda3 5976
bb70624e 5977@item MAIL
495aee44
CR
5978If this parameter is set to a filename or directory name
5979and the @env{MAILPATH} variable
bb70624e 5980is not set, Bash informs the user of the arrival of mail in
495aee44 5981the specified file or Maildir-format directory.
ccc6cda3 5982
bb70624e
JA
5983@item MAILPATH
5984A colon-separated list of filenames which the shell periodically checks
5985for new mail.
5986Each list entry can specify the message that is printed when new mail
ac50fbac 5987arrives in the mail file by separating the filename from the message with
bb70624e
JA
5988a @samp{?}.
5989When used in the text of the message, @code{$_} expands to the name of
5990the current mail file.
ccc6cda3 5991
bb70624e
JA
5992@item OPTARG
5993The value of the last option argument processed by the @code{getopts} builtin.
ccc6cda3 5994
bb70624e
JA
5995@item OPTIND
5996The index of the last option argument processed by the @code{getopts} builtin.
ccc6cda3 5997
bb70624e
JA
5998@item PATH
5999A colon-separated list of directories in which the shell looks for
6000commands.
b80f6443
JA
6001A zero-length (null) directory name in the value of @code{PATH} indicates the
6002current directory.
6003A null directory name may appear as two adjacent colons, or as an initial
6004or trailing colon.
6005
bb70624e
JA
6006@item PS1
6007The primary prompt string. The default value is @samp{\s-\v\$ }.
ac50fbac 6008@xref{Controlling the Prompt}, for the complete list of escape
28ef6c31 6009sequences that are expanded before @env{PS1} is displayed.
ccc6cda3 6010
bb70624e
JA
6011@item PS2
6012The secondary prompt string. The default value is @samp{> }.
d233b485
CR
6013@env{PS2} is expanded in the same way as @env{PS1} before being
6014displayed.
ccc6cda3 6015
bb70624e 6016@end vtable
ccc6cda3 6017
bb70624e
JA
6018@node Bash Variables
6019@section Bash Variables
ccc6cda3 6020
bb70624e
JA
6021These variables are set or used by Bash, but other shells
6022do not normally treat them specially.
ccc6cda3 6023
bb70624e
JA
6024A few variables used by Bash are described in different chapters:
6025variables for controlling the job control facilities
6026(@pxref{Job Control Variables}).
ccc6cda3 6027
bb70624e 6028@vtable @code
cce855bc 6029
8868edaf
CR
6030@item _
6031@vindex $_
6032($_, an underscore.)
6033At shell startup, set to the pathname used to invoke the
6034shell or shell script being executed as passed in the environment
6035or argument list.
6036Subsequently, expands to the last argument to the previous simple
6037command executed in the foreground, after expansion.
6038Also set to the full pathname used to invoke each command executed
6039and placed in the environment exported to that command.
6040When checking mail, this parameter holds the name of the mail file.
6041
bb70624e
JA
6042@item BASH
6043The full pathname used to execute the current instance of Bash.
cce855bc 6044
0001803f
CR
6045@item BASHOPTS
6046A colon-separated list of enabled shell options. Each word in
6047the list is a valid argument for the @option{-s} option to the
6048@code{shopt} builtin command (@pxref{The Shopt Builtin}).
6049The options appearing in @env{BASHOPTS} are those reported
6050as @samp{on} by @samp{shopt}.
6051If this variable is in the environment when Bash
6052starts up, each shell option in the list will be enabled before
6053reading any startup files. This variable is readonly.
6054
3185942a 6055@item BASHPID
495aee44 6056Expands to the process ID of the current Bash process.
3185942a
JA
6057This differs from @code{$$} under certain circumstances, such as subshells
6058that do not require Bash to be re-initialized.
d233b485 6059Assignments to @env{BASHPID} have no effect.
8868edaf 6060If @env{BASHPID}
d233b485
CR
6061is unset, it loses its special properties, even if it is
6062subsequently reset.
3185942a
JA
6063
6064@item BASH_ALIASES
6065An associative array variable whose members correspond to the internal
495aee44 6066list of aliases as maintained by the @code{alias} builtin.
3185942a 6067(@pxref{Bourne Shell Builtins}).
a0c0a00f
CR
6068Elements added to this array appear in the alias list; however,
6069unsetting array elements currently does not cause aliases to be removed
6070from the alias list.
8868edaf 6071If @env{BASH_ALIASES}
a0c0a00f
CR
6072is unset, it loses its special properties, even if it is
6073subsequently reset.
3185942a 6074
b80f6443
JA
6075@item BASH_ARGC
6076An array variable whose values are the number of parameters in each
6077frame of the current bash execution call stack. The number of
6078parameters to the current subroutine (shell function or script executed
6079with @code{.} or @code{source}) is at the top of the stack. When a
6080subroutine is executed, the number of parameters passed is pushed onto
6081@code{BASH_ARGC}.
95732b49 6082The shell sets @code{BASH_ARGC} only when in extended debugging mode
3185942a 6083(see @ref{The Shopt Builtin}
95732b49
JA
6084for a description of the @code{extdebug} option to the @code{shopt}
6085builtin).
d233b485
CR
6086Setting @code{extdebug} after the shell has started to execute a script,
6087or referencing this variable when @code{extdebug} is not set,
6088may result in inconsistent values.
b80f6443
JA
6089
6090@item BASH_ARGV
6091An array variable containing all of the parameters in the current bash
6092execution call stack. The final parameter of the last subroutine call
6093is at the top of the stack; the first parameter of the initial call is
6094at the bottom. When a subroutine is executed, the parameters supplied
6095are pushed onto @code{BASH_ARGV}.
95732b49 6096The shell sets @code{BASH_ARGV} only when in extended debugging mode
3185942a 6097(see @ref{The Shopt Builtin}
95732b49
JA
6098for a description of the @code{extdebug} option to the @code{shopt}
6099builtin).
d233b485
CR
6100Setting @code{extdebug} after the shell has started to execute a script,
6101or referencing this variable when @code{extdebug} is not set,
6102may result in inconsistent values.
6103
6104@item BASH_ARGV0
6105When referenced, this variable expands to the name of the shell or shell
6106script (identical to @code{$0}; @xref{Special Parameters},
6107for the description of special parameter 0).
6108Assignment to @code{BASH_ARGV0}
6109causes the value assigned to also be assigned to @code{$0}.
8868edaf 6110If @env{BASH_ARGV0}
d233b485
CR
6111is unset, it loses its special properties, even if it is
6112subsequently reset.
b80f6443 6113
3185942a
JA
6114@item BASH_CMDS
6115An associative array variable whose members correspond to the internal
6116hash table of commands as maintained by the @code{hash} builtin
6117(@pxref{Bourne Shell Builtins}).
a0c0a00f
CR
6118Elements added to this array appear in the hash table; however,
6119unsetting array elements currently does not cause command names to be removed
6120from the hash table.
8868edaf 6121If @env{BASH_CMDS}
a0c0a00f
CR
6122is unset, it loses its special properties, even if it is
6123subsequently reset.
3185942a 6124
b80f6443
JA
6125@item BASH_COMMAND
6126The command currently being executed or about to be executed, unless the
6127shell is executing a command as the result of a trap,
6128in which case it is the command executing at the time of the trap.
8868edaf
CR
6129If @env{BASH_COMMAND}
6130is unset, it loses its special properties, even if it is
6131subsequently reset.
b80f6443 6132
ac50fbac
CR
6133@item BASH_COMPAT
6134The value is used to set the shell's compatibility level.
8868edaf
CR
6135@xref{Shell Compatibility Mode}, for a description of the various
6136compatibility levels and their effects.
ac50fbac
CR
6137The value may be a decimal number (e.g., 4.2) or an integer (e.g., 42)
6138corresponding to the desired compatibility level.
8868edaf 6139If @env{BASH_COMPAT} is unset or set to the empty string, the compatibility
ac50fbac 6140level is set to the default for the current version.
8868edaf 6141If @env{BASH_COMPAT} is set to a value that is not one of the valid
ac50fbac
CR
6142compatibility levels, the shell prints an error message and sets the
6143compatibility level to the default for the current version.
8868edaf
CR
6144The valid values correspond to the compatibility levels
6145described below (@pxref{Shell Compatibility Mode}).
6146For example, 4.2 and 42 are valid values that correspond
6147to the @code{compat42} @code{shopt} option
6148and set the compatibility level to 42.
ac50fbac
CR
6149The current version is also a valid value.
6150
bb70624e
JA
6151@item BASH_ENV
6152If this variable is set when Bash is invoked to execute a shell
6153script, its value is expanded and used as the name of a startup file
6154to read before executing the script. @xref{Bash Startup Files}.
ccc6cda3 6155
b80f6443
JA
6156@item BASH_EXECUTION_STRING
6157The command argument to the @option{-c} invocation option.
6158
6159@item BASH_LINENO
6160An array variable whose members are the line numbers in source files
74091dd4 6161where each corresponding member of @env{FUNCNAME} was invoked.
495aee44
CR
6162@code{$@{BASH_LINENO[$i]@}} is the line number in the source file
6163(@code{$@{BASH_SOURCE[$i+1]@}}) where
3185942a 6164@code{$@{FUNCNAME[$i]@}} was called (or @code{$@{BASH_LINENO[$i-1]@}} if
495aee44 6165referenced within another shell function).
b80f6443
JA
6166Use @code{LINENO} to obtain the current line number.
6167
a0c0a00f
CR
6168@item BASH_LOADABLES_PATH
6169A colon-separated list of directories in which the shell looks for
6170dynamically loadable builtins specified by the
6171@code{enable} command.
6172
b80f6443
JA
6173@item BASH_REMATCH
6174An array variable whose members are assigned by the @samp{=~} binary
6175operator to the @code{[[} conditional command
6176(@pxref{Conditional Constructs}).
6177The element with index 0 is the portion of the string
6178matching the entire regular expression.
6179The element with index @var{n} is the portion of the
6180string matching the @var{n}th parenthesized subexpression.
b80f6443
JA
6181
6182@item BASH_SOURCE
495aee44
CR
6183An array variable whose members are the source filenames where the
6184corresponding shell function names in the @code{FUNCNAME} array
6185variable are defined.
6186The shell function @code{$@{FUNCNAME[$i]@}} is defined in the file
6187@code{$@{BASH_SOURCE[$i]@}} and called from @code{$@{BASH_SOURCE[$i+1]@}}
b80f6443
JA
6188
6189@item BASH_SUBSHELL
ac50fbac
CR
6190Incremented by one within each subshell or subshell environment when
6191the shell begins executing in that environment.
b80f6443 6192The initial value is 0.
8868edaf
CR
6193If @env{BASH_SUBSHELL}
6194is unset, it loses its special properties, even if it is
6195subsequently reset.
ccc6cda3 6196
bb70624e
JA
6197@item BASH_VERSINFO
6198A readonly array variable (@pxref{Arrays})
6199whose members hold version information for this instance of Bash.
6200The values assigned to the array members are as follows:
ccc6cda3 6201
bb70624e 6202@table @code
ccc6cda3 6203
bb70624e 6204@item BASH_VERSINFO[0]
74091dd4 6205The major version number (the @dfn{release}).
cce855bc 6206
bb70624e 6207@item BASH_VERSINFO[1]
74091dd4 6208The minor version number (the @dfn{version}).
ccc6cda3 6209
bb70624e
JA
6210@item BASH_VERSINFO[2]
6211The patch level.
ccc6cda3 6212
bb70624e
JA
6213@item BASH_VERSINFO[3]
6214The build version.
ccc6cda3 6215
bb70624e 6216@item BASH_VERSINFO[4]
74091dd4 6217The release status (e.g., @code{beta1}).
cce855bc 6218
bb70624e 6219@item BASH_VERSINFO[5]
28ef6c31 6220The value of @env{MACHTYPE}.
bb70624e 6221@end table
ccc6cda3 6222
b80f6443
JA
6223@item BASH_VERSION
6224The version number of the current instance of Bash.
6225
0001803f
CR
6226@item BASH_XTRACEFD
6227If set to an integer corresponding to a valid file descriptor, Bash
6228will write the trace output generated when @samp{set -x}
6229is enabled to that file descriptor.
6230This allows tracing output to be separated from diagnostic and error
6231messages.
6232The file descriptor is closed when @code{BASH_XTRACEFD} is unset or assigned
6233a new value.
6234Unsetting @code{BASH_XTRACEFD} or assigning it the empty string causes the
6235trace output to be sent to the standard error.
6236Note that setting @code{BASH_XTRACEFD} to 2 (the standard error file
6237descriptor) and then unsetting it will result in the standard error
6238being closed.
6239
ac50fbac
CR
6240@item CHILD_MAX
6241Set the number of exited child status values for the shell to remember.
6242Bash will not allow this value to be decreased below a @sc{posix}-mandated
6243minimum, and there is a maximum value (currently 8192) that this may
6244not exceed.
6245The minimum value is system-dependent.
6246
f73dda09 6247@item COLUMNS
495aee44 6248Used by the @code{select} command to determine the terminal width
ac50fbac
CR
6249when printing selection lists.
6250Automatically set if the @code{checkwinsize} option is enabled
6251(@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
f73dda09 6252@code{SIGWINCH}.
bb70624e
JA
6253
6254@item COMP_CWORD
28ef6c31 6255An index into @env{$@{COMP_WORDS@}} of the word containing the current
bb70624e
JA
6256cursor position.
6257This variable is available only in shell functions invoked by the
6258programmable completion facilities (@pxref{Programmable Completion}).
6259
6260@item COMP_LINE
6261The current command line.
6262This variable is available only in shell functions and external
6263commands invoked by the
6264programmable completion facilities (@pxref{Programmable Completion}).
6265
6266@item COMP_POINT
6267The index of the current cursor position relative to the beginning of
6268the current command.
6269If the current cursor position is at the end of the current command,
6270the value of this variable is equal to @code{$@{#COMP_LINE@}}.
6271This variable is available only in shell functions and external
6272commands invoked by the
6273programmable completion facilities (@pxref{Programmable Completion}).
6274
3185942a
JA
6275@item COMP_TYPE
6276Set to an integer value corresponding to the type of completion attempted
6277that caused a completion function to be called:
74091dd4 6278@key{TAB}, for normal completion,
3185942a
JA
6279@samp{?}, for listing completions after successive tabs,
6280@samp{!}, for listing alternatives on partial word completion,
6281@samp{@@}, to list completions if the word is not unmodified,
6282or
6283@samp{%}, for menu completion.
6284This variable is available only in shell functions and external
6285commands invoked by the
6286programmable completion facilities (@pxref{Programmable Completion}).
6287
6288@item COMP_KEY
6289The key (or final key of a key sequence) used to invoke the current
6290completion function.
6291
b80f6443
JA
6292@item COMP_WORDBREAKS
6293The set of characters that the Readline library treats as word
6294separators when performing word completion.
8868edaf
CR
6295If @env{COMP_WORDBREAKS}
6296is unset, it loses its special properties,
b80f6443
JA
6297even if it is subsequently reset.
6298
f73dda09
JA
6299@item COMP_WORDS
6300An array variable consisting of the individual
6301words in the current command line.
17345e5a
JA
6302The line is split into words as Readline would split it, using
6303@code{COMP_WORDBREAKS} as described above.
f73dda09
JA
6304This variable is available only in shell functions invoked by the
6305programmable completion facilities (@pxref{Programmable Completion}).
6306
bb70624e
JA
6307@item COMPREPLY
6308An array variable from which Bash reads the possible completions
6309generated by a shell function invoked by the programmable completion
6310facility (@pxref{Programmable Completion}).
ac50fbac 6311Each array element contains one possible completion.
b72432fd 6312
495aee44
CR
6313@item COPROC
6314An array variable created to hold the file descriptors
6315for output from and input to an unnamed coprocess (@pxref{Coprocesses}).
6316
bb70624e
JA
6317@item DIRSTACK
6318An array variable containing the current contents of the directory stack.
6319Directories appear in the stack in the order they are displayed by the
6320@code{dirs} builtin.
6321Assigning to members of this array variable may be used to modify
6322directories already in the stack, but the @code{pushd} and @code{popd}
6323builtins must be used to add and remove directories.
6324Assignment to this variable will not change the current directory.
8868edaf
CR
6325If @env{DIRSTACK}
6326is unset, it loses its special properties, even if
bb70624e 6327it is subsequently reset.
ccc6cda3 6328
b80f6443
JA
6329@item EMACS
6330If Bash finds this variable in the environment when the shell
6331starts with value @samp{t}, it assumes that the shell is running in an
495aee44
CR
6332Emacs shell buffer and disables line editing.
6333
6334@item ENV
74091dd4 6335Expanded and executed similarly to @code{BASH_ENV}
8868edaf
CR
6336(@pxref{Bash Startup Files})
6337when an interactive shell is invoked in
495aee44 6338@sc{posix} Mode (@pxref{Bash POSIX Mode}).
b80f6443 6339
d233b485
CR
6340@item EPOCHREALTIME
6341Each time this parameter is referenced, it expands to the number of seconds
6342since the Unix Epoch as a floating point value with micro-second granularity
74091dd4 6343(see the documentation for the C library function @code{time} for the
d233b485
CR
6344definition of Epoch).
6345Assignments to @env{EPOCHREALTIME} are ignored.
8868edaf
CR
6346If @env{EPOCHREALTIME}
6347is unset, it loses its special properties, even if
d233b485
CR
6348it is subsequently reset.
6349
6350@item EPOCHSECONDS
6351Each time this parameter is referenced, it expands to the number of seconds
6352since the Unix Epoch (see the documentation for the C library function
74091dd4 6353@code{time} for the definition of Epoch).
d233b485 6354Assignments to @env{EPOCHSECONDS} are ignored.
8868edaf
CR
6355If @env{EPOCHSECONDS}
6356is unset, it loses its special properties, even if
d233b485
CR
6357it is subsequently reset.
6358
bb70624e
JA
6359@item EUID
6360The numeric effective user id of the current user. This variable
6361is readonly.
ccc6cda3 6362
a0c0a00f
CR
6363@item EXECIGNORE
6364A colon-separated list of shell patterns (@pxref{Pattern Matching})
6365defining the list of filenames to be ignored by command search using
6366@code{PATH}.
6367Files whose full pathnames match one of these patterns are not considered
6368executable files for the purposes of completion and command execution
6369via @code{PATH} lookup.
6370This does not affect the behavior of the @code{[}, @code{test}, and @code{[[}
6371commands.
6372Full pathnames in the command hash table are not subject to @code{EXECIGNORE}.
6373Use this variable to ignore shared library files that have the executable
6374bit set, but are not executable files.
6375The pattern matching honors the setting of the @code{extglob} shell
6376option.
6377
bb70624e 6378@item FCEDIT
28ef6c31 6379The editor used as a default by the @option{-e} option to the @code{fc}
bb70624e 6380builtin command.
cce855bc 6381
bb70624e
JA
6382@item FIGNORE
6383A colon-separated list of suffixes to ignore when performing
6384filename completion.
ac50fbac 6385A filename whose suffix matches one of the entries in
28ef6c31 6386@env{FIGNORE}
ac50fbac 6387is excluded from the list of matched filenames. A sample
bb70624e 6388value is @samp{.o:~}
cce855bc 6389
f73dda09 6390@item FUNCNAME
b80f6443
JA
6391An array variable containing the names of all shell functions
6392currently in the execution call stack.
6393The element with index 0 is the name of any currently-executing
6394shell function.
495aee44
CR
6395The bottom-most element (the one with the highest index)
6396is @code{"main"}.
f73dda09 6397This variable exists only when a shell function is executing.
a0c0a00f 6398Assignments to @env{FUNCNAME} have no effect.
8868edaf
CR
6399If @env{FUNCNAME}
6400is unset, it loses its special properties, even if
f73dda09
JA
6401it is subsequently reset.
6402
495aee44
CR
6403This variable can be used with @code{BASH_LINENO} and @code{BASH_SOURCE}.
6404Each element of @code{FUNCNAME} has corresponding elements in
6405@code{BASH_LINENO} and @code{BASH_SOURCE} to describe the call stack.
6406For instance, @code{$@{FUNCNAME[$i]@}} was called from the file
6407@code{$@{BASH_SOURCE[$i+1]@}} at line number @code{$@{BASH_LINENO[$i]@}}.
6408The @code{caller} builtin displays the current call stack using this
6409information.
6410
6411@item FUNCNEST
6412If set to a numeric value greater than 0, defines a maximum function
6413nesting level. Function invocations that exceed this nesting level
6414will cause the current command to abort.
6415
bb70624e 6416@item GLOBIGNORE
d233b485 6417A colon-separated list of patterns defining the set of file names to
bb70624e 6418be ignored by filename expansion.
d233b485 6419If a file name matched by a filename expansion pattern also matches one
28ef6c31 6420of the patterns in @env{GLOBIGNORE}, it is removed from the list
bb70624e 6421of matches.
a0c0a00f
CR
6422The pattern matching honors the setting of the @code{extglob} shell
6423option.
ccc6cda3 6424
bb70624e
JA
6425@item GROUPS
6426An array variable containing the list of groups of which the current
6427user is a member.
a0c0a00f 6428Assignments to @env{GROUPS} have no effect.
8868edaf
CR
6429If @env{GROUPS}
6430is unset, it loses its special properties, even if it is
bb70624e 6431subsequently reset.
ccc6cda3 6432
bb70624e
JA
6433@item histchars
6434Up to three characters which control history expansion, quick
6435substitution, and tokenization (@pxref{History Interaction}).
6436The first character is the
74091dd4 6437@dfn{history expansion} character, that is, the character which signifies the
bb70624e
JA
6438start of a history expansion, normally @samp{!}. The second character is the
6439character which signifies `quick substitution' when seen as the first
6440character on a line, normally @samp{^}. The optional third character is the
6441character which indicates that the remainder of the line is a comment when
6442found as the first character of a word, usually @samp{#}. The history
6443comment character causes history substitution to be skipped for the
6444remaining words on the line. It does not necessarily cause the shell
6445parser to treat the rest of the line as a comment.
ccc6cda3 6446
bb70624e
JA
6447@item HISTCMD
6448The history number, or index in the history list, of the current
8868edaf
CR
6449command.
6450Assignments to @env{HISTCMD} are ignored.
6451If @env{HISTCMD}
6452is unset, it loses its special properties,
bb70624e 6453even if it is subsequently reset.
ccc6cda3 6454
bb70624e 6455@item HISTCONTROL
b80f6443
JA
6456A colon-separated list of values controlling how commands are saved on
6457the history list.
6458If the list of values includes @samp{ignorespace}, lines which begin
6459with a space character are not saved in the history list.
6460A value of @samp{ignoredups} causes lines which match the previous
6461history entry to not be saved.
6462A value of @samp{ignoreboth} is shorthand for
6463@samp{ignorespace} and @samp{ignoredups}.
6464A value of @samp{erasedups} causes all previous lines matching the
6465current line to be removed from the history list before that line
6466is saved.
6467Any value not in the above list is ignored.
6468If @env{HISTCONTROL} is unset, or does not include a valid value,
6469all lines read by the shell parser are saved on the history list,
6470subject to the value of @env{HISTIGNORE}.
bb70624e
JA
6471The second and subsequent lines of a multi-line compound command are
6472not tested, and are added to the history regardless of the value of
28ef6c31 6473@env{HISTCONTROL}.
ccc6cda3 6474
f73dda09
JA
6475@item HISTFILE
6476The name of the file to which the command history is saved. The
6477default value is @file{~/.bash_history}.
6478
6479@item HISTFILESIZE
ac50fbac
CR
6480The maximum number of lines contained in the history file.
6481When this variable is assigned a value, the history file is truncated,
6482if necessary, to contain no more than that number of lines
6483by removing the oldest entries.
f73dda09 6484The history file is also truncated to this size after
ac50fbac
CR
6485writing it when a shell exits.
6486If the value is 0, the history file is truncated to zero size.
6487Non-numeric values and numeric values less than zero inhibit truncation.
6488The shell sets the default value to the value of @env{HISTSIZE}
6489after reading any startup files.
f73dda09 6490
bb70624e
JA
6491@item HISTIGNORE
6492A colon-separated list of patterns used to decide which command
6493lines should be saved on the history list. Each pattern is
6494anchored at the beginning of the line and must match the complete
6495line (no implicit @samp{*} is appended). Each pattern is tested
28ef6c31 6496against the line after the checks specified by @env{HISTCONTROL}
bb70624e
JA
6497are applied. In addition to the normal shell pattern matching
6498characters, @samp{&} matches the previous history line. @samp{&}
6499may be escaped using a backslash; the backslash is removed
6500before attempting a match.
6501The second and subsequent lines of a multi-line compound command are
6502not tested, and are added to the history regardless of the value of
28ef6c31 6503@env{HISTIGNORE}.
a0c0a00f
CR
6504The pattern matching honors the setting of the @code{extglob} shell
6505option.
ccc6cda3 6506
28ef6c31 6507@env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A
bb70624e
JA
6508pattern of @samp{&} is identical to @code{ignoredups}, and a
6509pattern of @samp{[ ]*} is identical to @code{ignorespace}.
6510Combining these two patterns, separating them with a colon,
6511provides the functionality of @code{ignoreboth}.
ccc6cda3 6512
bb70624e
JA
6513@item HISTSIZE
6514The maximum number of commands to remember on the history list.
ac50fbac
CR
6515If the value is 0, commands are not saved in the history list.
6516Numeric values less than zero result in every command being saved
6517on the history list (there is no limit).
6518The shell sets the default value to 500 after reading any startup files.
ccc6cda3 6519
b80f6443
JA
6520@item HISTTIMEFORMAT
6521If this variable is set and not null, its value is used as a format string
74091dd4 6522for @code{strftime} to print the time stamp associated with each history
b80f6443
JA
6523entry displayed by the @code{history} builtin.
6524If this variable is set, time stamps are written to the history file so
6525they may be preserved across shell sessions.
3185942a
JA
6526This uses the history comment character to distinguish timestamps from
6527other history lines.
b80f6443 6528
bb70624e
JA
6529@item HOSTFILE
6530Contains the name of a file in the same format as @file{/etc/hosts} that
6531should be read when the shell needs to complete a hostname.
6532The list of possible hostname completions may be changed while the shell
6533is running;
6534the next time hostname completion is attempted after the
6535value is changed, Bash adds the contents of the new file to the
6536existing list.
0001803f
CR
6537If @env{HOSTFILE} is set, but has no value, or does not name a readable file,
6538Bash attempts to read
bb70624e 6539@file{/etc/hosts} to obtain the list of possible hostname completions.
28ef6c31 6540When @env{HOSTFILE} is unset, the hostname list is cleared.
ccc6cda3 6541
bb70624e
JA
6542@item HOSTNAME
6543The name of the current host.
ccc6cda3 6544
bb70624e
JA
6545@item HOSTTYPE
6546A string describing the machine Bash is running on.
ccc6cda3 6547
bb70624e
JA
6548@item IGNOREEOF
6549Controls the action of the shell on receipt of an @code{EOF} character
6550as the sole input. If set, the value denotes the number
6551of consecutive @code{EOF} characters that can be read as the
6552first character on an input line
6553before the shell will exit. If the variable exists but does not
d233b485 6554have a numeric value, or has no value, then the default is 10.
bb70624e
JA
6555If the variable does not exist, then @code{EOF} signifies the end of
6556input to the shell. This is only in effect for interactive shells.
ccc6cda3 6557
bb70624e
JA
6558@item INPUTRC
6559The name of the Readline initialization file, overriding the default
6560of @file{~/.inputrc}.
ccc6cda3 6561
d233b485
CR
6562@item INSIDE_EMACS
6563If Bash finds this variable in the environment when the shell
6564starts, it assumes that the shell is running in an Emacs shell buffer
6565and may disable line editing depending on the value of @env{TERM}.
6566
bb70624e
JA
6567@item LANG
6568Used to determine the locale category for any category not specifically
6569selected with a variable starting with @code{LC_}.
ccc6cda3 6570
bb70624e 6571@item LC_ALL
28ef6c31 6572This variable overrides the value of @env{LANG} and any other
bb70624e 6573@code{LC_} variable specifying a locale category.
ccc6cda3 6574
bb70624e
JA
6575@item LC_COLLATE
6576This variable determines the collation order used when sorting the
6577results of filename expansion, and
6578determines the behavior of range expressions, equivalence classes,
6579and collating sequences within filename expansion and pattern matching
6580(@pxref{Filename Expansion}).
ccc6cda3 6581
bb70624e
JA
6582@item LC_CTYPE
6583This variable determines the interpretation of characters and the
6584behavior of character classes within filename expansion and pattern
6585matching (@pxref{Filename Expansion}).
ccc6cda3 6586
bb70624e
JA
6587@item LC_MESSAGES
6588This variable determines the locale used to translate double-quoted
6589strings preceded by a @samp{$} (@pxref{Locale Translation}).
ccc6cda3 6590
bb70624e
JA
6591@item LC_NUMERIC
6592This variable determines the locale category used for number formatting.
ccc6cda3 6593
a0c0a00f
CR
6594@item LC_TIME
6595This variable determines the locale category used for data and time
6596formatting.
6597
f73dda09
JA
6598@item LINENO
6599The line number in the script or shell function currently executing.
8868edaf
CR
6600If @env{LINENO}
6601is unset, it loses its special properties, even if it is
6602subsequently reset.
f73dda09 6603
28ef6c31 6604@item LINES
495aee44 6605Used by the @code{select} command to determine the column length
ac50fbac
CR
6606for printing selection lists.
6607Automatically set if the @code{checkwinsize} option is enabled
6608(@pxref{The Shopt Builtin}), or in an interactive shell upon receipt of a
28ef6c31
JA
6609@code{SIGWINCH}.
6610
bb70624e
JA
6611@item MACHTYPE
6612A string that fully describes the system type on which Bash
6613is executing, in the standard @sc{gnu} @var{cpu-company-system} format.
ccc6cda3 6614
bb70624e
JA
6615@item MAILCHECK
6616How often (in seconds) that the shell should check for mail in the
28ef6c31
JA
6617files specified in the @env{MAILPATH} or @env{MAIL} variables.
6618The default is 60 seconds. When it is time to check
6619for mail, the shell does so before displaying the primary prompt.
6620If this variable is unset, or set to a value that is not a number
6621greater than or equal to zero, the shell disables mail checking.
ccc6cda3 6622
495aee44
CR
6623@item MAPFILE
6624An array variable created to hold the text read by the
6625@code{mapfile} builtin when no variable name is supplied.
6626
bb70624e
JA
6627@item OLDPWD
6628The previous working directory as set by the @code{cd} builtin.
ccc6cda3 6629
bb70624e
JA
6630@item OPTERR
6631If set to the value 1, Bash displays error messages
6632generated by the @code{getopts} builtin command.
ccc6cda3 6633
bb70624e
JA
6634@item OSTYPE
6635A string describing the operating system Bash is running on.
cce855bc 6636
bb70624e
JA
6637@item PIPESTATUS
6638An array variable (@pxref{Arrays})
6639containing a list of exit status values from the processes
6640in the most-recently-executed foreground pipeline (which may
6641contain only a single command).
cce855bc 6642
f73dda09 6643@item POSIXLY_CORRECT
ac50fbac 6644If this variable is in the environment when Bash starts, the shell
f73dda09
JA
6645enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the
6646startup files, as if the @option{--posix} invocation option had been supplied.
ac50fbac 6647If it is set while the shell is running, Bash enables @sc{posix} mode,
f73dda09
JA
6648as if the command
6649@example
6650@code{set -o posix}
6651@end example
6652@noindent
d233b485
CR
6653had been executed.
6654When the shell enters @sc{posix} mode, it sets this variable if it was
6655not already set.
f73dda09 6656
bb70624e
JA
6657@item PPID
6658The process @sc{id} of the shell's parent process. This variable
6659is readonly.
ccc6cda3 6660
bb70624e 6661@item PROMPT_COMMAND
8868edaf
CR
6662If this variable is set, and is an array,
6663the value of each set element is interpreted as a command to execute
6664before printing the primary prompt (@env{$PS1}).
6665If this is set but not an array variable,
6666its value is used as a command to execute instead.
ccc6cda3 6667
3185942a
JA
6668@item PROMPT_DIRTRIM
6669If set to a number greater than zero, the value is used as the number of
6670trailing directory components to retain when expanding the @code{\w} and
ac50fbac 6671@code{\W} prompt string escapes (@pxref{Controlling the Prompt}).
3185942a
JA
6672Characters removed are replaced with an ellipsis.
6673
a0c0a00f 6674@item PS0
8868edaf 6675The value of this parameter is expanded like @env{PS1}
a0c0a00f
CR
6676and displayed by interactive shells after reading a command
6677and before the command is executed.
6678
bb70624e
JA
6679@item PS3
6680The value of this variable is used as the prompt for the
6681@code{select} command. If this variable is not set, the
6682@code{select} command prompts with @samp{#? }
ccc6cda3 6683
bb70624e 6684@item PS4
74091dd4 6685The value of this parameter is expanded like @env{PS1}
d233b485
CR
6686and the expanded value is the prompt printed before the command line
6687is echoed when the @option{-x} option is set (@pxref{The Set Builtin}).
6688The first character of the expanded value is replicated multiple times,
6689as necessary, to indicate multiple levels of indirection.
bb70624e 6690The default is @samp{+ }.
ccc6cda3 6691
bb70624e
JA
6692@item PWD
6693The current working directory as set by the @code{cd} builtin.
ccc6cda3 6694
bb70624e 6695@item RANDOM
8868edaf
CR
6696Each time this parameter is referenced, it expands to a random integer
6697between 0 and 32767. Assigning a value to this
bb70624e 6698variable seeds the random number generator.
8868edaf
CR
6699If @env{RANDOM}
6700is unset, it loses its special properties, even if it is
6701subsequently reset.
ccc6cda3 6702
74091dd4
CR
6703@item READLINE_ARGUMENT
6704Any numeric argument given to a Readline command that was defined using
6705@samp{bind -x} (@pxref{Bash Builtins}
6706when it was invoked.
6707
495aee44
CR
6708@item READLINE_LINE
6709The contents of the Readline line buffer, for use
6710with @samp{bind -x} (@pxref{Bash Builtins}).
6711
8868edaf 6712@item READLINE_MARK
74091dd4 6713The position of the @dfn{mark} (saved insertion point) in the
8868edaf
CR
6714Readline line buffer, for use
6715with @samp{bind -x} (@pxref{Bash Builtins}).
6716The characters between the insertion point and the mark are often
74091dd4 6717called the @dfn{region}.
8868edaf 6718
495aee44
CR
6719@item READLINE_POINT
6720The position of the insertion point in the Readline line buffer, for use
6721with @samp{bind -x} (@pxref{Bash Builtins}).
6722
bb70624e
JA
6723@item REPLY
6724The default variable for the @code{read} builtin.
ccc6cda3 6725
bb70624e 6726@item SECONDS
74091dd4
CR
6727This variable expands to the number of seconds since the shell was started.
6728Assignment to this variable resets the count to the value assigned, and the
6729expanded value becomes the value assigned plus the number of seconds
bb70624e 6730since the assignment.
74091dd4 6731The number of seconds at shell invocation and the current time are always
8868edaf
CR
6732determined by querying the system clock.
6733If @env{SECONDS}
6734is unset, it loses its special properties,
6735even if it is subsequently reset.
ccc6cda3 6736
b80f6443 6737@item SHELL
8868edaf 6738This environment variable expands to the full pathname to the shell.
b80f6443
JA
6739If it is not set when the shell starts,
6740Bash assigns to it the full pathname of the current user's login shell.
6741
bb70624e
JA
6742@item SHELLOPTS
6743A colon-separated list of enabled shell options. Each word in
28ef6c31 6744the list is a valid argument for the @option{-o} option to the
bb70624e 6745@code{set} builtin command (@pxref{The Set Builtin}).
28ef6c31 6746The options appearing in @env{SHELLOPTS} are those reported
bb70624e
JA
6747as @samp{on} by @samp{set -o}.
6748If this variable is in the environment when Bash
6749starts up, each shell option in the list will be enabled before
6750reading any startup files. This variable is readonly.
ccc6cda3 6751
bb70624e
JA
6752@item SHLVL
6753Incremented by one each time a new instance of Bash is started. This is
6754intended to be a count of how deeply your Bash shells are nested.
ccc6cda3 6755
8868edaf
CR
6756@item SRANDOM
6757This variable expands to a 32-bit pseudo-random number each time it is
6758referenced. The random number generator is not linear on systems that
6759support @file{/dev/urandom} or @code{arc4random}, so each returned number
6760has no relationship to the numbers preceding it.
6761The random number generator cannot be seeded, so assignments to this
6762variable have no effect.
6763If @env{SRANDOM}
6764is unset, it loses its special properties,
6765even if it is subsequently reset.
6766
bb70624e
JA
6767@item TIMEFORMAT
6768The value of this parameter is used as a format string specifying
6769how the timing information for pipelines prefixed with the @code{time}
6770reserved word should be displayed.
6771The @samp{%} character introduces an
6772escape sequence that is expanded to a time value or other
6773information.
6774The escape sequences and their meanings are as
6775follows; the braces denote optional portions.
ccc6cda3 6776
bb70624e 6777@table @code
ccc6cda3 6778
bb70624e
JA
6779@item %%
6780A literal @samp{%}.
ccc6cda3 6781
bb70624e
JA
6782@item %[@var{p}][l]R
6783The elapsed time in seconds.
ccc6cda3 6784
bb70624e
JA
6785@item %[@var{p}][l]U
6786The number of CPU seconds spent in user mode.
ccc6cda3 6787
bb70624e
JA
6788@item %[@var{p}][l]S
6789The number of CPU seconds spent in system mode.
ccc6cda3 6790
bb70624e
JA
6791@item %P
6792The CPU percentage, computed as (%U + %S) / %R.
6793@end table
ccc6cda3 6794
bb70624e
JA
6795The optional @var{p} is a digit specifying the precision, the number of
6796fractional digits after a decimal point.
6797A value of 0 causes no decimal point or fraction to be output.
6798At most three places after the decimal point may be specified; values
6799of @var{p} greater than 3 are changed to 3.
6800If @var{p} is not specified, the value 3 is used.
ccc6cda3 6801
bb70624e
JA
6802The optional @code{l} specifies a longer format, including minutes, of
6803the form @var{MM}m@var{SS}.@var{FF}s.
6804The value of @var{p} determines whether or not the fraction is included.
ccc6cda3 6805
bb70624e
JA
6806If this variable is not set, Bash acts as if it had the value
6807@example
6808@code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'}
6809@end example
6810If the value is null, no timing information is displayed.
6811A trailing newline is added when the format string is displayed.
ccc6cda3 6812
bb70624e 6813@item TMOUT
7117c2d2
JA
6814If set to a value greater than zero, @code{TMOUT} is treated as the
6815default timeout for the @code{read} builtin (@pxref{Bash Builtins}).
6816The @code{select} command (@pxref{Conditional Constructs}) terminates
6817if input does not arrive after @code{TMOUT} seconds when input is coming
6818from a terminal.
6819
3185942a 6820In an interactive shell, the value is interpreted as
ac50fbac
CR
6821the number of seconds to wait for a line of input after issuing
6822the primary prompt.
6823Bash
6824terminates after waiting for that number of seconds if a complete
6825line of input does not arrive.
ccc6cda3 6826
95732b49
JA
6827@item TMPDIR
6828If set, Bash uses its value as the name of a directory in which
6829Bash creates temporary files for the shell's use.
6830
bb70624e
JA
6831@item UID
6832The numeric real user id of the current user. This variable is readonly.
ccc6cda3 6833
bb70624e 6834@end vtable
ccc6cda3 6835
bb70624e
JA
6836@node Bash Features
6837@chapter Bash Features
ccc6cda3 6838
ac50fbac 6839This chapter describes features unique to Bash.
ccc6cda3 6840
bb70624e
JA
6841@menu
6842* Invoking Bash:: Command line options that you can give
6843 to Bash.
6844* Bash Startup Files:: When and how Bash executes scripts.
6845* Interactive Shells:: What an interactive shell is.
6846* Bash Conditional Expressions:: Primitives used in composing expressions for
6847 the @code{test} builtin.
6848* Shell Arithmetic:: Arithmetic on shell variables.
6849* Aliases:: Substituting one command for another.
6850* Arrays:: Array Variables.
6851* The Directory Stack:: History of visited directories.
ac50fbac 6852* Controlling the Prompt:: Customizing the various prompt strings.
bb70624e
JA
6853* The Restricted Shell:: A more controlled mode of shell execution.
6854* Bash POSIX Mode:: Making Bash behave more closely to what
6855 the POSIX standard specifies.
8868edaf
CR
6856* Shell Compatibility Mode:: How Bash supports behavior that was present
6857 in earlier versions and has changed.
bb70624e 6858@end menu
ccc6cda3 6859
bb70624e
JA
6860@node Invoking Bash
6861@section Invoking Bash
ccc6cda3 6862
bb70624e 6863@example
d233b485
CR
6864bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6865 [-O @var{shopt_option}] [@var{argument} @dots{}]
6866bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6867 [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}]
6868bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}]
6869 [-O @var{shopt_option}] [@var{argument} @dots{}]
bb70624e 6870@end example
ccc6cda3 6871
495aee44
CR
6872All of the single-character options used with the @code{set} builtin
6873(@pxref{The Set Builtin}) can be used as options when the shell is invoked.
6874In addition, there are several multi-character
bb70624e 6875options that you can use. These options must appear on the command
7117c2d2 6876line before the single-character options to be recognized.
ccc6cda3 6877
bb70624e 6878@table @code
b80f6443
JA
6879@item --debugger
6880Arrange for the debugger profile to be executed before the shell
3185942a 6881starts. Turns on extended debugging mode (see @ref{The Shopt Builtin}
b80f6443 6882for a description of the @code{extdebug} option to the @code{shopt}
495aee44 6883builtin).
b80f6443 6884
bb70624e
JA
6885@item --dump-po-strings
6886A list of all double-quoted strings preceded by @samp{$}
95732b49 6887is printed on the standard output
bb70624e 6888in the @sc{gnu} @code{gettext} PO (portable object) file format.
28ef6c31 6889Equivalent to @option{-D} except for the output format.
ccc6cda3 6890
bb70624e 6891@item --dump-strings
28ef6c31 6892Equivalent to @option{-D}.
ccc6cda3 6893
bb70624e 6894@item --help
3185942a 6895Display a usage message on standard output and exit successfully.
ccc6cda3 6896
28ef6c31
JA
6897@item --init-file @var{filename}
6898@itemx --rcfile @var{filename}
6899Execute commands from @var{filename} (instead of @file{~/.bashrc})
6900in an interactive shell.
6901
bb70624e 6902@item --login
7117c2d2 6903Equivalent to @option{-l}.
ccc6cda3 6904
bb70624e
JA
6905@item --noediting
6906Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing})
6907to read command lines when the shell is interactive.
ccc6cda3 6908
bb70624e
JA
6909@item --noprofile
6910Don't load the system-wide startup file @file{/etc/profile}
6911or any of the personal initialization files
6912@file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile}
6913when Bash is invoked as a login shell.
ccc6cda3 6914
bb70624e
JA
6915@item --norc
6916Don't read the @file{~/.bashrc} initialization file in an
6917interactive shell. This is on by default if the shell is
6918invoked as @code{sh}.
ccc6cda3 6919
bb70624e
JA
6920@item --posix
6921Change the behavior of Bash where the default operation differs
0628567a 6922from the @sc{posix} standard to match the standard. This
bb70624e
JA
6923is intended to make Bash behave as a strict superset of that
6924standard. @xref{Bash POSIX Mode}, for a description of the Bash
6925@sc{posix} mode.
ccc6cda3 6926
bb70624e
JA
6927@item --restricted
6928Make the shell a restricted shell (@pxref{The Restricted Shell}).
ccc6cda3 6929
bb70624e 6930@item --verbose
28ef6c31 6931Equivalent to @option{-v}. Print shell input lines as they're read.
ccc6cda3 6932
bb70624e
JA
6933@item --version
6934Show version information for this instance of
6935Bash on the standard output and exit successfully.
ccc6cda3
JA
6936@end table
6937
bb70624e
JA
6938There are several single-character options that may be supplied at
6939invocation which are not available with the @code{set} builtin.
cce855bc 6940
bb70624e 6941@table @code
ac50fbac 6942@item -c
a0c0a00f
CR
6943Read and execute commands from the first non-option argument
6944@var{command_string}, then exit.
6945If there are arguments after the @var{command_string},
6946the first argument is assigned to @code{$0}
6947and any remaining arguments are assigned to the positional parameters.
6948The assignment to @code{$0} sets the name of the shell, which is used
6949in warning and error messages.
ccc6cda3 6950
bb70624e
JA
6951@item -i
6952Force the shell to run interactively. Interactive shells are
6953described in @ref{Interactive Shells}.
ccc6cda3 6954
7117c2d2
JA
6955@item -l
6956Make this shell act as if it had been directly invoked by login.
6957When the shell is interactive, this is equivalent to starting a
6958login shell with @samp{exec -l bash}.
6959When the shell is not interactive, the login shell startup files will
6960be executed.
6961@samp{exec bash -l} or @samp{exec bash --login}
6962will replace the current shell with a Bash login shell.
6963@xref{Bash Startup Files}, for a description of the special behavior
6964of a login shell.
6965
bb70624e
JA
6966@item -r
6967Make the shell a restricted shell (@pxref{The Restricted Shell}).
ccc6cda3 6968
bb70624e
JA
6969@item -s
6970If this option is present, or if no arguments remain after option
6971processing, then commands are read from the standard input.
6972This option allows the positional parameters to be set
d233b485
CR
6973when invoking an interactive shell or when reading input
6974through a pipe.
bb70624e
JA
6975
6976@item -D
6977A list of all double-quoted strings preceded by @samp{$}
95732b49 6978is printed on the standard output.
bb70624e
JA
6979These are the strings that
6980are subject to language translation when the current locale
6981is not @code{C} or @code{POSIX} (@pxref{Locale Translation}).
28ef6c31 6982This implies the @option{-n} option; no commands will be executed.
bb70624e 6983
f73dda09
JA
6984@item [-+]O [@var{shopt_option}]
6985@var{shopt_option} is one of the shell options accepted by the
3185942a 6986@code{shopt} builtin (@pxref{The Shopt Builtin}).
f73dda09
JA
6987If @var{shopt_option} is present, @option{-O} sets the value of that option;
6988@option{+O} unsets it.
6989If @var{shopt_option} is not supplied, the names and values of the shell
6990options accepted by @code{shopt} are printed on the standard output.
6991If the invocation option is @option{+O}, the output is displayed in a format
6992that may be reused as input.
6993
bb70624e
JA
6994@item --
6995A @code{--} signals the end of options and disables further option
6996processing.
6997Any arguments after the @code{--} are treated as filenames and arguments.
bb70624e
JA
6998@end table
6999
f73dda09
JA
7000@cindex login shell
7001A @emph{login} shell is one whose first character of argument zero is
7002@samp{-}, or one invoked with the @option{--login} option.
7003
bb70624e
JA
7004@cindex interactive shell
7005An @emph{interactive} shell is one started without non-option arguments,
28ef6c31
JA
7006unless @option{-s} is specified,
7007without specifying the @option{-c} option, and whose input and output are both
bb70624e 7008connected to terminals (as determined by @code{isatty(3)}), or one
28ef6c31 7009started with the @option{-i} option. @xref{Interactive Shells}, for more
bb70624e
JA
7010information.
7011
7012If arguments remain after option processing, and neither the
28ef6c31 7013@option{-c} nor the @option{-s}
bb70624e
JA
7014option has been supplied, the first argument is assumed to
7015be the name of a file containing shell commands (@pxref{Shell Scripts}).
7016When Bash is invoked in this fashion, @code{$0}
7017is set to the name of the file, and the positional parameters
7018are set to the remaining arguments.
7019Bash reads and executes commands from this file, then exits.
7020Bash's exit status is the exit status of the last command executed
7021in the script. If no commands are executed, the exit status is 0.
7022
7023@node Bash Startup Files
7024@section Bash Startup Files
7025@cindex startup files
7026
3185942a 7027This section describes how Bash executes its startup files.
bb70624e 7028If any of the files exist but cannot be read, Bash reports an error.
ac50fbac 7029Tildes are expanded in filenames as described above under
bb70624e
JA
7030Tilde Expansion (@pxref{Tilde Expansion}).
7031
7032Interactive shells are described in @ref{Interactive Shells}.
7033
28ef6c31 7034@subsubheading Invoked as an interactive login shell, or with @option{--login}
bb70624e
JA
7035
7036When Bash is invoked as an interactive login shell, or as a
28ef6c31 7037non-interactive shell with the @option{--login} option, it first reads and
bb70624e
JA
7038executes commands from the file @file{/etc/profile}, if that file exists.
7039After reading that file, it looks for @file{~/.bash_profile},
7040@file{~/.bash_login}, and @file{~/.profile}, in that order, and reads
7041and executes commands from the first one that exists and is readable.
28ef6c31 7042The @option{--noprofile} option may be used when the shell is started to
bb70624e
JA
7043inhibit this behavior.
7044
a0c0a00f
CR
7045When an interactive login shell exits,
7046or a non-interactive login shell executes the @code{exit} builtin command,
7047Bash reads and executes commands from
bb70624e
JA
7048the file @file{~/.bash_logout}, if it exists.
7049
7050@subsubheading Invoked as an interactive non-login shell
7051
7052When an interactive shell that is not a login shell is started, Bash
7053reads and executes commands from @file{~/.bashrc}, if that file exists.
28ef6c31
JA
7054This may be inhibited by using the @option{--norc} option.
7055The @option{--rcfile @var{file}} option will force Bash to read and
bb70624e
JA
7056execute commands from @var{file} instead of @file{~/.bashrc}.
7057
7058So, typically, your @file{~/.bash_profile} contains the line
7059@example
7060@code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi}
7061@end example
7062@noindent
7063after (or before) any login-specific initializations.
7064
7065@subsubheading Invoked non-interactively
7066
7067When Bash is started non-interactively, to run a shell script,
28ef6c31 7068for example, it looks for the variable @env{BASH_ENV} in the environment,
bb70624e
JA
7069expands its value if it appears there, and uses the expanded value as
7070the name of a file to read and execute. Bash behaves as if the
7071following command were executed:
7072@example
7073@code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi}
7074@end example
7075@noindent
28ef6c31 7076but the value of the @env{PATH} variable is not used to search for the
ac50fbac 7077filename.
bb70624e 7078
28ef6c31
JA
7079As noted above, if a non-interactive shell is invoked with the
7080@option{--login} option, Bash attempts to read and execute commands from the
7081login shell startup files.
7082
bb70624e
JA
7083@subsubheading Invoked with name @code{sh}
7084
7085If Bash is invoked with the name @code{sh}, it tries to mimic the
7086startup behavior of historical versions of @code{sh} as closely as
7087possible, while conforming to the @sc{posix} standard as well.
7088
7089When invoked as an interactive login shell, or as a non-interactive
28ef6c31 7090shell with the @option{--login} option, it first attempts to read
bb70624e
JA
7091and execute commands from @file{/etc/profile} and @file{~/.profile}, in
7092that order.
28ef6c31 7093The @option{--noprofile} option may be used to inhibit this behavior.
bb70624e 7094When invoked as an interactive shell with the name @code{sh}, Bash
28ef6c31 7095looks for the variable @env{ENV}, expands its value if it is defined,
bb70624e
JA
7096and uses the expanded value as the name of a file to read and execute.
7097Since a shell invoked as @code{sh} does not attempt to read and execute
28ef6c31 7098commands from any other startup files, the @option{--rcfile} option has
bb70624e
JA
7099no effect.
7100A non-interactive shell invoked with the name @code{sh} does not attempt
7101to read any other startup files.
7102
7103When invoked as @code{sh}, Bash enters @sc{posix} mode after
7104the startup files are read.
7105
7106@subsubheading Invoked in @sc{posix} mode
7107
7108When Bash is started in @sc{posix} mode, as with the
28ef6c31 7109@option{--posix} command line option, it follows the @sc{posix} standard
bb70624e 7110for startup files.
28ef6c31 7111In this mode, interactive shells expand the @env{ENV} variable
bb70624e
JA
7112and commands are read and executed from the file whose name is the
7113expanded value.
7114No other startup files are read.
7115
7116@subsubheading Invoked by remote shell daemon
7117
3185942a 7118Bash attempts to determine when it is being run with its standard input
74091dd4
CR
7119connected to a network connection, as when executed by
7120the historical remote shell daemon, usually @code{rshd},
7121or the secure shell daemon @code{sshd}.
7122If Bash
7123determines it is being run non-interactively in this fashion,
7124it reads and executes commands from @file{~/.bashrc}, if that
bb70624e
JA
7125file exists and is readable.
7126It will not do this if invoked as @code{sh}.
28ef6c31
JA
7127The @option{--norc} option may be used to inhibit this behavior, and the
7128@option{--rcfile} option may be used to force another file to be read, but
ac50fbac
CR
7129neither @code{rshd} nor @code{sshd} generally invoke the shell with those
7130options or allow them to be specified.
bb70624e
JA
7131
7132@subsubheading Invoked with unequal effective and real @sc{uid/gid}s
7133
7134If Bash is started with the effective user (group) id not equal to the
ac50fbac 7135real user (group) id, and the @option{-p} option is not supplied, no startup
bb70624e 7136files are read, shell functions are not inherited from the environment,
0001803f
CR
7137the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH}, and @env{GLOBIGNORE}
7138variables, if they appear in the environment, are ignored, and the effective
7139user id is set to the real user id.
ac50fbac 7140If the @option{-p} option is supplied at invocation, the startup behavior is
bb70624e
JA
7141the same, but the effective user id is not reset.
7142
7143@node Interactive Shells
7144@section Interactive Shells
7145@cindex interactive shell
7146@cindex shell, interactive
7147
7148@menu
7149* What is an Interactive Shell?:: What determines whether a shell is Interactive.
7150* Is this Shell Interactive?:: How to tell if a shell is interactive.
74091dd4 7151* Interactive Shell Behavior:: What changes in an interactive shell?
bb70624e
JA
7152@end menu
7153
7154@node What is an Interactive Shell?
7155@subsection What is an Interactive Shell?
7156
7157An interactive shell
74091dd4
CR
7158is one started without non-option arguments
7159(unless @option{-s} is specified)
7160and without specifying the @option{-c} option,
b80f6443 7161whose input and error output are both
bb70624e 7162connected to terminals (as determined by @code{isatty(3)}),
28ef6c31 7163or one started with the @option{-i} option.
bb70624e
JA
7164
7165An interactive shell generally reads from and writes to a user's
7166terminal.
7167
28ef6c31 7168The @option{-s} invocation option may be used to set the positional parameters
bb70624e
JA
7169when an interactive shell is started.
7170
7171@node Is this Shell Interactive?
7172@subsection Is this Shell Interactive?
7173
7174To determine within a startup script whether or not Bash is
7175running interactively,
7176test the value of the @samp{-} special parameter.
7177It contains @code{i} when the shell is interactive. For example:
7178
7179@example
7180case "$-" in
7181*i*) echo This shell is interactive ;;
7182*) echo This shell is not interactive ;;
7183esac
7184@end example
7185
7186Alternatively, startup scripts may examine the variable
28ef6c31 7187@env{PS1}; it is unset in non-interactive shells, and set in
bb70624e
JA
7188interactive shells. Thus:
7189
7190@example
7191if [ -z "$PS1" ]; then
7192 echo This shell is not interactive
7193else
7194 echo This shell is interactive
7195fi
7196@end example
7197
7198@node Interactive Shell Behavior
7199@subsection Interactive Shell Behavior
7200
7201When the shell is running interactively, it changes its behavior in
7202several ways.
7203
7204@enumerate
7205@item
7206Startup files are read and executed as described in @ref{Bash Startup Files}.
7207
7208@item
7209Job Control (@pxref{Job Control}) is enabled by default. When job
7210control is in effect, Bash ignores the keyboard-generated job control
7211signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}.
7212
7213@item
28ef6c31
JA
7214Bash expands and displays @env{PS1} before reading the first line
7215of a command, and expands and displays @env{PS2} before reading the
bb70624e 7216second and subsequent lines of a multi-line command.
d233b485
CR
7217Bash expands and displays @env{PS0} after it reads a command but before
7218executing it.
7219See @ref{Controlling the Prompt}, for a complete list of prompt
7220string escape sequences.
bb70624e
JA
7221
7222@item
74091dd4 7223Bash executes the values of the set elements of the @env{PROMPT_COMMAND}
8868edaf 7224array variable as commands before printing the primary prompt, @env{$PS1}
bb70624e
JA
7225(@pxref{Bash Variables}).
7226
7227@item
7228Readline (@pxref{Command Line Editing}) is used to read commands from
7229the user's terminal.
7230
7231@item
7232Bash inspects the value of the @code{ignoreeof} option to @code{set -o}
7233instead of exiting immediately when it receives an @code{EOF} on its
7234standard input when reading a command (@pxref{The Set Builtin}).
7235
7236@item
7237Command history (@pxref{Bash History Facilities})
7238and history expansion (@pxref{History Interaction})
7239are enabled by default.
28ef6c31 7240Bash will save the command history to the file named by @env{$HISTFILE}
ac50fbac 7241when a shell with history enabled exits.
bb70624e
JA
7242
7243@item
7244Alias expansion (@pxref{Aliases}) is performed by default.
7245
7246@item
7247In the absence of any traps, Bash ignores @code{SIGTERM}
7248(@pxref{Signals}).
7249
7250@item
7251In the absence of any traps, @code{SIGINT} is caught and handled
d233b485 7252(@pxref{Signals}).
bb70624e
JA
7253@code{SIGINT} will interrupt some shell builtins.
7254
7255@item
7256An interactive login shell sends a @code{SIGHUP} to all jobs on exit
3185942a 7257if the @code{huponexit} shell option has been enabled (@pxref{Signals}).
bb70624e
JA
7258
7259@item
28ef6c31 7260The @option{-n} invocation option is ignored, and @samp{set -n} has
bb70624e
JA
7261no effect (@pxref{The Set Builtin}).
7262
7263@item
7264Bash will check for mail periodically, depending on the values of the
28ef6c31 7265@env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables
bb70624e
JA
7266(@pxref{Bash Variables}).
7267
7268@item
7269Expansion errors due to references to unbound shell variables after
7270@samp{set -u} has been enabled will not cause the shell to exit
7271(@pxref{The Set Builtin}).
7272
7273@item
7274The shell will not exit on expansion errors caused by @var{var} being unset
7275or null in @code{$@{@var{var}:?@var{word}@}} expansions
7276(@pxref{Shell Parameter Expansion}).
7277
7278@item
7279Redirection errors encountered by shell builtins will not cause the
7280shell to exit.
7281
7282@item
7283When running in @sc{posix} mode, a special builtin returning an error
7284status will not cause the shell to exit (@pxref{Bash POSIX Mode}).
95732b49 7285
bb70624e
JA
7286@item
7287A failed @code{exec} will not cause the shell to exit
7288(@pxref{Bourne Shell Builtins}).
7289
7290@item
7291Parser syntax errors will not cause the shell to exit.
7292
7293@item
74091dd4
CR
7294If the @code{cdspell} shell option is enabled, the shell will attempt
7295simple spelling correction for directory arguments to the @code{cd}
7296builtin (see the description of the @code{cdspell}
3185942a 7297option to the @code{shopt} builtin in @ref{The Shopt Builtin}).
74091dd4 7298The @code{cdspell} option is only effective in interactive shells.
bb70624e
JA
7299
7300@item
28ef6c31 7301The shell will check the value of the @env{TMOUT} variable and exit
bb70624e 7302if a command is not read within the specified number of seconds after
28ef6c31 7303printing @env{$PS1} (@pxref{Bash Variables}).
bb70624e
JA
7304
7305@end enumerate
7306
7307@node Bash Conditional Expressions
7308@section Bash Conditional Expressions
7309@cindex expressions, conditional
7310
7311Conditional expressions are used by the @code{[[} compound command
74091dd4
CR
7312(@pxref{Conditional Constructs})
7313and the @code{test} and @code{[} builtin commands
7314(@pxref{Bourne Shell Builtins}).
7315The @code{test}
d233b485
CR
7316and @code{[} commands determine their behavior based on the number
7317of arguments; see the descriptions of those commands for any other
7318command-specific actions.
bb70624e 7319
d233b485
CR
7320Expressions may be unary or binary,
7321and are formed from the following primaries.
bb70624e
JA
7322Unary expressions are often used to examine the status of a file.
7323There are string operators and numeric comparison operators as well.
a0c0a00f
CR
7324Bash handles several filenames specially when they are used in
7325expressions.
7326If the operating system on which Bash is running provides these
7327special files, Bash will use them; otherwise it will emulate them
7328internally with this behavior:
bb70624e
JA
7329If the @var{file} argument to one of the primaries is of the form
7330@file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked.
7331If the @var{file} argument to one of the primaries is one of
7332@file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file
7333descriptor 0, 1, or 2, respectively, is checked.
ccc6cda3 7334
ac50fbac 7335When used with @code{[[}, the @samp{<} and @samp{>} operators sort
0001803f 7336lexicographically using the current locale.
495aee44 7337The @code{test} command uses ASCII ordering.
0001803f 7338
95732b49
JA
7339Unless otherwise specified, primaries that operate on files follow symbolic
7340links and operate on the target of the link, rather than the link itself.
7341
ccc6cda3 7342@table @code
cce855bc
JA
7343@item -a @var{file}
7344True if @var{file} exists.
7345
ccc6cda3
JA
7346@item -b @var{file}
7347True if @var{file} exists and is a block special file.
7348
7349@item -c @var{file}
7350True if @var{file} exists and is a character special file.
7351
7352@item -d @var{file}
7353True if @var{file} exists and is a directory.
7354
7355@item -e @var{file}
7356True if @var{file} exists.
7357
7358@item -f @var{file}
7359True if @var{file} exists and is a regular file.
7360
7361@item -g @var{file}
cce855bc 7362True if @var{file} exists and its set-group-id bit is set.
ccc6cda3 7363
b72432fd
JA
7364@item -h @var{file}
7365True if @var{file} exists and is a symbolic link.
7366
ccc6cda3 7367@item -k @var{file}
cce855bc 7368True if @var{file} exists and its "sticky" bit is set.
ccc6cda3
JA
7369
7370@item -p @var{file}
cce855bc 7371True if @var{file} exists and is a named pipe (FIFO).
ccc6cda3
JA
7372
7373@item -r @var{file}
7374True if @var{file} exists and is readable.
7375
7376@item -s @var{file}
7377True if @var{file} exists and has a size greater than zero.
7378
ccc6cda3 7379@item -t @var{fd}
cce855bc 7380True if file descriptor @var{fd} is open and refers to a terminal.
ccc6cda3
JA
7381
7382@item -u @var{file}
7383True if @var{file} exists and its set-user-id bit is set.
7384
7385@item -w @var{file}
7386True if @var{file} exists and is writable.
7387
7388@item -x @var{file}
7389True if @var{file} exists and is executable.
7390
ccc6cda3
JA
7391@item -G @var{file}
7392True if @var{file} exists and is owned by the effective group id.
7393
cce855bc
JA
7394@item -L @var{file}
7395True if @var{file} exists and is a symbolic link.
7396
495aee44
CR
7397@item -N @var{file}
7398True if @var{file} exists and has been modified since it was last read.
7399
7400@item -O @var{file}
7401True if @var{file} exists and is owned by the effective user id.
7402
cce855bc
JA
7403@item -S @var{file}
7404True if @var{file} exists and is a socket.
7405
495aee44
CR
7406@item @var{file1} -ef @var{file2}
7407True if @var{file1} and @var{file2} refer to the same device and
7408inode numbers.
cce855bc 7409
ccc6cda3 7410@item @var{file1} -nt @var{file2}
7117c2d2
JA
7411True if @var{file1} is newer (according to modification date)
7412than @var{file2}, or if @var{file1} exists and @var{file2} does not.
ccc6cda3
JA
7413
7414@item @var{file1} -ot @var{file2}
7117c2d2
JA
7415True if @var{file1} is older than @var{file2},
7416or if @var{file2} exists and @var{file1} does not.
ccc6cda3 7417
ccc6cda3 7418@item -o @var{optname}
495aee44 7419True if the shell option @var{optname} is enabled.
28ef6c31 7420The list of options appears in the description of the @option{-o}
ccc6cda3
JA
7421option to the @code{set} builtin (@pxref{The Set Builtin}).
7422
495aee44
CR
7423@item -v @var{varname}
7424True if the shell variable @var{varname} is set (has been assigned a value).
7425
ac50fbac
CR
7426@item -R @var{varname}
7427True if the shell variable @var{varname} is set and is a name reference.
7428
ccc6cda3
JA
7429@item -z @var{string}
7430True if the length of @var{string} is zero.
7431
7432@item -n @var{string}
7433@itemx @var{string}
7434True if the length of @var{string} is non-zero.
7435
cce855bc 7436@item @var{string1} == @var{string2}
0001803f 7437@itemx @var{string1} = @var{string2}
cce855bc 7438True if the strings are equal.
ac50fbac
CR
7439When used with the @code{[[} command, this performs pattern matching as
7440described above (@pxref{Conditional Constructs}).
7441
0001803f 7442@samp{=} should be used with the @code{test} command for @sc{posix} conformance.
ccc6cda3
JA
7443
7444@item @var{string1} != @var{string2}
7445True if the strings are not equal.
7446
7447@item @var{string1} < @var{string2}
0001803f 7448True if @var{string1} sorts before @var{string2} lexicographically.
ccc6cda3
JA
7449
7450@item @var{string1} > @var{string2}
0001803f 7451True if @var{string1} sorts after @var{string2} lexicographically.
ccc6cda3
JA
7452
7453@item @var{arg1} OP @var{arg2}
7454@code{OP} is one of
7455@samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}.
7456These arithmetic binary operators return true if @var{arg1}
7457is equal to, not equal to, less than, less than or equal to,
7458greater than, or greater than or equal to @var{arg2},
7459respectively. @var{Arg1} and @var{arg2}
7460may be positive or negative integers.
d233b485
CR
7461When used with the @code{[[} command, @var{Arg1} and @var{Arg2}
7462are evaluated as arithmetic expressions (@pxref{Shell Arithmetic}).
ccc6cda3
JA
7463@end table
7464
bb70624e
JA
7465@node Shell Arithmetic
7466@section Shell Arithmetic
7467@cindex arithmetic, shell
7468@cindex shell arithmetic
7469@cindex expressions, arithmetic
7470@cindex evaluation, arithmetic
7471@cindex arithmetic evaluation
ccc6cda3 7472
bb70624e 7473The shell allows arithmetic expressions to be evaluated, as one of
a0c0a00f
CR
7474the shell expansions or by using the @code{((} compound command, the
7475@code{let} builtin, or the @option{-i} option to the @code{declare} builtin.
ccc6cda3 7476
7117c2d2 7477Evaluation is done in fixed-width integers with no check for overflow,
bb70624e 7478though division by 0 is trapped and flagged as an error.
b80f6443
JA
7479The operators and their precedence, associativity, and values
7480are the same as in the C language.
bb70624e
JA
7481The following list of operators is grouped into levels of
7482equal-precedence operators.
7483The levels are listed in order of decreasing precedence.
cce855bc 7484
bb70624e 7485@table @code
d166f048 7486
bb70624e
JA
7487@item @var{id}++ @var{id}--
7488variable post-increment and post-decrement
cce855bc 7489
bb70624e
JA
7490@item ++@var{id} --@var{id}
7491variable pre-increment and pre-decrement
ccc6cda3 7492
bb70624e
JA
7493@item - +
7494unary minus and plus
ccc6cda3 7495
bb70624e
JA
7496@item ! ~
7497logical and bitwise negation
ccc6cda3 7498
bb70624e
JA
7499@item **
7500exponentiation
ccc6cda3 7501
bb70624e
JA
7502@item * / %
7503multiplication, division, remainder
ccc6cda3 7504
bb70624e
JA
7505@item + -
7506addition, subtraction
cce855bc 7507
bb70624e
JA
7508@item << >>
7509left and right bitwise shifts
cce855bc 7510
bb70624e
JA
7511@item <= >= < >
7512comparison
ccc6cda3 7513
bb70624e
JA
7514@item == !=
7515equality and inequality
ccc6cda3 7516
bb70624e
JA
7517@item &
7518bitwise AND
ccc6cda3 7519
bb70624e
JA
7520@item ^
7521bitwise exclusive OR
ccc6cda3 7522
bb70624e
JA
7523@item |
7524bitwise OR
cce855bc 7525
bb70624e
JA
7526@item &&
7527logical AND
cce855bc 7528
bb70624e
JA
7529@item ||
7530logical OR
cce855bc 7531
bb70624e 7532@item expr ? expr : expr
b80f6443 7533conditional operator
cce855bc 7534
bb70624e
JA
7535@item = *= /= %= += -= <<= >>= &= ^= |=
7536assignment
cce855bc 7537
bb70624e
JA
7538@item expr1 , expr2
7539comma
7540@end table
ccc6cda3 7541
bb70624e
JA
7542Shell variables are allowed as operands; parameter expansion is
7543performed before the expression is evaluated.
7544Within an expression, shell variables may also be referenced by name
7545without using the parameter expansion syntax.
b80f6443
JA
7546A shell variable that is null or unset evaluates to 0 when referenced
7547by name without using the parameter expansion syntax.
bb70624e 7548The value of a variable is evaluated as an arithmetic expression
b80f6443 7549when it is referenced, or when a variable which has been given the
74091dd4 7550@code{integer} attribute using @samp{declare -i} is assigned a value.
b80f6443 7551A null value evaluates to 0.
74091dd4 7552A shell variable need not have its @code{integer} attribute turned on
bb70624e 7553to be used in an expression.
ccc6cda3 7554
8868edaf
CR
7555Integer constants follow the C language definition, without suffixes or
7556character constants.
bb70624e
JA
7557Constants with a leading 0 are interpreted as octal numbers.
7558A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise,
495aee44 7559numbers take the form [@var{base}@code{#}]@var{n}, where the optional @var{base}
bb70624e 7560is a decimal number between 2 and 64 representing the arithmetic
ac50fbac
CR
7561base, and @var{n} is a number in that base.
7562If @var{base}@code{#} is omitted, then base 10 is used.
7563When specifying @var{n},
8868edaf 7564if a non-digit is required,
a0c0a00f 7565the digits greater than 9 are represented by the lowercase letters,
f73dda09 7566the uppercase letters, @samp{@@}, and @samp{_}, in that order.
bb70624e 7567If @var{base} is less than or equal to 36, lowercase and uppercase
95732b49 7568letters may be used interchangeably to represent numbers between 10
bb70624e 7569and 35.
ccc6cda3 7570
bb70624e
JA
7571Operators are evaluated in order of precedence. Sub-expressions in
7572parentheses are evaluated first and may override the precedence
7573rules above.
ccc6cda3 7574
bb70624e
JA
7575@node Aliases
7576@section Aliases
7577@cindex alias expansion
ccc6cda3 7578
74091dd4 7579@dfn{Aliases} allow a string to be substituted for a word when it is used
bb70624e
JA
7580as the first word of a simple command.
7581The shell maintains a list of aliases that may be set and unset with
7582the @code{alias} and @code{unalias} builtin commands.
ccc6cda3 7583
bb70624e
JA
7584The first word of each simple command, if unquoted, is checked to see
7585if it has an alias.
7586If so, that word is replaced by the text of the alias.
b80f6443
JA
7587The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the
7588shell metacharacters or quoting characters listed above may not appear
7589in an alias name.
7590The replacement text may contain any valid
7591shell input, including shell metacharacters.
bb70624e
JA
7592The first word of the replacement text is tested for
7593aliases, but a word that is identical to an alias being expanded
b80f6443
JA
7594is not expanded a second time.
7595This means that one may alias @code{ls} to @code{"ls -F"},
bb70624e 7596for instance, and Bash does not try to recursively expand the
ac50fbac
CR
7597replacement text.
7598If the last character of the alias value is a
74091dd4 7599@code{blank}, then the next command word following the
bb70624e 7600alias is also checked for alias expansion.
ccc6cda3 7601
bb70624e
JA
7602Aliases are created and listed with the @code{alias}
7603command, and removed with the @code{unalias} command.
ccc6cda3 7604
bb70624e
JA
7605There is no mechanism for using arguments in the replacement text,
7606as in @code{csh}.
74091dd4 7607If arguments are needed, use a shell function
bb70624e 7608(@pxref{Shell Functions}).
ccc6cda3 7609
bb70624e
JA
7610Aliases are not expanded when the shell is not interactive,
7611unless the @code{expand_aliases} shell option is set using
3185942a 7612@code{shopt} (@pxref{The Shopt Builtin}).
ccc6cda3 7613
bb70624e 7614The rules concerning the definition and use of aliases are
d233b485
CR
7615somewhat confusing. Bash
7616always reads at least one complete line of input,
7617and all lines that make up a compound command,
7618before executing any of the commands on that line or the compound command.
7619Aliases are expanded when a
bb70624e
JA
7620command is read, not when it is executed. Therefore, an
7621alias definition appearing on the same line as another
7622command does not take effect until the next line of input is read.
7623The commands following the alias definition
7624on that line are not affected by the new alias.
7625This behavior is also an issue when functions are executed.
7626Aliases are expanded when a function definition is read,
7627not when the function is executed, because a function definition
a0c0a00f 7628is itself a command. As a consequence, aliases
bb70624e
JA
7629defined in a function are not available until after that
7630function is executed. To be safe, always put
7631alias definitions on a separate line, and do not use @code{alias}
7632in compound commands.
ccc6cda3 7633
bb70624e 7634For almost every purpose, shell functions are preferred over aliases.
ccc6cda3 7635
bb70624e
JA
7636@node Arrays
7637@section Arrays
7638@cindex arrays
d166f048 7639
3185942a
JA
7640Bash provides one-dimensional indexed and associative array variables.
7641Any variable may be used as an indexed array;
7642the @code{declare} builtin will explicitly declare an array.
bb70624e
JA
7643There is no maximum
7644limit on the size of an array, nor any requirement that members
3185942a
JA
7645be indexed or assigned contiguously.
7646Indexed arrays are referenced using integers (including arithmetic
ac50fbac 7647expressions (@pxref{Shell Arithmetic})) and are zero-based;
3185942a 7648associative arrays use arbitrary strings.
ac50fbac 7649Unless otherwise noted, indexed array indices must be non-negative integers.
ccc6cda3 7650
3185942a
JA
7651An indexed array is created automatically if any variable is assigned to
7652using the syntax
bb70624e 7653@example
ac50fbac 7654@var{name}[@var{subscript}]=@var{value}
bb70624e 7655@end example
ccc6cda3 7656
bb70624e
JA
7657@noindent
7658The @var{subscript}
495aee44 7659is treated as an arithmetic expression that must evaluate to a number.
495aee44 7660To explicitly declare an array, use
bb70624e
JA
7661@example
7662declare -a @var{name}
7663@end example
7664@noindent
7665The syntax
7666@example
7667declare -a @var{name}[@var{subscript}]
7668@end example
7669@noindent
3185942a
JA
7670is also accepted; the @var{subscript} is ignored.
7671
ac50fbac 7672@noindent
3185942a
JA
7673Associative arrays are created using
7674@example
8868edaf 7675declare -A @var{name}
3185942a
JA
7676@end example
7677
7678Attributes may be
bb70624e
JA
7679specified for an array variable using the @code{declare} and
7680@code{readonly} builtins. Each attribute applies to all members of
7681an array.
ccc6cda3 7682
bb70624e
JA
7683Arrays are assigned to using compound assignments of the form
7684@example
ac50fbac 7685@var{name}=(@var{value1} @var{value2} @dots{} )
bb70624e
JA
7686@end example
7687@noindent
7688where each
8868edaf 7689@var{value} may be of the form @code{[@var{subscript}]=}@var{string}.
ac50fbac 7690Indexed array assignments do not require anything but @var{string}.
3185942a 7691When assigning to indexed arrays, if
bb70624e
JA
7692the optional subscript is supplied, that index is assigned to;
7693otherwise the index of the element assigned is the last index assigned
7694to by the statement plus one. Indexing starts at zero.
3185942a 7695
8868edaf
CR
7696Each @var{value} in the list undergoes all the shell expansions
7697described above (@pxref{Shell Expansions}).
7698
7699When assigning to an associative array, the words in a compound assignment
7700may be either assignment statements, for which the subscript is required,
7701or a list of words that is interpreted as a sequence of alternating keys
7702and values:
7703@var{name}=(@var{key1} @var{value1} @var{key2} @var{value2} @dots{} ).
7704These are treated identically to
7705@var{name}=( [@var{key1}]=@var{value1} [@var{key2}]=@var{value2} @dots{} ).
7706The first word in the list determines how the remaining words
7707are interpreted; all assignments in a list must be of the same type.
7708When using key/value pairs, the keys may not be missing or empty;
7709a final missing value is treated like the empty string.
3185942a 7710
bb70624e
JA
7711This syntax is also accepted by the @code{declare}
7712builtin. Individual array elements may be assigned to using the
ac50fbac
CR
7713@code{@var{name}[@var{subscript}]=@var{value}} syntax introduced above.
7714
7715When assigning to an indexed array, if @var{name}
7716is subscripted by a negative number, that number is
7717interpreted as relative to one greater than the maximum index of
7718@var{name}, so negative indices count back from the end of the
7719array, and an index of -1 references the last element.
cce855bc 7720
74091dd4
CR
7721The @samp{+=} operator will append to an array variable when assigning
7722using the compound assignment syntax; see @ref{Shell Parameters} above.
7723
bb70624e 7724Any element of an array may be referenced using
ac50fbac 7725@code{$@{@var{name}[@var{subscript}]@}}.
bb70624e
JA
7726The braces are required to avoid
7727conflicts with the shell's filename expansion operators. If the
7728@var{subscript} is @samp{@@} or @samp{*}, the word expands to all members
7729of the array @var{name}. These subscripts differ only when the word
95732b49
JA
7730appears within double quotes.
7731If the word is double-quoted,
ac50fbac 7732@code{$@{@var{name}[*]@}} expands to a single word with
bb70624e 7733the value of each array member separated by the first character of the
ac50fbac 7734@env{IFS} variable, and @code{$@{@var{name}[@@]@}} expands each element of
bb70624e 7735@var{name} to a separate word. When there are no array members,
ac50fbac 7736@code{$@{@var{name}[@@]@}} expands to nothing.
95732b49
JA
7737If the double-quoted expansion occurs within a word, the expansion of
7738the first parameter is joined with the beginning part of the original
7739word, and the expansion of the last parameter is joined with the last
7740part of the original word.
7741This is analogous to the
bb70624e 7742expansion of the special parameters @samp{@@} and @samp{*}.
ac50fbac
CR
7743@code{$@{#@var{name}[@var{subscript}]@}} expands to the length of
7744@code{$@{@var{name}[@var{subscript}]@}}.
bb70624e
JA
7745If @var{subscript} is @samp{@@} or
7746@samp{*}, the expansion is the number of elements in the array.
ac50fbac
CR
7747If the @var{subscript}
7748used to reference an element of an indexed array
a0c0a00f 7749evaluates to a number less than zero, it is
ac50fbac
CR
7750interpreted as relative to one greater than the maximum index of the array,
7751so negative indices count back from the end of the array,
7752and an index of -1 refers to the last element.
ccc6cda3 7753
a0c0a00f
CR
7754Referencing an array variable without a subscript is equivalent to
7755referencing with a subscript of 0.
7756Any reference to a variable using a valid subscript is legal, and
7757@code{bash} will create an array if necessary.
7758
0001803f
CR
7759An array variable is considered set if a subscript has been assigned a
7760value. The null string is a valid value.
7761
ac50fbac
CR
7762It is possible to obtain the keys (indices) of an array as well as the values.
7763$@{!@var{name}[@@]@} and $@{!@var{name}[*]@} expand to the indices
7764assigned in array variable @var{name}.
7765The treatment when in double quotes is similar to the expansion of the
7766special parameters @samp{@@} and @samp{*} within double quotes.
7767
bb70624e 7768The @code{unset} builtin is used to destroy arrays.
ac50fbac 7769@code{unset @var{name}[@var{subscript}]}
bb70624e 7770destroys the array element at index @var{subscript}.
ac50fbac 7771Negative subscripts to indexed arrays are interpreted as described above.
d233b485 7772Unsetting the last element of an array variable does not unset the variable.
ac50fbac 7773@code{unset @var{name}}, where @var{name} is an array, removes the
bb70624e 7774entire array.
74091dd4
CR
7775@code{unset @var{name}[@var{subscript}]} behaves differently
7776depending on the array type when given a
7777subscript of @samp{*} or @samp{@@}.
7778When @var{name} is an associative array, it removes the element with key
7779@samp{*} or @samp{@@}.
7780If @var{name} is an indexed array, @code{unset} removes all of the elements,
7781but does not remove the array itself.
ccc6cda3 7782
d233b485
CR
7783When using a variable name with a subscript as an argument to a command,
7784such as with @code{unset}, without using the word expansion syntax
7785described above, the argument is subject to the shell's filename expansion.
7786If filename expansion is not desired, the argument should be quoted.
7787
bb70624e 7788The @code{declare}, @code{local}, and @code{readonly}
3185942a
JA
7789builtins each accept a @option{-a} option to specify an indexed
7790array and a @option{-A} option to specify an associative array.
495aee44 7791If both options are supplied, @option{-A} takes precedence.
3185942a 7792The @code{read} builtin accepts a @option{-a}
bb70624e
JA
7793option to assign a list of words read from the standard input
7794to an array, and can read values from the standard input into
7795individual array elements. The @code{set} and @code{declare}
7796builtins display array values in a way that allows them to be
7797reused as input.
ccc6cda3 7798
bb70624e
JA
7799@node The Directory Stack
7800@section The Directory Stack
7801@cindex directory stack
ccc6cda3 7802
bb70624e
JA
7803@menu
7804* Directory Stack Builtins:: Bash builtin commands to manipulate
7805 the directory stack.
7806@end menu
ccc6cda3 7807
bb70624e
JA
7808The directory stack is a list of recently-visited directories. The
7809@code{pushd} builtin adds directories to the stack as it changes
7810the current directory, and the @code{popd} builtin removes specified
7811directories from the stack and changes the current directory to
7812the directory removed. The @code{dirs} builtin displays the contents
a0c0a00f 7813of the directory stack. The current directory is always the "top"
bb70624e 7814of the directory stack.
ccc6cda3 7815
bb70624e 7816The contents of the directory stack are also visible
28ef6c31 7817as the value of the @env{DIRSTACK} shell variable.
ccc6cda3 7818
bb70624e
JA
7819@node Directory Stack Builtins
7820@subsection Directory Stack Builtins
ccc6cda3 7821
bb70624e 7822@table @code
ccc6cda3 7823
bb70624e
JA
7824@item dirs
7825@btindex dirs
7826@example
ac50fbac 7827dirs [-clpv] [+@var{N} | -@var{N}]
bb70624e 7828@end example
ac50fbac 7829
bb70624e
JA
7830Display the list of currently remembered directories. Directories
7831are added to the list with the @code{pushd} command; the
7832@code{popd} command removes directories from the list.
a0c0a00f 7833The current directory is always the first directory in the stack.
ac50fbac 7834
bb70624e 7835@table @code
bb70624e
JA
7836@item -c
7837Clears the directory stack by deleting all of the elements.
7838@item -l
ac50fbac
CR
7839Produces a listing using full pathnames;
7840the default listing format uses a tilde to denote the home directory.
bb70624e
JA
7841@item -p
7842Causes @code{dirs} to print the directory stack with one entry per
7843line.
7844@item -v
7845Causes @code{dirs} to print the directory stack with one entry per
7846line, prefixing each entry with its index in the stack.
ac50fbac
CR
7847@item +@var{N}
7848Displays the @var{N}th directory (counting from the left of the
7849list printed by @code{dirs} when invoked without options), starting
7850with zero.
7851@item -@var{N}
7852Displays the @var{N}th directory (counting from the right of the
7853list printed by @code{dirs} when invoked without options), starting
7854with zero.
bb70624e
JA
7855@end table
7856
7857@item popd
7858@btindex popd
7859@example
ac50fbac 7860popd [-n] [+@var{N} | -@var{N}]
bb70624e
JA
7861@end example
7862
74091dd4 7863Removes elements from the directory stack.
a0c0a00f 7864The elements are numbered from 0 starting at the first directory
74091dd4
CR
7865listed by @code{dirs};
7866that is, @code{popd} is equivalent to @code{popd +0}.
7867
7868When no arguments are given, @code{popd}
7869removes the top directory from the stack and changes to
7870the new top directory.
7871
7872Arguments, if supplied, have the following meanings:
ac50fbac 7873
bb70624e 7874@table @code
ac50fbac
CR
7875@item -n
7876Suppresses the normal change of directory when removing directories
7877from the stack, so that only the stack is manipulated.
bb70624e
JA
7878@item +@var{N}
7879Removes the @var{N}th directory (counting from the left of the
74091dd4 7880list printed by @code{dirs}), starting with zero, from the stack.
bb70624e
JA
7881@item -@var{N}
7882Removes the @var{N}th directory (counting from the right of the
74091dd4 7883list printed by @code{dirs}), starting with zero, from the stack.
bb70624e
JA
7884@end table
7885
74091dd4
CR
7886If the top element of the directory stack is modified, and
7887the @option{-n} option was not supplied, @code{popd} uses the @code{cd}
7888builtin to change to the directory at the top of the stack.
7889If the @code{cd} fails, @code{popd} returns a non-zero value.
7890
7891Otherwise, @code{popd} returns an unsuccessful status if
7892an invalid option is encountered, the directory stack
7893is empty, or a non-existent directory stack entry is specified.
7894
7895If the @code{popd} command is successful,
7896Bash runs @code{dirs} to show the final contents of the directory stack,
7897and the return status is 0.
7898
bb70624e
JA
7899@btindex pushd
7900@item pushd
7901@example
ac50fbac 7902pushd [-n] [@var{+N} | @var{-N} | @var{dir}]
bb70624e
JA
7903@end example
7904
74091dd4
CR
7905Adds a directory to the top of the directory stack, or rotates
7906the stack, making the new top of the stack the current working
7907directory.
7908With no arguments, @code{pushd} exchanges the top two elements
7909of the directory stack.
7910
7911Arguments, if supplied, have the following meanings:
bb70624e
JA
7912
7913@table @code
3185942a 7914@item -n
a0c0a00f
CR
7915Suppresses the normal change of directory when rotating or
7916adding directories to the stack, so that only the stack is manipulated.
bb70624e
JA
7917@item +@var{N}
7918Brings the @var{N}th directory (counting from the left of the
7919list printed by @code{dirs}, starting with zero) to the top of
7920the list by rotating the stack.
7921@item -@var{N}
7922Brings the @var{N}th directory (counting from the right of the
7923list printed by @code{dirs}, starting with zero) to the top of
7924the list by rotating the stack.
bb70624e 7925@item @var{dir}
74091dd4 7926Makes @var{dir} be the top of the stack.
bb70624e 7927@end table
74091dd4
CR
7928
7929After the stack has been modified, if the @option{-n} option was not
7930supplied, @code{pushd} uses the @code{cd} builtin to change to the
7931directory at the top of the stack.
7932If the @code{cd} fails, @code{pushd} returns a non-zero value.
7933
7934Otherwise, if no arguments are supplied, @code{pushd} returns 0 unless the
7935directory stack is empty.
7936When rotating the directory stack, @code{pushd} returns 0 unless
7937the directory stack is empty or a non-existent directory stack element
7938is specified.
7939
7940If the @code{pushd} command is successful,
7941Bash runs @code{dirs} to show the final contents of the directory stack.
7942
bb70624e
JA
7943@end table
7944
ac50fbac 7945@node Controlling the Prompt
bb70624e
JA
7946@section Controlling the Prompt
7947@cindex prompting
ccc6cda3 7948
74091dd4 7949Bash examines the value of the array variable @env{PROMPT_COMMAND} just before
8868edaf 7950printing each primary prompt.
74091dd4 7951If any elements in @env{PROMPT_COMMAND} are set and non-null, Bash
8868edaf
CR
7952executes each value, in numeric order,
7953just as if it had been typed on the command line.
ccc6cda3 7954
bb70624e 7955In addition, the following table describes the special characters which
d233b485
CR
7956can appear in the prompt variables @env{PS0}, @env{PS1}, @env{PS2}, and
7957@env{PS4}:
ccc6cda3 7958
bb70624e
JA
7959@table @code
7960@item \a
7961A bell character.
7962@item \d
7963The date, in "Weekday Month Date" format (e.g., "Tue May 26").
7117c2d2
JA
7964@item \D@{@var{format}@}
7965The @var{format} is passed to @code{strftime}(3) and the result is inserted
7966into the prompt string; an empty @var{format} results in a locale-specific
7967time representation. The braces are required.
bb70624e
JA
7968@item \e
7969An escape character.
7970@item \h
7971The hostname, up to the first `.'.
7972@item \H
7973The hostname.
7974@item \j
7975The number of jobs currently managed by the shell.
7976@item \l
7977The basename of the shell's terminal device name.
7978@item \n
7979A newline.
7980@item \r
7981A carriage return.
7982@item \s
7983The name of the shell, the basename of @code{$0} (the portion
7984following the final slash).
7985@item \t
7986The time, in 24-hour HH:MM:SS format.
7987@item \T
7988The time, in 12-hour HH:MM:SS format.
7989@item \@@
7990The time, in 12-hour am/pm format.
f73dda09
JA
7991@item \A
7992The time, in 24-hour HH:MM format.
bb70624e
JA
7993@item \u
7994The username of the current user.
7995@item \v
7996The version of Bash (e.g., 2.00)
7997@item \V
7998The release of Bash, version + patchlevel (e.g., 2.00.0)
7999@item \w
74091dd4
CR
8000The value of the @code{PWD} shell variable (@env{$PWD}),
8001with @env{$HOME} abbreviated with a tilde
3185942a 8002(uses the @env{$PROMPT_DIRTRIM} variable).
bb70624e 8003@item \W
b80f6443 8004The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde.
bb70624e
JA
8005@item \!
8006The history number of this command.
8007@item \#
8008The command number of this command.
8009@item \$
8010If the effective uid is 0, @code{#}, otherwise @code{$}.
8011@item \@var{nnn}
8012The character whose ASCII code is the octal value @var{nnn}.
8013@item \\
8014A backslash.
8015@item \[
8016Begin a sequence of non-printing characters. This could be used to
8017embed a terminal control sequence into the prompt.
8018@item \]
8019End a sequence of non-printing characters.
8020@end table
ccc6cda3 8021
bb70624e
JA
8022The command number and the history number are usually different:
8023the history number of a command is its position in the history
8024list, which may include commands restored from the history file
8025(@pxref{Bash History Facilities}), while the command number is
8026the position in the sequence of commands executed during the current
8027shell session.
ccc6cda3 8028
bb70624e
JA
8029After the string is decoded, it is expanded via
8030parameter expansion, command substitution, arithmetic
8031expansion, and quote removal, subject to the value of the
d233b485 8032@code{promptvars} shell option (@pxref{The Shopt Builtin}).
8868edaf
CR
8033This can have unwanted side effects if escaped portions of the string
8034appear within command substitution or contain characters special to
8035word expansion.
ccc6cda3 8036
bb70624e
JA
8037@node The Restricted Shell
8038@section The Restricted Shell
8039@cindex restricted shell
ccc6cda3 8040
bb70624e 8041If Bash is started with the name @code{rbash}, or the
28ef6c31 8042@option{--restricted}
7117c2d2
JA
8043or
8044@option{-r}
bb70624e
JA
8045option is supplied at invocation, the shell becomes restricted.
8046A restricted shell is used to
8047set up an environment more controlled than the standard shell.
8048A restricted shell behaves identically to @code{bash}
7117c2d2
JA
8049with the exception that the following are disallowed or not performed:
8050
bb70624e
JA
8051@itemize @bullet
8052@item
8053Changing directories with the @code{cd} builtin.
8054@item
28ef6c31 8055Setting or unsetting the values of the @env{SHELL}, @env{PATH},
8868edaf 8056@env{HISTFILE},
28ef6c31 8057@env{ENV}, or @env{BASH_ENV} variables.
bb70624e
JA
8058@item
8059Specifying command names containing slashes.
8060@item
8061Specifying a filename containing a slash as an argument to the @code{.}
8062builtin command.
8063@item
8868edaf
CR
8064Specifying a filename containing a slash as an argument to the @code{history}
8065builtin command.
8066@item
28ef6c31 8067Specifying a filename containing a slash as an argument to the @option{-p}
bb70624e
JA
8068option to the @code{hash} builtin command.
8069@item
8070Importing function definitions from the shell environment at startup.
8071@item
28ef6c31 8072Parsing the value of @env{SHELLOPTS} from the shell environment at startup.
bb70624e
JA
8073@item
8074Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&},
8075@samp{&>}, and @samp{>>} redirection operators.
8076@item
8077Using the @code{exec} builtin to replace the shell with another command.
8078@item
8079Adding or deleting builtin commands with the
28ef6c31 8080@option{-f} and @option{-d} options to the @code{enable} builtin.
bb70624e 8081@item
7117c2d2
JA
8082Using the @code{enable} builtin command to enable disabled shell builtins.
8083@item
28ef6c31 8084Specifying the @option{-p} option to the @code{command} builtin.
bb70624e 8085@item
74091dd4 8086Turning off restricted mode with @samp{set +r} or @samp{shopt -u restricted_shell}.
bb70624e 8087@end itemize
cce855bc 8088
7117c2d2
JA
8089These restrictions are enforced after any startup files are read.
8090
8091When a command that is found to be a shell script is executed
8092(@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in
8093the shell spawned to execute the script.
8094
8868edaf
CR
8095The restricted shell mode is only one component of a useful restricted
8096environment. It should be accompanied by setting @env{PATH} to a value
8097that allows execution of only a few verified commands (commands that
74091dd4
CR
8098allow shell escapes are particularly vulnerable), changing the current
8099directory to a non-writable directory other than @env{$HOME} after login,
8868edaf
CR
8100not allowing the restricted shell to execute shell scripts, and cleaning
8101the environment of variables that cause some commands to modify their
8102behavior (e.g., @env{VISUAL} or @env{PAGER}).
8103
8104Modern systems provide more secure ways to implement a restricted environment,
8105such as @code{jails}, @code{zones}, or @code{containers}.
8106
8107
bb70624e
JA
8108@node Bash POSIX Mode
8109@section Bash POSIX Mode
8110@cindex POSIX Mode
cce855bc 8111
28ef6c31 8112Starting Bash with the @option{--posix} command-line option or executing
bb70624e 8113@samp{set -o posix} while Bash is running will cause Bash to conform more
0628567a 8114closely to the @sc{posix} standard by changing the behavior to
bb70624e 8115match that specified by @sc{posix} in areas where the Bash default differs.
ccc6cda3 8116
7117c2d2
JA
8117When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the
8118startup files.
8119
bb70624e 8120The following list is what's changed when `@sc{posix} mode' is in effect:
ccc6cda3 8121
bb70624e 8122@enumerate
d233b485
CR
8123@item
8124Bash ensures that the @env{POSIXLY_CORRECT} variable is set.
8125
bb70624e
JA
8126@item
8127When a command in the hash table no longer exists, Bash will re-search
28ef6c31 8128@env{$PATH} to find the new location. This is also available with
bb70624e 8129@samp{shopt -s checkhash}.
ccc6cda3 8130
8868edaf
CR
8131@item
8132Bash will not insert a command without the execute bit set into the
8133command hash table, even if it returns it as a (last-ditch) result
8134from a @env{$PATH} search.
8135
bb70624e 8136@item
28ef6c31
JA
8137The message printed by the job control code and builtins when a job
8138exits with a non-zero status is `Done(status)'.
ccc6cda3 8139
bb70624e
JA
8140@item
8141The message printed by the job control code and builtins when a job
28ef6c31
JA
8142is stopped is `Stopped(@var{signame})', where @var{signame} is, for
8143example, @code{SIGTSTP}.
ccc6cda3 8144
bb70624e 8145@item
a0c0a00f 8146Alias expansion is always enabled, even in non-interactive shells.
95732b49
JA
8147
8148@item
8149Reserved words appearing in a context where reserved words are recognized
8150do not undergo alias expansion.
ccc6cda3 8151
74091dd4
CR
8152@item
8153Alias expansion is performed when initially parsing a command substitution.
8154The default mode generally defers it, when enabled, until the command
8155substitution is executed. This means that command substitution will not
8156expand aliases that are defined after the command substitution is initially
8157parsed (e.g., as part of a function definition).
8158
bb70624e 8159@item
0628567a 8160The @sc{posix} @env{PS1} and @env{PS2} expansions of @samp{!} to
bb70624e 8161the history number and @samp{!!} to @samp{!} are enabled,
28ef6c31
JA
8162and parameter expansion is performed on the values of @env{PS1} and
8163@env{PS2} regardless of the setting of the @code{promptvars} option.
ccc6cda3 8164
bb70624e 8165@item
0628567a 8166The @sc{posix} startup files are executed (@env{$ENV}) rather than
bb70624e 8167the normal Bash files.
ccc6cda3 8168
bb70624e
JA
8169@item
8170Tilde expansion is only performed on assignments preceding a command
8171name, rather than on all assignment statements on the line.
cce855bc 8172
bb70624e
JA
8173@item
8174The default history file is @file{~/.sh_history} (this is the
28ef6c31 8175default value of @env{$HISTFILE}).
ccc6cda3 8176
bb70624e
JA
8177@item
8178Redirection operators do not perform filename expansion on the word
8179in the redirection unless the shell is interactive.
ccc6cda3 8180
bb70624e
JA
8181@item
8182Redirection operators do not perform word splitting on the word in the
8183redirection.
ccc6cda3 8184
bb70624e
JA
8185@item
8186Function names must be valid shell @code{name}s. That is, they may not
8187contain characters other than letters, digits, and underscores, and
8188may not start with a digit. Declaring a function with an invalid name
8189causes a fatal syntax error in non-interactive shells.
ccc6cda3 8190
ac50fbac
CR
8191@item
8192Function names may not be the same as one of the @sc{posix} special
8193builtins.
8194
bb70624e 8195@item
0628567a 8196@sc{posix} special builtins are found before shell functions
bb70624e 8197during command lookup.
cce855bc 8198
a0c0a00f
CR
8199@item
8200When printing shell function definitions (e.g., by @code{type}), Bash does
8201not print the @code{function} keyword.
8202
8203@item
8204Literal tildes that appear as the first character in elements of
8205the @env{PATH} variable are not expanded as described above
8206under @ref{Tilde Expansion}.
8207
495aee44
CR
8208@item
8209The @code{time} reserved word may be used by itself as a command. When
8210used in this way, it displays timing statistics for the shell and its
8211completed children. The @env{TIMEFORMAT} variable controls the format
8212of the timing information.
8213
8214@item
8215When parsing and expanding a $@{@dots{}@} expansion that appears within
8216double quotes, single quotes are no longer special and cannot be used to
8217quote a closing brace or other special character, unless the operator is
8218one of those defined to perform pattern removal. In this case, they do
8219not have to appear as matched pairs.
8220
8221@item
8222The parser does not recognize @code{time} as a reserved word if the next
8223token begins with a @samp{-}.
8224
8868edaf
CR
8225@ignore
8226@item
8227When parsing @code{$()} command substitutions containing here-documents,
8228the parser does not allow a here-document to be delimited by the closing
8229right parenthesis. The newline after the here-document delimiter is required.
8230@end ignore
8231
a0c0a00f
CR
8232@item
8233The @samp{!} character does not introduce history expansion within a
8234double-quoted string, even if the @code{histexpand} option is enabled.
8235
bb70624e 8236@item
0628567a 8237If a @sc{posix} special builtin returns an error status, a
bb70624e 8238non-interactive shell exits. The fatal errors are those listed in
495aee44 8239the @sc{posix} standard, and include things like passing incorrect options,
bb70624e
JA
8240redirection errors, variable assignment errors for assignments preceding
8241the command name, and so on.
ccc6cda3 8242
bb70624e
JA
8243@item
8244A non-interactive shell exits with an error status if a variable
8245assignment error occurs when no command name follows the assignment
8246statements.
8247A variable assignment error occurs, for example, when trying to assign
8248a value to a readonly variable.
ccc6cda3 8249
495aee44 8250@item
ac50fbac 8251A non-interactive shell exits with an error status if a variable
495aee44 8252assignment error occurs in an assignment statement preceding a special
74091dd4
CR
8253builtin, but not with any other simple command. For any other simple
8254command, the shell aborts execution of that command, and execution continues
8255at the top level ("the shell shall not perform any further processing of the
8256command in which the error occurred").
495aee44 8257
bb70624e
JA
8258@item
8259A non-interactive shell exits with an error status if the iteration
8260variable in a @code{for} statement or the selection variable in a
8261@code{select} statement is a readonly variable.
ccc6cda3 8262
a0c0a00f
CR
8263@item
8264Non-interactive shells exit if @var{filename} in @code{.} @var{filename}
8265is not found.
8266
8267@item
8268Non-interactive shells exit if a syntax error in an arithmetic expansion
8269results in an invalid expression.
8270
8271@item
8272Non-interactive shells exit if a parameter expansion error occurs.
8273
8274@item
8275Non-interactive shells exit if there is a syntax error in a script read
8276with the @code{.} or @code{source} builtins, or in a string processed by
8277the @code{eval} builtin.
8278
ac50fbac
CR
8279@item
8280While variable indirection is available, it may not be applied to the
8281@samp{#} and @samp{?} special parameters.
8282
a0c0a00f 8283@item
74091dd4 8284Expanding the @samp{*} special parameter in a pattern context where the
a0c0a00f
CR
8285expansion is double-quoted does not treat the @code{$*} as if it were
8286double-quoted.
8287
bb70624e 8288@item
0628567a 8289Assignment statements preceding @sc{posix} special builtins
bb70624e 8290persist in the shell environment after the builtin completes.
ccc6cda3 8291
a0c0a00f
CR
8292@item
8293The @code{command} builtin does not prevent builtins that take assignment
8294statements as arguments from expanding them as assignment statements;
8295when not in @sc{posix} mode, assignment builtins lose their assignment
8296statement expansion properties when preceded by @code{command}.
8297
8298@item
8299The @code{bg} builtin uses the required format to describe each job placed
8300in the background, which does not include an indication of whether the job
8301is the current or previous job.
8302
8303@item
8304The output of @samp{kill -l} prints all the signal names on a single line,
8305separated by spaces, without the @samp{SIG} prefix.
8306
8307@item
8308The @code{kill} builtin does not accept signal names with a @samp{SIG}
8309prefix.
8310
bb70624e
JA
8311@item
8312The @code{export} and @code{readonly} builtin commands display their
0628567a 8313output in the format required by @sc{posix}.
ccc6cda3 8314
28ef6c31
JA
8315@item
8316The @code{trap} builtin displays signal names without the leading
8317@code{SIG}.
8318
b80f6443
JA
8319@item
8320The @code{trap} builtin doesn't check the first argument for a possible
8321signal specification and revert the signal handling to the original
eb873671
JA
8322disposition if it is, unless that argument consists solely of digits and
8323is a valid signal number. If users want to reset the handler for a given
b80f6443
JA
8324signal to the original disposition, they should use @samp{-} as the
8325first argument.
8326
8868edaf
CR
8327@item
8328@code{trap -p} displays signals whose dispositions are set to SIG_DFL and
8329those that were ignored when the shell started.
8330
28ef6c31
JA
8331@item
8332The @code{.} and @code{source} builtins do not search the current directory
8333for the filename argument if it is not found by searching @env{PATH}.
8334
8335@item
a0c0a00f
CR
8336Enabling @sc{posix} mode has the effect of setting the
8337@code{inherit_errexit} option, so
8338subshells spawned to execute command substitutions inherit the value of
8339the @option{-e} option from the parent shell.
8340When the @code{inherit_errexit} option is not enabled,
28ef6c31
JA
8341Bash clears the @option{-e} option in such subshells.
8342
d233b485
CR
8343@item
8344Enabling @sc{posix} mode has the effect of setting the
8345@code{shift_verbose} option, so numeric arguments to @code{shift}
8346that exceed the number of positional parameters will result in an
8347error message.
8348
b80f6443
JA
8349@item
8350When the @code{alias} builtin displays alias definitions, it does not
8351display them with a leading @samp{alias } unless the @option{-p} option
8352is supplied.
8353
28ef6c31
JA
8354@item
8355When the @code{set} builtin is invoked without options, it does not display
8356shell function names and definitions.
8357
f73dda09
JA
8358@item
8359When the @code{set} builtin is invoked without options, it displays
8360variable values without quotes, unless they contain shell metacharacters,
8361even if the result contains nonprinting characters.
7117c2d2
JA
8362
8363@item
74091dd4 8364When the @code{cd} builtin is invoked in logical mode, and the pathname
7117c2d2
JA
8365constructed from @code{$PWD} and the directory name supplied as an argument
8366does not refer to an existing directory, @code{cd} will fail instead of
74091dd4 8367falling back to physical mode.
95732b49 8368
d233b485
CR
8369@item
8370When the @code{cd} builtin cannot change a directory because the
8371length of the pathname
8372constructed from @code{$PWD} and the directory name supplied as an argument
74091dd4 8373exceeds @code{PATH_MAX} when all symbolic links are expanded, @code{cd} will
d233b485
CR
8374fail instead of attempting to use only the supplied directory name.
8375
95732b49
JA
8376@item
8377The @code{pwd} builtin verifies that the value it prints is the same as the
8378current directory, even if it is not asked to check the file system with the
8379@option{-P} option.
8380
8381@item
8382When listing the history, the @code{fc} builtin does not include an
8383indication of whether or not a history entry has been modified.
8384
8385@item
8386The default editor used by @code{fc} is @code{ed}.
8387
8388@item
8389The @code{type} and @code{command} builtins will not report a non-executable
8390file as having been found, though the shell will attempt to execute such a
8391file if it is the only so-named file found in @code{$PATH}.
8392
8393@item
8394The @code{vi} editing mode will invoke the @code{vi} editor directly when
3185942a 8395the @samp{v} command is run, instead of checking @code{$VISUAL} and
95732b49
JA
8396@code{$EDITOR}.
8397
8398@item
8399When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret
8400any arguments to @code{echo} as options. Each argument is displayed, after
8401escape characters are converted.
8402
3185942a
JA
8403@item
8404The @code{ulimit} builtin uses a block size of 512 bytes for the @option{-c}
8405and @option{-f} options.
8406
0001803f
CR
8407@item
8408The arrival of @code{SIGCHLD} when a trap is set on @code{SIGCHLD} does
8409not interrupt the @code{wait} builtin and cause it to return immediately.
8410The trap command is run once for each child that exits.
8411
ac50fbac
CR
8412@item
8413The @code{read} builtin may be interrupted by a signal for which a trap
8414has been set.
8415If Bash receives a trapped signal while executing @code{read}, the trap
8416handler executes and @code{read} returns an exit status greater than 128.
8417
74091dd4
CR
8418@item
8419The @code{printf} builtin uses @code{double} (via @code{strtod}) to convert
8420arguments corresponding to floating point conversion specifiers, instead of
8421@code{long double} if it's available. The @samp{L} length modifier forces
8422@code{printf} to use @code{long double} if it's available.
8423
a0c0a00f
CR
8424@item
8425Bash removes an exited background process's status from the list of such
8426statuses after the @code{wait} builtin is used to obtain it.
8427
bb70624e 8428@end enumerate
ccc6cda3 8429
0628567a 8430There is other @sc{posix} behavior that Bash does not implement by
95732b49 8431default even when in @sc{posix} mode.
bb70624e 8432Specifically:
ccc6cda3 8433
bb70624e 8434@enumerate
28ef6c31
JA
8435
8436@item
95732b49
JA
8437The @code{fc} builtin checks @code{$EDITOR} as a program to edit history
8438entries if @code{FCEDIT} is unset, rather than defaulting directly to
8439@code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset.
28ef6c31
JA
8440
8441@item
95732b49
JA
8442As noted above, Bash requires the @code{xpg_echo} option to be enabled for
8443the @code{echo} builtin to be fully conformant.
28ef6c31 8444
bb70624e 8445@end enumerate
ccc6cda3 8446
95732b49
JA
8447Bash can be configured to be @sc{posix}-conformant by default, by specifying
8448the @option{--enable-strict-posix-default} to @code{configure} when building
8449(@pxref{Optional Features}).
8450
8868edaf
CR
8451@node Shell Compatibility Mode
8452@section Shell Compatibility Mode
8453@cindex Compatibility Level
8454@cindex Compatibility Mode
8455
74091dd4
CR
8456Bash-4.0 introduced the concept of a @dfn{shell compatibility level},
8457specified as a set of options to the shopt builtin
8868edaf
CR
8458(@code{compat31},
8459@code{compat32},
8460@code{compat40},
8461@code{compat41},
8462and so on).
8463There is only one current
8464compatibility level -- each option is mutually exclusive.
8465The compatibility level is intended to allow users to select behavior
8466from previous versions that is incompatible with newer versions
8467while they migrate scripts to use current features and
8468behavior. It's intended to be a temporary solution.
8469
8470This section does not mention behavior that is standard for a particular
8471version (e.g., setting @code{compat32} means that quoting the rhs of the regexp
8472matching operator quotes special regexp characters in the word, which is
74091dd4 8473default behavior in bash-3.2 and subsequent versions).
8868edaf
CR
8474
8475If a user enables, say, @code{compat32}, it may affect the behavior of other
8476compatibility levels up to and including the current compatibility level.
8477The idea is that each compatibility level controls behavior that changed
8478in that version of Bash,
8479but that behavior may have been present in earlier versions.
8480For instance, the change to use locale-based comparisons with the @code{[[}
8481command came in bash-4.1, and earlier versions used ASCII-based comparisons,
8482so enabling @code{compat32} will enable ASCII-based comparisons as well.
8483That granularity may not be sufficient for
8484all uses, and as a result users should employ compatibility levels carefully.
8485Read the documentation for a particular feature to find out the
8486current behavior.
8487
8488Bash-4.3 introduced a new shell variable: @env{BASH_COMPAT}.
8489The value assigned
8490to this variable (a decimal version number like 4.2, or an integer
8491corresponding to the @code{compat}@var{NN} option, like 42) determines the
8492compatibility level.
8493
8494Starting with bash-4.4, Bash has begun deprecating older compatibility
8495levels.
8496Eventually, the options will be removed in favor of @env{BASH_COMPAT}.
8497
8498Bash-5.0 is the final version for which there will be an individual shopt
8499option for the previous version. Users should use @env{BASH_COMPAT}
8500on bash-5.0 and later versions.
8501
8502The following table describes the behavior changes controlled by each
8503compatibility level setting.
8504The @code{compat}@var{NN} tag is used as shorthand for setting the
8505compatibility level
8506to @var{NN} using one of the following mechanisms.
8507For versions prior to bash-5.0, the compatibility level may be set using
8508the corresponding @code{compat}@var{NN} shopt option.
8509For bash-4.3 and later versions, the @env{BASH_COMPAT} variable is preferred,
8510and it is required for bash-5.1 and later versions.
8511
8512@table @code
8513@item compat31
8514@itemize @bullet
8515@item
8516quoting the rhs of the @code{[[} command's regexp matching operator (=~)
8517has no special effect
8518@end itemize
8519
8520@item compat32
8521@itemize @bullet
8522@item
8523interrupting a command list such as "a ; b ; c" causes the execution
8524of the next command in the list (in bash-4.0 and later versions,
8525the shell acts as if it received the interrupt, so
8526interrupting one command in a list aborts the execution of the
8527entire list)
8528@end itemize
8529
8530@item compat40
8531@itemize @bullet
8532@item
8533the @samp{<} and @samp{>} operators to the @code{[[} command do not
8534consider the current locale when comparing strings; they use ASCII
8535ordering.
8536Bash versions prior to bash-4.1 use ASCII collation and strcmp(3);
8537bash-4.1 and later use the current locale's collation sequence and
8538strcoll(3).
8539@end itemize
8540
8541@item compat41
8542@itemize @bullet
8543@item
8544in posix mode, @code{time} may be followed by options and still be
8545recognized as a reserved word (this is @sc{posix} interpretation 267)
8546@item
8547in posix mode, the parser requires that an even number of single
8548quotes occur in the @var{word} portion of a double-quoted $@{@dots{}@}
8549parameter expansion and treats them specially, so that characters within
8550the single quotes are considered quoted
8551(this is @sc{posix} interpretation 221)
8552@end itemize
8553
8554@item compat42
8555@itemize @bullet
8556@item
8557the replacement string in double-quoted pattern substitution does not
8558undergo quote removal, as it does in versions after bash-4.2
8559@item
8560in posix mode, single quotes are considered special when expanding
8561the @var{word} portion of a double-quoted $@{@dots{}@} parameter expansion
8562and can be used to quote a closing brace or other special character
8563(this is part of @sc{posix} interpretation 221);
8564in later versions, single quotes
8565are not special within double-quoted word expansions
8566@end itemize
8567
8568@item compat43
8569@itemize @bullet
8570@item
8571the shell does not print a warning message if an attempt is made to
8572use a quoted compound assignment as an argument to declare
74091dd4 8573(e.g., declare -a foo='(1 2)'). Later versions warn that this usage is
8868edaf
CR
8574deprecated
8575@item
8576word expansion errors are considered non-fatal errors that cause the
8577current command to fail, even in posix mode
8578(the default behavior is to make them fatal errors that cause the shell
8579to exit)
8580@item
8581when executing a shell function, the loop state (while/until/etc.)
8582is not reset, so @code{break} or @code{continue} in that function will break
8583or continue loops in the calling context. Bash-4.4 and later reset
8584the loop state to prevent this
8585@end itemize
8586
8587@item compat44
8588@itemize @bullet
8589@item
8590the shell sets up the values used by @env{BASH_ARGV} and @env{BASH_ARGC}
8591so they can expand to the shell's positional parameters even if extended
8592debugging mode is not enabled
8593@item
8594a subshell inherits loops from its parent context, so @code{break}
8595or @code{continue} will cause the subshell to exit.
8596Bash-5.0 and later reset the loop state to prevent the exit
8597@item
8598variable assignments preceding builtins like @code{export} and @code{readonly}
8599that set attributes continue to affect variables with the same
8600name in the calling environment even if the shell is not in posix
8601mode
8602@end itemize
8603
8604@item compat50 (set using BASH_COMPAT)
8605@itemize @bullet
8606@item
8607Bash-5.1 changed the way @code{$RANDOM} is generated to introduce slightly
8608more randomness. If the shell compatibility level is set to 50 or
8609lower, it reverts to the method from bash-5.0 and previous versions,
8610so seeding the random number generator by assigning a value to
8611@env{RANDOM} will produce the same sequence as in bash-5.0
8612@item
8613If the command hash table is empty, Bash versions prior to bash-5.1
8614printed an informational message to that effect, even when producing
8615output that can be reused as input. Bash-5.1 suppresses that message
8616when the @option{-l} option is supplied.
8617@end itemize
74091dd4
CR
8618
8619@item compat51 (set using BASH_COMPAT)
8620@itemize @bullet
8621@item
8622The @code{unset} builtin will unset the array @code{a} given an argument like
8623@samp{a[@@]}.
8624Bash-5.2 will unset an element with key @samp{@@} (associative arrays)
8625or remove all the elements without unsetting the array (indexed arrays)
8626@item
8627arithmetic commands ( ((...)) ) and the expressions in an arithmetic for
8628statement can be expanded more than once
8629@item
8630expressions used as arguments to arithmetic operators in the @code{[[}
8631conditional command can be expanded more than once
8632@item
8633the expressions in substring parameter brace expansion can be
8634expanded more than once
8635@item
8636the expressions in the $(( ... )) word expansion can be expanded
8637more than once
8638@item
8639arithmetic expressions used as indexed array subscripts can be
8640expanded more than once
8641@item
8642@code{test -v}, when given an argument of @samp{A[@@]}, where @var{A} is
8643an existing associative array, will return true if the array has any set
8644elements.
8645Bash-5.2 will look for and report on a key named @samp{@@}
8646@item
8647the $@{@var{parameter}[:]=@var{value}@} word expansion will return
8648@var{value}, before any variable-specific transformations have been
8649performed (e.g., converting to lowercase).
8650Bash-5.2 will return the final value assigned to the variable.
8651@item
8652Parsing command substitutions will behave as if extended glob
8653(@pxref{The Shopt Builtin})
8654is enabled, so that parsing a command substitution containing an extglob
8655pattern (say, as part of a shell function) will not fail.
8656This assumes the intent is to enable extglob before the command is executed
8657and word expansions are performed.
8658It will fail at word expansion time if extglob hasn't been
8659enabled by the time the command is executed.
8660@end itemize
8868edaf
CR
8661@end table
8662
bb70624e
JA
8663@node Job Control
8664@chapter Job Control
ccc6cda3 8665
bb70624e
JA
8666This chapter discusses what job control is, how it works, and how
8667Bash allows you to access its facilities.
ccc6cda3 8668
bb70624e
JA
8669@menu
8670* Job Control Basics:: How job control works.
8671* Job Control Builtins:: Bash builtin commands used to interact
8672 with job control.
8673* Job Control Variables:: Variables Bash uses to customize job
8674 control.
8675@end menu
ccc6cda3 8676
bb70624e
JA
8677@node Job Control Basics
8678@section Job Control Basics
8679@cindex job control
8680@cindex foreground
8681@cindex background
8682@cindex suspending jobs
ccc6cda3 8683
bb70624e
JA
8684Job control
8685refers to the ability to selectively stop (suspend)
8686the execution of processes and continue (resume)
8687their execution at a later point. A user typically employs
8688this facility via an interactive interface supplied jointly
0001803f 8689by the operating system kernel's terminal driver and Bash.
ccc6cda3 8690
bb70624e
JA
8691The shell associates a @var{job} with each pipeline. It keeps a
8692table of currently executing jobs, which may be listed with the
8693@code{jobs} command. When Bash starts a job
8694asynchronously, it prints a line that looks
8695like:
8696@example
8697[1] 25647
8698@end example
8699@noindent
8700indicating that this job is job number 1 and that the process @sc{id}
8701of the last process in the pipeline associated with this job is
870225647. All of the processes in a single pipeline are members of
8703the same job. Bash uses the @var{job} abstraction as the
8704basis for job control.
ccc6cda3 8705
bb70624e
JA
8706To facilitate the implementation of the user interface to job
8707control, the operating system maintains the notion of a current terminal
8708process group @sc{id}. Members of this process group (processes whose
8709process group @sc{id} is equal to the current terminal process group
8710@sc{id}) receive keyboard-generated signals such as @code{SIGINT}.
8711These processes are said to be in the foreground. Background
8712processes are those whose process group @sc{id} differs from the
8713terminal's; such processes are immune to keyboard-generated
0001803f
CR
8714signals. Only foreground processes are allowed to read from or, if
8715the user so specifies with @code{stty tostop}, write to the terminal.
8716Background processes which attempt to
8717read from (write to when @code{stty tostop} is in effect) the
8718terminal are sent a @code{SIGTTIN} (@code{SIGTTOU})
8719signal by the kernel's terminal driver,
8720which, unless caught, suspends the process.
ccc6cda3 8721
bb70624e
JA
8722If the operating system on which Bash is running supports
8723job control, Bash contains facilities to use it. Typing the
74091dd4 8724@dfn{suspend} character (typically @samp{^Z}, Control-Z) while a
bb70624e 8725process is running causes that process to be stopped and returns
74091dd4 8726control to Bash. Typing the @dfn{delayed suspend} character
bb70624e
JA
8727(typically @samp{^Y}, Control-Y) causes the process to be stopped
8728when it attempts to read input from the terminal, and control to
8729be returned to Bash. The user then manipulates the state of
8730this job, using the @code{bg} command to continue it in the
8731background, the @code{fg} command to continue it in the
8732foreground, or the @code{kill} command to kill it. A @samp{^Z}
8733takes effect immediately, and has the additional side effect of
8734causing pending output and typeahead to be discarded.
ccc6cda3 8735
bb70624e 8736There are a number of ways to refer to a job in the shell. The
74091dd4 8737character @samp{%} introduces a job specification (@dfn{jobspec}).
cce855bc 8738
bb70624e 8739Job number @code{n} may be referred to as @samp{%n}.
95732b49
JA
8740The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the
8741current job, which is the last job stopped while it was in the foreground
8742or started in the background.
8743A single @samp{%} (with no accompanying job specification) also refers
8744to the current job.
3185942a
JA
8745The previous job may be referenced using @samp{%-}.
8746If there is only a single job, @samp{%+} and @samp{%-} can both be used
8747to refer to that job.
8748In output pertaining to jobs (e.g., the output of the @code{jobs}
8749command), the current job is always flagged with a @samp{+}, and the
bb70624e 8750previous job with a @samp{-}.
cce855bc 8751
bb70624e
JA
8752A job may also be referred to
8753using a prefix of the name used to start it, or using a substring
8754that appears in its command line. For example, @samp{%ce} refers
8868edaf
CR
8755to a stopped job whose command name begins with @samp{ce}.
8756Using @samp{%?ce}, on the
bb70624e
JA
8757other hand, refers to any job containing the string @samp{ce} in
8758its command line. If the prefix or substring matches more than one job,
8759Bash reports an error.
cce855bc 8760
bb70624e
JA
8761Simply naming a job can be used to bring it into the foreground:
8762@samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the
8763background into the foreground. Similarly, @samp{%1 &} resumes
8764job 1 in the background, equivalent to @samp{bg %1}
cce855bc 8765
bb70624e
JA
8766The shell learns immediately whenever a job changes state.
8767Normally, Bash waits until it is about to print a prompt
8768before reporting changes in a job's status so as to not interrupt
f73dda09
JA
8769any other output.
8770If the @option{-b} option to the @code{set} builtin is enabled,
bb70624e 8771Bash reports such changes immediately (@pxref{The Set Builtin}).
f73dda09
JA
8772Any trap on @code{SIGCHLD} is executed for each child process
8773that exits.
ccc6cda3 8774
3185942a
JA
8775If an attempt to exit Bash is made while jobs are stopped, (or running, if
8776the @code{checkjobs} option is enabled -- see @ref{The Shopt Builtin}), the
8777shell prints a warning message, and if the @code{checkjobs} option is
8778enabled, lists the jobs and their statuses.
bb70624e
JA
8779The @code{jobs} command may then be used to inspect their status.
8780If a second attempt to exit is made without an intervening command,
3185942a 8781Bash does not print another warning, and any stopped jobs are terminated.
ccc6cda3 8782
d233b485
CR
8783When the shell is waiting for a job or process using the @code{wait}
8784builtin, and job control is enabled, @code{wait} will return when the
8868edaf 8785job changes state. The @option{-f} option causes @code{wait} to wait
d233b485
CR
8786until the job or process terminates before returning.
8787
bb70624e
JA
8788@node Job Control Builtins
8789@section Job Control Builtins
ccc6cda3
JA
8790
8791@table @code
8792
bb70624e
JA
8793@item bg
8794@btindex bg
ccc6cda3 8795@example
95732b49 8796bg [@var{jobspec} @dots{}]
ccc6cda3 8797@end example
ac50fbac 8798
95732b49 8799Resume each suspended job @var{jobspec} in the background, as if it
bb70624e
JA
8800had been started with @samp{&}.
8801If @var{jobspec} is not supplied, the current job is used.
8802The return status is zero unless it is run when job control is not
95732b49
JA
8803enabled, or, when run with job control enabled, any
8804@var{jobspec} was not found or specifies a job
8805that was started without job control.
cce855bc 8806
bb70624e
JA
8807@item fg
8808@btindex fg
8809@example
8810fg [@var{jobspec}]
8811@end example
ac50fbac 8812
bb70624e
JA
8813Resume the job @var{jobspec} in the foreground and make it the current job.
8814If @var{jobspec} is not supplied, the current job is used.
8815The return status is that of the command placed into the foreground,
8816or non-zero if run when job control is disabled or, when run with
8817job control enabled, @var{jobspec} does not specify a valid job or
8818@var{jobspec} specifies a job that was started without job control.
cce855bc 8819
bb70624e
JA
8820@item jobs
8821@btindex jobs
cce855bc 8822@example
bb70624e
JA
8823jobs [-lnprs] [@var{jobspec}]
8824jobs -x @var{command} [@var{arguments}]
cce855bc
JA
8825@end example
8826
bb70624e
JA
8827The first form lists the active jobs. The options have the
8828following meanings:
8829
8830@table @code
8831@item -l
8832List process @sc{id}s in addition to the normal information.
8833
8834@item -n
8835Display information only about jobs that have changed status since
8836the user was last notified of their status.
8837
8838@item -p
8839List only the process @sc{id} of the job's process group leader.
8840
8841@item -r
ac50fbac 8842Display only running jobs.
bb70624e
JA
8843
8844@item -s
ac50fbac 8845Display only stopped jobs.
ccc6cda3
JA
8846@end table
8847
bb70624e
JA
8848If @var{jobspec} is given,
8849output is restricted to information about that job.
8850If @var{jobspec} is not supplied, the status of all jobs is
8851listed.
ccc6cda3 8852
28ef6c31 8853If the @option{-x} option is supplied, @code{jobs} replaces any
bb70624e
JA
8854@var{jobspec} found in @var{command} or @var{arguments} with the
8855corresponding process group @sc{id}, and executes @var{command},
8856passing it @var{argument}s, returning its exit status.
ccc6cda3 8857
bb70624e
JA
8858@item kill
8859@btindex kill
ccc6cda3 8860@example
bb70624e 8861kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid}
a0c0a00f 8862kill -l|-L [@var{exit_status}]
ccc6cda3 8863@end example
ac50fbac 8864
bb70624e
JA
8865Send a signal specified by @var{sigspec} or @var{signum} to the process
8866named by job specification @var{jobspec} or process @sc{id} @var{pid}.
b80f6443
JA
8867@var{sigspec} is either a case-insensitive signal name such as
8868@code{SIGINT} (with or without the @code{SIG} prefix)
8869or a signal number; @var{signum} is a signal number.
bb70624e 8870If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used.
28ef6c31
JA
8871The @option{-l} option lists the signal names.
8872If any arguments are supplied when @option{-l} is given, the names of the
bb70624e
JA
8873signals corresponding to the arguments are listed, and the return status
8874is zero.
8875@var{exit_status} is a number specifying a signal number or the exit
8876status of a process terminated by a signal.
a0c0a00f 8877The @option{-L} option is equivalent to @option{-l}.
bb70624e
JA
8878The return status is zero if at least one signal was successfully sent,
8879or non-zero if an error occurs or an invalid option is encountered.
ccc6cda3 8880
bb70624e
JA
8881@item wait
8882@btindex wait
ccc6cda3 8883@example
8868edaf 8884wait [-fn] [-p @var{varname}] [@var{jobspec} or @var{pid} @dots{}]
ccc6cda3 8885@end example
ac50fbac 8886
95732b49
JA
8887Wait until the child process specified by each process @sc{id} @var{pid}
8888or job specification @var{jobspec} exits and return the exit status of the
8889last command waited for.
bb70624e 8890If a job spec is given, all processes in the job are waited for.
8868edaf
CR
8891If no arguments are given,
8892@code{wait} waits for all running background jobs and
8893the last-executed process substitution, if its process id is the same as
8894@var{$!},
8895and the return status is zero.
8896If the @option{-n} option is supplied, @code{wait} waits for a single job
74091dd4 8897from the list of @var{pid}s or @var{jobspec}s or, if no arguments are
8868edaf
CR
8898supplied, any job,
8899to complete and returns its exit status.
8900If none of the supplied arguments is a child of the shell, or if no arguments
8901are supplied and the shell has no unwaited-for children, the exit status
8902is 127.
8903If the @option{-p} option is supplied, the process or job identifier of the job
8904for which the exit status is returned is assigned to the variable
8905@var{varname} named by the option argument.
8906The variable will be unset initially, before any assignment.
8907This is useful only when the @option{-n} option is supplied.
8908Supplying the @option{-f} option, when job control is enabled,
8909forces @code{wait} to wait for each @var{pid} or @var{jobspec} to
74091dd4 8910terminate before returning its status, instead of returning when it changes
8868edaf 8911status.
bb70624e
JA
8912If neither @var{jobspec} nor @var{pid} specifies an active child process
8913of the shell, the return status is 127.
74091dd4
CR
8914If @code{wait} is interrupted by a signal, the return status will be greater
8915than 128, as described above (@pxref{Signals}).
8916Otherwise, the return status is the exit status
8917of the last process or job waited for.
bb70624e
JA
8918
8919@item disown
8920@btindex disown
ccc6cda3 8921@example
a0c0a00f 8922disown [-ar] [-h] [@var{jobspec} @dots{} | @var{pid} @dots{} ]
ccc6cda3 8923@end example
ac50fbac
CR
8924
8925Without options, remove each @var{jobspec} from the table of
bb70624e 8926active jobs.
28ef6c31 8927If the @option{-h} option is given, the job is not removed from the table,
bb70624e
JA
8928but is marked so that @code{SIGHUP} is not sent to the job if the shell
8929receives a @code{SIGHUP}.
ac50fbac
CR
8930If @var{jobspec} is not present, and neither the @option{-a} nor the
8931@option{-r} option is supplied, the current job is used.
28ef6c31
JA
8932If no @var{jobspec} is supplied, the @option{-a} option means to remove or
8933mark all jobs; the @option{-r} option without a @var{jobspec}
bb70624e 8934argument restricts operation to running jobs.
ccc6cda3 8935
bb70624e
JA
8936@item suspend
8937@btindex suspend
ccc6cda3 8938@example
bb70624e 8939suspend [-f]
ccc6cda3 8940@end example
ac50fbac 8941
bb70624e 8942Suspend the execution of this shell until it receives a
3185942a 8943@code{SIGCONT} signal.
74091dd4
CR
8944A login shell,
8945or a shell without job control enabled,
8946cannot be suspended; the @option{-f}
3185942a 8947option can be used to override this and force the suspension.
74091dd4
CR
8948The return status is 0 unless the shell is a login shell
8949or job control is not enabled
8950and
8951@option{-f}
8952is not supplied.
8953
bb70624e 8954@end table
ccc6cda3 8955
bb70624e
JA
8956When job control is not active, the @code{kill} and @code{wait}
8957builtins do not accept @var{jobspec} arguments. They must be
8958supplied process @sc{id}s.
ccc6cda3 8959
bb70624e
JA
8960@node Job Control Variables
8961@section Job Control Variables
ccc6cda3 8962
bb70624e 8963@vtable @code
cce855bc 8964
bb70624e
JA
8965@item auto_resume
8966This variable controls how the shell interacts with the user and
8967job control. If this variable exists then single word simple
8968commands without redirections are treated as candidates for resumption
8969of an existing job. There is no ambiguity allowed; if there is
8970more than one job beginning with the string typed, then
8971the most recently accessed job will be selected.
8972The name of a stopped job, in this context, is the command line
8973used to start it. If this variable is set to the value @samp{exact},
8974the string supplied must match the name of a stopped job exactly;
8975if set to @samp{substring},
8976the string supplied needs to match a substring of the name of a
8977stopped job. The @samp{substring} value provides functionality
8978analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}).
8979If set to any other value, the supplied string must
8980be a prefix of a stopped job's name; this provides functionality
8981analogous to the @samp{%} job @sc{id}.
cce855bc 8982
bb70624e 8983@end vtable
cce855bc 8984
bb70624e
JA
8985@set readline-appendix
8986@set history-appendix
8987@cindex Readline, how to use
b80f6443 8988@include rluser.texi
bb70624e 8989@cindex History, how to use
b80f6443 8990@include hsuser.texi
bb70624e
JA
8991@clear readline-appendix
8992@clear history-appendix
cce855bc 8993
bb70624e
JA
8994@node Installing Bash
8995@chapter Installing Bash
cce855bc 8996
bb70624e
JA
8997This chapter provides basic instructions for installing Bash on
8998the various supported platforms. The distribution supports the
8999@sc{gnu} operating systems, nearly every version of Unix, and several
9000non-Unix systems such as BeOS and Interix.
9001Other independent ports exist for
b80f6443 9002@sc{ms-dos}, @sc{os/2}, and Windows platforms.
cce855bc 9003
bb70624e
JA
9004@menu
9005* Basic Installation:: Installation instructions.
bb70624e
JA
9006* Compilers and Options:: How to set special options for various
9007 systems.
bb70624e
JA
9008* Compiling For Multiple Architectures:: How to compile Bash for more
9009 than one kind of system from
9010 the same source tree.
bb70624e 9011* Installation Names:: How to set the various paths used by the installation.
bb70624e 9012* Specifying the System Type:: How to configure Bash for a particular system.
bb70624e
JA
9013* Sharing Defaults:: How to share default configuration values among GNU
9014 programs.
bb70624e 9015* Operation Controls:: Options recognized by the configuration program.
bb70624e
JA
9016* Optional Features:: How to enable and disable optional features when
9017 building Bash.
9018@end menu
ccc6cda3 9019
bb70624e
JA
9020@node Basic Installation
9021@section Basic Installation
9022@cindex installation
9023@cindex configuration
9024@cindex Bash installation
9025@cindex Bash configuration
ccc6cda3 9026
bb70624e 9027These are installation instructions for Bash.
ccc6cda3 9028
bb70624e 9029The simplest way to compile Bash is:
ccc6cda3 9030
bb70624e 9031@enumerate
ccc6cda3 9032@item
bb70624e
JA
9033@code{cd} to the directory containing the source code and type
9034@samp{./configure} to configure Bash for your system. If you're
9035using @code{csh} on an old version of System V, you might need to
9036type @samp{sh ./configure} instead to prevent @code{csh} from trying
9037to execute @code{configure} itself.
9038
9039Running @code{configure} takes some time.
9040While running, it prints messages telling which features it is
9041checking for.
9042
ccc6cda3 9043@item
bb70624e
JA
9044Type @samp{make} to compile Bash and build the @code{bashbug} bug
9045reporting script.
9046
ccc6cda3 9047@item
bb70624e
JA
9048Optionally, type @samp{make tests} to run the Bash test suite.
9049
ccc6cda3 9050@item
bb70624e 9051Type @samp{make install} to install @code{bash} and @code{bashbug}.
74091dd4
CR
9052This will also install the manual pages and Info file, message translation
9053files, some supplemental documentation, a number of example loadable
9054builtin commands, and a set of header files for developing loadable
9055builtins.
9056You may need additional privileges to install @code{bash} to your
9057desired destination, so @samp{sudo make install} might be required.
9058More information about controlling the locations where @code{bash} and
9059other files are installed is below (@pxref{Installation Names}).
ccc6cda3 9060
bb70624e 9061@end enumerate
ccc6cda3 9062
bb70624e
JA
9063The @code{configure} shell script attempts to guess correct
9064values for various system-dependent variables used during
9065compilation. It uses those values to create a @file{Makefile} in
9066each directory of the package (the top directory, the
74091dd4 9067@file{builtins}, @file{doc}, @file{po}, and @file{support} directories,
bb70624e
JA
9068each directory under @file{lib}, and several others). It also creates a
9069@file{config.h} file containing system-dependent definitions.
9070Finally, it creates a shell script named @code{config.status} that you
9071can run in the future to recreate the current configuration, a
9072file @file{config.cache} that saves the results of its tests to
9073speed up reconfiguring, and a file @file{config.log} containing
9074compiler output (useful mainly for debugging @code{configure}).
9075If at some point
9076@file{config.cache} contains results you don't want to keep, you
9077may remove or edit it.
ccc6cda3 9078
bb70624e
JA
9079To find out more about the options and arguments that the
9080@code{configure} script understands, type
ccc6cda3 9081
bb70624e 9082@example
d233b485 9083bash-4.2$ ./configure --help
bb70624e 9084@end example
ccc6cda3 9085
bb70624e
JA
9086@noindent
9087at the Bash prompt in your Bash source directory.
ccc6cda3 9088
d233b485
CR
9089If you want to build Bash in a directory separate from the source
9090directory -- to build for multiple architectures, for example --
9091just use the full path to the configure script. The following commands
9092will build bash in a directory under @file{/usr/local/build} from
9093the source code in @file{/usr/local/src/bash-4.4}:
9094
9095@example
9096mkdir /usr/local/build/bash-4.4
9097cd /usr/local/build/bash-4.4
9098bash /usr/local/src/bash-4.4/configure
9099make
9100@end example
9101
9102See @ref{Compiling For Multiple Architectures} for more information
9103about building in a directory separate from the source.
9104
bb70624e
JA
9105If you need to do unusual things to compile Bash, please
9106try to figure out how @code{configure} could check whether or not
9107to do them, and mail diffs or instructions to
9108@email{bash-maintainers@@gnu.org} so they can be
9109considered for the next release.
ccc6cda3 9110
ac50fbac 9111The file @file{configure.ac} is used to create @code{configure}
74091dd4
CR
9112by a program called Autoconf.
9113You only need @file{configure.ac} if you want to change it or regenerate
9114@code{configure} using a newer version of Autoconf.
9115If you do this, make sure you are using Autoconf version 2.69 or
bb70624e 9116newer.
ccc6cda3 9117
bb70624e
JA
9118You can remove the program binaries and object files from the
9119source code directory by typing @samp{make clean}. To also remove the
9120files that @code{configure} created (so you can compile Bash for
9121a different kind of computer), type @samp{make distclean}.
ccc6cda3 9122
bb70624e
JA
9123@node Compilers and Options
9124@section Compilers and Options
ccc6cda3 9125
bb70624e
JA
9126Some systems require unusual options for compilation or linking
9127that the @code{configure} script does not know about. You can
9128give @code{configure} initial values for variables by setting
9129them in the environment. Using a Bourne-compatible shell, you
9130can do that on the command line like this:
ccc6cda3 9131
bb70624e
JA
9132@example
9133CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
9134@end example
ccc6cda3 9135
bb70624e 9136On systems that have the @code{env} program, you can do it like this:
ccc6cda3 9137
bb70624e
JA
9138@example
9139env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
9140@end example
ccc6cda3 9141
bb70624e
JA
9142The configuration process uses GCC to build Bash if it
9143is available.
cce855bc 9144
bb70624e
JA
9145@node Compiling For Multiple Architectures
9146@section Compiling For Multiple Architectures
ccc6cda3 9147
bb70624e
JA
9148You can compile Bash for more than one kind of computer at the
9149same time, by placing the object files for each architecture in their
9150own directory. To do this, you must use a version of @code{make} that
9151supports the @code{VPATH} variable, such as GNU @code{make}.
9152@code{cd} to the
9153directory where you want the object files and executables to go and run
d233b485
CR
9154the @code{configure} script from the source directory
9155(@pxref{Basic Installation}).
9156You may need to
28ef6c31 9157supply the @option{--srcdir=PATH} argument to tell @code{configure} where the
bb70624e
JA
9158source files are. @code{configure} automatically checks for the
9159source code in the directory that @code{configure} is in and in `..'.
ccc6cda3 9160
74091dd4 9161If you have to use a @code{make} that does not support the @code{VPATH}
bb70624e
JA
9162variable, you can compile Bash for one architecture at a
9163time in the source code directory. After you have installed
9164Bash for one architecture, use @samp{make distclean} before
9165reconfiguring for another architecture.
ccc6cda3 9166
bb70624e
JA
9167Alternatively, if your system supports symbolic links, you can use the
9168@file{support/mkclone} script to create a build tree which has
9169symbolic links back to each file in the source directory. Here's an
9170example that creates a build directory in the current directory from a
9171source directory @file{/usr/gnu/src/bash-2.0}:
9172
9173@example
9174bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 .
9175@end example
9176
9177@noindent
9178The @code{mkclone} script requires Bash, so you must have already built
9179Bash for at least one architecture before you can create build
9180directories for other architectures.
9181
9182@node Installation Names
9183@section Installation Names
ccc6cda3 9184
bb70624e 9185By default, @samp{make install} will install into
74091dd4
CR
9186@file{/usr/local/bin}, @file{/usr/local/man}, etc.;
9187that is, the @dfn{installation prefix} defaults to @file{/usr/local}.
9188You can specify an installation prefix other than @file{/usr/local} by
f73dda09 9189giving @code{configure} the option @option{--prefix=@var{PATH}},
74091dd4
CR
9190or by specifying a value for the @env{prefix} @samp{make}
9191variable when running @samp{make install}
9192(e.g., @samp{make install prefix=@var{PATH}}).
9193The @env{prefix} variable provides a default for @env{exec_prefix} and
9194other variables used when installing bash.
ccc6cda3 9195
bb70624e
JA
9196You can specify separate installation prefixes for
9197architecture-specific files and architecture-independent files.
9198If you give @code{configure} the option
28ef6c31 9199@option{--exec-prefix=@var{PATH}}, @samp{make install} will use
bb70624e 9200@var{PATH} as the prefix for installing programs and libraries.
74091dd4
CR
9201Documentation and other data files will still use the regular prefix.
9202
9203If you would like to change the installation locations for a single run,
9204you can specify these variables as arguments to @code{make}:
9205@samp{make install exec_prefix=/} will install @code{bash} and
9206@code{bashbug} into @file{/bin} instead of the default @file{/usr/local/bin}.
9207
9208If you want to see the files bash will install and where it will install
9209them without changing anything on your system, specify the variable
9210@env{DESTDIR} as an argument to @code{make}. Its value should be the
9211absolute directory path you'd like to use as the root of your sample
9212installation tree. For example,
9213
9214@example
9215mkdir /fs1/bash-install
9216make install DESTDIR=/fs1/bash-install
9217@end example
9218
9219@noindent
9220will install @code{bash} into @file{/fs1/bash-install/usr/local/bin/bash},
9221the documentation into directories within
9222@file{/fs1/bash-install/usr/local/share}, the example loadable builtins into
9223@file{/fs1/bash-install/usr/local/lib/bash}, and so on.
9224You can use the usual @env{exec_prefix} and @env{prefix} variables to alter
9225the directory paths beneath the value of @env{DESTDIR}.
9226
9227The GNU Makefile standards provide a more complete description of these
9228variables and their effects.
cce855bc 9229
bb70624e
JA
9230@node Specifying the System Type
9231@section Specifying the System Type
ccc6cda3 9232
bb70624e 9233There may be some features @code{configure} can not figure out
74091dd4 9234automatically, but needs to determine by the type of host Bash
bb70624e
JA
9235will run on. Usually @code{configure} can figure that
9236out, but if it prints a message saying it can not guess the host
28ef6c31 9237type, give it the @option{--host=TYPE} option. @samp{TYPE} can
bb70624e
JA
9238either be a short name for the system type, such as @samp{sun4},
9239or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM}
f73dda09 9240(e.g., @samp{i386-unknown-freebsd4.2}).
ccc6cda3 9241
bb70624e
JA
9242See the file @file{support/config.sub} for the possible
9243values of each field.
ccc6cda3 9244
bb70624e
JA
9245@node Sharing Defaults
9246@section Sharing Defaults
ccc6cda3 9247
bb70624e
JA
9248If you want to set default values for @code{configure} scripts to
9249share, you can create a site shell script called
9250@code{config.site} that gives default values for variables like
9251@code{CC}, @code{cache_file}, and @code{prefix}. @code{configure}
9252looks for @file{PREFIX/share/config.site} if it exists, then
9253@file{PREFIX/etc/config.site} if it exists. Or, you can set the
9254@code{CONFIG_SITE} environment variable to the location of the site
9255script. A warning: the Bash @code{configure} looks for a site script,
9256but not all @code{configure} scripts do.
d166f048 9257
bb70624e
JA
9258@node Operation Controls
9259@section Operation Controls
ccc6cda3 9260
bb70624e
JA
9261@code{configure} recognizes the following options to control how it
9262operates.
ccc6cda3 9263
bb70624e 9264@table @code
ccc6cda3 9265
bb70624e
JA
9266@item --cache-file=@var{file}
9267Use and save the results of the tests in
9268@var{file} instead of @file{./config.cache}. Set @var{file} to
9269@file{/dev/null} to disable caching, for debugging
9270@code{configure}.
ccc6cda3 9271
bb70624e
JA
9272@item --help
9273Print a summary of the options to @code{configure}, and exit.
ccc6cda3 9274
bb70624e
JA
9275@item --quiet
9276@itemx --silent
9277@itemx -q
9278Do not print messages saying which checks are being made.
ccc6cda3 9279
bb70624e
JA
9280@item --srcdir=@var{dir}
9281Look for the Bash source code in directory @var{dir}. Usually
9282@code{configure} can determine that directory automatically.
ccc6cda3 9283
bb70624e
JA
9284@item --version
9285Print the version of Autoconf used to generate the @code{configure}
9286script, and exit.
9287@end table
ccc6cda3 9288
bb70624e
JA
9289@code{configure} also accepts some other, not widely used, boilerplate
9290options. @samp{configure --help} prints the complete list.
ccc6cda3 9291
bb70624e
JA
9292@node Optional Features
9293@section Optional Features
ccc6cda3 9294
28ef6c31 9295The Bash @code{configure} has a number of @option{--enable-@var{feature}}
bb70624e 9296options, where @var{feature} indicates an optional part of Bash.
28ef6c31 9297There are also several @option{--with-@var{package}} options,
bb70624e
JA
9298where @var{package} is something like @samp{bash-malloc} or @samp{purify}.
9299To turn off the default use of a package, use
28ef6c31
JA
9300@option{--without-@var{package}}. To configure Bash without a feature
9301that is enabled by default, use @option{--disable-@var{feature}}.
ccc6cda3 9302
28ef6c31
JA
9303Here is a complete list of the @option{--enable-} and
9304@option{--with-} options that the Bash @code{configure} recognizes.
ccc6cda3 9305
bb70624e
JA
9306@table @code
9307@item --with-afs
9308Define if you are using the Andrew File System from Transarc.
ccc6cda3 9309
bb70624e
JA
9310@item --with-bash-malloc
9311Use the Bash version of
95732b49 9312@code{malloc} in the directory @file{lib/malloc}. This is not the same
bb70624e 9313@code{malloc} that appears in @sc{gnu} libc, but an older version
95732b49
JA
9314originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc}
9315is very fast, but wastes some space on each allocation.
bb70624e
JA
9316This option is enabled by default.
9317The @file{NOTES} file contains a list of systems for
9318which this should be turned off, and @code{configure} disables this
9319option automatically for a number of systems.
ccc6cda3 9320
bb70624e
JA
9321@item --with-curses
9322Use the curses library instead of the termcap library. This should
9323be supplied if your system has an inadequate or incomplete termcap
9324database.
ccc6cda3 9325
bb70624e
JA
9326@item --with-gnu-malloc
9327A synonym for @code{--with-bash-malloc}.
ccc6cda3 9328
f73dda09 9329@item --with-installed-readline[=@var{PREFIX}]
bb70624e
JA
9330Define this to make Bash link with a locally-installed version of Readline
9331rather than the version in @file{lib/readline}. This works only with
95732b49 9332Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not
f73dda09
JA
9333supplied, @code{configure} uses the values of the make variables
9334@code{includedir} and @code{libdir}, which are subdirectories of @code{prefix}
9335by default, to find the installed version of Readline if it is not in
9336the standard system include and library directories.
9337If @var{PREFIX} is @code{no}, Bash links with the version in
9338@file{lib/readline}.
9339If @var{PREFIX} is set to any other value, @code{configure} treats it as
9340a directory pathname and looks for
9341the installed version of Readline in subdirectories of that directory
9342(include files in @var{PREFIX}/@code{include} and the library in
9343@var{PREFIX}/@code{lib}).
ccc6cda3 9344
74091dd4
CR
9345@item --with-libintl-prefix[=@var{PREFIX}]
9346Define this to make Bash link with a locally-installed version of the
9347libintl library instead of the version in @file{lib/intl}.
9348
9349@item --with-libiconv-prefix[=@var{PREFIX}]
9350Define this to make Bash look for libiconv in @var{PREFIX} instead of the
9351standard system locations. There is no version included with Bash.
ccc6cda3 9352
bb70624e
JA
9353@item --enable-minimal-config
9354This produces a shell with minimal features, close to the historical
9355Bourne shell.
9356@end table
ccc6cda3 9357
28ef6c31 9358There are several @option{--enable-} options that alter how Bash is
74091dd4 9359compiled, linked, and installed, rather than changing run-time features.
ccc6cda3
JA
9360
9361@table @code
f73dda09 9362@item --enable-largefile
d233b485 9363Enable support for @uref{http://www.unix.org/version2/whatsnew/lfs20mar.html,
f73dda09 9364large files} if the operating system requires special compiler options
7117c2d2
JA
9365to build programs which can access large files. This is enabled by
9366default, if the operating system provides large file support.
f73dda09 9367
bb70624e
JA
9368@item --enable-profiling
9369This builds a Bash binary that produces profiling information to be
9370processed by @code{gprof} each time it is executed.
ccc6cda3 9371
74091dd4
CR
9372@item --enable-separate-helpfiles
9373Use external files for the documentation displayed by the @code{help} builtin
9374instead of storing the text internally.
9375
bb70624e
JA
9376@item --enable-static-link
9377This causes Bash to be linked statically, if @code{gcc} is being used.
9378This could be used to build a version to use as root's shell.
74091dd4 9379
bb70624e
JA
9380@end table
9381
9382The @samp{minimal-config} option can be used to disable all of
9383the following options, but it is processed first, so individual
9384options may be enabled using @samp{enable-@var{feature}}.
9385
74091dd4
CR
9386All of the following options except for
9387@samp{alt-array-implementation},
9388@samp{disabled-builtins},
9389@samp{direxpand-default},
9390@samp{strict-posix-default},
9391and
bb70624e
JA
9392@samp{xpg-echo-default} are
9393enabled by default, unless the operating system does not provide the
9394necessary support.
9395
9396@table @code
9397@item --enable-alias
9398Allow alias expansion and include the @code{alias} and @code{unalias}
9399builtins (@pxref{Aliases}).
ccc6cda3 9400
74091dd4
CR
9401@item --enable-alt-array-implementation
9402This builds bash using an alternate implementation of arrays
9403(@pxref{Arrays}) that provides faster access at the expense of using
9404more memory (sometimes many times more, depending on how sparse an array is).
9405
bb70624e
JA
9406@item --enable-arith-for-command
9407Include support for the alternate form of the @code{for} command
9408that behaves like the C language @code{for} statement
9409(@pxref{Looping Constructs}).
ccc6cda3 9410
bb70624e
JA
9411@item --enable-array-variables
9412Include support for one-dimensional array shell variables
9413(@pxref{Arrays}).
ccc6cda3 9414
bb70624e
JA
9415@item --enable-bang-history
9416Include support for @code{csh}-like history substitution
9417(@pxref{History Interaction}).
ccc6cda3 9418
bb70624e
JA
9419@item --enable-brace-expansion
9420Include @code{csh}-like brace expansion
9421( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ).
9422See @ref{Brace Expansion}, for a complete description.
ccc6cda3 9423
3185942a
JA
9424@item --enable-casemod-attributes
9425Include support for case-modifying attributes in the @code{declare} builtin
74091dd4 9426and assignment statements. Variables with the @code{uppercase} attribute,
3185942a
JA
9427for example, will have their values converted to uppercase upon assignment.
9428
9429@item --enable-casemod-expansion
9430Include support for case-modifying word expansions.
9431
bb70624e
JA
9432@item --enable-command-timing
9433Include support for recognizing @code{time} as a reserved word and for
9434displaying timing statistics for the pipeline following @code{time}
9435(@pxref{Pipelines}).
9436This allows pipelines as well as shell builtins and functions to be timed.
ccc6cda3 9437
bb70624e 9438@item --enable-cond-command
b80f6443
JA
9439Include support for the @code{[[} conditional command.
9440(@pxref{Conditional Constructs}).
9441
9442@item --enable-cond-regexp
495aee44 9443Include support for matching @sc{posix} regular expressions using the
b80f6443 9444@samp{=~} binary operator in the @code{[[} conditional command.
bb70624e 9445(@pxref{Conditional Constructs}).
ccc6cda3 9446
3185942a
JA
9447@item --enable-coprocesses
9448Include support for coprocesses and the @code{coproc} reserved word
9449(@pxref{Pipelines}).
9450
95732b49
JA
9451@item --enable-debugger
9452Include support for the bash debugger (distributed separately).
9453
d233b485
CR
9454@item --enable-dev-fd-stat-broken
9455If calling @code{stat} on /dev/fd/@var{N} returns different results than
9456calling @code{fstat} on file descriptor @var{N}, supply this option to
9457enable a workaround.
9458This has implications for conditional commands that test file attributes.
9459
ac50fbac
CR
9460@item --enable-direxpand-default
9461Cause the @code{direxpand} shell option (@pxref{The Shopt Builtin})
9462to be enabled by default when the shell starts.
9463It is normally disabled by default.
9464
bb70624e
JA
9465@item --enable-directory-stack
9466Include support for a @code{csh}-like directory stack and the
9467@code{pushd}, @code{popd}, and @code{dirs} builtins
9468(@pxref{The Directory Stack}).
ccc6cda3 9469
bb70624e
JA
9470@item --enable-disabled-builtins
9471Allow builtin commands to be invoked via @samp{builtin xxx}
9472even after @code{xxx} has been disabled using @samp{enable -n xxx}.
9473See @ref{Bash Builtins}, for details of the @code{builtin} and
9474@code{enable} builtin commands.
ccc6cda3 9475
bb70624e
JA
9476@item --enable-dparen-arithmetic
9477Include support for the @code{((@dots{}))} command
9478(@pxref{Conditional Constructs}).
ccc6cda3 9479
bb70624e
JA
9480@item --enable-extended-glob
9481Include support for the extended pattern matching features described
9482above under @ref{Pattern Matching}.
ccc6cda3 9483
0001803f 9484@item --enable-extended-glob-default
74091dd4 9485Set the default value of the @code{extglob} shell option described
0001803f
CR
9486above under @ref{The Shopt Builtin} to be enabled.
9487
a0c0a00f
CR
9488@item --enable-function-import
9489Include support for importing function definitions exported by another
9490instance of the shell from the environment. This option is enabled by
9491default.
9492
ac50fbac 9493@item --enable-glob-asciirange-default
74091dd4 9494Set the default value of the @code{globasciiranges} shell option described
ac50fbac
CR
9495above under @ref{The Shopt Builtin} to be enabled.
9496This controls the behavior of character ranges when used in pattern matching
9497bracket expressions.
9498
bb70624e
JA
9499@item --enable-help-builtin
9500Include the @code{help} builtin, which displays help on shell builtins and
9501variables (@pxref{Bash Builtins}).
ccc6cda3 9502
bb70624e
JA
9503@item --enable-history
9504Include command history and the @code{fc} and @code{history}
9505builtin commands (@pxref{Bash History Facilities}).
ccc6cda3 9506
bb70624e
JA
9507@item --enable-job-control
9508This enables the job control features (@pxref{Job Control}),
9509if the operating system supports them.
ccc6cda3 9510
b80f6443
JA
9511@item --enable-multibyte
9512This enables support for multibyte characters if the operating
9513system provides the necessary support.
9514
bb70624e
JA
9515@item --enable-net-redirections
9516This enables the special handling of filenames of the form
9517@code{/dev/tcp/@var{host}/@var{port}} and
9518@code{/dev/udp/@var{host}/@var{port}}
9519when used in redirections (@pxref{Redirections}).
ccc6cda3 9520
bb70624e
JA
9521@item --enable-process-substitution
9522This enables process substitution (@pxref{Process Substitution}) if
9523the operating system provides the necessary support.
ccc6cda3 9524
95732b49
JA
9525@item --enable-progcomp
9526Enable the programmable completion facilities
9527(@pxref{Programmable Completion}).
9528If Readline is not enabled, this option has no effect.
9529
bb70624e
JA
9530@item --enable-prompt-string-decoding
9531Turn on the interpretation of a number of backslash-escaped characters
d233b485 9532in the @env{$PS0}, @env{$PS1}, @env{$PS2}, and @env{$PS4} prompt
ac50fbac 9533strings. See @ref{Controlling the Prompt}, for a complete list of prompt
bb70624e 9534string escape sequences.
ccc6cda3 9535
bb70624e
JA
9536@item --enable-readline
9537Include support for command-line editing and history with the Bash
9538version of the Readline library (@pxref{Command Line Editing}).
ccc6cda3 9539
bb70624e
JA
9540@item --enable-restricted
9541Include support for a @dfn{restricted shell}. If this is enabled, Bash,
9542when called as @code{rbash}, enters a restricted mode. See
9543@ref{The Restricted Shell}, for a description of restricted mode.
ccc6cda3 9544
bb70624e 9545@item --enable-select
495aee44
CR
9546Include the @code{select} compound command, which allows the generation of
9547simple menus (@pxref{Conditional Constructs}).
ccc6cda3 9548
95732b49
JA
9549@item --enable-single-help-strings
9550Store the text displayed by the @code{help} builtin as a single string for
9551each help topic. This aids in translating the text to different languages.
9552You may need to disable this if your compiler cannot handle very long string
9553literals.
9554
9555@item --enable-strict-posix-default
9556Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}).
9557
74091dd4
CR
9558@item --enable-translatable-strings
9559Enable support for @code{$"@var{string}"} translatable strings
9560(@pxref{Locale Translation}).
9561
bb70624e
JA
9562@item --enable-usg-echo-default
9563A synonym for @code{--enable-xpg-echo-default}.
ccc6cda3 9564
bb70624e
JA
9565@item --enable-xpg-echo-default
9566Make the @code{echo} builtin expand backslash-escaped characters by default,
28ef6c31 9567without requiring the @option{-e} option.
bb70624e
JA
9568This sets the default value of the @code{xpg_echo} shell option to @code{on},
9569which makes the Bash @code{echo} behave more like the version specified in
95732b49 9570the Single Unix Specification, version 3.
bb70624e
JA
9571@xref{Bash Builtins}, for a description of the escape sequences that
9572@code{echo} recognizes.
bb70624e 9573@end table
ccc6cda3 9574
bb70624e
JA
9575The file @file{config-top.h} contains C Preprocessor
9576@samp{#define} statements for options which are not settable from
9577@code{configure}.
9578Some of these are not meant to be changed; beware of the consequences if
9579you do.
9580Read the comments associated with each definition for more
9581information about its effect.
ccc6cda3 9582
bb70624e
JA
9583@node Reporting Bugs
9584@appendix Reporting Bugs
ccc6cda3 9585
bb70624e
JA
9586Please report all bugs you find in Bash.
9587But first, you should
9588make sure that it really is a bug, and that it appears in the latest
b80f6443
JA
9589version of Bash.
9590The latest version of Bash is always available for FTP from
74091dd4
CR
9591@uref{ftp://ftp.gnu.org/pub/gnu/bash/} and from
9592@uref{http://git.savannah.gnu.org/cgit/bash.git/snapshot/bash-master.tar.gz}.
ccc6cda3 9593
bb70624e
JA
9594Once you have determined that a bug actually exists, use the
9595@code{bashbug} command to submit a bug report.
9596If you have a fix, you are encouraged to mail that as well!
9597Suggestions and `philosophical' bug reports may be mailed
9598to @email{bug-bash@@gnu.org} or posted to the Usenet
9599newsgroup @code{gnu.bash.bug}.
ccc6cda3 9600
bb70624e
JA
9601All bug reports should include:
9602@itemize @bullet
9603@item
9604The version number of Bash.
9605@item
9606The hardware and operating system.
9607@item
9608The compiler used to compile Bash.
9609@item
9610A description of the bug behaviour.
9611@item
9612A short script or `recipe' which exercises the bug and may be used
9613to reproduce it.
9614@end itemize
ccc6cda3 9615
bb70624e
JA
9616@noindent
9617@code{bashbug} inserts the first three items automatically into
9618the template it provides for filing a bug report.
ccc6cda3 9619
bb70624e 9620Please send all reports concerning this manual to
ac50fbac 9621@email{bug-bash@@gnu.org}.
ccc6cda3 9622
bb70624e
JA
9623@node Major Differences From The Bourne Shell
9624@appendix Major Differences From The Bourne Shell
d166f048 9625
bb70624e
JA
9626Bash implements essentially the same grammar, parameter and
9627variable expansion, redirection, and quoting as the Bourne Shell.
0628567a 9628Bash uses the @sc{posix} standard as the specification of
bb70624e
JA
9629how these features are to be implemented. There are some
9630differences between the traditional Bourne shell and Bash; this
9631section quickly details the differences of significance. A
9632number of these differences are explained in greater depth in
9633previous sections.
95732b49
JA
9634This section uses the version of @code{sh} included in SVR4.2 (the
9635last version of the historical Bourne shell) as the baseline reference.
ccc6cda3 9636
bb70624e 9637@itemize @bullet
ccc6cda3 9638
ccc6cda3 9639@item
bb70624e 9640Bash is @sc{posix}-conformant, even where the @sc{posix} specification
28ef6c31 9641differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}).
ccc6cda3
JA
9642
9643@item
bb70624e 9644Bash has multi-character invocation options (@pxref{Invoking Bash}).
ccc6cda3
JA
9645
9646@item
bb70624e
JA
9647Bash has command-line editing (@pxref{Command Line Editing}) and
9648the @code{bind} builtin.
ccc6cda3
JA
9649
9650@item
bb70624e 9651Bash provides a programmable word completion mechanism
3185942a
JA
9652(@pxref{Programmable Completion}), and builtin commands
9653@code{complete}, @code{compgen}, and @code{compopt}, to
9654manipulate it.
ccc6cda3 9655
bb70624e
JA
9656@item
9657Bash has command history (@pxref{Bash History Facilities}) and the
9658@code{history} and @code{fc} builtins to manipulate it.
b80f6443
JA
9659The Bash history list maintains timestamp information and uses the
9660value of the @code{HISTTIMEFORMAT} variable to display it.
ccc6cda3 9661
bb70624e
JA
9662@item
9663Bash implements @code{csh}-like history expansion
9664(@pxref{History Interaction}).
ccc6cda3 9665
bb70624e
JA
9666@item
9667Bash has one-dimensional array variables (@pxref{Arrays}), and the
9668appropriate variable expansions and assignment syntax to use them.
9669Several of the Bash builtins take options to act on arrays.
9670Bash provides a number of built-in array variables.
ccc6cda3 9671
bb70624e
JA
9672@item
9673The @code{$'@dots{}'} quoting syntax, which expands ANSI-C
9674backslash-escaped characters in the text between the single quotes,
9675is supported (@pxref{ANSI-C Quoting}).
ccc6cda3 9676
bb70624e
JA
9677@item
9678Bash supports the @code{$"@dots{}"} quoting syntax to do
9679locale-specific translation of the characters between the double
28ef6c31 9680quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings}
bb70624e
JA
9681invocation options list the translatable strings found in a script
9682(@pxref{Locale Translation}).
ccc6cda3 9683
bb70624e
JA
9684@item
9685Bash implements the @code{!} keyword to negate the return value of
9686a pipeline (@pxref{Pipelines}).
9687Very useful when an @code{if} statement needs to act only if a test fails.
95732b49
JA
9688The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to
9689return a failure status if any command fails.
ccc6cda3 9690
bb70624e
JA
9691@item
9692Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}).
9693The display of the timing statistics may be controlled with the
28ef6c31 9694@env{TIMEFORMAT} variable.
ccc6cda3 9695
bb70624e
JA
9696@item
9697Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))}
9698arithmetic for command, similar to the C language (@pxref{Looping Constructs}).
ccc6cda3 9699
bb70624e
JA
9700@item
9701Bash includes the @code{select} compound command, which allows the
9702generation of simple menus (@pxref{Conditional Constructs}).
ccc6cda3 9703
bb70624e
JA
9704@item
9705Bash includes the @code{[[} compound command, which makes conditional
95732b49
JA
9706testing part of the shell grammar (@pxref{Conditional Constructs}), including
9707optional regular expression matching.
9708
9709@item
9710Bash provides optional case-insensitive matching for the @code{case} and
9711@code{[[} constructs.
ccc6cda3 9712
bb70624e
JA
9713@item
9714Bash includes brace expansion (@pxref{Brace Expansion}) and tilde
9715expansion (@pxref{Tilde Expansion}).
ccc6cda3 9716
bb70624e
JA
9717@item
9718Bash implements command aliases and the @code{alias} and @code{unalias}
9719builtins (@pxref{Aliases}).
ccc6cda3 9720
bb70624e
JA
9721@item
9722Bash provides shell arithmetic, the @code{((} compound command
9723(@pxref{Conditional Constructs}),
9724and arithmetic expansion (@pxref{Shell Arithmetic}).
ccc6cda3 9725
bb70624e
JA
9726@item
9727Variables present in the shell's initial environment are automatically
9728exported to child processes. The Bourne shell does not normally do
9729this unless the variables are explicitly marked using the @code{export}
9730command.
ccc6cda3 9731
95732b49
JA
9732@item
9733Bash supports the @samp{+=} assignment operator, which appends to the value
9734of the variable named on the left hand side.
9735
bb70624e
JA
9736@item
9737Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%}
9738and @samp{##} expansions to remove leading or trailing substrings from
9739variable values (@pxref{Shell Parameter Expansion}).
ccc6cda3 9740
bb70624e
JA
9741@item
9742The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}},
9743is supported (@pxref{Shell Parameter Expansion}).
ccc6cda3 9744
bb70624e
JA
9745@item
9746The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}},
9747which expands to the substring of @code{var}'s value of length
9748@var{length}, beginning at @var{offset}, is present
9749(@pxref{Shell Parameter Expansion}).
ccc6cda3 9750
bb70624e
JA
9751@item
9752The expansion
74091dd4 9753@code{$@{@var{var}/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}},
bb70624e 9754which matches @var{pattern} and replaces it with @var{replacement} in
74091dd4 9755the value of @var{var}, is available (@pxref{Shell Parameter Expansion}).
ccc6cda3 9756
bb70624e 9757@item
ac50fbac 9758The expansion @code{$@{!@var{prefix}*@}} expansion, which expands to
bb70624e
JA
9759the names of all shell variables whose names begin with @var{prefix},
9760is available (@pxref{Shell Parameter Expansion}).
ccc6cda3 9761
bb70624e 9762@item
74091dd4 9763Bash has indirect variable expansion using @code{$@{!word@}}
bb70624e 9764(@pxref{Shell Parameter Expansion}).
ccc6cda3 9765
bb70624e
JA
9766@item
9767Bash can expand positional parameters beyond @code{$9} using
9768@code{$@{@var{num}@}}.
ccc6cda3 9769
bb70624e
JA
9770@item
9771The @sc{posix} @code{$()} form of command substitution
9772is implemented (@pxref{Command Substitution}),
9773and preferred to the Bourne shell's @code{``} (which
9774is also implemented for backwards compatibility).
ccc6cda3 9775
bb70624e
JA
9776@item
9777Bash has process substitution (@pxref{Process Substitution}).
ccc6cda3 9778
bb70624e
JA
9779@item
9780Bash automatically assigns variables that provide information about the
28ef6c31
JA
9781current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host
9782(@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}),
9783and the instance of Bash that is running (@env{BASH},
9784@env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables},
bb70624e 9785for details.
ccc6cda3 9786
bb70624e 9787@item
28ef6c31 9788The @env{IFS} variable is used to split only the results of expansion,
bb70624e
JA
9789not all words (@pxref{Word Splitting}).
9790This closes a longstanding shell security hole.
ccc6cda3 9791
ac50fbac
CR
9792@item
9793The filename expansion bracket expression code uses @samp{!} and @samp{^}
9794to negate the set of characters between the brackets.
9795The Bourne shell uses only @samp{!}.
9796
bb70624e 9797@item
0628567a 9798Bash implements the full set of @sc{posix} filename expansion operators,
74091dd4
CR
9799including character classes, equivalence classes, and
9800collating symbols (@pxref{Filename Expansion}).
ccc6cda3 9801
bb70624e
JA
9802@item
9803Bash implements extended pattern matching features when the @code{extglob}
9804shell option is enabled (@pxref{Pattern Matching}).
ccc6cda3 9805
bb70624e
JA
9806@item
9807It is possible to have a variable and a function with the same name;
9808@code{sh} does not separate the two name spaces.
ccc6cda3 9809
bb70624e
JA
9810@item
9811Bash functions are permitted to have local variables using the
9812@code{local} builtin, and thus useful recursive functions may be written
9813(@pxref{Bash Builtins}).
ccc6cda3 9814
bb70624e
JA
9815@item
9816Variable assignments preceding commands affect only that command, even
9817builtins and functions (@pxref{Environment}).
9818In @code{sh}, all variable assignments
9819preceding commands are global unless the command is executed from the
9820file system.
ccc6cda3 9821
bb70624e
JA
9822@item
9823Bash performs filename expansion on filenames specified as operands
9824to input and output redirection operators (@pxref{Redirections}).
ccc6cda3 9825
bb70624e
JA
9826@item
9827Bash contains the @samp{<>} redirection operator, allowing a file to be
9828opened for both reading and writing, and the @samp{&>} redirection
9829operator, for directing standard output and standard error to the same
9830file (@pxref{Redirections}).
ccc6cda3 9831
95732b49
JA
9832@item
9833Bash includes the @samp{<<<} redirection operator, allowing a string to
9834be used as the standard input to a command.
9835
9836@item
9837Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}}
9838redirection operators, which move one file descriptor to another.
9839
bb70624e
JA
9840@item
9841Bash treats a number of filenames specially when they are
9842used in redirection operators (@pxref{Redirections}).
ccc6cda3 9843
bb70624e
JA
9844@item
9845Bash can open network connections to arbitrary machines and services
9846with the redirection operators (@pxref{Redirections}).
ccc6cda3 9847
bb70624e
JA
9848@item
9849The @code{noclobber} option is available to avoid overwriting existing
9850files with output redirection (@pxref{The Set Builtin}).
9851The @samp{>|} redirection operator may be used to override @code{noclobber}.
d166f048 9852
bb70624e
JA
9853@item
9854The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins})
28ef6c31 9855each take @option{-L} and @option{-P} options to switch between logical and
bb70624e 9856physical modes.
d166f048 9857
bb70624e
JA
9858@item
9859Bash allows a function to override a builtin with the same name, and provides
9860access to that builtin's functionality within the function via the
9861@code{builtin} and @code{command} builtins (@pxref{Bash Builtins}).
d166f048 9862
bb70624e
JA
9863@item
9864The @code{command} builtin allows selective disabling of functions
9865when command lookup is performed (@pxref{Bash Builtins}).
ccc6cda3 9866
bb70624e
JA
9867@item
9868Individual builtins may be enabled or disabled using the @code{enable}
9869builtin (@pxref{Bash Builtins}).
b72432fd 9870
bb70624e
JA
9871@item
9872The Bash @code{exec} builtin takes additional options that allow users
9873to control the contents of the environment passed to the executed
9874command, and what the zeroth argument to the command is to be
9875(@pxref{Bourne Shell Builtins}).
ccc6cda3 9876
bb70624e
JA
9877@item
9878Shell functions may be exported to children via the environment
9879using @code{export -f} (@pxref{Shell Functions}).
ccc6cda3 9880
bb70624e
JA
9881@item
9882The Bash @code{export}, @code{readonly}, and @code{declare} builtins can
28ef6c31 9883take a @option{-f} option to act on shell functions, a @option{-p} option to
bb70624e 9884display variables with various attributes set in a format that can be
28ef6c31 9885used as shell input, a @option{-n} option to remove various variable
bb70624e
JA
9886attributes, and @samp{name=value} arguments to set variable attributes
9887and values simultaneously.
cce855bc 9888
bb70624e
JA
9889@item
9890The Bash @code{hash} builtin allows a name to be associated with
9891an arbitrary filename, even when that filename cannot be found by
28ef6c31 9892searching the @env{$PATH}, using @samp{hash -p}
bb70624e 9893(@pxref{Bourne Shell Builtins}).
cce855bc 9894
bb70624e
JA
9895@item
9896Bash includes a @code{help} builtin for quick reference to shell
9897facilities (@pxref{Bash Builtins}).
cce855bc 9898
bb70624e
JA
9899@item
9900The @code{printf} builtin is available to display formatted output
9901(@pxref{Bash Builtins}).
ccc6cda3 9902
bb70624e
JA
9903@item
9904The Bash @code{read} builtin (@pxref{Bash Builtins})
9905will read a line ending in @samp{\} with
28ef6c31 9906the @option{-r} option, and will use the @env{REPLY} variable as a
bb70624e
JA
9907default if no non-option arguments are supplied.
9908The Bash @code{read} builtin
28ef6c31
JA
9909also accepts a prompt string with the @option{-p} option and will use
9910Readline to obtain the line when given the @option{-e} option.
bb70624e 9911The @code{read} builtin also has additional options to control input:
28ef6c31
JA
9912the @option{-s} option will turn off echoing of input characters as
9913they are read, the @option{-t} option will allow @code{read} to time out
bb70624e 9914if input does not arrive within a specified number of seconds, the
28ef6c31
JA
9915@option{-n} option will allow reading only a specified number of
9916characters rather than a full line, and the @option{-d} option will read
bb70624e 9917until a particular character rather than newline.
ccc6cda3 9918
bb70624e
JA
9919@item
9920The @code{return} builtin may be used to abort execution of scripts
9921executed with the @code{.} or @code{source} builtins
9922(@pxref{Bourne Shell Builtins}).
ccc6cda3 9923
bb70624e
JA
9924@item
9925Bash includes the @code{shopt} builtin, for finer control of shell
3185942a 9926optional capabilities (@pxref{The Shopt Builtin}), and allows these options
f73dda09 9927to be set and unset at shell invocation (@pxref{Invoking Bash}).
ccc6cda3 9928
bb70624e
JA
9929@item
9930Bash has much more optional behavior controllable with the @code{set}
9931builtin (@pxref{The Set Builtin}).
ccc6cda3 9932
b80f6443 9933@item
ac50fbac 9934The @samp{-x} (@option{xtrace}) option displays commands other than
b80f6443
JA
9935simple commands when performing an execution trace
9936(@pxref{The Set Builtin}).
9937
bb70624e
JA
9938@item
9939The @code{test} builtin (@pxref{Bourne Shell Builtins})
9940is slightly different, as it implements the @sc{posix} algorithm,
9941which specifies the behavior based on the number of arguments.
d166f048 9942
b80f6443
JA
9943@item
9944Bash includes the @code{caller} builtin, which displays the context of
9945any active subroutine call (a shell function or a script executed with
74091dd4 9946the @code{.} or @code{source} builtins). This supports the Bash
b80f6443
JA
9947debugger.
9948
bb70624e 9949@item
f73dda09
JA
9950The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9951@code{DEBUG} pseudo-signal specification, similar to @code{EXIT}.
b80f6443
JA
9952Commands specified with a @code{DEBUG} trap are executed before every
9953simple command, @code{for} command, @code{case} command,
9954@code{select} command, every arithmetic @code{for} command, and before
9955the first command executes in a shell function.
7117c2d2 9956The @code{DEBUG} trap is not inherited by shell functions unless the
b80f6443
JA
9957function has been given the @code{trace} attribute or the
9958@code{functrace} option has been enabled using the @code{shopt} builtin.
9959The @code{extdebug} shell option has additional effects on the
9960@code{DEBUG} trap.
f73dda09
JA
9961
9962The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an
9963@code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}.
9964Commands specified with an @code{ERR} trap are executed after a simple
9965command fails, with a few exceptions.
b80f6443
JA
9966The @code{ERR} trap is not inherited by shell functions unless the
9967@code{-o errtrace} option to the @code{set} builtin is enabled.
9968
9969The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a
9970@code{RETURN} pseudo-signal specification, similar to
9971@code{EXIT} and @code{DEBUG}.
74091dd4 9972Commands specified with a @code{RETURN} trap are executed before
b80f6443
JA
9973execution resumes after a shell function or a shell script executed with
9974@code{.} or @code{source} returns.
95732b49
JA
9975The @code{RETURN} trap is not inherited by shell functions unless the
9976function has been given the @code{trace} attribute or the
9977@code{functrace} option has been enabled using the @code{shopt} builtin.
d166f048 9978
bb70624e
JA
9979@item
9980The Bash @code{type} builtin is more extensive and gives more information
9981about the names it finds (@pxref{Bash Builtins}).
cce855bc 9982
bb70624e 9983@item
28ef6c31 9984The Bash @code{umask} builtin permits a @option{-p} option to cause
bb70624e
JA
9985the output to be displayed in the form of a @code{umask} command
9986that may be reused as input (@pxref{Bourne Shell Builtins}).
ccc6cda3 9987
bb70624e
JA
9988@item
9989Bash implements a @code{csh}-like directory stack, and provides the
9990@code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it
9991(@pxref{The Directory Stack}).
9992Bash also makes the directory stack visible as the value of the
28ef6c31 9993@env{DIRSTACK} shell variable.
d166f048 9994
bb70624e
JA
9995@item
9996Bash interprets special backslash-escaped characters in the prompt
ac50fbac 9997strings when interactive (@pxref{Controlling the Prompt}).
cce855bc 9998
bb70624e
JA
9999@item
10000The Bash restricted mode is more useful (@pxref{The Restricted Shell});
10001the SVR4.2 shell restricted mode is too limited.
d166f048 10002
bb70624e
JA
10003@item
10004The @code{disown} builtin can remove a job from the internal shell
10005job table (@pxref{Job Control Builtins}) or suppress the sending
10006of @code{SIGHUP} to a job when the shell exits as the result of a
10007@code{SIGHUP}.
d166f048 10008
95732b49
JA
10009@item
10010Bash includes a number of features to support a separate debugger for
10011shell scripts.
10012
bb70624e
JA
10013@item
10014The SVR4.2 shell has two privilege-related builtins
10015(@code{mldmode} and @code{priv}) not present in Bash.
d166f048 10016
bb70624e
JA
10017@item
10018Bash does not have the @code{stop} or @code{newgrp} builtins.
ccc6cda3 10019
bb70624e 10020@item
28ef6c31 10021Bash does not use the @env{SHACCT} variable or perform shell accounting.
ccc6cda3 10022
bb70624e 10023@item
28ef6c31
JA
10024The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses
10025@env{TMOUT}.
ccc6cda3 10026
bb70624e 10027@end itemize
d166f048 10028
bb70624e
JA
10029@noindent
10030More features unique to Bash may be found in @ref{Bash Features}.
d166f048 10031
ccc6cda3 10032
bb70624e 10033@appendixsec Implementation Differences From The SVR4.2 Shell
ccc6cda3 10034
bb70624e
JA
10035Since Bash is a completely new implementation, it does not suffer from
10036many of the limitations of the SVR4.2 shell. For instance:
ccc6cda3 10037
bb70624e 10038@itemize @bullet
ccc6cda3 10039
bb70624e
JA
10040@item
10041Bash does not fork a subshell when redirecting into or out of
10042a shell control structure such as an @code{if} or @code{while}
10043statement.
ccc6cda3 10044
bb70624e
JA
10045@item
10046Bash does not allow unbalanced quotes. The SVR4.2 shell will silently
10047insert a needed closing quote at @code{EOF} under certain circumstances.
10048This can be the cause of some hard-to-find errors.
ccc6cda3 10049
bb70624e
JA
10050@item
10051The SVR4.2 shell uses a baroque memory management scheme based on
10052trapping @code{SIGSEGV}. If the shell is started from a process with
10053@code{SIGSEGV} blocked (e.g., by using the @code{system()} C library
10054function call), it misbehaves badly.
ccc6cda3 10055
ccc6cda3 10056@item
bb70624e 10057In a questionable attempt at security, the SVR4.2 shell,
28ef6c31 10058when invoked without the @option{-p} option, will alter its real
bb70624e
JA
10059and effective @sc{uid} and @sc{gid} if they are less than some
10060magic threshold value, commonly 100.
10061This can lead to unexpected results.
10062
ccc6cda3 10063@item
bb70624e
JA
10064The SVR4.2 shell does not allow users to trap @code{SIGSEGV},
10065@code{SIGALRM}, or @code{SIGCHLD}.
10066
ccc6cda3 10067@item
28ef6c31
JA
10068The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK},
10069@env{PATH}, @env{PS1}, or @env{PS2} variables to be unset.
bb70624e 10070
ccc6cda3 10071@item
bb70624e
JA
10072The SVR4.2 shell treats @samp{^} as the undocumented equivalent of
10073@samp{|}.
10074
ccc6cda3 10075@item
bb70624e
JA
10076Bash allows multiple option arguments when it is invoked (@code{-x -v});
10077the SVR4.2 shell allows only one option argument (@code{-xv}). In
10078fact, some versions of the shell dump core if the second argument begins
10079with a @samp{-}.
ccc6cda3 10080
bb70624e
JA
10081@item
10082The SVR4.2 shell exits a script if any builtin fails; Bash exits
0628567a
JA
10083a script only if one of the @sc{posix} special builtins fails, and
10084only for certain failures, as enumerated in the @sc{posix} standard.
ccc6cda3 10085
bb70624e
JA
10086@item
10087The SVR4.2 shell behaves differently when invoked as @code{jsh}
10088(it turns on job control).
10089@end itemize
ccc6cda3 10090
3185942a
JA
10091@node GNU Free Documentation License
10092@appendix GNU Free Documentation License
10093
10094@include fdl.texi
10095
10096@node Indexes
10097@appendix Indexes
b80f6443
JA
10098
10099@menu
3185942a
JA
10100* Builtin Index:: Index of Bash builtin commands.
10101* Reserved Word Index:: Index of Bash reserved words.
10102* Variable Index:: Quick reference helps you find the
10103 variable you want.
10104* Function Index:: Index of bindable Readline functions.
10105* Concept Index:: General index for concepts described in
10106 this manual.
b80f6443
JA
10107@end menu
10108
ccc6cda3 10109@node Builtin Index
3185942a 10110@appendixsec Index of Shell Builtin Commands
ccc6cda3
JA
10111@printindex bt
10112
10113@node Reserved Word Index
3185942a 10114@appendixsec Index of Shell Reserved Words
ccc6cda3
JA
10115@printindex rw
10116
10117@node Variable Index
3185942a 10118@appendixsec Parameter and Variable Index
ccc6cda3
JA
10119@printindex vr
10120
10121@node Function Index
3185942a 10122@appendixsec Function Index
ccc6cda3
JA
10123@printindex fn
10124
10125@node Concept Index
3185942a 10126@appendixsec Concept Index
ccc6cda3
JA
10127@printindex cp
10128
ccc6cda3 10129@bye