source: trunk/essentials/app-shells/bash/doc/bash.1@ 3230

Last change on this file since 3230 was 3228, checked in by bird, 19 years ago

bash 3.1

File size: 242.9 KB
Line 
1\"
2.\" MAN PAGE COMMENTS to
3.\"
4.\" Chet Ramey
5.\" Information Network Services
6.\" Case Western Reserve University
7.\" [email protected]
8.\"
9.\" Last Change: Sat Aug 27 13:28:44 EDT 2005
10.\"
11.\" bash_builtins, strip all but Built-Ins section
12.if \n(zZ=1 .ig zZ
13.if \n(zY=1 .ig zY
14.TH BASH 1 "2005 Aug 27" "GNU Bash-3.1-beta1"
15.\"
16.\" There's some problem with having a `@'
17.\" in a tagged paragraph with the BSD man macros.
18.\" It has to do with `@' appearing in the }1 macro.
19.\" This is a problem on 4.3 BSD and Ultrix, but Sun
20.\" appears to have fixed it.
21.\" If you're seeing the characters
22.\" `@u-3p' appearing before the lines reading
23.\" `possible-hostname-completions
24.\" and `complete-hostname' down in READLINE,
25.\" then uncomment this redefinition.
26.\"
27.de }1
28.ds ]X \&\\*(]B\\
29.nr )E 0
30.if !"\\$1"" .nr )I \\$1n
31.}f
32.ll \\n(LLu
33.in \\n()Ru+\\n(INu+\\n()Iu
34.ti \\n(INu
35.ie !\\n()Iu+\\n()Ru-\w\\*(]Xu-3p \{\\*(]X
36.br\}
37.el \\*(]X\h|\\n()Iu+\\n()Ru\c
38.}f
39..
40.\"
41.\" File Name macro. This used to be `.PN', for Path Name,
42.\" but Sun doesn't seem to like that very much.
43.\"
44.de FN
45\fI\|\\$1\|\fP
46..
47.SH NAME
48bash \- GNU Bourne-Again SHell
49.SH SYNOPSIS
50.B bash
51[options]
52[file]
53.SH COPYRIGHT
54.if n Bash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc.
55.if t Bash is Copyright \(co 1989-2005 by the Free Software Foundation, Inc.
56.SH DESCRIPTION
57.B Bash
58is an \fBsh\fR-compatible command language interpreter that
59executes commands read from the standard input or from a file.
60.B Bash
61also incorporates useful features from the \fIKorn\fP and \fIC\fP
62shells (\fBksh\fP and \fBcsh\fP).
63.PP
64.B Bash
65is intended to be a conformant implementation of the IEEE
66POSIX Shell and Tools specification (IEEE Working Group 1003\.2).
67.B Bash
68can be configured to be POSIX-conformant by default.
69.SH OPTIONS
70In addition to the single-character shell options documented in the
71description of the \fBset\fR builtin command, \fBbash\fR
72interprets the following options when it is invoked:
73.PP
74.PD 0
75.TP 10
76.BI \-c "\| string\^"
77If the
78.B \-c
79option is present, then commands are read from
80.IR string .
81If there are arguments after the
82.IR string ,
83they are assigned to the positional parameters, starting with
84.BR $0 .
85.TP
86.B \-i
87If the
88.B \-i
89option is present, the shell is
90.IR interactive .
91.TP
92.B \-l
93Make
94.B bash
95act as if it had been invoked as a login shell (see
96.SM
97.B INVOCATION
98below).
99.TP
100.B \-r
101If the
102.B \-r
103option is present, the shell becomes
104.I restricted
105(see
106.SM
107.B "RESTRICTED SHELL"
108below).
109.TP
110.B \-s
111If the
112.B \-s
113option is present, or if no arguments remain after option
114processing, then commands are read from the standard input.
115This option allows the positional parameters to be set
116when invoking an interactive shell.
117.TP
118.B \-D
119A list of all double-quoted strings preceded by \fB$\fP
120is printed on the standard output.
121These are the strings that
122are subject to language translation when the current locale
123is not \fBC\fP or \fBPOSIX\fP.
124This implies the \fB\-n\fP option; no commands will be executed.
125.TP
126.B [\-+]O [\fIshopt_option\fP]
127\fIshopt_option\fP is one of the shell options accepted by the
128\fBshopt\fP builtin (see
129.SM
130.B SHELL BUILTIN COMMANDS
131below).
132If \fIshopt_option\fP is present, \fB\-O\fP sets the value of that option;
133\fB+O\fP unsets it.
134If \fIshopt_option\fP is not supplied, the names and values of the shell
135options accepted by \fBshopt\fP are printed on the standard output.
136If the invocation option is \fB+O\fP, the output is displayed in a format
137that may be reused as input.
138.TP
139.B \-\-
140A
141.B \-\-
142signals the end of options and disables further option processing.
143Any arguments after the
144.B \-\-
145are treated as filenames and arguments. An argument of
146.B \-
147is equivalent to \fB\-\-\fP.
148.PD
149.PP
150.B Bash
151also interprets a number of multi-character options.
152These options must appear on the command line before the
153single-character options to be recognized.
154.PP
155.PD 0
156.TP
157.B \-\-debugger
158Arrange for the debugger profile to be executed before the shell
159starts.
160Turns on extended debugging mode (see the description of the
161.B extdebug
162option to the
163.B shopt
164builtin below)
165and shell function tracing (see the description of the
166\fB\-o functrace\fP option to the
167.B set
168builtin below).
169.TP
170.B \-\-dump\-po\-strings
171Equivalent to \fB\-D\fP, but the output is in the GNU \fIgettext\fP
172\fBpo\fP (portable object) file format.
173.TP
174.B \-\-dump\-strings
175Equivalent to \fB\-D\fP.
176.TP
177.B \-\-help
178Display a usage message on standard output and exit successfully.
179.TP
180\fB\-\-init\-file\fP \fIfile\fP
181.PD 0
182.TP
183\fB\-\-rcfile\fP \fIfile\fP
184.PD
185Execute commands from
186.I file
187instead of the standard personal initialization file
188.I ~/.bashrc
189if the shell is interactive (see
190.SM
191.B INVOCATION
192below).
193.TP
194.B \-\-login
195Equivalent to \fB\-l\fP.
196.TP
197.B \-\-noediting
198Do not use the GNU
199.B readline
200library to read command lines when the shell is interactive.
201.TP
202.B \-\-noprofile
203Do not read either the system-wide startup file
204.FN /etc/profile
205or any of the personal initialization files
206.IR ~/.bash_profile ,
207.IR ~/.bash_login ,
208or
209.IR ~/.profile .
210By default,
211.B bash
212reads these files when it is invoked as a login shell (see
213.SM
214.B INVOCATION
215below).
216.TP
217.B \-\-norc
218Do not read and execute the personal initialization file
219.I ~/.bashrc
220if the shell is interactive.
221This option is on by default if the shell is invoked as
222.BR sh .
223.TP
224.B \-\-posix
225Change the behavior of \fBbash\fP where the default operation differs
226from the POSIX 1003.2 standard to match the standard (\fIposix mode\fP).
227.TP
228.B \-\-restricted
229The shell becomes restricted (see
230.SM
231.B "RESTRICTED SHELL"
232below).
233.TP
234.B \-\-verbose
235Equivalent to \fB\-v\fP.
236.TP
237.B \-\-version
238Show version information for this instance of
239.B bash
240on the standard output and exit successfully.
241.PD
242.SH ARGUMENTS
243If arguments remain after option processing, and neither the
244.B \-c
245nor the
246.B \-s
247option has been supplied, the first argument is assumed to
248be the name of a file containing shell commands.
249If
250.B bash
251is invoked in this fashion,
252.B $0
253is set to the name of the file, and the positional parameters
254are set to the remaining arguments.
255.B Bash
256reads and executes commands from this file, then exits.
257\fBBash\fP's exit status is the exit status of the last command
258executed in the script.
259If no commands are executed, the exit status is 0.
260An attempt is first made to open the file in the current directory, and,
261if no file is found, then the shell searches the directories in
262.SM
263.B PATH
264for the script.
265.SH INVOCATION
266A \fIlogin shell\fP is one whose first character of argument zero is a
267.BR \- ,
268or one started with the
269.B \-\-login
270option.
271.PP
272An \fIinteractive\fP shell is one started without non-option arguments
273and without the
274.B \-c
275option
276whose standard input and error are
277both connected to terminals (as determined by
278.IR isatty (3)),
279or one started with the
280.B \-i
281option.
282.SM
283.B PS1
284is set and
285.B $\-
286includes
287.B i
288if
289.B bash
290is interactive,
291allowing a shell script or a startup file to test this state.
292.PP
293The following paragraphs describe how
294.B bash
295executes its startup files.
296If any of the files exist but cannot be read,
297.B bash
298reports an error.
299Tildes are expanded in file names as described below under
300.B "Tilde Expansion"
301in the
302.SM
303.B EXPANSION
304section.
305.PP
306When
307.B bash
308is invoked as an interactive login shell, or as a non-interactive shell
309with the \fB\-\-login\fP option, it first reads and
310executes commands from the file \fI/etc/profile\fP, if that
311file exists.
312After reading that file, it looks for \fI~/.bash_profile\fP,
313\fI~/.bash_login\fP, and \fI~/.profile\fP, in that order, and reads
314and executes commands from the first one that exists and is readable.
315The
316.B \-\-noprofile
317option may be used when the shell is started to inhibit this behavior.
318.PP
319When a login shell exits,
320.B bash
321reads and executes commands from the file \fI~/.bash_logout\fP, if it
322exists.
323.PP
324When an interactive shell that is not a login shell is started,
325.B bash
326reads and executes commands from \fI~/.bashrc\fP, if that file exists.
327This may be inhibited by using the
328.B \-\-norc
329option.
330The \fB\-\-rcfile\fP \fIfile\fP option will force
331.B bash
332to read and execute commands from \fIfile\fP instead of \fI~/.bashrc\fP.
333.PP
334When
335.B bash
336is started non-interactively, to run a shell script, for example, it
337looks for the variable
338.SM
339.B BASH_ENV
340in the environment, expands its value if it appears there, and uses the
341expanded value as the name of a file to read and execute.
342.B Bash
343behaves as if the following command were executed:
344.sp .5
345.RS
346.if t \f(CWif [ \-n "$BASH_ENV" ]; then . "$BASH_ENV"; fi\fP
347.if n if [ \-n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
348.RE
349.sp .5
350but the value of the
351.SM
352.B PATH
353variable is not used to search for the file name.
354.PP
355If
356.B bash
357is invoked with the name
358.BR sh ,
359it tries to mimic the startup behavior of historical versions of
360.B sh
361as closely as possible,
362while conforming to the POSIX standard as well.
363When invoked as an interactive login shell, or a non-interactive
364shell with the \fB\-\-login\fP option, it first attempts to
365read and execute commands from
366.I /etc/profile
367and
368.IR ~/.profile ,
369in that order.
370The
371.B \-\-noprofile
372option may be used to inhibit this behavior.
373When invoked as an interactive shell with the name
374.BR sh ,
375.B bash
376looks for the variable
377.SM
378.BR ENV ,
379expands its value if it is defined, and uses the
380expanded value as the name of a file to read and execute.
381Since a shell invoked as
382.B sh
383does not attempt to read and execute commands from any other startup
384files, the
385.B \-\-rcfile
386option has no effect.
387A non-interactive shell invoked with the name
388.B sh
389does not attempt to read any other startup files.
390When invoked as
391.BR sh ,
392.B bash
393enters
394.I posix
395mode after the startup files are read.
396.PP
397When
398.B bash
399is started in
400.I posix
401mode, as with the
402.B \-\-posix
403command line option, it follows the POSIX standard for startup files.
404In this mode, interactive shells expand the
405.SM
406.B ENV
407variable and commands are read and executed from the file
408whose name is the expanded value.
409No other startup files are read.
410.PP
411.B Bash
412attempts to determine when it is being run by the remote shell
413daemon, usually \fIrshd\fP.
414If
415.B bash
416determines it is being run by \fIrshd\fP, it reads and executes
417commands from \fI~/.bashrc\fP, if that file exists and is readable.
418It will not do this if invoked as \fBsh\fP.
419The
420.B \-\-norc
421option may be used to inhibit this behavior, and the
422.B \-\-rcfile
423option may be used to force another file to be read, but
424\fIrshd\fP does not generally invoke the shell with those options
425or allow them to be specified.
426.PP
427If the shell is started with the effective user (group) id not equal to the
428real user (group) id, and the \fB\-p\fP option is not supplied, no startup
429files are read, shell functions are not inherited from the environment, the
430.SM
431.B SHELLOPTS
432variable, if it appears in the environment, is ignored,
433and the effective user id is set to the real user id.
434If the \fB\-p\fP option is supplied at invocation, the startup behavior is
435the same, but the effective user id is not reset.
436.SH DEFINITIONS
437.PP
438The following definitions are used throughout the rest of this
439document.
440.PD 0
441.TP
442.B blank
443A space or tab.
444.TP
445.B word
446A sequence of characters considered as a single unit by the shell.
447Also known as a
448.BR token .
449.TP
450.B name
451A
452.I word
453consisting only of alphanumeric characters and underscores, and
454beginning with an alphabetic character or an underscore. Also
455referred to as an
456.BR identifier .
457.TP
458.B metacharacter
459A character that, when unquoted, separates words. One of the following:
460.br
461.RS
462.PP
463.if t \fB| & ; ( ) < > space tab\fP
464.if n \fB| & ; ( ) < > space tab\fP
465.RE
466.PP
467.TP
468.B control operator
469A \fItoken\fP that performs a control function. It is one of the following
470symbols:
471.RS
472.PP
473.if t \fB\(bv\(bv & && ; ;; ( ) | <newline>\fP
474.if n \fB|| & && ; ;; ( ) | <newline>\fP
475.RE
476.PD
477.SH "RESERVED WORDS"
478\fIReserved words\fP are words that have a special meaning to the shell.
479The following words are recognized as reserved when unquoted and either
480the first word of a simple command (see
481.SM
482.B SHELL GRAMMAR
483below) or the third word of a
484.B case
485or
486.B for
487command:
488.if t .RS
489.PP
490.B
491.if n ! case do done elif else esac fi for function if in select then until while { } time [[ ]]
492.if t ! case do done elif else esac fi for function if in select then until while { } time [[ ]]
493.if t .RE
494.SH "SHELL GRAMMAR"
495.SS Simple Commands
496.PP
497A \fIsimple command\fP is a sequence of optional variable assignments
498followed by \fBblank\fP-separated words and redirections, and
499terminated by a \fIcontrol operator\fP. The first word
500specifies the command to be executed, and is passed as argument zero.
501The remaining words are passed as arguments to the invoked command.
502.PP
503The return value of a \fIsimple command\fP is its exit status, or
504128+\fIn\^\fP if the command is terminated by signal
505.IR n .
506.SS Pipelines
507.PP
508A \fIpipeline\fP is a sequence of one or more commands separated by
509the character
510.BR | .
511The format for a pipeline is:
512.RS
513.PP
514[\fBtime\fP [\fB\-p\fP]] [ ! ] \fIcommand\fP [ \fB|\fP \fIcommand2\fP ... ]
515.RE
516.PP
517The standard output of
518.I command
519is connected via a pipe to the standard input of
520.IR command2 .
521This connection is performed before any redirections specified by the
522command (see
523.SM
524.B REDIRECTION
525below).
526.PP
527The return status of a pipeline is the exit status of the last
528command, unless the \fBpipefail\fP option is enabled.
529If \fBpipefail\fP is enabled, the pipeline's return status is the
530value of the last (rightmost) command to exit with a non-zero status,
531or zero if all commands exit successfully.
532If the reserved word
533.B !
534precedes a pipeline, the exit status of that pipeline is the logical
535negation of the exit status as described above.
536The shell waits for all commands in the pipeline to
537terminate before returning a value.
538.PP
539If the
540.B time
541reserved word precedes a pipeline, the elapsed as well as user and
542system time consumed by its execution are reported when the pipeline
543terminates.
544The \fB\-p\fP option changes the output format to that specified by POSIX.
545The
546.SM
547.B TIMEFORMAT
548variable may be set to a format string that specifies how the timing
549information should be displayed; see the description of
550.SM
551.B TIMEFORMAT
552under
553.B "Shell Variables"
554below.
555.PP
556Each command in a pipeline is executed as a separate process (i.e., in a
557subshell).
558.SS Lists
559.PP
560A \fIlist\fP is a sequence of one or more pipelines separated by one
561of the operators
562.BR ; ,
563.BR & ,
564.BR && ,
565or
566.BR \(bv\(bv ,
567and optionally terminated by one of
568.BR ; ,
569.BR & ,
570or
571.BR <newline> .
572.PP
573Of these list operators,
574.B &&
575and
576.B \(bv\(bv
577have equal precedence, followed by
578.B ;
579and
580.BR &,
581which have equal precedence.
582.PP
583A sequence of one or more newlines may appear in a \fIlist\fP instead
584of a semicolon to delimit commands.
585.PP
586If a command is terminated by the control operator
587.BR & ,
588the shell executes the command in the \fIbackground\fP
589in a subshell. The shell does not wait for the command to
590finish, and the return status is 0. Commands separated by a
591.B ;
592are executed sequentially; the shell waits for each
593command to terminate in turn. The return status is the
594exit status of the last command executed.
595.PP
596The control operators
597.B &&
598and
599.B \(bv\(bv
600denote AND lists and OR lists, respectively.
601An AND list has the form
602.RS
603.PP
604\fIcommand1\fP \fB&&\fP \fIcommand2\fP
605.RE
606.PP
607.I command2
608is executed if, and only if,
609.I command1
610returns an exit status of zero.
611.PP
612An OR list has the form
613.RS
614.PP
615\fIcommand1\fP \fB\(bv\(bv\fP \fIcommand2\fP
616.PP
617.RE
618.PP
619.I command2
620is executed if and only if
621.I command1
622returns a non-zero exit status. The return status of
623AND and OR lists is the exit status of the last command
624executed in the list.
625.SS Compound Commands
626.PP
627A \fIcompound command\fP is one of the following:
628.TP
629(\fIlist\fP)
630\fIlist\fP is executed in a subshell environment (see
631.SM
632\fBCOMMAND EXECUTION ENVIRONMENT\fP
633below).
634Variable assignments and builtin
635commands that affect the shell's environment do not remain in effect
636after the command completes. The return status is the exit status of
637\fIlist\fP.
638.TP
639{ \fIlist\fP; }
640\fIlist\fP is simply executed in the current shell environment.
641\fIlist\fP must be terminated with a newline or semicolon.
642This is known as a \fIgroup command\fP.
643The return status is the exit status of
644\fIlist\fP.
645Note that unlike the metacharacters \fB(\fP and \fB)\fP, \fB{\fP and
646\fB}\fP are \fIreserved words\fP and must occur where a reserved
647word is permitted to be recognized. Since they do not cause a word
648break, they must be separated from \fIlist\fP by whitespace.
649.TP
650((\fIexpression\fP))
651The \fIexpression\fP is evaluated according to the rules described
652below under
653.SM
654.BR "ARITHMETIC EVALUATION" .
655If the value of the expression is non-zero, the return status is 0;
656otherwise the return status is 1. This is exactly equivalent to
657\fBlet "\fIexpression\fP"\fR.
658.TP
659\fB[[\fP \fIexpression\fP \fB]]\fP
660Return a status of 0 or 1 depending on the evaluation of
661the conditional expression \fIexpression\fP.
662Expressions are composed of the primaries described below under
663.SM
664.BR "CONDITIONAL EXPRESSIONS" .
665Word splitting and pathname expansion are not performed on the words
666between the \fB[[\fP and \fB]]\fP; tilde expansion, parameter and
667variable expansion, arithmetic expansion, command substitution, process
668substitution, and quote removal are performed.
669Conditional operators such as \fB\-f\fP must be unquoted to be recognized
670as primaries.
671.if t .sp 0.5
672.if n .sp 1
673When the \fB==\fP and \fB!=\fP operators are used, the string to the
674right of the operator is considered a pattern and matched according
675to the rules described below under \fBPattern Matching\fP.
676If the shell option
677.B nocasematch
678is enabled, the match is performed without regard to the case
679of alphabetic characters.
680The return value is 0 if the string matches or does not match
681the pattern, respectively, and 1 otherwise.
682Any part of the pattern may be quoted to force it to be matched as a
683string.
684.if t .sp 0.5
685.if n .sp 1
686An additional binary operator, \fB=~\fP, is available, with the same
687precedence as \fB==\fP and \fB!=\fP.
688When it is used, the string to the right of the operator is considered
689an extended regular expression and matched accordingly (as in \fIregex\fP(3)).
690The return value is 0 if the string matches
691the pattern, and 1 otherwise.
692If the regular expression is syntactically incorrect, the conditional
693expression's return value is 2.
694If the shell option
695.B nocasematch
696is enabled, the match is performed without regard to the case
697of alphabetic characters.
698Substrings matched by parenthesized subexpressions within the regular
699expression are saved in the array variable \fBBASH_REMATCH\fP.
700The element of \fBBASH_REMATCH\fP with index 0 is the portion of the string
701matching the entire regular expression.
702The element of \fBBASH_REMATCH\fP with index \fIn\fP is the portion of the
703string matching the \fIn\fPth parenthesized subexpression.
704.if t .sp 0.5
705.if n .sp 1
706Expressions may be combined using the following operators, listed
707in decreasing order of precedence:
708.if t .sp 0.5
709.if n .sp 1
710.RS
711.PD 0
712.TP
713.B ( \fIexpression\fP )
714Returns the value of \fIexpression\fP.
715This may be used to override the normal precedence of operators.
716.TP
717.B ! \fIexpression\fP
718True if
719.I expression
720is false.
721.TP
722\fIexpression1\fP \fB&&\fP \fIexpression2\fP
723True if both
724.I expression1
725and
726.I expression2
727are true.
728.TP
729.if t \fIexpression1\fP \fB\(bv\(bv\fP \fIexpression2\fP
730.if n \fIexpression1\fP \fB||\fP \fIexpression2\fP
731True if either
732.I expression1
733or
734.I expression2
735is true.
736.PD
737.LP
738The \fB&&\fP and
739.if t \fB\(bv\(bv\fP
740.if n \fB||\fP
741operators do not evaluate \fIexpression2\fP if the value of
742\fIexpression1\fP is sufficient to determine the return value of
743the entire conditional expression.
744.RE
745.TP
746\fBfor\fP \fIname\fP [ \fBin\fP \fIword\fP ] ; \fBdo\fP \fIlist\fP ; \fBdone\fP
747The list of words following \fBin\fP is expanded, generating a list
748of items.
749The variable \fIname\fP is set to each element of this list
750in turn, and \fIlist\fP is executed each time.
751If the \fBin\fP \fIword\fP is omitted, the \fBfor\fP command executes
752\fIlist\fP once for each positional parameter that is set (see
753.SM
754.B PARAMETERS
755below).
756The return status is the exit status of the last command that executes.
757If the expansion of the items following \fBin\fP results in an empty
758list, no commands are executed, and the return status is 0.
759.TP
760\fBfor\fP (( \fIexpr1\fP ; \fIexpr2\fP ; \fIexpr3\fP )) ; \fBdo\fP \fIlist\fP ; \fBdone\fP
761First, the arithmetic expression \fIexpr1\fP is evaluated according
762to the rules described below under
763.SM
764.BR "ARITHMETIC EVALUATION" .
765The arithmetic expression \fIexpr2\fP is then evaluated repeatedly
766until it evaluates to zero.
767Each time \fIexpr2\fP evaluates to a non-zero value, \fIlist\fP is
768executed and the arithmetic expression \fIexpr3\fP is evaluated.
769If any expression is omitted, it behaves as if it evaluates to 1.
770The return value is the exit status of the last command in \fIlist\fP
771that is executed, or false if any of the expressions is invalid.
772.TP
773\fBselect\fP \fIname\fP [ \fBin\fP \fIword\fP ] ; \fBdo\fP \fIlist\fP ; \fBdone\fP
774The list of words following \fBin\fP is expanded, generating a list
775of items. The set of expanded words is printed on the standard
776error, each preceded by a number. If the \fBin\fP
777\fIword\fP is omitted, the positional parameters are printed (see
778.SM
779.B PARAMETERS
780below). The
781.B PS3
782prompt is then displayed and a line read from the standard input.
783If the line consists of a number corresponding to one of
784the displayed words, then the value of
785.I name
786is set to that word. If the line is empty, the words and prompt
787are displayed again. If EOF is read, the command completes. Any
788other value read causes
789.I name
790to be set to null. The line read is saved in the variable
791.BR REPLY .
792The
793.I list
794is executed after each selection until a
795.B break
796command is executed.
797The exit status of
798.B select
799is the exit status of the last command executed in
800.IR list ,
801or zero if no commands were executed.
802.TP
803\fBcase\fP \fIword\fP \fBin\fP [ [(] \fIpattern\fP [ \fB|\fP \fIpattern\fP ] \
804... ) \fIlist\fP ;; ] ... \fBesac\fP
805A \fBcase\fP command first expands \fIword\fP, and tries to match
806it against each \fIpattern\fP in turn, using the same matching rules
807as for pathname expansion (see
808.B Pathname Expansion
809below).
810If the shell option
811.B nocasematch
812is enabled, the match is performed without regard to the case
813of alphabetic characters.
814When a match is found, the
815corresponding \fIlist\fP is executed. After the first match, no
816subsequent matches are attempted. The exit status is zero if no
817pattern matches. Otherwise, it is the exit status of the
818last command executed in \fIlist\fP.
819.TP
820\fBif\fP \fIlist\fP; \fBthen\fP \fIlist;\fP \
821[ \fBelif\fP \fIlist\fP; \fBthen\fP \fIlist\fP; ] ... \
822[ \fBelse\fP \fIlist\fP; ] \fBfi\fP
823The
824.B if
825.I list
826is executed. If its exit status is zero, the
827\fBthen\fP \fIlist\fP is executed. Otherwise, each \fBelif\fP
828\fIlist\fP is executed in turn, and if its exit status is zero,
829the corresponding \fBthen\fP \fIlist\fP is executed and the
830command completes. Otherwise, the \fBelse\fP \fIlist\fP is
831executed, if present. The exit status is the exit status of the
832last command executed, or zero if no condition tested true.
833.TP
834\fBwhile\fP \fIlist\fP; \fBdo\fP \fIlist\fP; \fBdone\fP
835.PD 0
836.TP
837\fBuntil\fP \fIlist\fP; \fBdo\fP \fIlist\fP; \fBdone\fP
838.PD
839The \fBwhile\fP command continuously executes the \fBdo\fP
840\fIlist\fP as long as the last command in \fIlist\fP returns
841an exit status of zero. The \fBuntil\fP command is identical
842to the \fBwhile\fP command, except that the test is negated;
843the
844.B do
845.I list
846is executed as long as the last command in
847.I list
848returns a non-zero exit status.
849The exit status of the \fBwhile\fP and \fBuntil\fP commands
850is the exit status
851of the last \fBdo\fP \fIlist\fP command executed, or zero if
852none was executed.
853.SS Shell Function Definitions
854.PP
855A shell function is an object that is called like a simple command and
856executes a compound command with a new set of positional parameters.
857Shell functions are declared as follows:
858.TP
859[ \fBfunction\fP ] \fIname\fP () \fIcompound\-command\fP [\fIredirection\fP]
860This defines a function named \fIname\fP.
861The reserved word \fBfunction\fP is optional.
862If the \fBfunction\fP reserved word is supplied, the parentheses are optional.
863The \fIbody\fP of the function is the compound command
864.I compound\-command
865(see \fBCompound Commands\fP above).
866That command is usually a \fIlist\fP of commands between { and }, but
867may be any command listed under \fBCompound Commands\fP above.
868\fIcompound\-command\fP is executed whenever \fIname\fP is specified as the
869name of a simple command.
870Any redirections (see
871.SM
872.B REDIRECTION
873below) specified when a function is defined are performed
874when the function is executed.
875The exit status of a function definition is zero unless a syntax error
876occurs or a readonly function with the same name already exists.
877When executed, the exit status of a function is the exit status of the
878last command executed in the body. (See
879.SM
880.B FUNCTIONS
881below.)
882.SH COMMENTS
883In a non-interactive shell, or an interactive shell in which the
884.B interactive_comments
885option to the
886.B shopt
887builtin is enabled (see
888.SM
889.B "SHELL BUILTIN COMMANDS"
890below), a word beginning with
891.B #
892causes that word and all remaining characters on that line to
893be ignored. An interactive shell without the
894.B interactive_comments
895option enabled does not allow comments. The
896.B interactive_comments
897option is on by default in interactive shells.
898.SH QUOTING
899\fIQuoting\fP is used to remove the special meaning of certain
900characters or words to the shell. Quoting can be used to
901disable special treatment for special characters, to prevent
902reserved words from being recognized as such, and to prevent
903parameter expansion.
904.PP
905Each of the \fImetacharacters\fP listed above under
906.SM
907.B DEFINITIONS
908has special meaning to the shell and must be quoted if it is to
909represent itself.
910.PP
911When the command history expansion facilities are being used
912(see
913.SM
914.B HISTORY EXPANSION
915below), the
916\fIhistory expansion\fP character, usually \fB!\fP, must be quoted
917to prevent history expansion.
918.PP
919There are three quoting mechanisms: the
920.IR "escape character" ,
921single quotes, and double quotes.
922.PP
923A non-quoted backslash (\fB\e\fP) is the
924.IR "escape character" .
925It preserves the literal value of the next character that follows,
926with the exception of <newline>. If a \fB\e\fP<newline> pair
927appears, and the backslash is not itself quoted, the \fB\e\fP<newline>
928is treated as a line continuation (that is, it is removed from the
929input stream and effectively ignored).
930.PP
931Enclosing characters in single quotes preserves the literal value
932of each character within the quotes. A single quote may not occur
933between single quotes, even when preceded by a backslash.
934.PP
935Enclosing characters in double quotes preserves the literal value
936of all characters within the quotes, with the exception of
937.BR $ ,
938.BR ` ,
939.BR \e ,
940and, when history expansion is enabled,
941.BR ! .
942The characters
943.B $
944and
945.B `
946retain their special meaning within double quotes. The backslash
947retains its special meaning only when followed by one of the following
948characters:
949.BR $ ,
950.BR ` ,
951\^\fB"\fP\^,
952.BR \e ,
953or
954.BR <newline> .
955A double quote may be quoted within double quotes by preceding it with
956a backslash.
957If enabled, history expansion will be performed unless an
958.B !
959appearing in double quotes is escaped using a backslash.
960The backslash preceding the
961.B !
962is not removed.
963.PP
964The special parameters
965.B *
966and
967.B @
968have special meaning when in double
969quotes (see
970.SM
971.B PARAMETERS
972below).
973.PP
974Words of the form \fB$\fP'\fIstring\fP' are treated specially. The
975word expands to \fIstring\fP, with backslash-escaped characters replaced
976as specified by the ANSI C standard. Backslash escape sequences, if
977present, are decoded as follows:
978.RS
979.PD 0
980.TP
981.B \ea
982alert (bell)
983.TP
984.B \eb
985backspace
986.TP
987.B \ee
988an escape character
989.TP
990.B \ef
991form feed
992.TP
993.B \en
994new line
995.TP
996.B \er
997carriage return
998.TP
999.B \et
1000horizontal tab
1001.TP
1002.B \ev
1003vertical tab
1004.TP
1005.B \e\e
1006backslash
1007.TP
1008.B \e'
1009single quote
1010.TP
1011.B \e\fInnn\fP
1012the eight-bit character whose value is the octal value \fInnn\fP
1013(one to three digits)
1014.TP
1015.B \ex\fIHH\fP
1016the eight-bit character whose value is the hexadecimal value \fIHH\fP
1017(one or two hex digits)
1018.TP
1019.B \ec\fIx\fP
1020a control-\fIx\fP character
1021.PD
1022.RE
1023.LP
1024The expanded result is single-quoted, as if the dollar sign had
1025not been present.
1026.PP
1027A double-quoted string preceded by a dollar sign (\fB$\fP) will cause
1028the string to be translated according to the current locale.
1029If the current locale is \fBC\fP or \fBPOSIX\fP, the dollar sign
1030is ignored.
1031If the string is translated and replaced, the replacement is
1032double-quoted.
1033.SH PARAMETERS
1034A
1035.I parameter
1036is an entity that stores values.
1037It can be a
1038.IR name ,
1039a number, or one of the special characters listed below under
1040.BR "Special Parameters" .
1041A
1042.I variable
1043is a parameter denoted by a
1044.IR name .
1045A variable has a \fIvalue\fP and zero or more \fIattributes\fP.
1046Attributes are assigned using the
1047.B declare
1048builtin command (see
1049.B declare
1050below in
1051.SM
1052.BR "SHELL BUILTIN COMMANDS" ).
1053.PP
1054A parameter is set if it has been assigned a value. The null string is
1055a valid value. Once a variable is set, it may be unset only by using
1056the
1057.B unset
1058builtin command (see
1059.SM
1060.B SHELL BUILTIN COMMANDS
1061below).
1062.PP
1063A
1064.I variable
1065may be assigned to by a statement of the form
1066.RS
1067.PP
1068\fIname\fP=[\fIvalue\fP]
1069.RE
1070.PP
1071If
1072.I value
1073is not given, the variable is assigned the null string. All
1074.I values
1075undergo tilde expansion, parameter and variable expansion,
1076command substitution, arithmetic expansion, and quote
1077removal (see
1078.SM
1079.B EXPANSION
1080below). If the variable has its
1081.B integer
1082attribute set, then
1083.I value
1084is evaluated as an arithmetic expression even if the $((...)) expansion is
1085not used (see
1086.B "Arithmetic Expansion"
1087below).
1088Word splitting is not performed, with the exception
1089of \fB"$@"\fP as explained below under
1090.BR "Special Parameters" .
1091Pathname expansion is not performed.
1092Assignment statements may also appear as arguments to the
1093.BR alias ,
1094.BR declare ,
1095.BR typeset ,
1096.BR export ,
1097.BR readonly ,
1098and
1099.B local
1100builtin commands.
1101.PP
1102In the context where an assignment statement is assigning a value
1103to a shell variable or array index, the += operator can be used to
1104append to or add to the variable's previous value.
1105When += is applied to a variable for which the integer attribute has been
1106set, \fIvalue\fP is evaluated as an arithmetic expression and added to the
1107variable's current value, which is also evaluated.
1108When += is applied to an array variable using compound assignment (see
1109.B Arrays
1110below), the
1111variable's value is not unset (as it is when using =), and new values are
1112appended to the array beginning at one greater than the array's maximum index.
1113When applied to a string-valued variable, \fIvalue\fP is expanded and
1114appended to the variable's value.
1115.SS Positional Parameters
1116.PP
1117A
1118.I positional parameter
1119is a parameter denoted by one or more
1120digits, other than the single digit 0. Positional parameters are
1121assigned from the shell's arguments when it is invoked,
1122and may be reassigned using the
1123.B set
1124builtin command. Positional parameters may not be assigned to
1125with assignment statements. The positional parameters are
1126temporarily replaced when a shell function is executed (see
1127.SM
1128.B FUNCTIONS
1129below).
1130.PP
1131When a positional parameter consisting of more than a single
1132digit is expanded, it must be enclosed in braces (see
1133.SM
1134.B EXPANSION
1135below).
1136.SS Special Parameters
1137.PP
1138The shell treats several parameters specially. These parameters may
1139only be referenced; assignment to them is not allowed.
1140.PD 0
1141.TP
1142.B *
1143Expands to the positional parameters, starting from one. When the
1144expansion occurs within double quotes, it expands to a single word
1145with the value of each parameter separated by the first character
1146of the
1147.SM
1148.B IFS
1149special variable. That is, "\fB$*\fP" is equivalent
1150to "\fB$1\fP\fIc\fP\fB$2\fP\fIc\fP\fB...\fP", where
1151.I c
1152is the first character of the value of the
1153.SM
1154.B IFS
1155variable. If
1156.SM
1157.B IFS
1158is unset, the parameters are separated by spaces.
1159If
1160.SM
1161.B IFS
1162is null, the parameters are joined without intervening separators.
1163.TP
1164.B @
1165Expands to the positional parameters, starting from one. When the
1166expansion occurs within double quotes, each parameter expands to a
1167separate word. That is, "\fB$@\fP" is equivalent to
1168"\fB$1\fP" "\fB$2\fP" ...
1169If the double-quoted expansion occurs within a word, the expansion of
1170the first parameter is joined with the beginning part of the original
1171word, and the expansion of the last parameter is joined with the last
1172part of the original word.
1173When there are no positional parameters, "\fB$@\fP" and
1174.B $@
1175expand to nothing (i.e., they are removed).
1176.TP
1177.B #
1178Expands to the number of positional parameters in decimal.
1179.TP
1180.B ?
1181Expands to the status of the most recently executed foreground
1182pipeline.
1183.TP
1184.B \-
1185Expands to the current option flags as specified upon invocation,
1186by the
1187.B set
1188builtin command, or those set by the shell itself
1189(such as the
1190.B \-i
1191option).
1192.TP
1193.B $
1194Expands to the process ID of the shell. In a () subshell, it
1195expands to the process ID of the current shell, not the
1196subshell.
1197.TP
1198.B !
1199Expands to the process ID of the most recently executed background
1200(asynchronous) command.
1201.TP
1202.B 0
1203Expands to the name of the shell or shell script. This is set at
1204shell initialization. If
1205.B bash
1206is invoked with a file of commands,
1207.B $0
1208is set to the name of that file. If
1209.B bash
1210is started with the
1211.B \-c
1212option, then
1213.B $0
1214is set to the first argument after the string to be
1215executed, if one is present. Otherwise, it is set
1216to the file name used to invoke
1217.BR bash ,
1218as given by argument zero.
1219.TP
1220.B _
1221At shell startup, set to the absolute pathname used to invoke the
1222shell or shell script being executed as passed in the environment
1223or argument list.
1224Subsequently, expands to the last argument to the previous command,
1225after expansion.
1226Also set to the full pathname used to invoke each command executed
1227and placed in the environment exported to that command.
1228When checking mail, this parameter holds the name of the mail file
1229currently being checked.
1230.PD
1231.SS Shell Variables
1232.PP
1233The following variables are set by the shell:
1234.PP
1235.PD 0
1236.TP
1237.B BASH
1238Expands to the full file name used to invoke this instance of
1239.BR bash .
1240.TP
1241.B BASH_ARGC
1242An array variable whose values are the number of parameters in each
1243frame of the current bash execution call stack.
1244The number of
1245parameters to the current subroutine (shell function or script executed
1246with \fB.\fP or \fBsource\fP) is at the top of the stack.
1247When a subroutine is executed, the number of parameters passed is pushed onto
1248\fBBASH_ARGC\fP.
1249The shell sets \fBBASH_ARGC\fP only when in extended debugging mode
1250(see the description of the
1251.B extdebug
1252option to the
1253.B shopt
1254builtin below)
1255.TP
1256.B BASH_ARGV
1257An array variable containing all of the parameters in the current bash
1258execution call stack. The final parameter of the last subroutine call
1259is at the top of the stack; the first parameter of the initial call is
1260at the bottom. When a subroutine is executed, the parameters supplied
1261are pushed onto \fBBASH_ARGV\fP.
1262The shell sets \fBBASH_ARGV\fP only when in extended debugging mode
1263(see the description of the
1264.B extdebug
1265option to the
1266.B shopt
1267builtin below)
1268.TP
1269.B BASH_COMMAND
1270The command currently being executed or about to be executed, unless the
1271shell is executing a command as the result of a trap,
1272in which case it is the command executing at the time of the trap.
1273.TP
1274.B BASH_EXECUTION_STRING
1275The command argument to the \fB\-c\fP invocation option.
1276.TP
1277.B BASH_LINENO
1278An array variable whose members are the line numbers in source files
1279corresponding to each member of \fBFUNCNAME\fP.
1280\fB${BASH_LINENO[\fP\fI$i\fP\fB]}\fP is the line number in the source
1281file where \fB${FUNCNAME[\fP\fI$ifP\fB]}\fP was called.
1282The corresponding source file name is \fB${BASH_SOURCE[\fP\fI$i\fP\fB]}\fB.
1283Use \fBLINENO\fP to obtain the current line number.
1284.TP
1285.B BASH_REMATCH
1286An array variable whose members are assigned by the \fB=~\fP binary
1287operator to the \fB[[\fP conditional command.
1288The element with index 0 is the portion of the string
1289matching the entire regular expression.
1290The element with index \fIn\fP is the portion of the
1291string matching the \fIn\fPth parenthesized subexpression.
1292This variable is read-only.
1293.TP
1294.B BASH_SOURCE
1295An array variable whose members are the source filenames corresponding
1296to the elements in the \fBFUNCNAME\fP array variable.
1297.TP
1298.B BASH_SUBSHELL
1299Incremented by one each time a subshell or subshell environment is spawned.
1300The initial value is 0.
1301.TP
1302.B BASH_VERSINFO
1303A readonly array variable whose members hold version information for
1304this instance of
1305.BR bash .
1306The values assigned to the array members are as follows:
1307.sp .5
1308.RS
1309.PD 0
1310.TP 24
1311.B BASH_VERSINFO[\fR0\fP]
1312The major version number (the \fIrelease\fP).
1313.TP
1314.B BASH_VERSINFO[\fR1\fP]
1315The minor version number (the \fIversion\fP).
1316.TP
1317.B BASH_VERSINFO[\fR2\fP]
1318The patch level.
1319.TP
1320.B BASH_VERSINFO[\fR3\fP]
1321The build version.
1322.TP
1323.B BASH_VERSINFO[\fR4\fP]
1324The release status (e.g., \fIbeta1\fP).
1325.TP
1326.B BASH_VERSINFO[\fR5\fP]
1327The value of \fBMACHTYPE\fP.
1328.PD
1329.RE
1330.TP
1331.B BASH_VERSION
1332Expands to a string describing the version of this instance of
1333.BR bash .
1334.TP
1335.B COMP_CWORD
1336An index into \fB${COMP_WORDS}\fP of the word containing the current
1337cursor position.
1338This variable is available only in shell functions invoked by the
1339programmable completion facilities (see \fBProgrammable Completion\fP
1340below).
1341.TP
1342.B COMP_LINE
1343The current command line.
1344This variable is available only in shell functions and external
1345commands invoked by the
1346programmable completion facilities (see \fBProgrammable Completion\fP
1347below).
1348.TP
1349.B COMP_POINT
1350The index of the current cursor position relative to the beginning of
1351the current command.
1352If the current cursor position is at the end of the current command,
1353the value of this variable is equal to \fB${#COMP_LINE}\fP.
1354This variable is available only in shell functions and external
1355commands invoked by the
1356programmable completion facilities (see \fBProgrammable Completion\fP
1357below).
1358.TP
1359.B COMP_WORDBREAKS
1360The set of characters that the Readline library treats as word
1361separators when performing word completion.
1362If
1363.SM
1364.B COMP_WORDBREAKS
1365is unset, it loses its special properties, even if it is
1366subsequently reset.
1367.TP
1368.B COMP_WORDS
1369An array variable (see \fBArrays\fP below) consisting of the individual
1370words in the current command line.
1371This variable is available only in shell functions invoked by the
1372programmable completion facilities (see \fBProgrammable Completion\fP
1373below).
1374.TP
1375.B DIRSTACK
1376An array variable (see
1377.B Arrays
1378below) containing the current contents of the directory stack.
1379Directories appear in the stack in the order they are displayed by the
1380.B dirs
1381builtin.
1382Assigning to members of this array variable may be used to modify
1383directories already in the stack, but the
1384.B pushd
1385and
1386.B popd
1387builtins must be used to add and remove directories.
1388Assignment to this variable will not change the current directory.
1389If
1390.SM
1391.B DIRSTACK
1392is unset, it loses its special properties, even if it is
1393subsequently reset.
1394.TP
1395.B EUID
1396Expands to the effective user ID of the current user, initialized at
1397shell startup. This variable is readonly.
1398.TP
1399.B FUNCNAME
1400An array variable containing the names of all shell functions
1401currently in the execution call stack.
1402The element with index 0 is the name of any currently-executing
1403shell function.
1404The bottom-most element is "main".
1405This variable exists only when a shell function is executing.
1406Assignments to
1407.SM
1408.B FUNCNAME
1409have no effect and return an error status.
1410If
1411.SM
1412.B FUNCNAME
1413is unset, it loses its special properties, even if it is
1414subsequently reset.
1415.TP
1416.B GROUPS
1417An array variable containing the list of groups of which the current
1418user is a member.
1419Assignments to
1420.SM
1421.B GROUPS
1422have no effect and return an error status.
1423If
1424.SM
1425.B GROUPS
1426is unset, it loses its special properties, even if it is
1427subsequently reset.
1428.TP
1429.B HISTCMD
1430The history number, or index in the history list, of the current
1431command.
1432If
1433.SM
1434.B HISTCMD
1435is unset, it loses its special properties, even if it is
1436subsequently reset.
1437.TP
1438.B HOSTNAME
1439Automatically set to the name of the current host.
1440.TP
1441.B HOSTTYPE
1442Automatically set to a string that uniquely
1443describes the type of machine on which
1444.B bash
1445is executing.
1446The default is system-dependent.
1447.TP
1448.B LINENO
1449Each time this parameter is referenced, the shell substitutes
1450a decimal number representing the current sequential line number
1451(starting with 1) within a script or function. When not in a
1452script or function, the value substituted is not guaranteed to
1453be meaningful.
1454If
1455.SM
1456.B LINENO
1457is unset, it loses its special properties, even if it is
1458subsequently reset.
1459.TP
1460.B MACHTYPE
1461Automatically set to a string that fully describes the system
1462type on which
1463.B bash
1464is executing, in the standard GNU \fIcpu-company-system\fP format.
1465The default is system-dependent.
1466.TP
1467.B OLDPWD
1468The previous working directory as set by the
1469.B cd
1470command.
1471.TP
1472.B OPTARG
1473The value of the last option argument processed by the
1474.B getopts
1475builtin command (see
1476.SM
1477.B SHELL BUILTIN COMMANDS
1478below).
1479.TP
1480.B OPTIND
1481The index of the next argument to be processed by the