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