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