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