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