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