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