Penguin
Blame: bash(1)Part2
EditPageHistoryDiffInfoLikePages
Annotated edit history of bash(1)Part2 version 1, including all changes. View license author blame.
Rev Author # Line
1 JohnMcPherson 1 !!BASH MAN PAGE (Part 2)
2
3 __Navigation__%%%
4 This man page has been split into 7 pages:%%%
5 bash(1)
6 * NAME
7 * SYNOPSIS
8 * COPYRIGHT
9 * DESCRIPTION
10 * OPTIONS
11 * ARGUMENTS
12 * INVOCATION
13
14 __Part 2__
15 * __DEFINITIONS__
16 * __RESERVED WORDS__
17 * __SHELL GRAMMAR__
18 * __COMMENTS__
19 * __QUOTING__
20
21 [bash(1)Part3]
22 * PARAMETERS
23
24 [bash(1)Part4]
25 * EXPANSION
26
27 [bash(1)Part5]
28 * REDIRECTION
29 * ALIASES
30 * FUNCTIONS
31 * ARITHMETIC EVALUATION
32 * CONDITIONAL EXPRESSIONS
33 * SIMPLE COMMAND EXPANSION
34 * COMMAND EXECUTION
35 * COMMAND EXECUTION ENVIRONMENT
36 * ENVIRONMENT
37 * EXIT STATUS
38 * SIGNALS
39 * JOB CONTROL
40 * PROMPTING
41
42 [bash(1)Part6]
43 * READLINE
44 * HISTORY
45 * HISTORY EXPANSION
46
47
48 [bash(1)Part7]
49 * SHELL BUILTIN COMMANDS
50 * RESTRICTED SHELL
51 * SEE ALSO
52 * FILES
53 * AUTHORS
54 * BUG REPORTS
55 * BUGS
56
57 ----
58 !!DEFINITIONS
59
60
61 The following definitions are used throughout the rest of this document.
62
63
64 ;__blank __ : A space or tab.
65 ;__word__ : A sequence of characters considered as a single unit by the shell. Also known as a __token__ .
66 ;__name__ : A ''word'' consisting only of alphanumeric characters and underscores, and beginning with an alphabetic character or an underscore. Also referred to as an __identifier__ .
67 ;__metacharacter__ : A character that, when unquoted, separates words. One of the following:
68
69
70
71 __| & ; ( ) < > space tab__
72
73
74
75 ;__control operator__ : A ''token'' that performs a control function. It is one of the following symbols:
76
77
78 __|| & && ; ;; ( ) | <newline>__
79
80
81
82
83 !!RESERVED WORDS
84 ''Reserved words'' are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see __SHELL GRAMMAR__ below) or the third word of a __case __ or __for__ command:
85
86 .B
87 ! case do done elif else esac fi for function if in select then until while { } time [[[[ ]]
88
89
90 !!SHELL GRAMMAR
91
92
93 !Simple Commands
94
95
96 A ''simple command'' is a sequence of optional variable assignments followed by __blank__-separated words and redirections, and terminated by a ''control operator''. The first word specifies the command to be executed. The remaining words are passed as arguments to the invoked command.
97
98 The return value of a ''simple command'' is its exit status, or 128+''n'' if the command is terminated by signal ''n'' .
99
100 !Pipelines
101
102
103 A ''pipeline'' is a sequence of one or more commands separated by the character __|__ . The format for a pipeline is:
104
105 [[__time__ [[__-p__]] [[ ! ] ''command'' [[ __|__ ''command2'' ... ]
106
107
108 The standard output of ''command'' is connected to the standard input of ''command2'' . This connection is performed before any redirections specified by the command (see __REDIRECTION__ below).
109
110 If the reserved word __!__ precedes a pipeline, the exit status of that pipeline is the logical NOT of the exit status of the last command. Otherwise, the status of the pipeline is the exit status of the last command. The shell waits for all commands in the pipeline to terminate before returning a value.
111
112 If the __time__ reserved word precedes a pipeline, the elapsed as well as user and system time consumed by its execution are reported when the pipeline terminates. The __-p__ option changes the output format to that specified by POSIX. The __TIMEFORMAT__ variable may be set to a format string that specifies how the timing information should be displayed; see the description of __TIMEFORMAT__ under __Shell Variables__ below.
113
114 Each command in a pipeline is executed as a separate process (i.e., in a subshell).
115
116 !Lists
117
118
119 A ''list'' is a sequence of one or more pipelines separated by one of the operators __;__ , __&__ , __&&__ , or __||__ , and optionally terminated by one of __;__ , __&__ , or __<newline>__ .
120
121 Of these list operators, __&&__ and __||__ have equal precedence, followed by __;__ and __&,__ which have equal precedence.
122
123 If a command is terminated by the control operator __&__ , the shell executes the command in the ''background'' in a subshell. The shell does not wait for the command to finish, and the return status is 0. Commands separated by a __;__ are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.
124
125 The control operators __&&__ and __||__ denote AND lists and OR lists, respectively. An AND list has the form
126
127 ''command1'' __&&__ ''command2''
128
129
130 ''command2'' is executed if, and only if, ''command1'' returns an exit status of zero.
131
132 An OR list has the form
133
134 ''command1'' __||__ ''command2''
135
136
137 ''command2'' is executed if and only if ''command1'' returns a non-zero exit status. The return status of AND and OR lists is the exit status of the last command executed in the list.
138
139 !Compound Commands
140
141
142 A ''compound command'' is one of the following:
143 ;(''list'') : ''list'' is executed in a subshell. Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of ''list''.
144 ;{ ''list''; } : ''list'' is simply executed in the current shell environment. ''list'' must be terminated with a newline or semicolon. This is known as a ''group command''. The return status is the exit status of ''list''.
145 ;((''expression'')) : The ''expression'' is evaluated according to the rules described below under __ARITHMETIC EVALUATION__ . If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to __let "''expression''__".
146 ;__[[[[__ ''expression'' __]]__ : Return a status of 0 or 1 depending on the evaluation of the conditional expression ''expression''. Expressions are composed of the primaries described below under __CONDITIONAL EXPRESSIONS__ . Word splitting and pathname expansion are not performed on the words between the __[[[[__ and __]]__; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed.
147 .sp 1When the __==__ and __!=__ operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under __Pattern Matching__. The return value is 0 if the string matches or does not match the pattern, respectively, and 1 otherwise. Any part of the pattern may be quoted to force it to be matched as a string.
148 .sp 1Expressions may be combined using the following operators, listed in decreasing order of precedence:
149 .sp 1
150
151
152 ;__( ''expression'' )__ : Returns the value of ''expression''. This may be used to override the normal precedence of operators.
153 ;__! ''expression''__ : True if ''expression'' is false.
154 ;''expression1'' __&&__ ''expression2'' : True if both ''expression1'' and ''expression2'' are true.
155 ; ''expression1'' __||__ ''expression2'' : True if either ''expression1'' or ''expression2'' is true.
156
157
158
159 The __&&__ and
160 __||__ operators do not execute ''expression2'' if the value of ''expression1'' is sufficient to determine the return value of the entire conditional expression.
161 ;__for__ ''name'' [[ __in__ ''word'' ] ; __do__ ''list'' ; __done__ : The list of words following __in__ is expanded, generating a list of items. The variable ''name'' is set to each element of this list in turn, and ''list'' is executed each time. If the __in__ ''word'' is omitted, the __for__ command executes ''list'' once for each positional parameter that is set (see __PARAMETERS__ below). The return status is the exit status of the last command that executes. If the expansion of the items following __in__ results in an empty list, no commands are executed, and the return status is 0.
162 ;__for__ (( ''expr1'' ; ''expr2'' ; ''expr3'' )) ; __do__ ''list'' ; __done__ : First, the arithmetic expression ''expr1'' is evaluated according to the rules described below under __ARITHMETIC EVALUATION__ . The arithmetic expression ''expr2'' is then evaluated repeatedly until it evaluates to zero. Each time ''expr2'' evaluates to a non-zero value, ''list'' is executed and the arithmetic expression ''expr3'' is evaluated. If any expression is omitted, it behaves as if it evaluates to 1. The return value is the exit status of the last command in ''list'' that is executed, or false if any of the expressions is invalid.
163 ;__select__ ''name'' [[ __in__ ''word'' ] ; __do__ ''list'' ; __done__ : The list of words following __in__ is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the __in__ ''word'' is omitted, the positional parameters are printed (see __PARAMETERS__ below). The __PS3__ prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of ''name'' is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes ''name'' to be set to null. The line read is saved in the variable __REPLY__ . The ''list'' is executed after each selection until a __break__ or __return__ command is executed. The exit status of __select__ is the exit status of the last command executed in ''list'' , or zero if no commands were executed.
164 ;__case__ ''word'' __in__ [[ [[(] ''pattern'' [[ __|__ ''pattern'' ] ... ) ''list'' ;; ] ... __esac__: A __case__ command first expands ''word'', and tries to match it against each ''pattern'' in turn, using the same matching rules as for pathname expansion (see __Pathname Expansion__ below). When a match is found, the corresponding ''list'' is executed. After the first match, no subsequent matches are attempted. The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in ''list''.
165 ;__if__ ''list''; __then__ ''list;'' \ : [[ __elif__ ''list''; __then__ ''list''; ] ... \ [[ __else__ ''list''; ] __fi__ The __if __ ''list'' is executed. If its exit status is zero, the __then__ ''list'' is executed. Otherwise, each __elif__ ''list'' is executed in turn, and if its exit status is zero, the corresponding __then__ ''list'' is executed and the command completes. Otherwise, the __else__ ''list'' is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.
166
167
168 __while__ ''list''; __do__ ''list''; __done__
169 ;__until__ ''list''; __do__ ''list''; __done__ : The __while__ command continuously executes the __do__ ''list'' as long as the last command in ''list'' returns an exit status of zero. The __until__ command is identical to the __while__ command, except that the test is negated; the __do__ ''list'' is executed as long as the last command in ''list'' returns a non-zero exit status. The exit status of the __while__ and __until__ commands is the exit status of the last __do__ ''list'' command executed, or zero if none was executed.
170 ;[[ __function__ ] ''name'' () { ''list''; } : This defines a function named ''name''. The ''body'' of the function is the ''list'' of commands between { and }. This list is executed whenever ''name'' is specified as the name of a simple command. The exit status of a function is the exit status of the last command executed in the body. (See __FUNCTIONS__ below.)
171
172 !!COMMENTS
173 In a non-interactive shell, or an interactive shell in which the __interactive_comments__ option to the __shopt__ builtin is enabled (see __SHELL BUILTIN COMMANDS__ below), a word beginning with __#__ causes that word and all remaining characters on that line to be ignored. An interactive shell without the __interactive_comments__ option enabled does not allow comments. The __interactive_comments__ option is on by default in interactive shells.
174
175 !!QUOTING
176 ''Quoting'' is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.
177
178 Each of the ''metacharacters'' listed above under __DEFINITIONS__ has special meaning to the shell and must be quoted if it is to represent itself.
179
180 When the command history expansion facilities are being used, the ''history expansion'' character, usually __!__, must be quoted to prevent history expansion.
181
182 There are three quoting mechanisms: the ''escape character'' , single quotes, and double quotes.
183
184 A non-quoted backslash (__\__) is the ''escape character'' . It preserves the literal value of the next character that follows, with the exception of <newline>. If a __\__<newline> pair appears, and the backslash is not itself quoted, the __\__<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).
185
186 Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.
187
188 Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of __$__ , __`__ , and __\__ . The characters __$__ and __`__ retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: __$__ , __`__ , __"__, __\__ , or __<newline>__ . A double quote may be quoted within double quotes by preceding it with a backslash.
189
190 The special parameters __*__ and __@__ have special meaning when in double quotes (see __PARAMETERS__ below).
191
192 Words of the form __$__'''string''' are treated specially. The word expands to ''string'', with backslash-escaped characters replaced as specifed by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
193
194
195 ;__\a__ : alert (bell)
196 ;__\b__ : backspace
197 ;__\e__ : an escape character
198 ;__\f__ : form feed
199 ;__\n__ : new line
200 ;__\r__ : carriage return
201 ;__\t__ : horizontal tab
202 ;__\v__ : vertical tab
203 ;__\\e__ : backslash
204 ;__\'__ : single quote
205 ;__\''nnn''__ : the character whose ASCII code is the octal value ''nnn'' (one to three digits)
206 ;__\x''nnn''__ : the character whose ASCII code is the hexadecimal value ''nnn'' (one to three digits)
207
208
209
210
211 The expanded result is single-quoted, as if the dollar sign had not been present.
212
213 A double-quoted string preceded by a dollar sign (__$__) will cause the string to be translated according to the current locale. If the current locale is __C__ or __POSIX__, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.