]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
1 | \input texinfo.tex @c -*- texinfo -*- |
2 | @c %**start of header | |
3 | @setfilename bashref.info | |
4 | @settitle Bash Reference Manual | |
ccc6cda3 | 5 | |
d3a24ed2 | 6 | @include version.texi |
df0e4bfe | 7 | @c %**end of header |
e8ce775d | 8 | |
d3a24ed2 | 9 | @copying |
ccc6cda3 | 10 | This text is a brief description of the features that are present in |
d3a24ed2 | 11 | the Bash shell (version @value{VERSION}, @value{UPDATED}). |
ccc6cda3 JA |
12 | |
13 | This is Edition @value{EDITION}, last updated @value{UPDATED}, | |
14 | of @cite{The GNU Bash Reference Manual}, | |
15 | for @code{Bash}, Version @value{VERSION}. | |
16 | ||
5a318736 | 17 | Copyright @copyright{} 1988--2012 Free Software Foundation, Inc. |
ccc6cda3 | 18 | |
d3a24ed2 CR |
19 | @quotation |
20 | Permission is granted to copy, distribute and/or modify this document | |
c302751c | 21 | under the terms of the GNU Free Documentation License, Version 1.3 or |
d3a24ed2 | 22 | any later version published by the Free Software Foundation; with no |
8a44b411 CR |
23 | Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. |
24 | A copy of the license is included in the section entitled | |
25 | ``GNU Free Documentation License''. | |
d3a24ed2 CR |
26 | @end quotation |
27 | @end copying | |
28 | ||
29 | @defcodeindex bt | |
30 | @defcodeindex rw | |
31 | @set BashFeatures | |
32 | ||
33 | @dircategory Basics | |
34 | @direntry | |
35 | * Bash: (bash). The GNU Bourne-Again SHell. | |
36 | @end direntry | |
37 | ||
38 | @finalout | |
ccc6cda3 JA |
39 | |
40 | @titlepage | |
41 | @title Bash Reference Manual | |
42 | @subtitle Reference Documentation for Bash | |
b72432fd | 43 | @subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}. |
d3a24ed2 | 44 | @subtitle @value{UPDATED-MONTH} |
ccc6cda3 JA |
45 | @author Chet Ramey, Case Western Reserve University |
46 | @author Brian Fox, Free Software Foundation | |
d3a24ed2 | 47 | |
ccc6cda3 JA |
48 | @page |
49 | @vskip 0pt plus 1filll | |
d3a24ed2 | 50 | @insertcopying |
ccc6cda3 | 51 | |
ccc6cda3 JA |
52 | @end titlepage |
53 | ||
d3a24ed2 CR |
54 | @contents |
55 | ||
56 | @ifnottex | |
ccc6cda3 JA |
57 | @node Top, Introduction, (dir), (dir) |
58 | @top Bash Features | |
ccc6cda3 | 59 | |
ccc6cda3 | 60 | This text is a brief description of the features that are present in |
462a7a25 | 61 | the Bash shell (version @value{VERSION}, @value{UPDATED}). |
df0e4bfe | 62 | The Bash home page is @url{http://www.gnu.org/software/bash/}. |
ccc6cda3 JA |
63 | |
64 | This is Edition @value{EDITION}, last updated @value{UPDATED}, | |
65 | of @cite{The GNU Bash Reference Manual}, | |
66 | for @code{Bash}, Version @value{VERSION}. | |
67 | ||
ccc6cda3 JA |
68 | Bash contains features that appear in other popular shells, and some |
69 | features that only appear in Bash. Some of the shells that Bash has | |
70 | borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell | |
71 | (@file{ksh}), and the C-shell (@file{csh} and its successor, | |
b28ff8c9 | 72 | @file{tcsh}). The following menu breaks the features up into |
ed3f3b6c CR |
73 | categories, noting which features were inspired by other shells and |
74 | which are specific to Bash. | |
ccc6cda3 JA |
75 | |
76 | This manual is meant as a brief introduction to features found in | |
77 | Bash. The Bash manual page should be used as the definitive | |
78 | reference on shell behavior. | |
79 | ||
80 | @menu | |
81 | * Introduction:: An introduction to the shell. | |
ccc6cda3 JA |
82 | * Definitions:: Some definitions used in the rest of this |
83 | manual. | |
ccc6cda3 | 84 | * Basic Shell Features:: The shell "building blocks". |
bb70624e | 85 | * Shell Builtin Commands:: Commands that are a part of the shell. |
bb70624e | 86 | * Shell Variables:: Variables used or set by Bash. |
ccc6cda3 | 87 | * Bash Features:: Features found only in Bash. |
0f445e6c CR |
88 | * Job Control:: What job control is and how Bash allows you |
89 | to use it. | |
ccc6cda3 JA |
90 | * Command Line Editing:: Chapter describing the command line |
91 | editing features. | |
9d85af6d | 92 | * Using History Interactively:: Command History Expansion |
ccc6cda3 | 93 | * Installing Bash:: How to build and install Bash on your system. |
ccc6cda3 | 94 | * Reporting Bugs:: How to report bugs in Bash. |
bb70624e JA |
95 | * Major Differences From The Bourne Shell:: A terse list of the differences |
96 | between Bash and historical | |
97 | versions of /bin/sh. | |
de00a878 | 98 | * GNU Free Documentation License:: Copying and sharing this documentation. |
d3ad40de | 99 | * Indexes:: Various indexes for this manual. |
ccc6cda3 | 100 | @end menu |
d3a24ed2 | 101 | @end ifnottex |
ccc6cda3 JA |
102 | |
103 | @node Introduction | |
104 | @chapter Introduction | |
105 | @menu | |
106 | * What is Bash?:: A short description of Bash. | |
ccc6cda3 JA |
107 | * What is a shell?:: A brief introduction to shells. |
108 | @end menu | |
109 | ||
110 | @node What is Bash? | |
111 | @section What is Bash? | |
112 | ||
113 | Bash is the shell, or command language interpreter, | |
bb70624e | 114 | for the @sc{gnu} operating system. |
ccc6cda3 | 115 | The name is an acronym for the @samp{Bourne-Again SHell}, |
bb70624e | 116 | a pun on Stephen Bourne, the author of the direct ancestor of |
d3a24ed2 | 117 | the current Unix shell @code{sh}, |
ccc6cda3 JA |
118 | which appeared in the Seventh Edition Bell Labs Research version |
119 | of Unix. | |
120 | ||
bb70624e | 121 | Bash is largely compatible with @code{sh} and incorporates useful |
ccc6cda3 | 122 | features from the Korn shell @code{ksh} and the C shell @code{csh}. |
bb70624e | 123 | It is intended to be a conformant implementation of the @sc{ieee} |
ac18b312 CR |
124 | @sc{posix} Shell and Tools portion of the @sc{ieee} @sc{posix} |
125 | specification (@sc{ieee} Standard 1003.1). | |
cce855bc JA |
126 | It offers functional improvements over @code{sh} for both interactive and |
127 | programming use. | |
ccc6cda3 | 128 | |
bb70624e JA |
129 | While the @sc{gnu} operating system provides other shells, including |
130 | a version of @code{csh}, Bash is the default shell. | |
131 | Like other @sc{gnu} software, Bash is quite portable. It currently runs | |
ccc6cda3 | 132 | on nearly every version of Unix and a few other operating systems @minus{} |
bb70624e | 133 | independently-supported ports exist for @sc{ms-dos}, @sc{os/2}, |
d3a24ed2 | 134 | and Windows platforms. |
ccc6cda3 JA |
135 | |
136 | @node What is a shell? | |
137 | @section What is a shell? | |
138 | ||
139 | At its base, a shell is simply a macro processor that executes | |
d3a24ed2 CR |
140 | commands. The term macro processor means functionality where text |
141 | and symbols are expanded to create larger expressions. | |
142 | ||
143 | A Unix shell is both a command interpreter and a programming | |
144 | language. As a command interpreter, the shell provides the user | |
145 | interface to the rich set of @sc{gnu} utilities. The programming | |
d3ad40de | 146 | language features allow these utilities to be combined. |
d3a24ed2 | 147 | Files containing commands can be created, and become |
ccc6cda3 | 148 | commands themselves. These new commands have the same status as |
bb70624e | 149 | system commands in directories such as @file{/bin}, allowing users |
d3a24ed2 CR |
150 | or groups to establish custom environments to automate their common |
151 | tasks. | |
152 | ||
153 | Shells may be used interactively or non-interactively. In | |
154 | interactive mode, they accept input typed from the keyboard. | |
155 | When executing non-interactively, shells execute commands read | |
156 | from a file. | |
ccc6cda3 | 157 | |
bb70624e | 158 | A shell allows execution of @sc{gnu} commands, both synchronously and |
cce855bc JA |
159 | asynchronously. |
160 | The shell waits for synchronous commands to complete before accepting | |
161 | more input; asynchronous commands continue to execute in parallel | |
162 | with the shell while it reads and executes additional commands. | |
163 | The @dfn{redirection} constructs permit | |
bb70624e JA |
164 | fine-grained control of the input and output of those commands. |
165 | Moreover, the shell allows control over the contents of commands' | |
166 | environments. | |
bb70624e JA |
167 | |
168 | Shells also provide a small set of built-in | |
ccc6cda3 | 169 | commands (@dfn{builtins}) implementing functionality impossible |
bb70624e JA |
170 | or inconvenient to obtain via separate utilities. |
171 | For example, @code{cd}, @code{break}, @code{continue}, and | |
276cb932 | 172 | @code{exec} cannot be implemented outside of the shell because |
bb70624e JA |
173 | they directly manipulate the shell itself. |
174 | The @code{history}, @code{getopts}, @code{kill}, or @code{pwd} | |
175 | builtins, among others, could be implemented in separate utilities, | |
176 | but they are more convenient to use as builtin commands. | |
177 | All of the shell builtins are described in | |
ccc6cda3 JA |
178 | subsequent sections. |
179 | ||
180 | While executing commands is essential, most of the power (and | |
181 | complexity) of shells is due to their embedded programming | |
182 | languages. Like any high-level language, the shell provides | |
183 | variables, flow control constructs, quoting, and functions. | |
184 | ||
bb70624e | 185 | Shells offer features geared specifically for |
ccc6cda3 JA |
186 | interactive use rather than to augment the programming language. |
187 | These interactive features include job control, command line | |
d3a24ed2 | 188 | editing, command history and aliases. Each of these features is |
ccc6cda3 JA |
189 | described in this manual. |
190 | ||
191 | @node Definitions | |
192 | @chapter Definitions | |
193 | These definitions are used throughout the remainder of this manual. | |
194 | ||
195 | @table @code | |
196 | ||
197 | @item POSIX | |
198 | @cindex POSIX | |
199 | A family of open system standards based on Unix. Bash | |
ac18b312 CR |
200 | is primarily concerned with the Shell and Utilities portion of the |
201 | @sc{posix} 1003.1 standard. | |
ccc6cda3 JA |
202 | |
203 | @item blank | |
204 | A space or tab character. | |
205 | ||
206 | @item builtin | |
207 | @cindex builtin | |
208 | A command that is implemented internally by the shell itself, rather | |
209 | than by an executable program somewhere in the file system. | |
210 | ||
211 | @item control operator | |
212 | @cindex control operator | |
d7f49990 | 213 | A @code{token} that performs a control function. It is a @code{newline} |
ccc6cda3 JA |
214 | or one of the following: |
215 | @samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;}, | |
8943768b | 216 | @samp{|}, @samp{|&}, @samp{(}, or @samp{)}. |
ccc6cda3 JA |
217 | |
218 | @item exit status | |
219 | @cindex exit status | |
f73dda09 JA |
220 | The value returned by a command to its caller. The value is restricted |
221 | to eight bits, so the maximum value is 255. | |
ccc6cda3 JA |
222 | |
223 | @item field | |
224 | @cindex field | |
225 | A unit of text that is the result of one of the shell expansions. After | |
226 | expansion, when executing a command, the resulting fields are used as | |
227 | the command name and arguments. | |
228 | ||
229 | @item filename | |
230 | @cindex filename | |
231 | A string of characters used to identify a file. | |
232 | ||
233 | @item job | |
234 | @cindex job | |
235 | A set of processes comprising a pipeline, and any processes descended | |
236 | from it, that are all in the same process group. | |
237 | ||
238 | @item job control | |
239 | @cindex job control | |
d166f048 JA |
240 | A mechanism by which users can selectively stop (suspend) and restart |
241 | (resume) execution of processes. | |
ccc6cda3 JA |
242 | |
243 | @item metacharacter | |
244 | @cindex metacharacter | |
245 | A character that, when unquoted, separates words. A metacharacter is | |
246 | a @code{blank} or one of the following characters: | |
247 | @samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or | |
248 | @samp{>}. | |
249 | ||
250 | @item name | |
251 | @cindex name | |
252 | @cindex identifier | |
253 | A @code{word} consisting solely of letters, numbers, and underscores, | |
254 | and beginning with a letter or underscore. @code{Name}s are used as | |
255 | shell variable and function names. | |
256 | Also referred to as an @code{identifier}. | |
257 | ||
258 | @item operator | |
259 | @cindex operator, shell | |
260 | A @code{control operator} or a @code{redirection operator}. | |
261 | @xref{Redirections}, for a list of redirection operators. | |
3abf178f | 262 | Operators contain at least one unquoted @code{metacharacter}. |
ccc6cda3 JA |
263 | |
264 | @item process group | |
265 | @cindex process group | |
266 | A collection of related processes each having the same process | |
bb70624e | 267 | group @sc{id}. |
ccc6cda3 JA |
268 | |
269 | @item process group ID | |
270 | @cindex process group ID | |
d3ad40de | 271 | A unique identifier that represents a @code{process group} |
ccc6cda3 JA |
272 | during its lifetime. |
273 | ||
274 | @item reserved word | |
275 | @cindex reserved word | |
276 | A @code{word} that has a special meaning to the shell. Most reserved | |
277 | words introduce shell flow control constructs, such as @code{for} and | |
278 | @code{while}. | |
279 | ||
280 | @item return status | |
281 | @cindex return status | |
282 | A synonym for @code{exit status}. | |
283 | ||
284 | @item signal | |
285 | @cindex signal | |
bb70624e | 286 | A mechanism by which a process may be notified by the kernel |
ccc6cda3 JA |
287 | of an event occurring in the system. |
288 | ||
289 | @item special builtin | |
290 | @cindex special builtin | |
291 | A shell builtin command that has been classified as special by the | |
ac18b312 | 292 | @sc{posix} standard. |
ccc6cda3 JA |
293 | |
294 | @item token | |
295 | @cindex token | |
3abf178f CR |
296 | A sequence of characters considered a single unit by the shell. |
297 | It is either a @code{word} or an @code{operator}. | |
ccc6cda3 JA |
298 | |
299 | @item word | |
300 | @cindex word | |
3abf178f CR |
301 | A sequence of characters treated as a unit by the shell. |
302 | Words may not include unquoted @code{metacharacters}. | |
ccc6cda3 JA |
303 | @end table |
304 | ||
305 | @node Basic Shell Features | |
306 | @chapter Basic Shell Features | |
307 | @cindex Bourne shell | |
308 | ||
309 | Bash is an acronym for @samp{Bourne-Again SHell}. | |
310 | The Bourne shell is | |
311 | the traditional Unix shell originally written by Stephen Bourne. | |
312 | All of the Bourne shell builtin commands are available in Bash, | |
d3a24ed2 CR |
313 | The rules for evaluation and quoting are taken from the @sc{posix} |
314 | specification for the `standard' Unix shell. | |
ccc6cda3 | 315 | |
cce855bc | 316 | This chapter briefly summarizes the shell's `building blocks': |
ccc6cda3 JA |
317 | commands, control structures, shell functions, shell @i{parameters}, |
318 | shell expansions, | |
319 | @i{redirections}, which are a way to direct input and output from | |
320 | and to named files, and how the shell executes commands. | |
321 | ||
322 | @menu | |
323 | * Shell Syntax:: What your input means to the shell. | |
cce855bc | 324 | * Shell Commands:: The types of commands you can use. |
ccc6cda3 | 325 | * Shell Functions:: Grouping commands by name. |
d3a24ed2 CR |
326 | * Shell Parameters:: How the shell stores values. |
327 | * Shell Expansions:: How Bash expands parameters and the various | |
ccc6cda3 JA |
328 | expansions available. |
329 | * Redirections:: A way to control where input and output go. | |
330 | * Executing Commands:: What happens when you run a command. | |
331 | * Shell Scripts:: Executing files of shell commands. | |
332 | @end menu | |
333 | ||
334 | @node Shell Syntax | |
335 | @section Shell Syntax | |
336 | @menu | |
337 | * Shell Operation:: The basic operation of the shell. | |
ccc6cda3 | 338 | * Quoting:: How to remove the special meaning from characters. |
ccc6cda3 JA |
339 | * Comments:: How to specify comments. |
340 | @end menu | |
341 | ||
bb70624e JA |
342 | When the shell reads input, it proceeds through a |
343 | sequence of operations. If the input indicates the beginning of a | |
344 | comment, the shell ignores the comment symbol (@samp{#}), and the rest | |
345 | of that line. | |
346 | ||
347 | Otherwise, roughly speaking, the shell reads its input and | |
348 | divides the input into words and operators, employing the quoting rules | |
349 | to select which meanings to assign various words and characters. | |
350 | ||
351 | The shell then parses these tokens into commands and other constructs, | |
352 | removes the special meaning of certain words or characters, expands | |
353 | others, redirects input and output as needed, executes the specified | |
354 | command, waits for the command's exit status, and makes that exit status | |
355 | available for further inspection or processing. | |
356 | ||
ccc6cda3 JA |
357 | @node Shell Operation |
358 | @subsection Shell Operation | |
359 | ||
360 | The following is a brief description of the shell's operation when it | |
361 | reads and executes a command. Basically, the shell does the | |
362 | following: | |
363 | ||
364 | @enumerate | |
365 | @item | |
366 | Reads its input from a file (@pxref{Shell Scripts}), from a string | |
28ef6c31 | 367 | supplied as an argument to the @option{-c} invocation option |
ccc6cda3 JA |
368 | (@pxref{Invoking Bash}), or from the user's terminal. |
369 | ||
370 | @item | |
371 | Breaks the input into words and operators, obeying the quoting rules | |
cce855bc | 372 | described in @ref{Quoting}. These tokens are separated by |
ccc6cda3 JA |
373 | @code{metacharacters}. Alias expansion is performed by this step |
374 | (@pxref{Aliases}). | |
375 | ||
376 | @item | |
cce855bc JA |
377 | Parses the tokens into simple and compound commands |
378 | (@pxref{Shell Commands}). | |
ccc6cda3 JA |
379 | |
380 | @item | |
381 | Performs the various shell expansions (@pxref{Shell Expansions}), breaking | |
382 | the expanded tokens into lists of filenames (@pxref{Filename Expansion}) | |
383 | and commands and arguments. | |
384 | ||
385 | @item | |
386 | Performs any necessary redirections (@pxref{Redirections}) and removes | |
387 | the redirection operators and their operands from the argument list. | |
388 | ||
389 | @item | |
390 | Executes the command (@pxref{Executing Commands}). | |
391 | ||
392 | @item | |
393 | Optionally waits for the command to complete and collects its exit | |
cce855bc | 394 | status (@pxref{Exit Status}). |
ccc6cda3 JA |
395 | |
396 | @end enumerate | |
397 | ||
398 | @node Quoting | |
399 | @subsection Quoting | |
400 | @cindex quoting | |
401 | @menu | |
402 | * Escape Character:: How to remove the special meaning from a single | |
403 | character. | |
404 | * Single Quotes:: How to inhibit all interpretation of a sequence | |
405 | of characters. | |
406 | * Double Quotes:: How to suppress most of the interpretation of a | |
407 | sequence of characters. | |
408 | * ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings. | |
ccc6cda3 JA |
409 | * Locale Translation:: How to translate strings into different languages. |
410 | @end menu | |
411 | ||
412 | Quoting is used to remove the special meaning of certain | |
413 | characters or words to the shell. Quoting can be used to | |
414 | disable special treatment for special characters, to prevent | |
415 | reserved words from being recognized as such, and to prevent | |
416 | parameter expansion. | |
417 | ||
cce855bc JA |
418 | Each of the shell metacharacters (@pxref{Definitions}) |
419 | has special meaning to the shell and must be quoted if it is to | |
bb70624e | 420 | represent itself. |
5c96a638 CR |
421 | When the command history expansion facilities are being used |
422 | (@pxref{History Interaction}), the | |
bb70624e | 423 | @var{history expansion} character, usually @samp{!}, must be quoted |
28ef6c31 | 424 | to prevent history expansion. @xref{Bash History Facilities}, for |
bb70624e | 425 | more details concerning history expansion. |
d3a24ed2 | 426 | |
bb70624e | 427 | There are three quoting mechanisms: the |
ccc6cda3 JA |
428 | @var{escape character}, single quotes, and double quotes. |
429 | ||
430 | @node Escape Character | |
431 | @subsubsection Escape Character | |
432 | A non-quoted backslash @samp{\} is the Bash escape character. | |
433 | It preserves the literal value of the next character that follows, | |
434 | with the exception of @code{newline}. If a @code{\newline} pair | |
cce855bc JA |
435 | appears, and the backslash itself is not quoted, the @code{\newline} |
436 | is treated as a line continuation (that is, it is removed from | |
437 | the input stream and effectively ignored). | |
ccc6cda3 JA |
438 | |
439 | @node Single Quotes | |
440 | @subsubsection Single Quotes | |
441 | ||
bb70624e | 442 | Enclosing characters in single quotes (@samp{'}) preserves the literal value |
ccc6cda3 JA |
443 | of each character within the quotes. A single quote may not occur |
444 | between single quotes, even when preceded by a backslash. | |
445 | ||
446 | @node Double Quotes | |
447 | @subsubsection Double Quotes | |
448 | ||
bb70624e | 449 | Enclosing characters in double quotes (@samp{"}) preserves the literal value |
ccc6cda3 | 450 | of all characters within the quotes, with the exception of |
5c96a638 CR |
451 | @samp{$}, @samp{`}, @samp{\}, |
452 | and, when history expansion is enabled, @samp{!}. | |
ccc6cda3 | 453 | The characters @samp{$} and @samp{`} |
cce855bc JA |
454 | retain their special meaning within double quotes (@pxref{Shell Expansions}). |
455 | The backslash retains its special meaning only when followed by one of | |
456 | the following characters: | |
ccc6cda3 | 457 | @samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}. |
cce855bc JA |
458 | Within double quotes, backslashes that are followed by one of these |
459 | characters are removed. Backslashes preceding characters without a | |
460 | special meaning are left unmodified. | |
ccc6cda3 JA |
461 | A double quote may be quoted within double quotes by preceding it with |
462 | a backslash. | |
5c96a638 CR |
463 | If enabled, history expansion will be performed unless an @samp{!} |
464 | appearing in double quotes is escaped using a backslash. | |
465 | The backslash preceding the @samp{!} is not removed. | |
ccc6cda3 JA |
466 | |
467 | The special parameters @samp{*} and @samp{@@} have special meaning | |
468 | when in double quotes (@pxref{Shell Parameter Expansion}). | |
469 | ||
470 | @node ANSI-C Quoting | |
471 | @subsubsection ANSI-C Quoting | |
472 | @cindex quoting, ANSI | |
473 | ||
474 | Words of the form @code{$'@var{string}'} are treated specially. The | |
475 | word expands to @var{string}, with backslash-escaped characters replaced | |
28ef6c31 | 476 | as specified by the ANSI C standard. Backslash escape sequences, if |
ccc6cda3 JA |
477 | present, are decoded as follows: |
478 | ||
479 | @table @code | |
480 | @item \a | |
481 | alert (bell) | |
482 | @item \b | |
483 | backspace | |
484 | @item \e | |
a3143574 | 485 | @itemx \E |
ccc6cda3 JA |
486 | an escape character (not ANSI C) |
487 | @item \f | |
488 | form feed | |
489 | @item \n | |
490 | newline | |
491 | @item \r | |
492 | carriage return | |
493 | @item \t | |
494 | horizontal tab | |
495 | @item \v | |
496 | vertical tab | |
497 | @item \\ | |
498 | backslash | |
bb70624e JA |
499 | @item \' |
500 | single quote | |
a3143574 CR |
501 | @item \" |
502 | double quote | |
ccc6cda3 | 503 | @item \@var{nnn} |
f73dda09 | 504 | the eight-bit character whose value is the octal value @var{nnn} |
cce855bc | 505 | (one to three digits) |
f73dda09 JA |
506 | @item \x@var{HH} |
507 | the eight-bit character whose value is the hexadecimal value @var{HH} | |
508 | (one or two hex digits) | |
eb0b2ad8 CR |
509 | @item \u@var{HHHH} |
510 | the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value | |
511 | @var{HHHH} (one to four hex digits) | |
512 | @item \U@var{HHHHHHHH} | |
513 | the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value | |
514 | @var{HHHHHHHH} (one to eight hex digits) | |
7117c2d2 JA |
515 | @item \c@var{x} |
516 | a control-@var{x} character | |
ccc6cda3 JA |
517 | @end table |
518 | ||
519 | @noindent | |
bb70624e JA |
520 | The expanded result is single-quoted, as if the dollar sign had not |
521 | been present. | |
ccc6cda3 JA |
522 | |
523 | @node Locale Translation | |
524 | @subsubsection Locale-Specific Translation | |
525 | @cindex localization | |
f73dda09 JA |
526 | @cindex internationalization |
527 | @cindex native languages | |
528 | @cindex translation, native languages | |
ccc6cda3 JA |
529 | |
530 | A double-quoted string preceded by a dollar sign (@samp{$}) will cause | |
531 | the string to be translated according to the current locale. | |
532 | If the current locale is @code{C} or @code{POSIX}, the dollar sign | |
533 | is ignored. | |
534 | If the string is translated and replaced, the replacement is | |
535 | double-quoted. | |
536 | ||
f73dda09 JA |
537 | @vindex LC_MESSAGES |
538 | @vindex TEXTDOMAIN | |
539 | @vindex TEXTDOMAINDIR | |
28ef6c31 JA |
540 | Some systems use the message catalog selected by the @env{LC_MESSAGES} |
541 | shell variable. Others create the name of the message catalog from the | |
542 | value of the @env{TEXTDOMAIN} shell variable, possibly adding a | |
543 | suffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, you | |
544 | may need to set the @env{TEXTDOMAINDIR} variable to the location of | |
f73dda09 JA |
545 | the message catalog files. Still others use both variables in this |
546 | fashion: | |
547 | @env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo. | |
28ef6c31 | 548 | |
ccc6cda3 JA |
549 | @node Comments |
550 | @subsection Comments | |
551 | @cindex comments, shell | |
552 | ||
553 | In a non-interactive shell, or an interactive shell in which the | |
554 | @code{interactive_comments} option to the @code{shopt} | |
d3ad40de | 555 | builtin is enabled (@pxref{The Shopt Builtin}), |
ccc6cda3 JA |
556 | a word beginning with @samp{#} |
557 | causes that word and all remaining characters on that line to | |
558 | be ignored. An interactive shell without the @code{interactive_comments} | |
559 | option enabled does not allow comments. The @code{interactive_comments} | |
560 | option is on by default in interactive shells. | |
bb70624e | 561 | @xref{Interactive Shells}, for a description of what makes |
cce855bc JA |
562 | a shell interactive. |
563 | ||
564 | @node Shell Commands | |
565 | @section Shell Commands | |
566 | @cindex commands, shell | |
bb70624e JA |
567 | |
568 | A simple shell command such as @code{echo a b c} consists of the command | |
569 | itself followed by arguments, separated by spaces. | |
570 | ||
571 | More complex shell commands are composed of simple commands arranged together | |
572 | in a variety of ways: in a pipeline in which the output of one command | |
573 | becomes the input of a second, in a loop or conditional construct, or in | |
574 | some other grouping. | |
575 | ||
cce855bc JA |
576 | @menu |
577 | * Simple Commands:: The most common type of command. | |
578 | * Pipelines:: Connecting the input and output of several | |
579 | commands. | |
580 | * Lists:: How to execute commands sequentially. | |
d3a24ed2 | 581 | * Compound Commands:: Shell commands for control flow. |
09767ff0 | 582 | * Coprocesses:: Two-way communication between commands. |
6faad625 | 583 | * GNU Parallel:: Running commands in parallel. |
cce855bc | 584 | @end menu |
ccc6cda3 JA |
585 | |
586 | @node Simple Commands | |
cce855bc | 587 | @subsection Simple Commands |
ccc6cda3 JA |
588 | @cindex commands, simple |
589 | ||
cce855bc | 590 | A simple command is the kind of command encountered most often. |
ccc6cda3 | 591 | It's just a sequence of words separated by @code{blank}s, terminated |
cce855bc | 592 | by one of the shell's control operators (@pxref{Definitions}). The |
bb70624e JA |
593 | first word generally specifies a command to be executed, with the |
594 | rest of the words being that command's arguments. | |
ccc6cda3 JA |
595 | |
596 | The return status (@pxref{Exit Status}) of a simple command is | |
597 | its exit status as provided | |
bb70624e JA |
598 | by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if |
599 | the command was terminated by signal @var{n}. | |
ccc6cda3 JA |
600 | |
601 | @node Pipelines | |
cce855bc | 602 | @subsection Pipelines |
ccc6cda3 JA |
603 | @cindex pipeline |
604 | @cindex commands, pipelines | |
605 | ||
8943768b CR |
606 | A @code{pipeline} is a sequence of simple commands separated by one of |
607 | the control operators @samp{|} or @samp{|&}. | |
ccc6cda3 JA |
608 | |
609 | @rwindex time | |
610 | @rwindex ! | |
611 | @cindex command timing | |
612 | The format for a pipeline is | |
613 | @example | |
b28ff8c9 | 614 | [time [-p]] [!] @var{command1} [ | or |& @var{command2} ] @dots{} |
ccc6cda3 JA |
615 | @end example |
616 | ||
617 | @noindent | |
f73dda09 JA |
618 | The output of each command in the pipeline is connected via a pipe |
619 | to the input of the next command. | |
8943768b CR |
620 | That is, each command reads the previous command's output. This |
621 | connection is performed before any redirections specified by the | |
622 | command. | |
623 | ||
c920c360 CR |
624 | If @samp{|&} is used, @var{command1}'s standard output and standard error |
625 | are connected to | |
626 | @var{command2}'s standard input through the pipe; | |
627 | it is shorthand for @code{2>&1 |}. | |
628 | This implicit redirection of the standard error is | |
8943768b | 629 | performed after any redirections specified by the command. |
ccc6cda3 JA |
630 | |
631 | The reserved word @code{time} causes timing statistics | |
632 | to be printed for the pipeline once it finishes. | |
cce855bc JA |
633 | The statistics currently consist of elapsed (wall-clock) time and |
634 | user and system time consumed by the command's execution. | |
28ef6c31 | 635 | The @option{-p} option changes the output format to that specified |
bb70624e | 636 | by @sc{posix}. |
7d92f73f CR |
637 | When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), |
638 | it does not recognize @code{time} as a reserved word if the next | |
639 | token begins with a @samp{-}. | |
28ef6c31 | 640 | The @env{TIMEFORMAT} variable may be set to a format string that |
ccc6cda3 JA |
641 | specifies how the timing information should be displayed. |
642 | @xref{Bash Variables}, for a description of the available formats. | |
cce855bc JA |
643 | The use of @code{time} as a reserved word permits the timing of |
644 | shell builtins, shell functions, and pipelines. An external | |
645 | @code{time} command cannot time these easily. | |
646 | ||
6faad625 CR |
647 | When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), @code{time} |
648 | may be followed by a newline. In this case, the shell displays the | |
649 | total user and system time consumed by the shell and its children. | |
650 | The @env{TIMEFORMAT} variable may be used to specify the format of | |
651 | the time information. | |
652 | ||
cce855bc JA |
653 | If the pipeline is not executed asynchronously (@pxref{Lists}), the |
654 | shell waits for all commands in the pipeline to complete. | |
ccc6cda3 | 655 | |
cce855bc JA |
656 | Each command in a pipeline is executed in its own subshell |
657 | (@pxref{Command Execution Environment}). The exit | |
ccc6cda3 | 658 | status of a pipeline is the exit status of the last command in the |
d3a24ed2 CR |
659 | pipeline, unless the @code{pipefail} option is enabled |
660 | (@pxref{The Set Builtin}). | |
661 | If @code{pipefail} is enabled, the pipeline's return status is the | |
662 | value of the last (rightmost) command to exit with a non-zero status, | |
663 | or zero if all commands exit successfully. | |
664 | If the reserved word @samp{!} precedes the pipeline, the | |
665 | exit status is the logical negation of the exit status as described | |
666 | above. | |
667 | The shell waits for all commands in the pipeline to terminate before | |
668 | returning a value. | |
ccc6cda3 JA |
669 | |
670 | @node Lists | |
cce855bc | 671 | @subsection Lists of Commands |
ccc6cda3 JA |
672 | @cindex commands, lists |
673 | ||
674 | A @code{list} is a sequence of one or more pipelines separated by one | |
675 | of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||}, | |
676 | and optionally terminated by one of @samp{;}, @samp{&}, or a | |
677 | @code{newline}. | |
678 | ||
679 | Of these list operators, @samp{&&} and @samp{||} | |
680 | have equal precedence, followed by @samp{;} and @samp{&}, | |
681 | which have equal precedence. | |
682 | ||
7117c2d2 JA |
683 | A sequence of one or more newlines may appear in a @code{list} |
684 | to delimit commands, equivalent to a semicolon. | |
685 | ||
ccc6cda3 | 686 | If a command is terminated by the control operator @samp{&}, |
cce855bc JA |
687 | the shell executes the command asynchronously in a subshell. |
688 | This is known as executing the command in the @var{background}. | |
689 | The shell does not wait for the command to finish, and the return | |
690 | status is 0 (true). | |
bb70624e JA |
691 | When job control is not active (@pxref{Job Control}), |
692 | the standard input for asynchronous commands, in the absence of any | |
cce855bc JA |
693 | explicit redirections, is redirected from @code{/dev/null}. |
694 | ||
695 | Commands separated by a @samp{;} are executed sequentially; the shell | |
696 | waits for each command to terminate in turn. The return status is the | |
ccc6cda3 JA |
697 | exit status of the last command executed. |
698 | ||
6fbe7620 CR |
699 | @sc{and} and @sc{or} lists are sequences of one or more pipelines |
700 | separated by the control operators @samp{&&} and @samp{||}, | |
701 | respectively. @sc{and} and @sc{or} lists are executed with left | |
702 | associativity. | |
703 | ||
bb70624e | 704 | An @sc{and} list has the form |
ccc6cda3 | 705 | @example |
bb70624e | 706 | @var{command1} && @var{command2} |
ccc6cda3 JA |
707 | @end example |
708 | ||
709 | @noindent | |
bb70624e | 710 | @var{command2} is executed if, and only if, @var{command1} |
ccc6cda3 JA |
711 | returns an exit status of zero. |
712 | ||
bb70624e | 713 | An @sc{or} list has the form |
ccc6cda3 | 714 | @example |
bb70624e | 715 | @var{command1} || @var{command2} |
ccc6cda3 JA |
716 | @end example |
717 | ||
718 | @noindent | |
bb70624e | 719 | @var{command2} is executed if, and only if, @var{command1} |
ccc6cda3 JA |
720 | returns a non-zero exit status. |
721 | ||
722 | The return status of | |
bb70624e | 723 | @sc{and} and @sc{or} lists is the exit status of the last command |
ccc6cda3 JA |
724 | executed in the list. |
725 | ||
d3a24ed2 CR |
726 | @node Compound Commands |
727 | @subsection Compound Commands | |
728 | @cindex commands, compound | |
729 | ||
730 | @menu | |
731 | * Looping Constructs:: Shell commands for iterative action. | |
732 | * Conditional Constructs:: Shell commands for conditional execution. | |
733 | * Command Grouping:: Ways to group commands. | |
734 | @end menu | |
735 | ||
736 | Compound commands are the shell programming constructs. | |
737 | Each construct begins with a reserved word or control operator and is | |
738 | terminated by a corresponding reserved word or operator. | |
739 | Any redirections (@pxref{Redirections}) associated with a compound command | |
740 | apply to all commands within that compound command unless explicitly overridden. | |
741 | ||
f4f5e1c2 CR |
742 | In most cases a list of commands in a compound command's description may be |
743 | separated from the rest of the command by one or more newlines, and may be | |
744 | followed by a newline in place of a semicolon. | |
745 | ||
d3a24ed2 CR |
746 | Bash provides looping constructs, conditional commands, and mechanisms |
747 | to group commands and execute them as a unit. | |
748 | ||
ccc6cda3 | 749 | @node Looping Constructs |
d3a24ed2 | 750 | @subsubsection Looping Constructs |
ccc6cda3 JA |
751 | @cindex commands, looping |
752 | ||
ccc6cda3 JA |
753 | Bash supports the following looping constructs. |
754 | ||
bb70624e | 755 | Note that wherever a @samp{;} appears in the description of a |
cce855bc JA |
756 | command's syntax, it may be replaced with one or more newlines. |
757 | ||
ccc6cda3 JA |
758 | @table @code |
759 | @item until | |
760 | @rwindex until | |
761 | @rwindex do | |
762 | @rwindex done | |
763 | The syntax of the @code{until} command is: | |
b28ff8c9 | 764 | |
ccc6cda3 JA |
765 | @example |
766 | until @var{test-commands}; do @var{consequent-commands}; done | |
767 | @end example | |
b28ff8c9 | 768 | |
cce855bc | 769 | Execute @var{consequent-commands} as long as |
ccc6cda3 | 770 | @var{test-commands} has an exit status which is not zero. |
cce855bc JA |
771 | The return status is the exit status of the last command executed |
772 | in @var{consequent-commands}, or zero if none was executed. | |
ccc6cda3 JA |
773 | |
774 | @item while | |
775 | @rwindex while | |
776 | The syntax of the @code{while} command is: | |
b28ff8c9 | 777 | |
ccc6cda3 JA |
778 | @example |
779 | while @var{test-commands}; do @var{consequent-commands}; done | |
780 | @end example | |
781 | ||
cce855bc | 782 | Execute @var{consequent-commands} as long as |
ccc6cda3 | 783 | @var{test-commands} has an exit status of zero. |
cce855bc JA |
784 | The return status is the exit status of the last command executed |
785 | in @var{consequent-commands}, or zero if none was executed. | |
ccc6cda3 JA |
786 | |
787 | @item for | |
788 | @rwindex for | |
789 | The syntax of the @code{for} command is: | |
790 | ||
791 | @example | |
0d8616ff | 792 | for @var{name} [ [in [@var{words} @dots{}] ] ; ] do @var{commands}; done |
ccc6cda3 | 793 | @end example |
b28ff8c9 | 794 | |
cce855bc JA |
795 | Expand @var{words}, and execute @var{commands} once for each member |
796 | in the resultant list, with @var{name} bound to the current member. | |
bb70624e JA |
797 | If @samp{in @var{words}} is not present, the @code{for} command |
798 | executes the @var{commands} once for each positional parameter that is | |
799 | set, as if @samp{in "$@@"} had been specified | |
800 | (@pxref{Special Parameters}). | |
cce855bc JA |
801 | The return status is the exit status of the last command that executes. |
802 | If there are no items in the expansion of @var{words}, no commands are | |
803 | executed, and the return status is zero. | |
bb70624e JA |
804 | |
805 | An alternate form of the @code{for} command is also supported: | |
806 | ||
807 | @example | |
808 | for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done | |
809 | @end example | |
b28ff8c9 | 810 | |
bb70624e JA |
811 | First, the arithmetic expression @var{expr1} is evaluated according |
812 | to the rules described below (@pxref{Shell Arithmetic}). | |
813 | The arithmetic expression @var{expr2} is then evaluated repeatedly | |
814 | until it evaluates to zero. | |
815 | Each time @var{expr2} evaluates to a non-zero value, @var{commands} are | |
816 | executed and the arithmetic expression @var{expr3} is evaluated. | |
817 | If any expression is omitted, it behaves as if it evaluates to 1. | |
ee56a865 | 818 | The return value is the exit status of the last command in @var{commands} |
bb70624e | 819 | that is executed, or false if any of the expressions is invalid. |
ccc6cda3 JA |
820 | @end table |
821 | ||
822 | The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins}) | |
823 | may be used to control loop execution. | |
824 | ||
825 | @node Conditional Constructs | |
d3a24ed2 | 826 | @subsubsection Conditional Constructs |
ccc6cda3 JA |
827 | @cindex commands, conditional |
828 | ||
829 | @table @code | |
830 | @item if | |
831 | @rwindex if | |
832 | @rwindex then | |
833 | @rwindex else | |
834 | @rwindex elif | |
835 | @rwindex fi | |
836 | The syntax of the @code{if} command is: | |
837 | ||
838 | @example | |
839 | if @var{test-commands}; then | |
840 | @var{consequent-commands}; | |
841 | [elif @var{more-test-commands}; then | |
842 | @var{more-consequents};] | |
843 | [else @var{alternate-consequents};] | |
844 | fi | |
845 | @end example | |
846 | ||
cce855bc JA |
847 | The @var{test-commands} list is executed, and if its return status is zero, |
848 | the @var{consequent-commands} list is executed. | |
849 | If @var{test-commands} returns a non-zero status, each @code{elif} list | |
850 | is executed in turn, and if its exit status is zero, | |
ccc6cda3 JA |
851 | the corresponding @var{more-consequents} is executed and the |
852 | command completes. | |
853 | If @samp{else @var{alternate-consequents}} is present, and | |
854 | the final command in the final @code{if} or @code{elif} clause | |
cce855bc JA |
855 | has a non-zero exit status, then @var{alternate-consequents} is executed. |
856 | The return status is the exit status of the last command executed, or | |
857 | zero if no condition tested true. | |
ccc6cda3 JA |
858 | |
859 | @item case | |
860 | @rwindex case | |
861 | @rwindex in | |
862 | @rwindex esac | |
863 | The syntax of the @code{case} command is: | |
864 | ||
865 | @example | |
b28ff8c9 | 866 | case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac |
ccc6cda3 JA |
867 | @end example |
868 | ||
cce855bc JA |
869 | @code{case} will selectively execute the @var{command-list} corresponding to |
870 | the first @var{pattern} that matches @var{word}. | |
2206f89a | 871 | If the shell option @code{nocasematch} |
d3ad40de | 872 | (see the description of @code{shopt} in @ref{The Shopt Builtin}) |
2206f89a CR |
873 | is enabled, the match is performed without regard to the case |
874 | of alphabetic characters. | |
cce855bc JA |
875 | The @samp{|} is used to separate multiple patterns, and the @samp{)} |
876 | operator terminates a pattern list. | |
877 | A list of patterns and an associated command-list is known | |
8943768b CR |
878 | as a @var{clause}. |
879 | ||
1231ac47 | 880 | Each clause must be terminated with @samp{;;}, @samp{;&}, or @samp{;;&}. |
cce855bc JA |
881 | The @var{word} undergoes tilde expansion, parameter expansion, command |
882 | substitution, arithmetic expansion, and quote removal before matching is | |
883 | attempted. Each @var{pattern} undergoes tilde expansion, parameter | |
884 | expansion, command substitution, and arithmetic expansion. | |
885 | ||
886 | There may be an arbitrary number of @code{case} clauses, each terminated | |
8943768b CR |
887 | by a @samp{;;}, @samp{;&}, or @samp{;;&}. |
888 | The first pattern that matches determines the | |
cce855bc | 889 | command-list that is executed. |
ec860d76 CR |
890 | It's a common idiom to use @samp{*} as the final pattern to define the |
891 | default case, since that pattern will always match. | |
ccc6cda3 JA |
892 | |
893 | Here is an example using @code{case} in a script that could be used to | |
894 | describe one interesting feature of an animal: | |
895 | ||
896 | @example | |
897 | echo -n "Enter the name of an animal: " | |
898 | read ANIMAL | |
899 | echo -n "The $ANIMAL has " | |
900 | case $ANIMAL in | |
901 | horse | dog | cat) echo -n "four";; | |
902 | man | kangaroo ) echo -n "two";; | |
903 | *) echo -n "an unknown number of";; | |
904 | esac | |
905 | echo " legs." | |
906 | @end example | |
907 | ||
cce855bc | 908 | @noindent |
8943768b CR |
909 | |
910 | If the @samp{;;} operator is used, no subsequent matches are attempted after | |
911 | the first pattern match. | |
912 | Using @samp{;&} in place of @samp{;;} causes execution to continue with | |
913 | the @var{command-list} associated with the next clause, if any. | |
914 | Using @samp{;;&} in place of @samp{;;} causes the shell to test the patterns | |
915 | in the next clause, if any, and execute any associated @var{command-list} | |
916 | on a successful match. | |
917 | ||
cce855bc JA |
918 | The return status is zero if no @var{pattern} is matched. Otherwise, the |
919 | return status is the exit status of the @var{command-list} executed. | |
920 | ||
921 | @item select | |
922 | @rwindex select | |
923 | ||
924 | The @code{select} construct allows the easy generation of menus. | |
925 | It has almost the same syntax as the @code{for} command: | |
926 | ||
927 | @example | |
928 | select @var{name} [in @var{words} @dots{}]; do @var{commands}; done | |
929 | @end example | |
930 | ||
931 | The list of words following @code{in} is expanded, generating a list | |
932 | of items. The set of expanded words is printed on the standard | |
933 | error output stream, each preceded by a number. If the | |
934 | @samp{in @var{words}} is omitted, the positional parameters are printed, | |
d3ad40de | 935 | as if @samp{in "$@@"} had been specified. |
28ef6c31 | 936 | The @env{PS3} prompt is then displayed and a line is read from the |
cce855bc JA |
937 | standard input. |
938 | If the line consists of a number corresponding to one of the displayed | |
939 | words, then the value of @var{name} is set to that word. | |
940 | If the line is empty, the words and prompt are displayed again. | |
941 | If @code{EOF} is read, the @code{select} command completes. | |
942 | Any other value read causes @var{name} to be set to null. | |
28ef6c31 | 943 | The line read is saved in the variable @env{REPLY}. |
cce855bc JA |
944 | |
945 | The @var{commands} are executed after each selection until a | |
f73dda09 | 946 | @code{break} command is executed, at which |
cce855bc JA |
947 | point the @code{select} command completes. |
948 | ||
949 | Here is an example that allows the user to pick a filename from the | |
950 | current directory, and displays the name and index of the file | |
951 | selected. | |
952 | ||
953 | @example | |
954 | select fname in *; | |
955 | do | |
956 | echo you picked $fname \($REPLY\) | |
957 | break; | |
958 | done | |
959 | @end example | |
960 | ||
ccc6cda3 JA |
961 | @item ((@dots{})) |
962 | @example | |
963 | (( @var{expression} )) | |
964 | @end example | |
965 | ||
cce855bc JA |
966 | The arithmetic @var{expression} is evaluated according to the rules |
967 | described below (@pxref{Shell Arithmetic}). | |
ccc6cda3 JA |
968 | If the value of the expression is non-zero, the return status is 0; |
969 | otherwise the return status is 1. This is exactly equivalent to | |
970 | @example | |
971 | let "@var{expression}" | |
972 | @end example | |
cce855bc JA |
973 | @noindent |
974 | @xref{Bash Builtins}, for a full description of the @code{let} builtin. | |
975 | ||
976 | @item [[@dots{}]] | |
977 | @rwindex [[ | |
978 | @rwindex ]] | |
979 | @example | |
980 | [[ @var{expression} ]] | |
981 | @end example | |
982 | ||
983 | Return a status of 0 or 1 depending on the evaluation of | |
984 | the conditional expression @var{expression}. | |
985 | Expressions are composed of the primaries described below in | |
986 | @ref{Bash Conditional Expressions}. | |
987 | Word splitting and filename expansion are not performed on the words | |
df0e4bfe | 988 | between the @code{[[} and @code{]]}; tilde expansion, parameter and |
cce855bc JA |
989 | variable expansion, arithmetic expansion, command substitution, process |
990 | substitution, and quote removal are performed. | |
d3a24ed2 CR |
991 | Conditional operators such as @samp{-f} must be unquoted to be recognized |
992 | as primaries. | |
cce855bc | 993 | |
df0e4bfe | 994 | When used with @code{[[}, the @samp{<} and @samp{>} operators sort |
a8fd3f3e CR |
995 | lexicographically using the current locale. |
996 | ||
cce855bc JA |
997 | When the @samp{==} and @samp{!=} operators are used, the string to the |
998 | right of the operator is considered a pattern and matched according | |
999 | to the rules described below in @ref{Pattern Matching}. | |
df0e4bfe | 1000 | The @samp{=} operator is identical to @samp{==}. |
2206f89a | 1001 | If the shell option @code{nocasematch} |
d3ad40de | 1002 | (see the description of @code{shopt} in @ref{The Shopt Builtin}) |
2206f89a CR |
1003 | is enabled, the match is performed without regard to the case |
1004 | of alphabetic characters. | |
dc8fbaf9 CR |
1005 | The return value is 0 if the string matches (@samp{==}) or does not |
1006 | match (@samp{!=})the pattern, and 1 otherwise. | |
e192f341 CR |
1007 | Any part of the pattern may be quoted to force the quoted portion |
1008 | to be matched as a string. | |
cce855bc | 1009 | |
d3a24ed2 CR |
1010 | An additional binary operator, @samp{=~}, is available, with the same |
1011 | precedence as @samp{==} and @samp{!=}. | |
1012 | When it is used, the string to the right of the operator is considered | |
1013 | an extended regular expression and matched accordingly (as in @i{regex}3)). | |
1014 | The return value is 0 if the string matches | |
1015 | the pattern, and 1 otherwise. | |
1016 | If the regular expression is syntactically incorrect, the conditional | |
1017 | expression's return value is 2. | |
2206f89a | 1018 | If the shell option @code{nocasematch} |
d3ad40de | 1019 | (see the description of @code{shopt} in @ref{The Shopt Builtin}) |
d3a24ed2 CR |
1020 | is enabled, the match is performed without regard to the case |
1021 | of alphabetic characters. | |
e192f341 CR |
1022 | Any part of the pattern may be quoted to force the quoted portion |
1023 | to be matched as a string. | |
084c952b CR |
1024 | Bracket expressions in regular expressions must be treated carefully, |
1025 | since normal quoting characters lose their meanings between brackets. | |
1026 | If the pattern is stored in a shell variable, quoting the variable | |
1027 | expansion forces the entire pattern to be matched as a string. | |
d3a24ed2 CR |
1028 | Substrings matched by parenthesized subexpressions within the regular |
1029 | expression are saved in the array variable @code{BASH_REMATCH}. | |
1030 | The element of @code{BASH_REMATCH} with index 0 is the portion of the string | |
1031 | matching the entire regular expression. | |
1032 | The element of @code{BASH_REMATCH} with index @var{n} is the portion of the | |
1033 | string matching the @var{n}th parenthesized subexpression. | |
1034 | ||
e67d0029 CR |
1035 | For example, the following will match a line |
1036 | (stored in the shell variable @var{line}) | |
1037 | if there is a sequence of characters in the value consisting of | |
1038 | any number, including zero, of | |
1039 | space characters, zero or one instances of @samp{a}, then a @samp{b}: | |
1040 | @example | |
1041 | [[ $line =~ [[:space:]]*(a)?b ]] | |
1042 | @end example | |
1043 | ||
1044 | @noindent | |
1045 | That means values like @samp{aab} and @samp{ aaaaaab} will match, as | |
1046 | will a line containing a @samp{b} anywhere in its value. | |
1047 | ||
1048 | Storing the regular expression in a shell variable is often a useful | |
1049 | way to avoid problems with quoting characters that are special to the | |
1050 | shell. | |
1051 | It is sometimes difficult to specify a regular expression literally | |
1052 | without using quotes, or to keep track of the quoting used by regular | |
1053 | expressions while paying attention to the shell's quote removal. | |
1054 | Using a shell variable to store the pattern decreases these problems. | |
1055 | For example, the following is equivalent to the above: | |
1056 | @example | |
1057 | pattern='[[:space:]]*(a)?b' | |
1058 | [[ $line =~ $pattern ]] | |
1059 | @end example | |
1060 | ||
1061 | @noindent | |
1062 | If you want to match a character that's special to the regular expression | |
1063 | grammar, it has to be quoted to remove its special meaning. | |
1064 | This means that in the pattern @samp{xxx.txt}, the @samp{.} matches any | |
1065 | character in the string (its usual regular expression meaning), but in the | |
1066 | pattern @samp{"xxx.txt"} it can only match a literal @samp{.}. | |
1067 | Shell programmers should take special care with backslashes, since backslashes | |
1068 | are used both by the shell and regular expressions to remove the special | |
1069 | meaning from the following character. | |
1070 | The following two sets of commands are @emph{not} equivalent: | |
1071 | @example | |
1072 | pattern='\.' | |
1073 | ||
1074 | [[ . =~ $pattern ]] | |
1075 | [[ . =~ \. ]] | |
1076 | ||
1077 | [[ . =~ "$pattern" ]] | |
1078 | [[ . =~ '\.' ]] | |
1079 | @end example | |
1080 | ||
1081 | @noindent | |
1082 | The first two matches will succeed, but the second two will not, because | |
1083 | in the second two the backslash will be part of the pattern to be matched. | |
1084 | In the first two examples, the backslash removes the special meaning from | |
1085 | @samp{.}, so the literal @samp{.} matches. | |
1086 | If the string in the first examples were anything other than @samp{.}, say | |
1087 | @samp{a}, the pattern would not match, because the quoted @samp{.} in the | |
1088 | pattern loses its special meaning of matching any single character. | |
1089 | ||
cce855bc JA |
1090 | Expressions may be combined using the following operators, listed |
1091 | in decreasing order of precedence: | |
1092 | ||
1093 | @table @code | |
1094 | @item ( @var{expression} ) | |
1095 | Returns the value of @var{expression}. | |
1096 | This may be used to override the normal precedence of operators. | |
ccc6cda3 | 1097 | |
cce855bc JA |
1098 | @item ! @var{expression} |
1099 | True if @var{expression} is false. | |
1100 | ||
1101 | @item @var{expression1} && @var{expression2} | |
1102 | True if both @var{expression1} and @var{expression2} are true. | |
1103 | ||
1104 | @item @var{expression1} || @var{expression2} | |
1105 | True if either @var{expression1} or @var{expression2} is true. | |
ccc6cda3 | 1106 | @end table |
b28ff8c9 | 1107 | |
cce855bc | 1108 | @noindent |
45c0f7f8 | 1109 | The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the |
cce855bc JA |
1110 | value of @var{expression1} is sufficient to determine the return |
1111 | value of the entire conditional expression. | |
cce855bc | 1112 | @end table |
ccc6cda3 JA |
1113 | |
1114 | @node Command Grouping | |
d3a24ed2 | 1115 | @subsubsection Grouping Commands |
ccc6cda3 JA |
1116 | @cindex commands, grouping |
1117 | ||
1118 | Bash provides two ways to group a list of commands to be executed | |
1119 | as a unit. When commands are grouped, redirections may be applied | |
1120 | to the entire command list. For example, the output of all the | |
1121 | commands in the list may be redirected to a single stream. | |
1122 | ||
1123 | @table @code | |
1124 | @item () | |
1125 | @example | |
1126 | ( @var{list} ) | |
1127 | @end example | |
1128 | ||
1129 | Placing a list of commands between parentheses causes a subshell | |
d3a24ed2 CR |
1130 | environment to be created (@pxref{Command Execution Environment}), and each |
1131 | of the commands in @var{list} to be executed in that subshell. Since the | |
1132 | @var{list} is executed in a subshell, variable assignments do not remain in | |
1133 | effect after the subshell completes. | |
ccc6cda3 JA |
1134 | |
1135 | @item @{@} | |
1136 | @rwindex @{ | |
1137 | @rwindex @} | |
1138 | @example | |
1139 | @{ @var{list}; @} | |
1140 | @end example | |
1141 | ||
1142 | Placing a list of commands between curly braces causes the list to | |
1143 | be executed in the current shell context. No subshell is created. | |
cce855bc | 1144 | The semicolon (or newline) following @var{list} is required. |
ccc6cda3 JA |
1145 | @end table |
1146 | ||
1147 | In addition to the creation of a subshell, there is a subtle difference | |
1148 | between these two constructs due to historical reasons. The braces | |
1149 | are @code{reserved words}, so they must be separated from the @var{list} | |
641d8f00 CR |
1150 | by @code{blank}s or other shell metacharacters. |
1151 | The parentheses are @code{operators}, and are | |
ccc6cda3 | 1152 | recognized as separate tokens by the shell even if they are not separated |
d166f048 | 1153 | from the @var{list} by whitespace. |
ccc6cda3 JA |
1154 | |
1155 | The exit status of both of these constructs is the exit status of | |
1156 | @var{list}. | |
1157 | ||
09767ff0 CR |
1158 | @node Coprocesses |
1159 | @subsection Coprocesses | |
1160 | @cindex coprocess | |
1161 | ||
1162 | A @code{coprocess} is a shell command preceded by the @code{coproc} | |
1163 | reserved word. | |
1164 | A coprocess is executed asynchronously in a subshell, as if the command | |
1165 | had been terminated with the @samp{&} control operator, with a two-way pipe | |
1166 | established between the executing shell and the coprocess. | |
1167 | ||
1168 | The format for a coprocess is: | |
1169 | @example | |
b28ff8c9 | 1170 | coproc [@var{NAME}] @var{command} [@var{redirections}] |
09767ff0 CR |
1171 | @end example |
1172 | ||
1173 | @noindent | |
1174 | This creates a coprocess named @var{NAME}. | |
1175 | If @var{NAME} is not supplied, the default name is @var{COPROC}. | |
e141c35a CR |
1176 | @var{NAME} must not be supplied if @var{command} is a simple |
1177 | command (@pxref{Simple Commands}); otherwise, it is interpreted as | |
1178 | the first word of the simple command. | |
09767ff0 | 1179 | |
b28ff8c9 | 1180 | When the coprocess is executed, the shell creates an array variable |
09767ff0 | 1181 | (@pxref{Arrays}) |
b28ff8c9 | 1182 | named @env{NAME} in the context of the executing shell. |
09767ff0 CR |
1183 | The standard output of @var{command} |
1184 | is connected via a pipe to a file descriptor in the executing shell, | |
b28ff8c9 | 1185 | and that file descriptor is assigned to @env{NAME}[0]. |
09767ff0 CR |
1186 | The standard input of @var{command} |
1187 | is connected via a pipe to a file descriptor in the executing shell, | |
b28ff8c9 | 1188 | and that file descriptor is assigned to @env{NAME}[1]. |
09767ff0 CR |
1189 | This pipe is established before any redirections specified by the |
1190 | command (@pxref{Redirections}). | |
1191 | The file descriptors can be utilized as arguments to shell commands | |
1192 | and redirections using standard word expansions. | |
8360b906 | 1193 | The file descriptors are not available in subshells. |
09767ff0 | 1194 | |
dc60d4e0 | 1195 | The process ID of the shell spawned to execute the coprocess is |
b28ff8c9 | 1196 | available as the value of the variable @env{NAME}_PID. |
30915f17 CR |
1197 | The @code{wait} |
1198 | builtin command may be used to wait for the coprocess to terminate. | |
1199 | ||
09767ff0 CR |
1200 | The return status of a coprocess is the exit status of @var{command}. |
1201 | ||
6faad625 CR |
1202 | @node GNU Parallel |
1203 | @subsection GNU Parallel | |
1204 | ||
1205 | GNU Parallel, as its name suggests, can be used to build and run commands | |
1206 | in parallel. You may run the same command with different arguments, whether | |
1207 | they are filenames, usernames, hostnames, or lines read from files. | |
1208 | ||
1209 | For a complete description, refer to the GNU Parallel documentation. A few | |
1210 | examples should provide a brief introduction to its use. | |
1211 | ||
1212 | For example, it is easy to prefix each line in a text file with a specified | |
1213 | string: | |
1214 | @example | |
1215 | cat file | parallel -k echo prefix_string | |
1216 | @end example | |
220537f2 | 1217 | @noindent |
6faad625 CR |
1218 | The @option{-k} option is required to preserve the lines' order. |
1219 | ||
1220 | Similarly, you can append a specified string to each line in a text file: | |
1221 | @example | |
1222 | cat file | parallel -k echo @{@} append_string | |
1223 | @end example | |
1224 | ||
1225 | You can use Parallel to move files from the current directory when the | |
1226 | number of files is too large to process with one @code{mv} invocation: | |
1227 | @example | |
1228 | ls | parallel mv @{@} destdir | |
1229 | @end example | |
1230 | ||
1231 | As you can see, the @{@} is replaced with each line read from standard input. | |
1232 | This will run as many @code{mv} commands as there are files in the current | |
1233 | directory. You can emulate a parallel @code{xargs} by adding the @option{-X} | |
1234 | option: | |
1235 | @example | |
1236 | ls | parallel -X mv @{@} destdir | |
1237 | @end example | |
1238 | ||
1239 | GNU Parallel can replace certain common idioms that operate on lines read | |
1240 | from a file (in this case, filenames): | |
1241 | @example | |
1242 | for x in $(cat list); do | |
1243 | do-something1 $x config-$x | |
1244 | do-something2 < $x | |
1245 | done | process-output | |
1246 | @end example | |
1247 | ||
220537f2 | 1248 | @noindent |
6faad625 CR |
1249 | with a more compact syntax reminiscent of lambdas: |
1250 | @example | |
1251 | cat list | parallel "do-something1 @{@} config-@{@} ; do-something2 < @{@}" | process-output | |
1252 | @end example | |
1253 | ||
1254 | Parallel provides a built-in mechanism to remove filename extensions, which | |
1255 | lends itself to batch file transformations or renaming: | |
1256 | @example | |
1257 | ls *.gz | parallel -j+0 "zcat @{@} | bzip2 >@{.@}.bz2 && rm @{@}" | |
1258 | @end example | |
220537f2 | 1259 | @noindent |
6faad625 CR |
1260 | This will recompress all files in the current directory with names ending |
1261 | in .gz using bzip2, running one job per CPU (-j+0) in parallel. | |
1262 | ||
1263 | If a command generates output, you may want to preserve the input order in | |
1264 | the output. For instance, the following command | |
1265 | @example | |
1266 | @{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel traceroute | |
1267 | @end example | |
220537f2 | 1268 | @noindent |
6faad625 CR |
1269 | will display as output the traceroute invocation that finishes first. Using |
1270 | the @option{-k} option, as we saw above | |
1271 | @example | |
1272 | @{ echo foss.org.my ; echo debian.org; echo freenetproject.org; @} | parallel -k traceroute | |
1273 | @end example | |
220537f2 | 1274 | @noindent |
6faad625 CR |
1275 | will ensure that the output of @code{traceroute foss.org.my} is displayed first. |
1276 | ||
ccc6cda3 JA |
1277 | @node Shell Functions |
1278 | @section Shell Functions | |
1279 | @cindex shell function | |
1280 | @cindex functions, shell | |
1281 | ||
1282 | Shell functions are a way to group commands for later execution | |
1283 | using a single name for the group. They are executed just like | |
bb70624e JA |
1284 | a "regular" command. |
1285 | When the name of a shell function is used as a simple command name, | |
1286 | the list of commands associated with that function name is executed. | |
1287 | Shell functions are executed in the current | |
ccc6cda3 JA |
1288 | shell context; no new process is created to interpret them. |
1289 | ||
1290 | Functions are declared using this syntax: | |
1291 | @rwindex function | |
1292 | @example | |
b28ff8c9 CR |
1293 | @var{name} () @var{compound-command} [ @var{redirections} ] |
1294 | @end example | |
1295 | ||
1296 | or | |
1297 | ||
1298 | @example | |
1299 | function @var{name} [()] @var{compound-command} [ @var{redirections} ] | |
ccc6cda3 JA |
1300 | @end example |
1301 | ||
1302 | This defines a shell function named @var{name}. The reserved | |
cce855bc JA |
1303 | word @code{function} is optional. |
1304 | If the @code{function} reserved | |
1305 | word is supplied, the parentheses are optional. | |
d3a24ed2 CR |
1306 | The @var{body} of the function is the compound command |
1307 | @var{compound-command} (@pxref{Compound Commands}). | |
1308 | That command is usually a @var{list} enclosed between @{ and @}, but | |
1309 | may be any compound command listed above. | |
1310 | @var{compound-command} is executed whenever @var{name} is specified as the | |
1311 | name of a command. | |
b28ff8c9 CR |
1312 | When the shell is in @sc{posix} mode (@pxref{Bash POSIX Mode}), |
1313 | @var{name} may not be the same as one of the special builtins | |
1314 | (@pxref{Special Builtins}). | |
d3a24ed2 CR |
1315 | Any redirections (@pxref{Redirections}) associated with the shell function |
1316 | are performed when the function is executed. | |
1317 | ||
11a6f9a9 CR |
1318 | A function definition may be deleted using the @option{-f} option to the |
1319 | @code{unset} builtin (@pxref{Bourne Shell Builtins}). | |
1320 | ||
d3a24ed2 CR |
1321 | The exit status of a function definition is zero unless a syntax error |
1322 | occurs or a readonly function with the same name already exists. | |
1323 | When executed, the exit status of a function is the exit status of the | |
1324 | last command executed in the body. | |
1325 | ||
1326 | Note that for historical reasons, in the most common usage the curly braces | |
1327 | that surround the body of the function must be separated from the body by | |
bb70624e JA |
1328 | @code{blank}s or newlines. |
1329 | This is because the braces are reserved words and are only recognized | |
641d8f00 CR |
1330 | as such when they are separated from the command list |
1331 | by whitespace or another shell metacharacter. | |
d3a24ed2 CR |
1332 | Also, when using the braces, the @var{list} must be terminated by a semicolon, |
1333 | a @samp{&}, or a newline. | |
bb70624e | 1334 | |
ccc6cda3 JA |
1335 | When a function is executed, the arguments to the |
1336 | function become the positional parameters | |
1337 | during its execution (@pxref{Positional Parameters}). | |
d166f048 JA |
1338 | The special parameter @samp{#} that expands to the number of |
1339 | positional parameters is updated to reflect the change. | |
de8913bd | 1340 | Special parameter @code{0} is unchanged. |
d3a24ed2 CR |
1341 | The first element of the @env{FUNCNAME} variable is set to the |
1342 | name of the function while the function is executing. | |
4301bca7 | 1343 | |
d3a24ed2 CR |
1344 | All other aspects of the shell execution |
1345 | environment are identical between a function and its caller | |
4301bca7 CR |
1346 | with these exceptions: |
1347 | the @env{DEBUG} and @env{RETURN} traps | |
76a8d78d | 1348 | are not inherited unless the function has been given the |
d3a24ed2 CR |
1349 | @code{trace} attribute using the @code{declare} builtin or |
1350 | the @code{-o functrace} option has been enabled with | |
1351 | the @code{set} builtin, | |
4301bca7 CR |
1352 | (in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps), |
1353 | and the @env{ERR} trap is not inherited unless the @code{-o errtrace} | |
1354 | shell option has been enabled. | |
d3a24ed2 CR |
1355 | @xref{Bourne Shell Builtins}, for the description of the |
1356 | @code{trap} builtin. | |
ccc6cda3 | 1357 | |
6faad625 CR |
1358 | The @env{FUNCNEST} variable, if set to a numeric value greater |
1359 | than 0, defines a maximum function nesting level. Function | |
1360 | invocations that exceed the limit cause the entire command to | |
1361 | abort. | |
1362 | ||
ccc6cda3 JA |
1363 | If the builtin command @code{return} |
1364 | is executed in a function, the function completes and | |
1365 | execution resumes with the next command after the function | |
d3a24ed2 CR |
1366 | call. |
1367 | Any command associated with the @code{RETURN} trap is executed | |
1368 | before execution resumes. | |
1369 | When a function completes, the values of the | |
ccc6cda3 | 1370 | positional parameters and the special parameter @samp{#} |
cce855bc | 1371 | are restored to the values they had prior to the function's |
ccc6cda3 | 1372 | execution. If a numeric argument is given to @code{return}, |
f73dda09 | 1373 | that is the function's return status; otherwise the function's |
cce855bc JA |
1374 | return status is the exit status of the last command executed |
1375 | before the @code{return}. | |
ccc6cda3 JA |
1376 | |
1377 | Variables local to the function may be declared with the | |
1378 | @code{local} builtin. These variables are visible only to | |
1379 | the function and the commands it invokes. | |
1380 | ||
d3a24ed2 | 1381 | Function names and definitions may be listed with the |
b28ff8c9 CR |
1382 | @option{-f} option to the @code{declare} (@code{typeset}) |
1383 | builtin command (@pxref{Bash Builtins}). | |
d3a24ed2 CR |
1384 | The @option{-F} option to @code{declare} or @code{typeset} |
1385 | will list the function names only | |
1386 | (and optionally the source file and line number, if the @code{extdebug} | |
1387 | shell option is enabled). | |
1388 | Functions may be exported so that subshells | |
1389 | automatically have them defined with the | |
1390 | @option{-f} option to the @code{export} builtin | |
1391 | (@pxref{Bourne Shell Builtins}). | |
1392 | Note that shell functions and variables with the same name may result | |
1393 | in multiple identically-named entries in the environment passed to the | |
1394 | shell's children. | |
1395 | Care should be taken in cases where this may cause a problem. | |
1396 | ||
7d92f73f CR |
1397 | Functions may be recursive. |
1398 | The @code{FUNCNEST} variable may be used to limit the depth of the | |
1399 | function call stack and restrict the number of function invocations. | |
1400 | By default, no limit is placed on the number of recursive calls. | |
ccc6cda3 JA |
1401 | |
1402 | @node Shell Parameters | |
1403 | @section Shell Parameters | |
1404 | @cindex parameters | |
1405 | @cindex variable, shell | |
1406 | @cindex shell variable | |
1407 | ||
1408 | @menu | |
1409 | * Positional Parameters:: The shell's command-line arguments. | |
d3a24ed2 | 1410 | * Special Parameters:: Parameters denoted by special characters. |
ccc6cda3 JA |
1411 | @end menu |
1412 | ||
1413 | A @var{parameter} is an entity that stores values. | |
1414 | It can be a @code{name}, a number, or one of the special characters | |
1415 | listed below. | |
d3a24ed2 | 1416 | A @var{variable} is a parameter denoted by a @code{name}. |
f73dda09 JA |
1417 | A variable has a @var{value} and zero or more @var{attributes}. |
1418 | Attributes are assigned using the @code{declare} builtin command | |
1419 | (see the description of the @code{declare} builtin in @ref{Bash Builtins}). | |
ccc6cda3 JA |
1420 | |
1421 | A parameter is set if it has been assigned a value. The null string is | |
1422 | a valid value. Once a variable is set, it may be unset only by using | |
1423 | the @code{unset} builtin command. | |
1424 | ||
1425 | A variable may be assigned to by a statement of the form | |
1426 | @example | |
1427 | @var{name}=[@var{value}] | |
1428 | @end example | |
1429 | @noindent | |
1430 | If @var{value} | |
1431 | is not given, the variable is assigned the null string. All | |
1432 | @var{value}s undergo tilde expansion, parameter and variable expansion, | |
1433 | command substitution, arithmetic expansion, and quote | |
cce855bc | 1434 | removal (detailed below). If the variable has its @code{integer} |
f73dda09 | 1435 | attribute set, then @var{value} |
d3a24ed2 | 1436 | is evaluated as an arithmetic expression even if the @code{$((@dots{}))} |
cce855bc | 1437 | expansion is not used (@pxref{Arithmetic Expansion}). |
ccc6cda3 JA |
1438 | Word splitting is not performed, with the exception |
1439 | of @code{"$@@"} as explained below. | |
1440 | Filename expansion is not performed. | |
f73dda09 | 1441 | Assignment statements may also appear as arguments to the |
5e13499c | 1442 | @code{alias}, |
f73dda09 JA |
1443 | @code{declare}, @code{typeset}, @code{export}, @code{readonly}, |
1444 | and @code{local} builtin commands. | |
19baff85 CR |
1445 | When in @sc{posix} mode (@pxref{Bash POSIX Mode}), these builtins may appear |
1446 | in a command after one or more instances of the @code{command} builtin | |
1447 | and retain these assignment statement properties. | |
ccc6cda3 | 1448 | |
d11b8b46 CR |
1449 | In the context where an assignment statement is assigning a value |
1450 | to a shell variable or array index (@pxref{Arrays}), the @samp{+=} | |
1451 | operator can be used to | |
1452 | append to or add to the variable's previous value. | |
dc60d4e0 | 1453 | When @samp{+=} is applied to a variable for which the @var{integer} attribute |
d11b8b46 CR |
1454 | has been set, @var{value} is evaluated as an arithmetic expression and |
1455 | added to the variable's current value, which is also evaluated. | |
1456 | When @samp{+=} is applied to an array variable using compound assignment | |
1457 | (@pxref{Arrays}), the | |
1458 | variable's value is not unset (as it is when using @samp{=}), and new | |
1459 | values are appended to the array beginning at one greater than the array's | |
fdf670ea CR |
1460 | maximum index (for indexed arrays), or added as additional key-value pairs |
1461 | in an associative array. | |
d11b8b46 CR |
1462 | When applied to a string-valued variable, @var{value} is expanded and |
1463 | appended to the variable's value. | |
1464 | ||
87c1f4ec CR |
1465 | A variable can be assigned the @var{nameref} attribute using the |
1466 | @option{-n} option to the \fBdeclare\fP or \fBlocal\fP builtin commands | |
1467 | (@pxref{Bash Builtins}) | |
1468 | to create a @var{nameref}, or a reference to another variable. | |
1469 | This allows variables to be manipulated indirectly. | |
1470 | Whenever the nameref variable is referenced or assigned to, the operation | |
1471 | is actually performed on the variable specified by the nameref variable's | |
1472 | value. | |
1473 | A nameref is commonly used within shell functions to refer to a variable | |
1474 | whose name is passed as an argument to the function. | |
1475 | For instance, if a variable name is passed to a shell function as its first | |
1476 | argument, running | |
1477 | @example | |
1478 | declare -n ref=$1 | |
1479 | @end example | |
1480 | @noindent | |
1481 | inside the function creates a nameref variable @var{ref} whose value is | |
1482 | the variable name passed as the first argument. | |
1483 | References and assignments to @var{ref} are treated as references and | |
1484 | assignments to the variable whose name was passed as @code{$1}. | |
1485 | ||
1486 | If the control variable in a @code{for} loop has the nameref attribute, | |
1487 | the list of words can be a list of shell variables, and a name reference | |
1488 | will be established for each word in the list, in turn, when the loop is | |
1489 | executed. | |
1490 | Array variables cannot be given the @option{-n} attribute. | |
1491 | However, nameref variables can reference array variables and subscripted | |
1492 | array variables. | |
1493 | Namerefs can be unset using the @option{-n} option to the @code{unset} builtin | |
1494 | (@pxref{Bourne Shell Builtins}). | |
1495 | Otherwise, if @code{unset} is executed with the name of a nameref variable | |
1496 | as an argument, the variable referenced by the nameref variable will be unset. | |
1497 | ||
ccc6cda3 JA |
1498 | @node Positional Parameters |
1499 | @subsection Positional Parameters | |
1500 | @cindex parameters, positional | |
1501 | ||
cce855bc | 1502 | A @var{positional parameter} is a parameter denoted by one or more |
ccc6cda3 JA |
1503 | digits, other than the single digit @code{0}. Positional parameters are |
1504 | assigned from the shell's arguments when it is invoked, | |
cce855bc | 1505 | and may be reassigned using the @code{set} builtin command. |
bb70624e JA |
1506 | Positional parameter @code{N} may be referenced as @code{$@{N@}}, or |
1507 | as @code{$N} when @code{N} consists of a single digit. | |
1508 | Positional parameters may not be assigned to with assignment statements. | |
1509 | The @code{set} and @code{shift} builtins are used to set and | |
1510 | unset them (@pxref{Shell Builtin Commands}). | |
1511 | The positional parameters are | |
ccc6cda3 JA |
1512 | temporarily replaced when a shell function is executed |
1513 | (@pxref{Shell Functions}). | |
1514 | ||
1515 | When a positional parameter consisting of more than a single | |
1516 | digit is expanded, it must be enclosed in braces. | |
1517 | ||
1518 | @node Special Parameters | |
1519 | @subsection Special Parameters | |
1520 | @cindex parameters, special | |
1521 | ||
1522 | The shell treats several parameters specially. These parameters may | |
1523 | only be referenced; assignment to them is not allowed. | |
1524 | ||
1525 | @vtable @code | |
1526 | ||
1527 | @item * | |
1528 | Expands to the positional parameters, starting from one. When the | |
1529 | expansion occurs within double quotes, it expands to a single word | |
1530 | with the value of each parameter separated by the first character | |
28ef6c31 | 1531 | of the @env{IFS} |
ccc6cda3 JA |
1532 | special variable. That is, @code{"$*"} is equivalent |
1533 | to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c} | |
1534 | is the first character of the value of the @code{IFS} | |
d166f048 | 1535 | variable. |
28ef6c31 JA |
1536 | If @env{IFS} is unset, the parameters are separated by spaces. |
1537 | If @env{IFS} is null, the parameters are joined without intervening | |
d166f048 JA |
1538 | separators. |
1539 | ||
ccc6cda3 JA |
1540 | |
1541 | @item @@ | |
1542 | Expands to the positional parameters, starting from one. When the | |
cce855bc | 1543 | expansion occurs within double quotes, each parameter expands to a |
ccc6cda3 JA |
1544 | separate word. That is, @code{"$@@"} is equivalent to |
1545 | @code{"$1" "$2" @dots{}}. | |
be7d8f2d CR |
1546 | If the double-quoted expansion occurs within a word, the expansion of |
1547 | the first parameter is joined with the beginning part of the original | |
1548 | word, and the expansion of the last parameter is joined with the last | |
1549 | part of the original word. | |
ccc6cda3 JA |
1550 | When there are no positional parameters, @code{"$@@"} and |
1551 | @code{$@@} | |
1552 | expand to nothing (i.e., they are removed). | |
1553 | ||
1554 | @item # | |
1555 | Expands to the number of positional parameters in decimal. | |
1556 | ||
1557 | @item ? | |
1558 | Expands to the exit status of the most recently executed foreground | |
1559 | pipeline. | |
1560 | ||
1561 | @item - | |
bb70624e JA |
1562 | (A hyphen.) Expands to the current option flags as specified upon |
1563 | invocation, by the @code{set} | |
ccc6cda3 | 1564 | builtin command, or those set by the shell itself |
28ef6c31 | 1565 | (such as the @option{-i} option). |
ccc6cda3 JA |
1566 | |
1567 | @item $ | |
bb70624e JA |
1568 | Expands to the process @sc{id} of the shell. In a @code{()} subshell, it |
1569 | expands to the process @sc{id} of the invoking shell, not the subshell. | |
ccc6cda3 JA |
1570 | |
1571 | @item ! | |
bb70624e | 1572 | Expands to the process @sc{id} of the most recently executed background |
ccc6cda3 JA |
1573 | (asynchronous) command. |
1574 | ||
1575 | @item 0 | |
1576 | Expands to the name of the shell or shell script. This is set at | |
cce855bc JA |
1577 | shell initialization. If Bash is invoked with a file of commands |
1578 | (@pxref{Shell Scripts}), @code{$0} is set to the name of that file. | |
28ef6c31 | 1579 | If Bash is started with the @option{-c} option (@pxref{Invoking Bash}), |
cce855bc | 1580 | then @code{$0} is set to the first argument after the string to be |
ccc6cda3 JA |
1581 | executed, if one is present. Otherwise, it is set |
1582 | to the filename used to invoke Bash, as given by argument zero. | |
1583 | ||
1584 | @item _ | |
bb70624e | 1585 | (An underscore.) |
cc87ba64 CR |
1586 | At shell startup, set to the absolute pathname used to invoke the |
1587 | shell or shell script being executed as passed in the environment | |
1588 | or argument list. | |
ccc6cda3 JA |
1589 | Subsequently, expands to the last argument to the previous command, |
1590 | after expansion. | |
cc87ba64 CR |
1591 | Also set to the full pathname used to invoke each command executed |
1592 | and placed in the environment exported to that command. | |
ccc6cda3 JA |
1593 | When checking mail, this parameter holds the name of the mail file. |
1594 | @end vtable | |
1595 | ||
1596 | @node Shell Expansions | |
1597 | @section Shell Expansions | |
1598 | @cindex expansion | |
1599 | ||
1600 | Expansion is performed on the command line after it has been split into | |
1601 | @code{token}s. There are seven kinds of expansion performed: | |
b28ff8c9 | 1602 | |
ccc6cda3 JA |
1603 | @itemize @bullet |
1604 | @item brace expansion | |
1605 | @item tilde expansion | |
1606 | @item parameter and variable expansion | |
1607 | @item command substitution | |
1608 | @item arithmetic expansion | |
1609 | @item word splitting | |
1610 | @item filename expansion | |
1611 | @end itemize | |
1612 | ||
1613 | @menu | |
cce855bc JA |
1614 | * Brace Expansion:: Expansion of expressions within braces. |
1615 | * Tilde Expansion:: Expansion of the ~ character. | |
ccc6cda3 JA |
1616 | * Shell Parameter Expansion:: How Bash expands variables to their values. |
1617 | * Command Substitution:: Using the output of a command as an argument. | |
cce855bc | 1618 | * Arithmetic Expansion:: How to use arithmetic in shell expansions. |
ccc6cda3 JA |
1619 | * Process Substitution:: A way to write and read to and from a |
1620 | command. | |
1621 | * Word Splitting:: How the results of expansion are split into separate | |
1622 | arguments. | |
1623 | * Filename Expansion:: A shorthand for specifying filenames matching patterns. | |
1624 | * Quote Removal:: How and when quote characters are removed from | |
1625 | words. | |
1626 | @end menu | |
1627 | ||
ccc6cda3 JA |
1628 | The order of expansions is: brace expansion, tilde expansion, |
1629 | parameter, variable, and arithmetic expansion and | |
1630 | command substitution | |
1631 | (done in a left-to-right fashion), word splitting, and filename | |
1632 | expansion. | |
1633 | ||
1634 | On systems that can support it, there is an additional expansion | |
1635 | available: @var{process substitution}. This is performed at the | |
cce855bc | 1636 | same time as parameter, variable, and arithmetic expansion and |
ccc6cda3 JA |
1637 | command substitution. |
1638 | ||
1639 | Only brace expansion, word splitting, and filename expansion | |
1640 | can change the number of words of the expansion; other expansions | |
1641 | expand a single word to a single word. | |
1642 | The only exceptions to this are the expansions of | |
d166f048 | 1643 | @code{"$@@"} (@pxref{Special Parameters}) and @code{"$@{@var{name}[@@]@}"} |
ccc6cda3 JA |
1644 | (@pxref{Arrays}). |
1645 | ||
1646 | After all expansions, @code{quote removal} (@pxref{Quote Removal}) | |
1647 | is performed. | |
1648 | ||
cce855bc JA |
1649 | @node Brace Expansion |
1650 | @subsection Brace Expansion | |
1651 | @cindex brace expansion | |
1652 | @cindex expansion, brace | |
1653 | ||
bb70624e JA |
1654 | Brace expansion is a mechanism by which arbitrary strings may be generated. |
1655 | This mechanism is similar to | |
cce855bc | 1656 | @var{filename expansion} (@pxref{Filename Expansion}), |
b28ff8c9 | 1657 | but the filenames generated need not exist. |
bb70624e | 1658 | Patterns to be brace expanded take the form of an optional @var{preamble}, |
f6da9f85 | 1659 | followed by either a series of comma-separated strings or a sequence expression |
d3a24ed2 | 1660 | between a pair of braces, |
bb70624e JA |
1661 | followed by an optional @var{postscript}. |
1662 | The preamble is prefixed to each string contained within the braces, and | |
1663 | the postscript is then appended to each resulting string, expanding left | |
1664 | to right. | |
1665 | ||
1666 | Brace expansions may be nested. | |
1667 | The results of each expanded string are not sorted; left to right order | |
1668 | is preserved. | |
cce855bc JA |
1669 | For example, |
1670 | @example | |
1671 | bash$ echo a@{d,c,b@}e | |
1672 | ade ace abe | |
1673 | @end example | |
1674 | ||
d0ca3503 | 1675 | A sequence expression takes the form @code{@{@var{x}..@var{y}[..@var{incr}]@}}, |
8943768b CR |
1676 | where @var{x} and @var{y} are either integers or single characters, |
1677 | and @var{incr}, an optional increment, is an integer. | |
d3a24ed2 CR |
1678 | When integers are supplied, the expression expands to each number between |
1679 | @var{x} and @var{y}, inclusive. | |
8943768b CR |
1680 | Supplied integers may be prefixed with @samp{0} to force each term to have the |
1681 | same width. When either @var{x} or @var{y} begins with a zero, the shell | |
1682 | attempts to force all generated terms to contain the same number of digits, | |
1683 | zero-padding where necessary. | |
d3a24ed2 CR |
1684 | When characters are supplied, the expression expands to each character |
1685 | lexicographically between @var{x} and @var{y}, inclusive. Note that | |
1686 | both @var{x} and @var{y} must be of the same type. | |
8943768b CR |
1687 | When the increment is supplied, it is used as the difference between |
1688 | each term. The default increment is 1 or -1 as appropriate. | |
d3a24ed2 | 1689 | |
cce855bc JA |
1690 | Brace expansion is performed before any other expansions, |
1691 | and any characters special to other expansions are preserved | |
1692 | in the result. It is strictly textual. Bash | |
1693 | does not apply any syntactic interpretation to the context of the | |
1694 | expansion or the text between the braces. | |
bb70624e JA |
1695 | To avoid conflicts with parameter expansion, the string @samp{$@{} |
1696 | is not considered eligible for brace expansion. | |
cce855bc JA |
1697 | |
1698 | A correctly-formed brace expansion must contain unquoted opening | |
d3a24ed2 CR |
1699 | and closing braces, and at least one unquoted comma or a valid |
1700 | sequence expression. | |
cce855bc JA |
1701 | Any incorrectly formed brace expansion is left unchanged. |
1702 | ||
d3a24ed2 CR |
1703 | A @{ or @samp{,} may be quoted with a backslash to prevent its |
1704 | being considered part of a brace expression. | |
1705 | To avoid conflicts with parameter expansion, the string @samp{$@{} | |
1706 | is not considered eligible for brace expansion. | |
1707 | ||
cce855bc JA |
1708 | This construct is typically used as shorthand when the common |
1709 | prefix of the strings to be generated is longer than in the | |
1710 | above example: | |
1711 | @example | |
1712 | mkdir /usr/local/src/bash/@{old,new,dist,bugs@} | |
1713 | @end example | |
1714 | or | |
1715 | @example | |
1716 | chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@} | |
1717 | @end example | |
1718 | ||
1719 | @node Tilde Expansion | |
1720 | @subsection Tilde Expansion | |
1721 | @cindex tilde expansion | |
1722 | @cindex expansion, tilde | |
1723 | ||
1724 | If a word begins with an unquoted tilde character (@samp{~}), all of the | |
1725 | characters up to the first unquoted slash (or all characters, | |
1726 | if there is no unquoted slash) are considered a @var{tilde-prefix}. | |
1727 | If none of the characters in the tilde-prefix are quoted, the | |
1728 | characters in the tilde-prefix following the tilde are treated as a | |
1729 | possible @var{login name}. | |
1730 | If this login name is the null string, the tilde is replaced with the | |
28ef6c31 JA |
1731 | value of the @env{HOME} shell variable. |
1732 | If @env{HOME} is unset, the home directory of the user executing the | |
cce855bc JA |
1733 | shell is substituted instead. |
1734 | Otherwise, the tilde-prefix is replaced with the home directory | |
1735 | associated with the specified login name. | |
1736 | ||
1737 | If the tilde-prefix is @samp{~+}, the value of | |
28ef6c31 | 1738 | the shell variable @env{PWD} replaces the tilde-prefix. |
cce855bc | 1739 | If the tilde-prefix is @samp{~-}, the value of the shell variable |
28ef6c31 | 1740 | @env{OLDPWD}, if it is set, is substituted. |
cce855bc JA |
1741 | |
1742 | If the characters following the tilde in the tilde-prefix consist of a | |
1743 | number @var{N}, optionally prefixed by a @samp{+} or a @samp{-}, | |
1744 | the tilde-prefix is replaced with the | |
1745 | corresponding element from the directory stack, as it would be displayed | |
1746 | by the @code{dirs} builtin invoked with the characters following tilde | |
1747 | in the tilde-prefix as an argument (@pxref{The Directory Stack}). | |
1748 | If the tilde-prefix, sans the tilde, consists of a number without a | |
1749 | leading @samp{+} or @samp{-}, @samp{+} is assumed. | |
1750 | ||
1751 | If the login name is invalid, or the tilde expansion fails, the word is | |
1752 | left unchanged. | |
1753 | ||
1754 | Each variable assignment is checked for unquoted tilde-prefixes immediately | |
43df7bbb | 1755 | following a @samp{:} or the first @samp{=}. |
cce855bc | 1756 | In these cases, tilde expansion is also performed. |
b28ff8c9 | 1757 | Consequently, one may use filenames with tildes in assignments to |
28ef6c31 | 1758 | @env{PATH}, @env{MAILPATH}, and @env{CDPATH}, |
cce855bc JA |
1759 | and the shell assigns the expanded value. |
1760 | ||
1761 | The following table shows how Bash treats unquoted tilde-prefixes: | |
1762 | ||
1763 | @table @code | |
1764 | @item ~ | |
1765 | The value of @code{$HOME} | |
1766 | @item ~/foo | |
1767 | @file{$HOME/foo} | |
1768 | ||
1769 | @item ~fred/foo | |
1770 | The subdirectory @code{foo} of the home directory of the user | |
1771 | @code{fred} | |
1772 | ||
1773 | @item ~+/foo | |
1774 | @file{$PWD/foo} | |
1775 | ||
1776 | @item ~-/foo | |
1777 | @file{$@{OLDPWD-'~-'@}/foo} | |
1778 | ||
1779 | @item ~@var{N} | |
1780 | The string that would be displayed by @samp{dirs +@var{N}} | |
1781 | ||
1782 | @item ~+@var{N} | |
1783 | The string that would be displayed by @samp{dirs +@var{N}} | |
1784 | ||
1785 | @item ~-@var{N} | |
1786 | The string that would be displayed by @samp{dirs -@var{N}} | |
cce855bc JA |
1787 | @end table |
1788 | ||
ccc6cda3 JA |
1789 | @node Shell Parameter Expansion |
1790 | @subsection Shell Parameter Expansion | |
1791 | @cindex parameter expansion | |
1792 | @cindex expansion, parameter | |
1793 | ||
1794 | The @samp{$} character introduces parameter expansion, | |
1795 | command substitution, or arithmetic expansion. The parameter name | |
1796 | or symbol to be expanded may be enclosed in braces, which | |
1797 | are optional but serve to protect the variable to be expanded from | |
1798 | characters immediately following it which could be | |
1799 | interpreted as part of the name. | |
1800 | ||
cce855bc JA |
1801 | When braces are used, the matching ending brace is the first @samp{@}} |
1802 | not escaped by a backslash or within a quoted string, and not within an | |
1803 | embedded arithmetic expansion, command substitution, or parameter | |
1804 | expansion. | |
1805 | ||
ccc6cda3 | 1806 | The basic form of parameter expansion is $@{@var{parameter}@}. |
348a457e CR |
1807 | The value of @var{parameter} is substituted. |
1808 | The @var{parameter} is a shell parameter as described above | |
1809 | (@pxref{Shell Parameters}) or an array reference (@pxref{Arrays}). | |
1810 | The braces are required when @var{parameter} | |
ccc6cda3 | 1811 | is a positional parameter with more than one digit, |
348a457e | 1812 | or when @var{parameter} is followed by a character that is not to be |
ccc6cda3 JA |
1813 | interpreted as part of its name. |
1814 | ||
08e72d7a | 1815 | If the first character of @var{parameter} is an exclamation point (!), |
348a457e | 1816 | it introduces a level of variable indirection. |
ccc6cda3 JA |
1817 | Bash uses the value of the variable formed from the rest of |
1818 | @var{parameter} as the name of the variable; this variable is then | |
d166f048 | 1819 | expanded and that value is used in the rest of the substitution, rather |
ccc6cda3 JA |
1820 | than the value of @var{parameter} itself. |
1821 | This is known as @code{indirect expansion}. | |
4e136352 | 1822 | The exceptions to this are the expansions of $@{!@var{prefix}*@} |
d3a24ed2 | 1823 | and $@{!@var{name}[@@]@} |
bb70624e | 1824 | described below. |
d3a24ed2 CR |
1825 | The exclamation point must immediately follow the left brace in order to |
1826 | introduce indirection. | |
ccc6cda3 JA |
1827 | |
1828 | In each of the cases below, @var{word} is subject to tilde expansion, | |
1829 | parameter expansion, command substitution, and arithmetic expansion. | |
bb70624e | 1830 | |
e141c35a | 1831 | When not performing substring expansion, using the form described |
348a457e | 1832 | below (e.g., @samp{:-}), Bash tests for a parameter that is unset or null. |
e141c35a CR |
1833 | Omitting the colon results in a test only for a parameter that is unset. |
1834 | Put another way, if the colon is included, | |
1835 | the operator tests for both @var{parameter}'s existence and that its value | |
1836 | is not null; if the colon is omitted, the operator tests only for existence. | |
ccc6cda3 JA |
1837 | |
1838 | @table @code | |
1839 | ||
1840 | @item $@{@var{parameter}:@minus{}@var{word}@} | |
1841 | If @var{parameter} is unset or null, the expansion of | |
1842 | @var{word} is substituted. Otherwise, the value of | |
1843 | @var{parameter} is substituted. | |
1844 | ||
1845 | @item $@{@var{parameter}:=@var{word}@} | |
1846 | If @var{parameter} | |
1847 | is unset or null, the expansion of @var{word} | |
1848 | is assigned to @var{parameter}. | |
de8913bd CR |
1849 | The value of @var{parameter} is then substituted. |
1850 | Positional parameters and special parameters may not be assigned to | |
1851 | in this way. | |
ccc6cda3 JA |
1852 | |
1853 | @item $@{@var{parameter}:?@var{word}@} | |
1854 | If @var{parameter} | |
1855 | is null or unset, the expansion of @var{word} (or a message | |
1856 | to that effect if @var{word} | |
1857 | is not present) is written to the standard error and the shell, if it | |
1858 | is not interactive, exits. Otherwise, the value of @var{parameter} is | |
1859 | substituted. | |
1860 | ||
1861 | @item $@{@var{parameter}:+@var{word}@} | |
1862 | If @var{parameter} | |
1863 | is null or unset, nothing is substituted, otherwise the expansion of | |
1864 | @var{word} is substituted. | |
1865 | ||
1866 | @item $@{@var{parameter}:@var{offset}@} | |
1867 | @itemx $@{@var{parameter}:@var{offset}:@var{length}@} | |
348a457e CR |
1868 | This is referred to as Substring Expansion. |
1869 | It expands to up to @var{length} characters of the value of @var{parameter} | |
cce855bc | 1870 | starting at the character specified by @var{offset}. |
348a457e CR |
1871 | If @var{parameter} is @samp{@@}, an indexed array subscripted by |
1872 | @samp{@@} or @samp{*}, or an associative array name, the results differ as | |
1873 | described below. | |
1874 | If @var{length} is omitted, it expands to the substring of the value of | |
1875 | @var{parameter} starting at the character specified by @var{offset} | |
1876 | and extending to the end of the value. | |
ccc6cda3 | 1877 | @var{length} and @var{offset} are arithmetic expressions |
cce855bc | 1878 | (@pxref{Shell Arithmetic}). |
ccc6cda3 | 1879 | |
ccc6cda3 | 1880 | If @var{offset} evaluates to a number less than zero, the value |
348a457e CR |
1881 | is used as an offset in characters |
1882 | from the end of the value of @var{parameter}. | |
1883 | If @var{length} evaluates to a number less than zero, | |
1884 | it is interpreted as an offset in characters | |
1885 | from the end of the value of @var{parameter} rather than | |
1886 | a number of characters, and the expansion is the characters between | |
1887 | @var{offset} and that result. | |
1888 | Note that a negative offset must be separated from the colon by at least | |
1889 | one space to avoid being confused with the @samp{:-} expansion. | |
1890 | ||
1891 | Here are some examples illustrating substring expansion on parameters and | |
1892 | subscripted arrays: | |
1893 | ||
1894 | @verbatim | |
1895 | $ string=01234567890abcdefgh | |
1896 | $ echo ${string:7} | |
1897 | 7890abcdefgh | |
1898 | $ echo ${string:7:0} | |
1899 | ||
1900 | $ echo ${string:7:2} | |
1901 | 78 | |
1902 | $ echo ${string:7:-2} | |
1903 | 7890abcdef | |
1904 | $ echo ${string: -7} | |
1905 | bcdefgh | |
1906 | $ echo ${string: -7:0} | |
1907 | ||
1908 | $ echo ${string: -7:2} | |
1909 | bc | |
1910 | $ echo ${string: -7:-2} | |
1911 | bcdef | |
1912 | $ set -- 01234567890abcdefgh | |
1913 | $ echo ${1:7} | |
1914 | 7890abcdefgh | |
1915 | $ echo ${1:7:0} | |
1916 | ||
1917 | $ echo ${1:7:2} | |
1918 | 78 | |
1919 | $ echo ${1:7:-2} | |
1920 | 7890abcdef | |
1921 | $ echo ${1: -7} | |
1922 | bcdefgh | |
1923 | $ echo ${1: -7:0} | |
1924 | ||
1925 | $ echo ${1: -7:2} | |
1926 | bc | |
1927 | $ echo ${1: -7:-2} | |
1928 | bcdef | |
1929 | $ array[0]=01234567890abcdefgh | |
1930 | $ echo ${array[0]:7} | |
1931 | 7890abcdefgh | |
1932 | $ echo ${array[0]:7:0} | |
1933 | ||
1934 | $ echo ${array[0]:7:2} | |
1935 | 78 | |
1936 | $ echo ${array[0]:7:-2} | |
1937 | 7890abcdef | |
1938 | $ echo ${array[0]: -7} | |
1939 | bcdefgh | |
1940 | $ echo ${array[0]: -7:0} | |
1941 | ||
1942 | $ echo ${array[0]: -7:2} | |
1943 | bc | |
1944 | $ echo ${array[0]: -7:-2} | |
1945 | bcdef | |
1946 | @end verbatim | |
1947 | ||
ccc6cda3 JA |
1948 | If @var{parameter} is @samp{@@}, the result is @var{length} positional |
1949 | parameters beginning at @var{offset}. | |
348a457e CR |
1950 | A negative @var{offset} is taken relative to one greater than the greatest |
1951 | positional parameter, so an offset of -1 evaluates to the last positional | |
1952 | parameter. | |
1953 | It is an expansion error if @var{length} evaluates to a number less than zero. | |
1954 | ||
1955 | The following examples illustrate substring expansion using positional | |
1956 | parameters: | |
1957 | ||
1958 | @verbatim | |
1959 | $ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h | |
1960 | $ echo ${@:7} | |
1961 | 7 8 9 0 a b c d e f g h | |
1962 | $ echo ${@:7:0} | |
1963 | ||
1964 | $ echo ${@:7:2} | |
1965 | 7 8 | |
1966 | $ echo ${@:7:-2} | |
1967 | bash: -2: substring expression < 0 | |
1968 | $ echo ${@: -7:2} | |
1969 | b c | |
1970 | $ echo ${@:0} | |
1971 | ./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h | |
1972 | $ echo ${@:0:2} | |
1973 | ./bash 1 | |
1974 | $ echo ${@: -7:0} | |
1975 | ||
1976 | @end verbatim | |
1977 | ||
09767ff0 CR |
1978 | If @var{parameter} is an indexed array name subscripted |
1979 | by @samp{@@} or @samp{*}, the result is the @var{length} | |
cce855bc | 1980 | members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}. |
ec2199bd CR |
1981 | A negative @var{offset} is taken relative to one greater than the maximum |
1982 | index of the specified array. | |
348a457e CR |
1983 | It is an expansion error if @var{length} evaluates to a number less than zero. |
1984 | ||
1985 | These examples show how you can use substring expansion with indexed | |
1986 | arrays: | |
1987 | ||
1988 | @verbatim | |
1989 | $ array=(0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h) | |
1990 | $ echo ${array[@]:7} | |
1991 | 7 8 9 0 a b c d e f g h | |
1992 | $ echo ${array[@]:7:2} | |
1993 | 7 8 | |
1994 | $ echo ${array[@]: -7:2} | |
1995 | b c | |
1996 | $ echo ${array[@]: -7:-2} | |
1997 | bash: -2: substring expression < 0 | |
1998 | $ echo ${array[@]:0} | |
1999 | 0 1 2 3 4 5 6 7 8 9 0 a b c d e f g h | |
2000 | $ echo ${array[@]:0:2} | |
2001 | 0 1 | |
2002 | $ echo ${array[@]: -7:0} | |
2003 | ||
2004 | @end verbatim | |
2005 | ||
09767ff0 CR |
2006 | Substring expansion applied to an associative array produces undefined |
2007 | results. | |
2008 | ||
cce855bc | 2009 | Substring indexing is zero-based unless the positional parameters |
d3ad40de CR |
2010 | are used, in which case the indexing starts at 1 by default. |
2011 | If @var{offset} is 0, and the positional parameters are used, @code{$@@} is | |
2012 | prefixed to the list. | |
ccc6cda3 | 2013 | |
bb70624e | 2014 | @item $@{!@var{prefix}*@} |
d3a24ed2 | 2015 | @itemx $@{!@var{prefix}@@@} |
bb70624e | 2016 | Expands to the names of variables whose names begin with @var{prefix}, |
28ef6c31 | 2017 | separated by the first character of the @env{IFS} special variable. |
d3ad40de CR |
2018 | When @samp{@@} is used and the expansion appears within double quotes, each |
2019 | variable name expands to a separate word. | |
bb70624e | 2020 | |
d3a24ed2 CR |
2021 | @item $@{!@var{name}[@@]@} |
2022 | @itemx $@{!@var{name}[*]@} | |
2023 | If @var{name} is an array variable, expands to the list of array indices | |
2024 | (keys) assigned in @var{name}. | |
2025 | If @var{name} is not an array, expands to 0 if @var{name} is set and null | |
2026 | otherwise. | |
2027 | When @samp{@@} is used and the expansion appears within double quotes, each | |
2028 | key expands to a separate word. | |
2029 | ||
ccc6cda3 | 2030 | @item $@{#@var{parameter}@} |
cce855bc JA |
2031 | The length in characters of the expanded value of @var{parameter} is |
2032 | substituted. | |
2033 | If @var{parameter} is @samp{*} or @samp{@@}, the value substituted | |
2034 | is the number of positional parameters. | |
2035 | If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@}, | |
2036 | the value substituted is the number of elements in the array. | |
ccc6cda3 JA |
2037 | |
2038 | @item $@{@var{parameter}#@var{word}@} | |
2039 | @itemx $@{@var{parameter}##@var{word}@} | |
2040 | The @var{word} | |
2041 | is expanded to produce a pattern just as in filename | |
2042 | expansion (@pxref{Filename Expansion}). If the pattern matches | |
cce855bc JA |
2043 | the beginning of the expanded value of @var{parameter}, |
2044 | then the result of the expansion is the expanded value of @var{parameter} | |
ccc6cda3 JA |
2045 | with the shortest matching pattern (the @samp{#} case) or the |
2046 | longest matching pattern (the @samp{##} case) deleted. | |
2047 | If @var{parameter} is @samp{@@} or @samp{*}, | |
2048 | the pattern removal operation is applied to each positional | |
2049 | parameter in turn, and the expansion is the resultant list. | |
2050 | If @var{parameter} is an array variable subscripted with | |
2051 | @samp{@@} or @samp{*}, | |
2052 | the pattern removal operation is applied to each member of the | |
2053 | array in turn, and the expansion is the resultant list. | |
2054 | ||
2055 | @item $@{@var{parameter}%@var{word}@} | |
2056 | @itemx $@{@var{parameter}%%@var{word}@} | |
2057 | The @var{word} is expanded to produce a pattern just as in | |
2058 | filename expansion. | |
cce855bc JA |
2059 | If the pattern matches a trailing portion of the expanded value of |
2060 | @var{parameter}, then the result of the expansion is the value of | |
2061 | @var{parameter} with the shortest matching pattern (the @samp{%} case) | |
2062 | or the longest matching pattern (the @samp{%%} case) deleted. | |
ccc6cda3 JA |
2063 | If @var{parameter} is @samp{@@} or @samp{*}, |
2064 | the pattern removal operation is applied to each positional | |
2065 | parameter in turn, and the expansion is the resultant list. | |
2066 | If @var{parameter} | |
2067 | is an array variable subscripted with @samp{@@} or @samp{*}, | |
2068 | the pattern removal operation is applied to each member of the | |
2069 | array in turn, and the expansion is the resultant list. | |
2070 | ||
2071 | @item $@{@var{parameter}/@var{pattern}/@var{string}@} | |
ccc6cda3 JA |
2072 | |
2073 | The @var{pattern} is expanded to produce a pattern just as in | |
2074 | filename expansion. | |
2075 | @var{Parameter} is expanded and the longest match of @var{pattern} | |
2076 | against its value is replaced with @var{string}. | |
dc8fbaf9 CR |
2077 | If @var{pattern} begins with @samp{/}, all matches of @var{pattern} are |
2078 | replaced with @var{string}. Normally only the first match is replaced. | |
ccc6cda3 | 2079 | If @var{pattern} begins with @samp{#}, it must match at the beginning |
b72432fd | 2080 | of the expanded value of @var{parameter}. |
ccc6cda3 | 2081 | If @var{pattern} begins with @samp{%}, it must match at the end |
b72432fd | 2082 | of the expanded value of @var{parameter}. |
ccc6cda3 JA |
2083 | If @var{string} is null, matches of @var{pattern} are deleted |
2084 | and the @code{/} following @var{pattern} may be omitted. | |
2085 | If @var{parameter} is @samp{@@} or @samp{*}, | |
2086 | the substitution operation is applied to each positional | |
2087 | parameter in turn, and the expansion is the resultant list. | |
2088 | If @var{parameter} | |
2089 | is an array variable subscripted with @samp{@@} or @samp{*}, | |
2090 | the substitution operation is applied to each member of the | |
2091 | array in turn, and the expansion is the resultant list. | |
2092 | ||
09767ff0 CR |
2093 | @item $@{@var{parameter}^@var{pattern}@} |
2094 | @itemx $@{@var{parameter}^^@var{pattern}@} | |
2095 | @itemx $@{@var{parameter},@var{pattern}@} | |
2096 | @itemx $@{@var{parameter},,@var{pattern}@} | |
2097 | This expansion modifies the case of alphabetic characters in @var{parameter}. | |
2098 | The @var{pattern} is expanded to produce a pattern just as in | |
d0ca3503 | 2099 | filename expansion. |
45c0f7f8 CR |
2100 | Each character in the expanded value of @var{parameter} is tested against |
2101 | @var{pattern}, and, if it matches the pattern, its case is converted. | |
2102 | The pattern should not attempt to match more than one character. | |
09767ff0 CR |
2103 | The @samp{^} operator converts lowercase letters matching @var{pattern} |
2104 | to uppercase; the @samp{,} operator converts matching uppercase letters | |
2105 | to lowercase. | |
2106 | The @samp{^^} and @samp{,,} expansions convert each matched character in the | |
2107 | expanded value; the @samp{^} and @samp{,} expansions match and convert only | |
e141c35a | 2108 | the first character in the expanded value. |
09767ff0 CR |
2109 | If @var{pattern} is omitted, it is treated like a @samp{?}, which matches |
2110 | every character. | |
2111 | If @var{parameter} is @samp{@@} or @samp{*}, | |
2112 | the case modification operation is applied to each positional | |
2113 | parameter in turn, and the expansion is the resultant list. | |
2114 | If @var{parameter} | |
2115 | is an array variable subscripted with @samp{@@} or @samp{*}, | |
2116 | the case modification operation is applied to each member of the | |
2117 | array in turn, and the expansion is the resultant list. | |
ccc6cda3 JA |
2118 | @end table |
2119 | ||
2120 | @node Command Substitution | |
2121 | @subsection Command Substitution | |
2122 | @cindex command substitution | |
2123 | ||
2124 | Command substitution allows the output of a command to replace | |
bb70624e JA |
2125 | the command itself. |
2126 | Command substitution occurs when a command is enclosed as follows: | |
ccc6cda3 JA |
2127 | @example |
2128 | $(@var{command}) | |
2129 | @end example | |
2130 | @noindent | |
2131 | or | |
2132 | @example | |
2133 | `@var{command}` | |
2134 | @end example | |
2135 | ||
2136 | @noindent | |
2137 | Bash performs the expansion by executing @var{command} and | |
2138 | replacing the command substitution with the standard output of the | |
2139 | command, with any trailing newlines deleted. | |
cce855bc JA |
2140 | Embedded newlines are not deleted, but they may be removed during |
2141 | word splitting. | |
2142 | The command substitution @code{$(cat @var{file})} can be | |
2143 | replaced by the equivalent but faster @code{$(< @var{file})}. | |
ccc6cda3 JA |
2144 | |
2145 | When the old-style backquote form of substitution is used, | |
2146 | backslash retains its literal meaning except when followed by | |
cce855bc JA |
2147 | @samp{$}, @samp{`}, or @samp{\}. |
2148 | The first backquote not preceded by a backslash terminates the | |
2149 | command substitution. | |
ccc6cda3 JA |
2150 | When using the @code{$(@var{command})} form, all characters between |
2151 | the parentheses make up the command; none are treated specially. | |
2152 | ||
cce855bc JA |
2153 | Command substitutions may be nested. To nest when using the backquoted |
2154 | form, escape the inner backquotes with backslashes. | |
ccc6cda3 JA |
2155 | |
2156 | If the substitution appears within double quotes, word splitting and | |
2157 | filename expansion are not performed on the results. | |
2158 | ||
cce855bc JA |
2159 | @node Arithmetic Expansion |
2160 | @subsection Arithmetic Expansion | |
2161 | @cindex expansion, arithmetic | |
2162 | @cindex arithmetic expansion | |
2163 | ||
2164 | Arithmetic expansion allows the evaluation of an arithmetic expression | |
2165 | and the substitution of the result. The format for arithmetic expansion is: | |
2166 | ||
2167 | @example | |
2168 | $(( @var{expression} )) | |
2169 | @end example | |
2170 | ||
2171 | The expression is treated as if it were within double quotes, but | |
2172 | a double quote inside the parentheses is not treated specially. | |
2173 | All tokens in the expression undergo parameter expansion, command | |
2174 | substitution, and quote removal. | |
d3a24ed2 | 2175 | Arithmetic expansions may be nested. |
cce855bc JA |
2176 | |
2177 | The evaluation is performed according to the rules listed below | |
2178 | (@pxref{Shell Arithmetic}). | |
2179 | If the expression is invalid, Bash prints a message indicating | |
2180 | failure to the standard error and no substitution occurs. | |
2181 | ||
ccc6cda3 JA |
2182 | @node Process Substitution |
2183 | @subsection Process Substitution | |
2184 | @cindex process substitution | |
2185 | ||
2186 | Process substitution is supported on systems that support named | |
bb70624e | 2187 | pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files. |
ccc6cda3 JA |
2188 | It takes the form of |
2189 | @example | |
2190 | <(@var{list}) | |
2191 | @end example | |
2192 | @noindent | |
2193 | or | |
2194 | @example | |
2195 | >(@var{list}) | |
2196 | @end example | |
2197 | @noindent | |
2198 | The process @var{list} is run with its input or output connected to a | |
bb70624e | 2199 | @sc{fifo} or some file in @file{/dev/fd}. The name of this file is |
ccc6cda3 JA |
2200 | passed as an argument to the current command as the result of the |
2201 | expansion. If the @code{>(@var{list})} form is used, writing to | |
2202 | the file will provide input for @var{list}. If the | |
2203 | @code{<(@var{list})} form is used, the file passed as an | |
2204 | argument should be read to obtain the output of @var{list}. | |
bb70624e JA |
2205 | Note that no space may appear between the @code{<} or @code{>} |
2206 | and the left parenthesis, otherwise the construct would be interpreted | |
2207 | as a redirection. | |
ccc6cda3 | 2208 | |
cce855bc JA |
2209 | When available, process substitution is performed simultaneously with |
2210 | parameter and variable expansion, command substitution, and arithmetic | |
2211 | expansion. | |
ccc6cda3 JA |
2212 | |
2213 | @node Word Splitting | |
2214 | @subsection Word Splitting | |
2215 | @cindex word splitting | |
2216 | ||
2217 | The shell scans the results of parameter expansion, command substitution, | |
2218 | and arithmetic expansion that did not occur within double quotes for | |
2219 | word splitting. | |
2220 | ||
d3ad40de CR |
2221 | The shell treats each character of @env{$IFS} as a delimiter, and splits |
2222 | the results of the other expansions into words on these characters. | |
2223 | If @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>}, | |
2224 | the default, then sequences of | |
2225 | @code{ <space>}, @code{<tab>}, and @code{<newline>} | |
2226 | at the beginning and end of the results of the previous | |
2227 | expansions are ignored, and any sequence of @env{IFS} | |
2228 | characters not at the beginning or end serves to delimit words. | |
2229 | If @env{IFS} has a value other than the default, then sequences of | |
ccc6cda3 JA |
2230 | the whitespace characters @code{space} and @code{tab} |
2231 | are ignored at the beginning and end of the | |
2232 | word, as long as the whitespace character is in the | |
28ef6c31 JA |
2233 | value of @env{IFS} (an @env{IFS} whitespace character). |
2234 | Any character in @env{IFS} that is not @env{IFS} | |
2235 | whitespace, along with any adjacent @env{IFS} | |
2236 | whitespace characters, delimits a field. A sequence of @env{IFS} | |
ccc6cda3 | 2237 | whitespace characters is also treated as a delimiter. |
28ef6c31 | 2238 | If the value of @env{IFS} is null, no word splitting occurs. |
ccc6cda3 JA |
2239 | |
2240 | Explicit null arguments (@code{""} or @code{''}) are retained. | |
2241 | Unquoted implicit null arguments, resulting from the expansion of | |
bb70624e | 2242 | parameters that have no values, are removed. |
ccc6cda3 JA |
2243 | If a parameter with no value is expanded within double quotes, a |
2244 | null argument results and is retained. | |
2245 | ||
2246 | Note that if no expansion occurs, no splitting | |
2247 | is performed. | |
2248 | ||
2249 | @node Filename Expansion | |
2250 | @subsection Filename Expansion | |
cce855bc JA |
2251 | @menu |
2252 | * Pattern Matching:: How the shell matches patterns. | |
2253 | @end menu | |
ccc6cda3 JA |
2254 | @cindex expansion, filename |
2255 | @cindex expansion, pathname | |
2256 | @cindex filename expansion | |
2257 | @cindex pathname expansion | |
2258 | ||
28ef6c31 | 2259 | After word splitting, unless the @option{-f} option has been set |
cce855bc | 2260 | (@pxref{The Set Builtin}), Bash scans each word for the characters |
bb70624e | 2261 | @samp{*}, @samp{?}, and @samp{[}. |
ccc6cda3 JA |
2262 | If one of these characters appears, then the word is |
2263 | regarded as a @var{pattern}, | |
2264 | and replaced with an alphabetically sorted list of | |
b28ff8c9 CR |
2265 | filenames matching the pattern (@pxref{Pattern Matching}). |
2266 | If no matching filenames are found, | |
ccc6cda3 | 2267 | and the shell option @code{nullglob} is disabled, the word is left |
cce855bc JA |
2268 | unchanged. |
2269 | If the @code{nullglob} option is set, and no matches are found, the word | |
2270 | is removed. | |
d3a24ed2 CR |
2271 | If the @code{failglob} shell option is set, and no matches are found, |
2272 | an error message is printed and the command is not executed. | |
cce855bc JA |
2273 | If the shell option @code{nocaseglob} is enabled, the match is performed |
2274 | without regard to the case of alphabetic characters. | |
2275 | ||
d0ca3503 | 2276 | When a pattern is used for filename expansion, the character @samp{.} |
ccc6cda3 | 2277 | at the start of a filename or immediately following a slash |
cce855bc | 2278 | must be matched explicitly, unless the shell option @code{dotglob} is set. |
b28ff8c9 | 2279 | When matching a filename, the slash character must always be |
cce855bc | 2280 | matched explicitly. |
ccc6cda3 | 2281 | In other cases, the @samp{.} character is not treated specially. |
cce855bc | 2282 | |
d3ad40de | 2283 | See the description of @code{shopt} in @ref{The Shopt Builtin}, |
cce855bc | 2284 | for a description of the @code{nocaseglob}, @code{nullglob}, |
d3a24ed2 | 2285 | @code{failglob}, and @code{dotglob} options. |
ccc6cda3 | 2286 | |
28ef6c31 | 2287 | The @env{GLOBIGNORE} |
ccc6cda3 | 2288 | shell variable may be used to restrict the set of filenames matching a |
28ef6c31 | 2289 | pattern. If @env{GLOBIGNORE} |
ccc6cda3 | 2290 | is set, each matching filename that also matches one of the patterns in |
28ef6c31 | 2291 | @env{GLOBIGNORE} is removed from the list of matches. The filenames |
ccc6cda3 | 2292 | @file{.} and @file{..} |
d3a24ed2 CR |
2293 | are always ignored when @env{GLOBIGNORE} |
2294 | is set and not null. | |
2295 | However, setting @env{GLOBIGNORE} to a non-null value has the effect of | |
ccc6cda3 JA |
2296 | enabling the @code{dotglob} |
2297 | shell option, so all other filenames beginning with a | |
2298 | @samp{.} will match. | |
2299 | To get the old behavior of ignoring filenames beginning with a | |
28ef6c31 JA |
2300 | @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}. |
2301 | The @code{dotglob} option is disabled when @env{GLOBIGNORE} | |
ccc6cda3 JA |
2302 | is unset. |
2303 | ||
cce855bc JA |
2304 | @node Pattern Matching |
2305 | @subsubsection Pattern Matching | |
2306 | @cindex pattern matching | |
2307 | @cindex matching, pattern | |
2308 | ||
2309 | Any character that appears in a pattern, other than the special pattern | |
d3a24ed2 CR |
2310 | characters described below, matches itself. |
2311 | The @sc{nul} character may not occur in a pattern. | |
2312 | A backslash escapes the following character; the | |
2313 | escaping backslash is discarded when matching. | |
2314 | The special pattern characters must be quoted if they are to be matched | |
2315 | literally. | |
cce855bc | 2316 | |
ccc6cda3 JA |
2317 | The special pattern characters have the following meanings: |
2318 | @table @code | |
2319 | @item * | |
2320 | Matches any string, including the null string. | |
4ac1ff98 CR |
2321 | When the @code{globstar} shell option is enabled, and @samp{*} is used in |
2322 | a filename expansion context, two adjacent @samp{*}s used as a single | |
2323 | pattern will match all files and zero or more directories and | |
2324 | subdirectories. | |
2325 | If followed by a @samp{/}, two adjacent @samp{*}s will match only | |
2326 | directories and subdirectories. | |
ccc6cda3 JA |
2327 | @item ? |
2328 | Matches any single character. | |
2329 | @item [@dots{}] | |
2330 | Matches any one of the enclosed characters. A pair of characters | |
28ef6c31 JA |
2331 | separated by a hyphen denotes a @var{range expression}; |
2332 | any character that sorts between those two characters, inclusive, | |
2333 | using the current locale's collating sequence and character set, | |
ccc6cda3 JA |
2334 | is matched. If the first character following the |
2335 | @samp{[} is a @samp{!} or a @samp{^} | |
2336 | then any character not enclosed is matched. A @samp{@minus{}} | |
2337 | may be matched by including it as the first or last character | |
2338 | in the set. A @samp{]} may be matched by including it as the first | |
2339 | character in the set. | |
28ef6c31 | 2340 | The sorting order of characters in range expressions is determined by |
3443670e CR |
2341 | the current locale and the values of the |
2342 | @env{LC_COLLATE} and @env{LC_ALL} shell variables, if set. | |
28ef6c31 JA |
2343 | |
2344 | For example, in the default C locale, @samp{[a-dx-z]} is equivalent to | |
2345 | @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in | |
2346 | these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]}; | |
2347 | it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain | |
2348 | the traditional interpretation of ranges in bracket expressions, you can | |
2349 | force the use of the C locale by setting the @env{LC_COLLATE} or | |
74d0116b CR |
2350 | @env{LC_ALL} environment variable to the value @samp{C}, or enable the |
2351 | @code{globasciiranges} shell option. | |
cce855bc JA |
2352 | |
2353 | Within @samp{[} and @samp{]}, @var{character classes} can be specified | |
2354 | using the syntax | |
2355 | @code{[:}@var{class}@code{:]}, where @var{class} is one of the | |
ac18b312 | 2356 | following classes defined in the @sc{posix} standard: |
cce855bc JA |
2357 | @example |
2358 | alnum alpha ascii blank cntrl digit graph lower | |
7117c2d2 | 2359 | print punct space upper word xdigit |
cce855bc JA |
2360 | @end example |
2361 | @noindent | |
2362 | A character class matches any character belonging to that class. | |
7117c2d2 JA |
2363 | The @code{word} character class matches letters, digits, and the character |
2364 | @samp{_}. | |
cce855bc JA |
2365 | |
2366 | Within @samp{[} and @samp{]}, an @var{equivalence class} can be | |
2367 | specified using the syntax @code{[=}@var{c}@code{=]}, which | |
2368 | matches all characters with the same collation weight (as defined | |
2369 | by the current locale) as the character @var{c}. | |
2370 | ||
2371 | Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]} | |
2372 | matches the collating symbol @var{symbol}. | |
2373 | @end table | |
2374 | ||
2375 | If the @code{extglob} shell option is enabled using the @code{shopt} | |
2376 | builtin, several extended pattern matching operators are recognized. | |
2377 | In the following description, a @var{pattern-list} is a list of one | |
2378 | or more patterns separated by a @samp{|}. | |
2379 | Composite patterns may be formed using one or more of the following | |
2380 | sub-patterns: | |
2381 | ||
2382 | @table @code | |
2383 | @item ?(@var{pattern-list}) | |
2384 | Matches zero or one occurrence of the given patterns. | |
2385 | ||
2386 | @item *(@var{pattern-list}) | |
2387 | Matches zero or more occurrences of the given patterns. | |
2388 | ||
2389 | @item +(@var{pattern-list}) | |
2390 | Matches one or more occurrences of the given patterns. | |
2391 | ||
2392 | @item @@(@var{pattern-list}) | |
43df7bbb | 2393 | Matches one of the given patterns. |
cce855bc JA |
2394 | |
2395 | @item !(@var{pattern-list}) | |
2396 | Matches anything except one of the given patterns. | |
ccc6cda3 JA |
2397 | @end table |
2398 | ||
2399 | @node Quote Removal | |
2400 | @subsection Quote Removal | |
2401 | ||
2402 | After the preceding expansions, all unquoted occurrences of the | |
2403 | characters @samp{\}, @samp{'}, and @samp{"} that did not | |
2404 | result from one of the above expansions are removed. | |
2405 | ||
2406 | @node Redirections | |
2407 | @section Redirections | |
2408 | @cindex redirection | |
2409 | ||
2410 | Before a command is executed, its input and output | |
2411 | may be @var{redirected} | |
2412 | using a special notation interpreted by the shell. | |
c31d56a7 CR |
2413 | Redirection allows commands' file handles to be |
2414 | duplicated, opened, closed, | |
2415 | made to refer to different files, | |
2416 | and can change the files the command reads from and writes to. | |
2417 | Redirection may also be used to modify file handles in the | |
ccc6cda3 JA |
2418 | current shell execution environment. The following redirection |
2419 | operators may precede or appear anywhere within a | |
2420 | simple command or may follow a command. | |
2421 | Redirections are processed in the order they appear, from | |
2422 | left to right. | |
2423 | ||
a8fd3f3e CR |
2424 | Each redirection that may be preceded by a file descriptor number |
2425 | may instead be preceded by a word of the form @{@var{varname}@}. | |
2426 | In this case, for each redirection operator except | |
2427 | >&- and <&-, the shell will allocate a file descriptor greater | |
2428 | than 10 and assign it to @{@var{varname}@}. If >&- or <&- is preceded | |
2429 | by @{@var{varname}@}, the value of @var{varname} defines the file | |
2430 | descriptor to close. | |
2431 | ||
ccc6cda3 JA |
2432 | In the following descriptions, if the file descriptor number is |
2433 | omitted, and the first character of the redirection operator is | |
2434 | @samp{<}, the redirection refers to the standard input (file | |
2435 | descriptor 0). If the first character of the redirection operator | |
2436 | is @samp{>}, the redirection refers to the standard output (file | |
2437 | descriptor 1). | |
2438 | ||
cce855bc JA |
2439 | The word following the redirection operator in the following |
2440 | descriptions, unless otherwise noted, is subjected to brace expansion, | |
2441 | tilde expansion, parameter expansion, command substitution, arithmetic | |
bb70624e | 2442 | expansion, quote removal, filename expansion, and word splitting. |
cce855bc | 2443 | If it expands to more than one word, Bash reports an error. |
ccc6cda3 JA |
2444 | |
2445 | Note that the order of redirections is significant. For example, | |
2446 | the command | |
2447 | @example | |
2448 | ls > @var{dirlist} 2>&1 | |
2449 | @end example | |
2450 | @noindent | |
bb70624e JA |
2451 | directs both standard output (file descriptor 1) and standard error |
2452 | (file descriptor 2) to the file @var{dirlist}, while the command | |
ccc6cda3 JA |
2453 | @example |
2454 | ls 2>&1 > @var{dirlist} | |
2455 | @end example | |
2456 | @noindent | |
2457 | directs only the standard output to file @var{dirlist}, | |
db31fb26 | 2458 | because the standard error was made a copy of the standard output |
ccc6cda3 JA |
2459 | before the standard output was redirected to @var{dirlist}. |
2460 | ||
bb70624e JA |
2461 | Bash handles several filenames specially when they are used in |
2462 | redirections, as described in the following table: | |
2463 | ||
2464 | @table @code | |
2465 | @item /dev/fd/@var{fd} | |
2466 | If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated. | |
2467 | ||
2468 | @item /dev/stdin | |
2469 | File descriptor 0 is duplicated. | |
2470 | ||
2471 | @item /dev/stdout | |
2472 | File descriptor 1 is duplicated. | |
2473 | ||
2474 | @item /dev/stderr | |
2475 | File descriptor 2 is duplicated. | |
2476 | ||
2477 | @item /dev/tcp/@var{host}/@var{port} | |
2478 | If @var{host} is a valid hostname or Internet address, and @var{port} | |
e107650c CR |
2479 | is an integer port number or service name, Bash attempts to open |
2480 | the corresponding TCP socket. | |
bb70624e JA |
2481 | |
2482 | @item /dev/udp/@var{host}/@var{port} | |
2483 | If @var{host} is a valid hostname or Internet address, and @var{port} | |
e107650c CR |
2484 | is an integer port number or service name, Bash attempts to open |
2485 | the corresponding UDP socket. | |
bb70624e JA |
2486 | @end table |
2487 | ||
cce855bc JA |
2488 | A failure to open or create a file causes the redirection to fail. |
2489 | ||
cac4cdbf CR |
2490 | Redirections using file descriptors greater than 9 should be used with |
2491 | care, as they may conflict with file descriptors the shell uses | |
2492 | internally. | |
2493 | ||
ccc6cda3 JA |
2494 | @subsection Redirecting Input |
2495 | Redirection of input causes the file whose name results from | |
2496 | the expansion of @var{word} | |
2497 | to be opened for reading on file descriptor @code{n}, | |
2498 | or the standard input (file descriptor 0) if @code{n} | |
2499 | is not specified. | |
2500 | ||
2501 | The general format for redirecting input is: | |
2502 | @example | |
7117c2d2 | 2503 | [@var{n}]<@var{word} |
ccc6cda3 JA |
2504 | @end example |
2505 | ||
2506 | @subsection Redirecting Output | |
2507 | Redirection of output causes the file whose name results from | |
2508 | the expansion of @var{word} | |
7117c2d2 JA |
2509 | to be opened for writing on file descriptor @var{n}, |
2510 | or the standard output (file descriptor 1) if @var{n} | |
ccc6cda3 JA |
2511 | is not specified. If the file does not exist it is created; |
2512 | if it does exist it is truncated to zero size. | |
2513 | ||
2514 | The general format for redirecting output is: | |
2515 | @example | |
7117c2d2 | 2516 | [@var{n}]>[|]@var{word} |
ccc6cda3 JA |
2517 | @end example |
2518 | ||
cce855bc JA |
2519 | If the redirection operator is @samp{>}, and the @code{noclobber} |
2520 | option to the @code{set} builtin has been enabled, the redirection | |
bb70624e | 2521 | will fail if the file whose name results from the expansion of |
cce855bc JA |
2522 | @var{word} exists and is a regular file. |
2523 | If the redirection operator is @samp{>|}, or the redirection operator is | |
2524 | @samp{>} and the @code{noclobber} option is not enabled, the redirection | |
2525 | is attempted even if the file named by @var{word} exists. | |
ccc6cda3 JA |
2526 | |
2527 | @subsection Appending Redirected Output | |
2528 | Redirection of output in this fashion | |
2529 | causes the file whose name results from | |
2530 | the expansion of @var{word} | |
7117c2d2 JA |
2531 | to be opened for appending on file descriptor @var{n}, |
2532 | or the standard output (file descriptor 1) if @var{n} | |
ccc6cda3 JA |
2533 | is not specified. If the file does not exist it is created. |
2534 | ||
2535 | The general format for appending output is: | |
2536 | @example | |
7117c2d2 | 2537 | [@var{n}]>>@var{word} |
ccc6cda3 JA |
2538 | @end example |
2539 | ||
2540 | @subsection Redirecting Standard Output and Standard Error | |
8943768b | 2541 | This construct allows both the |
ccc6cda3 JA |
2542 | standard output (file descriptor 1) and |
2543 | the standard error output (file descriptor 2) | |
2544 | to be redirected to the file whose name is the | |
8943768b | 2545 | expansion of @var{word}. |
ccc6cda3 JA |
2546 | |
2547 | There are two formats for redirecting standard output and | |
2548 | standard error: | |
2549 | @example | |
2550 | &>@var{word} | |
2551 | @end example | |
2552 | @noindent | |
2553 | and | |
2554 | @example | |
2555 | >&@var{word} | |
2556 | @end example | |
2557 | @noindent | |
2558 | Of the two forms, the first is preferred. | |
2559 | This is semantically equivalent to | |
2560 | @example | |
2561 | >@var{word} 2>&1 | |
2562 | @end example | |
c5402025 CR |
2563 | When using the second form, @var{word} may not expand to a number or |
2564 | @samp{-}. If it does, other redirection operators apply | |
2565 | (see Duplicating File Descriptors below) for compatibility reasons. | |
ccc6cda3 | 2566 | |
8943768b CR |
2567 | @subsection Appending Standard Output and Standard Error |
2568 | This construct allows both the | |
2569 | standard output (file descriptor 1) and | |
2570 | the standard error output (file descriptor 2) | |
2571 | to be appended to the file whose name is the | |
2572 | expansion of @var{word}. | |
2573 | ||
2574 | The format for appending standard output and standard error is: | |
2575 | @example | |
2576 | &>>@var{word} | |
2577 | @end example | |
2578 | @noindent | |
2579 | This is semantically equivalent to | |
2580 | @example | |
2581 | >>@var{word} 2>&1 | |
2582 | @end example | |
f6da9f85 | 2583 | (see Duplicating File Descriptors below). |
8943768b | 2584 | |
ccc6cda3 JA |
2585 | @subsection Here Documents |
2586 | This type of redirection instructs the shell to read input from the | |
2587 | current source until a line containing only @var{word} | |
2588 | (with no trailing blanks) is seen. All of | |
2589 | the lines read up to that point are then used as the standard | |
2590 | input for a command. | |
2591 | ||
7117c2d2 | 2592 | The format of here-documents is: |
ccc6cda3 JA |
2593 | @example |
2594 | <<[@minus{}]@var{word} | |
2595 | @var{here-document} | |
2596 | @var{delimiter} | |
2597 | @end example | |
2598 | ||
7610e0c5 CR |
2599 | No parameter and variable expansion, command substitution, |
2600 | arithmetic expansion, or filename expansion is performed on | |
ccc6cda3 JA |
2601 | @var{word}. If any characters in @var{word} are quoted, the |
2602 | @var{delimiter} is the result of quote removal on @var{word}, | |
cce855bc JA |
2603 | and the lines in the here-document are not expanded. |
2604 | If @var{word} is unquoted, | |
e73012f1 CR |
2605 | all lines of the here-document are subjected to |
2606 | parameter expansion, command substitution, and arithmetic expansion, | |
2607 | the character sequence @code{\newline} is ignored, and @samp{\} | |
ccc6cda3 JA |
2608 | must be used to quote the characters |
2609 | @samp{\}, @samp{$}, and @samp{`}. | |
2610 | ||
2611 | If the redirection operator is @samp{<<-}, | |
2612 | then all leading tab characters are stripped from input lines and the | |
2613 | line containing @var{delimiter}. | |
2614 | This allows here-documents within shell scripts to be indented in a | |
2615 | natural fashion. | |
2616 | ||
7117c2d2 JA |
2617 | @subsection Here Strings |
2618 | A variant of here documents, the format is: | |
2619 | @example | |
2620 | <<< @var{word} | |
2621 | @end example | |
2622 | ||
7610e0c5 CR |
2623 | The @var{word} undergoes |
2624 | brace expansion, tilde expansion, parameter and variable expansion, | |
2625 | command substitution, arithmetic expansion, and quote removal. | |
e73012f1 | 2626 | Pathname expansion and word splitting are not performed. |
7610e0c5 CR |
2627 | The result is supplied as a single string to the command on its |
2628 | standard input. | |
7117c2d2 | 2629 | |
ccc6cda3 JA |
2630 | @subsection Duplicating File Descriptors |
2631 | The redirection operator | |
2632 | @example | |
7117c2d2 | 2633 | [@var{n}]<&@var{word} |
ccc6cda3 JA |
2634 | @end example |
2635 | @noindent | |
2636 | is used to duplicate input file descriptors. | |
2637 | If @var{word} | |
7117c2d2 | 2638 | expands to one or more digits, the file descriptor denoted by @var{n} |
cce855bc JA |
2639 | is made to be a copy of that file descriptor. |
2640 | If the digits in @var{word} do not specify a file descriptor open for | |
2641 | input, a redirection error occurs. | |
2642 | If @var{word} | |
c5402025 CR |
2643 | evaluates to @samp{-}, file descriptor @var{n} is closed. |
2644 | If @var{n} is not specified, the standard input (file descriptor 0) is used. | |
ccc6cda3 JA |
2645 | |
2646 | The operator | |
2647 | @example | |
7117c2d2 | 2648 | [@var{n}]>&@var{word} |
ccc6cda3 JA |
2649 | @end example |
2650 | @noindent | |
2651 | is used similarly to duplicate output file descriptors. If | |
7117c2d2 | 2652 | @var{n} is not specified, the standard output (file descriptor 1) is used. |
cce855bc JA |
2653 | If the digits in @var{word} do not specify a file descriptor open for |
2654 | output, a redirection error occurs. | |
c5402025 CR |
2655 | If @var{word} |
2656 | evaluates to @samp{-}, file descriptor @var{n} is closed. | |
7117c2d2 | 2657 | As a special case, if @var{n} is omitted, and @var{word} does not |
c5402025 | 2658 | expand to one or more digits or @samp{-}, the standard output and standard |
ccc6cda3 JA |
2659 | error are redirected as described previously. |
2660 | ||
7117c2d2 JA |
2661 | @subsection Moving File Descriptors |
2662 | The redirection operator | |
2663 | @example | |
2664 | [@var{n}]<&@var{digit}- | |
2665 | @end example | |
2666 | @noindent | |
2667 | moves the file descriptor @var{digit} to file descriptor @var{n}, | |
2668 | or the standard input (file descriptor 0) if @var{n} is not specified. | |
2669 | @var{digit} is closed after being duplicated to @var{n}. | |
2670 | ||
2671 | Similarly, the redirection operator | |
2672 | @example | |
2673 | [@var{n}]>&@var{digit}- | |
2674 | @end example | |
2675 | @noindent | |
2676 | moves the file descriptor @var{digit} to file descriptor @var{n}, | |
2677 | or the standard output (file descriptor 1) if @var{n} is not specified. | |
2678 | ||
ccc6cda3 JA |
2679 | @subsection Opening File Descriptors for Reading and Writing |
2680 | The redirection operator | |
2681 | @example | |
7117c2d2 | 2682 | [@var{n}]<>@var{word} |
ccc6cda3 JA |
2683 | @end example |
2684 | @noindent | |
2685 | causes the file whose name is the expansion of @var{word} | |
2686 | to be opened for both reading and writing on file descriptor | |
7117c2d2 | 2687 | @var{n}, or on file descriptor 0 if @var{n} |
ccc6cda3 JA |
2688 | is not specified. If the file does not exist, it is created. |
2689 | ||
2690 | @node Executing Commands | |
2691 | @section Executing Commands | |
2692 | ||
2693 | @menu | |
cce855bc JA |
2694 | * Simple Command Expansion:: How Bash expands simple commands before |
2695 | executing them. | |
ccc6cda3 | 2696 | * Command Search and Execution:: How Bash finds commands and runs them. |
cce855bc JA |
2697 | * Command Execution Environment:: The environment in which Bash |
2698 | executes commands that are not | |
2699 | shell builtins. | |
ccc6cda3 | 2700 | * Environment:: The environment given to a command. |
ccc6cda3 JA |
2701 | * Exit Status:: The status returned by commands and how Bash |
2702 | interprets it. | |
ccc6cda3 JA |
2703 | * Signals:: What happens when Bash or a command it runs |
2704 | receives a signal. | |
2705 | @end menu | |
2706 | ||
cce855bc JA |
2707 | @node Simple Command Expansion |
2708 | @subsection Simple Command Expansion | |
2709 | @cindex command expansion | |
2710 | ||
2711 | When a simple command is executed, the shell performs the following | |
2712 | expansions, assignments, and redirections, from left to right. | |
2713 | ||
2714 | @enumerate | |
2715 | @item | |
2716 | The words that the parser has marked as variable assignments (those | |
2717 | preceding the command name) and redirections are saved for later | |
2718 | processing. | |
2719 | ||
2720 | @item | |
2721 | The words that are not variable assignments or redirections are | |
2722 | expanded (@pxref{Shell Expansions}). | |
2723 | If any words remain after expansion, the first word | |
2724 | is taken to be the name of the command and the remaining words are | |
2725 | the arguments. | |
2726 | ||
2727 | @item | |
2728 | Redirections are performed as described above (@pxref{Redirections}). | |
2729 | ||
2730 | @item | |
2731 | The text after the @samp{=} in each variable assignment undergoes tilde | |
2732 | expansion, parameter expansion, command substitution, arithmetic expansion, | |
2733 | and quote removal before being assigned to the variable. | |
2734 | @end enumerate | |
2735 | ||
2736 | If no command name results, the variable assignments affect the current | |
2737 | shell environment. Otherwise, the variables are added to the environment | |
2738 | of the executed command and do not affect the current shell environment. | |
2739 | If any of the assignments attempts to assign a value to a readonly variable, | |
2740 | an error occurs, and the command exits with a non-zero status. | |
2741 | ||
2742 | If no command name results, redirections are performed, but do not | |
2743 | affect the current shell environment. A redirection error causes the | |
2744 | command to exit with a non-zero status. | |
2745 | ||
2746 | If there is a command name left after expansion, execution proceeds as | |
2747 | described below. Otherwise, the command exits. If one of the expansions | |
2748 | contained a command substitution, the exit status of the command is | |
2749 | the exit status of the last command substitution performed. If there | |
2750 | were no command substitutions, the command exits with a status of zero. | |
2751 | ||
ccc6cda3 JA |
2752 | @node Command Search and Execution |
2753 | @subsection Command Search and Execution | |
2754 | @cindex command execution | |
2755 | @cindex command search | |
2756 | ||
2757 | After a command has been split into words, if it results in a | |
2758 | simple command and an optional list of arguments, the following | |
2759 | actions are taken. | |
2760 | ||
2761 | @enumerate | |
2762 | @item | |
2763 | If the command name contains no slashes, the shell attempts to | |
2764 | locate it. If there exists a shell function by that name, that | |
bb70624e | 2765 | function is invoked as described in @ref{Shell Functions}. |
ccc6cda3 JA |
2766 | |
2767 | @item | |
2768 | If the name does not match a function, the shell searches for | |
2769 | it in the list of shell builtins. If a match is found, that | |
2770 | builtin is invoked. | |
2771 | ||
2772 | @item | |
2773 | If the name is neither a shell function nor a builtin, | |
2774 | and contains no slashes, Bash searches each element of | |
28ef6c31 | 2775 | @env{$PATH} for a directory containing an executable file |
ccc6cda3 | 2776 | by that name. Bash uses a hash table to remember the full |
28ef6c31 | 2777 | pathnames of executable files to avoid multiple @env{PATH} searches |
cce855bc | 2778 | (see the description of @code{hash} in @ref{Bourne Shell Builtins}). |
28ef6c31 | 2779 | A full search of the directories in @env{$PATH} |
ccc6cda3 | 2780 | is performed only if the command is not found in the hash table. |
245a493c CR |
2781 | If the search is unsuccessful, the shell searches for a defined shell |
2782 | function named @code{command_not_found_handle}. | |
2783 | If that function exists, it is invoked with the original command and | |
2784 | the original command's arguments as its arguments, and the function's | |
2785 | exit status becomes the exit status of the shell. | |
2786 | If that function is not defined, the shell prints an error | |
cce855bc | 2787 | message and returns an exit status of 127. |
ccc6cda3 JA |
2788 | |
2789 | @item | |
2790 | If the search is successful, or if the command name contains | |
cce855bc JA |
2791 | one or more slashes, the shell executes the named program in |
2792 | a separate execution environment. | |
ccc6cda3 JA |
2793 | Argument 0 is set to the name given, and the remaining arguments |
2794 | to the command are set to the arguments supplied, if any. | |
2795 | ||
2796 | @item | |
2797 | If this execution fails because the file is not in executable | |
cce855bc JA |
2798 | format, and the file is not a directory, it is assumed to be a |
2799 | @var{shell script} and the shell executes it as described in | |
2800 | @ref{Shell Scripts}. | |
2801 | ||
2802 | @item | |
2803 | If the command was not begun asynchronously, the shell waits for | |
2804 | the command to complete and collects its exit status. | |
2805 | ||
ccc6cda3 JA |
2806 | @end enumerate |
2807 | ||
cce855bc JA |
2808 | @node Command Execution Environment |
2809 | @subsection Command Execution Environment | |
2810 | @cindex execution environment | |
2811 | ||
2812 | The shell has an @var{execution environment}, which consists of the | |
2813 | following: | |
2814 | ||
2815 | @itemize @bullet | |
2816 | @item | |
2817 | open files inherited by the shell at invocation, as modified by | |
2818 | redirections supplied to the @code{exec} builtin | |
2819 | ||
2820 | @item | |
2821 | the current working directory as set by @code{cd}, @code{pushd}, or | |
2822 | @code{popd}, or inherited by the shell at invocation | |
2823 | ||
2824 | @item | |
2825 | the file creation mode mask as set by @code{umask} or inherited from | |
2826 | the shell's parent | |
2827 | ||
2828 | @item | |
2829 | current traps set by @code{trap} | |
2830 | ||
2831 | @item | |
2832 | shell parameters that are set by variable assignment or with @code{set} | |
2833 | or inherited from the shell's parent in the environment | |
2834 | ||
2835 | @item | |
2836 | shell functions defined during execution or inherited from the shell's | |
2837 | parent in the environment | |
2838 | ||
2839 | @item | |
2840 | options enabled at invocation (either by default or with command-line | |
2841 | arguments) or by @code{set} | |
2842 | ||
2843 | @item | |
d3ad40de | 2844 | options enabled by @code{shopt} (@pxref{The Shopt Builtin}) |
cce855bc JA |
2845 | |
2846 | @item | |
2847 | shell aliases defined with @code{alias} (@pxref{Aliases}) | |
2848 | ||
2849 | @item | |
bb70624e | 2850 | various process @sc{id}s, including those of background jobs |
cce855bc | 2851 | (@pxref{Lists}), the value of @code{$$}, and the value of |
28ef6c31 | 2852 | @env{$PPID} |
cce855bc JA |
2853 | |
2854 | @end itemize | |
2855 | ||
2856 | When a simple command other than a builtin or shell function | |
2857 | is to be executed, it | |
2858 | is invoked in a separate execution environment that consists of | |
2859 | the following. Unless otherwise noted, the values are inherited | |
2860 | from the shell. | |
2861 | ||
2862 | @itemize @bullet | |
2863 | @item | |
2864 | the shell's open files, plus any modifications and additions specified | |
2865 | by redirections to the command | |
2866 | ||
2867 | @item | |
2868 | the current working directory | |
2869 | ||
2870 | @item | |
2871 | the file creation mode mask | |
2872 | ||
2873 | @item | |
d3a24ed2 CR |
2874 | shell variables and functions marked for export, along with variables |
2875 | exported for the command, passed in the environment (@pxref{Environment}) | |
cce855bc JA |
2876 | |
2877 | @item | |
2878 | traps caught by the shell are reset to the values inherited from the | |
2879 | shell's parent, and traps ignored by the shell are ignored | |
2880 | ||
2881 | @end itemize | |
2882 | ||
2883 | A command invoked in this separate environment cannot affect the | |
2884 | shell's execution environment. | |
2885 | ||
d3a24ed2 CR |
2886 | Command substitution, commands grouped with parentheses, |
2887 | and asynchronous commands are invoked in a | |
cce855bc JA |
2888 | subshell environment that is a duplicate of the shell environment, |
2889 | except that traps caught by the shell are reset to the values | |
2890 | that the shell inherited from its parent at invocation. Builtin | |
2891 | commands that are invoked as part of a pipeline are also executed | |
2892 | in a subshell environment. Changes made to the subshell environment | |
2893 | cannot affect the shell's execution environment. | |
2894 | ||
012bac39 CR |
2895 | Subshells spawned to execute command substitutions inherit the value of |
2896 | the @option{-e} option from the parent shell. When not in @sc{posix} mode, | |
2897 | Bash clears the @option{-e} option in such subshells. | |
2898 | ||
f73dda09 JA |
2899 | If a command is followed by a @samp{&} and job control is not active, the |
2900 | default standard input for the command is the empty file @file{/dev/null}. | |
2901 | Otherwise, the invoked command inherits the file descriptors of the calling | |
2902 | shell as modified by redirections. | |
2903 | ||
ccc6cda3 JA |
2904 | @node Environment |
2905 | @subsection Environment | |
2906 | @cindex environment | |
2907 | ||
2908 | When a program is invoked it is given an array of strings | |
2909 | called the @var{environment}. | |
2910 | This is a list of name-value pairs, of the form @code{name=value}. | |
2911 | ||
bb70624e JA |
2912 | Bash provides several ways to manipulate the environment. |
2913 | On invocation, the shell scans its own environment and | |
ccc6cda3 JA |
2914 | creates a parameter for each name found, automatically marking |
2915 | it for @var{export} | |
2916 | to child processes. Executed commands inherit the environment. | |
2917 | The @code{export} and @samp{declare -x} | |
2918 | commands allow parameters and functions to be added to and | |
2919 | deleted from the environment. If the value of a parameter | |
2920 | in the environment is modified, the new value becomes part | |
2921 | of the environment, replacing the old. The environment | |
2922 | inherited by any executed command consists of the shell's | |
2923 | initial environment, whose values may be modified in the shell, | |
cce855bc JA |
2924 | less any pairs removed by the @code{unset} and @samp{export -n} |
2925 | commands, plus any additions via the @code{export} and | |
2926 | @samp{declare -x} commands. | |
ccc6cda3 JA |
2927 | |
2928 | The environment for any simple command | |
2929 | or function may be augmented temporarily by prefixing it with | |
2930 | parameter assignments, as described in @ref{Shell Parameters}. | |
2931 | These assignment statements affect only the environment seen | |
2932 | by that command. | |
2933 | ||
28ef6c31 | 2934 | If the @option{-k} option is set (@pxref{The Set Builtin}), then all |
ccc6cda3 JA |
2935 | parameter assignments are placed in the environment for a command, |
2936 | not just those that precede the command name. | |
2937 | ||
2938 | When Bash invokes an external command, the variable @samp{$_} | |
b28ff8c9 | 2939 | is set to the full pathname of the command and passed to that |
ccc6cda3 JA |
2940 | command in its environment. |
2941 | ||
2942 | @node Exit Status | |
2943 | @subsection Exit Status | |
2944 | @cindex exit status | |
2945 | ||
35ee8ea0 CR |
2946 | The exit status of an executed command is the value returned by the |
2947 | @var{waitpid} system call or equivalent function. Exit statuses | |
2948 | fall between 0 and 255, though, as explained below, the shell may | |
2949 | use values above 125 specially. Exit statuses from shell builtins and | |
b28ff8c9 | 2950 | compound commands are also limited to this range. Under certain |
35ee8ea0 CR |
2951 | circumstances, the shell will use special values to indicate specific |
2952 | failure modes. | |
2953 | ||
cce855bc | 2954 | For the shell's purposes, a command which exits with a |
ccc6cda3 JA |
2955 | zero exit status has succeeded. |
2956 | A non-zero exit status indicates failure. | |
2957 | This seemingly counter-intuitive scheme is used so there | |
2958 | is one well-defined way to indicate success and a variety of | |
2959 | ways to indicate various failure modes. | |
bb70624e JA |
2960 | When a command terminates on a fatal signal whose number is @var{N}, |
2961 | Bash uses the value 128+@var{N} as the exit status. | |
ccc6cda3 JA |
2962 | |
2963 | If a command is not found, the child process created to | |
2964 | execute it returns a status of 127. If a command is found | |
2965 | but is not executable, the return status is 126. | |
2966 | ||
cce855bc JA |
2967 | If a command fails because of an error during expansion or redirection, |
2968 | the exit status is greater than zero. | |
2969 | ||
ccc6cda3 JA |
2970 | The exit status is used by the Bash conditional commands |
2971 | (@pxref{Conditional Constructs}) and some of the list | |
2972 | constructs (@pxref{Lists}). | |
2973 | ||
2974 | All of the Bash builtins return an exit status of zero if they succeed | |
2975 | and a non-zero status on failure, so they may be used by the | |
2976 | conditional and list constructs. | |
cce855bc | 2977 | All builtins return an exit status of 2 to indicate incorrect usage. |
ccc6cda3 JA |
2978 | |
2979 | @node Signals | |
2980 | @subsection Signals | |
2981 | @cindex signal handling | |
2982 | ||
cce855bc | 2983 | When Bash is interactive, in the absence of any traps, it ignores |
ccc6cda3 JA |
2984 | @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell), |
2985 | and @code{SIGINT} | |
2986 | is caught and handled (so that the @code{wait} builtin is interruptible). | |
2987 | When Bash receives a @code{SIGINT}, it breaks out of any executing loops. | |
2988 | In all cases, Bash ignores @code{SIGQUIT}. | |
2989 | If job control is in effect (@pxref{Job Control}), Bash | |
2990 | ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. | |
2991 | ||
5e13499c | 2992 | Non-builtin commands started by Bash have signal handlers set to the |
cce855bc JA |
2993 | values inherited by the shell from its parent. |
2994 | When job control is not in effect, asynchronous commands | |
5e13499c CR |
2995 | ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited |
2996 | handlers. | |
cce855bc JA |
2997 | Commands run as a result of |
2998 | command substitution ignore the keyboard-generated job control signals | |
ccc6cda3 JA |
2999 | @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. |
3000 | ||
3001 | The shell exits by default upon receipt of a @code{SIGHUP}. | |
f73dda09 JA |
3002 | Before exiting, an interactive shell resends the @code{SIGHUP} to |
3003 | all jobs, running or stopped. | |
cce855bc JA |
3004 | Stopped jobs are sent @code{SIGCONT} to ensure that they receive |
3005 | the @code{SIGHUP}. | |
3006 | To prevent the shell from sending the @code{SIGHUP} signal to a | |
3007 | particular job, it should be removed | |
3008 | from the jobs table with the @code{disown} | |
3009 | builtin (@pxref{Job Control Builtins}) or marked | |
3010 | to not receive @code{SIGHUP} using @code{disown -h}. | |
3011 | ||
3012 | If the @code{huponexit} shell option has been set with @code{shopt} | |
d3ad40de | 3013 | (@pxref{The Shopt Builtin}), Bash sends a @code{SIGHUP} to all jobs when |
cce855bc JA |
3014 | an interactive login shell exits. |
3015 | ||
5e13499c CR |
3016 | If Bash is waiting for a command to complete and receives a signal |
3017 | for which a trap has been set, the trap will not be executed until | |
3018 | the command completes. | |
cce855bc JA |
3019 | When Bash is waiting for an asynchronous |
3020 | command via the @code{wait} builtin, the reception of a signal for | |
3021 | which a trap has been set will cause the @code{wait} builtin to return | |
3022 | immediately with an exit status greater than 128, immediately after | |
3023 | which the trap is executed. | |
ccc6cda3 JA |
3024 | |
3025 | @node Shell Scripts | |
3026 | @section Shell Scripts | |
3027 | @cindex shell script | |
3028 | ||
3029 | A shell script is a text file containing shell commands. When such | |
3030 | a file is used as the first non-option argument when invoking Bash, | |
28ef6c31 | 3031 | and neither the @option{-c} nor @option{-s} option is supplied |
ccc6cda3 JA |
3032 | (@pxref{Invoking Bash}), |
3033 | Bash reads and executes commands from the file, then exits. This | |
f73dda09 JA |
3034 | mode of operation creates a non-interactive shell. The shell first |
3035 | searches for the file in the current directory, and looks in the | |
3036 | directories in @env{$PATH} if not found there. | |
3037 | ||
3038 | When Bash runs | |
ccc6cda3 JA |
3039 | a shell script, it sets the special parameter @code{0} to the name |
3040 | of the file, rather than the name of the shell, and the positional | |
3041 | parameters are set to the remaining arguments, if any are given. | |
3042 | If no additional arguments are supplied, the positional parameters | |
3043 | are unset. | |
3044 | ||
3045 | A shell script may be made executable by using the @code{chmod} command | |
3046 | to turn on the execute bit. When Bash finds such a file while | |
28ef6c31 | 3047 | searching the @env{$PATH} for a command, it spawns a subshell to |
ccc6cda3 JA |
3048 | execute it. In other words, executing |
3049 | @example | |
3050 | filename @var{arguments} | |
3051 | @end example | |
3052 | @noindent | |
3053 | is equivalent to executing | |
3054 | @example | |
3055 | bash filename @var{arguments} | |
3056 | @end example | |
3057 | ||
3058 | @noindent | |
3059 | if @code{filename} is an executable shell script. | |
3060 | This subshell reinitializes itself, so that the effect is as if a | |
cce855bc JA |
3061 | new shell had been invoked to interpret the script, with the |
3062 | exception that the locations of commands remembered by the parent | |
3063 | (see the description of @code{hash} in @ref{Bourne Shell Builtins}) | |
3064 | are retained by the child. | |
ccc6cda3 | 3065 | |
bb70624e | 3066 | Most versions of Unix make this a part of the operating system's command |
ccc6cda3 JA |
3067 | execution mechanism. If the first line of a script begins with |
3068 | the two characters @samp{#!}, the remainder of the line specifies | |
bb70624e JA |
3069 | an interpreter for the program. |
3070 | Thus, you can specify Bash, @code{awk}, Perl, or some other | |
3071 | interpreter and write the rest of the script file in that language. | |
3072 | ||
3073 | The arguments to the interpreter | |
ccc6cda3 JA |
3074 | consist of a single optional argument following the interpreter |
3075 | name on the first line of the script file, followed by the name of | |
3076 | the script file, followed by the rest of the arguments. Bash | |
3077 | will perform this action on operating systems that do not handle it | |
3078 | themselves. Note that some older versions of Unix limit the interpreter | |
3079 | name and argument to a maximum of 32 characters. | |
3080 | ||
bb70624e JA |
3081 | Bash scripts often begin with @code{#! /bin/bash} (assuming that |
3082 | Bash has been installed in @file{/bin}), since this ensures that | |
3083 | Bash will be used to interpret the script, even if it is executed | |
3084 | under another shell. | |
3085 | ||
3086 | @node Shell Builtin Commands | |
3087 | @chapter Shell Builtin Commands | |
ccc6cda3 JA |
3088 | |
3089 | @menu | |
3090 | * Bourne Shell Builtins:: Builtin commands inherited from the Bourne | |
3091 | Shell. | |
bb70624e | 3092 | * Bash Builtins:: Table of builtins specific to Bash. |
d3ad40de CR |
3093 | * Modifying Shell Behavior:: Builtins to modify shell attributes and |
3094 | optional behavior. | |
bb70624e | 3095 | * Special Builtins:: Builtin commands classified specially by |
ac18b312 | 3096 | POSIX. |
ccc6cda3 JA |
3097 | @end menu |
3098 | ||
bb70624e JA |
3099 | Builtin commands are contained within the shell itself. |
3100 | When the name of a builtin command is used as the first word of | |
3101 | a simple command (@pxref{Simple Commands}), the shell executes | |
3102 | the command directly, without invoking another program. | |
3103 | Builtin commands are necessary to implement functionality impossible | |
3104 | or inconvenient to obtain with separate utilities. | |
3105 | ||
dc8fbaf9 | 3106 | This section briefly describes the builtins which Bash inherits from |
bb70624e JA |
3107 | the Bourne Shell, as well as the builtin commands which are unique |
3108 | to or have been extended in Bash. | |
3109 | ||
3110 | Several builtin commands are described in other chapters: builtin | |
3111 | commands which provide the Bash interface to the job control | |
3112 | facilities (@pxref{Job Control Builtins}), the directory stack | |
3113 | (@pxref{Directory Stack Builtins}), the command history | |
3114 | (@pxref{Bash History Builtins}), and the programmable completion | |
3115 | facilities (@pxref{Programmable Completion Builtins}). | |
3116 | ||
3117 | Many of the builtins have been extended by @sc{posix} or Bash. | |
ccc6cda3 | 3118 | |
de8913bd CR |
3119 | Unless otherwise noted, each builtin command documented as accepting |
3120 | options preceded by @samp{-} accepts @samp{--} | |
3121 | to signify the end of the options. | |
6932f7f5 CR |
3122 | The @code{:}, @code{true}, @code{false}, and @code{test} |
3123 | builtins do not accept options and do not treat @samp{--} specially. | |
3124 | The @code{exit}, @code{logout}, @code{break}, @code{continue}, @code{let}, | |
3125 | and @code{shift} builtins accept and process arguments beginning | |
3126 | with @samp{-} without requiring @samp{--}. | |
3127 | Other builtins that accept arguments but are not specified as accepting | |
3128 | options interpret arguments beginning with @samp{-} as invalid options and | |
3129 | require @samp{--} to prevent this interpretation. | |
de8913bd | 3130 | |
ccc6cda3 JA |
3131 | @node Bourne Shell Builtins |
3132 | @section Bourne Shell Builtins | |
3133 | ||
bb70624e | 3134 | The following shell builtin commands are inherited from the Bourne Shell. |
ac18b312 | 3135 | These commands are implemented as specified by the @sc{posix} standard. |
ccc6cda3 JA |
3136 | |
3137 | @table @code | |
bb70624e | 3138 | @item : @r{(a colon)} |
ccc6cda3 JA |
3139 | @btindex : |
3140 | @example | |
3141 | : [@var{arguments}] | |
3142 | @end example | |
b28ff8c9 | 3143 | |
ccc6cda3 | 3144 | Do nothing beyond expanding @var{arguments} and performing redirections. |
cce855bc | 3145 | The return status is zero. |
ccc6cda3 | 3146 | |
bb70624e | 3147 | @item . @r{(a period)} |
ccc6cda3 JA |
3148 | @btindex . |
3149 | @example | |
b72432fd | 3150 | . @var{filename} [@var{arguments}] |
ccc6cda3 | 3151 | @end example |
b28ff8c9 | 3152 | |
ccc6cda3 | 3153 | Read and execute commands from the @var{filename} argument in the |
cce855bc | 3154 | current shell context. If @var{filename} does not contain a slash, |
28ef6c31 JA |
3155 | the @env{PATH} variable is used to find @var{filename}. |
3156 | When Bash is not in @sc{posix} mode, the current directory is searched | |
3157 | if @var{filename} is not found in @env{$PATH}. | |
b72432fd JA |
3158 | If any @var{arguments} are supplied, they become the positional |
3159 | parameters when @var{filename} is executed. Otherwise the positional | |
3160 | parameters are unchanged. | |
cce855bc JA |
3161 | The return status is the exit status of the last command executed, or |
3162 | zero if no commands are executed. If @var{filename} is not found, or | |
3163 | cannot be read, the return status is non-zero. | |
bb70624e | 3164 | This builtin is equivalent to @code{source}. |
ccc6cda3 JA |
3165 | |
3166 | @item break | |
3167 | @btindex break | |
3168 | @example | |
3169 | break [@var{n}] | |
3170 | @end example | |
b28ff8c9 | 3171 | |
ccc6cda3 JA |
3172 | Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop. |
3173 | If @var{n} is supplied, the @var{n}th enclosing loop is exited. | |
cce855bc JA |
3174 | @var{n} must be greater than or equal to 1. |
3175 | The return status is zero unless @var{n} is not greater than or equal to 1. | |
ccc6cda3 JA |
3176 | |
3177 | @item cd | |
3178 | @btindex cd | |
3179 | @example | |
220537f2 | 3180 | cd [-L|[-P [-e]]] [@var{directory}] |
ccc6cda3 | 3181 | @end example |
b28ff8c9 | 3182 | |
d3a24ed2 | 3183 | Change the current working directory to @var{directory}. |
2dead0c4 CR |
3184 | If @var{directory} is not supplied, the value of the @env{HOME} |
3185 | shell variable is used. | |
3186 | Any additional arguments following @var{directory} are ignored. | |
45c0f7f8 CR |
3187 | If the shell variable |
3188 | @env{CDPATH} exists, it is used as a search path: | |
3189 | each directory name in @env{CDPATH} is searched for | |
3190 | @var{directory}, with alternative directory names in @env{CDPATH} | |
3191 | separated by a colon (@samp{:}). | |
d3a24ed2 CR |
3192 | If @var{directory} begins with a slash, @env{CDPATH} is not used. |
3193 | ||
45c0f7f8 CR |
3194 | The @option{-P} option means to not follow symbolic links: symbolic links |
3195 | are resolved while @code{cd} is traversing @var{directory} and before | |
3196 | processing an instance of @samp{..} in @var{directory}. | |
3197 | ||
3198 | By default, or when the @option{-L} option is supplied, symbolic links | |
3199 | in @var{directory} are resolved after @code{cd} processes an instance | |
3200 | of @samp{..} in @var{directory}. | |
3201 | ||
3202 | If @samp{..} appears in @var{directory}, it is processed by removing the | |
3203 | immediately preceding pathname component, back to a slash or the beginning | |
3204 | of @var{directory}. | |
3205 | ||
220537f2 CR |
3206 | If the @option{-e} option is supplied with @option{-P} |
3207 | and the current working directory cannot be successfully determined | |
3208 | after a successful directory change, @code{cd} will return an unsuccessful | |
3209 | status. | |
19baff85 CR |
3210 | If @var{directory} is @samp{-}, it is converted to @env{$OLDPWD} |
3211 | before the directory change is attempted. | |
d3a24ed2 CR |
3212 | |
3213 | If a non-empty directory name from @env{CDPATH} is used, or if | |
3214 | @samp{-} is the first argument, and the directory change is | |
3215 | successful, the absolute pathname of the new working directory is | |
3216 | written to the standard output. | |
3217 | ||
cce855bc JA |
3218 | The return status is zero if the directory is successfully changed, |
3219 | non-zero otherwise. | |
ccc6cda3 JA |
3220 | |
3221 | @item continue | |
3222 | @btindex continue | |
3223 | @example | |
3224 | continue [@var{n}] | |
3225 | @end example | |
b28ff8c9 | 3226 | |
ccc6cda3 JA |
3227 | Resume the next iteration of an enclosing @code{for}, @code{while}, |
3228 | @code{until}, or @code{select} loop. | |
cce855bc JA |
3229 | If @var{n} is supplied, the execution of the @var{n}th enclosing loop |
3230 | is resumed. | |
3231 | @var{n} must be greater than or equal to 1. | |
3232 | The return status is zero unless @var{n} is not greater than or equal to 1. | |
ccc6cda3 JA |
3233 | |
3234 | @item eval | |
3235 | @btindex eval | |
3236 | @example | |
3237 | eval [@var{arguments}] | |
3238 | @end example | |
b28ff8c9 | 3239 | |
cce855bc JA |
3240 | The arguments are concatenated together into a single command, which is |
3241 | then read and executed, and its exit status returned as the exit status | |
3242 | of @code{eval}. | |
3243 | If there are no arguments or only empty arguments, the return status is | |
3244 | zero. | |
ccc6cda3 JA |
3245 | |
3246 | @item exec | |
3247 | @btindex exec | |
3248 | @example | |
cce855bc | 3249 | exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]] |
ccc6cda3 | 3250 | @end example |
b28ff8c9 | 3251 | |
ccc6cda3 | 3252 | If @var{command} |
cce855bc | 3253 | is supplied, it replaces the shell without creating a new process. |
28ef6c31 | 3254 | If the @option{-l} option is supplied, the shell places a dash at the |
d3ad40de | 3255 | beginning of the zeroth argument passed to @var{command}. |
ccc6cda3 | 3256 | This is what the @code{login} program does. |
28ef6c31 | 3257 | The @option{-c} option causes @var{command} to be executed with an empty |
cce855bc | 3258 | environment. |
28ef6c31 | 3259 | If @option{-a} is supplied, the shell passes @var{name} as the zeroth |
cce855bc | 3260 | argument to @var{command}. |
b28ff8c9 CR |
3261 | If @var{command} |
3262 | cannot be executed for some reason, a non-interactive shell exits, | |
3263 | unless the @code{execfail} shell option | |
3264 | is enabled. In that case, it returns failure. | |
3265 | An interactive shell returns failure if the file cannot be executed. | |
ccc6cda3 | 3266 | If no @var{command} is specified, redirections may be used to affect |
cce855bc JA |
3267 | the current shell environment. If there are no redirection errors, the |
3268 | return status is zero; otherwise the return status is non-zero. | |
ccc6cda3 JA |
3269 | |
3270 | @item exit | |
3271 | @btindex exit | |
3272 | @example | |
3273 | exit [@var{n}] | |
3274 | @end example | |
b28ff8c9 | 3275 | |
ccc6cda3 | 3276 | Exit the shell, returning a status of @var{n} to the shell's parent. |
bb70624e | 3277 | If @var{n} is omitted, the exit status is that of the last command executed. |
cce855bc | 3278 | Any trap on @code{EXIT} is executed before the shell terminates. |
ccc6cda3 JA |
3279 | |
3280 | @item export | |
3281 | @btindex export | |
3282 | @example | |
3283 | export [-fn] [-p] [@var{name}[=@var{value}]] | |
3284 | @end example | |
b28ff8c9 | 3285 | |
ccc6cda3 | 3286 | Mark each @var{name} to be passed to child processes |
28ef6c31 | 3287 | in the environment. If the @option{-f} option is supplied, the @var{name}s |
cce855bc | 3288 | refer to shell functions; otherwise the names refer to shell variables. |
28ef6c31 JA |
3289 | The @option{-n} option means to no longer mark each @var{name} for export. |
3290 | If no @var{names} are supplied, or if the @option{-p} option is given, a | |
b28ff8c9 | 3291 | list of names of all exported variables is displayed. |
28ef6c31 | 3292 | The @option{-p} option displays output in a form that may be reused as input. |
d3a24ed2 CR |
3293 | If a variable name is followed by =@var{value}, the value of |
3294 | the variable is set to @var{value}. | |
3295 | ||
cce855bc | 3296 | The return status is zero unless an invalid option is supplied, one of |
28ef6c31 | 3297 | the names is not a valid shell variable name, or @option{-f} is supplied |
cce855bc | 3298 | with a name that is not a shell function. |
ccc6cda3 JA |
3299 | |
3300 | @item getopts | |
3301 | @btindex getopts | |
3302 | @example | |
3303 | getopts @var{optstring} @var{name} [@var{args}] | |
3304 | @end example | |
b28ff8c9 | 3305 | |
ccc6cda3 | 3306 | @code{getopts} is used by shell scripts to parse positional parameters. |
bb70624e JA |
3307 | @var{optstring} contains the option characters to be recognized; if a |
3308 | character is followed by a colon, the option is expected to have an | |
b28ff8c9 | 3309 | argument, which should be separated from it by whitespace. |
bb70624e JA |
3310 | The colon (@samp{:}) and question mark (@samp{?}) may not be |
3311 | used as option characters. | |
ccc6cda3 JA |
3312 | Each time it is invoked, @code{getopts} |
3313 | places the next option in the shell variable @var{name}, initializing | |
3314 | @var{name} if it does not exist, | |
3315 | and the index of the next argument to be processed into the | |
28ef6c31 JA |
3316 | variable @env{OPTIND}. |
3317 | @env{OPTIND} is initialized to 1 each time the shell or a shell script | |
cce855bc JA |
3318 | is invoked. |
3319 | When an option requires an argument, | |
28ef6c31 JA |
3320 | @code{getopts} places that argument into the variable @env{OPTARG}. |
3321 | The shell does not reset @env{OPTIND} automatically; it must be manually | |
cce855bc JA |
3322 | reset between multiple calls to @code{getopts} within the same shell |
3323 | invocation if a new set of parameters is to be used. | |
3324 | ||
3325 | When the end of options is encountered, @code{getopts} exits with a | |
3326 | return value greater than zero. | |
28ef6c31 | 3327 | @env{OPTIND} is set to the index of the first non-option argument, |
dc60d4e0 | 3328 | and @var{name} is set to @samp{?}. |
cce855bc JA |
3329 | |
3330 | @code{getopts} | |
3331 | normally parses the positional parameters, but if more arguments are | |
3332 | given in @var{args}, @code{getopts} parses those instead. | |
ccc6cda3 JA |
3333 | |
3334 | @code{getopts} can report errors in two ways. If the first character of | |
3335 | @var{optstring} is a colon, @var{silent} | |
b28ff8c9 | 3336 | error reporting is used. In normal operation, diagnostic messages |
cce855bc | 3337 | are printed when invalid options or missing option arguments are |
ccc6cda3 | 3338 | encountered. |
28ef6c31 | 3339 | If the variable @env{OPTERR} |
cce855bc | 3340 | is set to 0, no error messages will be displayed, even if the first |
ccc6cda3 JA |
3341 | character of @code{optstring} is not a colon. |
3342 | ||
cce855bc | 3343 | If an invalid option is seen, |
ccc6cda3 | 3344 | @code{getopts} places @samp{?} into @var{name} and, if not silent, |
28ef6c31 | 3345 | prints an error message and unsets @env{OPTARG}. |
ccc6cda3 | 3346 | If @code{getopts} is silent, the option character found is placed in |
28ef6c31 | 3347 | @env{OPTARG} and no diagnostic message is printed. |
ccc6cda3 JA |
3348 | |
3349 | If a required argument is not found, and @code{getopts} | |
3350 | is not silent, a question mark (@samp{?}) is placed in @var{name}, | |
3351 | @code{OPTARG} is unset, and a diagnostic message is printed. | |
3352 | If @code{getopts} is silent, then a colon (@samp{:}) is placed in | |
28ef6c31 | 3353 | @var{name} and @env{OPTARG} is set to the option character found. |
ccc6cda3 | 3354 | |
ccc6cda3 JA |
3355 | @item hash |
3356 | @btindex hash | |
3357 | @example | |
11a6f9a9 | 3358 | hash [-r] [-p @var{filename}] [-dt] [@var{name}] |
ccc6cda3 | 3359 | @end example |
b28ff8c9 | 3360 | |
4691dc6b CR |
3361 | Each time @code{hash} is invoked, it remembers the full pathnames of the |
3362 | commands specified as @var{name} arguments, | |
cce855bc JA |
3363 | so they need not be searched for on subsequent invocations. |
3364 | The commands are found by searching through the directories listed in | |
28ef6c31 | 3365 | @env{$PATH}. |
4691dc6b | 3366 | Any previously-remembered pathname is discarded. |
28ef6c31 | 3367 | The @option{-p} option inhibits the path search, and @var{filename} is |
cce855bc | 3368 | used as the location of @var{name}. |
28ef6c31 | 3369 | The @option{-r} option causes the shell to forget all remembered locations. |
7117c2d2 JA |
3370 | The @option{-d} option causes the shell to forget the remembered location |
3371 | of each @var{name}. | |
f73dda09 JA |
3372 | If the @option{-t} option is supplied, the full pathname to which each |
3373 | @var{name} corresponds is printed. If multiple @var{name} arguments are | |
3374 | supplied with @option{-t} the @var{name} is printed before the hashed | |
3375 | full pathname. | |
7117c2d2 JA |
3376 | The @option{-l} option causes output to be displayed in a format |
3377 | that may be reused as input. | |
3378 | If no arguments are given, or if only @option{-l} is supplied, | |
3379 | information about remembered commands is printed. | |
cce855bc JA |
3380 | The return status is zero unless a @var{name} is not found or an invalid |
3381 | option is supplied. | |
ccc6cda3 JA |
3382 | |
3383 | @item pwd | |
3384 | @btindex pwd | |
3385 | @example | |
3386 | pwd [-LP] | |
3387 | @end example | |
b28ff8c9 | 3388 | |
bb70624e | 3389 | Print the absolute pathname of the current working directory. |
28ef6c31 | 3390 | If the @option{-P} option is supplied, the pathname printed will not |
cce855bc | 3391 | contain symbolic links. |
28ef6c31 | 3392 | If the @option{-L} option is supplied, the pathname printed may contain |
cce855bc JA |
3393 | symbolic links. |
3394 | The return status is zero unless an error is encountered while | |
3395 | determining the name of the current directory or an invalid option | |
3396 | is supplied. | |
ccc6cda3 JA |
3397 | |
3398 | @item readonly | |
3399 | @btindex readonly | |
3400 | @example | |
54a1fa7c | 3401 | readonly [-aAf] [-p] [@var{name}[=@var{value}]] @dots{} |
ccc6cda3 | 3402 | @end example |
b28ff8c9 | 3403 | |
cce855bc JA |
3404 | Mark each @var{name} as readonly. |
3405 | The values of these names may not be changed by subsequent assignment. | |
28ef6c31 | 3406 | If the @option{-f} option is supplied, each @var{name} refers to a shell |
cce855bc | 3407 | function. |
fdf670ea CR |
3408 | The @option{-a} option means each @var{name} refers to an indexed |
3409 | array variable; the @option{-A} option means each @var{name} refers | |
3410 | to an associative array variable. | |
54a1fa7c | 3411 | If both options are supplied, @option{-A} takes precedence. |
28ef6c31 | 3412 | If no @var{name} arguments are given, or if the @option{-p} |
ccc6cda3 | 3413 | option is supplied, a list of all readonly names is printed. |
54a1fa7c CR |
3414 | The other options may be used to restrict the output to a subset of |
3415 | the set of readonly names. | |
28ef6c31 | 3416 | The @option{-p} option causes output to be displayed in a format that |
cce855bc | 3417 | may be reused as input. |
d3a24ed2 CR |
3418 | If a variable name is followed by =@var{value}, the value of |
3419 | the variable is set to @var{value}. | |
cce855bc JA |
3420 | The return status is zero unless an invalid option is supplied, one of |
3421 | the @var{name} arguments is not a valid shell variable or function name, | |
28ef6c31 | 3422 | or the @option{-f} option is supplied with a name that is not a shell function. |
ccc6cda3 JA |
3423 | |
3424 | @item return | |
3425 | @btindex return | |
3426 | @example | |
3427 | return [@var{n}] | |
3428 | @end example | |
b28ff8c9 CR |
3429 | |
3430 | Cause a shell function to stop executing and return the value @var{n} | |
3431 | to its caller. | |
bb70624e JA |
3432 | If @var{n} is not supplied, the return value is the exit status of the |
3433 | last command executed in the function. | |
b28ff8c9 CR |
3434 | @code{return} may also be used to terminate execution of a script |
3435 | being executed with the @code{.} (@code{source}) builtin, | |
3436 | returning either @var{n} or | |
bb70624e JA |
3437 | the exit status of the last command executed within the script as the exit |
3438 | status of the script. | |
3d4f66ca CR |
3439 | If @var{n} is supplied, the return value is its least significant |
3440 | 8 bits. | |
d3a24ed2 CR |
3441 | Any command associated with the @code{RETURN} trap is executed |
3442 | before execution resumes after the function or script. | |
3d4f66ca CR |
3443 | The return status is non-zero if @code{return} is supplied a non-numeric |
3444 | argument or is used outside a function | |
bb70624e | 3445 | and not during the execution of a script by @code{.} or @code{source}. |
ccc6cda3 JA |
3446 | |
3447 | @item shift | |
3448 | @btindex shift | |
3449 | @example | |
3450 | shift [@var{n}] | |
3451 | @end example | |
b28ff8c9 | 3452 | |
cce855bc JA |
3453 | Shift the positional parameters to the left by @var{n}. |
3454 | The positional parameters from @var{n}+1 @dots{} @code{$#} are | |
d3ad40de CR |
3455 | renamed to @code{$1} @dots{} @code{$#}-@var{n}. |
3456 | Parameters represented by the numbers @code{$#} to @code{$#}-@var{n}+1 | |
3457 | are unset. | |
cce855bc JA |
3458 | @var{n} must be a non-negative number less than or equal to @code{$#}. |
3459 | If @var{n} is zero or greater than @code{$#}, the positional parameters | |
3460 | are not changed. | |
bb70624e | 3461 | If @var{n} is not supplied, it is assumed to be 1. |
cce855bc JA |
3462 | The return status is zero unless @var{n} is greater than @code{$#} or |
3463 | less than zero, non-zero otherwise. | |
ccc6cda3 | 3464 | |
df0e4bfe | 3465 | @item test |
ccc6cda3 JA |
3466 | @itemx [ |
3467 | @btindex test | |
3468 | @btindex [ | |
b28ff8c9 CR |
3469 | @example |
3470 | test @var{expr} | |
3471 | @end example | |
3472 | ||
df0e4bfe CR |
3473 | Evaluate a conditional express |
3474 | ion @var{expr} and return a status of 0 | |
b28ff8c9 | 3475 | (true) or 1 (false). |
cce855bc JA |
3476 | Each operator and operand must be a separate argument. |
3477 | Expressions are composed of the primaries described below in | |
3478 | @ref{Bash Conditional Expressions}. | |
3ffb039a CR |
3479 | @code{test} does not accept any options, nor does it accept and ignore |
3480 | an argument of @option{--} as signifying the end of options. | |
cce855bc | 3481 | |
bb70624e JA |
3482 | When the @code{[} form is used, the last argument to the command must |
3483 | be a @code{]}. | |
3484 | ||
cce855bc JA |
3485 | Expressions may be combined using the following operators, listed in |
3486 | decreasing order of precedence. | |
641d8f00 | 3487 | The evaluation depends on the number of arguments; see below. |
06dff54a | 3488 | Operator precedence is used when there are five or more arguments. |
cce855bc JA |
3489 | |
3490 | @table @code | |
3491 | @item ! @var{expr} | |
3492 | True if @var{expr} is false. | |
3493 | ||
3494 | @item ( @var{expr} ) | |
3495 | Returns the value of @var{expr}. | |
3496 | This may be used to override the normal precedence of operators. | |
3497 | ||
3498 | @item @var{expr1} -a @var{expr2} | |
3499 | True if both @var{expr1} and @var{expr2} are true. | |
3500 | ||
3501 | @item @var{expr1} -o @var{expr2} | |
3502 | True if either @var{expr1} or @var{expr2} is true. | |
3503 | @end table | |
3504 | ||
3505 | The @code{test} and @code{[} builtins evaluate conditional | |
3506 | expressions using a set of rules based on the number of arguments. | |
3507 | ||
3508 | @table @asis | |
3509 | @item 0 arguments | |
3510 | The expression is false. | |
3511 | ||
3512 | @item 1 argument | |
3513 | The expression is true if and only if the argument is not null. | |
3514 | ||
3515 | @item 2 arguments | |
3516 | If the first argument is @samp{!}, the expression is true if and | |
3517 | only if the second argument is null. | |
3518 | If the first argument is one of the unary conditional operators | |
3519 | (@pxref{Bash Conditional Expressions}), the expression | |
3520 | is true if the unary test is true. | |
3521 | If the first argument is not a valid unary operator, the expression is | |
3522 | false. | |
3523 | ||
3524 | @item 3 arguments | |
adc6cff5 | 3525 | The following conditions are applied in the order listed. |
cce855bc JA |
3526 | If the second argument is one of the binary conditional |
3527 | operators (@pxref{Bash Conditional Expressions}), the | |
3528 | result of the expression is the result of the binary test using the | |
3529 | first and third arguments as operands. | |
641d8f00 CR |
3530 | The @samp{-a} and @samp{-o} operators are considered binary operators |
3531 | when there are three arguments. | |
cce855bc JA |
3532 | If the first argument is @samp{!}, the value is the negation of |
3533 | the two-argument test using the second and third arguments. | |
3534 | If the first argument is exactly @samp{(} and the third argument is | |
3535 | exactly @samp{)}, the result is the one-argument test of the second | |
3536 | argument. | |
3537 | Otherwise, the expression is false. | |
cce855bc JA |
3538 | |
3539 | @item 4 arguments | |
3540 | If the first argument is @samp{!}, the result is the negation of | |
3541 | the three-argument expression composed of the remaining arguments. | |
3542 | Otherwise, the expression is parsed and evaluated according to | |
3543 | precedence using the rules listed above. | |
3544 | ||
3545 | @item 5 or more arguments | |
3546 | The expression is parsed and evaluated according to precedence | |
3547 | using the rules listed above. | |
3548 | @end table | |
ccc6cda3 | 3549 | |
54a1fa7c CR |
3550 | When used with @code{test} or @samp{[}, the @samp{<} and @samp{>} |
3551 | operators sort lexicographically using ASCII ordering. | |
3552 | ||
ccc6cda3 JA |
3553 | @item times |
3554 | @btindex times | |
3555 | @example | |
3556 | times | |
3557 | @end example | |
b28ff8c9 | 3558 | |
ccc6cda3 | 3559 | Print out the user and system times used by the shell and its children. |
cce855bc | 3560 | The return status is zero. |
ccc6cda3 JA |
3561 | |
3562 | @item trap | |
3563 | @btindex trap | |
3564 | @example | |
d166f048 | 3565 | trap [-lp] [@var{arg}] [@var{sigspec} @dots{}] |
ccc6cda3 | 3566 | @end example |
b28ff8c9 | 3567 | |
ccc6cda3 | 3568 | The commands in @var{arg} are to be read and executed when the |
61deeb13 CR |
3569 | shell receives signal @var{sigspec}. If @var{arg} is absent (and |
3570 | there is a single @var{sigspec}) or | |
3571 | equal to @samp{-}, each specified signal's disposition is reset | |
3572 | to the value it had when the shell was started. | |
d166f048 JA |
3573 | If @var{arg} is the null string, then the signal specified by |
3574 | each @var{sigspec} is ignored by the shell and commands it invokes. | |
28ef6c31 | 3575 | If @var{arg} is not present and @option{-p} has been supplied, |
bb70624e JA |
3576 | the shell displays the trap commands associated with each @var{sigspec}. |
3577 | If no arguments are supplied, or | |
28ef6c31 | 3578 | only @option{-p} is given, @code{trap} prints the list of commands |
cce855bc JA |
3579 | associated with each signal number in a form that may be reused as |
3580 | shell input. | |
d3a24ed2 CR |
3581 | The @option{-l} option causes the shell to print a list of signal names |
3582 | and their corresponding numbers. | |
61deeb13 CR |
3583 | Each @var{sigspec} is either a signal name or a signal number. |
3584 | Signal names are case insensitive and the @code{SIG} prefix is optional. | |
4301bca7 | 3585 | |
d166f048 JA |
3586 | If a @var{sigspec} |
3587 | is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits. | |
3588 | If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed | |
d3a24ed2 CR |
3589 | before every simple command, @code{for} command, @code{case} command, |
3590 | @code{select} command, every arithmetic @code{for} command, and before | |
3591 | the first command executes in a shell function. | |
bf6bd355 | 3592 | Refer to the description of the @code{extdebug} option to the |
d3ad40de | 3593 | @code{shopt} builtin (@pxref{The Shopt Builtin}) for details of its |
d3a24ed2 | 3594 | effect on the @code{DEBUG} trap. |
4301bca7 CR |
3595 | If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed |
3596 | each time a shell function or a script executed with the @code{.} or | |
3597 | @code{source} builtins finishes executing. | |
3598 | ||
f73dda09 | 3599 | If a @var{sigspec} is @code{ERR}, the command @var{arg} |
5e13499c CR |
3600 | is executed whenever a simple command has a non-zero exit status, |
3601 | subject to the following conditions. | |
d3a24ed2 CR |
3602 | The @code{ERR} trap is not executed if the failed command is part of the |
3603 | command list immediately following an @code{until} or @code{while} keyword, | |
4b9cc222 | 3604 | part of the test following the @code{if} or @code{elif} reserved words, |
9d85af6d CR |
3605 | part of a command executed in a @code{&&} or @code{||} list, |
3606 | or if the command's return | |
f73dda09 | 3607 | status is being inverted using @code{!}. |
5e13499c | 3608 | These are the same conditions obeyed by the @code{errexit} option. |
ccc6cda3 JA |
3609 | |
3610 | Signals ignored upon entry to the shell cannot be trapped or reset. | |
d3ad40de | 3611 | Trapped signals that are not being ignored are reset to their original |
4301bca7 | 3612 | values in a subshell or subshell environment when one is created. |
ccc6cda3 | 3613 | |
cce855bc JA |
3614 | The return status is zero unless a @var{sigspec} does not specify a |
3615 | valid signal. | |
3616 | ||
ccc6cda3 JA |
3617 | @item umask |
3618 | @btindex umask | |
3619 | @example | |
cce855bc | 3620 | umask [-p] [-S] [@var{mode}] |
ccc6cda3 | 3621 | @end example |
b28ff8c9 | 3622 | |
ccc6cda3 JA |
3623 | Set the shell process's file creation mask to @var{mode}. If |
3624 | @var{mode} begins with a digit, it is interpreted as an octal number; | |
3625 | if not, it is interpreted as a symbolic mode mask similar | |
3626 | to that accepted by the @code{chmod} command. If @var{mode} is | |
28ef6c31 | 3627 | omitted, the current value of the mask is printed. If the @option{-S} |
ccc6cda3 JA |
3628 | option is supplied without a @var{mode} argument, the mask is printed |
3629 | in a symbolic format. | |
28ef6c31 | 3630 | If the @option{-p} option is supplied, and @var{mode} |
cce855bc JA |
3631 | is omitted, the output is in a form that may be reused as input. |
3632 | The return status is zero if the mode is successfully changed or if | |
3633 | no @var{mode} argument is supplied, and non-zero otherwise. | |
ccc6cda3 | 3634 | |
bb70624e JA |
3635 | Note that when the mode is interpreted as an octal number, each number |
3636 | of the umask is subtracted from @code{7}. Thus, a umask of @code{022} | |
3637 | results in permissions of @code{755}. | |
3638 | ||
ccc6cda3 JA |
3639 | @item unset |
3640 | @btindex unset | |
3641 | @example | |
87c1f4ec | 3642 | unset [-fnv] [@var{name}] |
ccc6cda3 | 3643 | @end example |
b28ff8c9 CR |
3644 | |
3645 | Remove each variable or function @var{name}. | |
276cb932 CR |
3646 | If the @option{-v} option is given, each |
3647 | @var{name} refers to a shell variable and that variable is remvoved. | |
28ef6c31 | 3648 | If the @option{-f} option is given, the @var{name}s refer to shell |
ccc6cda3 | 3649 | functions, and the function definition is removed. |
87c1f4ec CR |
3650 | If the @option{-n} option is supplied, and @var{name} is a variable with |
3651 | the @var{nameref} attribute, @var{name} will be unset rather than the | |
3652 | variable it references. | |
3653 | @option{-n} has no effect if the @option{-f} option is supplied. | |
276cb932 CR |
3654 | If no options are supplied, each @var{name} refers to a variable; if |
3655 | there is no variable by that name, any function with that name is | |
3656 | unset. | |
cce855bc | 3657 | Readonly variables and functions may not be unset. |
d3a24ed2 | 3658 | The return status is zero unless a @var{name} is readonly. |
ccc6cda3 JA |
3659 | @end table |
3660 | ||
bb70624e JA |
3661 | @node Bash Builtins |
3662 | @section Bash Builtin Commands | |
ccc6cda3 | 3663 | |
bb70624e JA |
3664 | This section describes builtin commands which are unique to |
3665 | or have been extended in Bash. | |
ac18b312 | 3666 | Some of these commands are specified in the @sc{posix} standard. |
ccc6cda3 | 3667 | |
bb70624e | 3668 | @table @code |
ccc6cda3 | 3669 | |
bb70624e JA |
3670 | @item alias |
3671 | @btindex alias | |
3672 | @example | |
b28ff8c9 | 3673 | alias [-p] [@var{name}[=@var{value}] @dots{}] |
bb70624e | 3674 | @end example |
ccc6cda3 | 3675 | |
28ef6c31 | 3676 | Without arguments or with the @option{-p} option, @code{alias} prints |
bb70624e JA |
3677 | the list of aliases on the standard output in a form that allows |
3678 | them to be reused as input. | |
3679 | If arguments are supplied, an alias is defined for each @var{name} | |
3680 | whose @var{value} is given. If no @var{value} is given, the name | |
3681 | and value of the alias is printed. | |
3682 | Aliases are described in @ref{Aliases}. | |
ccc6cda3 | 3683 | |
bb70624e JA |
3684 | @item bind |
3685 | @btindex bind | |
3686 | @example | |
ba4ab055 | 3687 | bind [-m @var{keymap}] [-lpsvPSVX] |
bb70624e JA |
3688 | bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}] |
3689 | bind [-m @var{keymap}] -f @var{filename} | |
3690 | bind [-m @var{keymap}] -x @var{keyseq:shell-command} | |
3691 | bind [-m @var{keymap}] @var{keyseq:function-name} | |
7117c2d2 | 3692 | bind @var{readline-command} |
bb70624e | 3693 | @end example |
ccc6cda3 | 3694 | |
bb70624e | 3695 | Display current Readline (@pxref{Command Line Editing}) |
7117c2d2 JA |
3696 | key and function bindings, |
3697 | bind a key sequence to a Readline function or macro, | |
3698 | or set a Readline variable. | |
3699 | Each non-option argument is a command as it would appear in a | |
d3ad40de | 3700 | Readline initialization file (@pxref{Readline Init File}), |
7117c2d2 | 3701 | but each binding or command must be passed as a separate argument; e.g., |
bb70624e | 3702 | @samp{"\C-x\C-r":re-read-init-file}. |
6a8fd0ed | 3703 | |
bb70624e | 3704 | Options, if supplied, have the following meanings: |
cce855bc | 3705 | |
bb70624e JA |
3706 | @table @code |
3707 | @item -m @var{keymap} | |
3708 | Use @var{keymap} as the keymap to be affected by | |
3709 | the subsequent bindings. Acceptable @var{keymap} | |
3710 | names are | |
3711 | @code{emacs}, | |
3712 | @code{emacs-standard}, | |
3713 | @code{emacs-meta}, | |
3714 | @code{emacs-ctlx}, | |
3715 | @code{vi}, | |
28ef6c31 | 3716 | @code{vi-move}, |
bb70624e JA |
3717 | @code{vi-command}, and |
3718 | @code{vi-insert}. | |
3719 | @code{vi} is equivalent to @code{vi-command}; | |
3720 | @code{emacs} is equivalent to @code{emacs-standard}. | |
cce855bc | 3721 | |
bb70624e JA |
3722 | @item -l |
3723 | List the names of all Readline functions. | |
cce855bc | 3724 | |
bb70624e JA |
3725 | @item -p |
3726 | Display Readline function names and bindings in such a way that they | |
3727 | can be used as input or in a Readline initialization file. | |
cce855bc | 3728 | |
bb70624e JA |
3729 | @item -P |
3730 | List current Readline function names and bindings. | |
ccc6cda3 | 3731 | |
bb70624e JA |
3732 | @item -v |
3733 | Display Readline variable names and values in such a way that they | |
3734 | can be used as input or in a Readline initialization file. | |
ccc6cda3 | 3735 | |
bb70624e JA |
3736 | @item -V |
3737 | List current Readline variable names and values. | |
ccc6cda3 | 3738 | |
bb70624e JA |
3739 | @item -s |
3740 | Display Readline key sequences bound to macros and the strings they output | |
3741 | in such a way that they can be used as input or in a Readline | |
3742 | initialization file. | |
ccc6cda3 | 3743 | |
bb70624e JA |
3744 | @item -S |
3745 | Display Readline key sequences bound to macros and the strings they output. | |
ccc6cda3 | 3746 | |
bb70624e JA |
3747 | @item -f @var{filename} |
3748 | Read key bindings from @var{filename}. | |
ccc6cda3 | 3749 | |
bb70624e JA |
3750 | @item -q @var{function} |
3751 | Query about which keys invoke the named @var{function}. | |
ccc6cda3 | 3752 | |
bb70624e JA |
3753 | @item -u @var{function} |
3754 | Unbind all keys bound to the named @var{function}. | |
cce855bc | 3755 | |
bb70624e JA |
3756 | @item -r @var{keyseq} |
3757 | Remove any current binding for @var{keyseq}. | |
ccc6cda3 | 3758 | |
bb70624e JA |
3759 | @item -x @var{keyseq:shell-command} |
3760 | Cause @var{shell-command} to be executed whenever @var{keyseq} is | |
3761 | entered. | |
8943768b CR |
3762 | When @var{shell-command} is executed, the shell sets the |
3763 | @code{READLINE_LINE} variable to the contents of the Readline line | |
3764 | buffer and the @code{READLINE_POINT} variable to the current location | |
3765 | of the insertion point. | |
3766 | If the executed command changes the value of @code{READLINE_LINE} or | |
3767 | @code{READLINE_POINT}, those new values will be reflected in the | |
3768 | editing state. | |
ba4ab055 CR |
3769 | |
3770 | @item -X | |
3771 | List all key sequences bound to shell commands and the associated commands | |
3772 | in a format that can be reused as input. | |
bb70624e | 3773 | @end table |
ccc6cda3 | 3774 | |
bb70624e JA |
3775 | @noindent |
3776 | The return status is zero unless an invalid option is supplied or an | |
3777 | error occurs. | |
ccc6cda3 | 3778 | |
bb70624e JA |
3779 | @item builtin |
3780 | @btindex builtin | |
3781 | @example | |
3782 | builtin [@var{shell-builtin} [@var{args}]] | |
3783 | @end example | |
b28ff8c9 | 3784 | |
bb70624e JA |
3785 | Run a shell builtin, passing it @var{args}, and return its exit status. |
3786 | This is useful when defining a shell function with the same | |
3787 | name as a shell builtin, retaining the functionality of the builtin within | |
3788 | the function. | |
3789 | The return status is non-zero if @var{shell-builtin} is not a shell | |
3790 | builtin command. | |
ccc6cda3 | 3791 | |
d3a24ed2 CR |
3792 | @item caller |
3793 | @btindex caller | |
3794 | @example | |
3795 | caller [@var{expr}] | |
3796 | @end example | |
b28ff8c9 | 3797 | |
d3a24ed2 | 3798 | Returns the context of any active subroutine call (a shell function or |
5e13499c | 3799 | a script executed with the @code{.} or @code{source} builtins). |
d3a24ed2 CR |
3800 | |
3801 | Without @var{expr}, @code{caller} displays the line number and source | |
3802 | filename of the current subroutine call. | |
3803 | If a non-negative integer is supplied as @var{expr}, @code{caller} | |
3804 | displays the line number, subroutine name, and source file corresponding | |
3805 | to that position in the current execution call stack. This extra | |
3806 | information may be used, for example, to print a stack trace. The | |
3807 | current frame is frame 0. | |
3808 | ||
3809 | The return value is 0 unless the shell is not executing a subroutine | |
3810 | call or @var{expr} does not correspond to a valid position in the | |
3811 | call stack. | |
3812 | ||
bb70624e JA |
3813 | @item command |
3814 | @btindex command | |
3815 | @example | |
3816 | command [-pVv] @var{command} [@var{arguments} @dots{}] | |
3817 | @end example | |
b28ff8c9 | 3818 | |
bb70624e JA |
3819 | Runs @var{command} with @var{arguments} ignoring any shell function |
3820 | named @var{command}. | |
3821 | Only shell builtin commands or commands found by searching the | |
28ef6c31 | 3822 | @env{PATH} are executed. |
bb70624e JA |
3823 | If there is a shell function named @code{ls}, running @samp{command ls} |
3824 | within the function will execute the external command @code{ls} | |
3825 | instead of calling the function recursively. | |
28ef6c31 | 3826 | The @option{-p} option means to use a default value for @env{PATH} |
bb70624e JA |
3827 | that is guaranteed to find all of the standard utilities. |
3828 | The return status in this case is 127 if @var{command} cannot be | |
3829 | found or an error occurred, and the exit status of @var{command} | |
3830 | otherwise. | |
cce855bc | 3831 | |
28ef6c31 JA |
3832 | If either the @option{-V} or @option{-v} option is supplied, a |
3833 | description of @var{command} is printed. The @option{-v} option | |
bb70624e | 3834 | causes a single word indicating the command or file name used to |
28ef6c31 | 3835 | invoke @var{command} to be displayed; the @option{-V} option produces |
bb70624e JA |
3836 | a more verbose description. In this case, the return status is |
3837 | zero if @var{command} is found, and non-zero if not. | |
cce855bc | 3838 | |
bb70624e JA |
3839 | @item declare |
3840 | @btindex declare | |
3841 | @example | |
87c1f4ec | 3842 | declare [-aAfFgilnrtux] [-p] [@var{name}[=@var{value}] @dots{}] |
bb70624e | 3843 | @end example |
ccc6cda3 | 3844 | |
bb70624e JA |
3845 | Declare variables and give them attributes. If no @var{name}s |
3846 | are given, then display the values of variables instead. | |
ccc6cda3 | 3847 | |
28ef6c31 | 3848 | The @option{-p} option will display the attributes and values of each |
d3a24ed2 | 3849 | @var{name}. |
6fbe7620 CR |
3850 | When @option{-p} is used with @var{name} arguments, additional options |
3851 | are ignored. | |
3852 | ||
3853 | When @option{-p} is supplied without @var{name} arguments, @code{declare} | |
3854 | will display the attributes and values of all variables having the | |
3855 | attributes specified by the additional options. | |
3856 | If no other options are supplied with @option{-p}, @code{declare} will | |
3857 | display the attributes and values of all shell variables. The @option{-f} | |
3858 | option will restrict the display to shell functions. | |
3859 | ||
28ef6c31 | 3860 | The @option{-F} option inhibits the display of function definitions; |
d3a24ed2 CR |
3861 | only the function name and attributes are printed. |
3862 | If the @code{extdebug} shell option is enabled using @code{shopt} | |
d3ad40de | 3863 | (@pxref{The Shopt Builtin}), the source file name and line number where |
d3a24ed2 CR |
3864 | the function is defined are displayed as well. |
3865 | @option{-F} implies @option{-f}. | |
6faad625 CR |
3866 | |
3867 | The @option{-g} option forces variables to be created or modified at | |
54a1fa7c | 3868 | the global scope, even when @code{declare} is executed in a shell function. |
6faad625 CR |
3869 | It is ignored in all other cases. |
3870 | ||
d3a24ed2 CR |
3871 | The following options can be used to restrict output to variables with |
3872 | the specified attributes or to give variables attributes: | |
cce855bc | 3873 | |
bb70624e JA |
3874 | @table @code |
3875 | @item -a | |
fdf670ea CR |
3876 | Each @var{name} is an indexed array variable (@pxref{Arrays}). |
3877 | ||
3878 | @item -A | |
3879 | Each @var{name} is an associative array variable (@pxref{Arrays}). | |
cce855bc | 3880 | |
bb70624e JA |
3881 | @item -f |
3882 | Use function names only. | |
ccc6cda3 | 3883 | |
bb70624e JA |
3884 | @item -i |
3885 | The variable is to be treated as | |
3886 | an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is | |
3887 | performed when the variable is assigned a value. | |
ccc6cda3 | 3888 | |
09767ff0 CR |
3889 | @item -l |
3890 | When the variable is assigned a value, all upper-case characters are | |
3891 | converted to lower-case. | |
3892 | The upper-case attribute is disabled. | |
3893 | ||
87c1f4ec CR |
3894 | @item -n |
3895 | Give each @var{name} the @var{nameref} attribute, making | |
3896 | it a name reference to another variable. | |
3897 | That other variable is defined by the value of @var{name}. | |
3898 | All references and assignments to @var{name}, except for changing the | |
3899 | @option{-n} attribute itself, are performed on the variable referenced by | |
3900 | @var{name}'s value. | |
3901 | The @option{-n} attribute cannot be applied to array variables. | |
3902 | ||
bb70624e JA |
3903 | @item -r |
3904 | Make @var{name}s readonly. These names cannot then be assigned values | |
3905 | by subsequent assignment statements or unset. | |
ccc6cda3 | 3906 | |
7117c2d2 JA |
3907 | @item -t |
3908 | Give each @var{name} the @code{trace} attribute. | |
76a8d78d CR |
3909 | Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from |
3910 | the calling shell. | |
7117c2d2 JA |
3911 | The trace attribute has no special meaning for variables. |
3912 | ||
09767ff0 CR |
3913 | @item -u |
3914 | When the variable is assigned a value, all lower-case characters are | |
3915 | converted to upper-case. | |
3916 | The lower-case attribute is disabled. | |
3917 | ||
bb70624e JA |
3918 | @item -x |
3919 | Mark each @var{name} for export to subsequent commands via | |
3920 | the environment. | |
3921 | @end table | |
ccc6cda3 | 3922 | |
d3ad40de CR |
3923 | Using @samp{+} instead of @samp{-} turns off the attribute instead, |
3924 | with the exceptions that @samp{+a} | |
3925 | may not be used to destroy an array variable and @samp{+r} will not | |
3926 | remove the readonly attribute. | |
bb70624e | 3927 | When used in a function, @code{declare} makes each @var{name} local, |
b28ff8c9 | 3928 | as with the @code{local} command, unless the @option{-g} option is used. |
6faad625 CR |
3929 | If a variable name is followed by =@var{value}, the value of the variable |
3930 | is set to @var{value}. | |
ccc6cda3 | 3931 | |
bb70624e JA |
3932 | The return status is zero unless an invalid option is encountered, |
3933 | an attempt is made to define a function using @samp{-f foo=bar}, | |
3934 | an attempt is made to assign a value to a readonly variable, | |
3935 | an attempt is made to assign a value to an array variable without | |
3936 | using the compound assignment syntax (@pxref{Arrays}), | |
3937 | one of the @var{names} is not a valid shell variable name, | |
3938 | an attempt is made to turn off readonly status for a readonly variable, | |
3939 | an attempt is made to turn off array status for an array variable, | |
28ef6c31 | 3940 | or an attempt is made to display a non-existent function with @option{-f}. |
ccc6cda3 | 3941 | |
bb70624e JA |
3942 | @item echo |
3943 | @btindex echo | |
3944 | @example | |
3945 | echo [-neE] [@var{arg} @dots{}] | |
3946 | @end example | |
b28ff8c9 | 3947 | |
bb70624e JA |
3948 | Output the @var{arg}s, separated by spaces, terminated with a |
3949 | newline. | |
f6da9f85 | 3950 | The return status is 0 unless a write error occurs. |
28ef6c31 JA |
3951 | If @option{-n} is specified, the trailing newline is suppressed. |
3952 | If the @option{-e} option is given, interpretation of the following | |
bb70624e | 3953 | backslash-escaped characters is enabled. |
28ef6c31 | 3954 | The @option{-E} option disables the interpretation of these escape characters, |
bb70624e JA |
3955 | even on systems where they are interpreted by default. |
3956 | The @code{xpg_echo} shell option may be used to | |
3957 | dynamically determine whether or not @code{echo} expands these | |
3958 | escape characters by default. | |
3ffb039a CR |
3959 | @code{echo} does not interpret @option{--} to mean the end of options. |
3960 | ||
bb70624e JA |
3961 | @code{echo} interprets the following escape sequences: |
3962 | @table @code | |
3963 | @item \a | |
3964 | alert (bell) | |
3965 | @item \b | |
3966 | backspace | |
3967 | @item \c | |
2e4498b3 | 3968 | suppress further output |
bb70624e | 3969 | @item \e |
9ec5ed66 | 3970 | @itemx \E |
bb70624e JA |
3971 | escape |
3972 | @item \f | |
3973 | form feed | |
3974 | @item \n | |
3975 | new line | |
3976 | @item \r | |
3977 | carriage return | |
3978 | @item \t | |
3979 | horizontal tab | |
3980 | @item \v | |
3981 | vertical tab | |
3982 | @item \\ | |
3983 | backslash | |
7117c2d2 JA |
3984 | @item \0@var{nnn} |
3985 | the eight-bit character whose value is the octal value @var{nnn} | |
3986 | (zero to three octal digits) | |
f73dda09 JA |
3987 | @item \x@var{HH} |
3988 | the eight-bit character whose value is the hexadecimal value @var{HH} | |
3989 | (one or two hex digits) | |
eb0b2ad8 CR |
3990 | @item \u@var{HHHH} |
3991 | the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value | |
3992 | @var{HHHH} (one to four hex digits) | |
3993 | @item \U@var{HHHHHHHH} | |
3994 | the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value | |
3995 | @var{HHHHHHHH} (one to eight hex digits) | |
bb70624e | 3996 | @end table |
ccc6cda3 | 3997 | |
bb70624e JA |
3998 | @item enable |
3999 | @btindex enable | |
4000 | @example | |
d3ad40de | 4001 | enable [-a] [-dnps] [-f @var{filename}] [@var{name} @dots{}] |
bb70624e | 4002 | @end example |
b28ff8c9 | 4003 | |
bb70624e JA |
4004 | Enable and disable builtin shell commands. |
4005 | Disabling a builtin allows a disk command which has the same name | |
4006 | as a shell builtin to be executed without specifying a full pathname, | |
4007 | even though the shell normally searches for builtins before disk commands. | |
28ef6c31 | 4008 | If @option{-n} is used, the @var{name}s become disabled. Otherwise |
bb70624e | 4009 | @var{name}s are enabled. For example, to use the @code{test} binary |
28ef6c31 | 4010 | found via @env{$PATH} instead of the shell builtin version, type |
bb70624e | 4011 | @samp{enable -n test}. |
ccc6cda3 | 4012 | |
28ef6c31 | 4013 | If the @option{-p} option is supplied, or no @var{name} arguments appear, |
bb70624e JA |
4014 | a list of shell builtins is printed. With no other arguments, the list |
4015 | consists of all enabled shell builtins. | |
28ef6c31 | 4016 | The @option{-a} option means to list |
bb70624e | 4017 | each builtin with an indication of whether or not it is enabled. |
ccc6cda3 | 4018 | |
28ef6c31 | 4019 | The @option{-f} option means to load the new builtin command @var{name} |
bb70624e | 4020 | from shared object @var{filename}, on systems that support dynamic loading. |
28ef6c31 | 4021 | The @option{-d} option will delete a builtin loaded with @option{-f}. |
ccc6cda3 | 4022 | |
bb70624e | 4023 | If there are no options, a list of the shell builtins is displayed. |
28ef6c31 JA |
4024 | The @option{-s} option restricts @code{enable} to the @sc{posix} special |
4025 | builtins. If @option{-s} is used with @option{-f}, the new builtin becomes | |
bb70624e | 4026 | a special builtin (@pxref{Special Builtins}). |
ccc6cda3 | 4027 | |
bb70624e JA |
4028 | The return status is zero unless a @var{name} is not a shell builtin |
4029 | or there is an error loading a new builtin from a shared object. | |
cce855bc | 4030 | |
bb70624e JA |
4031 | @item help |
4032 | @btindex help | |
4033 | @example | |
6a8fd0ed | 4034 | help [-dms] [@var{pattern}] |
bb70624e | 4035 | @end example |
b28ff8c9 | 4036 | |
bb70624e JA |
4037 | Display helpful information about builtin commands. |
4038 | If @var{pattern} is specified, @code{help} gives detailed help | |
4039 | on all commands matching @var{pattern}, otherwise a list of | |
4040 | the builtins is printed. | |
6a8fd0ed CR |
4041 | |
4042 | Options, if supplied, have the following meanings: | |
4043 | ||
4044 | @table @code | |
4045 | @item -d | |
4046 | Display a short description of each @var{pattern} | |
4047 | @item -m | |
4048 | Display the description of each @var{pattern} in a manpage-like format | |
4049 | @item -s | |
4050 | Display only a short usage synopsis for each @var{pattern} | |
4051 | @end table | |
4052 | ||
bb70624e | 4053 | The return status is zero unless no command matches @var{pattern}. |
ccc6cda3 | 4054 | |
bb70624e JA |
4055 | @item let |
4056 | @btindex let | |
4057 | @example | |
b28ff8c9 | 4058 | let @var{expression} [@var{expression} @dots{}] |
bb70624e | 4059 | @end example |
b28ff8c9 | 4060 | |
bb70624e JA |
4061 | The @code{let} builtin allows arithmetic to be performed on shell |
4062 | variables. Each @var{expression} is evaluated according to the | |
4063 | rules given below in @ref{Shell Arithmetic}. If the | |
4064 | last @var{expression} evaluates to 0, @code{let} returns 1; | |
4065 | otherwise 0 is returned. | |
ccc6cda3 | 4066 | |
bb70624e JA |
4067 | @item local |
4068 | @btindex local | |
4069 | @example | |
d3a24ed2 | 4070 | local [@var{option}] @var{name}[=@var{value}] @dots{} |
bb70624e | 4071 | @end example |
b28ff8c9 | 4072 | |
bb70624e JA |
4073 | For each argument, a local variable named @var{name} is created, |
4074 | and assigned @var{value}. | |
4075 | The @var{option} can be any of the options accepted by @code{declare}. | |
4076 | @code{local} can only be used within a function; it makes the variable | |
4077 | @var{name} have a visible scope restricted to that function and its | |
4078 | children. The return status is zero unless @code{local} is used outside | |
4079 | a function, an invalid @var{name} is supplied, or @var{name} is a | |
4080 | readonly variable. | |
ccc6cda3 | 4081 | |
bb70624e JA |
4082 | @item logout |
4083 | @btindex logout | |
4084 | @example | |
4085 | logout [@var{n}] | |
4086 | @end example | |
b28ff8c9 | 4087 | |
bb70624e JA |
4088 | Exit a login shell, returning a status of @var{n} to the shell's |
4089 | parent. | |
cce855bc | 4090 | |
6a8fd0ed CR |
4091 | @item mapfile |
4092 | @btindex mapfile | |
4093 | @example | |
b28ff8c9 CR |
4094 | mapfile [-n @var{count}] [-O @var{origin}] [-s @var{count}] [-t] [-u @var{fd}] |
4095 | [-C @var{callback}] [-c @var{quantum}] [@var{array}] | |
6a8fd0ed | 4096 | @end example |
b28ff8c9 | 4097 | |
e1e48bba | 4098 | Read lines from the standard input into the indexed array variable @var{array}, |
6a8fd0ed CR |
4099 | or from file descriptor @var{fd} |
4100 | if the @option{-u} option is supplied. | |
4101 | The variable @code{MAPFILE} is the default @var{array}. | |
4102 | Options, if supplied, have the following meanings: | |
b28ff8c9 | 4103 | |
6a8fd0ed CR |
4104 | @table @code |
4105 | ||
4106 | @item -n | |
4107 | Copy at most @var{count} lines. If @var{count} is 0, all lines are copied. | |
4108 | @item -O | |
4109 | Begin assigning to @var{array} at index @var{origin}. | |
4110 | The default index is 0. | |
4111 | @item -s | |
4112 | Discard the first @var{count} lines read. | |
4113 | @item -t | |
984a1947 | 4114 | Remove a trailing newline from each line read. |
6a8fd0ed CR |
4115 | @item -u |
4116 | Read lines from file descriptor @var{fd} instead of the standard input. | |
4117 | @item -C | |
4118 | Evaluate @var{callback} each time @var{quantum}P lines are read. | |
4119 | The @option{-c} option specifies @var{quantum}. | |
4120 | @item -c | |
4121 | Specify the number of lines read between each call to @var{callback}. | |
4122 | @end table | |
4123 | ||
4124 | If @option{-C} is specified without @option{-c}, | |
4125 | the default quantum is 5000. | |
e141c35a | 4126 | When @var{callback} is evaluated, it is supplied the index of the next |
6faad625 CR |
4127 | array element to be assigned and the line to be assigned to that element |
4128 | as additional arguments. | |
e141c35a CR |
4129 | @var{callback} is evaluated after the line is read but before the |
4130 | array element is assigned. | |
6a8fd0ed CR |
4131 | |
4132 | If not supplied with an explicit origin, @code{mapfile} will clear @var{array} | |
4133 | before assigning to it. | |
4134 | ||
4135 | @code{mapfile} returns successfully unless an invalid option or option | |
e1e48bba CR |
4136 | argument is supplied, @var{array} is invalid or unassignable, or @var{array} |
4137 | is not an indexed array. | |
6a8fd0ed | 4138 | |
bb70624e JA |
4139 | @item printf |
4140 | @btindex printf | |
4141 | @example | |
9cbcc93b | 4142 | printf [-v @var{var}] @var{format} [@var{arguments}] |
bb70624e | 4143 | @end example |
b28ff8c9 | 4144 | |
bb70624e JA |
4145 | Write the formatted @var{arguments} to the standard output under the |
4146 | control of the @var{format}. | |
6faad625 CR |
4147 | The @option{-v} option causes the output to be assigned to the variable |
4148 | @var{var} rather than being printed to the standard output. | |
4149 | ||
bb70624e JA |
4150 | The @var{format} is a character string which contains three types of objects: |
4151 | plain characters, which are simply copied to standard output, character | |
4152 | escape sequences, which are converted and copied to the standard output, and | |
4153 | format specifications, each of which causes printing of the next successive | |
4154 | @var{argument}. | |
6faad625 CR |
4155 | In addition to the standard @code{printf(1)} formats, @code{printf} |
4156 | interprets the following extensions: | |
4157 | ||
4158 | @table @code | |
4159 | @item %b | |
b28ff8c9 | 4160 | Causes @code{printf} to expand backslash escape sequences in the |
6faad625 | 4161 | corresponding @var{argument}, |
b28ff8c9 | 4162 | except that @samp{\c} terminates output, backslashes in |
5e13499c | 4163 | @samp{\'}, @samp{\"}, and @samp{\?} are not removed, and octal escapes |
b28ff8c9 | 4164 | beginning with @samp{\0} may contain up to four digits. |
6faad625 | 4165 | @item %q |
b28ff8c9 | 4166 | Causes @code{printf} to output the |
bb70624e | 4167 | corresponding @var{argument} in a format that can be reused as shell input. |
6faad625 | 4168 | @item %(@var{datefmt})T |
b28ff8c9 | 4169 | Causes @code{printf} to output the date-time string resulting from using |
6faad625 CR |
4170 | @var{datefmt} as a format string for @code{strftime}(3). The corresponding |
4171 | @var{argument} is an integer representing the number of seconds since the | |
4172 | epoch. Two special argument values may be used: -1 represents the current | |
4173 | time, and -2 represents the time the shell was invoked. | |
4174 | @end table | |
cce855bc | 4175 | |
6faad625 | 4176 | @noindent |
112ff2a6 CR |
4177 | Arguments to non-string format specifiers are treated as C language constants, |
4178 | except that a leading plus or minus sign is allowed, and if the leading | |
4179 | character is a single or double quote, the value is the ASCII value of | |
4180 | the following character. | |
4181 | ||
bb70624e JA |
4182 | The @var{format} is reused as necessary to consume all of the @var{arguments}. |
4183 | If the @var{format} requires more @var{arguments} than are supplied, the | |
4184 | extra format specifications behave as if a zero value or null string, as | |
4185 | appropriate, had been supplied. The return value is zero on success, | |
4186 | non-zero on failure. | |
ccc6cda3 | 4187 | |
bb70624e JA |
4188 | @item read |
4189 | @btindex read | |
4190 | @example | |
b28ff8c9 CR |
4191 | read [-ers] [-a @var{aname}] [-d @var{delim}] [-i @var{text}] [-n @var{nchars}] |
4192 | [-N @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}] | |
bb70624e | 4193 | @end example |
b28ff8c9 | 4194 | |
7117c2d2 JA |
4195 | One line is read from the standard input, or from the file descriptor |
4196 | @var{fd} supplied as an argument to the @option{-u} option, and the first word | |
bb70624e JA |
4197 | is assigned to the first @var{name}, the second word to the second @var{name}, |
4198 | and so on, with leftover words and their intervening separators assigned | |
4199 | to the last @var{name}. | |
7117c2d2 | 4200 | If there are fewer words read from the input stream than names, |
bb70624e | 4201 | the remaining names are assigned empty values. |
28ef6c31 | 4202 | The characters in the value of the @env{IFS} variable |
bb70624e JA |
4203 | are used to split the line into words. |
4204 | The backslash character @samp{\} may be used to remove any special | |
4205 | meaning for the next character read and for line continuation. | |
4206 | If no names are supplied, the line read is assigned to the | |
28ef6c31 | 4207 | variable @env{REPLY}. |
7117c2d2 | 4208 | The return code is zero, unless end-of-file is encountered, @code{read} |
ba4ab055 CR |
4209 | times out (in which case the return code is greater than 128), |
4210 | a variable assignment error (such as assigning to a readonly variable) occurs, | |
4211 | or an invalid file descriptor is supplied as the argument to @option{-u}. | |
09767ff0 | 4212 | |
bb70624e | 4213 | Options, if supplied, have the following meanings: |
ccc6cda3 | 4214 | |
bb70624e JA |
4215 | @table @code |
4216 | @item -a @var{aname} | |
4217 | The words are assigned to sequential indices of the array variable | |
4218 | @var{aname}, starting at 0. | |
4219 | All elements are removed from @var{aname} before the assignment. | |
4220 | Other @var{name} arguments are ignored. | |
ccc6cda3 | 4221 | |
bb70624e JA |
4222 | @item -d @var{delim} |
4223 | The first character of @var{delim} is used to terminate the input line, | |
4224 | rather than newline. | |
ccc6cda3 | 4225 | |
bb70624e JA |
4226 | @item -e |
4227 | Readline (@pxref{Command Line Editing}) is used to obtain the line. | |
14e8b2a7 CR |
4228 | Readline uses the current (or default, if line editing was not previously |
4229 | active) editing settings. | |
ccc6cda3 | 4230 | |
1d0e1a34 CR |
4231 | @item -i @var{text} |
4232 | If Readline is being used to read the line, @var{text} is placed into | |
4233 | the editing buffer before editing begins. | |
4234 | ||
bb70624e JA |
4235 | @item -n @var{nchars} |
4236 | @code{read} returns after reading @var{nchars} characters rather than | |
08e72d7a CR |
4237 | waiting for a complete line of input, but honor a delimiter if fewer |
4238 | than @var{nchars} characters are read before the delimiter. | |
4239 | ||
4240 | @item -N @var{nchars} | |
4241 | @code{read} returns after reading exactly @var{nchars} characters rather | |
4242 | than waiting for a complete line of input, unless EOF is encountered or | |
a3143574 CR |
4243 | @code{read} times out. |
4244 | Delimiter characters encountered in the input are | |
4245 | not treated specially and do not cause @code{read} to return until | |
4246 | @var{nchars} characters are read. | |
ccc6cda3 | 4247 | |
bb70624e JA |
4248 | @item -p @var{prompt} |
4249 | Display @var{prompt}, without a trailing newline, before attempting | |
4250 | to read any input. | |
4251 | The prompt is displayed only if input is coming from a terminal. | |
ccc6cda3 | 4252 | |
bb70624e JA |
4253 | @item -r |
4254 | If this option is given, backslash does not act as an escape character. | |
4255 | The backslash is considered to be part of the line. | |
4256 | In particular, a backslash-newline pair may not be used as a line | |
4257 | continuation. | |
ccc6cda3 | 4258 | |
bb70624e JA |
4259 | @item -s |
4260 | Silent mode. If input is coming from a terminal, characters are | |
4261 | not echoed. | |
ccc6cda3 | 4262 | |
bb70624e JA |
4263 | @item -t @var{timeout} |
4264 | Cause @code{read} to time out and return failure if a complete line of | |
4265 | input is not read within @var{timeout} seconds. | |
4ac1ff98 CR |
4266 | @var{timeout} may be a decimal number with a fractional portion following |
4267 | the decimal point. | |
e33f2203 CR |
4268 | This option is only effective if @code{read} is reading input from a |
4269 | terminal, pipe, or other special file; it has no effect when reading | |
4270 | from regular files. | |
b13b8a87 CR |
4271 | If @var{timeout} is 0, @code{read} returns immediately, without trying to |
4272 | read and data. The exit status is 0 if input is available on | |
4273 | the specified file descriptor, non-zero otherwise. | |
e33f2203 | 4274 | The exit status is greater than 128 if the timeout is exceeded. |
ccc6cda3 | 4275 | |
7117c2d2 JA |
4276 | @item -u @var{fd} |
4277 | Read input from file descriptor @var{fd}. | |
bb70624e | 4278 | @end table |
ccc6cda3 | 4279 | |
e141c35a CR |
4280 | @item readarray |
4281 | @btindex readarray | |
4282 | @example | |
b28ff8c9 CR |
4283 | readarray [-n @var{count}] [-O @var{origin}] [-s @var{count}] [-t] [-u @var{fd}] |
4284 | [-C @var{callback}] [-c @var{quantum}] [@var{array}] | |
e141c35a | 4285 | @end example |
b28ff8c9 | 4286 | |
e1e48bba | 4287 | Read lines from the standard input into the indexed array variable @var{array}, |
e141c35a CR |
4288 | or from file descriptor @var{fd} |
4289 | if the @option{-u} option is supplied. | |
4290 | ||
4291 | A synonym for @code{mapfile}. | |
4292 | ||
d3ad40de CR |
4293 | @item source |
4294 | @btindex source | |
f37c27ea | 4295 | @example |
d3ad40de | 4296 | source @var{filename} |
f37c27ea | 4297 | @end example |
b28ff8c9 | 4298 | |
d3ad40de | 4299 | A synonym for @code{.} (@pxref{Bourne Shell Builtins}). |
ccc6cda3 | 4300 | |
d3ad40de CR |
4301 | @item type |
4302 | @btindex type | |
4303 | @example | |
4304 | type [-afptP] [@var{name} @dots{}] | |
4305 | @end example | |
b28ff8c9 | 4306 | |
d3ad40de CR |
4307 | For each @var{name}, indicate how it would be interpreted if used as a |
4308 | command name. | |
cce855bc | 4309 | |
d3ad40de CR |
4310 | If the @option{-t} option is used, @code{type} prints a single word |
4311 | which is one of @samp{alias}, @samp{function}, @samp{builtin}, | |
4312 | @samp{file} or @samp{keyword}, | |
4313 | if @var{name} is an alias, shell function, shell builtin, | |
4314 | disk file, or shell reserved word, respectively. | |
4315 | If the @var{name} is not found, then nothing is printed, and | |
4316 | @code{type} returns a failure status. | |
ccc6cda3 | 4317 | |
d3ad40de CR |
4318 | If the @option{-p} option is used, @code{type} either returns the name |
4319 | of the disk file that would be executed, or nothing if @option{-t} | |
4320 | would not return @samp{file}. | |
cce855bc | 4321 | |
d3ad40de CR |
4322 | The @option{-P} option forces a path search for each @var{name}, even if |
4323 | @option{-t} would not return @samp{file}. | |
ccc6cda3 | 4324 | |
d3ad40de | 4325 | If a command is hashed, @option{-p} and @option{-P} print the hashed value, |
b28ff8c9 | 4326 | which is not necessarily the file that appears first in @code{$PATH}. |
f37c27ea | 4327 | |
d3ad40de CR |
4328 | If the @option{-a} option is used, @code{type} returns all of the places |
4329 | that contain an executable named @var{file}. | |
4330 | This includes aliases and functions, if and only if the @option{-p} option | |
4331 | is not also used. | |
f37c27ea | 4332 | |
d3ad40de CR |
4333 | If the @option{-f} option is used, @code{type} does not attempt to find |
4334 | shell functions, as with the @code{command} builtin. | |
ccc6cda3 | 4335 | |
6a8fd0ed CR |
4336 | The return status is zero if all of the @var{names} are found, non-zero |
4337 | if any are not found. | |
ccc6cda3 | 4338 | |
d3ad40de CR |
4339 | @item typeset |
4340 | @btindex typeset | |
4341 | @example | |
87c1f4ec | 4342 | typeset [-afFgrxilnrtux] [-p] [@var{name}[=@var{value}] @dots{}] |
d3ad40de | 4343 | @end example |
b28ff8c9 | 4344 | |
d3ad40de | 4345 | The @code{typeset} command is supplied for compatibility with the Korn |
b28ff8c9 CR |
4346 | shell. |
4347 | It is a synonym for the @code{declare} builtin command. | |
cce855bc | 4348 | |
d3ad40de CR |
4349 | @item ulimit |
4350 | @btindex ulimit | |
4351 | @example | |
6fbe7620 | 4352 | ulimit [-abcdefilmnpqrstuvxHST] [@var{limit}] |
d3ad40de | 4353 | @end example |
b28ff8c9 | 4354 | |
d3ad40de CR |
4355 | @code{ulimit} provides control over the resources available to processes |
4356 | started by the shell, on systems that allow such control. If an | |
4357 | option is given, it is interpreted as follows: | |
b28ff8c9 | 4358 | |
d3ad40de CR |
4359 | @table @code |
4360 | @item -S | |
4361 | Change and report the soft limit associated with a resource. | |
ccc6cda3 | 4362 | |
d3ad40de CR |
4363 | @item -H |
4364 | Change and report the hard limit associated with a resource. | |
cce855bc | 4365 | |
d3ad40de CR |
4366 | @item -a |
4367 | All current limits are reported. | |
ccc6cda3 | 4368 | |
6fbe7620 CR |
4369 | @item -b |
4370 | The maximum socket buffer size. | |
4371 | ||
d3ad40de CR |
4372 | @item -c |
4373 | The maximum size of core files created. | |
d3a24ed2 | 4374 | |
d3ad40de CR |
4375 | @item -d |
4376 | The maximum size of a process's data segment. | |
d3a24ed2 | 4377 | |
d3ad40de CR |
4378 | @item -e |
4379 | The maximum scheduling priority ("nice"). | |
d3a24ed2 | 4380 | |
d3ad40de CR |
4381 | @item -f |
4382 | The maximum size of files written by the shell and its children. | |
2206f89a | 4383 | |
d3ad40de CR |
4384 | @item -i |
4385 | The maximum number of pending signals. | |
2206f89a | 4386 | |
d3ad40de CR |
4387 | @item -l |
4388 | The maximum size that may be locked into memory. | |
2206f89a | 4389 | |
d3ad40de | 4390 | @item -m |
a05a1337 | 4391 | The maximum resident set size (many systems do not honor this limit). |
d3a24ed2 | 4392 | |
d3ad40de | 4393 | @item -n |
a05a1337 CR |
4394 | The maximum number of open file descriptors (most systems do not |
4395 | allow this value to be set). | |
ccc6cda3 | 4396 | |
d3ad40de CR |
4397 | @item -p |
4398 | The pipe buffer size. | |
d3a24ed2 | 4399 | |
d3ad40de CR |
4400 | @item -q |
4401 | The maximum number of bytes in POSIX message queues. | |
d3a24ed2 | 4402 | |
d3ad40de CR |
4403 | @item -r |
4404 | The maximum real-time scheduling priority. | |
d3a24ed2 | 4405 | |
d3ad40de CR |
4406 | @item -s |
4407 | The maximum stack size. | |
d3a24ed2 | 4408 | |
d3ad40de CR |
4409 | @item -t |
4410 | The maximum amount of cpu time in seconds. | |
ccc6cda3 | 4411 | |
d3ad40de CR |
4412 | @item -u |
4413 | The maximum number of processes available to a single user. | |
ccc6cda3 | 4414 | |
d3ad40de | 4415 | @item -v |
68dfe178 CR |
4416 | The maximum amount of virtual memory available to the shell, and, on |
4417 | some systems, to its children. | |
ccc6cda3 | 4418 | |
d3ad40de CR |
4419 | @item -x |
4420 | The maximum number of file locks. | |
ccc6cda3 | 4421 | |
6fbe7620 CR |
4422 | @item -T |
4423 | The maximum number of threads. | |
d3ad40de | 4424 | @end table |
cce855bc | 4425 | |
b28ff8c9 CR |
4426 | If @var{limit} is given, and the @option{-a} option is not used, |
4427 | @var{limit} is the new value of the specified resource. | |
4428 | The special @var{limit} values @code{hard}, @code{soft}, and | |
d3ad40de CR |
4429 | @code{unlimited} stand for the current hard limit, the current soft limit, |
4430 | and no limit, respectively. | |
79e6c7dc CR |
4431 | A hard limit cannot be increased by a non-root user once it is set; |
4432 | a soft limit may be increased up to the value of the hard limit. | |
d3ad40de CR |
4433 | Otherwise, the current value of the soft limit for the specified resource |
4434 | is printed, unless the @option{-H} option is supplied. | |
4435 | When setting new limits, if neither @option{-H} nor @option{-S} is supplied, | |
4436 | both the hard and soft limits are set. | |
4437 | If no option is given, then @option{-f} is assumed. Values are in 1024-byte | |
b28ff8c9 CR |
4438 | increments, except for @option{-t}, which is in seconds; @option{-p}, |
4439 | which is in units of 512-byte blocks; and @option{-T}, @option{-b}, | |
4440 | @option{-n} and @option{-u}, which are unscaled values. | |
28157acd | 4441 | |
d3ad40de CR |
4442 | The return status is zero unless an invalid option or argument is supplied, |
4443 | or an error occurs while setting a new limit. | |
28157acd CR |
4444 | |
4445 | @item unalias | |
4446 | @btindex unalias | |
4447 | @example | |
4448 | unalias [-a] [@var{name} @dots{} ] | |
4449 | @end example | |
4450 | ||
4451 | Remove each @var{name} from the list of aliases. If @option{-a} is | |
4452 | supplied, all aliases are removed. | |
4453 | Aliases are described in @ref{Aliases}. | |
28157acd | 4454 | @end table |
ccc6cda3 | 4455 | |
d3ad40de CR |
4456 | @node Modifying Shell Behavior |
4457 | @section Modifying Shell Behavior | |
4458 | ||
4459 | @menu | |
4460 | * The Set Builtin:: Change the values of shell attributes and | |
4461 | positional parameters. | |
4462 | * The Shopt Builtin:: Modify shell optional behavior. | |
4463 | @end menu | |
4464 | ||
bb70624e | 4465 | @node The Set Builtin |
d3ad40de | 4466 | @subsection The Set Builtin |
ccc6cda3 | 4467 | |
d3ad40de CR |
4468 | This builtin is so complicated that it deserves its own section. @code{set} |
4469 | allows you to change the values of shell options and set the positional | |
4470 | parameters, or to display the names and values of shell variables. | |
ccc6cda3 | 4471 | |
bb70624e JA |
4472 | @table @code |
4473 | @item set | |
4474 | @btindex set | |
4475 | @example | |
dc60d4e0 CR |
4476 | set [--abefhkmnptuvxBCEHPT] [-o @var{option-name}] [@var{argument} @dots{}] |
4477 | set [+abefhkmnptuvxBCEHPT] [+o @var{option-name}] [@var{argument} @dots{}] | |
bb70624e | 4478 | @end example |
ccc6cda3 | 4479 | |
bb70624e JA |
4480 | If no options or arguments are supplied, @code{set} displays the names |
4481 | and values of all shell variables and functions, sorted according to the | |
54cdd75a CR |
4482 | current locale, in a format that may be reused as input |
4483 | for setting or resetting the currently-set variables. | |
4484 | Read-only variables cannot be reset. | |
4485 | In @sc{posix} mode, only shell variables are listed. | |
ccc6cda3 | 4486 | |
bb70624e JA |
4487 | When options are supplied, they set or unset shell attributes. |
4488 | Options, if specified, have the following meanings: | |
ccc6cda3 | 4489 | |
bb70624e JA |
4490 | @table @code |
4491 | @item -a | |
28ef6c31 JA |
4492 | Mark variables and function which are modified or created for export |
4493 | to the environment of subsequent commands. | |
ccc6cda3 | 4494 | |
bb70624e JA |
4495 | @item -b |
4496 | Cause the status of terminated background jobs to be reported | |
4497 | immediately, rather than before printing the next primary prompt. | |
ccc6cda3 | 4498 | |
bb70624e | 4499 | @item -e |
15825757 CR |
4500 | Exit immediately if |
4501 | a pipeline (@pxref{Pipelines}), which may consist of a single simple command | |
4502 | (@pxref{Simple Commands}), | |
4503 | a list (@pxref{Lists}), | |
4504 | or a compound command (@pxref{Compound Commands}) | |
a05a1337 CR |
4505 | returns a non-zero status. |
4506 | The shell does not exit if the command that fails is part of the | |
d3ad40de CR |
4507 | command list immediately following a @code{while} or @code{until} keyword, |
4508 | part of the test in an @code{if} statement, | |
a05a1337 CR |
4509 | part of any command executed in a @code{&&} or @code{||} list except |
4510 | the command following the final @code{&&} or @code{||}, | |
d3ad40de | 4511 | any command in a pipeline but the last, |
a05a1337 | 4512 | or if the command's return status is being inverted with @code{!}. |
15825757 CR |
4513 | If a compound command other than a subshell |
4514 | returns a non-zero status because a command failed | |
4515 | while @option{-e} was being ignored, the shell does not exit. | |
f73dda09 | 4516 | A trap on @code{ERR}, if set, is executed before the shell exits. |
ccc6cda3 | 4517 | |
a05a1337 CR |
4518 | This option applies to the shell environment and each subshell environment |
4519 | separately (@pxref{Command Execution Environment}), and may cause | |
4520 | subshells to exit before executing all the commands in the subshell. | |
4521 | ||
49cf7828 CR |
4522 | If a shell function executes in a context where @option{-e} is being ignored, |
4523 | even if @option{-e} is set, none of the commands executed within the function | |
4524 | body will be affected by the @option{-e} setting. | |
4525 | If a shell function sets @option{-e} while executing in a context where | |
4526 | @option{-e} is ignored, that setting will not have any effect until the | |
4527 | command containing the function call completes. | |
4528 | ||
bb70624e | 4529 | @item -f |
d0ca3503 | 4530 | Disable filename expansion (globbing). |
ccc6cda3 | 4531 | |
bb70624e JA |
4532 | @item -h |
4533 | Locate and remember (hash) commands as they are looked up for execution. | |
4534 | This option is enabled by default. | |
ccc6cda3 | 4535 | |
bb70624e JA |
4536 | @item -k |
4537 | All arguments in the form of assignment statements are placed | |
4538 | in the environment for a command, not just those that precede | |
4539 | the command name. | |
ccc6cda3 | 4540 | |
bb70624e JA |
4541 | @item -m |
4542 | Job control is enabled (@pxref{Job Control}). | |
f6da9f85 CR |
4543 | All processes run in a separate process group. |
4544 | When a background job completes, the shell prints a line | |
4545 | containing its exit status. | |
ccc6cda3 | 4546 | |
bb70624e JA |
4547 | @item -n |
4548 | Read commands but do not execute them; this may be used to check a | |
4549 | script for syntax errors. | |
4550 | This option is ignored by interactive shells. | |
b72432fd | 4551 | |
bb70624e | 4552 | @item -o @var{option-name} |
ccc6cda3 | 4553 | |
bb70624e | 4554 | Set the option corresponding to @var{option-name}: |
ccc6cda3 | 4555 | |
bb70624e JA |
4556 | @table @code |
4557 | @item allexport | |
4558 | Same as @code{-a}. | |
ccc6cda3 | 4559 | |
bb70624e JA |
4560 | @item braceexpand |
4561 | Same as @code{-B}. | |
ccc6cda3 | 4562 | |
bb70624e JA |
4563 | @item emacs |
4564 | Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}). | |
10a4e415 | 4565 | This also affects the editing interface used for @code{read -e}. |
d166f048 | 4566 | |
bb70624e JA |
4567 | @item errexit |
4568 | Same as @code{-e}. | |
d166f048 | 4569 | |
d3a24ed2 CR |
4570 | @item errtrace |
4571 | Same as @code{-E}. | |
4572 | ||
4573 | @item functrace | |
4574 | Same as @code{-T}. | |
4575 | ||
bb70624e JA |
4576 | @item hashall |
4577 | Same as @code{-h}. | |
ccc6cda3 | 4578 | |
bb70624e JA |
4579 | @item histexpand |
4580 | Same as @code{-H}. | |
ccc6cda3 | 4581 | |
bb70624e JA |
4582 | @item history |
4583 | Enable command history, as described in @ref{Bash History Facilities}. | |
4584 | This option is on by default in interactive shells. | |
ccc6cda3 | 4585 | |
bb70624e JA |
4586 | @item ignoreeof |
4587 | An interactive shell will not exit upon reading EOF. | |
ccc6cda3 | 4588 | |
bb70624e JA |
4589 | @item keyword |
4590 | Same as @code{-k}. | |
ccc6cda3 | 4591 | |
bb70624e JA |
4592 | @item monitor |
4593 | Same as @code{-m}. | |
ccc6cda3 | 4594 | |
bb70624e JA |
4595 | @item noclobber |
4596 | Same as @code{-C}. | |
ccc6cda3 | 4597 | |
28157acd CR |
4598 | @item noexec |
4599 | Same as @code{-n}. | |
f37c27ea | 4600 | |
28157acd CR |
4601 | @item noglob |
4602 | Same as @code{-f}. | |
f37c27ea | 4603 | |
28157acd CR |
4604 | @item nolog |
4605 | Currently ignored. | |
f37c27ea | 4606 | |
28157acd CR |
4607 | @item notify |
4608 | Same as @code{-b}. | |
f37c27ea | 4609 | |
28157acd CR |
4610 | @item nounset |
4611 | Same as @code{-u}. | |
f37c27ea | 4612 | |
28157acd CR |
4613 | @item onecmd |
4614 | Same as @code{-t}. | |
f37c27ea | 4615 | |
28157acd CR |
4616 | @item physical |
4617 | Same as @code{-P}. | |
f37c27ea | 4618 | |
28157acd CR |
4619 | @item pipefail |
4620 | If set, the return value of a pipeline is the value of the last | |
4621 | (rightmost) command to exit with a non-zero status, or zero if all | |
4622 | commands in the pipeline exit successfully. | |
4623 | This option is disabled by default. | |
f37c27ea | 4624 | |
28157acd CR |
4625 | @item posix |
4626 | Change the behavior of Bash where the default operation differs | |
4627 | from the @sc{posix} standard to match the standard | |
4628 | (@pxref{Bash POSIX Mode}). | |
4629 | This is intended to make Bash behave as a strict superset of that | |
4630 | standard. | |
f37c27ea | 4631 | |
28157acd CR |
4632 | @item privileged |
4633 | Same as @code{-p}. | |
f37c27ea | 4634 | |
28157acd CR |
4635 | @item verbose |
4636 | Same as @code{-v}. | |
f37c27ea | 4637 | |
28157acd CR |
4638 | @item vi |
4639 | Use a @code{vi}-style line editing interface. | |
10a4e415 | 4640 | This also affects the editing interface used for @code{read -e}. |
f37c27ea | 4641 | |
28157acd CR |
4642 | @item xtrace |
4643 | Same as @code{-x}. | |
4644 | @end table | |
f37c27ea | 4645 | |
28157acd CR |
4646 | @item -p |
4647 | Turn on privileged mode. | |
4648 | In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not | |
4649 | processed, shell functions are not inherited from the environment, | |
691aebcb CR |
4650 | and the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH} and @env{GLOBIGNORE} |
4651 | variables, if they appear in the environment, are ignored. | |
28157acd | 4652 | If the shell is started with the effective user (group) id not equal to the |
b28ff8c9 | 4653 | real user (group) id, and the @option{-p} option is not supplied, these actions |
28157acd | 4654 | are taken and the effective user id is set to the real user id. |
b28ff8c9 | 4655 | If the @option{-p} option is supplied at startup, the effective user id is |
28157acd CR |
4656 | not reset. |
4657 | Turning this option off causes the effective user | |
4658 | and group ids to be set to the real user and group ids. | |
f37c27ea | 4659 | |
28157acd CR |
4660 | @item -t |
4661 | Exit after reading and executing one command. | |
f37c27ea | 4662 | |
28157acd | 4663 | @item -u |
2c471a92 CR |
4664 | Treat unset variables and parameters other than the special parameters |
4665 | @samp{@@} or @samp{*} as an error when performing parameter expansion. | |
28157acd CR |
4666 | An error message will be written to the standard error, and a non-interactive |
4667 | shell will exit. | |
f37c27ea | 4668 | |
28157acd CR |
4669 | @item -v |
4670 | Print shell input lines as they are read. | |
f37c27ea | 4671 | |
28157acd CR |
4672 | @item -x |
4673 | Print a trace of simple commands, @code{for} commands, @code{case} | |
4674 | commands, @code{select} commands, and arithmetic @code{for} commands | |
4675 | and their arguments or associated word lists after they are | |
4676 | expanded and before they are executed. The value of the @env{PS4} | |
4677 | variable is expanded and the resultant value is printed before | |
4678 | the command and its expanded arguments. | |
f37c27ea | 4679 | |
28157acd CR |
4680 | @item -B |
4681 | The shell will perform brace expansion (@pxref{Brace Expansion}). | |
4682 | This option is on by default. | |
f37c27ea | 4683 | |
28157acd CR |
4684 | @item -C |
4685 | Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>} | |
4686 | from overwriting existing files. | |
f37c27ea | 4687 | |
28157acd CR |
4688 | @item -E |
4689 | If set, any trap on @code{ERR} is inherited by shell functions, command | |
4690 | substitutions, and commands executed in a subshell environment. | |
4691 | The @code{ERR} trap is normally not inherited in such cases. | |
f37c27ea | 4692 | |
28157acd CR |
4693 | @item -H |
4694 | Enable @samp{!} style history substitution (@pxref{History Interaction}). | |
4695 | This option is on by default for interactive shells. | |
f37c27ea | 4696 | |
28157acd | 4697 | @item -P |
45c0f7f8 | 4698 | If set, do not resolve symbolic links when performing commands such as |
28157acd CR |
4699 | @code{cd} which change the current directory. The physical directory |
4700 | is used instead. By default, Bash follows | |
4701 | the logical chain of directories when performing commands | |
4702 | which change the current directory. | |
f37c27ea | 4703 | |
28157acd CR |
4704 | For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys} |
4705 | then: | |
4706 | @example | |
4707 | $ cd /usr/sys; echo $PWD | |
4708 | /usr/sys | |
4709 | $ cd ..; pwd | |
4710 | /usr | |
4711 | @end example | |
f37c27ea | 4712 | |
28157acd CR |
4713 | @noindent |
4714 | If @code{set -P} is on, then: | |
4715 | @example | |
4716 | $ cd /usr/sys; echo $PWD | |
4717 | /usr/local/sys | |
4718 | $ cd ..; pwd | |
4719 | /usr/local | |
4720 | @end example | |
f37c27ea | 4721 | |
28157acd CR |
4722 | @item -T |
4723 | If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by | |
4724 | shell functions, command substitutions, and commands executed | |
4725 | in a subshell environment. | |
4726 | The @code{DEBUG} and @code{RETURN} traps are normally not inherited | |
4727 | in such cases. | |
f37c27ea | 4728 | |
28157acd CR |
4729 | @item -- |
4730 | If no arguments follow this option, then the positional parameters are | |
4731 | unset. Otherwise, the positional parameters are set to the | |
4732 | @var{arguments}, even if some of them begin with a @samp{-}. | |
f37c27ea | 4733 | |
28157acd CR |
4734 | @item - |
4735 | Signal the end of options, cause all remaining @var{arguments} | |
4736 | to be assigned to the positional parameters. The @option{-x} | |
4737 | and @option{-v} options are turned off. | |
4738 | If there are no arguments, the positional parameters remain unchanged. | |
f37c27ea CR |
4739 | @end table |
4740 | ||
28157acd CR |
4741 | Using @samp{+} rather than @samp{-} causes these options to be |
4742 | turned off. The options can also be used upon invocation of the | |
4743 | shell. The current set of options may be found in @code{$-}. | |
4744 | ||
4745 | The remaining N @var{arguments} are positional parameters and are | |
4746 | assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}. | |
4747 | The special parameter @code{#} is set to N. | |
f37c27ea | 4748 | |
28157acd | 4749 | The return status is always zero unless an invalid option is supplied. |
f37c27ea CR |
4750 | @end table |
4751 | ||
d3ad40de CR |
4752 | @node The Shopt Builtin |
4753 | @subsection The Shopt Builtin | |
4754 | ||
4755 | This builtin allows you to change additional shell optional behavior. | |
4756 | ||
4757 | @table @code | |
4758 | ||
4759 | @item shopt | |
4760 | @btindex shopt | |
4761 | @example | |
4762 | shopt [-pqsu] [-o] [@var{optname} @dots{}] | |
4763 | @end example | |
b28ff8c9 | 4764 | |
d3ad40de CR |
4765 | Toggle the values of variables controlling optional shell behavior. |
4766 | With no options, or with the @option{-p} option, a list of all settable | |
4767 | options is displayed, with an indication of whether or not each is set. | |
4768 | The @option{-p} option causes output to be displayed in a form that | |
4769 | may be reused as input. | |
4770 | Other options have the following meanings: | |
4771 | ||
4772 | @table @code | |
4773 | @item -s | |
4774 | Enable (set) each @var{optname}. | |
4775 | ||
4776 | @item -u | |
4777 | Disable (unset) each @var{optname}. | |
4778 | ||
4779 | @item -q | |
4780 | Suppresses normal output; the return status | |
4781 | indicates whether the @var{optname} is set or unset. | |
4782 | If multiple @var{optname} arguments are given with @option{-q}, | |
4783 | the return status is zero if all @var{optnames} are enabled; | |
4784 | non-zero otherwise. | |
4785 | ||
4786 | @item -o | |
4787 | Restricts the values of | |
4788 | @var{optname} to be those defined for the @option{-o} option to the | |
4789 | @code{set} builtin (@pxref{The Set Builtin}). | |
4790 | @end table | |
4791 | ||
4792 | If either @option{-s} or @option{-u} | |
b28ff8c9 | 4793 | is used with no @var{optname} arguments, @code{shopt} shows only |
d3ad40de CR |
4794 | those options which are set or unset, respectively. |
4795 | ||
4796 | Unless otherwise noted, the @code{shopt} options are disabled (off) | |
4797 | by default. | |
4798 | ||
4799 | The return status when listing options is zero if all @var{optnames} | |
4800 | are enabled, non-zero otherwise. When setting or unsetting options, | |
4801 | the return status is zero unless an @var{optname} is not a valid shell | |
4802 | option. | |
4803 | ||
4804 | The list of @code{shopt} options is: | |
4805 | @table @code | |
4806 | ||
4807 | @item autocd | |
4808 | If set, a command name that is the name of a directory is executed as if | |
4809 | it were the argument to the @code{cd} command. | |
4810 | This option is only used by interactive shells. | |
4811 | ||
4812 | @item cdable_vars | |
4813 | If this is set, an argument to the @code{cd} builtin command that | |
4814 | is not a directory is assumed to be the name of a variable whose | |
4815 | value is the directory to change to. | |
4816 | ||
4817 | @item cdspell | |
4818 | If set, minor errors in the spelling of a directory component in a | |
4819 | @code{cd} command will be corrected. | |
4820 | The errors checked for are transposed characters, | |
4821 | a missing character, and a character too many. | |
4822 | If a correction is found, the corrected path is printed, | |
4823 | and the command proceeds. | |
4824 | This option is only used by interactive shells. | |
4825 | ||
4826 | @item checkhash | |
4827 | If this is set, Bash checks that a command found in the hash | |
4828 | table exists before trying to execute it. If a hashed command no | |
4829 | longer exists, a normal path search is performed. | |
4830 | ||
4831 | @item checkjobs | |
4832 | If set, Bash lists the status of any stopped and running jobs before | |
4833 | exiting an interactive shell. If any jobs are running, this causes | |
4834 | the exit to be deferred until a second exit is attempted without an | |
4835 | intervening command (@pxref{Job Control}). | |
4836 | The shell always postpones exiting if any jobs are stopped. | |
4837 | ||
4838 | @item checkwinsize | |
51f7ea36 CR |
4839 | If set, Bash checks the window size after each command |
4840 | and, if necessary, updates the values of | |
d3ad40de CR |
4841 | @env{LINES} and @env{COLUMNS}. |
4842 | ||
4843 | @item cmdhist | |
4844 | If set, Bash | |
4845 | attempts to save all lines of a multiple-line | |
4846 | command in the same history entry. This allows | |
4847 | easy re-editing of multi-line commands. | |
4848 | ||
35ee8ea0 CR |
4849 | @item compat31 |
4850 | If set, Bash | |
4851 | changes its behavior to that of version 3.1 with respect to quoted | |
5a318736 CR |
4852 | arguments to the conditional command's @samp{=~} operator |
4853 | and with respect to locale-specific | |
df0e4bfe | 4854 | string comparison when using the @code{[[} |
5a318736 CR |
4855 | conditional command's @samp{<} and @samp{>} operators. |
4856 | Bash versions prior to bash-4.1 use ASCII collation and strcmp(3); | |
4857 | bash-4.1 and later use the current locale's collation sequence and strcoll(3). | |
35ee8ea0 | 4858 | |
1665e22a CR |
4859 | @item compat32 |
4860 | If set, Bash | |
4861 | changes its behavior to that of version 3.2 with respect to locale-specific | |
df0e4bfe | 4862 | string comparison when using the @code{[[} |
5a318736 | 4863 | conditional command's @samp{<} and @samp{>} operators (see previous item). |
1665e22a CR |
4864 | |
4865 | @item compat40 | |
4866 | If set, Bash | |
4867 | changes its behavior to that of version 4.0 with respect to locale-specific | |
df0e4bfe | 4868 | string comparison when using the @code{[[} |
5a318736 CR |
4869 | conditional command's @samp{<} and @samp{>} operators (see description |
4870 | of @code{compat31}) | |
54a1fa7c | 4871 | and the effect of interrupting a command list. |
5a318736 CR |
4872 | Bash versions 4.0 and later interrupt the list as if the shell received the |
4873 | interrupt; previous versions continue with the next command in the list. | |
1665e22a | 4874 | |
67362c60 CR |
4875 | @item compat41 |
4876 | If set, Bash, when in posix mode, treats a single quote in a double-quoted | |
4877 | parameter expansion as a special character. The single quotes must match | |
4878 | (an even number) and the characters between the single quotes are considered | |
4879 | quoted. This is the behavior of @sc{posix} mode through version 4.1. | |
4880 | The default Bash behavior remains as in previous versions. | |
4881 | ||
ddef12ff CR |
4882 | @item complete_fullquote |
4883 | If set, Bash | |
4884 | quotes all shell metacharacters in filenames and directory names when | |
4885 | performing completion. | |
4886 | If not set, Bash | |
4887 | removes metacharacters such as the dollar sign from the set of | |
4888 | characters that will be quoted in completed filenames | |
4889 | when these metacharacters appear in shell variable references in words to be | |
4890 | completed. | |
4891 | This means that dollar signs in variable names that expand to directories | |
4892 | will not be quoted; | |
4893 | however, any dollar signs appearing in filenames will not be quoted, either. | |
4894 | This is active only when bash is using backslashes to quote completed | |
4895 | filenames. | |
4896 | This variable is set by default, which is the default Bash behavior in | |
4897 | versions through 4.2. | |
4898 | ||
74d0116b CR |
4899 | @item direxpand |
4900 | If set, Bash | |
4901 | replaces directory names with the results of word expansion when performing | |
4902 | filename completion. This changes the contents of the readline editing | |
4903 | buffer. | |
4904 | If not set, Bash attempts to preserve what the user typed. | |
4905 | ||
4ac1ff98 CR |
4906 | @item dirspell |
4907 | If set, Bash | |
4908 | attempts spelling correction on directory names during word completion | |
4909 | if the directory name initially supplied does not exist. | |
4910 | ||
d3ad40de CR |
4911 | @item dotglob |
4912 | If set, Bash includes filenames beginning with a `.' in | |
4913 | the results of filename expansion. | |
4914 | ||
4915 | @item execfail | |
4916 | If this is set, a non-interactive shell will not exit if | |
4917 | it cannot execute the file specified as an argument to the @code{exec} | |
4918 | builtin command. An interactive shell does not exit if @code{exec} | |
4919 | fails. | |
4920 | ||
4921 | @item expand_aliases | |
4922 | If set, aliases are expanded as described below under Aliases, | |
4923 | @ref{Aliases}. | |
4924 | This option is enabled by default for interactive shells. | |
4925 | ||
4926 | @item extdebug | |
4927 | If set, behavior intended for use by debuggers is enabled: | |
4928 | ||
4929 | @enumerate | |
4930 | @item | |
4931 | The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins}) | |
4932 | displays the source file name and line number corresponding to each function | |
4933 | name supplied as an argument. | |
4934 | ||
4935 | @item | |
4936 | If the command run by the @code{DEBUG} trap returns a non-zero value, the | |
4937 | next command is skipped and not executed. | |
4938 | ||
4939 | @item | |
4940 | If the command run by the @code{DEBUG} trap returns a value of 2, and the | |
4941 | shell is executing in a subroutine (a shell function or a shell script | |
4942 | executed by the @code{.} or @code{source} builtins), a call to | |
4943 | @code{return} is simulated. | |
4944 | ||
4945 | @item | |
4946 | @code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their | |
4947 | descriptions (@pxref{Bash Variables}). | |
4948 | ||
4949 | @item | |
4950 | Function tracing is enabled: command substitution, shell functions, and | |
4951 | subshells invoked with @code{( @var{command} )} inherit the | |
4952 | @code{DEBUG} and @code{RETURN} traps. | |
4953 | ||
4954 | @item | |
4955 | Error tracing is enabled: command substitution, shell functions, and | |
4956 | subshells invoked with @code{( @var{command} )} inherit the | |
5cdaaf76 | 4957 | @code{ERR} trap. |
d3ad40de CR |
4958 | @end enumerate |
4959 | ||
4960 | @item extglob | |
4961 | If set, the extended pattern matching features described above | |
4962 | (@pxref{Pattern Matching}) are enabled. | |
4963 | ||
4964 | @item extquote | |
4965 | If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is | |
4966 | performed within @code{$@{@var{parameter}@}} expansions | |
4967 | enclosed in double quotes. This option is enabled by default. | |
4968 | ||
4969 | @item failglob | |
d0ca3503 | 4970 | If set, patterns which fail to match filenames during filename expansion |
d3ad40de CR |
4971 | result in an expansion error. |
4972 | ||
4973 | @item force_fignore | |
4974 | If set, the suffixes specified by the @env{FIGNORE} shell variable | |
4975 | cause words to be ignored when performing word completion even if | |
4976 | the ignored words are the only possible completions. | |
4977 | @xref{Bash Variables}, for a description of @env{FIGNORE}. | |
4978 | This option is enabled by default. | |
4979 | ||
74d0116b CR |
4980 | @item globasciiranges |
4981 | If set, range expressions used in pattern matching (@pxref{Pattern Matching}) | |
4982 | behave as if in the traditional C locale when performing | |
4983 | comparisons. That is, the current locale's collating sequence | |
4984 | is not taken into account, so | |
4985 | @samp{b} will not collate between @samp{A} and @samp{B}, | |
4986 | and upper-case and lower-case ASCII characters will collate together. | |
4987 | ||
4ac1ff98 CR |
4988 | @item globstar |
4989 | If set, the pattern @samp{**} used in a filename expansion context will | |
54a1fa7c | 4990 | match all files and zero or more directories and subdirectories. |
4ac1ff98 CR |
4991 | If the pattern is followed by a @samp{/}, only directories and |
4992 | subdirectories match. | |
4993 | ||
d3ad40de CR |
4994 | @item gnu_errfmt |
4995 | If set, shell error messages are written in the standard @sc{gnu} error | |
4996 | message format. | |
4997 | ||
4998 | @item histappend | |
4999 | If set, the history list is appended to the file named by the value | |
5000 | of the @env{HISTFILE} | |
5001 | variable when the shell exits, rather than overwriting the file. | |
5002 | ||
5003 | @item histreedit | |
5004 | If set, and Readline | |
5005 | is being used, a user is given the opportunity to re-edit a | |
5006 | failed history substitution. | |
5007 | ||
5008 | @item histverify | |
5009 | If set, and Readline | |
5010 | is being used, the results of history substitution are not immediately | |
5011 | passed to the shell parser. Instead, the resulting line is loaded into | |
5012 | the Readline editing buffer, allowing further modification. | |
5013 | ||
5014 | @item hostcomplete | |
5015 | If set, and Readline is being used, Bash will attempt to perform | |
5016 | hostname completion when a word containing a @samp{@@} is being | |
5017 | completed (@pxref{Commands For Completion}). This option is enabled | |
5018 | by default. | |
5019 | ||
5020 | @item huponexit | |
5021 | If set, Bash will send @code{SIGHUP} to all jobs when an interactive | |
5022 | login shell exits (@pxref{Signals}). | |
5023 | ||
5024 | @item interactive_comments | |
5025 | Allow a word beginning with @samp{#} | |
5026 | to cause that word and all remaining characters on that | |
5027 | line to be ignored in an interactive shell. | |
5028 | This option is enabled by default. | |
5029 | ||
7d92f73f CR |
5030 | @item lastpipe |
5031 | If set, and job control is not active, the shell runs the last command of | |
5032 | a pipeline not executed in the background in the current shell environment. | |
5033 | ||
d3ad40de CR |
5034 | @item lithist |
5035 | If enabled, and the @code{cmdhist} | |
5036 | option is enabled, multi-line commands are saved to the history with | |
5037 | embedded newlines rather than using semicolon separators where possible. | |
5038 | ||
5039 | @item login_shell | |
5040 | The shell sets this option if it is started as a login shell | |
5041 | (@pxref{Invoking Bash}). | |
5042 | The value may not be changed. | |
5043 | ||
5044 | @item mailwarn | |
5045 | If set, and a file that Bash is checking for mail has been | |
5046 | accessed since the last time it was checked, the message | |
5047 | @code{"The mail in @var{mailfile} has been read"} is displayed. | |
5048 | ||
5049 | @item no_empty_cmd_completion | |
5050 | If set, and Readline is being used, Bash will not attempt to search | |
5051 | the @env{PATH} for possible completions when completion is attempted | |
5052 | on an empty line. | |
5053 | ||
5054 | @item nocaseglob | |
5055 | If set, Bash matches filenames in a case-insensitive fashion when | |
5056 | performing filename expansion. | |
5057 | ||
5058 | @item nocasematch | |
5059 | If set, Bash matches patterns in a case-insensitive fashion when | |
5060 | performing matching while executing @code{case} or @code{[[} | |
5061 | conditional commands. | |
5062 | ||
5063 | @item nullglob | |
5064 | If set, Bash allows filename patterns which match no | |
5065 | files to expand to a null string, rather than themselves. | |
5066 | ||
5067 | @item progcomp | |
5068 | If set, the programmable completion facilities | |
5069 | (@pxref{Programmable Completion}) are enabled. | |
5070 | This option is enabled by default. | |
5071 | ||
5072 | @item promptvars | |
5073 | If set, prompt strings undergo | |
5074 | parameter expansion, command substitution, arithmetic | |
5075 | expansion, and quote removal after being expanded | |
b28ff8c9 | 5076 | as described below (@pxref{Controlling the Prompt}). |
d3ad40de CR |
5077 | This option is enabled by default. |
5078 | ||
5079 | @item restricted_shell | |
5080 | The shell sets this option if it is started in restricted mode | |
5081 | (@pxref{The Restricted Shell}). | |
5082 | The value may not be changed. | |
5083 | This is not reset when the startup files are executed, allowing | |
5084 | the startup files to discover whether or not a shell is restricted. | |
5085 | ||
5086 | @item shift_verbose | |
5087 | If this is set, the @code{shift} | |
5088 | builtin prints an error message when the shift count exceeds the | |
5089 | number of positional parameters. | |
5090 | ||
5091 | @item sourcepath | |
5092 | If set, the @code{source} builtin uses the value of @env{PATH} | |
5093 | to find the directory containing the file supplied as an argument. | |
5094 | This option is enabled by default. | |
5095 | ||
5096 | @item xpg_echo | |
5097 | If set, the @code{echo} builtin expands backslash-escape sequences | |
5098 | by default. | |
5099 | ||
5100 | @end table | |
5101 | ||
5102 | @noindent | |
5103 | The return status when listing options is zero if all @var{optnames} | |
5104 | are enabled, non-zero otherwise. | |
5105 | When setting or unsetting options, the return status is zero unless an | |
5106 | @var{optname} is not a valid shell option. | |
d3ad40de CR |
5107 | @end table |
5108 | ||
bb70624e JA |
5109 | @node Special Builtins |
5110 | @section Special Builtins | |
5111 | @cindex special builtin | |
cce855bc | 5112 | |
ac18b312 | 5113 | For historical reasons, the @sc{posix} standard has classified |
bb70624e JA |
5114 | several builtin commands as @emph{special}. |
5115 | When Bash is executing in @sc{posix} mode, the special builtins | |
5116 | differ from other builtin commands in three respects: | |
ccc6cda3 | 5117 | |
bb70624e JA |
5118 | @enumerate |
5119 | @item | |
5120 | Special builtins are found before shell functions during command lookup. | |
cce855bc | 5121 | |
bb70624e JA |
5122 | @item |
5123 | If a special builtin returns an error status, a non-interactive shell exits. | |
cce855bc | 5124 | |
bb70624e JA |
5125 | @item |
5126 | Assignment statements preceding the command stay in effect in the shell | |
5127 | environment after the command completes. | |
5128 | @end enumerate | |
ccc6cda3 | 5129 | |
bb70624e JA |
5130 | When Bash is not executing in @sc{posix} mode, these builtins behave no |
5131 | differently than the rest of the Bash builtin commands. | |
5132 | The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}. | |
ccc6cda3 | 5133 | |
bb70624e | 5134 | These are the @sc{posix} special builtins: |
ccc6cda3 | 5135 | @example |
bb70624e JA |
5136 | @w{break : . continue eval exec exit export readonly return set} |
5137 | @w{shift trap unset} | |
ccc6cda3 | 5138 | @end example |
ccc6cda3 | 5139 | |
bb70624e JA |
5140 | @node Shell Variables |
5141 | @chapter Shell Variables | |
cce855bc | 5142 | |
bb70624e JA |
5143 | @menu |
5144 | * Bourne Shell Variables:: Variables which Bash uses in the same way | |
5145 | as the Bourne Shell. | |
5146 | * Bash Variables:: List of variables that exist in Bash. | |
5147 | @end menu | |
cce855bc | 5148 | |
bb70624e JA |
5149 | This chapter describes the shell variables that Bash uses. |
5150 | Bash automatically assigns default values to a number of variables. | |
ccc6cda3 | 5151 | |
bb70624e JA |
5152 | @node Bourne Shell Variables |
5153 | @section Bourne Shell Variables | |
ccc6cda3 | 5154 | |
bb70624e JA |
5155 | Bash uses certain shell variables in the same way as the Bourne shell. |
5156 | In some cases, Bash assigns a default value to the variable. | |
ccc6cda3 | 5157 | |
bb70624e | 5158 | @vtable @code |
ccc6cda3 | 5159 | |
bb70624e JA |
5160 | @item CDPATH |
5161 | A colon-separated list of directories used as a search path for | |
5162 | the @code{cd} builtin command. | |
ccc6cda3 | 5163 | |
bb70624e JA |
5164 | @item HOME |
5165 | The current user's home directory; the default for the @code{cd} builtin | |
5166 | command. | |
5167 | The value of this variable is also used by tilde expansion | |
5168 | (@pxref{Tilde Expansion}). | |
ccc6cda3 | 5169 | |
bb70624e JA |
5170 | @item IFS |
5171 | A list of characters that separate fields; used when the shell splits | |
5172 | words as part of expansion. | |
ccc6cda3 | 5173 | |
bb70624e | 5174 | @item MAIL |
9ec5ed66 CR |
5175 | If this parameter is set to a filename or directory name |
5176 | and the @env{MAILPATH} variable | |
bb70624e | 5177 | is not set, Bash informs the user of the arrival of mail in |
9ec5ed66 | 5178 | the specified file or Maildir-format directory. |
ccc6cda3 | 5179 | |
bb70624e JA |
5180 | @item MAILPATH |
5181 | A colon-separated list of filenames which the shell periodically checks | |
5182 | for new mail. | |
5183 | Each list entry can specify the message that is printed when new mail | |
b28ff8c9 | 5184 | arrives in the mail file by separating the filename from the message with |
bb70624e JA |
5185 | a @samp{?}. |
5186 | When used in the text of the message, @code{$_} expands to the name of | |
5187 | the current mail file. | |
ccc6cda3 | 5188 | |
bb70624e JA |
5189 | @item OPTARG |
5190 | The value of the last option argument processed by the @code{getopts} builtin. | |
ccc6cda3 | 5191 | |
bb70624e JA |
5192 | @item OPTIND |
5193 | The index of the last option argument processed by the @code{getopts} builtin. | |
ccc6cda3 | 5194 | |
bb70624e JA |
5195 | @item PATH |
5196 | A colon-separated list of directories in which the shell looks for | |
5197 | commands. | |
d3a24ed2 CR |
5198 | A zero-length (null) directory name in the value of @code{PATH} indicates the |
5199 | current directory. | |
5200 | A null directory name may appear as two adjacent colons, or as an initial | |
5201 | or trailing colon. | |
5202 | ||
ccc6cda3 | 5203 | |
bb70624e JA |
5204 | @item PS1 |
5205 | The primary prompt string. The default value is @samp{\s-\v\$ }. | |
b28ff8c9 | 5206 | @xref{Controlling the Prompt}, for the complete list of escape |
28ef6c31 | 5207 | sequences that are expanded before @env{PS1} is displayed. |
ccc6cda3 | 5208 | |
bb70624e JA |
5209 | @item PS2 |
5210 | The secondary prompt string. The default value is @samp{> }. | |
ccc6cda3 | 5211 | |
bb70624e | 5212 | @end vtable |
ccc6cda3 | 5213 | |
bb70624e JA |
5214 | @node Bash Variables |
5215 | @section Bash Variables | |
ccc6cda3 | 5216 | |
bb70624e JA |
5217 | These variables are set or used by Bash, but other shells |
5218 | do not normally treat them specially. | |
ccc6cda3 | 5219 | |
bb70624e JA |
5220 | A few variables used by Bash are described in different chapters: |
5221 | variables for controlling the job control facilities | |
5222 | (@pxref{Job Control Variables}). | |
ccc6cda3 | 5223 | |
bb70624e | 5224 | @vtable @code |
cce855bc | 5225 | |
bb70624e JA |
5226 | @item BASH |
5227 | The full pathname used to execute the current instance of Bash. | |
cce855bc | 5228 | |
691aebcb CR |
5229 | @item BASHOPTS |
5230 | A colon-separated list of enabled shell options. Each word in | |
5231 | the list is a valid argument for the @option{-s} option to the | |
5232 | @code{shopt} builtin command (@pxref{The Shopt Builtin}). | |
5233 | The options appearing in @env{BASHOPTS} are those reported | |
5234 | as @samp{on} by @samp{shopt}. | |
5235 | If this variable is in the environment when Bash | |
5236 | starts up, each shell option in the list will be enabled before | |
5237 | reading any startup files. This variable is readonly. | |
5238 | ||
d3ad40de | 5239 | @item BASHPID |
dc60d4e0 | 5240 | Expands to the process ID of the current Bash process. |
d3ad40de CR |
5241 | This differs from @code{$$} under certain circumstances, such as subshells |
5242 | that do not require Bash to be re-initialized. | |
5243 | ||
fdf670ea CR |
5244 | @item BASH_ALIASES |
5245 | An associative array variable whose members correspond to the internal | |
dc60d4e0 | 5246 | list of aliases as maintained by the @code{alias} builtin. |
fdf670ea CR |
5247 | (@pxref{Bourne Shell Builtins}). |
5248 | Elements added to this array appear in the alias list; unsetting array | |
5249 | elements cause aliases to be removed from the alias list. | |
5250 | ||
d3a24ed2 CR |
5251 | @item BASH_ARGC |
5252 | An array variable whose values are the number of parameters in each | |
5253 | frame of the current bash execution call stack. The number of | |
5254 | parameters to the current subroutine (shell function or script executed | |
5255 | with @code{.} or @code{source}) is at the top of the stack. When a | |
5256 | subroutine is executed, the number of parameters passed is pushed onto | |
5257 | @code{BASH_ARGC}. | |
2206f89a | 5258 | The shell sets @code{BASH_ARGC} only when in extended debugging mode |
d3ad40de | 5259 | (see @ref{The Shopt Builtin} |
2206f89a CR |
5260 | for a description of the @code{extdebug} option to the @code{shopt} |
5261 | builtin). | |
d3a24ed2 CR |
5262 | |
5263 | @item BASH_ARGV | |
5264 | An array variable containing all of the parameters in the current bash | |
5265 | execution call stack. The final parameter of the last subroutine call | |
5266 | is at the top of the stack; the first parameter of the initial call is | |
5267 | at the bottom. When a subroutine is executed, the parameters supplied | |
5268 | are pushed onto @code{BASH_ARGV}. | |
2206f89a | 5269 | The shell sets @code{BASH_ARGV} only when in extended debugging mode |
d3ad40de | 5270 | (see @ref{The Shopt Builtin} |
2206f89a CR |
5271 | for a description of the @code{extdebug} option to the @code{shopt} |
5272 | builtin). | |
d3a24ed2 | 5273 | |
fdf670ea CR |
5274 | @item BASH_CMDS |
5275 | An associative array variable whose members correspond to the internal | |
5276 | hash table of commands as maintained by the @code{hash} builtin | |
5277 | (@pxref{Bourne Shell Builtins}). | |
5278 | Elements added to this array appear in the hash table; unsetting array | |
5279 | elements cause commands to be removed from the hash table. | |
5280 | ||
d3a24ed2 CR |
5281 | @item BASH_COMMAND |
5282 | The command currently being executed or about to be executed, unless the | |
5283 | shell is executing a command as the result of a trap, | |
5284 | in which case it is the command executing at the time of the trap. | |
5285 | ||
bb70624e JA |
5286 | @item BASH_ENV |
5287 | If this variable is set when Bash is invoked to execute a shell | |
5288 | script, its value is expanded and used as the name of a startup file | |
5289 | to read before executing the script. @xref{Bash Startup Files}. | |
ccc6cda3 | 5290 | |
d3a24ed2 CR |
5291 | @item BASH_EXECUTION_STRING |
5292 | The command argument to the @option{-c} invocation option. | |
5293 | ||
5294 | @item BASH_LINENO | |
5295 | An array variable whose members are the line numbers in source files | |
7d92f73f CR |
5296 | where each corresponding member of @var{FUNCNAME} was invoked. |
5297 | @code{$@{BASH_LINENO[$i]@}} is the line number in the source file | |
5298 | (@code{$@{BASH_SOURCE[$i+1]@}}) where | |
3d4e09aa | 5299 | @code{$@{FUNCNAME[$i]@}} was called (or @code{$@{BASH_LINENO[$i-1]@}} if |
7d92f73f | 5300 | referenced within another shell function). |
d3a24ed2 CR |
5301 | Use @code{LINENO} to obtain the current line number. |
5302 | ||
5303 | @item BASH_REMATCH | |
5304 | An array variable whose members are assigned by the @samp{=~} binary | |
5305 | operator to the @code{[[} conditional command | |
5306 | (@pxref{Conditional Constructs}). | |
5307 | The element with index 0 is the portion of the string | |
5308 | matching the entire regular expression. | |
5309 | The element with index @var{n} is the portion of the | |
5310 | string matching the @var{n}th parenthesized subexpression. | |
5311 | This variable is read-only. | |
5312 | ||
5313 | @item BASH_SOURCE | |
7d92f73f CR |
5314 | An array variable whose members are the source filenames where the |
5315 | corresponding shell function names in the @code{FUNCNAME} array | |
5316 | variable are defined. | |
5317 | The shell function @code{$@{FUNCNAME[$i]@}} is defined in the file | |
5318 | @code{$@{BASH_SOURCE[$i]@}} and called from @code{$@{BASH_SOURCE[$i+1]@}} | |
d3a24ed2 CR |
5319 | |
5320 | @item BASH_SUBSHELL | |
c1854f2d CR |
5321 | Incremented by one within each subshell or subshell environment when |
5322 | the shell begins executing in that environment. | |
d3a24ed2 | 5323 | The initial value is 0. |
ccc6cda3 | 5324 | |
bb70624e JA |
5325 | @item BASH_VERSINFO |
5326 | A readonly array variable (@pxref{Arrays}) | |
5327 | whose members hold version information for this instance of Bash. | |
5328 | The values assigned to the array members are as follows: | |
ccc6cda3 | 5329 | |
bb70624e | 5330 | @table @code |
ccc6cda3 | 5331 | |
bb70624e JA |
5332 | @item BASH_VERSINFO[0] |
5333 | The major version number (the @var{release}). | |
cce855bc | 5334 | |
bb70624e JA |
5335 | @item BASH_VERSINFO[1] |
5336 | The minor version number (the @var{version}). | |
ccc6cda3 | 5337 | |
bb70624e JA |
5338 | @item BASH_VERSINFO[2] |
5339 | The patch level. | |
ccc6cda3 | 5340 | |
bb70624e JA |
5341 | @item BASH_VERSINFO[3] |
5342 | The build version. | |
ccc6cda3 | 5343 | |
bb70624e JA |
5344 | @item BASH_VERSINFO[4] |
5345 | The release status (e.g., @var{beta1}). | |
cce855bc | 5346 | |
bb70624e | 5347 | @item BASH_VERSINFO[5] |
28ef6c31 | 5348 | The value of @env{MACHTYPE}. |
bb70624e | 5349 | @end table |
ccc6cda3 | 5350 | |
d3a24ed2 CR |
5351 | @item BASH_VERSION |
5352 | The version number of the current instance of Bash. | |
5353 | ||
8f714a7c CR |
5354 | @item BASH_XTRACEFD |
5355 | If set to an integer corresponding to a valid file descriptor, Bash | |
5356 | will write the trace output generated when @samp{set -x} | |
5357 | is enabled to that file descriptor. | |
5358 | This allows tracing output to be separated from diagnostic and error | |
5359 | messages. | |
5360 | The file descriptor is closed when @code{BASH_XTRACEFD} is unset or assigned | |
5361 | a new value. | |
5362 | Unsetting @code{BASH_XTRACEFD} or assigning it the empty string causes the | |
5363 | trace output to be sent to the standard error. | |
5364 | Note that setting @code{BASH_XTRACEFD} to 2 (the standard error file | |
5365 | descriptor) and then unsetting it will result in the standard error | |
5366 | being closed. | |
5367 | ||
f73dda09 | 5368 | @item COLUMNS |
54a1fa7c | 5369 | Used by the @code{select} command to determine the terminal width |
276cb932 CR |
5370 | when printing selection lists. Automatically set by an interactive shell |
5371 | upon receipt of a | |
f73dda09 | 5372 | @code{SIGWINCH}. |
bb70624e JA |
5373 | |
5374 | @item COMP_CWORD | |
28ef6c31 | 5375 | An index into @env{$@{COMP_WORDS@}} of the word containing the current |
bb70624e JA |
5376 | cursor position. |
5377 | This variable is available only in shell functions invoked by the | |
5378 | programmable completion facilities (@pxref{Programmable Completion}). | |
5379 | ||
5380 | @item COMP_LINE | |
5381 | The current command line. | |
5382 | This variable is available only in shell functions and external | |
5383 | commands invoked by the | |
5384 | programmable completion facilities (@pxref{Programmable Completion}). | |
5385 | ||
5386 | @item COMP_POINT | |
5387 | The index of the current cursor position relative to the beginning of | |
5388 | the current command. | |
5389 | If the current cursor position is at the end of the current command, | |
5390 | the value of this variable is equal to @code{$@{#COMP_LINE@}}. | |
5391 | This variable is available only in shell functions and external | |
5392 | commands invoked by the | |
5393 | programmable completion facilities (@pxref{Programmable Completion}). | |
5394 | ||
d3ad40de CR |
5395 | @item COMP_TYPE |
5396 | Set to an integer value corresponding to the type of completion attempted | |
5397 | that caused a completion function to be called: | |
5398 | @var{TAB}, for normal completion, | |
5399 | @samp{?}, for listing completions after successive tabs, | |
5400 | @samp{!}, for listing alternatives on partial word completion, | |
5401 | @samp{@@}, to list completions if the word is not unmodified, | |
5402 | or | |
5403 | @samp{%}, for menu completion. | |
5404 | This variable is available only in shell functions and external | |
5405 | commands invoked by the | |
5406 | programmable completion facilities (@pxref{Programmable Completion}). | |
5407 | ||
5408 | @item COMP_KEY | |
5409 | The key (or final key of a key sequence) used to invoke the current | |
5410 | completion function. | |
5411 | ||
d3a24ed2 CR |
5412 | @item COMP_WORDBREAKS |
5413 | The set of characters that the Readline library treats as word | |
5414 | separators when performing word completion. | |
5415 | If @code{COMP_WORDBREAKS} is unset, it loses its special properties, | |
5416 | even if it is subsequently reset. | |
5417 | ||
f73dda09 JA |
5418 | @item COMP_WORDS |
5419 | An array variable consisting of the individual | |
5420 | words in the current command line. | |
db31fb26 CR |
5421 | The line is split into words as Readline would split it, using |
5422 | @code{COMP_WORDBREAKS} as described above. | |
f73dda09 JA |
5423 | This variable is available only in shell functions invoked by the |
5424 | programmable completion facilities (@pxref{Programmable Completion}). | |
5425 | ||
bb70624e JA |
5426 | @item COMPREPLY |
5427 | An array variable from which Bash reads the possible completions | |
5428 | generated by a shell function invoked by the programmable completion | |
5429 | facility (@pxref{Programmable Completion}). | |
ba4ab055 | 5430 | Each array element contains one possible completion. |
b72432fd | 5431 | |
5cdaaf76 CR |
5432 | @item COPROC |
5433 | An array variable created to hold the file descriptors | |
5434 | for output from and input to an unnamed coprocess (@pxref{Coprocesses}). | |
5435 | ||
bb70624e JA |
5436 | @item DIRSTACK |
5437 | An array variable containing the current contents of the directory stack. | |
5438 | Directories appear in the stack in the order they are displayed by the | |
5439 | @code{dirs} builtin. | |
5440 | Assigning to members of this array variable may be used to modify | |
5441 | directories already in the stack, but the @code{pushd} and @code{popd} | |
5442 | builtins must be used to add and remove directories. | |
5443 | Assignment to this variable will not change the current directory. | |
28ef6c31 | 5444 | If @env{DIRSTACK} is unset, it loses its special properties, even if |
bb70624e | 5445 | it is subsequently reset. |
ccc6cda3 | 5446 | |
d3a24ed2 CR |
5447 | @item EMACS |
5448 | If Bash finds this variable in the environment when the shell | |
5449 | starts with value @samp{t}, it assumes that the shell is running in an | |
5f8cde23 | 5450 | Emacs shell buffer and disables line editing. |
d3a24ed2 | 5451 | |
5cdaaf76 CR |
5452 | @item ENV |
5453 | Similar to @code{BASH_ENV}; used when the shell is invoked in | |
5454 | @sc{posix} Mode (@pxref{Bash POSIX Mode}). | |
5455 | ||
bb70624e JA |
5456 | @item EUID |
5457 | The numeric effective user id of the current user. This variable | |
5458 | is readonly. | |
ccc6cda3 | 5459 | |
bb70624e | 5460 | @item FCEDIT |
28ef6c31 | 5461 | The editor used as a default by the @option{-e} option to the @code{fc} |
bb70624e | 5462 | builtin command. |
cce855bc | 5463 | |
bb70624e JA |
5464 | @item FIGNORE |
5465 | A colon-separated list of suffixes to ignore when performing | |
5466 | filename completion. | |
b28ff8c9 | 5467 | A filename whose suffix matches one of the entries in |
28ef6c31 | 5468 | @env{FIGNORE} |
b28ff8c9 | 5469 | is excluded from the list of matched filenames. A sample |
bb70624e | 5470 | value is @samp{.o:~} |
cce855bc | 5471 | |
f73dda09 | 5472 | @item FUNCNAME |
d3a24ed2 CR |
5473 | An array variable containing the names of all shell functions |
5474 | currently in the execution call stack. | |
5475 | The element with index 0 is the name of any currently-executing | |
5476 | shell function. | |
7d92f73f CR |
5477 | The bottom-most element (the one with the highest index) |
5478 | is @code{"main"}. | |
f73dda09 JA |
5479 | This variable exists only when a shell function is executing. |
5480 | Assignments to @env{FUNCNAME} have no effect and return an error status. | |
5481 | If @env{FUNCNAME} is unset, it loses its special properties, even if | |
5482 | it is subsequently reset. | |
5483 | ||
7d92f73f CR |
5484 | This variable can be used with @code{BASH_LINENO} and @code{BASH_SOURCE}. |
5485 | Each element of @code{FUNCNAME} has corresponding elements in | |
5486 | @code{BASH_LINENO} and @code{BASH_SOURCE} to describe the call stack. | |
5487 | For instance, @code{$@{FUNCNAME[$i]@}} was called from the file | |
5488 | @code{$@{BASH_SOURCE[$i+1]@}} at line number @code{$@{BASH_LINENO[$i]@}}. | |
5489 | The @code{caller} builtin displays the current call stack using this | |
5490 | information. | |
5491 | ||
6faad625 CR |
5492 | @item FUNCNEST |
5493 | If set to a numeric value greater than 0, defines a maximum function | |
5494 | nesting level. Function invocations that exceed this nesting level | |
5495 | will cause the current command to abort. | |
5496 | ||
bb70624e JA |
5497 | @item GLOBIGNORE |
5498 | A colon-separated list of patterns defining the set of filenames to | |
5499 | be ignored by filename expansion. | |
5500 | If a filename matched by a filename expansion pattern also matches one | |
28ef6c31 | 5501 | of the patterns in @env{GLOBIGNORE}, it is removed from the list |
bb70624e | 5502 | of matches. |
ccc6cda3 | 5503 | |
bb70624e JA |
5504 | @item GROUPS |
5505 | An array variable containing the list of groups of which the current | |
5506 | user is a member. | |
28ef6c31 JA |
5507 | Assignments to @env{GROUPS} have no effect and return an error status. |
5508 | If @env{GROUPS} is unset, it loses its special properties, even if it is | |
bb70624e | 5509 | subsequently reset. |
ccc6cda3 | 5510 | |
bb70624e JA |
5511 | @item histchars |
5512 | Up to three characters which control history expansion, quick | |
5513 | substitution, and tokenization (@pxref{History Interaction}). | |
5514 | The first character is the | |
5515 | @var{history expansion} character, that is, the character which signifies the | |
5516 | start of a history expansion, normally @samp{!}. The second character is the | |
5517 | character which signifies `quick substitution' when seen as the first | |
5518 | character on a line, normally @samp{^}. The optional third character is the | |
5519 | character which indicates that the remainder of the line is a comment when | |
5520 | found as the first character of a word, usually @samp{#}. The history | |
5521 | comment character causes history substitution to be skipped for the | |
5522 | remaining words on the line. It does not necessarily cause the shell | |
5523 | parser to treat the rest of the line as a comment. | |
ccc6cda3 | 5524 | |
bb70624e JA |
5525 | @item HISTCMD |
5526 | The history number, or index in the history list, of the current | |
28ef6c31 | 5527 | command. If @env{HISTCMD} is unset, it loses its special properties, |
bb70624e | 5528 | even if it is subsequently reset. |
ccc6cda3 | 5529 | |
bb70624e | 5530 | @item HISTCONTROL |
d3a24ed2 CR |
5531 | A colon-separated list of values controlling how commands are saved on |
5532 | the history list. | |
5533 | If the list of values includes @samp{ignorespace}, lines which begin | |
5534 | with a space character are not saved in the history list. | |
5535 | A value of @samp{ignoredups} causes lines which match the previous | |
5536 | history entry to not be saved. | |
5537 | A value of @samp{ignoreboth} is shorthand for | |
5538 | @samp{ignorespace} and @samp{ignoredups}. | |
5539 | A value of @samp{erasedups} causes all previous lines matching the | |
5540 | current line to be removed from the history list before that line | |
5541 | is saved. | |
5542 | Any value not in the above list is ignored. | |
5543 | If @env{HISTCONTROL} is unset, or does not include a valid value, | |
5544 | all lines read by the shell parser are saved on the history list, | |
5545 | subject to the value of @env{HISTIGNORE}. | |
bb70624e JA |
5546 | The second and subsequent lines of a multi-line compound command are |
5547 | not tested, and are added to the history regardless of the value of | |
28ef6c31 | 5548 | @env{HISTCONTROL}. |
ccc6cda3 | 5549 | |
f73dda09 JA |
5550 | @item HISTFILE |
5551 | The name of the file to which the command history is saved. The | |
5552 | default value is @file{~/.bash_history}. | |
5553 | ||
5554 | @item HISTFILESIZE | |
4b82d1cd CR |
5555 | The maximum number of lines contained in the history file. |
5556 | When this variable is assigned a value, the history file is truncated, | |
5557 | if necessary, to contain no more than that number of lines | |
5558 | by removing the oldest entries. | |
f73dda09 | 5559 | The history file is also truncated to this size after |
ed3f3b6c | 5560 | writing it when a shell exits. |
4b82d1cd | 5561 | If the value is 0, the history file is truncated to zero size. |
e67d0029 | 5562 | Non-numeric values and numeric values less than zero inhibit truncation. |
4b82d1cd CR |
5563 | The shell sets the default value to the value of @env{HISTSIZE} |
5564 | after reading any startup files. | |
f73dda09 | 5565 | |
bb70624e JA |
5566 | @item HISTIGNORE |
5567 | A colon-separated list of patterns used to decide which command | |
5568 | lines should be saved on the history list. Each pattern is | |
5569 | anchored at the beginning of the line and must match the complete | |
5570 | line (no implicit @samp{*} is appended). Each pattern is tested | |
28ef6c31 | 5571 | against the line after the checks specified by @env{HISTCONTROL} |
bb70624e JA |
5572 | are applied. In addition to the normal shell pattern matching |
5573 | characters, @samp{&} matches the previous history line. @samp{&} | |
5574 | may be escaped using a backslash; the backslash is removed | |
5575 | before attempting a match. | |
5576 | The second and subsequent lines of a multi-line compound command are | |
5577 | not tested, and are added to the history regardless of the value of | |
28ef6c31 | 5578 | @env{HISTIGNORE}. |
ccc6cda3 | 5579 | |
28ef6c31 | 5580 | @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A |
bb70624e JA |
5581 | pattern of @samp{&} is identical to @code{ignoredups}, and a |
5582 | pattern of @samp{[ ]*} is identical to @code{ignorespace}. | |
5583 | Combining these two patterns, separating them with a colon, | |
5584 | provides the functionality of @code{ignoreboth}. | |
ccc6cda3 | 5585 | |
bb70624e JA |
5586 | @item HISTSIZE |
5587 | The maximum number of commands to remember on the history list. | |
4b82d1cd CR |
5588 | If the value is 0, commands are not saved in the history list. |
5589 | Numeric values less than zero result in every command being saved | |
5590 | on the history list (there is no limit). | |
5591 | The shell sets the default value to 500 after reading any startup files. | |
ccc6cda3 | 5592 | |
d3a24ed2 CR |
5593 | @item HISTTIMEFORMAT |
5594 | If this variable is set and not null, its value is used as a format string | |
5595 | for @var{strftime} to print the time stamp associated with each history | |
5596 | entry displayed by the @code{history} builtin. | |
5597 | If this variable is set, time stamps are written to the history file so | |
5598 | they may be preserved across shell sessions. | |
d3ad40de CR |
5599 | This uses the history comment character to distinguish timestamps from |
5600 | other history lines. | |
d3a24ed2 | 5601 | |
bb70624e JA |
5602 | @item HOSTFILE |
5603 | Contains the name of a file in the same format as @file{/etc/hosts} that | |
5604 | should be read when the shell needs to complete a hostname. | |
5605 | The list of possible hostname completions may be changed while the shell | |
5606 | is running; | |
5607 | the next time hostname completion is attempted after the | |
5608 | value is changed, Bash adds the contents of the new file to the | |
5609 | existing list. | |
9dd88db7 CR |
5610 | If @env{HOSTFILE} is set, but has no value, or does not name a readable file, |
5611 | Bash attempts to read | |
bb70624e | 5612 | @file{/etc/hosts} to obtain the list of possible hostname completions. |
28ef6c31 | 5613 | When @env{HOSTFILE} is unset, the hostname list is cleared. |
ccc6cda3 | 5614 | |
bb70624e JA |
5615 | @item HOSTNAME |
5616 | The name of the current host. | |
ccc6cda3 | 5617 | |
bb70624e JA |
5618 | @item HOSTTYPE |
5619 | A string describing the machine Bash is running on. | |
ccc6cda3 | 5620 | |
bb70624e JA |
5621 | @item IGNOREEOF |
5622 | Controls the action of the shell on receipt of an @code{EOF} character | |
5623 | as the sole input. If set, the value denotes the number | |
5624 | of consecutive @code{EOF} characters that can be read as the | |
5625 | first character on an input line | |
5626 | before the shell will exit. If the variable exists but does not | |
5627 | have a numeric value (or has no value) then the default is 10. | |
5628 | If the variable does not exist, then @code{EOF} signifies the end of | |
5629 | input to the shell. This is only in effect for interactive shells. | |
ccc6cda3 | 5630 | |
bb70624e JA |
5631 | @item INPUTRC |
5632 | The name of the Readline initialization file, overriding the default | |
5633 | of @file{~/.inputrc}. | |
ccc6cda3 | 5634 | |
bb70624e JA |
5635 | @item LANG |
5636 | Used to determine the locale category for any category not specifically | |
5637 | selected with a variable starting with @code{LC_}. | |
ccc6cda3 | 5638 | |
bb70624e | 5639 | @item LC_ALL |
28ef6c31 | 5640 | This variable overrides the value of @env{LANG} and any other |
bb70624e | 5641 | @code{LC_} variable specifying a locale category. |
ccc6cda3 | 5642 | |
bb70624e JA |
5643 | @item LC_COLLATE |
5644 | This variable determines the collation order used when sorting the | |
5645 | results of filename expansion, and | |
5646 | determines the behavior of range expressions, equivalence classes, | |
5647 | and collating sequences within filename expansion and pattern matching | |
5648 | (@pxref{Filename Expansion}). | |
ccc6cda3 | 5649 | |
bb70624e JA |
5650 | @item LC_CTYPE |
5651 | This variable determines the interpretation of characters and the | |
5652 | behavior of character classes within filename expansion and pattern | |
5653 | matching (@pxref{Filename Expansion}). | |
ccc6cda3 | 5654 | |
bb70624e JA |
5655 | @item LC_MESSAGES |
5656 | This variable determines the locale used to translate double-quoted | |
5657 | strings preceded by a @samp{$} (@pxref{Locale Translation}). | |
ccc6cda3 | 5658 | |
bb70624e JA |
5659 | @item LC_NUMERIC |
5660 | This variable determines the locale category used for number formatting. | |
ccc6cda3 | 5661 | |
f73dda09 JA |
5662 | @item LINENO |
5663 | The line number in the script or shell function currently executing. | |
5664 | ||
28ef6c31 | 5665 | @item LINES |
54a1fa7c | 5666 | Used by the @code{select} command to determine the column length |
276cb932 CR |
5667 | for printing selection lists. Automatically set by an interactive shell |
5668 | upon receipt of a | |
28ef6c31 JA |
5669 | @code{SIGWINCH}. |
5670 | ||
bb70624e JA |
5671 | @item MACHTYPE |
5672 | A string that fully describes the system type on which Bash | |
5673 | is executing, in the standard @sc{gnu} @var{cpu-company-system} format. | |
ccc6cda3 | 5674 | |
bb70624e JA |
5675 | @item MAILCHECK |
5676 | How often (in seconds) that the shell should check for mail in the | |
28ef6c31 JA |
5677 | files specified in the @env{MAILPATH} or @env{MAIL} variables. |
5678 | The default is 60 seconds. When it is time to check | |
5679 | for mail, the shell does so before displaying the primary prompt. | |
5680 | If this variable is unset, or set to a value that is not a number | |
5681 | greater than or equal to zero, the shell disables mail checking. | |
ccc6cda3 | 5682 | |
5cdaaf76 CR |
5683 | @item MAPFILE |
5684 | An array variable created to hold the text read by the | |
5685 | @code{mapfile} builtin when no variable name is supplied. | |
5686 | ||
bb70624e JA |
5687 | @item OLDPWD |
5688 | The previous working directory as set by the @code{cd} builtin. | |
ccc6cda3 | 5689 | |
bb70624e JA |
5690 | @item OPTERR |
5691 | If set to the value 1, Bash displays error messages | |
5692 | generated by the @code{getopts} builtin command. | |
ccc6cda3 | 5693 | |
bb70624e JA |
5694 | @item OSTYPE |
5695 | A string describing the operating system Bash is running on. | |
cce855bc | 5696 | |
bb70624e JA |
5697 | @item PIPESTATUS |
5698 | An array variable (@pxref{Arrays}) | |
5699 | containing a list of exit status values from the processes | |
5700 | in the most-recently-executed foreground pipeline (which may | |
5701 | contain only a single command). | |
cce855bc | 5702 | |
f73dda09 | 5703 | @item POSIXLY_CORRECT |
b28ff8c9 | 5704 | If this variable is in the environment when Bash starts, the shell |
f73dda09 JA |
5705 | enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the |
5706 | startup files, as if the @option{--posix} invocation option had been supplied. | |
b28ff8c9 | 5707 | If it is set while the shell is running, Bash enables @sc{posix} mode, |
f73dda09 JA |
5708 | as if the command |
5709 | @example | |
5710 | @code{set -o posix} | |
5711 | @end example | |
5712 | @noindent | |
5713 | had been executed. | |
5714 | ||
bb70624e JA |
5715 | @item PPID |
5716 | The process @sc{id} of the shell's parent process. This variable | |
5717 | is readonly. | |
ccc6cda3 | 5718 | |
bb70624e JA |
5719 | @item PROMPT_COMMAND |
5720 | If set, the value is interpreted as a command to execute | |
28ef6c31 | 5721 | before the printing of each primary prompt (@env{$PS1}). |
ccc6cda3 | 5722 | |
ed35cb4a CR |
5723 | @item PROMPT_DIRTRIM |
5724 | If set to a number greater than zero, the value is used as the number of | |
5725 | trailing directory components to retain when expanding the @code{\w} and | |
b28ff8c9 | 5726 | @code{\W} prompt string escapes (@pxref{Controlling the Prompt}). |
ed35cb4a CR |
5727 | Characters removed are replaced with an ellipsis. |
5728 | ||
bb70624e JA |
5729 | @item PS3 |
5730 | The value of this variable is used as the prompt for the | |
5731 | @code{select} command. If this variable is not set, the | |
5732 | @code{select} command prompts with @samp{#? } | |
ccc6cda3 | 5733 | |
bb70624e JA |
5734 | @item PS4 |
5735 | The value is the prompt printed before the command line is echoed | |
28ef6c31 JA |
5736 | when the @option{-x} option is set (@pxref{The Set Builtin}). |
5737 | The first character of @env{PS4} is replicated multiple times, as | |
bb70624e JA |
5738 | necessary, to indicate multiple levels of indirection. |
5739 | The default is @samp{+ }. | |
ccc6cda3 | 5740 | |
bb70624e JA |
5741 | @item PWD |
5742 | The current working directory as set by the @code{cd} builtin. | |
ccc6cda3 | 5743 | |
bb70624e JA |
5744 | @item RANDOM |
5745 | Each time this parameter is referenced, a random integer | |
5746 | between 0 and 32767 is generated. Assigning a value to this | |
5747 | variable seeds the random number generator. | |
ccc6cda3 | 5748 | |
5cdaaf76 CR |
5749 | @item READLINE_LINE |
5750 | The contents of the Readline line buffer, for use | |
5751 | with @samp{bind -x} (@pxref{Bash Builtins}). | |
5752 | ||
5753 | @item READLINE_POINT | |
5754 | The position of the insertion point in the Readline line buffer, for use | |
5755 | with @samp{bind -x} (@pxref{Bash Builtins}). | |
5756 | ||
bb70624e JA |
5757 | @item REPLY |
5758 | The default variable for the @code{read} builtin. | |
ccc6cda3 | 5759 | |
bb70624e JA |
5760 | @item SECONDS |
5761 | This variable expands to the number of seconds since the | |
5762 | shell was started. Assignment to this variable resets | |
5763 | the count to the value assigned, and the expanded value | |
5764 | becomes the value assigned plus the number of seconds | |
5765 | since the assignment. | |
ccc6cda3 | 5766 | |
61deeb13 CR |
5767 | @item SHELL |
5768 | The full pathname to the shell is kept in this environment variable. | |
5769 | If it is not set when the shell starts, | |
5770 | Bash assigns to it the full pathname of the current user's login shell. | |
5771 | ||
bb70624e JA |
5772 | @item SHELLOPTS |
5773 | A colon-separated list of enabled shell options. Each word in | |
28ef6c31 | 5774 | the list is a valid argument for the @option{-o} option to the |
bb70624e | 5775 | @code{set} builtin command (@pxref{The Set Builtin}). |
28ef6c31 | 5776 | The options appearing in @env{SHELLOPTS} are those reported |
bb70624e JA |
5777 | as @samp{on} by @samp{set -o}. |
5778 | If this variable is in the environment when Bash | |
5779 | starts up, each shell option in the list will be enabled before | |
5780 | reading any startup files. This variable is readonly. | |
ccc6cda3 | 5781 | |
bb70624e JA |
5782 | @item SHLVL |
5783 | Incremented by one each time a new instance of Bash is started. This is | |
5784 | intended to be a count of how deeply your Bash shells are nested. | |
ccc6cda3 | 5785 | |
bb70624e JA |
5786 | @item TIMEFORMAT |
5787 | The value of this parameter is used as a format string specifying | |
5788 | how the timing information for pipelines prefixed with the @code{time} | |
5789 | reserved word should be displayed. | |
5790 | The @samp{%} character introduces an | |
5791 | escape sequence that is expanded to a time value or other | |
5792 | information. | |
5793 | The escape sequences and their meanings are as | |
5794 | follows; the braces denote optional portions. | |
ccc6cda3 | 5795 | |
bb70624e | 5796 | @table @code |
ccc6cda3 | 5797 | |
bb70624e JA |
5798 | @item %% |
5799 | A literal @samp{%}. | |
ccc6cda3 | 5800 | |
bb70624e JA |
5801 | @item %[@var{p}][l]R |
5802 | The elapsed time in seconds. | |
ccc6cda3 | 5803 | |
bb70624e JA |
5804 | @item %[@var{p}][l]U |
5805 | The number of CPU seconds spent in user mode. | |
ccc6cda3 | 5806 | |
bb70624e JA |
5807 | @item %[@var{p}][l]S |
5808 | The number of CPU seconds spent in system mode. | |
ccc6cda3 | 5809 | |
bb70624e JA |
5810 | @item %P |
5811 | The CPU percentage, computed as (%U + %S) / %R. | |
5812 | @end table | |
ccc6cda3 | 5813 | |
bb70624e JA |
5814 | The optional @var{p} is a digit specifying the precision, the number of |
5815 | fractional digits after a decimal point. | |
5816 | A value of 0 causes no decimal point or fraction to be output. | |
5817 | At most three places after the decimal point may be specified; values | |
5818 | of @var{p} greater than 3 are changed to 3. | |
5819 | If @var{p} is not specified, the value 3 is used. | |
ccc6cda3 | 5820 | |
bb70624e JA |
5821 | The optional @code{l} specifies a longer format, including minutes, of |
5822 | the form @var{MM}m@var{SS}.@var{FF}s. | |
5823 | The value of @var{p} determines whether or not the fraction is included. | |
ccc6cda3 | 5824 | |
bb70624e JA |
5825 | If this variable is not set, Bash acts as if it had the value |
5826 | @example | |
5827 | @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'} | |
5828 | @end example | |
5829 | If the value is null, no timing information is displayed. | |
5830 | A trailing newline is added when the format string is displayed. | |
ccc6cda3 | 5831 | |
bb70624e | 5832 | @item TMOUT |
7117c2d2 JA |
5833 | If set to a value greater than zero, @code{TMOUT} is treated as the |
5834 | default timeout for the @code{read} builtin (@pxref{Bash Builtins}). | |
5835 | The @code{select} command (@pxref{Conditional Constructs}) terminates | |
5836 | if input does not arrive after @code{TMOUT} seconds when input is coming | |
5837 | from a terminal. | |
5838 | ||
d3ad40de | 5839 | In an interactive shell, the value is interpreted as |
ed3f3b6c CR |
5840 | the number of seconds to wait for a line of input after issuing |
5841 | the primary prompt. | |
5842 | Bash | |
5843 | terminates after waiting for that number of seconds if a complete | |
5844 | line of input does not arrive. | |
ccc6cda3 | 5845 | |
1569c106 CR |
5846 | @item TMPDIR |
5847 | If set, Bash uses its value as the name of a directory in which | |
5848 | Bash creates temporary files for the shell's use. | |
5849 | ||
bb70624e JA |
5850 | @item UID |
5851 | The numeric real user id of the current user. This variable is readonly. | |
ccc6cda3 | 5852 | |
bb70624e | 5853 | @end vtable |
ccc6cda3 | 5854 | |
bb70624e JA |
5855 | @node Bash Features |
5856 | @chapter Bash Features | |
ccc6cda3 | 5857 | |
b28ff8c9 | 5858 | This chapter describes features unique to Bash. |
ccc6cda3 | 5859 | |
bb70624e JA |
5860 | @menu |
5861 | * Invoking Bash:: Command line options that you can give | |
5862 | to Bash. | |
5863 | * Bash Startup Files:: When and how Bash executes scripts. | |
5864 | * Interactive Shells:: What an interactive shell is. | |
5865 | * Bash Conditional Expressions:: Primitives used in composing expressions for | |
5866 | the @code{test} builtin. | |
5867 | * Shell Arithmetic:: Arithmetic on shell variables. | |
5868 | * Aliases:: Substituting one command for another. | |
5869 | * Arrays:: Array Variables. | |
5870 | * The Directory Stack:: History of visited directories. | |
b28ff8c9 | 5871 | * Controlling the Prompt:: Customizing the various prompt strings. |
bb70624e JA |
5872 | * The Restricted Shell:: A more controlled mode of shell execution. |
5873 | * Bash POSIX Mode:: Making Bash behave more closely to what | |
5874 | the POSIX standard specifies. | |
5875 | @end menu | |
ccc6cda3 | 5876 | |
bb70624e JA |
5877 | @node Invoking Bash |
5878 | @section Invoking Bash | |
ccc6cda3 | 5879 | |
bb70624e | 5880 | @example |
f73dda09 JA |
5881 | bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}] |
5882 | bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}] | |
5883 | bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}] | |
bb70624e | 5884 | @end example |
ccc6cda3 | 5885 | |
3d8cce26 CR |
5886 | All of the single-character options used with the @code{set} builtin |
5887 | (@pxref{The Set Builtin}) can be used as options when the shell is invoked. | |
5888 | In addition, there are several multi-character | |
bb70624e | 5889 | options that you can use. These options must appear on the command |
7117c2d2 | 5890 | line before the single-character options to be recognized. |
ccc6cda3 | 5891 | |
bb70624e | 5892 | @table @code |
d3a24ed2 CR |
5893 | @item --debugger |
5894 | Arrange for the debugger profile to be executed before the shell | |
d3ad40de | 5895 | starts. Turns on extended debugging mode (see @ref{The Shopt Builtin} |
d3a24ed2 | 5896 | for a description of the @code{extdebug} option to the @code{shopt} |
3d8cce26 | 5897 | builtin). |
d3a24ed2 | 5898 | |
bb70624e JA |
5899 | @item --dump-po-strings |
5900 | A list of all double-quoted strings preceded by @samp{$} | |
22e63b05 | 5901 | is printed on the standard output |
bb70624e | 5902 | in the @sc{gnu} @code{gettext} PO (portable object) file format. |
28ef6c31 | 5903 | Equivalent to @option{-D} except for the output format. |
ccc6cda3 | 5904 | |
bb70624e | 5905 | @item --dump-strings |
28ef6c31 | 5906 | Equivalent to @option{-D}. |
ccc6cda3 | 5907 | |
bb70624e | 5908 | @item --help |
d3ad40de | 5909 | Display a usage message on standard output and exit successfully. |
ccc6cda3 | 5910 | |
28ef6c31 JA |
5911 | @item --init-file @var{filename} |
5912 | @itemx --rcfile @var{filename} | |
5913 | Execute commands from @var{filename} (instead of @file{~/.bashrc}) | |
5914 | in an interactive shell. | |
5915 | ||
bb70624e | 5916 | @item --login |
7117c2d2 | 5917 | Equivalent to @option{-l}. |
ccc6cda3 | 5918 | |
bb70624e JA |
5919 | @item --noediting |
5920 | Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing}) | |
5921 | to read command lines when the shell is interactive. | |
ccc6cda3 | 5922 | |
bb70624e JA |
5923 | @item --noprofile |
5924 | Don't load the system-wide startup file @file{/etc/profile} | |
5925 | or any of the personal initialization files | |
5926 | @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile} | |
5927 | when Bash is invoked as a login shell. | |
ccc6cda3 | 5928 | |
bb70624e JA |
5929 | @item --norc |
5930 | Don't read the @file{~/.bashrc} initialization file in an | |
5931 | interactive shell. This is on by default if the shell is | |
5932 | invoked as @code{sh}. | |
ccc6cda3 | 5933 | |
bb70624e JA |
5934 | @item --posix |
5935 | Change the behavior of Bash where the default operation differs | |
ac18b312 | 5936 | from the @sc{posix} standard to match the standard. This |
bb70624e JA |
5937 | is intended to make Bash behave as a strict superset of that |
5938 | standard. @xref{Bash POSIX Mode}, for a description of the Bash | |
5939 | @sc{posix} mode. | |
ccc6cda3 | 5940 | |
bb70624e JA |
5941 | @item --restricted |
5942 | Make the shell a restricted shell (@pxref{The Restricted Shell}). | |
ccc6cda3 | 5943 | |
bb70624e | 5944 | @item --verbose |
28ef6c31 | 5945 | Equivalent to @option{-v}. Print shell input lines as they're read. |
ccc6cda3 | 5946 | |
bb70624e JA |
5947 | @item --version |
5948 | Show version information for this instance of | |
5949 | Bash on the standard output and exit successfully. | |
ccc6cda3 JA |
5950 | @end table |
5951 | ||
bb70624e JA |
5952 | There are several single-character options that may be supplied at |
5953 | invocation which are not available with the @code{set} builtin. | |
cce855bc | 5954 | |
bb70624e | 5955 | @table @code |
e67d0029 CR |
5956 | @item -c |
5957 | Read and execute commands from the first non-option @var{argument} | |
5958 | after processing the options, then exit. | |
5959 | Any remaining arguments are assigned to the | |
bb70624e | 5960 | positional parameters, starting with @code{$0}. |
ccc6cda3 | 5961 | |
bb70624e JA |
5962 | @item -i |
5963 | Force the shell to run interactively. Interactive shells are | |
5964 | described in @ref{Interactive Shells}. | |
ccc6cda3 | 5965 | |
7117c2d2 JA |
5966 | @item -l |
5967 | Make this shell act as if it had been directly invoked by login. | |
5968 | When the shell is interactive, this is equivalent to starting a | |
5969 | login shell with @samp{exec -l bash}. | |
5970 | When the shell is not interactive, the login shell startup files will | |
5971 | be executed. | |
5972 | @samp{exec bash -l} or @samp{exec bash --login} | |
5973 | will replace the current shell with a Bash login shell. | |
5974 | @xref{Bash Startup Files}, for a description of the special behavior | |
5975 | of a login shell. | |
5976 | ||
bb70624e JA |
5977 | @item -r |
5978 | Make the shell a restricted shell (@pxref{The Restricted Shell}). | |
ccc6cda3 | 5979 | |
bb70624e JA |
5980 | @item -s |
5981 | If this option is present, or if no arguments remain after option | |
5982 | processing, then commands are read from the standard input. | |
5983 | This option allows the positional parameters to be set | |
5984 | when invoking an interactive shell. | |
5985 | ||
5986 | @item -D | |
5987 | A list of all double-quoted strings preceded by @samp{$} | |
22e63b05 | 5988 | is printed on the standard output. |
bb70624e JA |
5989 | These are the strings that |
5990 | are subject to language translation when the current locale | |
5991 | is not @code{C} or @code{POSIX} (@pxref{Locale Translation}). | |
28ef6c31 | 5992 | This implies the @option{-n} option; no commands will be executed. |
bb70624e | 5993 | |
f73dda09 JA |
5994 | @item [-+]O [@var{shopt_option}] |
5995 | @var{shopt_option} is one of the shell options accepted by the | |
d3ad40de | 5996 | @code{shopt} builtin (@pxref{The Shopt Builtin}). |
f73dda09 JA |
5997 | If @var{shopt_option} is present, @option{-O} sets the value of that option; |
5998 | @option{+O} unsets it. | |
5999 | If @var{shopt_option} is not supplied, the names and values of the shell | |
6000 | options accepted by @code{shopt} are printed on the standard output. | |
6001 | If the invocation option is @option{+O}, the output is displayed in a format | |
6002 | that may be reused as input. | |
6003 | ||
bb70624e JA |
6004 | @item -- |
6005 | A @code{--} signals the end of options and disables further option | |
6006 | processing. | |
6007 | Any arguments after the @code{--} are treated as filenames and arguments. | |
bb70624e JA |
6008 | @end table |
6009 | ||
f73dda09 JA |
6010 | @cindex login shell |
6011 | A @emph{login} shell is one whose first character of argument zero is | |
6012 | @samp{-}, or one invoked with the @option{--login} option. | |
6013 | ||
bb70624e JA |
6014 | @cindex interactive shell |
6015 | An @emph{interactive} shell is one started without non-option arguments, | |
28ef6c31 JA |
6016 | unless @option{-s} is specified, |
6017 | without specifying the @option{-c} option, and whose input and output are both | |
bb70624e | 6018 | connected to terminals (as determined by @code{isatty(3)}), or one |
28ef6c31 | 6019 | started with the @option{-i} option. @xref{Interactive Shells}, for more |
bb70624e JA |
6020 | information. |
6021 | ||
6022 | If arguments remain after option processing, and neither the | |
28ef6c31 | 6023 | @option{-c} nor the @option{-s} |
bb70624e JA |
6024 | option has been supplied, the first argument is assumed to |
6025 | be the name of a file containing shell commands (@pxref{Shell Scripts}). | |
6026 | When Bash is invoked in this fashion, @code{$0} | |
6027 | is set to the name of the file, and the positional parameters | |
6028 | are set to the remaining arguments. | |
6029 | Bash reads and executes commands from this file, then exits. | |
6030 | Bash's exit status is the exit status of the last command executed | |
6031 | in the script. If no commands are executed, the exit status is 0. | |
6032 | ||
6033 | @node Bash Startup Files | |
6034 | @section Bash Startup Files | |
6035 | @cindex startup files | |
6036 | ||
d3ad40de | 6037 | This section describes how Bash executes its startup files. |
bb70624e | 6038 | If any of the files exist but cannot be read, Bash reports an error. |
b28ff8c9 | 6039 | Tildes are expanded in filenames as described above under |
bb70624e JA |
6040 | Tilde Expansion (@pxref{Tilde Expansion}). |
6041 | ||
6042 | Interactive shells are described in @ref{Interactive Shells}. | |
6043 | ||
28ef6c31 | 6044 | @subsubheading Invoked as an interactive login shell, or with @option{--login} |
bb70624e JA |
6045 | |
6046 | When Bash is invoked as an interactive login shell, or as a | |
28ef6c31 | 6047 | non-interactive shell with the @option{--login} option, it first reads and |
bb70624e JA |
6048 | executes commands from the file @file{/etc/profile}, if that file exists. |
6049 | After reading that file, it looks for @file{~/.bash_profile}, | |
6050 | @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads | |
6051 | and executes commands from the first one that exists and is readable. | |
28ef6c31 | 6052 | The @option{--noprofile} option may be used when the shell is started to |
bb70624e JA |
6053 | inhibit this behavior. |
6054 | ||
6055 | When a login shell exits, Bash reads and executes commands from | |
6056 | the file @file{~/.bash_logout}, if it exists. | |
6057 | ||
6058 | @subsubheading Invoked as an interactive non-login shell | |
6059 | ||
6060 | When an interactive shell that is not a login shell is started, Bash | |
6061 | reads and executes commands from @file{~/.bashrc}, if that file exists. | |
28ef6c31 JA |
6062 | This may be inhibited by using the @option{--norc} option. |
6063 | The @option{--rcfile @var{file}} option will force Bash to read and | |
bb70624e JA |
6064 | execute commands from @var{file} instead of @file{~/.bashrc}. |
6065 | ||
6066 | So, typically, your @file{~/.bash_profile} contains the line | |
6067 | @example | |
6068 | @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi} | |
6069 | @end example | |
6070 | @noindent | |
6071 | after (or before) any login-specific initializations. | |
6072 | ||
6073 | @subsubheading Invoked non-interactively | |
6074 | ||
6075 | When Bash is started non-interactively, to run a shell script, | |
28ef6c31 | 6076 | for example, it looks for the variable @env{BASH_ENV} in the environment, |
bb70624e JA |
6077 | expands its value if it appears there, and uses the expanded value as |
6078 | the name of a file to read and execute. Bash behaves as if the | |
6079 | following command were executed: | |
6080 | @example | |
6081 | @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi} | |
6082 | @end example | |
6083 | @noindent | |
28ef6c31 | 6084 | but the value of the @env{PATH} variable is not used to search for the |
b28ff8c9 | 6085 | filename. |
bb70624e | 6086 | |
28ef6c31 JA |
6087 | As noted above, if a non-interactive shell is invoked with the |
6088 | @option{--login} option, Bash attempts to read and execute commands from the | |
6089 | login shell startup files. | |
6090 | ||
bb70624e JA |
6091 | @subsubheading Invoked with name @code{sh} |
6092 | ||
6093 | If Bash is invoked with the name @code{sh}, it tries to mimic the | |
6094 | startup behavior of historical versions of @code{sh} as closely as | |
6095 | possible, while conforming to the @sc{posix} standard as well. | |
6096 | ||
6097 | When invoked as an interactive login shell, or as a non-interactive | |
28ef6c31 | 6098 | shell with the @option{--login} option, it first attempts to read |
bb70624e JA |
6099 | and execute commands from @file{/etc/profile} and @file{~/.profile}, in |
6100 | that order. | |
28ef6c31 | 6101 | The @option{--noprofile} option may be used to inhibit this behavior. |
bb70624e | 6102 | When invoked as an interactive shell with the name @code{sh}, Bash |
28ef6c31 | 6103 | looks for the variable @env{ENV}, expands its value if it is defined, |
bb70624e JA |
6104 | and uses the expanded value as the name of a file to read and execute. |
6105 | Since a shell invoked as @code{sh} does not attempt to read and execute | |
28ef6c31 | 6106 | commands from any other startup files, the @option{--rcfile} option has |
bb70624e JA |
6107 | no effect. |
6108 | A non-interactive shell invoked with the name @code{sh} does not attempt | |
6109 | to read any other startup files. | |
6110 | ||
6111 | When invoked as @code{sh}, Bash enters @sc{posix} mode after | |
6112 | the startup files are read. | |
6113 | ||
6114 | @subsubheading Invoked in @sc{posix} mode | |
6115 | ||
6116 | When Bash is started in @sc{posix} mode, as with the | |
28ef6c31 | 6117 | @option{--posix} command line option, it follows the @sc{posix} standard |
bb70624e | 6118 | for startup files. |
28ef6c31 | 6119 | In this mode, interactive shells expand the @env{ENV} variable |
bb70624e JA |
6120 | and commands are read and executed from the file whose name is the |
6121 | expanded value. | |
6122 | No other startup files are read. | |
6123 | ||
6124 | @subsubheading Invoked by remote shell daemon | |
6125 | ||
866961ad | 6126 | Bash attempts to determine when it is being run with its standard input |
dc60d4e0 | 6127 | connected to a network connection, as when executed by the remote shell |
866961ad CR |
6128 | daemon, usually @code{rshd}, or the secure shell daemon @code{sshd}. |
6129 | If Bash determines it is being run in | |
6130 | this fashion, it reads and executes commands from @file{~/.bashrc}, if that | |
bb70624e JA |
6131 | file exists and is readable. |
6132 | It will not do this if invoked as @code{sh}. | |
28ef6c31 JA |
6133 | The @option{--norc} option may be used to inhibit this behavior, and the |
6134 | @option{--rcfile} option may be used to force another file to be read, but | |
bb70624e JA |
6135 | @code{rshd} does not generally invoke the shell with those options or |
6136 | allow them to be specified. | |
6137 | ||
6138 | @subsubheading Invoked with unequal effective and real @sc{uid/gid}s | |
6139 | ||
6140 | If Bash is started with the effective user (group) id not equal to the | |
b28ff8c9 | 6141 | real user (group) id, and the @option{-p} option is not supplied, no startup |
bb70624e | 6142 | files are read, shell functions are not inherited from the environment, |
691aebcb CR |
6143 | the @env{SHELLOPTS}, @env{BASHOPTS}, @env{CDPATH}, and @env{GLOBIGNORE} |
6144 | variables, if they appear in the environment, are ignored, and the effective | |
6145 | user id is set to the real user id. | |
b28ff8c9 | 6146 | If the @option{-p} option is supplied at invocation, the startup behavior is |
bb70624e JA |
6147 | the same, but the effective user id is not reset. |
6148 | ||
6149 | @node Interactive Shells | |
6150 | @section Interactive Shells | |
6151 | @cindex interactive shell | |
6152 | @cindex shell, interactive | |
6153 | ||
6154 | @menu | |
6155 | * What is an Interactive Shell?:: What determines whether a shell is Interactive. | |
6156 | * Is this Shell Interactive?:: How to tell if a shell is interactive. | |
6157 | * Interactive Shell Behavior:: What changes in a interactive shell? | |
6158 | @end menu | |
6159 | ||
6160 | @node What is an Interactive Shell? | |
6161 | @subsection What is an Interactive Shell? | |
6162 | ||
6163 | An interactive shell | |
28ef6c31 | 6164 | is one started without non-option arguments, unless @option{-s} is |
d3ad40de | 6165 | specified, without specifying the @option{-c} option, and |
da5b17cd | 6166 | whose input and error output are both |
bb70624e | 6167 | connected to terminals (as determined by @code{isatty(3)}), |
28ef6c31 | 6168 | or one started with the @option{-i} option. |
bb70624e JA |
6169 | |
6170 | An interactive shell generally reads from and writes to a user's | |
6171 | terminal. | |
6172 | ||
28ef6c31 | 6173 | The @option{-s} invocation option may be used to set the positional parameters |
bb70624e JA |
6174 | when an interactive shell is started. |
6175 | ||
6176 | @node Is this Shell Interactive? | |
6177 | @subsection Is this Shell Interactive? | |
6178 | ||
6179 | To determine within a startup script whether or not Bash is | |
6180 | running interactively, | |
6181 | test the value of the @samp{-} special parameter. | |
6182 | It contains @code{i} when the shell is interactive. For example: | |
6183 | ||
6184 | @example | |
6185 | case "$-" in | |
6186 | *i*) echo This shell is interactive ;; | |
6187 | *) echo This shell is not interactive ;; | |
6188 | esac | |
6189 | @end example | |
6190 | ||
6191 | Alternatively, startup scripts may examine the variable | |
28ef6c31 | 6192 | @env{PS1}; it is unset in non-interactive shells, and set in |
bb70624e JA |
6193 | interactive shells. Thus: |
6194 | ||
6195 | @example | |
6196 | if [ -z "$PS1" ]; then | |
6197 | echo This shell is not interactive | |
6198 | else | |
6199 | echo This shell is interactive | |
6200 | fi | |
6201 | @end example | |
6202 | ||
6203 | @node Interactive Shell Behavior | |
6204 | @subsection Interactive Shell Behavior | |
6205 | ||
6206 | When the shell is running interactively, it changes its behavior in | |
6207 | several ways. | |
6208 | ||
6209 | @enumerate | |
6210 | @item | |
6211 | Startup files are read and executed as described in @ref{Bash Startup Files}. | |
6212 | ||
6213 | @item | |
6214 | Job Control (@pxref{Job Control}) is enabled by default. When job | |
6215 | control is in effect, Bash ignores the keyboard-generated job control | |
6216 | signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. | |
6217 | ||
6218 | @item | |
28ef6c31 JA |
6219 | Bash expands and displays @env{PS1} before reading the first line |
6220 | of a command, and expands and displays @env{PS2} before reading the | |
bb70624e JA |
6221 | second and subsequent lines of a multi-line command. |
6222 | ||
6223 | @item | |
28ef6c31 JA |
6224 | Bash executes the value of the @env{PROMPT_COMMAND} variable as a command |
6225 | before printing the primary prompt, @env{$PS1} | |
bb70624e JA |
6226 | (@pxref{Bash Variables}). |
6227 | ||
6228 | @item | |
6229 | Readline (@pxref{Command Line Editing}) is used to read commands from | |
6230 | the user's terminal. | |
6231 | ||
6232 | @item | |
6233 | Bash inspects the value of the @code{ignoreeof} option to @code{set -o} | |
6234 | instead of exiting immediately when it receives an @code{EOF} on its | |
6235 | standard input when reading a command (@pxref{The Set Builtin}). | |
6236 | ||
6237 | @item | |
6238 | Command history (@pxref{Bash History Facilities}) | |
6239 | and history expansion (@pxref{History Interaction}) | |
6240 | are enabled by default. | |
28ef6c31 | 6241 | Bash will save the command history to the file named by @env{$HISTFILE} |
ed3f3b6c | 6242 | when a shell with history enabled exits. |
bb70624e JA |
6243 | |
6244 | @item | |
6245 | Alias expansion (@pxref{Aliases}) is performed by default. | |
6246 | ||
6247 | @item | |
6248 | In the absence of any traps, Bash ignores @code{SIGTERM} | |
6249 | (@pxref{Signals}). | |
6250 | ||
6251 | @item | |
6252 | In the absence of any traps, @code{SIGINT} is caught and handled | |
6253 | ((@pxref{Signals}). | |
6254 | @code{SIGINT} will interrupt some shell builtins. | |
6255 | ||
6256 | @item | |
6257 | An interactive login shell sends a @code{SIGHUP} to all jobs on exit | |
d3ad40de | 6258 | if the @code{huponexit} shell option has been enabled (@pxref{Signals}). |
bb70624e JA |
6259 | |
6260 | @item | |
28ef6c31 | 6261 | The @option{-n} invocation option is ignored, and @samp{set -n} has |
bb70624e JA |
6262 | no effect (@pxref{The Set Builtin}). |
6263 | ||
6264 | @item | |
6265 | Bash will check for mail periodically, depending on the values of the | |
28ef6c31 | 6266 | @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables |
bb70624e JA |
6267 | (@pxref{Bash Variables}). |
6268 | ||
6269 | @item | |
6270 | Expansion errors due to references to unbound shell variables after | |
6271 | @samp{set -u} has been enabled will not cause the shell to exit | |
6272 | (@pxref{The Set Builtin}). | |
6273 | ||
6274 | @item | |
6275 | The shell will not exit on expansion errors caused by @var{var} being unset | |
6276 | or null in @code{$@{@var{var}:?@var{word}@}} expansions | |
6277 | (@pxref{Shell Parameter Expansion}). | |
6278 | ||
6279 | @item | |
6280 | Redirection errors encountered by shell builtins will not cause the | |
6281 | shell to exit. | |
6282 | ||
6283 | @item | |
6284 | When running in @sc{posix} mode, a special builtin returning an error | |
6285 | status will not cause the shell to exit (@pxref{Bash POSIX Mode}). | |
7790f917 | 6286 | |
bb70624e JA |
6287 | @item |
6288 | A failed @code{exec} will not cause the shell to exit | |
6289 | (@pxref{Bourne Shell Builtins}). | |
6290 | ||
6291 | @item | |
6292 | Parser syntax errors will not cause the shell to exit. | |
6293 | ||
6294 | @item | |
6295 | Simple spelling correction for directory arguments to the @code{cd} | |
6296 | builtin is enabled by default (see the description of the @code{cdspell} | |
d3ad40de | 6297 | option to the @code{shopt} builtin in @ref{The Shopt Builtin}). |
bb70624e JA |
6298 | |
6299 | @item | |
28ef6c31 | 6300 | The shell will check the value of the @env{TMOUT} variable and exit |
bb70624e | 6301 | if a command is not read within the specified number of seconds after |
28ef6c31 | 6302 | printing @env{$PS1} (@pxref{Bash Variables}). |
bb70624e JA |
6303 | |
6304 | @end enumerate | |
6305 | ||
6306 | @node Bash Conditional Expressions | |
6307 | @section Bash Conditional Expressions | |
6308 | @cindex expressions, conditional | |
6309 | ||
6310 | Conditional expressions are used by the @code{[[} compound command | |
6311 | and the @code{test} and @code{[} builtin commands. | |
6312 | ||
6313 | Expressions may be unary or binary. | |
6314 | Unary expressions are often used to examine the status of a file. | |
6315 | There are string operators and numeric comparison operators as well. | |
6316 | If the @var{file} argument to one of the primaries is of the form | |
6317 | @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked. | |
6318 | If the @var{file} argument to one of the primaries is one of | |
6319 | @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file | |
6320 | descriptor 0, 1, or 2, respectively, is checked. | |
ccc6cda3 | 6321 | |
df0e4bfe | 6322 | When used with @code{[[}, the @samp{<} and @samp{>} operators sort |
6bf8a8a7 | 6323 | lexicographically using the current locale. |
54a1fa7c | 6324 | The @code{test} command uses ASCII ordering. |
6bf8a8a7 | 6325 | |
be7d8f2d CR |
6326 | Unless otherwise specified, primaries that operate on files follow symbolic |
6327 | links and operate on the target of the link, rather than the link itself. | |
6328 | ||
ccc6cda3 | 6329 | @table @code |
cce855bc JA |
6330 | @item -a @var{file} |
6331 | True if @var{file} exists. | |
6332 | ||
ccc6cda3 JA |
6333 | @item -b @var{file} |
6334 | True if @var{file} exists and is a block special file. | |
6335 | ||
6336 | @item -c @var{file} | |
6337 | True if @var{file} exists and is a character special file. | |
6338 | ||
6339 | @item -d @var{file} | |
6340 | True if @var{file} exists and is a directory. | |
6341 | ||
6342 | @item -e @var{file} | |
6343 | True if @var{file} exists. | |
6344 | ||
6345 | @item -f @var{file} | |
6346 | True if @var{file} exists and is a regular file. | |
6347 | ||
6348 | @item -g @var{file} | |
cce855bc | 6349 | True if @var{file} exists and its set-group-id bit is set. |
ccc6cda3 | 6350 | |
b72432fd JA |
6351 | @item -h @var{file} |
6352 | True if @var{file} exists and is a symbolic link. | |
6353 | ||
ccc6cda3 | 6354 | @item -k @var{file} |
cce855bc | 6355 | True if @var{file} exists and its "sticky" bit is set. |
ccc6cda3 JA |
6356 | |
6357 | @item -p @var{file} | |
cce855bc | 6358 | True if @var{file} exists and is a named pipe (FIFO). |
ccc6cda3 JA |
6359 | |
6360 | @item -r @var{file} | |
6361 | True if @var{file} exists and is readable. | |
6362 | ||
6363 | @item -s @var{file} | |
6364 | True if @var{file} exists and has a size greater than zero. | |
6365 | ||
ccc6cda3 | 6366 | @item -t @var{fd} |
cce855bc | 6367 | True if file descriptor @var{fd} is open and refers to a terminal. |
ccc6cda3 JA |
6368 | |
6369 | @item -u @var{file} | |
6370 | True if @var{file} exists and its set-user-id bit is set. | |
6371 | ||
6372 | @item -w @var{file} | |
6373 | True if @var{file} exists and is writable. | |
6374 | ||
6375 | @item -x @var{file} | |
6376 | True if @var{file} exists and is executable. | |
6377 | ||
ccc6cda3 JA |
6378 | @item -G @var{file} |
6379 | True if @var{file} exists and is owned by the effective group id. | |
6380 | ||
cce855bc JA |
6381 | @item -L @var{file} |
6382 | True if @var{file} exists and is a symbolic link. | |
6383 | ||
5cdaaf76 CR |
6384 | @item -N @var{file} |
6385 | True if @var{file} exists and has been modified since it was last read. | |
6386 | ||
6387 | @item -O @var{file} | |
6388 | True if @var{file} exists and is owned by the effective user id. | |
6389 | ||
cce855bc JA |
6390 | @item -S @var{file} |
6391 | True if @var{file} exists and is a socket. | |
6392 | ||
5cdaaf76 CR |
6393 | @item @var{file1} -ef @var{file2} |
6394 | True if @var{file1} and @var{file2} refer to the same device and | |
6395 | inode numbers. | |
cce855bc | 6396 | |
ccc6cda3 | 6397 | @item @var{file1} -nt @var{file2} |
7117c2d2 JA |
6398 | True if @var{file1} is newer (according to modification date) |
6399 | than @var{file2}, or if @var{file1} exists and @var{file2} does not. | |
ccc6cda3 JA |
6400 | |
6401 | @item @var{file1} -ot @var{file2} | |
7117c2d2 JA |
6402 | True if @var{file1} is older than @var{file2}, |
6403 | or if @var{file2} exists and @var{file1} does not. | |
ccc6cda3 | 6404 | |
ccc6cda3 | 6405 | @item -o @var{optname} |
6faad625 | 6406 | True if the shell option @var{optname} is enabled. |
28ef6c31 | 6407 | The list of options appears in the description of the @option{-o} |
ccc6cda3 JA |
6408 | option to the @code{set} builtin (@pxref{The Set Builtin}). |
6409 | ||
6faad625 CR |
6410 | @item -v @var{varname} |
6411 | True if the shell variable @var{varname} is set (has been assigned a value). | |
6412 | ||
ccc6cda3 JA |
6413 | @item -z @var{string} |
6414 | True if the length of @var{string} is zero. | |
6415 | ||
6416 | @item -n @var{string} | |
6417 | @itemx @var{string} | |
6418 | True if the length of @var{string} is non-zero. | |
6419 | ||
cce855bc | 6420 | @item @var{string1} == @var{string2} |
a3143574 | 6421 | @itemx @var{string1} = @var{string2} |
cce855bc | 6422 | True if the strings are equal. |
df0e4bfe CR |
6423 | When used with the @code{[[} command, this performs pattern matching as |
6424 | described above (@pxref{Conditional Constructs}). | |
6425 | ||
a3143574 | 6426 | @samp{=} should be used with the @code{test} command for @sc{posix} conformance. |
ccc6cda3 JA |
6427 | |
6428 | @item @var{string1} != @var{string2} | |
6429 | True if the strings are not equal. | |
6430 | ||
6431 | @item @var{string1} < @var{string2} | |
0d8616ff | 6432 | True if @var{string1} sorts before @var{string2} lexicographically. |
ccc6cda3 JA |
6433 | |
6434 | @item @var{string1} > @var{string2} | |
0d8616ff | 6435 | True if @var{string1} sorts after @var{string2} lexicographically. |
ccc6cda3 JA |
6436 | |
6437 | @item @var{arg1} OP @var{arg2} | |
6438 | @code{OP} is one of | |
6439 | @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}. | |
6440 | These arithmetic binary operators return true if @var{arg1} | |
6441 | is equal to, not equal to, less than, less than or equal to, | |
6442 | greater than, or greater than or equal to @var{arg2}, | |
6443 | respectively. @var{Arg1} and @var{arg2} | |
6444 | may be positive or negative integers. | |
ccc6cda3 JA |
6445 | @end table |
6446 | ||
bb70624e JA |
6447 | @node Shell Arithmetic |
6448 | @section Shell Arithmetic | |
6449 | @cindex arithmetic, shell | |
6450 | @cindex shell arithmetic | |
6451 | @cindex expressions, arithmetic | |
6452 | @cindex evaluation, arithmetic | |
6453 | @cindex arithmetic evaluation | |
ccc6cda3 | 6454 | |
bb70624e | 6455 | The shell allows arithmetic expressions to be evaluated, as one of |
d3a24ed2 CR |
6456 | the shell expansions or by the @code{let} and the @option{-i} option |
6457 | to the @code{declare} builtins. | |
ccc6cda3 | 6458 | |
7117c2d2 | 6459 | Evaluation is done in fixed-width integers with no check for overflow, |
bb70624e | 6460 | though division by 0 is trapped and flagged as an error. |
d3a24ed2 CR |
6461 | The operators and their precedence, associativity, and values |
6462 | are the same as in the C language. | |
bb70624e JA |
6463 | The following list of operators is grouped into levels of |
6464 | equal-precedence operators. | |
6465 | The levels are listed in order of decreasing precedence. | |
cce855bc | 6466 | |
bb70624e | 6467 | @table @code |
d166f048 | 6468 | |
bb70624e JA |
6469 | @item @var{id}++ @var{id}-- |
6470 | variable post-increment and post-decrement | |
cce855bc | 6471 | |
bb70624e JA |
6472 | @item ++@var{id} --@var{id} |
6473 | variable pre-increment and pre-decrement | |
ccc6cda3 | 6474 | |
bb70624e JA |
6475 | @item - + |
6476 | unary minus and plus | |
ccc6cda3 | 6477 | |
bb70624e JA |
6478 | @item ! ~ |
6479 | logical and bitwise negation | |
ccc6cda3 | 6480 | |
bb70624e JA |
6481 | @item ** |
6482 | exponentiation | |
ccc6cda3 | 6483 | |
bb70624e JA |
6484 | @item * / % |
6485 | multiplication, division, remainder | |
ccc6cda3 | 6486 | |
bb70624e JA |
6487 | @item + - |
6488 | addition, subtraction | |
cce855bc | 6489 | |
bb70624e JA |
6490 | @item << >> |
6491 | left and right bitwise shifts | |
cce855bc | 6492 | |
bb70624e JA |
6493 | @item <= >= < > |
6494 | comparison | |
ccc6cda3 | 6495 | |
bb70624e JA |
6496 | @item == != |
6497 | equality and inequality | |
ccc6cda3 | 6498 | |
bb70624e JA |
6499 | @item & |
6500 | bitwise AND | |
ccc6cda3 | 6501 | |
bb70624e JA |
6502 | @item ^ |
6503 | bitwise exclusive OR | |
ccc6cda3 | 6504 | |
bb70624e JA |
6505 | @item | |
6506 | bitwise OR | |
cce855bc | 6507 | |
bb70624e JA |
6508 | @item && |
6509 | logical AND | |
cce855bc | 6510 | |
bb70624e JA |
6511 | @item || |
6512 | logical OR | |
cce855bc | 6513 | |
bb70624e | 6514 | @item expr ? expr : expr |
d3a24ed2 | 6515 | conditional operator |
cce855bc | 6516 | |
bb70624e JA |
6517 | @item = *= /= %= += -= <<= >>= &= ^= |= |
6518 | assignment | |
cce855bc | 6519 | |
bb70624e JA |
6520 | @item expr1 , expr2 |
6521 | comma | |
6522 | @end table | |
ccc6cda3 | 6523 | |
bb70624e JA |
6524 | Shell variables are allowed as operands; parameter expansion is |
6525 | performed before the expression is evaluated. | |
6526 | Within an expression, shell variables may also be referenced by name | |
6527 | without using the parameter expansion syntax. | |
d3a24ed2 CR |
6528 | A shell variable that is null or unset evaluates to 0 when referenced |
6529 | by name without using the parameter expansion syntax. | |
bb70624e | 6530 | The value of a variable is evaluated as an arithmetic expression |
d3a24ed2 CR |
6531 | when it is referenced, or when a variable which has been given the |
6532 | @var{integer} attribute using @samp{declare -i} is assigned a value. | |
6533 | A null value evaluates to 0. | |
dc60d4e0 | 6534 | A shell variable need not have its @var{integer} attribute turned on |
bb70624e | 6535 | to be used in an expression. |
ccc6cda3 | 6536 | |
bb70624e JA |
6537 | Constants with a leading 0 are interpreted as octal numbers. |
6538 | A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise, | |
1b13a290 | 6539 | numbers take the form [@var{base}@code{#}]@var{n}, where the optional @var{base} |
bb70624e | 6540 | is a decimal number between 2 and 64 representing the arithmetic |
c31d56a7 CR |
6541 | base, and @var{n} is a number in that base. |
6542 | If @var{base}@code{#} is omitted, then base 10 is used. | |
6543 | When specifying @var{n}, | |
6544 | he digits greater than 9 are represented by the lowercase letters, | |
f73dda09 | 6545 | the uppercase letters, @samp{@@}, and @samp{_}, in that order. |
bb70624e | 6546 | If @var{base} is less than or equal to 36, lowercase and uppercase |
f75912ae | 6547 | letters may be used interchangeably to represent numbers between 10 |
bb70624e | 6548 | and 35. |
ccc6cda3 | 6549 | |
bb70624e JA |
6550 | Operators are evaluated in order of precedence. Sub-expressions in |
6551 | parentheses are evaluated first and may override the precedence | |
6552 | rules above. | |
ccc6cda3 | 6553 | |
bb70624e JA |
6554 | @node Aliases |
6555 | @section Aliases | |
6556 | @cindex alias expansion | |
ccc6cda3 | 6557 | |
bb70624e JA |
6558 | @var{Aliases} allow a string to be substituted for a word when it is used |
6559 | as the first word of a simple command. | |
6560 | The shell maintains a list of aliases that may be set and unset with | |
6561 | the @code{alias} and @code{unalias} builtin commands. | |
ccc6cda3 | 6562 | |
bb70624e JA |
6563 | The first word of each simple command, if unquoted, is checked to see |
6564 | if it has an alias. | |
6565 | If so, that word is replaced by the text of the alias. | |
de8913bd CR |
6566 | The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the |
6567 | shell metacharacters or quoting characters listed above may not appear | |
6568 | in an alias name. | |
6569 | The replacement text may contain any valid | |
6570 | shell input, including shell metacharacters. | |
bb70624e JA |
6571 | The first word of the replacement text is tested for |
6572 | aliases, but a word that is identical to an alias being expanded | |
de8913bd CR |
6573 | is not expanded a second time. |
6574 | This means that one may alias @code{ls} to @code{"ls -F"}, | |
bb70624e | 6575 | for instance, and Bash does not try to recursively expand the |
b28ff8c9 CR |
6576 | replacement text. |
6577 | If the last character of the alias value is a | |
6578 | @var{blank}, then the next command word following the | |
bb70624e | 6579 | alias is also checked for alias expansion. |
ccc6cda3 | 6580 | |
bb70624e JA |
6581 | Aliases are created and listed with the @code{alias} |
6582 | command, and removed with the @code{unalias} command. | |
ccc6cda3 | 6583 | |
bb70624e JA |
6584 | There is no mechanism for using arguments in the replacement text, |
6585 | as in @code{csh}. | |
6586 | If arguments are needed, a shell function should be used | |
6587 | (@pxref{Shell Functions}). | |
ccc6cda3 | 6588 | |
bb70624e JA |
6589 | Aliases are not expanded when the shell is not interactive, |
6590 | unless the @code{expand_aliases} shell option is set using | |
d3ad40de | 6591 | @code{shopt} (@pxref{The Shopt Builtin}). |
ccc6cda3 | 6592 | |
bb70624e JA |
6593 | The rules concerning the definition and use of aliases are |
6594 | somewhat confusing. Bash | |
6595 | always reads at least one complete line | |
6596 | of input before executing any | |
6597 | of the commands on that line. Aliases are expanded when a | |
6598 | command is read, not when it is executed. Therefore, an | |
6599 | alias definition appearing on the same line as another | |
6600 | command does not take effect until the next line of input is read. | |
6601 | The commands following the alias definition | |
6602 | on that line are not affected by the new alias. | |
6603 | This behavior is also an issue when functions are executed. | |
6604 | Aliases are expanded when a function definition is read, | |
6605 | not when the function is executed, because a function definition | |
6606 | is itself a compound command. As a consequence, aliases | |
6607 | defined in a function are not available until after that | |
6608 | function is executed. To be safe, always put | |
6609 | alias definitions on a separate line, and do not use @code{alias} | |
6610 | in compound commands. | |
ccc6cda3 | 6611 | |
bb70624e | 6612 | For almost every purpose, shell functions are preferred over aliases. |
ccc6cda3 | 6613 | |
bb70624e JA |
6614 | @node Arrays |
6615 | @section Arrays | |
6616 | @cindex arrays | |
d166f048 | 6617 | |
fdf670ea | 6618 | Bash provides one-dimensional indexed and associative array variables. |
09767ff0 CR |
6619 | Any variable may be used as an indexed array; |
6620 | the @code{declare} builtin will explicitly declare an array. | |
bb70624e JA |
6621 | There is no maximum |
6622 | limit on the size of an array, nor any requirement that members | |
fdf670ea CR |
6623 | be indexed or assigned contiguously. |
6624 | Indexed arrays are referenced using integers (including arithmetic | |
40647963 | 6625 | expressions (@pxref{Shell Arithmetic})) and are zero-based; |
fdf670ea | 6626 | associative arrays use arbitrary strings. |
861a1900 | 6627 | Unless otherwise noted, indexed array indices must be non-negative integers. |
ccc6cda3 | 6628 | |
fdf670ea CR |
6629 | An indexed array is created automatically if any variable is assigned to |
6630 | using the syntax | |
bb70624e | 6631 | @example |
b28ff8c9 | 6632 | @var{name}[@var{subscript}]=@var{value} |
bb70624e | 6633 | @end example |
ccc6cda3 | 6634 | |
bb70624e JA |
6635 | @noindent |
6636 | The @var{subscript} | |
67362c60 | 6637 | is treated as an arithmetic expression that must evaluate to a number. |
67362c60 | 6638 | To explicitly declare an array, use |
bb70624e JA |
6639 | @example |
6640 | declare -a @var{name} | |
6641 | @end example | |
6642 | @noindent | |
6643 | The syntax | |
6644 | @example | |
6645 | declare -a @var{name}[@var{subscript}] | |
6646 | @end example | |
6647 | @noindent | |
09767ff0 | 6648 | is also accepted; the @var{subscript} is ignored. |
ccc6cda3 | 6649 | |
b28ff8c9 | 6650 | @noindent |
fdf670ea CR |
6651 | Associative arrays are created using |
6652 | @example | |
6653 | declare -A @var{name}. | |
6654 | @end example | |
6655 | ||
09767ff0 CR |
6656 | Attributes may be |
6657 | specified for an array variable using the @code{declare} and | |
6658 | @code{readonly} builtins. Each attribute applies to all members of | |
6659 | an array. | |
6660 | ||
bb70624e JA |
6661 | Arrays are assigned to using compound assignments of the form |
6662 | @example | |
b28ff8c9 | 6663 | @var{name}=(@var{value1} @var{value2} @dots{} ) |
bb70624e JA |
6664 | @end example |
6665 | @noindent | |
6666 | where each | |
09767ff0 | 6667 | @var{value} is of the form @code{[@var{subscript}]=}@var{string}. |
b28ff8c9 | 6668 | Indexed array assignments do not require anything but @var{string}. |
09767ff0 | 6669 | When assigning to indexed arrays, if |
bb70624e JA |
6670 | the optional subscript is supplied, that index is assigned to; |
6671 | otherwise the index of the element assigned is the last index assigned | |
6672 | to by the statement plus one. Indexing starts at zero. | |
fdf670ea CR |
6673 | |
6674 | When assigning to an associative array, the subscript is required. | |
6675 | ||
bb70624e JA |
6676 | This syntax is also accepted by the @code{declare} |
6677 | builtin. Individual array elements may be assigned to using the | |
b28ff8c9 | 6678 | @code{@var{name}[@var{subscript}]=@var{value}} syntax introduced above. |
cce855bc | 6679 | |
bb70624e | 6680 | Any element of an array may be referenced using |
b28ff8c9 | 6681 | @code{$@{@var{name}[@var{subscript}]@}}. |
bb70624e JA |
6682 | The braces are required to avoid |
6683 | conflicts with the shell's filename expansion operators. If the | |
6684 | @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members | |
6685 | of the array @var{name}. These subscripts differ only when the word | |
be7d8f2d CR |
6686 | appears within double quotes. |
6687 | If the word is double-quoted, | |
b28ff8c9 | 6688 | @code{$@{@var{name}[*]@}} expands to a single word with |
bb70624e | 6689 | the value of each array member separated by the first character of the |
b28ff8c9 | 6690 | @env{IFS} variable, and @code{$@{@var{name}[@@]@}} expands each element of |
bb70624e | 6691 | @var{name} to a separate word. When there are no array members, |
b28ff8c9 | 6692 | @code{$@{@var{name}[@@]@}} expands to nothing. |
be7d8f2d CR |
6693 | If the double-quoted expansion occurs within a word, the expansion of |
6694 | the first parameter is joined with the beginning part of the original | |
6695 | word, and the expansion of the last parameter is joined with the last | |
6696 | part of the original word. | |
6697 | This is analogous to the | |
bb70624e | 6698 | expansion of the special parameters @samp{@@} and @samp{*}. |
b28ff8c9 CR |
6699 | @code{$@{#@var{name}[@var{subscript}]@}} expands to the length of |
6700 | @code{$@{@var{name}[@var{subscript}]@}}. | |
bb70624e JA |
6701 | If @var{subscript} is @samp{@@} or |
6702 | @samp{*}, the expansion is the number of elements in the array. | |
6703 | Referencing an array variable without a subscript is equivalent to | |
fdf670ea | 6704 | referencing with a subscript of 0. |
d9e1f41e CR |
6705 | If the @var{subscript} |
6706 | used to reference an element of an indexed array | |
6707 | evaluates to a number less than zero, it is used as | |
6708 | an offset from one greater than the array's maximum index (so a subcript | |
6709 | of -1 refers to the last element of the array). | |
ccc6cda3 | 6710 | |
94a5513e CR |
6711 | An array variable is considered set if a subscript has been assigned a |
6712 | value. The null string is a valid value. | |
6713 | ||
bb70624e | 6714 | The @code{unset} builtin is used to destroy arrays. |
b28ff8c9 | 6715 | @code{unset @var{name}[@var{subscript}]} |
bb70624e | 6716 | destroys the array element at index @var{subscript}. |
a82a04b3 | 6717 | Care must be taken to avoid unwanted side effects caused by filename |
d0ca3503 | 6718 | expansion. |
b28ff8c9 CR |
6719 | @code{unset @var{name}}, where @var{name} is an array, removes the |
6720 | entire array. A subscript of @samp{*} or @samp{@@} also removes the | |
bb70624e | 6721 | entire array. |
ccc6cda3 | 6722 | |
bb70624e | 6723 | The @code{declare}, @code{local}, and @code{readonly} |
fdf670ea CR |
6724 | builtins each accept a @option{-a} option to specify an indexed |
6725 | array and a @option{-A} option to specify an associative array. | |
54a1fa7c | 6726 | If both options are supplied, @option{-A} takes precedence. |
fdf670ea | 6727 | The @code{read} builtin accepts a @option{-a} |
bb70624e JA |
6728 | option to assign a list of words read from the standard input |
6729 | to an array, and can read values from the standard input into | |
6730 | individual array elements. The @code{set} and @code{declare} | |
6731 | builtins display array values in a way that allows them to be | |
6732 | reused as input. | |
ccc6cda3 | 6733 | |
bb70624e JA |
6734 | @node The Directory Stack |
6735 | @section The Directory Stack | |
6736 | @cindex directory stack | |
ccc6cda3 | 6737 | |
bb70624e JA |
6738 | @menu |
6739 | * Directory Stack Builtins:: Bash builtin commands to manipulate | |
6740 | the directory stack. | |
6741 | @end menu | |
ccc6cda3 | 6742 | |
bb70624e JA |
6743 | The directory stack is a list of recently-visited directories. The |
6744 | @code{pushd} builtin adds directories to the stack as it changes | |
6745 | the current directory, and the @code{popd} builtin removes specified | |
6746 | directories from the stack and changes the current directory to | |
6747 | the directory removed. The @code{dirs} builtin displays the contents | |
6748 | of the directory stack. | |
ccc6cda3 | 6749 | |
bb70624e | 6750 | The contents of the directory stack are also visible |
28ef6c31 | 6751 | as the value of the @env{DIRSTACK} shell variable. |
ccc6cda3 | 6752 | |
bb70624e JA |
6753 | @node Directory Stack Builtins |
6754 | @subsection Directory Stack Builtins | |
ccc6cda3 | 6755 | |
bb70624e | 6756 | @table @code |
ccc6cda3 | 6757 | |
bb70624e JA |
6758 | @item dirs |
6759 | @btindex dirs | |
6760 | @example | |
b28ff8c9 | 6761 | dirs [-clpv] [+@var{N} | -@var{N}] |
bb70624e | 6762 | @end example |
b28ff8c9 | 6763 | |
bb70624e JA |
6764 | Display the list of currently remembered directories. Directories |
6765 | are added to the list with the @code{pushd} command; the | |
6766 | @code{popd} command removes directories from the list. | |
b28ff8c9 | 6767 | |
bb70624e | 6768 | @table @code |
bb70624e JA |
6769 | @item -c |
6770 | Clears the directory stack by deleting all of the elements. | |
6771 | @item -l | |
b28ff8c9 CR |
6772 | Produces a listing using full pathnames; |
6773 | the default listing format uses a tilde to denote the home directory. | |
bb70624e JA |
6774 | @item -p |
6775 | Causes @code{dirs} to print the directory stack with one entry per | |
6776 | line. | |
6777 | @item -v | |
6778 | Causes @code{dirs} to print the directory stack with one entry per | |
6779 | line, prefixing each entry with its index in the stack. | |
b28ff8c9 CR |
6780 | @item +@var{N} |
6781 | Displays the @var{N}th directory (counting from the left of the | |
6782 | list printed by @code{dirs} when invoked without options), starting | |
6783 | with zero. | |
6784 | @item -@var{N} | |
6785 | Displays the @var{N}th directory (counting from the right of the | |
6786 | list printed by @code{dirs} when invoked without options), starting | |
6787 | with zero. | |
bb70624e JA |
6788 | @end table |
6789 | ||
6790 | @item popd | |
6791 | @btindex popd | |
6792 | @example | |
b28ff8c9 | 6793 | popd [-n] [+@var{N} | -@var{N}] |
bb70624e JA |
6794 | @end example |
6795 | ||
6796 | Remove the top entry from the directory stack, and @code{cd} | |
6797 | to the new top directory. | |
6798 | When no arguments are given, @code{popd} | |
6799 | removes the top directory from the stack and | |
6800 | performs a @code{cd} to the new top directory. The | |
6801 | elements are numbered from 0 starting at the first directory listed with | |
b28ff8c9 CR |
6802 | @code{dirs}; that is, @code{popd} is equivalent to @code{popd +0}. |
6803 | ||
bb70624e | 6804 | @table @code |
b28ff8c9 CR |
6805 | @item -n |
6806 | Suppresses the normal change of directory when removing directories | |
6807 | from the stack, so that only the stack is manipulated. | |
bb70624e JA |
6808 | @item +@var{N} |
6809 | Removes the @var{N}th directory (counting from the left of the | |
6810 | list printed by @code{dirs}), starting with zero. | |
6811 | @item -@var{N} | |
6812 | Removes the @var{N}th directory (counting from the right of the | |
6813 | list printed by @code{dirs}), starting with zero. | |
bb70624e JA |
6814 | @end table |
6815 | ||
6816 | @btindex pushd | |
6817 | @item pushd | |
6818 | @example | |
b28ff8c9 | 6819 | pushd [-n] [@var{+N} | @var{-N} | @var{dir}] |
bb70624e JA |
6820 | @end example |
6821 | ||
6822 | Save the current directory on the top of the directory stack | |
6823 | and then @code{cd} to @var{dir}. | |
6824 | With no arguments, @code{pushd} exchanges the top two directories. | |
6825 | ||
6826 | @table @code | |
d3ad40de CR |
6827 | @item -n |
6828 | Suppresses the normal change of directory when adding directories | |
6829 | to the stack, so that only the stack is manipulated. | |
bb70624e JA |
6830 | @item +@var{N} |
6831 | Brings the @var{N}th directory (counting from the left of the | |
6832 | list printed by @code{dirs}, starting with zero) to the top of | |
6833 | the list by rotating the stack. | |
6834 | @item -@var{N} | |
6835 | Brings the @var{N}th directory (counting from the right of the | |
6836 | list printed by @code{dirs}, starting with zero) to the top of | |
6837 | the list by rotating the stack. | |
bb70624e | 6838 | @item @var{dir} |
b28ff8c9 CR |
6839 | Makes the current working directory be the top of the stack, making |
6840 | it the new current directory as if it had been supplied as an argument | |
6841 | to the @code{cd} builtin. | |
bb70624e | 6842 | @end table |
bb70624e JA |
6843 | @end table |
6844 | ||
b28ff8c9 | 6845 | @node Controlling the Prompt |
bb70624e JA |
6846 | @section Controlling the Prompt |
6847 | @cindex prompting | |
ccc6cda3 | 6848 | |
28ef6c31 JA |
6849 | The value of the variable @env{PROMPT_COMMAND} is examined just before |
6850 | Bash prints each primary prompt. If @env{PROMPT_COMMAND} is set and | |
bb70624e JA |
6851 | has a non-null value, then the |
6852 | value is executed just as if it had been typed on the command line. | |
ccc6cda3 | 6853 | |
bb70624e | 6854 | In addition, the following table describes the special characters which |
b28ff8c9 | 6855 | can appear in the prompt variables @env{PS1} to @env{PS4}: |
ccc6cda3 | 6856 | |
bb70624e JA |
6857 | @table @code |
6858 | @item \a | |
6859 | A bell character. | |
6860 | @item \d | |
6861 | The date, in "Weekday Month Date" format (e.g., "Tue May 26"). | |
7117c2d2 JA |
6862 | @item \D@{@var{format}@} |
6863 | The @var{format} is passed to @code{strftime}(3) and the result is inserted | |
6864 | into the prompt string; an empty @var{format} results in a locale-specific | |
6865 | time representation. The braces are required. | |
bb70624e JA |
6866 | @item \e |
6867 | An escape character. | |
6868 | @item \h | |
6869 | The hostname, up to the first `.'. | |
6870 | @item \H | |
6871 | The hostname. | |
6872 | @item \j | |
6873 | The number of jobs currently managed by the shell. | |
6874 | @item \l | |
6875 | The basename of the shell's terminal device name. | |
6876 | @item \n | |
6877 | A newline. | |
6878 | @item \r | |
6879 | A carriage return. | |
6880 | @item \s | |
6881 | The name of the shell, the basename of @code{$0} (the portion | |
6882 | following the final slash). | |
6883 | @item \t | |
6884 | The time, in 24-hour HH:MM:SS format. | |
6885 | @item \T | |
6886 | The time, in 12-hour HH:MM:SS format. | |
6887 | @item \@@ | |
6888 | The time, in 12-hour am/pm format. | |
f73dda09 JA |
6889 | @item \A |
6890 | The time, in 24-hour HH:MM format. | |
bb70624e JA |
6891 | @item \u |
6892 | The username of the current user. | |
6893 | @item \v | |
6894 | The version of Bash (e.g., 2.00) | |
6895 | @item \V | |
6896 | The release of Bash, version + patchlevel (e.g., 2.00.0) | |
6897 | @item \w | |
ed35cb4a CR |
6898 | The current working directory, with @env{$HOME} abbreviated with a tilde |
6899 | (uses the @env{$PROMPT_DIRTRIM} variable). | |
bb70624e | 6900 | @item \W |
12d937f9 | 6901 | The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde. |
bb70624e JA |
6902 | @item \! |
6903 | The history number of this command. | |
6904 | @item \# | |
6905 | The command number of this command. | |
6906 | @item \$ | |
6907 | If the effective uid is 0, @code{#}, otherwise @code{$}. | |
6908 | @item \@var{nnn} | |
6909 | The character whose ASCII code is the octal value @var{nnn}. | |
6910 | @item \\ | |
6911 | A backslash. | |
6912 | @item \[ | |
6913 | Begin a sequence of non-printing characters. This could be used to | |
6914 | embed a terminal control sequence into the prompt. | |
6915 | @item \] | |
6916 | End a sequence of non-printing characters. | |
6917 | @end table | |
ccc6cda3 | 6918 | |
bb70624e JA |
6919 | The command number and the history number are usually different: |
6920 | the history number of a command is its position in the history | |
6921 | list, which may include commands restored from the history file | |
6922 | (@pxref{Bash History Facilities}), while the command number is | |
6923 | the position in the sequence of commands executed during the current | |
6924 | shell session. | |
ccc6cda3 | 6925 | |
bb70624e JA |
6926 | After the string is decoded, it is expanded via |
6927 | parameter expansion, command substitution, arithmetic | |
6928 | expansion, and quote removal, subject to the value of the | |
6929 | @code{promptvars} shell option (@pxref{Bash Builtins}). | |
ccc6cda3 | 6930 | |
bb70624e JA |
6931 | @node The Restricted Shell |
6932 | @section The Restricted Shell | |
6933 | @cindex restricted shell | |
ccc6cda3 | 6934 | |
bb70624e | 6935 | If Bash is started with the name @code{rbash}, or the |
28ef6c31 | 6936 | @option{--restricted} |
7117c2d2 JA |
6937 | or |
6938 | @option{-r} | |
bb70624e JA |
6939 | option is supplied at invocation, the shell becomes restricted. |
6940 | A restricted shell is used to | |
6941 | set up an environment more controlled than the standard shell. | |
6942 | A restricted shell behaves identically to @code{bash} | |
7117c2d2 JA |
6943 | with the exception that the following are disallowed or not performed: |
6944 | ||
bb70624e JA |
6945 | @itemize @bullet |
6946 | @item | |
6947 | Changing directories with the @code{cd} builtin. | |
6948 | @item | |
28ef6c31 JA |
6949 | Setting or unsetting the values of the @env{SHELL}, @env{PATH}, |
6950 | @env{ENV}, or @env{BASH_ENV} variables. | |
bb70624e JA |
6951 | @item |
6952 | Specifying command names containing slashes. | |
6953 | @item | |
6954 | Specifying a filename containing a slash as an argument to the @code{.} | |
6955 | builtin command. | |
6956 | @item | |
28ef6c31 | 6957 | Specifying a filename containing a slash as an argument to the @option{-p} |
bb70624e JA |
6958 | option to the @code{hash} builtin command. |
6959 | @item | |
6960 | Importing function definitions from the shell environment at startup. | |
6961 | @item | |
28ef6c31 | 6962 | Parsing the value of @env{SHELLOPTS} from the shell environment at startup. |
bb70624e JA |
6963 | @item |
6964 | Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&}, | |
6965 | @samp{&>}, and @samp{>>} redirection operators. | |
6966 | @item | |
6967 | Using the @code{exec} builtin to replace the shell with another command. | |
6968 | @item | |
6969 | Adding or deleting builtin commands with the | |
28ef6c31 | 6970 | @option{-f} and @option{-d} options to the @code{enable} builtin. |
bb70624e | 6971 | @item |
7117c2d2 JA |
6972 | Using the @code{enable} builtin command to enable disabled shell builtins. |
6973 | @item | |
28ef6c31 | 6974 | Specifying the @option{-p} option to the @code{command} builtin. |
bb70624e JA |
6975 | @item |
6976 | Turning off restricted mode with @samp{set +r} or @samp{set +o restricted}. | |
6977 | @end itemize | |
cce855bc | 6978 | |
7117c2d2 JA |
6979 | These restrictions are enforced after any startup files are read. |
6980 | ||
6981 | When a command that is found to be a shell script is executed | |
6982 | (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in | |
6983 | the shell spawned to execute the script. | |
6984 | ||
bb70624e JA |
6985 | @node Bash POSIX Mode |
6986 | @section Bash POSIX Mode | |
6987 | @cindex POSIX Mode | |
cce855bc | 6988 | |
28ef6c31 | 6989 | Starting Bash with the @option{--posix} command-line option or executing |
bb70624e | 6990 | @samp{set -o posix} while Bash is running will cause Bash to conform more |
ac18b312 | 6991 | closely to the @sc{posix} standard by changing the behavior to |
bb70624e | 6992 | match that specified by @sc{posix} in areas where the Bash default differs. |
ccc6cda3 | 6993 | |
7117c2d2 JA |
6994 | When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the |
6995 | startup files. | |
6996 | ||
bb70624e | 6997 | The following list is what's changed when `@sc{posix} mode' is in effect: |
ccc6cda3 | 6998 | |
bb70624e JA |
6999 | @enumerate |
7000 | @item | |
7001 | When a command in the hash table no longer exists, Bash will re-search | |
28ef6c31 | 7002 | @env{$PATH} to find the new location. This is also available with |
bb70624e | 7003 | @samp{shopt -s checkhash}. |
ccc6cda3 | 7004 | |
bb70624e | 7005 | @item |
28ef6c31 JA |
7006 | The message printed by the job control code and builtins when a job |
7007 | exits with a non-zero status is `Done(status)'. | |
ccc6cda3 | 7008 | |
bb70624e JA |
7009 | @item |
7010 | The message printed by the job control code and builtins when a job | |
28ef6c31 JA |
7011 | is stopped is `Stopped(@var{signame})', where @var{signame} is, for |
7012 | example, @code{SIGTSTP}. | |
ccc6cda3 | 7013 | |
7790f917 CR |
7014 | @item |
7015 | The @code{bg} builtin uses the required format to describe each job placed | |
7016 | in the background, which does not include an indication of whether the job | |
7017 | is the current or previous job. | |
7018 | ||
bb70624e | 7019 | @item |
9d2b70f0 CR |
7020 | Reserved words appearing in a context where reserved words are recognized |
7021 | do not undergo alias expansion. | |
ccc6cda3 | 7022 | |
bb70624e | 7023 | @item |
ac18b312 | 7024 | The @sc{posix} @env{PS1} and @env{PS2} expansions of @samp{!} to |
bb70624e | 7025 | the history number and @samp{!!} to @samp{!} are enabled, |
28ef6c31 JA |
7026 | and parameter expansion is performed on the values of @env{PS1} and |
7027 | @env{PS2} regardless of the setting of the @code{promptvars} option. | |
ccc6cda3 | 7028 | |
bb70624e | 7029 | @item |
ac18b312 | 7030 | The @sc{posix} startup files are executed (@env{$ENV}) rather than |
bb70624e | 7031 | the normal Bash files. |
ccc6cda3 | 7032 | |
bb70624e JA |
7033 | @item |
7034 | Tilde expansion is only performed on assignments preceding a command | |
7035 | name, rather than on all assignment statements on the line. | |
cce855bc | 7036 | |
19baff85 CR |
7037 | @item |
7038 | The @code{command} builtin does not prevent builtins that take assignment | |
7039 | statements as arguments from expanding them as assignment statements; | |
7040 | when not in POSIX mode, assignment builtins lose their assignment | |
7041 | statement expansion properties when preceded by @code{command}. | |
7042 | ||
bb70624e JA |
7043 | @item |
7044 | The default history file is @file{~/.sh_history} (this is the | |
28ef6c31 | 7045 | default value of @env{$HISTFILE}). |
ccc6cda3 | 7046 | |
bb70624e JA |
7047 | @item |
7048 | The output of @samp{kill -l} prints all the signal names on a single line, | |
d3a24ed2 CR |
7049 | separated by spaces, without the @samp{SIG} prefix. |
7050 | ||
7051 | @item | |
7052 | The @code{kill} builtin does not accept signal names with a @samp{SIG} | |
7053 | prefix. | |
ccc6cda3 | 7054 | |
bb70624e JA |
7055 | @item |
7056 | Non-interactive shells exit if @var{filename} in @code{.} @var{filename} | |
7057 | is not found. | |
ccc6cda3 | 7058 | |
bb70624e JA |
7059 | @item |
7060 | Non-interactive shells exit if a syntax error in an arithmetic expansion | |
7061 | results in an invalid expression. | |
ccc6cda3 | 7062 | |
6faad625 CR |
7063 | @item |
7064 | Non-interactive shells exit if there is a syntax error in a script read | |
7065 | with the @code{.} or @code{source} builtins, or in a string processed by | |
7066 | the @code{eval} builtin. | |
7067 | ||
bb70624e JA |
7068 | @item |
7069 | Redirection operators do not perform filename expansion on the word | |
7070 | in the redirection unless the shell is interactive. | |
ccc6cda3 | 7071 | |
bb70624e JA |
7072 | @item |
7073 | Redirection operators do not perform word splitting on the word in the | |
7074 | redirection. | |
ccc6cda3 | 7075 | |
bb70624e JA |
7076 | @item |
7077 | Function names must be valid shell @code{name}s. That is, they may not | |
7078 | contain characters other than letters, digits, and underscores, and | |
7079 | may not start with a digit. Declaring a function with an invalid name | |
7080 | causes a fatal syntax error in non-interactive shells. | |
ccc6cda3 | 7081 | |
b28ff8c9 CR |
7082 | @item |
7083 | Function names may not be the same as one of the @sc{posix} special | |
7084 | builtins. | |
7085 | ||
bb70624e | 7086 | @item |
ac18b312 | 7087 | @sc{posix} special builtins are found before shell functions |
bb70624e | 7088 | during command lookup. |
cce855bc | 7089 | |
6faad625 CR |
7090 | @item |
7091 | The @code{time} reserved word may be used by itself as a command. When | |
7092 | used in this way, it displays timing statistics for the shell and its | |
7093 | completed children. The @env{TIMEFORMAT} variable controls the format | |
7094 | of the timing information. | |
7095 | ||
7096 | @item | |
7097 | When parsing and expanding a $@{@dots{}@} expansion that appears within | |
7098 | double quotes, single quotes are no longer special and cannot be used to | |
7099 | quote a closing brace or other special character, unless the operator is | |
7100 | one of those defined to perform pattern removal. In this case, they do | |
7101 | not have to appear as matched pairs. | |
7102 | ||
7d92f73f CR |
7103 | @item |
7104 | The parser does not recognize @code{time} as a reserved word if the next | |
7105 | token begins with a @samp{-}. | |
7106 | ||
bb70624e | 7107 | @item |
ac18b312 | 7108 | If a @sc{posix} special builtin returns an error status, a |
bb70624e | 7109 | non-interactive shell exits. The fatal errors are those listed in |
5cdaaf76 | 7110 | the @sc{posix} standard, and include things like passing incorrect options, |
bb70624e JA |
7111 | redirection errors, variable assignment errors for assignments preceding |
7112 | the command name, and so on. | |
ccc6cda3 | 7113 | |
bb70624e JA |
7114 | @item |
7115 | A non-interactive shell exits with an error status if a variable | |
7116 | assignment error occurs when no command name follows the assignment | |
7117 | statements. | |
7118 | A variable assignment error occurs, for example, when trying to assign | |
7119 | a value to a readonly variable. | |
ccc6cda3 | 7120 | |
e05be32d CR |
7121 | @item |
7122 | A non-interactive shell exists with an error status if a variable | |
7123 | assignment error occurs in an assignment statement preceding a special | |
7124 | builtin, but not with any other simple command. | |
7125 | ||
bb70624e JA |
7126 | @item |
7127 | A non-interactive shell exits with an error status if the iteration | |
7128 | variable in a @code{for} statement or the selection variable in a | |
7129 | @code{select} statement is a readonly variable. | |
ccc6cda3 | 7130 | |
bb70624e JA |
7131 | @item |
7132 | Process substitution is not available. | |
ccc6cda3 | 7133 | |
276cb932 CR |
7134 | @item |
7135 | While variable indirection is available, it may not be applied to the | |
7136 | @samp{#} and @samp{?} special parameters. | |
7137 | ||
bb70624e | 7138 | @item |
ac18b312 | 7139 | Assignment statements preceding @sc{posix} special builtins |
bb70624e | 7140 | persist in the shell environment after the builtin completes. |
ccc6cda3 | 7141 | |
28ef6c31 JA |
7142 | @item |
7143 | Assignment statements preceding shell function calls persist in the | |
7144 | shell environment after the function returns, as if a @sc{posix} | |
7145 | special builtin command had been executed. | |
7146 | ||
bb70624e JA |
7147 | @item |
7148 | The @code{export} and @code{readonly} builtin commands display their | |
ac18b312 | 7149 | output in the format required by @sc{posix}. |
ccc6cda3 | 7150 | |
28ef6c31 JA |
7151 | @item |
7152 | The @code{trap} builtin displays signal names without the leading | |
7153 | @code{SIG}. | |
7154 | ||
d3a24ed2 CR |
7155 | @item |
7156 | The @code{trap} builtin doesn't check the first argument for a possible | |
7157 | signal specification and revert the signal handling to the original | |
898cc92e CR |
7158 | disposition if it is, unless that argument consists solely of digits and |
7159 | is a valid signal number. If users want to reset the handler for a given | |
d3a24ed2 CR |
7160 | signal to the original disposition, they should use @samp{-} as the |
7161 | first argument. | |
7162 | ||
28ef6c31 JA |
7163 | @item |
7164 | The @code{.} and @code{source} builtins do not search the current directory | |
7165 | for the filename argument if it is not found by searching @env{PATH}. | |
7166 | ||
7167 | @item | |
7168 | Subshells spawned to execute command substitutions inherit the value of | |
7169 | the @option{-e} option from the parent shell. When not in @sc{posix} mode, | |
7170 | Bash clears the @option{-e} option in such subshells. | |
7171 | ||
7172 | @item | |
7173 | Alias expansion is always enabled, even in non-interactive shells. | |
7174 | ||
d3a24ed2 CR |
7175 | @item |
7176 | When the @code{alias} builtin displays alias definitions, it does not | |
7177 | display them with a leading @samp{alias } unless the @option{-p} option | |
7178 | is supplied. | |
7179 | ||
28ef6c31 JA |
7180 | @item |
7181 | When the @code{set} builtin is invoked without options, it does not display | |
7182 | shell function names and definitions. | |
7183 | ||
f73dda09 JA |
7184 | @item |
7185 | When the @code{set} builtin is invoked without options, it displays | |
7186 | variable values without quotes, unless they contain shell metacharacters, | |
7187 | even if the result contains nonprinting characters. | |
7117c2d2 JA |
7188 | |
7189 | @item | |
7190 | When the @code{cd} builtin is invoked in @var{logical} mode, and the pathname | |
7191 | constructed from @code{$PWD} and the directory name supplied as an argument | |
7192 | does not refer to an existing directory, @code{cd} will fail instead of | |
7193 | falling back to @var{physical} mode. | |
898cc92e | 7194 | |
7790f917 CR |
7195 | @item |
7196 | The @code{pwd} builtin verifies that the value it prints is the same as the | |
7197 | current directory, even if it is not asked to check the file system with the | |
7198 | @option{-P} option. | |
7199 | ||
898cc92e CR |
7200 | @item |
7201 | When listing the history, the @code{fc} builtin does not include an | |
7202 | indication of whether or not a history entry has been modified. | |
7203 | ||
7204 | @item | |
7205 | The default editor used by @code{fc} is @code{ed}. | |
7206 | ||
b954488b CR |
7207 | @item |
7208 | The @code{type} and @code{command} builtins will not report a non-executable | |
7209 | file as having been found, though the shell will attempt to execute such a | |
7210 | file if it is the only so-named file found in @code{$PATH}. | |
7211 | ||
7790f917 CR |
7212 | @item |
7213 | The @code{vi} editing mode will invoke the @code{vi} editor directly when | |
dd4f3dd8 | 7214 | the @samp{v} command is run, instead of checking @code{$VISUAL} and |
7790f917 CR |
7215 | @code{$EDITOR}. |
7216 | ||
de3341d1 CR |
7217 | @item |
7218 | When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret | |
7219 | any arguments to @code{echo} as options. Each argument is displayed, after | |
7220 | escape characters are converted. | |
7221 | ||
e33f2203 CR |
7222 | @item |
7223 | The @code{ulimit} builtin uses a block size of 512 bytes for the @option{-c} | |
7224 | and @option{-f} options. | |
7225 | ||
176b12ee CR |
7226 | @item |
7227 | The arrival of @code{SIGCHLD} when a trap is set on @code{SIGCHLD} does | |
7228 | not interrupt the @code{wait} builtin and cause it to return immediately. | |
7229 | The trap command is run once for each child that exits. | |
7230 | ||
5a318736 CR |
7231 | @item |
7232 | The @code{read} builtin may be interrupted by a signal for which a trap | |
7233 | has been set. | |
7234 | If Bash receives a trapped signal while executing @code{read}, the trap | |
7235 | handler executes and @code{read} returns an exit status greater than 128. | |
7236 | ||
bb70624e | 7237 | @end enumerate |
ccc6cda3 | 7238 | |
ac18b312 | 7239 | There is other @sc{posix} behavior that Bash does not implement by |
7790f917 | 7240 | default even when in @sc{posix} mode. |
bb70624e | 7241 | Specifically: |
ccc6cda3 | 7242 | |
bb70624e | 7243 | @enumerate |
28ef6c31 JA |
7244 | |
7245 | @item | |
7790f917 CR |
7246 | The @code{fc} builtin checks @code{$EDITOR} as a program to edit history |
7247 | entries if @code{FCEDIT} is unset, rather than defaulting directly to | |
7248 | @code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset. | |
28ef6c31 | 7249 | |
ff247e74 | 7250 | @item |
7790f917 CR |
7251 | As noted above, Bash requires the @code{xpg_echo} option to be enabled for |
7252 | the @code{echo} builtin to be fully conformant. | |
ff247e74 | 7253 | |
bb70624e | 7254 | @end enumerate |
ccc6cda3 | 7255 | |
7790f917 CR |
7256 | Bash can be configured to be @sc{posix}-conformant by default, by specifying |
7257 | the @option{--enable-strict-posix-default} to @code{configure} when building | |
7258 | (@pxref{Optional Features}). | |
7259 | ||
bb70624e JA |
7260 | @node Job Control |
7261 | @chapter Job Control | |
ccc6cda3 | 7262 | |
bb70624e JA |
7263 | This chapter discusses what job control is, how it works, and how |
7264 | Bash allows you to access its facilities. | |
ccc6cda3 | 7265 | |
bb70624e JA |
7266 | @menu |
7267 | * Job Control Basics:: How job control works. | |
7268 | * Job Control Builtins:: Bash builtin commands used to interact | |
7269 | with job control. | |
7270 | * Job Control Variables:: Variables Bash uses to customize job | |
7271 | control. | |
7272 | @end menu | |
ccc6cda3 | 7273 | |
bb70624e JA |
7274 | @node Job Control Basics |
7275 | @section Job Control Basics | |
7276 | @cindex job control | |
7277 | @cindex foreground | |
7278 | @cindex background | |
7279 | @cindex suspending jobs | |
ccc6cda3 | 7280 | |
bb70624e JA |
7281 | Job control |
7282 | refers to the ability to selectively stop (suspend) | |
7283 | the execution of processes and continue (resume) | |
7284 | their execution at a later point. A user typically employs | |
7285 | this facility via an interactive interface supplied jointly | |
602bb739 | 7286 | by the operating system kernel's terminal driver and Bash. |
ccc6cda3 | 7287 | |
bb70624e JA |
7288 | The shell associates a @var{job} with each pipeline. It keeps a |
7289 | table of currently executing jobs, which may be listed with the | |
7290 | @code{jobs} command. When Bash starts a job | |
7291 | asynchronously, it prints a line that looks | |
7292 | like: | |
7293 | @example | |
7294 | [1] 25647 | |
7295 | @end example | |
7296 | @noindent | |
7297 | indicating that this job is job number 1 and that the process @sc{id} | |
7298 | of the last process in the pipeline associated with this job is | |
7299 | 25647. All of the processes in a single pipeline are members of | |
7300 | the same job. Bash uses the @var{job} abstraction as the | |
7301 | basis for job control. | |
ccc6cda3 | 7302 | |
bb70624e JA |
7303 | To facilitate the implementation of the user interface to job |
7304 | control, the operating system maintains the notion of a current terminal | |
7305 | process group @sc{id}. Members of this process group (processes whose | |
7306 | process group @sc{id} is equal to the current terminal process group | |
7307 | @sc{id}) receive keyboard-generated signals such as @code{SIGINT}. | |
7308 | These processes are said to be in the foreground. Background | |
7309 | processes are those whose process group @sc{id} differs from the | |
7310 | terminal's; such processes are immune to keyboard-generated | |
602bb739 CR |
7311 | signals. Only foreground processes are allowed to read from or, if |
7312 | the user so specifies with @code{stty tostop}, write to the terminal. | |
7313 | Background processes which attempt to | |
7314 | read from (write to when @code{stty tostop} is in effect) the | |
7315 | terminal are sent a @code{SIGTTIN} (@code{SIGTTOU}) | |
7316 | signal by the kernel's terminal driver, | |
7317 | which, unless caught, suspends the process. | |
ccc6cda3 | 7318 | |
bb70624e JA |
7319 | If the operating system on which Bash is running supports |
7320 | job control, Bash contains facilities to use it. Typing the | |
7321 | @var{suspend} character (typically @samp{^Z}, Control-Z) while a | |
7322 | process is running causes that process to be stopped and returns | |
7323 | control to Bash. Typing the @var{delayed suspend} character | |
7324 | (typically @samp{^Y}, Control-Y) causes the process to be stopped | |
7325 | when it attempts to read input from the terminal, and control to | |
7326 | be returned to Bash. The user then manipulates the state of | |
7327 | this job, using the @code{bg} command to continue it in the | |
7328 | background, the @code{fg} command to continue it in the | |
7329 | foreground, or the @code{kill} command to kill it. A @samp{^Z} | |
7330 | takes effect immediately, and has the additional side effect of | |
7331 | causing pending output and typeahead to be discarded. | |
ccc6cda3 | 7332 | |
bb70624e | 7333 | There are a number of ways to refer to a job in the shell. The |
8e1a6eaa | 7334 | character @samp{%} introduces a job specification (@var{jobspec}). |
cce855bc | 7335 | |
bb70624e | 7336 | Job number @code{n} may be referred to as @samp{%n}. |
43df7bbb CR |
7337 | The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the |
7338 | current job, which is the last job stopped while it was in the foreground | |
7339 | or started in the background. | |
7340 | A single @samp{%} (with no accompanying job specification) also refers | |
7341 | to the current job. | |
e33f2203 CR |
7342 | The previous job may be referenced using @samp{%-}. |
7343 | If there is only a single job, @samp{%+} and @samp{%-} can both be used | |
7344 | to refer to that job. | |
7345 | In output pertaining to jobs (e.g., the output of the @code{jobs} | |
7346 | command), the current job is always flagged with a @samp{+}, and the | |
bb70624e | 7347 | previous job with a @samp{-}. |
cce855bc | 7348 | |
bb70624e JA |
7349 | A job may also be referred to |
7350 | using a prefix of the name used to start it, or using a substring | |
7351 | that appears in its command line. For example, @samp{%ce} refers | |
b28ff8c9 | 7352 | to a stopped @code{ce} job. Using @samp{%?ce}, on the |
bb70624e JA |
7353 | other hand, refers to any job containing the string @samp{ce} in |
7354 | its command line. If the prefix or substring matches more than one job, | |
7355 | Bash reports an error. | |
cce855bc | 7356 | |
bb70624e JA |
7357 | Simply naming a job can be used to bring it into the foreground: |
7358 | @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the | |
7359 | background into the foreground. Similarly, @samp{%1 &} resumes | |
7360 | job 1 in the background, equivalent to @samp{bg %1} | |
cce855bc | 7361 | |
bb70624e JA |
7362 | The shell learns immediately whenever a job changes state. |
7363 | Normally, Bash waits until it is about to print a prompt | |
7364 | before reporting changes in a job's status so as to not interrupt | |
f73dda09 JA |
7365 | any other output. |
7366 | If the @option{-b} option to the @code{set} builtin is enabled, | |
bb70624e | 7367 | Bash reports such changes immediately (@pxref{The Set Builtin}). |
f73dda09 JA |
7368 | Any trap on @code{SIGCHLD} is executed for each child process |
7369 | that exits. | |
ccc6cda3 | 7370 | |
d3ad40de CR |
7371 | If an attempt to exit Bash is made while jobs are stopped, (or running, if |
7372 | the @code{checkjobs} option is enabled -- see @ref{The Shopt Builtin}), the | |
7373 | shell prints a warning message, and if the @code{checkjobs} option is | |
7374 | enabled, lists the jobs and their statuses. | |
bb70624e JA |
7375 | The @code{jobs} command may then be used to inspect their status. |
7376 | If a second attempt to exit is made without an intervening command, | |
d3ad40de | 7377 | Bash does not print another warning, and any stopped jobs are terminated. |
ccc6cda3 | 7378 | |
bb70624e JA |
7379 | @node Job Control Builtins |
7380 | @section Job Control Builtins | |
ccc6cda3 JA |
7381 | |
7382 | @table @code | |
7383 | ||
bb70624e JA |
7384 | @item bg |
7385 | @btindex bg | |
ccc6cda3 | 7386 | @example |
de3341d1 | 7387 | bg [@var{jobspec} @dots{}] |
ccc6cda3 | 7388 | @end example |
b28ff8c9 | 7389 | |
de3341d1 | 7390 | Resume each suspended job @var{jobspec} in the background, as if it |
bb70624e JA |
7391 | had been started with @samp{&}. |
7392 | If @var{jobspec} is not supplied, the current job is used. | |
7393 | The return status is zero unless it is run when job control is not | |
ff247e74 CR |
7394 | enabled, or, when run with job control enabled, any |
7395 | @var{jobspec} was not found or specifies a job | |
de3341d1 | 7396 | that was started without job control. |
cce855bc | 7397 | |
bb70624e JA |
7398 | @item fg |
7399 | @btindex fg | |
7400 | @example | |
7401 | fg [@var{jobspec}] | |
7402 | @end example | |
b28ff8c9 | 7403 | |
bb70624e JA |
7404 | Resume the job @var{jobspec} in the foreground and make it the current job. |
7405 | If @var{jobspec} is not supplied, the current job is used. | |
7406 | The return status is that of the command placed into the foreground, | |
7407 | or non-zero if run when job control is disabled or, when run with | |
7408 | job control enabled, @var{jobspec} does not specify a valid job or | |
7409 | @var{jobspec} specifies a job that was started without job control. | |
cce855bc | 7410 | |
bb70624e JA |
7411 | @item jobs |
7412 | @btindex jobs | |
cce855bc | 7413 | @example |
bb70624e JA |
7414 | jobs [-lnprs] [@var{jobspec}] |
7415 | jobs -x @var{command} [@var{arguments}] | |
cce855bc JA |
7416 | @end example |
7417 | ||
bb70624e JA |
7418 | The first form lists the active jobs. The options have the |
7419 | following meanings: | |
7420 | ||
7421 | @table @code | |
7422 | @item -l | |
7423 | List process @sc{id}s in addition to the normal information. | |
7424 | ||
7425 | @item -n | |
7426 | Display information only about jobs that have changed status since | |
7427 | the user was last notified of their status. | |
7428 | ||
7429 | @item -p | |
7430 | List only the process @sc{id} of the job's process group leader. | |
7431 | ||
7432 | @item -r | |
b28ff8c9 | 7433 | Display only running jobs. |
bb70624e JA |
7434 | |
7435 | @item -s | |
b28ff8c9 | 7436 | Display only stopped jobs. |
ccc6cda3 JA |
7437 | @end table |
7438 | ||
bb70624e JA |
7439 | If @var{jobspec} is given, |
7440 | output is restricted to information about that job. | |
7441 | If @var{jobspec} is not supplied, the status of all jobs is | |
7442 | listed. | |
ccc6cda3 | 7443 | |
28ef6c31 | 7444 | If the @option{-x} option is supplied, @code{jobs} replaces any |
bb70624e JA |
7445 | @var{jobspec} found in @var{command} or @var{arguments} with the |
7446 | corresponding process group @sc{id}, and executes @var{command}, | |
7447 | passing it @var{argument}s, returning its exit status. | |
ccc6cda3 | 7448 | |
bb70624e JA |
7449 | @item kill |
7450 | @btindex kill | |
ccc6cda3 | 7451 | @example |
bb70624e JA |
7452 | kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid} |
7453 | kill -l [@var{exit_status}] | |
ccc6cda3 | 7454 | @end example |
b28ff8c9 | 7455 | |
bb70624e JA |
7456 | Send a signal specified by @var{sigspec} or @var{signum} to the process |
7457 | named by job specification @var{jobspec} or process @sc{id} @var{pid}. | |
61deeb13 CR |
7458 | @var{sigspec} is either a case-insensitive signal name such as |
7459 | @code{SIGINT} (with or without the @code{SIG} prefix) | |
7460 | or a signal number; @var{signum} is a signal number. | |
bb70624e | 7461 | If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used. |
28ef6c31 JA |
7462 | The @option{-l} option lists the signal names. |
7463 | If any arguments are supplied when @option{-l} is given, the names of the | |
bb70624e JA |
7464 | signals corresponding to the arguments are listed, and the return status |
7465 | is zero. | |
7466 | @var{exit_status} is a number specifying a signal number or the exit | |
7467 | status of a process terminated by a signal. | |
7468 | The return status is zero if at least one signal was successfully sent, | |
7469 | or non-zero if an error occurs or an invalid option is encountered. | |
ccc6cda3 | 7470 | |
bb70624e JA |
7471 | @item wait |
7472 | @btindex wait | |
ccc6cda3 | 7473 | @example |
b28ff8c9 | 7474 | wait [@var{jobspec} or @var{pid} @dots{}] |
ccc6cda3 | 7475 | @end example |
b28ff8c9 | 7476 | |
d90269dd CR |
7477 | Wait until the child process specified by each process @sc{id} @var{pid} |
7478 | or job specification @var{jobspec} exits and return the exit status of the | |
7479 | last command waited for. | |
bb70624e JA |
7480 | If a job spec is given, all processes in the job are waited for. |
7481 | If no arguments are given, all currently active child processes are | |
7482 | waited for, and the return status is zero. | |
7483 | If neither @var{jobspec} nor @var{pid} specifies an active child process | |
7484 | of the shell, the return status is 127. | |
7485 | ||
7486 | @item disown | |
7487 | @btindex disown | |
ccc6cda3 | 7488 | @example |
bb70624e | 7489 | disown [-ar] [-h] [@var{jobspec} @dots{}] |
ccc6cda3 | 7490 | @end example |
b28ff8c9 CR |
7491 | |
7492 | Without options, remove each @var{jobspec} from the table of | |
bb70624e | 7493 | active jobs. |
28ef6c31 | 7494 | If the @option{-h} option is given, the job is not removed from the table, |
bb70624e JA |
7495 | but is marked so that @code{SIGHUP} is not sent to the job if the shell |
7496 | receives a @code{SIGHUP}. | |
28ef6c31 | 7497 | If @var{jobspec} is not present, and neither the @option{-a} nor @option{-r} |
bb70624e | 7498 | option is supplied, the current job is used. |
28ef6c31 JA |
7499 | If no @var{jobspec} is supplied, the @option{-a} option means to remove or |
7500 | mark all jobs; the @option{-r} option without a @var{jobspec} | |
bb70624e | 7501 | argument restricts operation to running jobs. |
ccc6cda3 | 7502 | |
bb70624e JA |
7503 | @item suspend |
7504 | @btindex suspend | |
ccc6cda3 | 7505 | @example |
bb70624e | 7506 | suspend [-f] |
ccc6cda3 | 7507 | @end example |
b28ff8c9 | 7508 | |
bb70624e | 7509 | Suspend the execution of this shell until it receives a |
09767ff0 CR |
7510 | @code{SIGCONT} signal. |
7511 | A login shell cannot be suspended; the @option{-f} | |
7512 | option can be used to override this and force the suspension. | |
bb70624e | 7513 | @end table |
ccc6cda3 | 7514 | |
bb70624e JA |
7515 | When job control is not active, the @code{kill} and @code{wait} |
7516 | builtins do not accept @var{jobspec} arguments. They must be | |
7517 | supplied process @sc{id}s. | |
ccc6cda3 | 7518 | |
bb70624e JA |
7519 | @node Job Control Variables |
7520 | @section Job Control Variables | |
ccc6cda3 | 7521 | |
bb70624e | 7522 | @vtable @code |
cce855bc | 7523 | |
bb70624e JA |
7524 | @item auto_resume |
7525 | This variable controls how the shell interacts with the user and | |
7526 | job control. If this variable exists then single word simple | |
7527 | commands without redirections are treated as candidates for resumption | |
7528 | of an existing job. There is no ambiguity allowed; if there is | |
7529 | more than one job beginning with the string typed, then | |
7530 | the most recently accessed job will be selected. | |
7531 | The name of a stopped job, in this context, is the command line | |
7532 | used to start it. If this variable is set to the value @samp{exact}, | |
7533 | the string supplied must match the name of a stopped job exactly; | |
7534 | if set to @samp{substring}, | |
7535 | the string supplied needs to match a substring of the name of a | |
7536 | stopped job. The @samp{substring} value provides functionality | |
7537 | analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}). | |
7538 | If set to any other value, the supplied string must | |
7539 | be a prefix of a stopped job's name; this provides functionality | |
7540 | analogous to the @samp{%} job @sc{id}. | |
cce855bc | 7541 | |
bb70624e | 7542 | @end vtable |
cce855bc | 7543 | |
bb70624e JA |
7544 | @set readline-appendix |
7545 | @set history-appendix | |
7546 | @cindex Readline, how to use | |
d3a24ed2 | 7547 | @include rluser.texi |
bb70624e | 7548 | @cindex History, how to use |
d3a24ed2 | 7549 | @include hsuser.texi |
bb70624e JA |
7550 | @clear readline-appendix |
7551 | @clear history-appendix | |
cce855bc | 7552 | |
bb70624e JA |
7553 | @node Installing Bash |
7554 | @chapter Installing Bash | |
cce855bc | 7555 | |
bb70624e JA |
7556 | This chapter provides basic instructions for installing Bash on |
7557 | the various supported platforms. The distribution supports the | |
7558 | @sc{gnu} operating systems, nearly every version of Unix, and several | |
7559 | non-Unix systems such as BeOS and Interix. | |
7560 | Other independent ports exist for | |
d3a24ed2 | 7561 | @sc{ms-dos}, @sc{os/2}, and Windows platforms. |
cce855bc | 7562 | |
bb70624e JA |
7563 | @menu |
7564 | * Basic Installation:: Installation instructions. | |
bb70624e JA |
7565 | * Compilers and Options:: How to set special options for various |
7566 | systems. | |
bb70624e JA |
7567 | * Compiling For Multiple Architectures:: How to compile Bash for more |
7568 | than one kind of system from | |
7569 | the same source tree. | |
bb70624e | 7570 | * Installation Names:: How to set the various paths used by the installation. |
bb70624e | 7571 | * Specifying the System Type:: How to configure Bash for a particular system. |
bb70624e JA |
7572 | * Sharing Defaults:: How to share default configuration values among GNU |
7573 | programs. | |
bb70624e | 7574 | * Operation Controls:: Options recognized by the configuration program. |
bb70624e JA |
7575 | * Optional Features:: How to enable and disable optional features when |
7576 | building Bash. | |
7577 | @end menu | |
ccc6cda3 | 7578 | |
bb70624e JA |
7579 | @node Basic Installation |
7580 | @section Basic Installation | |
7581 | @cindex installation | |
7582 | @cindex configuration | |
7583 | @cindex Bash installation | |
7584 | @cindex Bash configuration | |
ccc6cda3 | 7585 | |
bb70624e | 7586 | These are installation instructions for Bash. |
ccc6cda3 | 7587 | |
bb70624e | 7588 | The simplest way to compile Bash is: |
ccc6cda3 | 7589 | |
bb70624e | 7590 | @enumerate |
ccc6cda3 | 7591 | @item |
bb70624e JA |
7592 | @code{cd} to the directory containing the source code and type |
7593 | @samp{./configure} to configure Bash for your system. If you're | |
7594 | using @code{csh} on an old version of System V, you might need to | |
7595 | type @samp{sh ./configure} instead to prevent @code{csh} from trying | |
7596 | to execute @code{configure} itself. | |
7597 | ||
7598 | Running @code{configure} takes some time. | |
7599 | While running, it prints messages telling which features it is | |
7600 | checking for. | |
7601 | ||
ccc6cda3 | 7602 | @item |
bb70624e JA |
7603 | Type @samp{make} to compile Bash and build the @code{bashbug} bug |
7604 | reporting script. | |
7605 | ||
ccc6cda3 | 7606 | @item |
bb70624e JA |
7607 | Optionally, type @samp{make tests} to run the Bash test suite. |
7608 | ||
ccc6cda3 | 7609 | @item |
bb70624e JA |
7610 | Type @samp{make install} to install @code{bash} and @code{bashbug}. |
7611 | This will also install the manual pages and Info file. | |
ccc6cda3 | 7612 | |
bb70624e | 7613 | @end enumerate |
ccc6cda3 | 7614 | |
bb70624e JA |
7615 | The @code{configure} shell script attempts to guess correct |
7616 | values for various system-dependent variables used during | |
7617 | compilation. It uses those values to create a @file{Makefile} in | |
7618 | each directory of the package (the top directory, the | |
7619 | @file{builtins}, @file{doc}, and @file{support} directories, | |
7620 | each directory under @file{lib}, and several others). It also creates a | |
7621 | @file{config.h} file containing system-dependent definitions. | |
7622 | Finally, it creates a shell script named @code{config.status} that you | |
7623 | can run in the future to recreate the current configuration, a | |
7624 | file @file{config.cache} that saves the results of its tests to | |
7625 | speed up reconfiguring, and a file @file{config.log} containing | |
7626 | compiler output (useful mainly for debugging @code{configure}). | |
7627 | If at some point | |
7628 | @file{config.cache} contains results you don't want to keep, you | |
7629 | may remove or edit it. | |
ccc6cda3 | 7630 | |
bb70624e JA |
7631 | To find out more about the options and arguments that the |
7632 | @code{configure} script understands, type | |
ccc6cda3 | 7633 | |
bb70624e JA |
7634 | @example |
7635 | bash-2.04$ ./configure --help | |
7636 | @end example | |
ccc6cda3 | 7637 | |
bb70624e JA |
7638 | @noindent |
7639 | at the Bash prompt in your Bash source directory. | |
ccc6cda3 | 7640 | |
bb70624e JA |
7641 | If you need to do unusual things to compile Bash, please |
7642 | try to figure out how @code{configure} could check whether or not | |
7643 | to do them, and mail diffs or instructions to | |
7644 | @email{bash-maintainers@@gnu.org} so they can be | |
7645 | considered for the next release. | |
ccc6cda3 | 7646 | |
bb70624e JA |
7647 | The file @file{configure.in} is used to create @code{configure} |
7648 | by a program called Autoconf. You only need | |
7649 | @file{configure.in} if you want to change it or regenerate | |
7650 | @code{configure} using a newer version of Autoconf. If | |
f73dda09 | 7651 | you do this, make sure you are using Autoconf version 2.50 or |
bb70624e | 7652 | newer. |
ccc6cda3 | 7653 | |
bb70624e JA |
7654 | You can remove the program binaries and object files from the |
7655 | source code directory by typing @samp{make clean}. To also remove the | |
7656 | files that @code{configure} created (so you can compile Bash for | |
7657 | a different kind of computer), type @samp{make distclean}. | |
ccc6cda3 | 7658 | |
bb70624e JA |
7659 | @node Compilers and Options |
7660 | @section Compilers and Options | |
ccc6cda3 | 7661 | |
bb70624e JA |
7662 | Some systems require unusual options for compilation or linking |
7663 | that the @code{configure} script does not know about. You can | |
7664 | give @code{configure} initial values for variables by setting | |
7665 | them in the environment. Using a Bourne-compatible shell, you | |
7666 | can do that on the command line like this: | |
ccc6cda3 | 7667 | |
bb70624e JA |
7668 | @example |
7669 | CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure | |
7670 | @end example | |
ccc6cda3 | 7671 | |
bb70624e | 7672 | On systems that have the @code{env} program, you can do it like this: |
ccc6cda3 | 7673 | |
bb70624e JA |
7674 | @example |
7675 | env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure | |
7676 | @end example | |
ccc6cda3 | 7677 | |
bb70624e JA |
7678 | The configuration process uses GCC to build Bash if it |
7679 | is available. | |
cce855bc | 7680 | |
bb70624e JA |
7681 | @node Compiling For Multiple Architectures |
7682 | @section Compiling For Multiple Architectures | |
ccc6cda3 | 7683 | |
bb70624e JA |
7684 | You can compile Bash for more than one kind of computer at the |
7685 | same time, by placing the object files for each architecture in their | |
7686 | own directory. To do this, you must use a version of @code{make} that | |
7687 | supports the @code{VPATH} variable, such as GNU @code{make}. | |
7688 | @code{cd} to the | |
7689 | directory where you want the object files and executables to go and run | |
7690 | the @code{configure} script from the source directory. You may need to | |
28ef6c31 | 7691 | supply the @option{--srcdir=PATH} argument to tell @code{configure} where the |
bb70624e JA |
7692 | source files are. @code{configure} automatically checks for the |
7693 | source code in the directory that @code{configure} is in and in `..'. | |
ccc6cda3 | 7694 | |
bb70624e JA |
7695 | If you have to use a @code{make} that does not supports the @code{VPATH} |
7696 | variable, you can compile Bash for one architecture at a | |
7697 | time in the source code directory. After you have installed | |
7698 | Bash for one architecture, use @samp{make distclean} before | |
7699 | reconfiguring for another architecture. | |
ccc6cda3 | 7700 | |
bb70624e JA |
7701 | Alternatively, if your system supports symbolic links, you can use the |
7702 | @file{support/mkclone} script to create a build tree which has | |
7703 | symbolic links back to each file in the source directory. Here's an | |
7704 | example that creates a build directory in the current directory from a | |
7705 | source directory @file{/usr/gnu/src/bash-2.0}: | |
7706 | ||
7707 | @example | |
7708 | bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 . | |
7709 | @end example | |
7710 | ||
7711 | @noindent | |
7712 | The @code{mkclone} script requires Bash, so you must have already built | |
7713 | Bash for at least one architecture before you can create build | |
7714 | directories for other architectures. | |
7715 | ||
7716 | @node Installation Names | |
7717 | @section Installation Names | |
ccc6cda3 | 7718 | |
bb70624e JA |
7719 | By default, @samp{make install} will install into |
7720 | @file{/usr/local/bin}, @file{/usr/local/man}, etc. You can | |
7721 | specify an installation prefix other than @file{/usr/local} by | |
f73dda09 JA |
7722 | giving @code{configure} the option @option{--prefix=@var{PATH}}, |
7723 | or by specifying a value for the @code{DESTDIR} @samp{make} | |
7724 | variable when running @samp{make install}. | |
ccc6cda3 | 7725 | |
bb70624e JA |
7726 | You can specify separate installation prefixes for |
7727 | architecture-specific files and architecture-independent files. | |
7728 | If you give @code{configure} the option | |
28ef6c31 | 7729 | @option{--exec-prefix=@var{PATH}}, @samp{make install} will use |
bb70624e JA |
7730 | @var{PATH} as the prefix for installing programs and libraries. |
7731 | Documentation and other data files will still use the regular prefix. | |
cce855bc | 7732 | |
bb70624e JA |
7733 | @node Specifying the System Type |
7734 | @section Specifying the System Type | |
ccc6cda3 | 7735 | |
bb70624e | 7736 | There may be some features @code{configure} can not figure out |
f73dda09 | 7737 | automatically, but need to determine by the type of host Bash |
bb70624e JA |
7738 | will run on. Usually @code{configure} can figure that |
7739 | out, but if it prints a message saying it can not guess the host | |
28ef6c31 | 7740 | type, give it the @option{--host=TYPE} option. @samp{TYPE} can |
bb70624e JA |
7741 | either be a short name for the system type, such as @samp{sun4}, |
7742 | or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM} | |
f73dda09 | 7743 | (e.g., @samp{i386-unknown-freebsd4.2}). |
ccc6cda3 | 7744 | |
bb70624e JA |
7745 | See the file @file{support/config.sub} for the possible |
7746 | values of each field. | |
ccc6cda3 | 7747 | |
bb70624e JA |
7748 | @node Sharing Defaults |
7749 | @section Sharing Defaults | |
ccc6cda3 | 7750 | |
bb70624e JA |
7751 | If you want to set default values for @code{configure} scripts to |
7752 | share, you can create a site shell script called | |
7753 | @code{config.site} that gives default values for variables like | |
7754 | @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure} | |
7755 | looks for @file{PREFIX/share/config.site} if it exists, then | |
7756 | @file{PREFIX/etc/config.site} if it exists. Or, you can set the | |
7757 | @code{CONFIG_SITE} environment variable to the location of the site | |
7758 | script. A warning: the Bash @code{configure} looks for a site script, | |
7759 | but not all @code{configure} scripts do. | |
d166f048 | 7760 | |
bb70624e JA |
7761 | @node Operation Controls |
7762 | @section Operation Controls | |
ccc6cda3 | 7763 | |
bb70624e JA |
7764 | @code{configure} recognizes the following options to control how it |
7765 | operates. | |
ccc6cda3 | 7766 | |
bb70624e | 7767 | @table @code |
ccc6cda3 | 7768 | |
bb70624e JA |
7769 | @item --cache-file=@var{file} |
7770 | Use and save the results of the tests in | |
7771 | @var{file} instead of @file{./config.cache}. Set @var{file} to | |
7772 | @file{/dev/null} to disable caching, for debugging | |
7773 | @code{configure}. | |
ccc6cda3 | 7774 | |
bb70624e JA |
7775 | @item --help |
7776 | Print a summary of the options to @code{configure}, and exit. | |
ccc6cda3 | 7777 | |
bb70624e JA |
7778 | @item --quiet |
7779 | @itemx --silent | |
7780 | @itemx -q | |
7781 | Do not print messages saying which checks are being made. | |
ccc6cda3 | 7782 | |
bb70624e JA |
7783 | @item --srcdir=@var{dir} |
7784 | Look for the Bash source code in directory @var{dir}. Usually | |
7785 | @code{configure} can determine that directory automatically. | |
ccc6cda3 | 7786 | |
bb70624e JA |
7787 | @item --version |
7788 | Print the version of Autoconf used to generate the @code{configure} | |
7789 | script, and exit. | |
7790 | @end table | |
ccc6cda3 | 7791 | |
bb70624e JA |
7792 | @code{configure} also accepts some other, not widely used, boilerplate |
7793 | options. @samp{configure --help} prints the complete list. | |
ccc6cda3 | 7794 | |
bb70624e JA |
7795 | @node Optional Features |
7796 | @section Optional Features | |
ccc6cda3 | 7797 | |
28ef6c31 | 7798 | The Bash @code{configure} has a number of @option{--enable-@var{feature}} |
bb70624e | 7799 | options, where @var{feature} indicates an optional part of Bash. |
28ef6c31 | 7800 | There are also several @option{--with-@var{package}} options, |
bb70624e JA |
7801 | where @var{package} is something like @samp{bash-malloc} or @samp{purify}. |
7802 | To turn off the default use of a package, use | |
28ef6c31 JA |
7803 | @option{--without-@var{package}}. To configure Bash without a feature |
7804 | that is enabled by default, use @option{--disable-@var{feature}}. | |
ccc6cda3 | 7805 | |
28ef6c31 JA |
7806 | Here is a complete list of the @option{--enable-} and |
7807 | @option{--with-} options that the Bash @code{configure} recognizes. | |
ccc6cda3 | 7808 | |
bb70624e JA |
7809 | @table @code |
7810 | @item --with-afs | |
7811 | Define if you are using the Andrew File System from Transarc. | |
ccc6cda3 | 7812 | |
bb70624e JA |
7813 | @item --with-bash-malloc |
7814 | Use the Bash version of | |
7790f917 | 7815 | @code{malloc} in the directory @file{lib/malloc}. This is not the same |
bb70624e | 7816 | @code{malloc} that appears in @sc{gnu} libc, but an older version |
7790f917 CR |
7817 | originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc} |
7818 | is very fast, but wastes some space on each allocation. | |
bb70624e JA |
7819 | This option is enabled by default. |
7820 | The @file{NOTES} file contains a list of systems for | |
7821 | which this should be turned off, and @code{configure} disables this | |
7822 | option automatically for a number of systems. | |
ccc6cda3 | 7823 | |
bb70624e JA |
7824 | @item --with-curses |
7825 | Use the curses library instead of the termcap library. This should | |
7826 | be supplied if your system has an inadequate or incomplete termcap | |
7827 | database. | |
ccc6cda3 | 7828 | |
bb70624e JA |
7829 | @item --with-gnu-malloc |
7830 | A synonym for @code{--with-bash-malloc}. | |
ccc6cda3 | 7831 | |
f73dda09 | 7832 | @item --with-installed-readline[=@var{PREFIX}] |
bb70624e JA |
7833 | Define this to make Bash link with a locally-installed version of Readline |
7834 | rather than the version in @file{lib/readline}. This works only with | |
01ed5ba4 | 7835 | Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not |
f73dda09 JA |
7836 | supplied, @code{configure} uses the values of the make variables |
7837 | @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix} | |
7838 | by default, to find the installed version of Readline if it is not in | |
7839 | the standard system include and library directories. | |
7840 | If @var{PREFIX} is @code{no}, Bash links with the version in | |
7841 | @file{lib/readline}. | |
7842 | If @var{PREFIX} is set to any other value, @code{configure} treats it as | |
7843 | a directory pathname and looks for | |
7844 | the installed version of Readline in subdirectories of that directory | |
7845 | (include files in @var{PREFIX}/@code{include} and the library in | |
7846 | @var{PREFIX}/@code{lib}). | |
ccc6cda3 | 7847 | |
bb70624e JA |
7848 | @item --with-purify |
7849 | Define this to use the Purify memory allocation checker from Rational | |
7850 | Software. | |
ccc6cda3 | 7851 | |
bb70624e JA |
7852 | @item --enable-minimal-config |
7853 | This produces a shell with minimal features, close to the historical | |
7854 | Bourne shell. | |
7855 | @end table | |
ccc6cda3 | 7856 | |
28ef6c31 | 7857 | There are several @option{--enable-} options that alter how Bash is |
bb70624e | 7858 | compiled and linked, rather than changing run-time features. |
ccc6cda3 JA |
7859 | |
7860 | @table @code | |
f73dda09 JA |
7861 | @item --enable-largefile |
7862 | Enable support for @uref{http://www.sas.com/standards/large_file/x_open.20Mar96.html, | |
7863 | large files} if the operating system requires special compiler options | |
7117c2d2 JA |
7864 | to build programs which can access large files. This is enabled by |
7865 | default, if the operating system provides large file support. | |
f73dda09 | 7866 | |
bb70624e JA |
7867 | @item --enable-profiling |
7868 | This builds a Bash binary that produces profiling information to be | |
7869 | processed by @code{gprof} each time it is executed. | |
ccc6cda3 | 7870 | |
bb70624e JA |
7871 | @item --enable-static-link |
7872 | This causes Bash to be linked statically, if @code{gcc} is being used. | |
7873 | This could be used to build a version to use as root's shell. | |
7874 | @end table | |
7875 | ||
7876 | The @samp{minimal-config} option can be used to disable all of | |
7877 | the following options, but it is processed first, so individual | |
7878 | options may be enabled using @samp{enable-@var{feature}}. | |
7879 | ||
7880 | All of the following options except for @samp{disabled-builtins} and | |
7881 | @samp{xpg-echo-default} are | |
7882 | enabled by default, unless the operating system does not provide the | |
7883 | necessary support. | |
7884 | ||
7885 | @table @code | |
7886 | @item --enable-alias | |
7887 | Allow alias expansion and include the @code{alias} and @code{unalias} | |
7888 | builtins (@pxref{Aliases}). | |
ccc6cda3 | 7889 | |
bb70624e JA |
7890 | @item --enable-arith-for-command |
7891 | Include support for the alternate form of the @code{for} command | |
7892 | that behaves like the C language @code{for} statement | |
7893 | (@pxref{Looping Constructs}). | |
ccc6cda3 | 7894 | |
bb70624e JA |
7895 | @item --enable-array-variables |
7896 | Include support for one-dimensional array shell variables | |
7897 | (@pxref{Arrays}). | |
ccc6cda3 | 7898 | |
bb70624e JA |
7899 | @item --enable-bang-history |
7900 | Include support for @code{csh}-like history substitution | |
7901 | (@pxref{History Interaction}). | |
ccc6cda3 | 7902 | |
bb70624e JA |
7903 | @item --enable-brace-expansion |
7904 | Include @code{csh}-like brace expansion | |
7905 | ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ). | |
7906 | See @ref{Brace Expansion}, for a complete description. | |
ccc6cda3 | 7907 | |
fdf670ea CR |
7908 | @item --enable-casemod-attributes |
7909 | Include support for case-modifying attributes in the @code{declare} builtin | |
7910 | and assignment statements. Variables with the @var{uppercase} attribute, | |
7911 | for example, will have their values converted to uppercase upon assignment. | |
7912 | ||
7913 | @item --enable-casemod-expansion | |
7914 | Include support for case-modifying word expansions. | |
7915 | ||
bb70624e JA |
7916 | @item --enable-command-timing |
7917 | Include support for recognizing @code{time} as a reserved word and for | |
7918 | displaying timing statistics for the pipeline following @code{time} | |
7919 | (@pxref{Pipelines}). | |
7920 | This allows pipelines as well as shell builtins and functions to be timed. | |
ccc6cda3 | 7921 | |
bb70624e | 7922 | @item --enable-cond-command |
d3a24ed2 CR |
7923 | Include support for the @code{[[} conditional command. |
7924 | (@pxref{Conditional Constructs}). | |
7925 | ||
7926 | @item --enable-cond-regexp | |
5cdaaf76 | 7927 | Include support for matching @sc{posix} regular expressions using the |
d3a24ed2 | 7928 | @samp{=~} binary operator in the @code{[[} conditional command. |
bb70624e | 7929 | (@pxref{Conditional Constructs}). |
ccc6cda3 | 7930 | |
09767ff0 CR |
7931 | @item --enable-coprocesses |
7932 | Include support for coprocesses and the @code{coproc} reserved word | |
7933 | (@pxref{Pipelines}). | |
7934 | ||
01ed5ba4 CR |
7935 | @item --enable-debugger |
7936 | Include support for the bash debugger (distributed separately). | |
7937 | ||
bb70624e JA |
7938 | @item --enable-directory-stack |
7939 | Include support for a @code{csh}-like directory stack and the | |
7940 | @code{pushd}, @code{popd}, and @code{dirs} builtins | |
7941 | (@pxref{The Directory Stack}). | |
ccc6cda3 | 7942 | |
bb70624e JA |
7943 | @item --enable-disabled-builtins |
7944 | Allow builtin commands to be invoked via @samp{builtin xxx} | |
7945 | even after @code{xxx} has been disabled using @samp{enable -n xxx}. | |
7946 | See @ref{Bash Builtins}, for details of the @code{builtin} and | |
7947 | @code{enable} builtin commands. | |
ccc6cda3 | 7948 | |
bb70624e JA |
7949 | @item --enable-dparen-arithmetic |
7950 | Include support for the @code{((@dots{}))} command | |
7951 | (@pxref{Conditional Constructs}). | |
ccc6cda3 | 7952 | |
bb70624e JA |
7953 | @item --enable-extended-glob |
7954 | Include support for the extended pattern matching features described | |
7955 | above under @ref{Pattern Matching}. | |
ccc6cda3 | 7956 | |
691aebcb CR |
7957 | @item --enable-extended-glob-default |
7958 | Set the default value of the @var{extglob} shell option described | |
7959 | above under @ref{The Shopt Builtin} to be enabled. | |
7960 | ||
bb70624e JA |
7961 | @item --enable-help-builtin |
7962 | Include the @code{help} builtin, which displays help on shell builtins and | |
7963 | variables (@pxref{Bash Builtins}). | |
ccc6cda3 | 7964 | |
bb70624e JA |
7965 | @item --enable-history |
7966 | Include command history and the @code{fc} and @code{history} | |
7967 | builtin commands (@pxref{Bash History Facilities}). | |
ccc6cda3 | 7968 | |
bb70624e JA |
7969 | @item --enable-job-control |
7970 | This enables the job control features (@pxref{Job Control}), | |
7971 | if the operating system supports them. | |
ccc6cda3 | 7972 | |
d3a24ed2 CR |
7973 | @item --enable-multibyte |
7974 | This enables support for multibyte characters if the operating | |
7975 | system provides the necessary support. | |
7976 | ||
bb70624e JA |
7977 | @item --enable-net-redirections |
7978 | This enables the special handling of filenames of the form | |
7979 | @code{/dev/tcp/@var{host}/@var{port}} and | |
7980 | @code{/dev/udp/@var{host}/@var{port}} | |
7981 | when used in redirections (@pxref{Redirections}). | |
ccc6cda3 | 7982 | |
bb70624e JA |
7983 | @item --enable-process-substitution |
7984 | This enables process substitution (@pxref{Process Substitution}) if | |
7985 | the operating system provides the necessary support. | |
ccc6cda3 | 7986 | |
01ed5ba4 CR |
7987 | @item --enable-progcomp |
7988 | Enable the programmable completion facilities | |
7989 | (@pxref{Programmable Completion}). | |
7990 | If Readline is not enabled, this option has no effect. | |
7991 | ||
bb70624e JA |
7992 | @item --enable-prompt-string-decoding |
7993 | Turn on the interpretation of a number of backslash-escaped characters | |
28ef6c31 | 7994 | in the @env{$PS1}, @env{$PS2}, @env{$PS3}, and @env{$PS4} prompt |
b28ff8c9 | 7995 | strings. See @ref{Controlling the Prompt}, for a complete list of prompt |
bb70624e | 7996 | string escape sequences. |
ccc6cda3 | 7997 | |
bb70624e JA |
7998 | @item --enable-readline |
7999 | Include support for command-line editing and history with the Bash | |
8000 | version of the Readline library (@pxref{Command Line Editing}). | |
ccc6cda3 | 8001 | |
bb70624e JA |
8002 | @item --enable-restricted |
8003 | Include support for a @dfn{restricted shell}. If this is enabled, Bash, | |
8004 | when called as @code{rbash}, enters a restricted mode. See | |
8005 | @ref{The Restricted Shell}, for a description of restricted mode. | |
ccc6cda3 | 8006 | |
bb70624e | 8007 | @item --enable-select |
54a1fa7c CR |
8008 | Include the @code{select} compound command, which allows the generation of |
8009 | simple menus (@pxref{Conditional Constructs}). | |
ccc6cda3 | 8010 | |
01ed5ba4 CR |
8011 | @item --enable-separate-helpfiles |
8012 | Use external files for the documentation displayed by the @code{help} builtin | |
8013 | instead of storing the text internally. | |
8014 | ||
8015 | @item --enable-single-help-strings | |
8016 | Store the text displayed by the @code{help} builtin as a single string for | |
8017 | each help topic. This aids in translating the text to different languages. | |
8018 | You may need to disable this if your compiler cannot handle very long string | |
8019 | literals. | |
8020 | ||
7790f917 | 8021 | @item --enable-strict-posix-default |
1c72c0cd | 8022 | Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}). |
7790f917 | 8023 | |
bb70624e JA |
8024 | @item --enable-usg-echo-default |
8025 | A synonym for @code{--enable-xpg-echo-default}. | |
ccc6cda3 | 8026 | |
bb70624e JA |
8027 | @item --enable-xpg-echo-default |
8028 | Make the @code{echo} builtin expand backslash-escaped characters by default, | |
28ef6c31 | 8029 | without requiring the @option{-e} option. |
bb70624e JA |
8030 | This sets the default value of the @code{xpg_echo} shell option to @code{on}, |
8031 | which makes the Bash @code{echo} behave more like the version specified in | |
01ed5ba4 | 8032 | the Single Unix Specification, version 3. |
bb70624e JA |
8033 | @xref{Bash Builtins}, for a description of the escape sequences that |
8034 | @code{echo} recognizes. | |
bb70624e | 8035 | @end table |
ccc6cda3 | 8036 | |
bb70624e JA |
8037 | The file @file{config-top.h} contains C Preprocessor |
8038 | @samp{#define} statements for options which are not settable from | |
8039 | @code{configure}. | |
8040 | Some of these are not meant to be changed; beware of the consequences if | |
8041 | you do. | |
8042 | Read the comments associated with each definition for more | |
8043 | information about its effect. | |
ccc6cda3 | 8044 | |
bb70624e JA |
8045 | @node Reporting Bugs |
8046 | @appendix Reporting Bugs | |
ccc6cda3 | 8047 | |
bb70624e JA |
8048 | Please report all bugs you find in Bash. |
8049 | But first, you should | |
8050 | make sure that it really is a bug, and that it appears in the latest | |
d3a24ed2 CR |
8051 | version of Bash. |
8052 | The latest version of Bash is always available for FTP from | |
2d0c84ef | 8053 | @uref{ftp://ftp.gnu.org/pub/gnu/bash/}. |
ccc6cda3 | 8054 | |
bb70624e JA |
8055 | Once you have determined that a bug actually exists, use the |
8056 | @code{bashbug} command to submit a bug report. | |
8057 | If you have a fix, you are encouraged to mail that as well! | |
8058 | Suggestions and `philosophical' bug reports may be mailed | |
8059 | to @email{bug-bash@@gnu.org} or posted to the Usenet | |
8060 | newsgroup @code{gnu.bash.bug}. | |
ccc6cda3 | 8061 | |
bb70624e JA |
8062 | All bug reports should include: |
8063 | @itemize @bullet | |
8064 | @item | |
8065 | The version number of Bash. | |
8066 | @item | |
8067 | The hardware and operating system. | |
8068 | @item | |
8069 | The compiler used to compile Bash. | |
8070 | @item | |
8071 | A description of the bug behaviour. | |
8072 | @item | |
8073 | A short script or `recipe' which exercises the bug and may be used | |
8074 | to reproduce it. | |
8075 | @end itemize | |
ccc6cda3 | 8076 | |
bb70624e JA |
8077 | @noindent |
8078 | @code{bashbug} inserts the first three items automatically into | |
8079 | the template it provides for filing a bug report. | |
ccc6cda3 | 8080 | |
bb70624e | 8081 | Please send all reports concerning this manual to |
df0e4bfe | 8082 | @email{bug-bash@@gnu.org}. |
ccc6cda3 | 8083 | |
bb70624e JA |
8084 | @node Major Differences From The Bourne Shell |
8085 | @appendix Major Differences From The Bourne Shell | |
d166f048 | 8086 | |
bb70624e JA |
8087 | Bash implements essentially the same grammar, parameter and |
8088 | variable expansion, redirection, and quoting as the Bourne Shell. | |
ac18b312 | 8089 | Bash uses the @sc{posix} standard as the specification of |
bb70624e JA |
8090 | how these features are to be implemented. There are some |
8091 | differences between the traditional Bourne shell and Bash; this | |
8092 | section quickly details the differences of significance. A | |
8093 | number of these differences are explained in greater depth in | |
8094 | previous sections. | |
7790f917 CR |
8095 | This section uses the version of @code{sh} included in SVR4.2 (the |
8096 | last version of the historical Bourne shell) as the baseline reference. | |
ccc6cda3 | 8097 | |
bb70624e | 8098 | @itemize @bullet |
ccc6cda3 | 8099 | |
ccc6cda3 | 8100 | @item |
bb70624e | 8101 | Bash is @sc{posix}-conformant, even where the @sc{posix} specification |
28ef6c31 | 8102 | differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}). |
ccc6cda3 JA |
8103 | |
8104 | @item | |
bb70624e | 8105 | Bash has multi-character invocation options (@pxref{Invoking Bash}). |
ccc6cda3 JA |
8106 | |
8107 | @item | |
bb70624e JA |
8108 | Bash has command-line editing (@pxref{Command Line Editing}) and |
8109 | the @code{bind} builtin. | |
ccc6cda3 JA |
8110 | |
8111 | @item | |
bb70624e | 8112 | Bash provides a programmable word completion mechanism |
6fbe7620 CR |
8113 | (@pxref{Programmable Completion}), and builtin commands |
8114 | @code{complete}, @code{compgen}, and @code{compopt}, to | |
8115 | manipulate it. | |
ccc6cda3 | 8116 | |
bb70624e JA |
8117 | @item |
8118 | Bash has command history (@pxref{Bash History Facilities}) and the | |
8119 | @code{history} and @code{fc} builtins to manipulate it. | |
5e13499c CR |
8120 | The Bash history list maintains timestamp information and uses the |
8121 | value of the @code{HISTTIMEFORMAT} variable to display it. | |
ccc6cda3 | 8122 | |
bb70624e JA |
8123 | @item |
8124 | Bash implements @code{csh}-like history expansion | |
8125 | (@pxref{History Interaction}). | |
ccc6cda3 | 8126 | |
bb70624e JA |
8127 | @item |
8128 | Bash has one-dimensional array variables (@pxref{Arrays}), and the | |
8129 | appropriate variable expansions and assignment syntax to use them. | |
8130 | Several of the Bash builtins take options to act on arrays. | |
8131 | Bash provides a number of built-in array variables. | |
ccc6cda3 | 8132 | |
bb70624e JA |
8133 | @item |
8134 | The @code{$'@dots{}'} quoting syntax, which expands ANSI-C | |
8135 | backslash-escaped characters in the text between the single quotes, | |
8136 | is supported (@pxref{ANSI-C Quoting}). | |
ccc6cda3 | 8137 | |
bb70624e JA |
8138 | @item |
8139 | Bash supports the @code{$"@dots{}"} quoting syntax to do | |
8140 | locale-specific translation of the characters between the double | |
28ef6c31 | 8141 | quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings} |
bb70624e JA |
8142 | invocation options list the translatable strings found in a script |
8143 | (@pxref{Locale Translation}). | |
ccc6cda3 | 8144 | |
bb70624e JA |
8145 | @item |
8146 | Bash implements the @code{!} keyword to negate the return value of | |
8147 | a pipeline (@pxref{Pipelines}). | |
8148 | Very useful when an @code{if} statement needs to act only if a test fails. | |
7790f917 CR |
8149 | The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to |
8150 | return a failure status if any command fails. | |
ccc6cda3 | 8151 | |
bb70624e JA |
8152 | @item |
8153 | Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}). | |
8154 | The display of the timing statistics may be controlled with the | |
28ef6c31 | 8155 | @env{TIMEFORMAT} variable. |
ccc6cda3 | 8156 | |
bb70624e JA |
8157 | @item |
8158 | Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))} | |
8159 | arithmetic for command, similar to the C language (@pxref{Looping Constructs}). | |
ccc6cda3 | 8160 | |
bb70624e JA |
8161 | @item |
8162 | Bash includes the @code{select} compound command, which allows the | |
8163 | generation of simple menus (@pxref{Conditional Constructs}). | |
ccc6cda3 | 8164 | |
bb70624e JA |
8165 | @item |
8166 | Bash includes the @code{[[} compound command, which makes conditional | |
7790f917 CR |
8167 | testing part of the shell grammar (@pxref{Conditional Constructs}), including |
8168 | optional regular expression matching. | |
8169 | ||
8170 | @item | |
8171 | Bash provides optional case-insensitive matching for the @code{case} and | |
8172 | @code{[[} constructs. | |
ccc6cda3 | 8173 | |
bb70624e JA |
8174 | @item |
8175 | Bash includes brace expansion (@pxref{Brace Expansion}) and tilde | |
8176 | expansion (@pxref{Tilde Expansion}). | |
ccc6cda3 | 8177 | |
bb70624e JA |
8178 | @item |
8179 | Bash implements command aliases and the @code{alias} and @code{unalias} | |
8180 | builtins (@pxref{Aliases}). | |
ccc6cda3 | 8181 | |
bb70624e JA |
8182 | @item |
8183 | Bash provides shell arithmetic, the @code{((} compound command | |
8184 | (@pxref{Conditional Constructs}), | |
8185 | and arithmetic expansion (@pxref{Shell Arithmetic}). | |
ccc6cda3 | 8186 | |
bb70624e JA |
8187 | @item |
8188 | Variables present in the shell's initial environment are automatically | |
8189 | exported to child processes. The Bourne shell does not normally do | |
8190 | this unless the variables are explicitly marked using the @code{export} | |
8191 | command. | |
ccc6cda3 | 8192 | |
7790f917 CR |
8193 | @item |
8194 | Bash supports the @samp{+=} assignment operator, which appends to the value | |
8195 | of the variable named on the left hand side. | |
8196 | ||
bb70624e JA |
8197 | @item |
8198 | Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%} | |
8199 | and @samp{##} expansions to remove leading or trailing substrings from | |
8200 | variable values (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8201 | |
bb70624e JA |
8202 | @item |
8203 | The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}}, | |
8204 | is supported (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8205 | |
bb70624e JA |
8206 | @item |
8207 | The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}}, | |
8208 | which expands to the substring of @code{var}'s value of length | |
8209 | @var{length}, beginning at @var{offset}, is present | |
8210 | (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8211 | |
bb70624e JA |
8212 | @item |
8213 | The expansion | |
8214 | @code{$@{var/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}}, | |
8215 | which matches @var{pattern} and replaces it with @var{replacement} in | |
8216 | the value of @code{var}, is available (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8217 | |
bb70624e | 8218 | @item |
4e136352 | 8219 | The expansion @code{$@{!@var{prefix}*@}} expansion, which expands to |
bb70624e JA |
8220 | the names of all shell variables whose names begin with @var{prefix}, |
8221 | is available (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8222 | |
bb70624e JA |
8223 | @item |
8224 | Bash has @var{indirect} variable expansion using @code{$@{!word@}} | |
8225 | (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 8226 | |
bb70624e JA |
8227 | @item |
8228 | Bash can expand positional parameters beyond @code{$9} using | |
8229 | @code{$@{@var{num}@}}. | |
ccc6cda3 | 8230 | |
bb70624e JA |
8231 | @item |
8232 | The @sc{posix} @code{$()} form of command substitution | |
8233 | is implemented (@pxref{Command Substitution}), | |
8234 | and preferred to the Bourne shell's @code{``} (which | |
8235 | is also implemented for backwards compatibility). | |
ccc6cda3 | 8236 | |
bb70624e JA |
8237 | @item |
8238 | Bash has process substitution (@pxref{Process Substitution}). | |
ccc6cda3 | 8239 | |
bb70624e JA |
8240 | @item |
8241 | Bash automatically assigns variables that provide information about the | |
28ef6c31 JA |
8242 | current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host |
8243 | (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}), | |
8244 | and the instance of Bash that is running (@env{BASH}, | |
8245 | @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables}, | |
bb70624e | 8246 | for details. |
ccc6cda3 | 8247 | |
bb70624e | 8248 | @item |
28ef6c31 | 8249 | The @env{IFS} variable is used to split only the results of expansion, |
bb70624e JA |
8250 | not all words (@pxref{Word Splitting}). |
8251 | This closes a longstanding shell security hole. | |
ccc6cda3 | 8252 | |
df0e4bfe CR |
8253 | @item |
8254 | The filename expansion bracket expression code uses @samp{!} and @samp{^} | |
8255 | to negate the set of characters between the brackets. | |
8256 | The Bourne shell uses only @samp{!}. | |
8257 | ||
bb70624e | 8258 | @item |
ac18b312 | 8259 | Bash implements the full set of @sc{posix} filename expansion operators, |
bb70624e JA |
8260 | including @var{character classes}, @var{equivalence classes}, and |
8261 | @var{collating symbols} (@pxref{Filename Expansion}). | |
ccc6cda3 | 8262 | |
bb70624e JA |
8263 | @item |
8264 | Bash implements extended pattern matching features when the @code{extglob} | |
8265 | shell option is enabled (@pxref{Pattern Matching}). | |
ccc6cda3 | 8266 | |
bb70624e JA |
8267 | @item |
8268 | It is possible to have a variable and a function with the same name; | |
8269 | @code{sh} does not separate the two name spaces. | |
ccc6cda3 | 8270 | |
bb70624e JA |
8271 | @item |
8272 | Bash functions are permitted to have local variables using the | |
8273 | @code{local} builtin, and thus useful recursive functions may be written | |
8274 | (@pxref{Bash Builtins}). | |
ccc6cda3 | 8275 | |
bb70624e JA |
8276 | @item |
8277 | Variable assignments preceding commands affect only that command, even | |
8278 | builtins and functions (@pxref{Environment}). | |
8279 | In @code{sh}, all variable assignments | |
8280 | preceding commands are global unless the command is executed from the | |
8281 | file system. | |
ccc6cda3 | 8282 | |
bb70624e JA |
8283 | @item |
8284 | Bash performs filename expansion on filenames specified as operands | |
8285 | to input and output redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 8286 | |
bb70624e JA |
8287 | @item |
8288 | Bash contains the @samp{<>} redirection operator, allowing a file to be | |
8289 | opened for both reading and writing, and the @samp{&>} redirection | |
8290 | operator, for directing standard output and standard error to the same | |
8291 | file (@pxref{Redirections}). | |
ccc6cda3 | 8292 | |
7790f917 CR |
8293 | @item |
8294 | Bash includes the @samp{<<<} redirection operator, allowing a string to | |
8295 | be used as the standard input to a command. | |
8296 | ||
8297 | @item | |
8298 | Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}} | |
8299 | redirection operators, which move one file descriptor to another. | |
8300 | ||
bb70624e JA |
8301 | @item |
8302 | Bash treats a number of filenames specially when they are | |
8303 | used in redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 8304 | |
bb70624e JA |
8305 | @item |
8306 | Bash can open network connections to arbitrary machines and services | |
8307 | with the redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 8308 | |
bb70624e JA |
8309 | @item |
8310 | The @code{noclobber} option is available to avoid overwriting existing | |
8311 | files with output redirection (@pxref{The Set Builtin}). | |
8312 | The @samp{>|} redirection operator may be used to override @code{noclobber}. | |
d166f048 | 8313 | |
bb70624e JA |
8314 | @item |
8315 | The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins}) | |
28ef6c31 | 8316 | each take @option{-L} and @option{-P} options to switch between logical and |
bb70624e | 8317 | physical modes. |
d166f048 | 8318 | |
bb70624e JA |
8319 | @item |
8320 | Bash allows a function to override a builtin with the same name, and provides | |
8321 | access to that builtin's functionality within the function via the | |
8322 | @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}). | |
d166f048 | 8323 | |
bb70624e JA |
8324 | @item |
8325 | The @code{command} builtin allows selective disabling of functions | |
8326 | when command lookup is performed (@pxref{Bash Builtins}). | |
ccc6cda3 | 8327 | |
bb70624e JA |
8328 | @item |
8329 | Individual builtins may be enabled or disabled using the @code{enable} | |
8330 | builtin (@pxref{Bash Builtins}). | |
b72432fd | 8331 | |
bb70624e JA |
8332 | @item |
8333 | The Bash @code{exec} builtin takes additional options that allow users | |
8334 | to control the contents of the environment passed to the executed | |
8335 | command, and what the zeroth argument to the command is to be | |
8336 | (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 8337 | |
bb70624e JA |
8338 | @item |
8339 | Shell functions may be exported to children via the environment | |
8340 | using @code{export -f} (@pxref{Shell Functions}). | |
ccc6cda3 | 8341 | |
bb70624e JA |
8342 | @item |
8343 | The Bash @code{export}, @code{readonly}, and @code{declare} builtins can | |
28ef6c31 | 8344 | take a @option{-f} option to act on shell functions, a @option{-p} option to |
bb70624e | 8345 | display variables with various attributes set in a format that can be |
28ef6c31 | 8346 | used as shell input, a @option{-n} option to remove various variable |
bb70624e JA |
8347 | attributes, and @samp{name=value} arguments to set variable attributes |
8348 | and values simultaneously. | |
cce855bc | 8349 | |
bb70624e JA |
8350 | @item |
8351 | The Bash @code{hash} builtin allows a name to be associated with | |
8352 | an arbitrary filename, even when that filename cannot be found by | |
28ef6c31 | 8353 | searching the @env{$PATH}, using @samp{hash -p} |
bb70624e | 8354 | (@pxref{Bourne Shell Builtins}). |
cce855bc | 8355 | |
bb70624e JA |
8356 | @item |
8357 | Bash includes a @code{help} builtin for quick reference to shell | |
8358 | facilities (@pxref{Bash Builtins}). | |
cce855bc | 8359 | |
bb70624e JA |
8360 | @item |
8361 | The @code{printf} builtin is available to display formatted output | |
8362 | (@pxref{Bash Builtins}). | |
ccc6cda3 | 8363 | |
bb70624e JA |
8364 | @item |
8365 | The Bash @code{read} builtin (@pxref{Bash Builtins}) | |
8366 | will read a line ending in @samp{\} with | |
28ef6c31 | 8367 | the @option{-r} option, and will use the @env{REPLY} variable as a |
bb70624e JA |
8368 | default if no non-option arguments are supplied. |
8369 | The Bash @code{read} builtin | |
28ef6c31 JA |
8370 | also accepts a prompt string with the @option{-p} option and will use |
8371 | Readline to obtain the line when given the @option{-e} option. | |
bb70624e | 8372 | The @code{read} builtin also has additional options to control input: |
28ef6c31 JA |
8373 | the @option{-s} option will turn off echoing of input characters as |
8374 | they are read, the @option{-t} option will allow @code{read} to time out | |
bb70624e | 8375 | if input does not arrive within a specified number of seconds, the |
28ef6c31 JA |
8376 | @option{-n} option will allow reading only a specified number of |
8377 | characters rather than a full line, and the @option{-d} option will read | |
bb70624e | 8378 | until a particular character rather than newline. |
ccc6cda3 | 8379 | |
bb70624e JA |
8380 | @item |
8381 | The @code{return} builtin may be used to abort execution of scripts | |
8382 | executed with the @code{.} or @code{source} builtins | |
8383 | (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 8384 | |
bb70624e JA |
8385 | @item |
8386 | Bash includes the @code{shopt} builtin, for finer control of shell | |
d3ad40de | 8387 | optional capabilities (@pxref{The Shopt Builtin}), and allows these options |
f73dda09 | 8388 | to be set and unset at shell invocation (@pxref{Invoking Bash}). |
ccc6cda3 | 8389 | |
bb70624e JA |
8390 | @item |
8391 | Bash has much more optional behavior controllable with the @code{set} | |
8392 | builtin (@pxref{The Set Builtin}). | |
ccc6cda3 | 8393 | |
5e13499c | 8394 | @item |
b28ff8c9 | 8395 | The @samp{-x} (@option{xtrace}) option displays commands other than |
5e13499c CR |
8396 | simple commands when performing an execution trace |
8397 | (@pxref{The Set Builtin}). | |
8398 | ||
bb70624e JA |
8399 | @item |
8400 | The @code{test} builtin (@pxref{Bourne Shell Builtins}) | |
8401 | is slightly different, as it implements the @sc{posix} algorithm, | |
8402 | which specifies the behavior based on the number of arguments. | |
d166f048 | 8403 | |
5e13499c CR |
8404 | @item |
8405 | Bash includes the @code{caller} builtin, which displays the context of | |
8406 | any active subroutine call (a shell function or a script executed with | |
8407 | the @code{.} or @code{source} builtins). This supports the bash | |
8408 | debugger. | |
8409 | ||
bb70624e | 8410 | @item |
f73dda09 JA |
8411 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a |
8412 | @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}. | |
d3a24ed2 CR |
8413 | Commands specified with a @code{DEBUG} trap are executed before every |
8414 | simple command, @code{for} command, @code{case} command, | |
8415 | @code{select} command, every arithmetic @code{for} command, and before | |
8416 | the first command executes in a shell function. | |
7117c2d2 | 8417 | The @code{DEBUG} trap is not inherited by shell functions unless the |
d3a24ed2 CR |
8418 | function has been given the @code{trace} attribute or the |
8419 | @code{functrace} option has been enabled using the @code{shopt} builtin. | |
8420 | The @code{extdebug} shell option has additional effects on the | |
8421 | @code{DEBUG} trap. | |
f73dda09 JA |
8422 | |
8423 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an | |
8424 | @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}. | |
8425 | Commands specified with an @code{ERR} trap are executed after a simple | |
8426 | command fails, with a few exceptions. | |
d3a24ed2 CR |
8427 | The @code{ERR} trap is not inherited by shell functions unless the |
8428 | @code{-o errtrace} option to the @code{set} builtin is enabled. | |
8429 | ||
8430 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a | |
8431 | @code{RETURN} pseudo-signal specification, similar to | |
8432 | @code{EXIT} and @code{DEBUG}. | |
8433 | Commands specified with an @code{RETURN} trap are executed before | |
8434 | execution resumes after a shell function or a shell script executed with | |
8435 | @code{.} or @code{source} returns. | |
76a8d78d CR |
8436 | The @code{RETURN} trap is not inherited by shell functions unless the |
8437 | function has been given the @code{trace} attribute or the | |
8438 | @code{functrace} option has been enabled using the @code{shopt} builtin. | |
d166f048 | 8439 | |
bb70624e JA |
8440 | @item |
8441 | The Bash @code{type} builtin is more extensive and gives more information | |
8442 | about the names it finds (@pxref{Bash Builtins}). | |
cce855bc | 8443 | |
bb70624e | 8444 | @item |
28ef6c31 | 8445 | The Bash @code{umask} builtin permits a @option{-p} option to cause |
bb70624e JA |
8446 | the output to be displayed in the form of a @code{umask} command |
8447 | that may be reused as input (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 8448 | |
bb70624e JA |
8449 | @item |
8450 | Bash implements a @code{csh}-like directory stack, and provides the | |
8451 | @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it | |
8452 | (@pxref{The Directory Stack}). | |
8453 | Bash also makes the directory stack visible as the value of the | |
28ef6c31 | 8454 | @env{DIRSTACK} shell variable. |
d166f048 | 8455 | |
bb70624e JA |
8456 | @item |
8457 | Bash interprets special backslash-escaped characters in the prompt | |
b28ff8c9 | 8458 | strings when interactive (@pxref{Controlling the Prompt}). |
cce855bc | 8459 | |
bb70624e JA |
8460 | @item |
8461 | The Bash restricted mode is more useful (@pxref{The Restricted Shell}); | |
8462 | the SVR4.2 shell restricted mode is too limited. | |
d166f048 | 8463 | |
bb70624e JA |
8464 | @item |
8465 | The @code{disown} builtin can remove a job from the internal shell | |
8466 | job table (@pxref{Job Control Builtins}) or suppress the sending | |
8467 | of @code{SIGHUP} to a job when the shell exits as the result of a | |
8468 | @code{SIGHUP}. | |
d166f048 | 8469 | |
7790f917 CR |
8470 | @item |
8471 | Bash includes a number of features to support a separate debugger for | |
8472 | shell scripts. | |
8473 | ||
bb70624e JA |
8474 | @item |
8475 | The SVR4.2 shell has two privilege-related builtins | |
8476 | (@code{mldmode} and @code{priv}) not present in Bash. | |
d166f048 | 8477 | |
bb70624e JA |
8478 | @item |
8479 | Bash does not have the @code{stop} or @code{newgrp} builtins. | |
ccc6cda3 | 8480 | |
bb70624e | 8481 | @item |
28ef6c31 | 8482 | Bash does not use the @env{SHACCT} variable or perform shell accounting. |
ccc6cda3 | 8483 | |
bb70624e | 8484 | @item |
28ef6c31 JA |
8485 | The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses |
8486 | @env{TMOUT}. | |
ccc6cda3 | 8487 | |
bb70624e | 8488 | @end itemize |
d166f048 | 8489 | |
bb70624e JA |
8490 | @noindent |
8491 | More features unique to Bash may be found in @ref{Bash Features}. | |
d166f048 | 8492 | |
ccc6cda3 | 8493 | |
bb70624e | 8494 | @appendixsec Implementation Differences From The SVR4.2 Shell |
ccc6cda3 | 8495 | |
bb70624e JA |
8496 | Since Bash is a completely new implementation, it does not suffer from |
8497 | many of the limitations of the SVR4.2 shell. For instance: | |
ccc6cda3 | 8498 | |
bb70624e | 8499 | @itemize @bullet |
ccc6cda3 | 8500 | |
bb70624e JA |
8501 | @item |
8502 | Bash does not fork a subshell when redirecting into or out of | |
8503 | a shell control structure such as an @code{if} or @code{while} | |
8504 | statement. | |
ccc6cda3 | 8505 | |
bb70624e JA |
8506 | @item |
8507 | Bash does not allow unbalanced quotes. The SVR4.2 shell will silently | |
8508 | insert a needed closing quote at @code{EOF} under certain circumstances. | |
8509 | This can be the cause of some hard-to-find errors. | |
ccc6cda3 | 8510 | |
bb70624e JA |
8511 | @item |
8512 | The SVR4.2 shell uses a baroque memory management scheme based on | |
8513 | trapping @code{SIGSEGV}. If the shell is started from a process with | |
8514 | @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library | |
8515 | function call), it misbehaves badly. | |
ccc6cda3 | 8516 | |
ccc6cda3 | 8517 | @item |
bb70624e | 8518 | In a questionable attempt at security, the SVR4.2 shell, |
28ef6c31 | 8519 | when invoked without the @option{-p} option, will alter its real |
bb70624e JA |
8520 | and effective @sc{uid} and @sc{gid} if they are less than some |
8521 | magic threshold value, commonly 100. | |
8522 | This can lead to unexpected results. | |
8523 | ||
ccc6cda3 | 8524 | @item |
bb70624e JA |
8525 | The SVR4.2 shell does not allow users to trap @code{SIGSEGV}, |
8526 | @code{SIGALRM}, or @code{SIGCHLD}. | |
8527 | ||
ccc6cda3 | 8528 | @item |
28ef6c31 JA |
8529 | The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK}, |
8530 | @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset. | |
bb70624e | 8531 | |
ccc6cda3 | 8532 | @item |
bb70624e JA |
8533 | The SVR4.2 shell treats @samp{^} as the undocumented equivalent of |
8534 | @samp{|}. | |
8535 | ||
ccc6cda3 | 8536 | @item |
bb70624e JA |
8537 | Bash allows multiple option arguments when it is invoked (@code{-x -v}); |
8538 | the SVR4.2 shell allows only one option argument (@code{-xv}). In | |
8539 | fact, some versions of the shell dump core if the second argument begins | |
8540 | with a @samp{-}. | |
ccc6cda3 | 8541 | |
bb70624e JA |
8542 | @item |
8543 | The SVR4.2 shell exits a script if any builtin fails; Bash exits | |
ac18b312 CR |
8544 | a script only if one of the @sc{posix} special builtins fails, and |
8545 | only for certain failures, as enumerated in the @sc{posix} standard. | |
ccc6cda3 | 8546 | |
bb70624e JA |
8547 | @item |
8548 | The SVR4.2 shell behaves differently when invoked as @code{jsh} | |
8549 | (it turns on job control). | |
8550 | @end itemize | |
ccc6cda3 | 8551 | |
de00a878 CR |
8552 | @node GNU Free Documentation License |
8553 | @appendix GNU Free Documentation License | |
d3a24ed2 CR |
8554 | |
8555 | @include fdl.texi | |
8556 | ||
d3ad40de CR |
8557 | @node Indexes |
8558 | @appendix Indexes | |
8559 | ||
8560 | @menu | |
8561 | * Builtin Index:: Index of Bash builtin commands. | |
8562 | * Reserved Word Index:: Index of Bash reserved words. | |
8563 | * Variable Index:: Quick reference helps you find the | |
8564 | variable you want. | |
8565 | * Function Index:: Index of bindable Readline functions. | |
8566 | * Concept Index:: General index for concepts described in | |
8567 | this manual. | |
8568 | @end menu | |
8569 | ||
ccc6cda3 | 8570 | @node Builtin Index |
d3ad40de | 8571 | @appendixsec Index of Shell Builtin Commands |
ccc6cda3 JA |
8572 | @printindex bt |
8573 | ||
8574 | @node Reserved Word Index | |
d3ad40de | 8575 | @appendixsec Index of Shell Reserved Words |
ccc6cda3 JA |
8576 | @printindex rw |
8577 | ||
8578 | @node Variable Index | |
d3ad40de | 8579 | @appendixsec Parameter and Variable Index |
ccc6cda3 JA |
8580 | @printindex vr |
8581 | ||
8582 | @node Function Index | |
d3ad40de | 8583 | @appendixsec Function Index |
ccc6cda3 JA |
8584 | @printindex fn |
8585 | ||
8586 | @node Concept Index | |
d3ad40de | 8587 | @appendixsec Concept Index |
ccc6cda3 JA |
8588 | @printindex cp |
8589 | ||
ccc6cda3 | 8590 | @bye |