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