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