source: trunk/third/bash/doc/bashref.texi @ 21742

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