]>
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 | |
5 | @c %**end of header | |
6 | ||
ccc6cda3 | 7 | @setchapternewpage odd |
ccc6cda3 | 8 | |
b80f6443 | 9 | @include version.texi |
e8ce775d | 10 | |
b80f6443 | 11 | @copying |
ccc6cda3 | 12 | This text is a brief description of the features that are present in |
b80f6443 | 13 | the Bash shell (version @value{VERSION}, @value{UPDATED}). |
ccc6cda3 JA |
14 | |
15 | This is Edition @value{EDITION}, last updated @value{UPDATED}, | |
16 | of @cite{The GNU Bash Reference Manual}, | |
17 | for @code{Bash}, Version @value{VERSION}. | |
18 | ||
95732b49 | 19 | Copyright @copyright{} 1988-2005 Free Software Foundation, Inc. |
ccc6cda3 JA |
20 | |
21 | Permission is granted to make and distribute verbatim copies of | |
22 | this manual provided the copyright notice and this permission notice | |
23 | are preserved on all copies. | |
24 | ||
b80f6443 JA |
25 | @quotation |
26 | Permission is granted to copy, distribute and/or modify this document | |
27 | under the terms of the GNU Free Documentation License, Version 1.1 or | |
28 | any later version published by the Free Software Foundation; with no | |
29 | Invariant Sections, with the Front-Cover texts being ``A GNU Manual,'' | |
30 | and with the Back-Cover Texts as in (a) below. A copy of the license is | |
31 | included in the section entitled ``GNU Free Documentation License.'' | |
32 | ||
33 | (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify | |
34 | this GNU Manual, like GNU software. Copies published by the Free | |
35 | Software Foundation raise funds for GNU development.'' | |
36 | @end quotation | |
37 | @end copying | |
38 | ||
39 | @defcodeindex bt | |
40 | @defcodeindex rw | |
41 | @set BashFeatures | |
42 | ||
43 | @dircategory Basics | |
44 | @direntry | |
45 | * Bash: (bash). The GNU Bourne-Again SHell. | |
46 | @end direntry | |
47 | ||
48 | @finalout | |
ccc6cda3 JA |
49 | |
50 | @titlepage | |
51 | @title Bash Reference Manual | |
52 | @subtitle Reference Documentation for Bash | |
b72432fd | 53 | @subtitle Edition @value{EDITION}, for @code{Bash} Version @value{VERSION}. |
b80f6443 | 54 | @subtitle @value{UPDATED-MONTH} |
ccc6cda3 JA |
55 | @author Chet Ramey, Case Western Reserve University |
56 | @author Brian Fox, Free Software Foundation | |
b80f6443 | 57 | |
ccc6cda3 JA |
58 | @page |
59 | @vskip 0pt plus 1filll | |
b80f6443 | 60 | @insertcopying |
ccc6cda3 | 61 | |
b80f6443 JA |
62 | @sp 1 |
63 | Published by the Free Software Foundation @* | |
64 | 59 Temple Place, Suite 330, @* | |
65 | Boston, MA 02111-1307 @* | |
66 | USA @* | |
ccc6cda3 | 67 | |
ccc6cda3 JA |
68 | @end titlepage |
69 | ||
b80f6443 JA |
70 | @contents |
71 | ||
72 | @ifnottex | |
ccc6cda3 JA |
73 | @node Top, Introduction, (dir), (dir) |
74 | @top Bash Features | |
ccc6cda3 | 75 | |
ccc6cda3 | 76 | This text is a brief description of the features that are present in |
b80f6443 | 77 | the Bash shell (version @value{VERSION}, @value{UPDATED}).. |
ccc6cda3 JA |
78 | |
79 | This is Edition @value{EDITION}, last updated @value{UPDATED}, | |
80 | of @cite{The GNU Bash Reference Manual}, | |
81 | for @code{Bash}, Version @value{VERSION}. | |
82 | ||
ccc6cda3 JA |
83 | Bash contains features that appear in other popular shells, and some |
84 | features that only appear in Bash. Some of the shells that Bash has | |
85 | borrowed concepts from are the Bourne Shell (@file{sh}), the Korn Shell | |
86 | (@file{ksh}), and the C-shell (@file{csh} and its successor, | |
87 | @file{tcsh}). The following menu breaks the features up into | |
88 | categories based upon which one of these other shells inspired the | |
89 | feature. | |
90 | ||
91 | This manual is meant as a brief introduction to features found in | |
92 | Bash. The Bash manual page should be used as the definitive | |
93 | reference on shell behavior. | |
94 | ||
95 | @menu | |
96 | * Introduction:: An introduction to the shell. | |
ccc6cda3 JA |
97 | * Definitions:: Some definitions used in the rest of this |
98 | manual. | |
ccc6cda3 | 99 | * Basic Shell Features:: The shell "building blocks". |
bb70624e | 100 | * Shell Builtin Commands:: Commands that are a part of the shell. |
bb70624e | 101 | * Shell Variables:: Variables used or set by Bash. |
ccc6cda3 | 102 | * Bash Features:: Features found only in Bash. |
95732b49 JA |
103 | * Job Control:: What job control is and how Bash allows you |
104 | to use it. | |
105 | * Using History Interactively:: Command History Expansion | |
ccc6cda3 JA |
106 | * Command Line Editing:: Chapter describing the command line |
107 | editing features. | |
ccc6cda3 | 108 | * Installing Bash:: How to build and install Bash on your system. |
ccc6cda3 | 109 | * Reporting Bugs:: How to report bugs in Bash. |
bb70624e JA |
110 | * Major Differences From The Bourne Shell:: A terse list of the differences |
111 | between Bash and historical | |
112 | versions of /bin/sh. | |
b80f6443 | 113 | * Copying This Manual:: Copying this manual. |
ccc6cda3 | 114 | * Builtin Index:: Index of Bash builtin commands. |
ccc6cda3 | 115 | * Reserved Word Index:: Index of Bash reserved words. |
ccc6cda3 JA |
116 | * Variable Index:: Quick reference helps you find the |
117 | variable you want. | |
ccc6cda3 | 118 | * Function Index:: Index of bindable Readline functions. |
ccc6cda3 JA |
119 | * Concept Index:: General index for concepts described in |
120 | this manual. | |
121 | @end menu | |
b80f6443 | 122 | @end ifnottex |
ccc6cda3 JA |
123 | |
124 | @node Introduction | |
125 | @chapter Introduction | |
126 | @menu | |
127 | * What is Bash?:: A short description of Bash. | |
ccc6cda3 JA |
128 | * What is a shell?:: A brief introduction to shells. |
129 | @end menu | |
130 | ||
131 | @node What is Bash? | |
132 | @section What is Bash? | |
133 | ||
134 | Bash is the shell, or command language interpreter, | |
bb70624e | 135 | for the @sc{gnu} operating system. |
ccc6cda3 | 136 | The name is an acronym for the @samp{Bourne-Again SHell}, |
bb70624e | 137 | a pun on Stephen Bourne, the author of the direct ancestor of |
b80f6443 | 138 | the current Unix shell @code{sh}, |
ccc6cda3 JA |
139 | which appeared in the Seventh Edition Bell Labs Research version |
140 | of Unix. | |
141 | ||
bb70624e | 142 | Bash is largely compatible with @code{sh} and incorporates useful |
ccc6cda3 | 143 | features from the Korn shell @code{ksh} and the C shell @code{csh}. |
bb70624e JA |
144 | It is intended to be a conformant implementation of the @sc{ieee} |
145 | @sc{posix} Shell and Tools specification (@sc{ieee} Working Group 1003.2). | |
cce855bc JA |
146 | It offers functional improvements over @code{sh} for both interactive and |
147 | programming use. | |
ccc6cda3 | 148 | |
bb70624e JA |
149 | While the @sc{gnu} operating system provides other shells, including |
150 | a version of @code{csh}, Bash is the default shell. | |
151 | Like other @sc{gnu} software, Bash is quite portable. It currently runs | |
ccc6cda3 | 152 | on nearly every version of Unix and a few other operating systems @minus{} |
bb70624e | 153 | independently-supported ports exist for @sc{ms-dos}, @sc{os/2}, |
b80f6443 | 154 | and Windows platforms. |
ccc6cda3 JA |
155 | |
156 | @node What is a shell? | |
157 | @section What is a shell? | |
158 | ||
159 | At its base, a shell is simply a macro processor that executes | |
b80f6443 JA |
160 | commands. The term macro processor means functionality where text |
161 | and symbols are expanded to create larger expressions. | |
162 | ||
163 | A Unix shell is both a command interpreter and a programming | |
164 | language. As a command interpreter, the shell provides the user | |
165 | interface to the rich set of @sc{gnu} utilities. The programming | |
166 | language features allow these utilitites to be combined. | |
167 | Files containing commands can be created, and become | |
ccc6cda3 | 168 | commands themselves. These new commands have the same status as |
bb70624e | 169 | system commands in directories such as @file{/bin}, allowing users |
b80f6443 JA |
170 | or groups to establish custom environments to automate their common |
171 | tasks. | |
172 | ||
173 | Shells may be used interactively or non-interactively. In | |
174 | interactive mode, they accept input typed from the keyboard. | |
175 | When executing non-interactively, shells execute commands read | |
176 | from a file. | |
ccc6cda3 | 177 | |
bb70624e | 178 | A shell allows execution of @sc{gnu} commands, both synchronously and |
cce855bc JA |
179 | asynchronously. |
180 | The shell waits for synchronous commands to complete before accepting | |
181 | more input; asynchronous commands continue to execute in parallel | |
182 | with the shell while it reads and executes additional commands. | |
183 | The @dfn{redirection} constructs permit | |
bb70624e JA |
184 | fine-grained control of the input and output of those commands. |
185 | Moreover, the shell allows control over the contents of commands' | |
186 | environments. | |
bb70624e JA |
187 | |
188 | Shells also provide a small set of built-in | |
ccc6cda3 | 189 | commands (@dfn{builtins}) implementing functionality impossible |
bb70624e JA |
190 | or inconvenient to obtain via separate utilities. |
191 | For example, @code{cd}, @code{break}, @code{continue}, and | |
192 | @code{exec}) cannot be implemented outside of the shell because | |
193 | they directly manipulate the shell itself. | |
194 | The @code{history}, @code{getopts}, @code{kill}, or @code{pwd} | |
195 | builtins, among others, could be implemented in separate utilities, | |
196 | but they are more convenient to use as builtin commands. | |
197 | All of the shell builtins are described in | |
ccc6cda3 JA |
198 | subsequent sections. |
199 | ||
200 | While executing commands is essential, most of the power (and | |
201 | complexity) of shells is due to their embedded programming | |
202 | languages. Like any high-level language, the shell provides | |
203 | variables, flow control constructs, quoting, and functions. | |
204 | ||
bb70624e | 205 | Shells offer features geared specifically for |
ccc6cda3 JA |
206 | interactive use rather than to augment the programming language. |
207 | These interactive features include job control, command line | |
b80f6443 | 208 | editing, command history and aliases. Each of these features is |
ccc6cda3 JA |
209 | described in this manual. |
210 | ||
211 | @node Definitions | |
212 | @chapter Definitions | |
213 | These definitions are used throughout the remainder of this manual. | |
214 | ||
215 | @table @code | |
216 | ||
217 | @item POSIX | |
218 | @cindex POSIX | |
219 | A family of open system standards based on Unix. Bash | |
bb70624e | 220 | is concerned with @sc{posix} 1003.2, the Shell and Tools Standard. |
ccc6cda3 JA |
221 | |
222 | @item blank | |
223 | A space or tab character. | |
224 | ||
225 | @item builtin | |
226 | @cindex builtin | |
227 | A command that is implemented internally by the shell itself, rather | |
228 | than by an executable program somewhere in the file system. | |
229 | ||
230 | @item control operator | |
231 | @cindex control operator | |
232 | A @code{word} that performs a control function. It is a @code{newline} | |
233 | or one of the following: | |
234 | @samp{||}, @samp{&&}, @samp{&}, @samp{;}, @samp{;;}, | |
235 | @samp{|}, @samp{(}, or @samp{)}. | |
236 | ||
237 | @item exit status | |
238 | @cindex exit status | |
f73dda09 JA |
239 | The value returned by a command to its caller. The value is restricted |
240 | to eight bits, so the maximum value is 255. | |
ccc6cda3 JA |
241 | |
242 | @item field | |
243 | @cindex field | |
244 | A unit of text that is the result of one of the shell expansions. After | |
245 | expansion, when executing a command, the resulting fields are used as | |
246 | the command name and arguments. | |
247 | ||
248 | @item filename | |
249 | @cindex filename | |
250 | A string of characters used to identify a file. | |
251 | ||
252 | @item job | |
253 | @cindex job | |
254 | A set of processes comprising a pipeline, and any processes descended | |
255 | from it, that are all in the same process group. | |
256 | ||
257 | @item job control | |
258 | @cindex job control | |
d166f048 JA |
259 | A mechanism by which users can selectively stop (suspend) and restart |
260 | (resume) execution of processes. | |
ccc6cda3 JA |
261 | |
262 | @item metacharacter | |
263 | @cindex metacharacter | |
264 | A character that, when unquoted, separates words. A metacharacter is | |
265 | a @code{blank} or one of the following characters: | |
266 | @samp{|}, @samp{&}, @samp{;}, @samp{(}, @samp{)}, @samp{<}, or | |
267 | @samp{>}. | |
268 | ||
269 | @item name | |
270 | @cindex name | |
271 | @cindex identifier | |
272 | A @code{word} consisting solely of letters, numbers, and underscores, | |
273 | and beginning with a letter or underscore. @code{Name}s are used as | |
274 | shell variable and function names. | |
275 | Also referred to as an @code{identifier}. | |
276 | ||
277 | @item operator | |
278 | @cindex operator, shell | |
279 | A @code{control operator} or a @code{redirection operator}. | |
280 | @xref{Redirections}, for a list of redirection operators. | |
281 | ||
282 | @item process group | |
283 | @cindex process group | |
284 | A collection of related processes each having the same process | |
bb70624e | 285 | group @sc{id}. |
ccc6cda3 JA |
286 | |
287 | @item process group ID | |
288 | @cindex process group ID | |
289 | A unique identifer that represents a @code{process group} | |
290 | during its lifetime. | |
291 | ||
292 | @item reserved word | |
293 | @cindex reserved word | |
294 | A @code{word} that has a special meaning to the shell. Most reserved | |
295 | words introduce shell flow control constructs, such as @code{for} and | |
296 | @code{while}. | |
297 | ||
298 | @item return status | |
299 | @cindex return status | |
300 | A synonym for @code{exit status}. | |
301 | ||
302 | @item signal | |
303 | @cindex signal | |
bb70624e | 304 | A mechanism by which a process may be notified by the kernel |
ccc6cda3 JA |
305 | of an event occurring in the system. |
306 | ||
307 | @item special builtin | |
308 | @cindex special builtin | |
309 | A shell builtin command that has been classified as special by the | |
bb70624e | 310 | @sc{posix} 1003.2 standard. |
ccc6cda3 JA |
311 | |
312 | @item token | |
313 | @cindex token | |
314 | A sequence of characters considered a single unit by the shell. It is | |
315 | either a @code{word} or an @code{operator}. | |
316 | ||
317 | @item word | |
318 | @cindex word | |
319 | A @code{token} that is not an @code{operator}. | |
320 | @end table | |
321 | ||
322 | @node Basic Shell Features | |
323 | @chapter Basic Shell Features | |
324 | @cindex Bourne shell | |
325 | ||
326 | Bash is an acronym for @samp{Bourne-Again SHell}. | |
327 | The Bourne shell is | |
328 | the traditional Unix shell originally written by Stephen Bourne. | |
329 | All of the Bourne shell builtin commands are available in Bash, | |
b80f6443 JA |
330 | The rules for evaluation and quoting are taken from the @sc{posix} |
331 | specification for the `standard' Unix shell. | |
ccc6cda3 | 332 | |
cce855bc | 333 | This chapter briefly summarizes the shell's `building blocks': |
ccc6cda3 JA |
334 | commands, control structures, shell functions, shell @i{parameters}, |
335 | shell expansions, | |
336 | @i{redirections}, which are a way to direct input and output from | |
337 | and to named files, and how the shell executes commands. | |
338 | ||
339 | @menu | |
340 | * Shell Syntax:: What your input means to the shell. | |
cce855bc | 341 | * Shell Commands:: The types of commands you can use. |
ccc6cda3 | 342 | * Shell Functions:: Grouping commands by name. |
b80f6443 JA |
343 | * Shell Parameters:: How the shell stores values. |
344 | * Shell Expansions:: How Bash expands parameters and the various | |
ccc6cda3 JA |
345 | expansions available. |
346 | * Redirections:: A way to control where input and output go. | |
347 | * Executing Commands:: What happens when you run a command. | |
348 | * Shell Scripts:: Executing files of shell commands. | |
349 | @end menu | |
350 | ||
351 | @node Shell Syntax | |
352 | @section Shell Syntax | |
353 | @menu | |
354 | * Shell Operation:: The basic operation of the shell. | |
ccc6cda3 | 355 | * Quoting:: How to remove the special meaning from characters. |
ccc6cda3 JA |
356 | * Comments:: How to specify comments. |
357 | @end menu | |
358 | ||
bb70624e JA |
359 | When the shell reads input, it proceeds through a |
360 | sequence of operations. If the input indicates the beginning of a | |
361 | comment, the shell ignores the comment symbol (@samp{#}), and the rest | |
362 | of that line. | |
363 | ||
364 | Otherwise, roughly speaking, the shell reads its input and | |
365 | divides the input into words and operators, employing the quoting rules | |
366 | to select which meanings to assign various words and characters. | |
367 | ||
368 | The shell then parses these tokens into commands and other constructs, | |
369 | removes the special meaning of certain words or characters, expands | |
370 | others, redirects input and output as needed, executes the specified | |
371 | command, waits for the command's exit status, and makes that exit status | |
372 | available for further inspection or processing. | |
373 | ||
ccc6cda3 JA |
374 | @node Shell Operation |
375 | @subsection Shell Operation | |
376 | ||
377 | The following is a brief description of the shell's operation when it | |
378 | reads and executes a command. Basically, the shell does the | |
379 | following: | |
380 | ||
381 | @enumerate | |
382 | @item | |
383 | Reads its input from a file (@pxref{Shell Scripts}), from a string | |
28ef6c31 | 384 | supplied as an argument to the @option{-c} invocation option |
ccc6cda3 JA |
385 | (@pxref{Invoking Bash}), or from the user's terminal. |
386 | ||
387 | @item | |
388 | Breaks the input into words and operators, obeying the quoting rules | |
cce855bc | 389 | described in @ref{Quoting}. These tokens are separated by |
ccc6cda3 JA |
390 | @code{metacharacters}. Alias expansion is performed by this step |
391 | (@pxref{Aliases}). | |
392 | ||
393 | @item | |
cce855bc JA |
394 | Parses the tokens into simple and compound commands |
395 | (@pxref{Shell Commands}). | |
ccc6cda3 JA |
396 | |
397 | @item | |
398 | Performs the various shell expansions (@pxref{Shell Expansions}), breaking | |
399 | the expanded tokens into lists of filenames (@pxref{Filename Expansion}) | |
400 | and commands and arguments. | |
401 | ||
402 | @item | |
403 | Performs any necessary redirections (@pxref{Redirections}) and removes | |
404 | the redirection operators and their operands from the argument list. | |
405 | ||
406 | @item | |
407 | Executes the command (@pxref{Executing Commands}). | |
408 | ||
409 | @item | |
410 | Optionally waits for the command to complete and collects its exit | |
cce855bc | 411 | status (@pxref{Exit Status}). |
ccc6cda3 JA |
412 | |
413 | @end enumerate | |
414 | ||
415 | @node Quoting | |
416 | @subsection Quoting | |
417 | @cindex quoting | |
418 | @menu | |
419 | * Escape Character:: How to remove the special meaning from a single | |
420 | character. | |
421 | * Single Quotes:: How to inhibit all interpretation of a sequence | |
422 | of characters. | |
423 | * Double Quotes:: How to suppress most of the interpretation of a | |
424 | sequence of characters. | |
425 | * ANSI-C Quoting:: How to expand ANSI-C sequences in quoted strings. | |
ccc6cda3 JA |
426 | * Locale Translation:: How to translate strings into different languages. |
427 | @end menu | |
428 | ||
429 | Quoting is used to remove the special meaning of certain | |
430 | characters or words to the shell. Quoting can be used to | |
431 | disable special treatment for special characters, to prevent | |
432 | reserved words from being recognized as such, and to prevent | |
433 | parameter expansion. | |
434 | ||
cce855bc JA |
435 | Each of the shell metacharacters (@pxref{Definitions}) |
436 | has special meaning to the shell and must be quoted if it is to | |
bb70624e | 437 | represent itself. |
95732b49 JA |
438 | When the command history expansion facilities are being used |
439 | (@pxref{History Interaction}), the | |
bb70624e | 440 | @var{history expansion} character, usually @samp{!}, must be quoted |
28ef6c31 | 441 | to prevent history expansion. @xref{Bash History Facilities}, for |
bb70624e | 442 | more details concerning history expansion. |
b80f6443 | 443 | |
bb70624e | 444 | There are three quoting mechanisms: the |
ccc6cda3 JA |
445 | @var{escape character}, single quotes, and double quotes. |
446 | ||
447 | @node Escape Character | |
448 | @subsubsection Escape Character | |
449 | A non-quoted backslash @samp{\} is the Bash escape character. | |
450 | It preserves the literal value of the next character that follows, | |
451 | with the exception of @code{newline}. If a @code{\newline} pair | |
cce855bc JA |
452 | appears, and the backslash itself is not quoted, the @code{\newline} |
453 | is treated as a line continuation (that is, it is removed from | |
454 | the input stream and effectively ignored). | |
ccc6cda3 JA |
455 | |
456 | @node Single Quotes | |
457 | @subsubsection Single Quotes | |
458 | ||
bb70624e | 459 | Enclosing characters in single quotes (@samp{'}) preserves the literal value |
ccc6cda3 JA |
460 | of each character within the quotes. A single quote may not occur |
461 | between single quotes, even when preceded by a backslash. | |
462 | ||
463 | @node Double Quotes | |
464 | @subsubsection Double Quotes | |
465 | ||
bb70624e | 466 | Enclosing characters in double quotes (@samp{"}) preserves the literal value |
ccc6cda3 | 467 | of all characters within the quotes, with the exception of |
95732b49 JA |
468 | @samp{$}, @samp{`}, @samp{\}, |
469 | and, when history expansion is enabled, @samp{!}. | |
ccc6cda3 | 470 | The characters @samp{$} and @samp{`} |
cce855bc JA |
471 | retain their special meaning within double quotes (@pxref{Shell Expansions}). |
472 | The backslash retains its special meaning only when followed by one of | |
473 | the following characters: | |
ccc6cda3 | 474 | @samp{$}, @samp{`}, @samp{"}, @samp{\}, or @code{newline}. |
cce855bc JA |
475 | Within double quotes, backslashes that are followed by one of these |
476 | characters are removed. Backslashes preceding characters without a | |
477 | special meaning are left unmodified. | |
ccc6cda3 JA |
478 | A double quote may be quoted within double quotes by preceding it with |
479 | a backslash. | |
95732b49 JA |
480 | If enabled, history expansion will be performed unless an @samp{!} |
481 | appearing in double quotes is escaped using a backslash. | |
482 | The backslash preceding the @samp{!} is not removed. | |
ccc6cda3 JA |
483 | |
484 | The special parameters @samp{*} and @samp{@@} have special meaning | |
485 | when in double quotes (@pxref{Shell Parameter Expansion}). | |
486 | ||
487 | @node ANSI-C Quoting | |
488 | @subsubsection ANSI-C Quoting | |
489 | @cindex quoting, ANSI | |
490 | ||
491 | Words of the form @code{$'@var{string}'} are treated specially. The | |
492 | word expands to @var{string}, with backslash-escaped characters replaced | |
28ef6c31 | 493 | as specified by the ANSI C standard. Backslash escape sequences, if |
ccc6cda3 JA |
494 | present, are decoded as follows: |
495 | ||
496 | @table @code | |
497 | @item \a | |
498 | alert (bell) | |
499 | @item \b | |
500 | backspace | |
501 | @item \e | |
502 | an escape character (not ANSI C) | |
503 | @item \f | |
504 | form feed | |
505 | @item \n | |
506 | newline | |
507 | @item \r | |
508 | carriage return | |
509 | @item \t | |
510 | horizontal tab | |
511 | @item \v | |
512 | vertical tab | |
513 | @item \\ | |
514 | backslash | |
bb70624e JA |
515 | @item \' |
516 | single quote | |
ccc6cda3 | 517 | @item \@var{nnn} |
f73dda09 | 518 | the eight-bit character whose value is the octal value @var{nnn} |
cce855bc | 519 | (one to three digits) |
f73dda09 JA |
520 | @item \x@var{HH} |
521 | the eight-bit character whose value is the hexadecimal value @var{HH} | |
522 | (one or two hex digits) | |
7117c2d2 JA |
523 | @item \c@var{x} |
524 | a control-@var{x} character | |
ccc6cda3 JA |
525 | @end table |
526 | ||
527 | @noindent | |
bb70624e JA |
528 | The expanded result is single-quoted, as if the dollar sign had not |
529 | been present. | |
ccc6cda3 JA |
530 | |
531 | @node Locale Translation | |
532 | @subsubsection Locale-Specific Translation | |
533 | @cindex localization | |
f73dda09 JA |
534 | @cindex internationalization |
535 | @cindex native languages | |
536 | @cindex translation, native languages | |
ccc6cda3 JA |
537 | |
538 | A double-quoted string preceded by a dollar sign (@samp{$}) will cause | |
539 | the string to be translated according to the current locale. | |
540 | If the current locale is @code{C} or @code{POSIX}, the dollar sign | |
541 | is ignored. | |
542 | If the string is translated and replaced, the replacement is | |
543 | double-quoted. | |
544 | ||
f73dda09 JA |
545 | @vindex LC_MESSAGES |
546 | @vindex TEXTDOMAIN | |
547 | @vindex TEXTDOMAINDIR | |
28ef6c31 JA |
548 | Some systems use the message catalog selected by the @env{LC_MESSAGES} |
549 | shell variable. Others create the name of the message catalog from the | |
550 | value of the @env{TEXTDOMAIN} shell variable, possibly adding a | |
551 | suffix of @samp{.mo}. If you use the @env{TEXTDOMAIN} variable, you | |
552 | may need to set the @env{TEXTDOMAINDIR} variable to the location of | |
f73dda09 JA |
553 | the message catalog files. Still others use both variables in this |
554 | fashion: | |
555 | @env{TEXTDOMAINDIR}/@env{LC_MESSAGES}/LC_MESSAGES/@env{TEXTDOMAIN}.mo. | |
28ef6c31 | 556 | |
ccc6cda3 JA |
557 | @node Comments |
558 | @subsection Comments | |
559 | @cindex comments, shell | |
560 | ||
561 | In a non-interactive shell, or an interactive shell in which the | |
562 | @code{interactive_comments} option to the @code{shopt} | |
563 | builtin is enabled (@pxref{Bash Builtins}), | |
564 | a word beginning with @samp{#} | |
565 | causes that word and all remaining characters on that line to | |
566 | be ignored. An interactive shell without the @code{interactive_comments} | |
567 | option enabled does not allow comments. The @code{interactive_comments} | |
568 | option is on by default in interactive shells. | |
bb70624e | 569 | @xref{Interactive Shells}, for a description of what makes |
cce855bc JA |
570 | a shell interactive. |
571 | ||
572 | @node Shell Commands | |
573 | @section Shell Commands | |
574 | @cindex commands, shell | |
bb70624e JA |
575 | |
576 | A simple shell command such as @code{echo a b c} consists of the command | |
577 | itself followed by arguments, separated by spaces. | |
578 | ||
579 | More complex shell commands are composed of simple commands arranged together | |
580 | in a variety of ways: in a pipeline in which the output of one command | |
581 | becomes the input of a second, in a loop or conditional construct, or in | |
582 | some other grouping. | |
583 | ||
cce855bc JA |
584 | @menu |
585 | * Simple Commands:: The most common type of command. | |
586 | * Pipelines:: Connecting the input and output of several | |
587 | commands. | |
588 | * Lists:: How to execute commands sequentially. | |
b80f6443 | 589 | * Compound Commands:: Shell commands for control flow. |
cce855bc | 590 | @end menu |
ccc6cda3 JA |
591 | |
592 | @node Simple Commands | |
cce855bc | 593 | @subsection Simple Commands |
ccc6cda3 JA |
594 | @cindex commands, simple |
595 | ||
cce855bc | 596 | A simple command is the kind of command encountered most often. |
ccc6cda3 | 597 | It's just a sequence of words separated by @code{blank}s, terminated |
cce855bc | 598 | by one of the shell's control operators (@pxref{Definitions}). The |
bb70624e JA |
599 | first word generally specifies a command to be executed, with the |
600 | rest of the words being that command's arguments. | |
ccc6cda3 JA |
601 | |
602 | The return status (@pxref{Exit Status}) of a simple command is | |
603 | its exit status as provided | |
bb70624e JA |
604 | by the @sc{posix} 1003.1 @code{waitpid} function, or 128+@var{n} if |
605 | the command was terminated by signal @var{n}. | |
ccc6cda3 JA |
606 | |
607 | @node Pipelines | |
cce855bc | 608 | @subsection Pipelines |
ccc6cda3 JA |
609 | @cindex pipeline |
610 | @cindex commands, pipelines | |
611 | ||
612 | A @code{pipeline} is a sequence of simple commands separated by | |
613 | @samp{|}. | |
614 | ||
615 | @rwindex time | |
616 | @rwindex ! | |
617 | @cindex command timing | |
618 | The format for a pipeline is | |
619 | @example | |
620 | [@code{time} [@code{-p}]] [@code{!}] @var{command1} [@code{|} @var{command2} @dots{}] | |
621 | @end example | |
622 | ||
623 | @noindent | |
f73dda09 JA |
624 | The output of each command in the pipeline is connected via a pipe |
625 | to the input of the next command. | |
626 | That is, each command reads the previous command's output. | |
ccc6cda3 JA |
627 | |
628 | The reserved word @code{time} causes timing statistics | |
629 | to be printed for the pipeline once it finishes. | |
cce855bc JA |
630 | The statistics currently consist of elapsed (wall-clock) time and |
631 | user and system time consumed by the command's execution. | |
28ef6c31 | 632 | The @option{-p} option changes the output format to that specified |
bb70624e | 633 | by @sc{posix}. |
28ef6c31 | 634 | The @env{TIMEFORMAT} variable may be set to a format string that |
ccc6cda3 JA |
635 | specifies how the timing information should be displayed. |
636 | @xref{Bash Variables}, for a description of the available formats. | |
cce855bc JA |
637 | The use of @code{time} as a reserved word permits the timing of |
638 | shell builtins, shell functions, and pipelines. An external | |
639 | @code{time} command cannot time these easily. | |
640 | ||
641 | If the pipeline is not executed asynchronously (@pxref{Lists}), the | |
642 | shell waits for all commands in the pipeline to complete. | |
ccc6cda3 | 643 | |
cce855bc JA |
644 | Each command in a pipeline is executed in its own subshell |
645 | (@pxref{Command Execution Environment}). The exit | |
ccc6cda3 | 646 | status of a pipeline is the exit status of the last command in the |
b80f6443 JA |
647 | pipeline, unless the @code{pipefail} option is enabled |
648 | (@pxref{The Set Builtin}). | |
649 | If @code{pipefail} is enabled, the pipeline's return status is the | |
650 | value of the last (rightmost) command to exit with a non-zero status, | |
651 | or zero if all commands exit successfully. | |
652 | If the reserved word @samp{!} precedes the pipeline, the | |
653 | exit status is the logical negation of the exit status as described | |
654 | above. | |
655 | The shell waits for all commands in the pipeline to terminate before | |
656 | returning a value. | |
ccc6cda3 JA |
657 | |
658 | @node Lists | |
cce855bc | 659 | @subsection Lists of Commands |
ccc6cda3 JA |
660 | @cindex commands, lists |
661 | ||
662 | A @code{list} is a sequence of one or more pipelines separated by one | |
663 | of the operators @samp{;}, @samp{&}, @samp{&&}, or @samp{||}, | |
664 | and optionally terminated by one of @samp{;}, @samp{&}, or a | |
665 | @code{newline}. | |
666 | ||
667 | Of these list operators, @samp{&&} and @samp{||} | |
668 | have equal precedence, followed by @samp{;} and @samp{&}, | |
669 | which have equal precedence. | |
670 | ||
7117c2d2 JA |
671 | A sequence of one or more newlines may appear in a @code{list} |
672 | to delimit commands, equivalent to a semicolon. | |
673 | ||
ccc6cda3 | 674 | If a command is terminated by the control operator @samp{&}, |
cce855bc JA |
675 | the shell executes the command asynchronously in a subshell. |
676 | This is known as executing the command in the @var{background}. | |
677 | The shell does not wait for the command to finish, and the return | |
678 | status is 0 (true). | |
bb70624e JA |
679 | When job control is not active (@pxref{Job Control}), |
680 | the standard input for asynchronous commands, in the absence of any | |
cce855bc JA |
681 | explicit redirections, is redirected from @code{/dev/null}. |
682 | ||
683 | Commands separated by a @samp{;} are executed sequentially; the shell | |
684 | waits for each command to terminate in turn. The return status is the | |
ccc6cda3 JA |
685 | exit status of the last command executed. |
686 | ||
687 | The control operators @samp{&&} and @samp{||} | |
bb70624e JA |
688 | denote @sc{and} lists and @sc{or} lists, respectively. |
689 | An @sc{and} list has the form | |
ccc6cda3 | 690 | @example |
bb70624e | 691 | @var{command1} && @var{command2} |
ccc6cda3 JA |
692 | @end example |
693 | ||
694 | @noindent | |
bb70624e | 695 | @var{command2} is executed if, and only if, @var{command1} |
ccc6cda3 JA |
696 | returns an exit status of zero. |
697 | ||
bb70624e | 698 | An @sc{or} list has the form |
ccc6cda3 | 699 | @example |
bb70624e | 700 | @var{command1} || @var{command2} |
ccc6cda3 JA |
701 | @end example |
702 | ||
703 | @noindent | |
bb70624e | 704 | @var{command2} is executed if, and only if, @var{command1} |
ccc6cda3 JA |
705 | returns a non-zero exit status. |
706 | ||
707 | The return status of | |
bb70624e | 708 | @sc{and} and @sc{or} lists is the exit status of the last command |
ccc6cda3 JA |
709 | executed in the list. |
710 | ||
b80f6443 JA |
711 | @node Compound Commands |
712 | @subsection Compound Commands | |
713 | @cindex commands, compound | |
714 | ||
715 | @menu | |
716 | * Looping Constructs:: Shell commands for iterative action. | |
717 | * Conditional Constructs:: Shell commands for conditional execution. | |
718 | * Command Grouping:: Ways to group commands. | |
719 | @end menu | |
720 | ||
721 | Compound commands are the shell programming constructs. | |
722 | Each construct begins with a reserved word or control operator and is | |
723 | terminated by a corresponding reserved word or operator. | |
724 | Any redirections (@pxref{Redirections}) associated with a compound command | |
725 | apply to all commands within that compound command unless explicitly overridden. | |
726 | ||
727 | Bash provides looping constructs, conditional commands, and mechanisms | |
728 | to group commands and execute them as a unit. | |
729 | ||
ccc6cda3 | 730 | @node Looping Constructs |
b80f6443 | 731 | @subsubsection Looping Constructs |
ccc6cda3 JA |
732 | @cindex commands, looping |
733 | ||
ccc6cda3 JA |
734 | Bash supports the following looping constructs. |
735 | ||
bb70624e | 736 | Note that wherever a @samp{;} appears in the description of a |
cce855bc JA |
737 | command's syntax, it may be replaced with one or more newlines. |
738 | ||
ccc6cda3 JA |
739 | @table @code |
740 | @item until | |
741 | @rwindex until | |
742 | @rwindex do | |
743 | @rwindex done | |
744 | The syntax of the @code{until} command is: | |
745 | @example | |
746 | until @var{test-commands}; do @var{consequent-commands}; done | |
747 | @end example | |
cce855bc | 748 | Execute @var{consequent-commands} as long as |
ccc6cda3 | 749 | @var{test-commands} has an exit status which is not zero. |
cce855bc JA |
750 | The return status is the exit status of the last command executed |
751 | in @var{consequent-commands}, or zero if none was executed. | |
ccc6cda3 JA |
752 | |
753 | @item while | |
754 | @rwindex while | |
755 | The syntax of the @code{while} command is: | |
756 | @example | |
757 | while @var{test-commands}; do @var{consequent-commands}; done | |
758 | @end example | |
759 | ||
cce855bc | 760 | Execute @var{consequent-commands} as long as |
ccc6cda3 | 761 | @var{test-commands} has an exit status of zero. |
cce855bc JA |
762 | The return status is the exit status of the last command executed |
763 | in @var{consequent-commands}, or zero if none was executed. | |
ccc6cda3 JA |
764 | |
765 | @item for | |
766 | @rwindex for | |
767 | The syntax of the @code{for} command is: | |
768 | ||
769 | @example | |
770 | for @var{name} [in @var{words} @dots{}]; do @var{commands}; done | |
771 | @end example | |
cce855bc JA |
772 | Expand @var{words}, and execute @var{commands} once for each member |
773 | in the resultant list, with @var{name} bound to the current member. | |
bb70624e JA |
774 | If @samp{in @var{words}} is not present, the @code{for} command |
775 | executes the @var{commands} once for each positional parameter that is | |
776 | set, as if @samp{in "$@@"} had been specified | |
777 | (@pxref{Special Parameters}). | |
cce855bc JA |
778 | The return status is the exit status of the last command that executes. |
779 | If there are no items in the expansion of @var{words}, no commands are | |
780 | executed, and the return status is zero. | |
bb70624e JA |
781 | |
782 | An alternate form of the @code{for} command is also supported: | |
783 | ||
784 | @example | |
785 | for (( @var{expr1} ; @var{expr2} ; @var{expr3} )) ; do @var{commands} ; done | |
786 | @end example | |
787 | First, the arithmetic expression @var{expr1} is evaluated according | |
788 | to the rules described below (@pxref{Shell Arithmetic}). | |
789 | The arithmetic expression @var{expr2} is then evaluated repeatedly | |
790 | until it evaluates to zero. | |
791 | Each time @var{expr2} evaluates to a non-zero value, @var{commands} are | |
792 | executed and the arithmetic expression @var{expr3} is evaluated. | |
793 | If any expression is omitted, it behaves as if it evaluates to 1. | |
794 | The return value is the exit status of the last command in @var{list} | |
795 | that is executed, or false if any of the expressions is invalid. | |
796 | ||
ccc6cda3 JA |
797 | @end table |
798 | ||
799 | The @code{break} and @code{continue} builtins (@pxref{Bourne Shell Builtins}) | |
800 | may be used to control loop execution. | |
801 | ||
802 | @node Conditional Constructs | |
b80f6443 | 803 | @subsubsection Conditional Constructs |
ccc6cda3 JA |
804 | @cindex commands, conditional |
805 | ||
806 | @table @code | |
807 | @item if | |
808 | @rwindex if | |
809 | @rwindex then | |
810 | @rwindex else | |
811 | @rwindex elif | |
812 | @rwindex fi | |
813 | The syntax of the @code{if} command is: | |
814 | ||
815 | @example | |
816 | if @var{test-commands}; then | |
817 | @var{consequent-commands}; | |
818 | [elif @var{more-test-commands}; then | |
819 | @var{more-consequents};] | |
820 | [else @var{alternate-consequents};] | |
821 | fi | |
822 | @end example | |
823 | ||
cce855bc JA |
824 | The @var{test-commands} list is executed, and if its return status is zero, |
825 | the @var{consequent-commands} list is executed. | |
826 | If @var{test-commands} returns a non-zero status, each @code{elif} list | |
827 | is executed in turn, and if its exit status is zero, | |
ccc6cda3 JA |
828 | the corresponding @var{more-consequents} is executed and the |
829 | command completes. | |
830 | If @samp{else @var{alternate-consequents}} is present, and | |
831 | the final command in the final @code{if} or @code{elif} clause | |
cce855bc JA |
832 | has a non-zero exit status, then @var{alternate-consequents} is executed. |
833 | The return status is the exit status of the last command executed, or | |
834 | zero if no condition tested true. | |
ccc6cda3 JA |
835 | |
836 | @item case | |
837 | @rwindex case | |
838 | @rwindex in | |
839 | @rwindex esac | |
840 | The syntax of the @code{case} command is: | |
841 | ||
842 | @example | |
cce855bc | 843 | @code{case @var{word} in [ [(] @var{pattern} [| @var{pattern}]@dots{}) @var{command-list} ;;]@dots{} esac} |
ccc6cda3 JA |
844 | @end example |
845 | ||
cce855bc JA |
846 | @code{case} will selectively execute the @var{command-list} corresponding to |
847 | the first @var{pattern} that matches @var{word}. | |
95732b49 JA |
848 | If the shell option @code{nocasematch} |
849 | (see the description of @code{shopt} in @ref{Bash Builtins}) | |
850 | is enabled, the match is performed without regard to the case | |
851 | of alphabetic characters. | |
cce855bc JA |
852 | The @samp{|} is used to separate multiple patterns, and the @samp{)} |
853 | operator terminates a pattern list. | |
854 | A list of patterns and an associated command-list is known | |
855 | as a @var{clause}. Each clause must be terminated with @samp{;;}. | |
856 | The @var{word} undergoes tilde expansion, parameter expansion, command | |
857 | substitution, arithmetic expansion, and quote removal before matching is | |
858 | attempted. Each @var{pattern} undergoes tilde expansion, parameter | |
859 | expansion, command substitution, and arithmetic expansion. | |
860 | ||
861 | There may be an arbitrary number of @code{case} clauses, each terminated | |
862 | by a @samp{;;}. The first pattern that matches determines the | |
863 | command-list that is executed. | |
ccc6cda3 JA |
864 | |
865 | Here is an example using @code{case} in a script that could be used to | |
866 | describe one interesting feature of an animal: | |
867 | ||
868 | @example | |
869 | echo -n "Enter the name of an animal: " | |
870 | read ANIMAL | |
871 | echo -n "The $ANIMAL has " | |
872 | case $ANIMAL in | |
873 | horse | dog | cat) echo -n "four";; | |
874 | man | kangaroo ) echo -n "two";; | |
875 | *) echo -n "an unknown number of";; | |
876 | esac | |
877 | echo " legs." | |
878 | @end example | |
879 | ||
cce855bc JA |
880 | @noindent |
881 | The return status is zero if no @var{pattern} is matched. Otherwise, the | |
882 | return status is the exit status of the @var{command-list} executed. | |
883 | ||
884 | @item select | |
885 | @rwindex select | |
886 | ||
887 | The @code{select} construct allows the easy generation of menus. | |
888 | It has almost the same syntax as the @code{for} command: | |
889 | ||
890 | @example | |
891 | select @var{name} [in @var{words} @dots{}]; do @var{commands}; done | |
892 | @end example | |
893 | ||
894 | The list of words following @code{in} is expanded, generating a list | |
895 | of items. The set of expanded words is printed on the standard | |
896 | error output stream, each preceded by a number. If the | |
897 | @samp{in @var{words}} is omitted, the positional parameters are printed, | |
898 | as if @samp{in "$@@"} had been specifed. | |
28ef6c31 | 899 | The @env{PS3} prompt is then displayed and a line is read from the |
cce855bc JA |
900 | standard input. |
901 | If the line consists of a number corresponding to one of the displayed | |
902 | words, then the value of @var{name} is set to that word. | |
903 | If the line is empty, the words and prompt are displayed again. | |
904 | If @code{EOF} is read, the @code{select} command completes. | |
905 | Any other value read causes @var{name} to be set to null. | |
28ef6c31 | 906 | The line read is saved in the variable @env{REPLY}. |
cce855bc JA |
907 | |
908 | The @var{commands} are executed after each selection until a | |
f73dda09 | 909 | @code{break} command is executed, at which |
cce855bc JA |
910 | point the @code{select} command completes. |
911 | ||
912 | Here is an example that allows the user to pick a filename from the | |
913 | current directory, and displays the name and index of the file | |
914 | selected. | |
915 | ||
916 | @example | |
917 | select fname in *; | |
918 | do | |
919 | echo you picked $fname \($REPLY\) | |
920 | break; | |
921 | done | |
922 | @end example | |
923 | ||
ccc6cda3 JA |
924 | @item ((@dots{})) |
925 | @example | |
926 | (( @var{expression} )) | |
927 | @end example | |
928 | ||
cce855bc JA |
929 | The arithmetic @var{expression} is evaluated according to the rules |
930 | described below (@pxref{Shell Arithmetic}). | |
ccc6cda3 JA |
931 | If the value of the expression is non-zero, the return status is 0; |
932 | otherwise the return status is 1. This is exactly equivalent to | |
933 | @example | |
934 | let "@var{expression}" | |
935 | @end example | |
cce855bc JA |
936 | @noindent |
937 | @xref{Bash Builtins}, for a full description of the @code{let} builtin. | |
938 | ||
939 | @item [[@dots{}]] | |
940 | @rwindex [[ | |
941 | @rwindex ]] | |
942 | @example | |
943 | [[ @var{expression} ]] | |
944 | @end example | |
945 | ||
946 | Return a status of 0 or 1 depending on the evaluation of | |
947 | the conditional expression @var{expression}. | |
948 | Expressions are composed of the primaries described below in | |
949 | @ref{Bash Conditional Expressions}. | |
950 | Word splitting and filename expansion are not performed on the words | |
951 | between the @samp{[[} and @samp{]]}; tilde expansion, parameter and | |
952 | variable expansion, arithmetic expansion, command substitution, process | |
953 | substitution, and quote removal are performed. | |
b80f6443 JA |
954 | Conditional operators such as @samp{-f} must be unquoted to be recognized |
955 | as primaries. | |
cce855bc JA |
956 | |
957 | When the @samp{==} and @samp{!=} operators are used, the string to the | |
958 | right of the operator is considered a pattern and matched according | |
959 | to the rules described below in @ref{Pattern Matching}. | |
95732b49 JA |
960 | If the shell option @code{nocasematch} |
961 | (see the description of @code{shopt} in @ref{Bash Builtins}) | |
962 | is enabled, the match is performed without regard to the case | |
963 | of alphabetic characters. | |
cce855bc JA |
964 | The return value is 0 if the string matches or does not match |
965 | the pattern, respectively, and 1 otherwise. | |
966 | Any part of the pattern may be quoted to force it to be matched as a | |
967 | string. | |
968 | ||
b80f6443 JA |
969 | An additional binary operator, @samp{=~}, is available, with the same |
970 | precedence as @samp{==} and @samp{!=}. | |
971 | When it is used, the string to the right of the operator is considered | |
972 | an extended regular expression and matched accordingly (as in @i{regex}3)). | |
973 | The return value is 0 if the string matches | |
974 | the pattern, and 1 otherwise. | |
975 | If the regular expression is syntactically incorrect, the conditional | |
976 | expression's return value is 2. | |
95732b49 | 977 | If the shell option @code{nocasematch} |
b80f6443 JA |
978 | (see the description of @code{shopt} in @ref{Bash Builtins}) |
979 | is enabled, the match is performed without regard to the case | |
980 | of alphabetic characters. | |
981 | Substrings matched by parenthesized subexpressions within the regular | |
982 | expression are saved in the array variable @code{BASH_REMATCH}. | |
983 | The element of @code{BASH_REMATCH} with index 0 is the portion of the string | |
984 | matching the entire regular expression. | |
985 | The element of @code{BASH_REMATCH} with index @var{n} is the portion of the | |
986 | string matching the @var{n}th parenthesized subexpression. | |
987 | ||
cce855bc JA |
988 | Expressions may be combined using the following operators, listed |
989 | in decreasing order of precedence: | |
990 | ||
991 | @table @code | |
992 | @item ( @var{expression} ) | |
993 | Returns the value of @var{expression}. | |
994 | This may be used to override the normal precedence of operators. | |
ccc6cda3 | 995 | |
cce855bc JA |
996 | @item ! @var{expression} |
997 | True if @var{expression} is false. | |
998 | ||
999 | @item @var{expression1} && @var{expression2} | |
1000 | True if both @var{expression1} and @var{expression2} are true. | |
1001 | ||
1002 | @item @var{expression1} || @var{expression2} | |
1003 | True if either @var{expression1} or @var{expression2} is true. | |
ccc6cda3 | 1004 | @end table |
cce855bc | 1005 | @noindent |
7117c2d2 | 1006 | The @code{&&} and @code{||} operators do not evaluate @var{expression2} if the |
cce855bc JA |
1007 | value of @var{expression1} is sufficient to determine the return |
1008 | value of the entire conditional expression. | |
ccc6cda3 | 1009 | |
cce855bc | 1010 | @end table |
ccc6cda3 JA |
1011 | |
1012 | @node Command Grouping | |
b80f6443 | 1013 | @subsubsection Grouping Commands |
ccc6cda3 JA |
1014 | @cindex commands, grouping |
1015 | ||
1016 | Bash provides two ways to group a list of commands to be executed | |
1017 | as a unit. When commands are grouped, redirections may be applied | |
1018 | to the entire command list. For example, the output of all the | |
1019 | commands in the list may be redirected to a single stream. | |
1020 | ||
1021 | @table @code | |
1022 | @item () | |
1023 | @example | |
1024 | ( @var{list} ) | |
1025 | @end example | |
1026 | ||
1027 | Placing a list of commands between parentheses causes a subshell | |
b80f6443 JA |
1028 | environment to be created (@pxref{Command Execution Environment}), and each |
1029 | of the commands in @var{list} to be executed in that subshell. Since the | |
1030 | @var{list} is executed in a subshell, variable assignments do not remain in | |
1031 | effect after the subshell completes. | |
ccc6cda3 JA |
1032 | |
1033 | @item @{@} | |
1034 | @rwindex @{ | |
1035 | @rwindex @} | |
1036 | @example | |
1037 | @{ @var{list}; @} | |
1038 | @end example | |
1039 | ||
1040 | Placing a list of commands between curly braces causes the list to | |
1041 | be executed in the current shell context. No subshell is created. | |
cce855bc | 1042 | The semicolon (or newline) following @var{list} is required. |
ccc6cda3 JA |
1043 | @end table |
1044 | ||
1045 | In addition to the creation of a subshell, there is a subtle difference | |
1046 | between these two constructs due to historical reasons. The braces | |
1047 | are @code{reserved words}, so they must be separated from the @var{list} | |
1048 | by @code{blank}s. The parentheses are @code{operators}, and are | |
1049 | recognized as separate tokens by the shell even if they are not separated | |
d166f048 | 1050 | from the @var{list} by whitespace. |
ccc6cda3 JA |
1051 | |
1052 | The exit status of both of these constructs is the exit status of | |
1053 | @var{list}. | |
1054 | ||
1055 | @node Shell Functions | |
1056 | @section Shell Functions | |
1057 | @cindex shell function | |
1058 | @cindex functions, shell | |
1059 | ||
1060 | Shell functions are a way to group commands for later execution | |
1061 | using a single name for the group. They are executed just like | |
bb70624e JA |
1062 | a "regular" command. |
1063 | When the name of a shell function is used as a simple command name, | |
1064 | the list of commands associated with that function name is executed. | |
1065 | Shell functions are executed in the current | |
ccc6cda3 JA |
1066 | shell context; no new process is created to interpret them. |
1067 | ||
1068 | Functions are declared using this syntax: | |
1069 | @rwindex function | |
1070 | @example | |
b80f6443 | 1071 | [ @code{function} ] @var{name} () @var{compound-command} [ @var{redirections} ] |
ccc6cda3 JA |
1072 | @end example |
1073 | ||
1074 | This defines a shell function named @var{name}. The reserved | |
cce855bc JA |
1075 | word @code{function} is optional. |
1076 | If the @code{function} reserved | |
1077 | word is supplied, the parentheses are optional. | |
b80f6443 JA |
1078 | The @var{body} of the function is the compound command |
1079 | @var{compound-command} (@pxref{Compound Commands}). | |
1080 | That command is usually a @var{list} enclosed between @{ and @}, but | |
1081 | may be any compound command listed above. | |
1082 | @var{compound-command} is executed whenever @var{name} is specified as the | |
1083 | name of a command. | |
1084 | Any redirections (@pxref{Redirections}) associated with the shell function | |
1085 | are performed when the function is executed. | |
1086 | ||
1087 | The exit status of a function definition is zero unless a syntax error | |
1088 | occurs or a readonly function with the same name already exists. | |
1089 | When executed, the exit status of a function is the exit status of the | |
1090 | last command executed in the body. | |
1091 | ||
1092 | Note that for historical reasons, in the most common usage the curly braces | |
1093 | that surround the body of the function must be separated from the body by | |
bb70624e JA |
1094 | @code{blank}s or newlines. |
1095 | This is because the braces are reserved words and are only recognized | |
1096 | as such when they are separated by whitespace. | |
b80f6443 JA |
1097 | Also, when using the braces, the @var{list} must be terminated by a semicolon, |
1098 | a @samp{&}, or a newline. | |
bb70624e | 1099 | |
ccc6cda3 JA |
1100 | When a function is executed, the arguments to the |
1101 | function become the positional parameters | |
1102 | during its execution (@pxref{Positional Parameters}). | |
d166f048 JA |
1103 | The special parameter @samp{#} that expands to the number of |
1104 | positional parameters is updated to reflect the change. | |
b80f6443 JA |
1105 | Special parameter @code{0} is unchanged. |
1106 | The first element of the @env{FUNCNAME} variable is set to the | |
1107 | name of the function while the function is executing. | |
1108 | All other aspects of the shell execution | |
1109 | environment are identical between a function and its caller | |
95732b49 JA |
1110 | with the exception that the @env{DEBUG} and @env{RETURN} traps |
1111 | are not inherited unless the function has been given the | |
b80f6443 JA |
1112 | @code{trace} attribute using the @code{declare} builtin or |
1113 | the @code{-o functrace} option has been enabled with | |
1114 | the @code{set} builtin, | |
95732b49 | 1115 | (in which case all functions inherit the @env{DEBUG} and @env{RETURN} traps). |
b80f6443 JA |
1116 | @xref{Bourne Shell Builtins}, for the description of the |
1117 | @code{trap} builtin. | |
ccc6cda3 JA |
1118 | |
1119 | If the builtin command @code{return} | |
1120 | is executed in a function, the function completes and | |
1121 | execution resumes with the next command after the function | |
b80f6443 JA |
1122 | call. |
1123 | Any command associated with the @code{RETURN} trap is executed | |
1124 | before execution resumes. | |
1125 | When a function completes, the values of the | |
ccc6cda3 | 1126 | positional parameters and the special parameter @samp{#} |
cce855bc | 1127 | are restored to the values they had prior to the function's |
ccc6cda3 | 1128 | execution. If a numeric argument is given to @code{return}, |
f73dda09 | 1129 | that is the function's return status; otherwise the function's |
cce855bc JA |
1130 | return status is the exit status of the last command executed |
1131 | before the @code{return}. | |
ccc6cda3 JA |
1132 | |
1133 | Variables local to the function may be declared with the | |
1134 | @code{local} builtin. These variables are visible only to | |
1135 | the function and the commands it invokes. | |
1136 | ||
b80f6443 JA |
1137 | Function names and definitions may be listed with the |
1138 | @option{-f} option to the @code{declare} or @code{typeset} | |
1139 | builtin commands (@pxref{Bash Builtins}). | |
1140 | The @option{-F} option to @code{declare} or @code{typeset} | |
1141 | will list the function names only | |
1142 | (and optionally the source file and line number, if the @code{extdebug} | |
1143 | shell option is enabled). | |
1144 | Functions may be exported so that subshells | |
1145 | automatically have them defined with the | |
1146 | @option{-f} option to the @code{export} builtin | |
1147 | (@pxref{Bourne Shell Builtins}). | |
1148 | Note that shell functions and variables with the same name may result | |
1149 | in multiple identically-named entries in the environment passed to the | |
1150 | shell's children. | |
1151 | Care should be taken in cases where this may cause a problem. | |
1152 | ||
ccc6cda3 JA |
1153 | Functions may be recursive. No limit is placed on the number of |
1154 | recursive calls. | |
1155 | ||
1156 | @node Shell Parameters | |
1157 | @section Shell Parameters | |
1158 | @cindex parameters | |
1159 | @cindex variable, shell | |
1160 | @cindex shell variable | |
1161 | ||
1162 | @menu | |
1163 | * Positional Parameters:: The shell's command-line arguments. | |
b80f6443 | 1164 | * Special Parameters:: Parameters denoted by special characters. |
ccc6cda3 JA |
1165 | @end menu |
1166 | ||
1167 | A @var{parameter} is an entity that stores values. | |
1168 | It can be a @code{name}, a number, or one of the special characters | |
1169 | listed below. | |
b80f6443 | 1170 | A @var{variable} is a parameter denoted by a @code{name}. |
f73dda09 JA |
1171 | A variable has a @var{value} and zero or more @var{attributes}. |
1172 | Attributes are assigned using the @code{declare} builtin command | |
1173 | (see the description of the @code{declare} builtin in @ref{Bash Builtins}). | |
ccc6cda3 JA |
1174 | |
1175 | A parameter is set if it has been assigned a value. The null string is | |
1176 | a valid value. Once a variable is set, it may be unset only by using | |
1177 | the @code{unset} builtin command. | |
1178 | ||
1179 | A variable may be assigned to by a statement of the form | |
1180 | @example | |
1181 | @var{name}=[@var{value}] | |
1182 | @end example | |
1183 | @noindent | |
1184 | If @var{value} | |
1185 | is not given, the variable is assigned the null string. All | |
1186 | @var{value}s undergo tilde expansion, parameter and variable expansion, | |
1187 | command substitution, arithmetic expansion, and quote | |
cce855bc | 1188 | removal (detailed below). If the variable has its @code{integer} |
f73dda09 | 1189 | attribute set, then @var{value} |
b80f6443 | 1190 | is evaluated as an arithmetic expression even if the @code{$((@dots{}))} |
cce855bc | 1191 | expansion is not used (@pxref{Arithmetic Expansion}). |
ccc6cda3 JA |
1192 | Word splitting is not performed, with the exception |
1193 | of @code{"$@@"} as explained below. | |
1194 | Filename expansion is not performed. | |
f73dda09 | 1195 | Assignment statements may also appear as arguments to the |
b80f6443 | 1196 | @code{alias}, |
f73dda09 JA |
1197 | @code{declare}, @code{typeset}, @code{export}, @code{readonly}, |
1198 | and @code{local} builtin commands. | |
ccc6cda3 | 1199 | |
95732b49 JA |
1200 | In the context where an assignment statement is assigning a value |
1201 | to a shell variable or array index (@pxref{Arrays}), the @samp{+=} | |
1202 | operator can be used to | |
1203 | append to or add to the variable's previous value. | |
1204 | When @samp{+=} is applied to a variable for which the integer attribute | |
1205 | has been set, @var{value} is evaluated as an arithmetic expression and | |
1206 | added to the variable's current value, which is also evaluated. | |
1207 | When @samp{+=} is applied to an array variable using compound assignment | |
1208 | (@pxref{Arrays}), the | |
1209 | variable's value is not unset (as it is when using @samp{=}), and new | |
1210 | values are appended to the array beginning at one greater than the array's | |
1211 | maximum index. | |
1212 | When applied to a string-valued variable, @var{value} is expanded and | |
1213 | appended to the variable's value. | |
1214 | ||
ccc6cda3 JA |
1215 | @node Positional Parameters |
1216 | @subsection Positional Parameters | |
1217 | @cindex parameters, positional | |
1218 | ||
cce855bc | 1219 | A @var{positional parameter} is a parameter denoted by one or more |
ccc6cda3 JA |
1220 | digits, other than the single digit @code{0}. Positional parameters are |
1221 | assigned from the shell's arguments when it is invoked, | |
cce855bc | 1222 | and may be reassigned using the @code{set} builtin command. |
bb70624e JA |
1223 | Positional parameter @code{N} may be referenced as @code{$@{N@}}, or |
1224 | as @code{$N} when @code{N} consists of a single digit. | |
1225 | Positional parameters may not be assigned to with assignment statements. | |
1226 | The @code{set} and @code{shift} builtins are used to set and | |
1227 | unset them (@pxref{Shell Builtin Commands}). | |
1228 | The positional parameters are | |
ccc6cda3 JA |
1229 | temporarily replaced when a shell function is executed |
1230 | (@pxref{Shell Functions}). | |
1231 | ||
1232 | When a positional parameter consisting of more than a single | |
1233 | digit is expanded, it must be enclosed in braces. | |
1234 | ||
1235 | @node Special Parameters | |
1236 | @subsection Special Parameters | |
1237 | @cindex parameters, special | |
1238 | ||
1239 | The shell treats several parameters specially. These parameters may | |
1240 | only be referenced; assignment to them is not allowed. | |
1241 | ||
1242 | @vtable @code | |
1243 | ||
1244 | @item * | |
1245 | Expands to the positional parameters, starting from one. When the | |
1246 | expansion occurs within double quotes, it expands to a single word | |
1247 | with the value of each parameter separated by the first character | |
28ef6c31 | 1248 | of the @env{IFS} |
ccc6cda3 JA |
1249 | special variable. That is, @code{"$*"} is equivalent |
1250 | to @code{"$1@var{c}$2@var{c}@dots{}"}, where @var{c} | |
1251 | is the first character of the value of the @code{IFS} | |
d166f048 | 1252 | variable. |
28ef6c31 JA |
1253 | If @env{IFS} is unset, the parameters are separated by spaces. |
1254 | If @env{IFS} is null, the parameters are joined without intervening | |
d166f048 JA |
1255 | separators. |
1256 | ||
ccc6cda3 JA |
1257 | |
1258 | @item @@ | |
1259 | Expands to the positional parameters, starting from one. When the | |
cce855bc | 1260 | expansion occurs within double quotes, each parameter expands to a |
ccc6cda3 JA |
1261 | separate word. That is, @code{"$@@"} is equivalent to |
1262 | @code{"$1" "$2" @dots{}}. | |
95732b49 JA |
1263 | If the double-quoted expansion occurs within a word, the expansion of |
1264 | the first parameter is joined with the beginning part of the original | |
1265 | word, and the expansion of the last parameter is joined with the last | |
1266 | part of the original word. | |
ccc6cda3 JA |
1267 | When there are no positional parameters, @code{"$@@"} and |
1268 | @code{$@@} | |
1269 | expand to nothing (i.e., they are removed). | |
1270 | ||
1271 | @item # | |
1272 | Expands to the number of positional parameters in decimal. | |
1273 | ||
1274 | @item ? | |
1275 | Expands to the exit status of the most recently executed foreground | |
1276 | pipeline. | |
1277 | ||
1278 | @item - | |
bb70624e JA |
1279 | (A hyphen.) Expands to the current option flags as specified upon |
1280 | invocation, by the @code{set} | |
ccc6cda3 | 1281 | builtin command, or those set by the shell itself |
28ef6c31 | 1282 | (such as the @option{-i} option). |
ccc6cda3 JA |
1283 | |
1284 | @item $ | |
bb70624e JA |
1285 | Expands to the process @sc{id} of the shell. In a @code{()} subshell, it |
1286 | expands to the process @sc{id} of the invoking shell, not the subshell. | |
ccc6cda3 JA |
1287 | |
1288 | @item ! | |
bb70624e | 1289 | Expands to the process @sc{id} of the most recently executed background |
ccc6cda3 JA |
1290 | (asynchronous) command. |
1291 | ||
1292 | @item 0 | |
1293 | Expands to the name of the shell or shell script. This is set at | |
cce855bc JA |
1294 | shell initialization. If Bash is invoked with a file of commands |
1295 | (@pxref{Shell Scripts}), @code{$0} is set to the name of that file. | |
28ef6c31 | 1296 | If Bash is started with the @option{-c} option (@pxref{Invoking Bash}), |
cce855bc | 1297 | then @code{$0} is set to the first argument after the string to be |
ccc6cda3 JA |
1298 | executed, if one is present. Otherwise, it is set |
1299 | to the filename used to invoke Bash, as given by argument zero. | |
1300 | ||
1301 | @item _ | |
bb70624e | 1302 | (An underscore.) |
95732b49 JA |
1303 | At shell startup, set to the absolute pathname used to invoke the |
1304 | shell or shell script being executed as passed in the environment | |
1305 | or argument list. | |
ccc6cda3 JA |
1306 | Subsequently, expands to the last argument to the previous command, |
1307 | after expansion. | |
95732b49 JA |
1308 | Also set to the full pathname used to invoke each command executed |
1309 | and placed in the environment exported to that command. | |
ccc6cda3 JA |
1310 | When checking mail, this parameter holds the name of the mail file. |
1311 | @end vtable | |
1312 | ||
1313 | @node Shell Expansions | |
1314 | @section Shell Expansions | |
1315 | @cindex expansion | |
1316 | ||
1317 | Expansion is performed on the command line after it has been split into | |
1318 | @code{token}s. There are seven kinds of expansion performed: | |
1319 | @itemize @bullet | |
1320 | @item brace expansion | |
1321 | @item tilde expansion | |
1322 | @item parameter and variable expansion | |
1323 | @item command substitution | |
1324 | @item arithmetic expansion | |
1325 | @item word splitting | |
1326 | @item filename expansion | |
1327 | @end itemize | |
1328 | ||
1329 | @menu | |
cce855bc JA |
1330 | * Brace Expansion:: Expansion of expressions within braces. |
1331 | * Tilde Expansion:: Expansion of the ~ character. | |
ccc6cda3 JA |
1332 | * Shell Parameter Expansion:: How Bash expands variables to their values. |
1333 | * Command Substitution:: Using the output of a command as an argument. | |
cce855bc | 1334 | * Arithmetic Expansion:: How to use arithmetic in shell expansions. |
ccc6cda3 JA |
1335 | * Process Substitution:: A way to write and read to and from a |
1336 | command. | |
1337 | * Word Splitting:: How the results of expansion are split into separate | |
1338 | arguments. | |
1339 | * Filename Expansion:: A shorthand for specifying filenames matching patterns. | |
1340 | * Quote Removal:: How and when quote characters are removed from | |
1341 | words. | |
1342 | @end menu | |
1343 | ||
ccc6cda3 JA |
1344 | The order of expansions is: brace expansion, tilde expansion, |
1345 | parameter, variable, and arithmetic expansion and | |
1346 | command substitution | |
1347 | (done in a left-to-right fashion), word splitting, and filename | |
1348 | expansion. | |
1349 | ||
1350 | On systems that can support it, there is an additional expansion | |
1351 | available: @var{process substitution}. This is performed at the | |
cce855bc | 1352 | same time as parameter, variable, and arithmetic expansion and |
ccc6cda3 JA |
1353 | command substitution. |
1354 | ||
1355 | Only brace expansion, word splitting, and filename expansion | |
1356 | can change the number of words of the expansion; other expansions | |
1357 | expand a single word to a single word. | |
1358 | The only exceptions to this are the expansions of | |
d166f048 | 1359 | @code{"$@@"} (@pxref{Special Parameters}) and @code{"$@{@var{name}[@@]@}"} |
ccc6cda3 JA |
1360 | (@pxref{Arrays}). |
1361 | ||
1362 | After all expansions, @code{quote removal} (@pxref{Quote Removal}) | |
1363 | is performed. | |
1364 | ||
cce855bc JA |
1365 | @node Brace Expansion |
1366 | @subsection Brace Expansion | |
1367 | @cindex brace expansion | |
1368 | @cindex expansion, brace | |
1369 | ||
bb70624e JA |
1370 | Brace expansion is a mechanism by which arbitrary strings may be generated. |
1371 | This mechanism is similar to | |
cce855bc | 1372 | @var{filename expansion} (@pxref{Filename Expansion}), |
bb70624e JA |
1373 | but the file names generated need not exist. |
1374 | Patterns to be brace expanded take the form of an optional @var{preamble}, | |
b80f6443 JA |
1375 | followed by either a series of comma-separated strings or a sequnce expression |
1376 | between a pair of braces, | |
bb70624e JA |
1377 | followed by an optional @var{postscript}. |
1378 | The preamble is prefixed to each string contained within the braces, and | |
1379 | the postscript is then appended to each resulting string, expanding left | |
1380 | to right. | |
1381 | ||
1382 | Brace expansions may be nested. | |
1383 | The results of each expanded string are not sorted; left to right order | |
1384 | is preserved. | |
cce855bc JA |
1385 | For example, |
1386 | @example | |
1387 | bash$ echo a@{d,c,b@}e | |
1388 | ade ace abe | |
1389 | @end example | |
1390 | ||
b80f6443 JA |
1391 | A sequence expression takes the form @code{@{@var{x}..@var{y}@}}, |
1392 | where @var{x} and @var{y} are either integers or single characters. | |
1393 | When integers are supplied, the expression expands to each number between | |
1394 | @var{x} and @var{y}, inclusive. | |
1395 | When characters are supplied, the expression expands to each character | |
1396 | lexicographically between @var{x} and @var{y}, inclusive. Note that | |
1397 | both @var{x} and @var{y} must be of the same type. | |
1398 | ||
cce855bc JA |
1399 | Brace expansion is performed before any other expansions, |
1400 | and any characters special to other expansions are preserved | |
1401 | in the result. It is strictly textual. Bash | |
1402 | does not apply any syntactic interpretation to the context of the | |
1403 | expansion or the text between the braces. | |
bb70624e JA |
1404 | To avoid conflicts with parameter expansion, the string @samp{$@{} |
1405 | is not considered eligible for brace expansion. | |
cce855bc JA |
1406 | |
1407 | A correctly-formed brace expansion must contain unquoted opening | |
b80f6443 JA |
1408 | and closing braces, and at least one unquoted comma or a valid |
1409 | sequence expression. | |
cce855bc JA |
1410 | Any incorrectly formed brace expansion is left unchanged. |
1411 | ||
b80f6443 JA |
1412 | A @{ or @samp{,} may be quoted with a backslash to prevent its |
1413 | being considered part of a brace expression. | |
1414 | To avoid conflicts with parameter expansion, the string @samp{$@{} | |
1415 | is not considered eligible for brace expansion. | |
1416 | ||
cce855bc JA |
1417 | This construct is typically used as shorthand when the common |
1418 | prefix of the strings to be generated is longer than in the | |
1419 | above example: | |
1420 | @example | |
1421 | mkdir /usr/local/src/bash/@{old,new,dist,bugs@} | |
1422 | @end example | |
1423 | or | |
1424 | @example | |
1425 | chown root /usr/@{ucb/@{ex,edit@},lib/@{ex?.?*,how_ex@}@} | |
1426 | @end example | |
1427 | ||
1428 | @node Tilde Expansion | |
1429 | @subsection Tilde Expansion | |
1430 | @cindex tilde expansion | |
1431 | @cindex expansion, tilde | |
1432 | ||
1433 | If a word begins with an unquoted tilde character (@samp{~}), all of the | |
1434 | characters up to the first unquoted slash (or all characters, | |
1435 | if there is no unquoted slash) are considered a @var{tilde-prefix}. | |
1436 | If none of the characters in the tilde-prefix are quoted, the | |
1437 | characters in the tilde-prefix following the tilde are treated as a | |
1438 | possible @var{login name}. | |
1439 | If this login name is the null string, the tilde is replaced with the | |
28ef6c31 JA |
1440 | value of the @env{HOME} shell variable. |
1441 | If @env{HOME} is unset, the home directory of the user executing the | |
cce855bc JA |
1442 | shell is substituted instead. |
1443 | Otherwise, the tilde-prefix is replaced with the home directory | |
1444 | associated with the specified login name. | |
1445 | ||
1446 | If the tilde-prefix is @samp{~+}, the value of | |
28ef6c31 | 1447 | the shell variable @env{PWD} replaces the tilde-prefix. |
cce855bc | 1448 | If the tilde-prefix is @samp{~-}, the value of the shell variable |
28ef6c31 | 1449 | @env{OLDPWD}, if it is set, is substituted. |
cce855bc JA |
1450 | |
1451 | If the characters following the tilde in the tilde-prefix consist of a | |
1452 | number @var{N}, optionally prefixed by a @samp{+} or a @samp{-}, | |
1453 | the tilde-prefix is replaced with the | |
1454 | corresponding element from the directory stack, as it would be displayed | |
1455 | by the @code{dirs} builtin invoked with the characters following tilde | |
1456 | in the tilde-prefix as an argument (@pxref{The Directory Stack}). | |
1457 | If the tilde-prefix, sans the tilde, consists of a number without a | |
1458 | leading @samp{+} or @samp{-}, @samp{+} is assumed. | |
1459 | ||
1460 | If the login name is invalid, or the tilde expansion fails, the word is | |
1461 | left unchanged. | |
1462 | ||
1463 | Each variable assignment is checked for unquoted tilde-prefixes immediately | |
95732b49 | 1464 | following a @samp{:} or the first @samp{=}. |
cce855bc JA |
1465 | In these cases, tilde expansion is also performed. |
1466 | Consequently, one may use file names with tildes in assignments to | |
28ef6c31 | 1467 | @env{PATH}, @env{MAILPATH}, and @env{CDPATH}, |
cce855bc JA |
1468 | and the shell assigns the expanded value. |
1469 | ||
1470 | The following table shows how Bash treats unquoted tilde-prefixes: | |
1471 | ||
1472 | @table @code | |
1473 | @item ~ | |
1474 | The value of @code{$HOME} | |
1475 | @item ~/foo | |
1476 | @file{$HOME/foo} | |
1477 | ||
1478 | @item ~fred/foo | |
1479 | The subdirectory @code{foo} of the home directory of the user | |
1480 | @code{fred} | |
1481 | ||
1482 | @item ~+/foo | |
1483 | @file{$PWD/foo} | |
1484 | ||
1485 | @item ~-/foo | |
1486 | @file{$@{OLDPWD-'~-'@}/foo} | |
1487 | ||
1488 | @item ~@var{N} | |
1489 | The string that would be displayed by @samp{dirs +@var{N}} | |
1490 | ||
1491 | @item ~+@var{N} | |
1492 | The string that would be displayed by @samp{dirs +@var{N}} | |
1493 | ||
1494 | @item ~-@var{N} | |
1495 | The string that would be displayed by @samp{dirs -@var{N}} | |
1496 | ||
1497 | @end table | |
1498 | ||
ccc6cda3 JA |
1499 | @node Shell Parameter Expansion |
1500 | @subsection Shell Parameter Expansion | |
1501 | @cindex parameter expansion | |
1502 | @cindex expansion, parameter | |
1503 | ||
1504 | The @samp{$} character introduces parameter expansion, | |
1505 | command substitution, or arithmetic expansion. The parameter name | |
1506 | or symbol to be expanded may be enclosed in braces, which | |
1507 | are optional but serve to protect the variable to be expanded from | |
1508 | characters immediately following it which could be | |
1509 | interpreted as part of the name. | |
1510 | ||
cce855bc JA |
1511 | When braces are used, the matching ending brace is the first @samp{@}} |
1512 | not escaped by a backslash or within a quoted string, and not within an | |
1513 | embedded arithmetic expansion, command substitution, or parameter | |
1514 | expansion. | |
1515 | ||
ccc6cda3 JA |
1516 | The basic form of parameter expansion is $@{@var{parameter}@}. |
1517 | The value of @var{parameter} is substituted. The braces are required | |
1518 | when @var{parameter} | |
1519 | is a positional parameter with more than one digit, | |
1520 | or when @var{parameter} | |
1521 | is followed by a character that is not to be | |
1522 | interpreted as part of its name. | |
1523 | ||
1524 | If the first character of @var{parameter} is an exclamation point, | |
1525 | a level of variable indirection is introduced. | |
1526 | Bash uses the value of the variable formed from the rest of | |
1527 | @var{parameter} as the name of the variable; this variable is then | |
d166f048 | 1528 | expanded and that value is used in the rest of the substitution, rather |
ccc6cda3 JA |
1529 | than the value of @var{parameter} itself. |
1530 | This is known as @code{indirect expansion}. | |
b80f6443 JA |
1531 | The exceptions to this are the expansions of $@{!@var{prefix*}@} |
1532 | and $@{!@var{name}[@@]@} | |
bb70624e | 1533 | described below. |
b80f6443 JA |
1534 | The exclamation point must immediately follow the left brace in order to |
1535 | introduce indirection. | |
ccc6cda3 JA |
1536 | |
1537 | In each of the cases below, @var{word} is subject to tilde expansion, | |
1538 | parameter expansion, command substitution, and arithmetic expansion. | |
bb70624e | 1539 | |
ccc6cda3 JA |
1540 | When not performing substring expansion, Bash tests for a parameter |
1541 | that is unset or null; omitting the colon results in a test only for a | |
bb70624e JA |
1542 | parameter that is unset. Put another way, if the colon is included, |
1543 | the operator tests for both existence and that the value is not null; | |
1544 | if the colon is omitted, the operator tests only for existence. | |
ccc6cda3 JA |
1545 | |
1546 | @table @code | |
1547 | ||
1548 | @item $@{@var{parameter}:@minus{}@var{word}@} | |
1549 | If @var{parameter} is unset or null, the expansion of | |
1550 | @var{word} is substituted. Otherwise, the value of | |
1551 | @var{parameter} is substituted. | |
1552 | ||
1553 | @item $@{@var{parameter}:=@var{word}@} | |
1554 | If @var{parameter} | |
1555 | is unset or null, the expansion of @var{word} | |
1556 | is assigned to @var{parameter}. | |
b80f6443 JA |
1557 | The value of @var{parameter} is then substituted. |
1558 | Positional parameters and special parameters may not be assigned to | |
1559 | in this way. | |
ccc6cda3 JA |
1560 | |
1561 | @item $@{@var{parameter}:?@var{word}@} | |
1562 | If @var{parameter} | |
1563 | is null or unset, the expansion of @var{word} (or a message | |
1564 | to that effect if @var{word} | |
1565 | is not present) is written to the standard error and the shell, if it | |
1566 | is not interactive, exits. Otherwise, the value of @var{parameter} is | |
1567 | substituted. | |
1568 | ||
1569 | @item $@{@var{parameter}:+@var{word}@} | |
1570 | If @var{parameter} | |
1571 | is null or unset, nothing is substituted, otherwise the expansion of | |
1572 | @var{word} is substituted. | |
1573 | ||
1574 | @item $@{@var{parameter}:@var{offset}@} | |
1575 | @itemx $@{@var{parameter}:@var{offset}:@var{length}@} | |
bb70624e | 1576 | Expands to up to @var{length} characters of @var{parameter} |
cce855bc | 1577 | starting at the character specified by @var{offset}. |
ccc6cda3 | 1578 | If @var{length} is omitted, expands to the substring of |
bb70624e | 1579 | @var{parameter} starting at the character specified by @var{offset}. |
ccc6cda3 | 1580 | @var{length} and @var{offset} are arithmetic expressions |
cce855bc | 1581 | (@pxref{Shell Arithmetic}). |
ccc6cda3 JA |
1582 | This is referred to as Substring Expansion. |
1583 | ||
1584 | @var{length} must evaluate to a number greater than or equal to zero. | |
1585 | If @var{offset} evaluates to a number less than zero, the value | |
1586 | is used as an offset from the end of the value of @var{parameter}. | |
1587 | If @var{parameter} is @samp{@@}, the result is @var{length} positional | |
1588 | parameters beginning at @var{offset}. | |
1589 | If @var{parameter} is an array name indexed by @samp{@@} or @samp{*}, | |
1590 | the result is the @var{length} | |
cce855bc | 1591 | members of the array beginning with @code{$@{@var{parameter}[@var{offset}]@}}. |
95732b49 JA |
1592 | A negative @var{offset} is taken relative to one greater than the maximum |
1593 | index of the specified array. | |
1594 | Note that a negative offset must be separated from the colon by at least | |
1595 | one space to avoid being confused with the @samp{:-} expansion. | |
cce855bc JA |
1596 | Substring indexing is zero-based unless the positional parameters |
1597 | are used, in which case the indexing starts at 1. | |
ccc6cda3 | 1598 | |
bb70624e | 1599 | @item $@{!@var{prefix}*@} |
b80f6443 | 1600 | @itemx $@{!@var{prefix}@@@} |
bb70624e | 1601 | Expands to the names of variables whose names begin with @var{prefix}, |
28ef6c31 | 1602 | separated by the first character of the @env{IFS} special variable. |
bb70624e | 1603 | |
b80f6443 JA |
1604 | @item $@{!@var{name}[@@]@} |
1605 | @itemx $@{!@var{name}[*]@} | |
1606 | If @var{name} is an array variable, expands to the list of array indices | |
1607 | (keys) assigned in @var{name}. | |
1608 | If @var{name} is not an array, expands to 0 if @var{name} is set and null | |
1609 | otherwise. | |
1610 | When @samp{@@} is used and the expansion appears within double quotes, each | |
1611 | key expands to a separate word. | |
1612 | ||
ccc6cda3 | 1613 | @item $@{#@var{parameter}@} |
cce855bc JA |
1614 | The length in characters of the expanded value of @var{parameter} is |
1615 | substituted. | |
1616 | If @var{parameter} is @samp{*} or @samp{@@}, the value substituted | |
1617 | is the number of positional parameters. | |
1618 | If @var{parameter} is an array name subscripted by @samp{*} or @samp{@@}, | |
1619 | the value substituted is the number of elements in the array. | |
ccc6cda3 JA |
1620 | |
1621 | @item $@{@var{parameter}#@var{word}@} | |
1622 | @itemx $@{@var{parameter}##@var{word}@} | |
1623 | The @var{word} | |
1624 | is expanded to produce a pattern just as in filename | |
1625 | expansion (@pxref{Filename Expansion}). If the pattern matches | |
cce855bc JA |
1626 | the beginning of the expanded value of @var{parameter}, |
1627 | then the result of the expansion is the expanded value of @var{parameter} | |
ccc6cda3 JA |
1628 | with the shortest matching pattern (the @samp{#} case) or the |
1629 | longest matching pattern (the @samp{##} case) deleted. | |
1630 | If @var{parameter} is @samp{@@} or @samp{*}, | |
1631 | the pattern removal operation is applied to each positional | |
1632 | parameter in turn, and the expansion is the resultant list. | |
1633 | If @var{parameter} is an array variable subscripted with | |
1634 | @samp{@@} or @samp{*}, | |
1635 | the pattern removal operation is applied to each member of the | |
1636 | array in turn, and the expansion is the resultant list. | |
1637 | ||
1638 | @item $@{@var{parameter}%@var{word}@} | |
1639 | @itemx $@{@var{parameter}%%@var{word}@} | |
1640 | The @var{word} is expanded to produce a pattern just as in | |
1641 | filename expansion. | |
cce855bc JA |
1642 | If the pattern matches a trailing portion of the expanded value of |
1643 | @var{parameter}, then the result of the expansion is the value of | |
1644 | @var{parameter} with the shortest matching pattern (the @samp{%} case) | |
1645 | or the longest matching pattern (the @samp{%%} case) deleted. | |
ccc6cda3 JA |
1646 | If @var{parameter} is @samp{@@} or @samp{*}, |
1647 | the pattern removal operation is applied to each positional | |
1648 | parameter in turn, and the expansion is the resultant list. | |
1649 | If @var{parameter} | |
1650 | is an array variable subscripted with @samp{@@} or @samp{*}, | |
1651 | the pattern removal operation is applied to each member of the | |
1652 | array in turn, and the expansion is the resultant list. | |
1653 | ||
1654 | @item $@{@var{parameter}/@var{pattern}/@var{string}@} | |
1655 | @itemx $@{@var{parameter}//@var{pattern}/@var{string}@} | |
1656 | ||
1657 | The @var{pattern} is expanded to produce a pattern just as in | |
1658 | filename expansion. | |
1659 | @var{Parameter} is expanded and the longest match of @var{pattern} | |
1660 | against its value is replaced with @var{string}. | |
1661 | In the first form, only the first match is replaced. | |
1662 | The second form causes all matches of @var{pattern} to be | |
1663 | replaced with @var{string}. | |
1664 | If @var{pattern} begins with @samp{#}, it must match at the beginning | |
b72432fd | 1665 | of the expanded value of @var{parameter}. |
ccc6cda3 | 1666 | If @var{pattern} begins with @samp{%}, it must match at the end |
b72432fd | 1667 | of the expanded value of @var{parameter}. |
ccc6cda3 JA |
1668 | If @var{string} is null, matches of @var{pattern} are deleted |
1669 | and the @code{/} following @var{pattern} may be omitted. | |
1670 | If @var{parameter} is @samp{@@} or @samp{*}, | |
1671 | the substitution operation is applied to each positional | |
1672 | parameter in turn, and the expansion is the resultant list. | |
1673 | If @var{parameter} | |
1674 | is an array variable subscripted with @samp{@@} or @samp{*}, | |
1675 | the substitution operation is applied to each member of the | |
1676 | array in turn, and the expansion is the resultant list. | |
1677 | ||
1678 | @end table | |
1679 | ||
1680 | @node Command Substitution | |
1681 | @subsection Command Substitution | |
1682 | @cindex command substitution | |
1683 | ||
1684 | Command substitution allows the output of a command to replace | |
bb70624e JA |
1685 | the command itself. |
1686 | Command substitution occurs when a command is enclosed as follows: | |
ccc6cda3 JA |
1687 | @example |
1688 | $(@var{command}) | |
1689 | @end example | |
1690 | @noindent | |
1691 | or | |
1692 | @example | |
1693 | `@var{command}` | |
1694 | @end example | |
1695 | ||
1696 | @noindent | |
1697 | Bash performs the expansion by executing @var{command} and | |
1698 | replacing the command substitution with the standard output of the | |
1699 | command, with any trailing newlines deleted. | |
cce855bc JA |
1700 | Embedded newlines are not deleted, but they may be removed during |
1701 | word splitting. | |
1702 | The command substitution @code{$(cat @var{file})} can be | |
1703 | replaced by the equivalent but faster @code{$(< @var{file})}. | |
ccc6cda3 JA |
1704 | |
1705 | When the old-style backquote form of substitution is used, | |
1706 | backslash retains its literal meaning except when followed by | |
cce855bc JA |
1707 | @samp{$}, @samp{`}, or @samp{\}. |
1708 | The first backquote not preceded by a backslash terminates the | |
1709 | command substitution. | |
ccc6cda3 JA |
1710 | When using the @code{$(@var{command})} form, all characters between |
1711 | the parentheses make up the command; none are treated specially. | |
1712 | ||
cce855bc JA |
1713 | Command substitutions may be nested. To nest when using the backquoted |
1714 | form, escape the inner backquotes with backslashes. | |
ccc6cda3 JA |
1715 | |
1716 | If the substitution appears within double quotes, word splitting and | |
1717 | filename expansion are not performed on the results. | |
1718 | ||
cce855bc JA |
1719 | @node Arithmetic Expansion |
1720 | @subsection Arithmetic Expansion | |
1721 | @cindex expansion, arithmetic | |
1722 | @cindex arithmetic expansion | |
1723 | ||
1724 | Arithmetic expansion allows the evaluation of an arithmetic expression | |
1725 | and the substitution of the result. The format for arithmetic expansion is: | |
1726 | ||
1727 | @example | |
1728 | $(( @var{expression} )) | |
1729 | @end example | |
1730 | ||
1731 | The expression is treated as if it were within double quotes, but | |
1732 | a double quote inside the parentheses is not treated specially. | |
1733 | All tokens in the expression undergo parameter expansion, command | |
1734 | substitution, and quote removal. | |
b80f6443 | 1735 | Arithmetic expansions may be nested. |
cce855bc JA |
1736 | |
1737 | The evaluation is performed according to the rules listed below | |
1738 | (@pxref{Shell Arithmetic}). | |
1739 | If the expression is invalid, Bash prints a message indicating | |
1740 | failure to the standard error and no substitution occurs. | |
1741 | ||
ccc6cda3 JA |
1742 | @node Process Substitution |
1743 | @subsection Process Substitution | |
1744 | @cindex process substitution | |
1745 | ||
1746 | Process substitution is supported on systems that support named | |
bb70624e | 1747 | pipes (@sc{fifo}s) or the @file{/dev/fd} method of naming open files. |
ccc6cda3 JA |
1748 | It takes the form of |
1749 | @example | |
1750 | <(@var{list}) | |
1751 | @end example | |
1752 | @noindent | |
1753 | or | |
1754 | @example | |
1755 | >(@var{list}) | |
1756 | @end example | |
1757 | @noindent | |
1758 | The process @var{list} is run with its input or output connected to a | |
bb70624e | 1759 | @sc{fifo} or some file in @file{/dev/fd}. The name of this file is |
ccc6cda3 JA |
1760 | passed as an argument to the current command as the result of the |
1761 | expansion. If the @code{>(@var{list})} form is used, writing to | |
1762 | the file will provide input for @var{list}. If the | |
1763 | @code{<(@var{list})} form is used, the file passed as an | |
1764 | argument should be read to obtain the output of @var{list}. | |
bb70624e JA |
1765 | Note that no space may appear between the @code{<} or @code{>} |
1766 | and the left parenthesis, otherwise the construct would be interpreted | |
1767 | as a redirection. | |
ccc6cda3 | 1768 | |
cce855bc JA |
1769 | When available, process substitution is performed simultaneously with |
1770 | parameter and variable expansion, command substitution, and arithmetic | |
1771 | expansion. | |
ccc6cda3 JA |
1772 | |
1773 | @node Word Splitting | |
1774 | @subsection Word Splitting | |
1775 | @cindex word splitting | |
1776 | ||
1777 | The shell scans the results of parameter expansion, command substitution, | |
1778 | and arithmetic expansion that did not occur within double quotes for | |
1779 | word splitting. | |
1780 | ||
28ef6c31 | 1781 | The shell treats each character of @env{$IFS} |
ccc6cda3 JA |
1782 | as a delimiter, and splits the results of the other |
1783 | expansions into words on these characters. If | |
28ef6c31 JA |
1784 | @env{IFS} is unset, or its value is exactly @code{<space><tab><newline>}, |
1785 | the default, then any sequence of @env{IFS} | |
1786 | characters serves to delimit words. If @env{IFS} | |
ccc6cda3 JA |
1787 | has a value other than the default, then sequences of |
1788 | the whitespace characters @code{space} and @code{tab} | |
1789 | are ignored at the beginning and end of the | |
1790 | word, as long as the whitespace character is in the | |
28ef6c31 JA |
1791 | value of @env{IFS} (an @env{IFS} whitespace character). |
1792 | Any character in @env{IFS} that is not @env{IFS} | |
1793 | whitespace, along with any adjacent @env{IFS} | |
1794 | whitespace characters, delimits a field. A sequence of @env{IFS} | |
ccc6cda3 | 1795 | whitespace characters is also treated as a delimiter. |
28ef6c31 | 1796 | If the value of @env{IFS} is null, no word splitting occurs. |
ccc6cda3 JA |
1797 | |
1798 | Explicit null arguments (@code{""} or @code{''}) are retained. | |
1799 | Unquoted implicit null arguments, resulting from the expansion of | |
bb70624e | 1800 | parameters that have no values, are removed. |
ccc6cda3 JA |
1801 | If a parameter with no value is expanded within double quotes, a |
1802 | null argument results and is retained. | |
1803 | ||
1804 | Note that if no expansion occurs, no splitting | |
1805 | is performed. | |
1806 | ||
1807 | @node Filename Expansion | |
1808 | @subsection Filename Expansion | |
cce855bc JA |
1809 | @menu |
1810 | * Pattern Matching:: How the shell matches patterns. | |
1811 | @end menu | |
ccc6cda3 JA |
1812 | @cindex expansion, filename |
1813 | @cindex expansion, pathname | |
1814 | @cindex filename expansion | |
1815 | @cindex pathname expansion | |
1816 | ||
28ef6c31 | 1817 | After word splitting, unless the @option{-f} option has been set |
cce855bc | 1818 | (@pxref{The Set Builtin}), Bash scans each word for the characters |
bb70624e | 1819 | @samp{*}, @samp{?}, and @samp{[}. |
ccc6cda3 JA |
1820 | If one of these characters appears, then the word is |
1821 | regarded as a @var{pattern}, | |
1822 | and replaced with an alphabetically sorted list of | |
1823 | file names matching the pattern. If no matching file names are found, | |
1824 | and the shell option @code{nullglob} is disabled, the word is left | |
cce855bc JA |
1825 | unchanged. |
1826 | If the @code{nullglob} option is set, and no matches are found, the word | |
1827 | is removed. | |
b80f6443 JA |
1828 | If the @code{failglob} shell option is set, and no matches are found, |
1829 | an error message is printed and the command is not executed. | |
cce855bc JA |
1830 | If the shell option @code{nocaseglob} is enabled, the match is performed |
1831 | without regard to the case of alphabetic characters. | |
1832 | ||
1833 | When a pattern is used for filename generation, the character @samp{.} | |
ccc6cda3 | 1834 | at the start of a filename or immediately following a slash |
cce855bc JA |
1835 | must be matched explicitly, unless the shell option @code{dotglob} is set. |
1836 | When matching a file name, the slash character must always be | |
1837 | matched explicitly. | |
ccc6cda3 | 1838 | In other cases, the @samp{.} character is not treated specially. |
cce855bc | 1839 | |
ccc6cda3 | 1840 | See the description of @code{shopt} in @ref{Bash Builtins}, |
cce855bc | 1841 | for a description of the @code{nocaseglob}, @code{nullglob}, |
b80f6443 | 1842 | @code{failglob}, and @code{dotglob} options. |
ccc6cda3 | 1843 | |
28ef6c31 | 1844 | The @env{GLOBIGNORE} |
ccc6cda3 | 1845 | shell variable may be used to restrict the set of filenames matching a |
28ef6c31 | 1846 | pattern. If @env{GLOBIGNORE} |
ccc6cda3 | 1847 | is set, each matching filename that also matches one of the patterns in |
28ef6c31 | 1848 | @env{GLOBIGNORE} is removed from the list of matches. The filenames |
ccc6cda3 | 1849 | @file{.} and @file{..} |
b80f6443 JA |
1850 | are always ignored when @env{GLOBIGNORE} |
1851 | is set and not null. | |
1852 | However, setting @env{GLOBIGNORE} to a non-null value has the effect of | |
ccc6cda3 JA |
1853 | enabling the @code{dotglob} |
1854 | shell option, so all other filenames beginning with a | |
1855 | @samp{.} will match. | |
1856 | To get the old behavior of ignoring filenames beginning with a | |
28ef6c31 JA |
1857 | @samp{.}, make @samp{.*} one of the patterns in @env{GLOBIGNORE}. |
1858 | The @code{dotglob} option is disabled when @env{GLOBIGNORE} | |
ccc6cda3 JA |
1859 | is unset. |
1860 | ||
cce855bc JA |
1861 | @node Pattern Matching |
1862 | @subsubsection Pattern Matching | |
1863 | @cindex pattern matching | |
1864 | @cindex matching, pattern | |
1865 | ||
1866 | Any character that appears in a pattern, other than the special pattern | |
b80f6443 JA |
1867 | characters described below, matches itself. |
1868 | The @sc{nul} character may not occur in a pattern. | |
1869 | A backslash escapes the following character; the | |
1870 | escaping backslash is discarded when matching. | |
1871 | The special pattern characters must be quoted if they are to be matched | |
1872 | literally. | |
cce855bc | 1873 | |
ccc6cda3 JA |
1874 | The special pattern characters have the following meanings: |
1875 | @table @code | |
1876 | @item * | |
1877 | Matches any string, including the null string. | |
1878 | @item ? | |
1879 | Matches any single character. | |
1880 | @item [@dots{}] | |
1881 | Matches any one of the enclosed characters. A pair of characters | |
28ef6c31 JA |
1882 | separated by a hyphen denotes a @var{range expression}; |
1883 | any character that sorts between those two characters, inclusive, | |
1884 | using the current locale's collating sequence and character set, | |
ccc6cda3 JA |
1885 | is matched. If the first character following the |
1886 | @samp{[} is a @samp{!} or a @samp{^} | |
1887 | then any character not enclosed is matched. A @samp{@minus{}} | |
1888 | may be matched by including it as the first or last character | |
1889 | in the set. A @samp{]} may be matched by including it as the first | |
1890 | character in the set. | |
28ef6c31 JA |
1891 | The sorting order of characters in range expressions is determined by |
1892 | the current locale and the value of the @env{LC_COLLATE} shell variable, | |
1893 | if set. | |
1894 | ||
1895 | For example, in the default C locale, @samp{[a-dx-z]} is equivalent to | |
1896 | @samp{[abcdxyz]}. Many locales sort characters in dictionary order, and in | |
1897 | these locales @samp{[a-dx-z]} is typically not equivalent to @samp{[abcdxyz]}; | |
1898 | it might be equivalent to @samp{[aBbCcDdxXyYz]}, for example. To obtain | |
1899 | the traditional interpretation of ranges in bracket expressions, you can | |
1900 | force the use of the C locale by setting the @env{LC_COLLATE} or | |
1901 | @env{LC_ALL} environment variable to the value @samp{C}. | |
cce855bc JA |
1902 | |
1903 | Within @samp{[} and @samp{]}, @var{character classes} can be specified | |
1904 | using the syntax | |
1905 | @code{[:}@var{class}@code{:]}, where @var{class} is one of the | |
bb70624e | 1906 | following classes defined in the @sc{posix} 1003.2 standard: |
cce855bc JA |
1907 | @example |
1908 | alnum alpha ascii blank cntrl digit graph lower | |
7117c2d2 | 1909 | print punct space upper word xdigit |
cce855bc JA |
1910 | @end example |
1911 | @noindent | |
1912 | A character class matches any character belonging to that class. | |
7117c2d2 JA |
1913 | The @code{word} character class matches letters, digits, and the character |
1914 | @samp{_}. | |
cce855bc JA |
1915 | |
1916 | Within @samp{[} and @samp{]}, an @var{equivalence class} can be | |
1917 | specified using the syntax @code{[=}@var{c}@code{=]}, which | |
1918 | matches all characters with the same collation weight (as defined | |
1919 | by the current locale) as the character @var{c}. | |
1920 | ||
1921 | Within @samp{[} and @samp{]}, the syntax @code{[.}@var{symbol}@code{.]} | |
1922 | matches the collating symbol @var{symbol}. | |
1923 | @end table | |
1924 | ||
1925 | If the @code{extglob} shell option is enabled using the @code{shopt} | |
1926 | builtin, several extended pattern matching operators are recognized. | |
1927 | In the following description, a @var{pattern-list} is a list of one | |
1928 | or more patterns separated by a @samp{|}. | |
1929 | Composite patterns may be formed using one or more of the following | |
1930 | sub-patterns: | |
1931 | ||
1932 | @table @code | |
1933 | @item ?(@var{pattern-list}) | |
1934 | Matches zero or one occurrence of the given patterns. | |
1935 | ||
1936 | @item *(@var{pattern-list}) | |
1937 | Matches zero or more occurrences of the given patterns. | |
1938 | ||
1939 | @item +(@var{pattern-list}) | |
1940 | Matches one or more occurrences of the given patterns. | |
1941 | ||
1942 | @item @@(@var{pattern-list}) | |
95732b49 | 1943 | Matches one of the given patterns. |
cce855bc JA |
1944 | |
1945 | @item !(@var{pattern-list}) | |
1946 | Matches anything except one of the given patterns. | |
ccc6cda3 JA |
1947 | @end table |
1948 | ||
1949 | @node Quote Removal | |
1950 | @subsection Quote Removal | |
1951 | ||
1952 | After the preceding expansions, all unquoted occurrences of the | |
1953 | characters @samp{\}, @samp{'}, and @samp{"} that did not | |
1954 | result from one of the above expansions are removed. | |
1955 | ||
1956 | @node Redirections | |
1957 | @section Redirections | |
1958 | @cindex redirection | |
1959 | ||
1960 | Before a command is executed, its input and output | |
1961 | may be @var{redirected} | |
1962 | using a special notation interpreted by the shell. | |
1963 | Redirection may also be used to open and close files for the | |
1964 | current shell execution environment. The following redirection | |
1965 | operators may precede or appear anywhere within a | |
1966 | simple command or may follow a command. | |
1967 | Redirections are processed in the order they appear, from | |
1968 | left to right. | |
1969 | ||
1970 | In the following descriptions, if the file descriptor number is | |
1971 | omitted, and the first character of the redirection operator is | |
1972 | @samp{<}, the redirection refers to the standard input (file | |
1973 | descriptor 0). If the first character of the redirection operator | |
1974 | is @samp{>}, the redirection refers to the standard output (file | |
1975 | descriptor 1). | |
1976 | ||
cce855bc JA |
1977 | The word following the redirection operator in the following |
1978 | descriptions, unless otherwise noted, is subjected to brace expansion, | |
1979 | tilde expansion, parameter expansion, command substitution, arithmetic | |
bb70624e | 1980 | expansion, quote removal, filename expansion, and word splitting. |
cce855bc | 1981 | If it expands to more than one word, Bash reports an error. |
ccc6cda3 JA |
1982 | |
1983 | Note that the order of redirections is significant. For example, | |
1984 | the command | |
1985 | @example | |
1986 | ls > @var{dirlist} 2>&1 | |
1987 | @end example | |
1988 | @noindent | |
bb70624e JA |
1989 | directs both standard output (file descriptor 1) and standard error |
1990 | (file descriptor 2) to the file @var{dirlist}, while the command | |
ccc6cda3 JA |
1991 | @example |
1992 | ls 2>&1 > @var{dirlist} | |
1993 | @end example | |
1994 | @noindent | |
1995 | directs only the standard output to file @var{dirlist}, | |
1996 | because the standard error was duplicated as standard output | |
1997 | before the standard output was redirected to @var{dirlist}. | |
1998 | ||
bb70624e JA |
1999 | Bash handles several filenames specially when they are used in |
2000 | redirections, as described in the following table: | |
2001 | ||
2002 | @table @code | |
2003 | @item /dev/fd/@var{fd} | |
2004 | If @var{fd} is a valid integer, file descriptor @var{fd} is duplicated. | |
2005 | ||
2006 | @item /dev/stdin | |
2007 | File descriptor 0 is duplicated. | |
2008 | ||
2009 | @item /dev/stdout | |
2010 | File descriptor 1 is duplicated. | |
2011 | ||
2012 | @item /dev/stderr | |
2013 | File descriptor 2 is duplicated. | |
2014 | ||
2015 | @item /dev/tcp/@var{host}/@var{port} | |
2016 | If @var{host} is a valid hostname or Internet address, and @var{port} | |
f73dda09 JA |
2017 | is an integer port number or service name, Bash attempts to open a TCP |
2018 | connection to the corresponding socket. | |
bb70624e JA |
2019 | |
2020 | @item /dev/udp/@var{host}/@var{port} | |
2021 | If @var{host} is a valid hostname or Internet address, and @var{port} | |
f73dda09 JA |
2022 | is an integer port number or service name, Bash attempts to open a UDP |
2023 | connection to the corresponding socket. | |
bb70624e JA |
2024 | |
2025 | @end table | |
2026 | ||
cce855bc JA |
2027 | A failure to open or create a file causes the redirection to fail. |
2028 | ||
95732b49 JA |
2029 | Redirections using file descriptors greater than 9 should be used with |
2030 | care, as they may conflict with file descriptors the shell uses | |
2031 | internally. | |
2032 | ||
ccc6cda3 JA |
2033 | @subsection Redirecting Input |
2034 | Redirection of input causes the file whose name results from | |
2035 | the expansion of @var{word} | |
2036 | to be opened for reading on file descriptor @code{n}, | |
2037 | or the standard input (file descriptor 0) if @code{n} | |
2038 | is not specified. | |
2039 | ||
2040 | The general format for redirecting input is: | |
2041 | @example | |
7117c2d2 | 2042 | [@var{n}]<@var{word} |
ccc6cda3 JA |
2043 | @end example |
2044 | ||
2045 | @subsection Redirecting Output | |
2046 | Redirection of output causes the file whose name results from | |
2047 | the expansion of @var{word} | |
7117c2d2 JA |
2048 | to be opened for writing on file descriptor @var{n}, |
2049 | or the standard output (file descriptor 1) if @var{n} | |
ccc6cda3 JA |
2050 | is not specified. If the file does not exist it is created; |
2051 | if it does exist it is truncated to zero size. | |
2052 | ||
2053 | The general format for redirecting output is: | |
2054 | @example | |
7117c2d2 | 2055 | [@var{n}]>[|]@var{word} |
ccc6cda3 JA |
2056 | @end example |
2057 | ||
cce855bc JA |
2058 | If the redirection operator is @samp{>}, and the @code{noclobber} |
2059 | option to the @code{set} builtin has been enabled, the redirection | |
bb70624e | 2060 | will fail if the file whose name results from the expansion of |
cce855bc JA |
2061 | @var{word} exists and is a regular file. |
2062 | If the redirection operator is @samp{>|}, or the redirection operator is | |
2063 | @samp{>} and the @code{noclobber} option is not enabled, the redirection | |
2064 | is attempted even if the file named by @var{word} exists. | |
ccc6cda3 JA |
2065 | |
2066 | @subsection Appending Redirected Output | |
2067 | Redirection of output in this fashion | |
2068 | causes the file whose name results from | |
2069 | the expansion of @var{word} | |
7117c2d2 JA |
2070 | to be opened for appending on file descriptor @var{n}, |
2071 | or the standard output (file descriptor 1) if @var{n} | |
ccc6cda3 JA |
2072 | is not specified. If the file does not exist it is created. |
2073 | ||
2074 | The general format for appending output is: | |
2075 | @example | |
7117c2d2 | 2076 | [@var{n}]>>@var{word} |
ccc6cda3 JA |
2077 | @end example |
2078 | ||
2079 | @subsection Redirecting Standard Output and Standard Error | |
2080 | Bash allows both the | |
2081 | standard output (file descriptor 1) and | |
2082 | the standard error output (file descriptor 2) | |
2083 | to be redirected to the file whose name is the | |
2084 | expansion of @var{word} with this construct. | |
2085 | ||
2086 | There are two formats for redirecting standard output and | |
2087 | standard error: | |
2088 | @example | |
2089 | &>@var{word} | |
2090 | @end example | |
2091 | @noindent | |
2092 | and | |
2093 | @example | |
2094 | >&@var{word} | |
2095 | @end example | |
2096 | @noindent | |
2097 | Of the two forms, the first is preferred. | |
2098 | This is semantically equivalent to | |
2099 | @example | |
2100 | >@var{word} 2>&1 | |
2101 | @end example | |
2102 | ||
2103 | @subsection Here Documents | |
2104 | This type of redirection instructs the shell to read input from the | |
2105 | current source until a line containing only @var{word} | |
2106 | (with no trailing blanks) is seen. All of | |
2107 | the lines read up to that point are then used as the standard | |
2108 | input for a command. | |
2109 | ||
7117c2d2 | 2110 | The format of here-documents is: |
ccc6cda3 JA |
2111 | @example |
2112 | <<[@minus{}]@var{word} | |
2113 | @var{here-document} | |
2114 | @var{delimiter} | |
2115 | @end example | |
2116 | ||
bb70624e JA |
2117 | No parameter expansion, command substitution, arithmetic expansion, |
2118 | or filename expansion is performed on | |
ccc6cda3 JA |
2119 | @var{word}. If any characters in @var{word} are quoted, the |
2120 | @var{delimiter} is the result of quote removal on @var{word}, | |
cce855bc JA |
2121 | and the lines in the here-document are not expanded. |
2122 | If @var{word} is unquoted, | |
ccc6cda3 JA |
2123 | all lines of the here-document are subjected to parameter expansion, |
2124 | command substitution, and arithmetic expansion. In the latter | |
bb70624e | 2125 | case, the character sequence @code{\newline} is ignored, and @samp{\} |
ccc6cda3 JA |
2126 | must be used to quote the characters |
2127 | @samp{\}, @samp{$}, and @samp{`}. | |
2128 | ||
2129 | If the redirection operator is @samp{<<-}, | |
2130 | then all leading tab characters are stripped from input lines and the | |
2131 | line containing @var{delimiter}. | |
2132 | This allows here-documents within shell scripts to be indented in a | |
2133 | natural fashion. | |
2134 | ||
7117c2d2 JA |
2135 | @subsection Here Strings |
2136 | A variant of here documents, the format is: | |
2137 | @example | |
2138 | <<< @var{word} | |
2139 | @end example | |
2140 | ||
2141 | The @var{word} is expanded and supplied to the command on its standard | |
2142 | input. | |
2143 | ||
ccc6cda3 JA |
2144 | @subsection Duplicating File Descriptors |
2145 | The redirection operator | |
2146 | @example | |
7117c2d2 | 2147 | [@var{n}]<&@var{word} |
ccc6cda3 JA |
2148 | @end example |
2149 | @noindent | |
2150 | is used to duplicate input file descriptors. | |
2151 | If @var{word} | |
7117c2d2 | 2152 | expands to one or more digits, the file descriptor denoted by @var{n} |
cce855bc JA |
2153 | is made to be a copy of that file descriptor. |
2154 | If the digits in @var{word} do not specify a file descriptor open for | |
2155 | input, a redirection error occurs. | |
2156 | If @var{word} | |
7117c2d2 JA |
2157 | evaluates to @samp{-}, file descriptor @var{n} is closed. If |
2158 | @var{n} is not specified, the standard input (file descriptor 0) is used. | |
ccc6cda3 JA |
2159 | |
2160 | The operator | |
2161 | @example | |
7117c2d2 | 2162 | [@var{n}]>&@var{word} |
ccc6cda3 JA |
2163 | @end example |
2164 | @noindent | |
2165 | is used similarly to duplicate output file descriptors. If | |
7117c2d2 | 2166 | @var{n} is not specified, the standard output (file descriptor 1) is used. |
cce855bc JA |
2167 | If the digits in @var{word} do not specify a file descriptor open for |
2168 | output, a redirection error occurs. | |
7117c2d2 | 2169 | As a special case, if @var{n} is omitted, and @var{word} does not |
ccc6cda3 JA |
2170 | expand to one or more digits, the standard output and standard |
2171 | error are redirected as described previously. | |
2172 | ||
7117c2d2 JA |
2173 | @subsection Moving File Descriptors |
2174 | The redirection operator | |
2175 | @example | |
2176 | [@var{n}]<&@var{digit}- | |
2177 | @end example | |
2178 | @noindent | |
2179 | moves the file descriptor @var{digit} to file descriptor @var{n}, | |
2180 | or the standard input (file descriptor 0) if @var{n} is not specified. | |
2181 | @var{digit} is closed after being duplicated to @var{n}. | |
2182 | ||
2183 | Similarly, the redirection operator | |
2184 | @example | |
2185 | [@var{n}]>&@var{digit}- | |
2186 | @end example | |
2187 | @noindent | |
2188 | moves the file descriptor @var{digit} to file descriptor @var{n}, | |
2189 | or the standard output (file descriptor 1) if @var{n} is not specified. | |
2190 | ||
ccc6cda3 JA |
2191 | @subsection Opening File Descriptors for Reading and Writing |
2192 | The redirection operator | |
2193 | @example | |
7117c2d2 | 2194 | [@var{n}]<>@var{word} |
ccc6cda3 JA |
2195 | @end example |
2196 | @noindent | |
2197 | causes the file whose name is the expansion of @var{word} | |
2198 | to be opened for both reading and writing on file descriptor | |
7117c2d2 | 2199 | @var{n}, or on file descriptor 0 if @var{n} |
ccc6cda3 JA |
2200 | is not specified. If the file does not exist, it is created. |
2201 | ||
2202 | @node Executing Commands | |
2203 | @section Executing Commands | |
2204 | ||
2205 | @menu | |
cce855bc JA |
2206 | * Simple Command Expansion:: How Bash expands simple commands before |
2207 | executing them. | |
ccc6cda3 | 2208 | * Command Search and Execution:: How Bash finds commands and runs them. |
cce855bc JA |
2209 | * Command Execution Environment:: The environment in which Bash |
2210 | executes commands that are not | |
2211 | shell builtins. | |
ccc6cda3 | 2212 | * Environment:: The environment given to a command. |
ccc6cda3 JA |
2213 | * Exit Status:: The status returned by commands and how Bash |
2214 | interprets it. | |
ccc6cda3 JA |
2215 | * Signals:: What happens when Bash or a command it runs |
2216 | receives a signal. | |
2217 | @end menu | |
2218 | ||
cce855bc JA |
2219 | @node Simple Command Expansion |
2220 | @subsection Simple Command Expansion | |
2221 | @cindex command expansion | |
2222 | ||
2223 | When a simple command is executed, the shell performs the following | |
2224 | expansions, assignments, and redirections, from left to right. | |
2225 | ||
2226 | @enumerate | |
2227 | @item | |
2228 | The words that the parser has marked as variable assignments (those | |
2229 | preceding the command name) and redirections are saved for later | |
2230 | processing. | |
2231 | ||
2232 | @item | |
2233 | The words that are not variable assignments or redirections are | |
2234 | expanded (@pxref{Shell Expansions}). | |
2235 | If any words remain after expansion, the first word | |
2236 | is taken to be the name of the command and the remaining words are | |
2237 | the arguments. | |
2238 | ||
2239 | @item | |
2240 | Redirections are performed as described above (@pxref{Redirections}). | |
2241 | ||
2242 | @item | |
2243 | The text after the @samp{=} in each variable assignment undergoes tilde | |
2244 | expansion, parameter expansion, command substitution, arithmetic expansion, | |
2245 | and quote removal before being assigned to the variable. | |
2246 | @end enumerate | |
2247 | ||
2248 | If no command name results, the variable assignments affect the current | |
2249 | shell environment. Otherwise, the variables are added to the environment | |
2250 | of the executed command and do not affect the current shell environment. | |
2251 | If any of the assignments attempts to assign a value to a readonly variable, | |
2252 | an error occurs, and the command exits with a non-zero status. | |
2253 | ||
2254 | If no command name results, redirections are performed, but do not | |
2255 | affect the current shell environment. A redirection error causes the | |
2256 | command to exit with a non-zero status. | |
2257 | ||
2258 | If there is a command name left after expansion, execution proceeds as | |
2259 | described below. Otherwise, the command exits. If one of the expansions | |
2260 | contained a command substitution, the exit status of the command is | |
2261 | the exit status of the last command substitution performed. If there | |
2262 | were no command substitutions, the command exits with a status of zero. | |
2263 | ||
ccc6cda3 JA |
2264 | @node Command Search and Execution |
2265 | @subsection Command Search and Execution | |
2266 | @cindex command execution | |
2267 | @cindex command search | |
2268 | ||
2269 | After a command has been split into words, if it results in a | |
2270 | simple command and an optional list of arguments, the following | |
2271 | actions are taken. | |
2272 | ||
2273 | @enumerate | |
2274 | @item | |
2275 | If the command name contains no slashes, the shell attempts to | |
2276 | locate it. If there exists a shell function by that name, that | |
bb70624e | 2277 | function is invoked as described in @ref{Shell Functions}. |
ccc6cda3 JA |
2278 | |
2279 | @item | |
2280 | If the name does not match a function, the shell searches for | |
2281 | it in the list of shell builtins. If a match is found, that | |
2282 | builtin is invoked. | |
2283 | ||
2284 | @item | |
2285 | If the name is neither a shell function nor a builtin, | |
2286 | and contains no slashes, Bash searches each element of | |
28ef6c31 | 2287 | @env{$PATH} for a directory containing an executable file |
ccc6cda3 | 2288 | by that name. Bash uses a hash table to remember the full |
28ef6c31 | 2289 | pathnames of executable files to avoid multiple @env{PATH} searches |
cce855bc | 2290 | (see the description of @code{hash} in @ref{Bourne Shell Builtins}). |
28ef6c31 | 2291 | A full search of the directories in @env{$PATH} |
ccc6cda3 JA |
2292 | is performed only if the command is not found in the hash table. |
2293 | If the search is unsuccessful, the shell prints an error | |
cce855bc | 2294 | message and returns an exit status of 127. |
ccc6cda3 JA |
2295 | |
2296 | @item | |
2297 | If the search is successful, or if the command name contains | |
cce855bc JA |
2298 | one or more slashes, the shell executes the named program in |
2299 | a separate execution environment. | |
ccc6cda3 JA |
2300 | Argument 0 is set to the name given, and the remaining arguments |
2301 | to the command are set to the arguments supplied, if any. | |
2302 | ||
2303 | @item | |
2304 | If this execution fails because the file is not in executable | |
cce855bc JA |
2305 | format, and the file is not a directory, it is assumed to be a |
2306 | @var{shell script} and the shell executes it as described in | |
2307 | @ref{Shell Scripts}. | |
2308 | ||
2309 | @item | |
2310 | If the command was not begun asynchronously, the shell waits for | |
2311 | the command to complete and collects its exit status. | |
2312 | ||
ccc6cda3 JA |
2313 | @end enumerate |
2314 | ||
cce855bc JA |
2315 | @node Command Execution Environment |
2316 | @subsection Command Execution Environment | |
2317 | @cindex execution environment | |
2318 | ||
2319 | The shell has an @var{execution environment}, which consists of the | |
2320 | following: | |
2321 | ||
2322 | @itemize @bullet | |
2323 | @item | |
2324 | open files inherited by the shell at invocation, as modified by | |
2325 | redirections supplied to the @code{exec} builtin | |
2326 | ||
2327 | @item | |
2328 | the current working directory as set by @code{cd}, @code{pushd}, or | |
2329 | @code{popd}, or inherited by the shell at invocation | |
2330 | ||
2331 | @item | |
2332 | the file creation mode mask as set by @code{umask} or inherited from | |
2333 | the shell's parent | |
2334 | ||
2335 | @item | |
2336 | current traps set by @code{trap} | |
2337 | ||
2338 | @item | |
2339 | shell parameters that are set by variable assignment or with @code{set} | |
2340 | or inherited from the shell's parent in the environment | |
2341 | ||
2342 | @item | |
2343 | shell functions defined during execution or inherited from the shell's | |
2344 | parent in the environment | |
2345 | ||
2346 | @item | |
2347 | options enabled at invocation (either by default or with command-line | |
2348 | arguments) or by @code{set} | |
2349 | ||
2350 | @item | |
2351 | options enabled by @code{shopt} | |
2352 | ||
2353 | @item | |
2354 | shell aliases defined with @code{alias} (@pxref{Aliases}) | |
2355 | ||
2356 | @item | |
bb70624e | 2357 | various process @sc{id}s, including those of background jobs |
cce855bc | 2358 | (@pxref{Lists}), the value of @code{$$}, and the value of |
28ef6c31 | 2359 | @env{$PPID} |
cce855bc JA |
2360 | |
2361 | @end itemize | |
2362 | ||
2363 | When a simple command other than a builtin or shell function | |
2364 | is to be executed, it | |
2365 | is invoked in a separate execution environment that consists of | |
2366 | the following. Unless otherwise noted, the values are inherited | |
2367 | from the shell. | |
2368 | ||
2369 | @itemize @bullet | |
2370 | @item | |
2371 | the shell's open files, plus any modifications and additions specified | |
2372 | by redirections to the command | |
2373 | ||
2374 | @item | |
2375 | the current working directory | |
2376 | ||
2377 | @item | |
2378 | the file creation mode mask | |
2379 | ||
2380 | @item | |
b80f6443 JA |
2381 | shell variables and functions marked for export, along with variables |
2382 | exported for the command, passed in the environment (@pxref{Environment}) | |
cce855bc JA |
2383 | |
2384 | @item | |
2385 | traps caught by the shell are reset to the values inherited from the | |
2386 | shell's parent, and traps ignored by the shell are ignored | |
2387 | ||
2388 | @end itemize | |
2389 | ||
2390 | A command invoked in this separate environment cannot affect the | |
2391 | shell's execution environment. | |
2392 | ||
b80f6443 JA |
2393 | Command substitution, commands grouped with parentheses, |
2394 | and asynchronous commands are invoked in a | |
cce855bc JA |
2395 | subshell environment that is a duplicate of the shell environment, |
2396 | except that traps caught by the shell are reset to the values | |
2397 | that the shell inherited from its parent at invocation. Builtin | |
2398 | commands that are invoked as part of a pipeline are also executed | |
2399 | in a subshell environment. Changes made to the subshell environment | |
2400 | cannot affect the shell's execution environment. | |
2401 | ||
f73dda09 JA |
2402 | If a command is followed by a @samp{&} and job control is not active, the |
2403 | default standard input for the command is the empty file @file{/dev/null}. | |
2404 | Otherwise, the invoked command inherits the file descriptors of the calling | |
2405 | shell as modified by redirections. | |
2406 | ||
ccc6cda3 JA |
2407 | @node Environment |
2408 | @subsection Environment | |
2409 | @cindex environment | |
2410 | ||
2411 | When a program is invoked it is given an array of strings | |
2412 | called the @var{environment}. | |
2413 | This is a list of name-value pairs, of the form @code{name=value}. | |
2414 | ||
bb70624e JA |
2415 | Bash provides several ways to manipulate the environment. |
2416 | On invocation, the shell scans its own environment and | |
ccc6cda3 JA |
2417 | creates a parameter for each name found, automatically marking |
2418 | it for @var{export} | |
2419 | to child processes. Executed commands inherit the environment. | |
2420 | The @code{export} and @samp{declare -x} | |
2421 | commands allow parameters and functions to be added to and | |
2422 | deleted from the environment. If the value of a parameter | |
2423 | in the environment is modified, the new value becomes part | |
2424 | of the environment, replacing the old. The environment | |
2425 | inherited by any executed command consists of the shell's | |
2426 | initial environment, whose values may be modified in the shell, | |
cce855bc JA |
2427 | less any pairs removed by the @code{unset} and @samp{export -n} |
2428 | commands, plus any additions via the @code{export} and | |
2429 | @samp{declare -x} commands. | |
ccc6cda3 JA |
2430 | |
2431 | The environment for any simple command | |
2432 | or function may be augmented temporarily by prefixing it with | |
2433 | parameter assignments, as described in @ref{Shell Parameters}. | |
2434 | These assignment statements affect only the environment seen | |
2435 | by that command. | |
2436 | ||
28ef6c31 | 2437 | If the @option{-k} option is set (@pxref{The Set Builtin}), then all |
ccc6cda3 JA |
2438 | parameter assignments are placed in the environment for a command, |
2439 | not just those that precede the command name. | |
2440 | ||
2441 | When Bash invokes an external command, the variable @samp{$_} | |
2442 | is set to the full path name of the command and passed to that | |
2443 | command in its environment. | |
2444 | ||
2445 | @node Exit Status | |
2446 | @subsection Exit Status | |
2447 | @cindex exit status | |
2448 | ||
cce855bc | 2449 | For the shell's purposes, a command which exits with a |
ccc6cda3 JA |
2450 | zero exit status has succeeded. |
2451 | A non-zero exit status indicates failure. | |
2452 | This seemingly counter-intuitive scheme is used so there | |
2453 | is one well-defined way to indicate success and a variety of | |
2454 | ways to indicate various failure modes. | |
bb70624e JA |
2455 | When a command terminates on a fatal signal whose number is @var{N}, |
2456 | Bash uses the value 128+@var{N} as the exit status. | |
ccc6cda3 JA |
2457 | |
2458 | If a command is not found, the child process created to | |
2459 | execute it returns a status of 127. If a command is found | |
2460 | but is not executable, the return status is 126. | |
2461 | ||
cce855bc JA |
2462 | If a command fails because of an error during expansion or redirection, |
2463 | the exit status is greater than zero. | |
2464 | ||
ccc6cda3 JA |
2465 | The exit status is used by the Bash conditional commands |
2466 | (@pxref{Conditional Constructs}) and some of the list | |
2467 | constructs (@pxref{Lists}). | |
2468 | ||
2469 | All of the Bash builtins return an exit status of zero if they succeed | |
2470 | and a non-zero status on failure, so they may be used by the | |
2471 | conditional and list constructs. | |
cce855bc | 2472 | All builtins return an exit status of 2 to indicate incorrect usage. |
ccc6cda3 JA |
2473 | |
2474 | @node Signals | |
2475 | @subsection Signals | |
2476 | @cindex signal handling | |
2477 | ||
cce855bc | 2478 | When Bash is interactive, in the absence of any traps, it ignores |
ccc6cda3 JA |
2479 | @code{SIGTERM} (so that @samp{kill 0} does not kill an interactive shell), |
2480 | and @code{SIGINT} | |
2481 | is caught and handled (so that the @code{wait} builtin is interruptible). | |
2482 | When Bash receives a @code{SIGINT}, it breaks out of any executing loops. | |
2483 | In all cases, Bash ignores @code{SIGQUIT}. | |
2484 | If job control is in effect (@pxref{Job Control}), Bash | |
2485 | ignores @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. | |
2486 | ||
b80f6443 | 2487 | Non-builtin commands started by Bash have signal handlers set to the |
cce855bc JA |
2488 | values inherited by the shell from its parent. |
2489 | When job control is not in effect, asynchronous commands | |
b80f6443 JA |
2490 | ignore @code{SIGINT} and @code{SIGQUIT} in addition to these inherited |
2491 | handlers. | |
cce855bc JA |
2492 | Commands run as a result of |
2493 | command substitution ignore the keyboard-generated job control signals | |
ccc6cda3 JA |
2494 | @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. |
2495 | ||
2496 | The shell exits by default upon receipt of a @code{SIGHUP}. | |
f73dda09 JA |
2497 | Before exiting, an interactive shell resends the @code{SIGHUP} to |
2498 | all jobs, running or stopped. | |
cce855bc JA |
2499 | Stopped jobs are sent @code{SIGCONT} to ensure that they receive |
2500 | the @code{SIGHUP}. | |
2501 | To prevent the shell from sending the @code{SIGHUP} signal to a | |
2502 | particular job, it should be removed | |
2503 | from the jobs table with the @code{disown} | |
2504 | builtin (@pxref{Job Control Builtins}) or marked | |
2505 | to not receive @code{SIGHUP} using @code{disown -h}. | |
2506 | ||
2507 | If the @code{huponexit} shell option has been set with @code{shopt} | |
2508 | (@pxref{Bash Builtins}), Bash sends a @code{SIGHUP} to all jobs when | |
2509 | an interactive login shell exits. | |
2510 | ||
b80f6443 JA |
2511 | If Bash is waiting for a command to complete and receives a signal |
2512 | for which a trap has been set, the trap will not be executed until | |
2513 | the command completes. | |
cce855bc JA |
2514 | When Bash is waiting for an asynchronous |
2515 | command via the @code{wait} builtin, the reception of a signal for | |
2516 | which a trap has been set will cause the @code{wait} builtin to return | |
2517 | immediately with an exit status greater than 128, immediately after | |
2518 | which the trap is executed. | |
ccc6cda3 JA |
2519 | |
2520 | @node Shell Scripts | |
2521 | @section Shell Scripts | |
2522 | @cindex shell script | |
2523 | ||
2524 | A shell script is a text file containing shell commands. When such | |
2525 | a file is used as the first non-option argument when invoking Bash, | |
28ef6c31 | 2526 | and neither the @option{-c} nor @option{-s} option is supplied |
ccc6cda3 JA |
2527 | (@pxref{Invoking Bash}), |
2528 | Bash reads and executes commands from the file, then exits. This | |
f73dda09 JA |
2529 | mode of operation creates a non-interactive shell. The shell first |
2530 | searches for the file in the current directory, and looks in the | |
2531 | directories in @env{$PATH} if not found there. | |
2532 | ||
2533 | When Bash runs | |
ccc6cda3 JA |
2534 | a shell script, it sets the special parameter @code{0} to the name |
2535 | of the file, rather than the name of the shell, and the positional | |
2536 | parameters are set to the remaining arguments, if any are given. | |
2537 | If no additional arguments are supplied, the positional parameters | |
2538 | are unset. | |
2539 | ||
2540 | A shell script may be made executable by using the @code{chmod} command | |
2541 | to turn on the execute bit. When Bash finds such a file while | |
28ef6c31 | 2542 | searching the @env{$PATH} for a command, it spawns a subshell to |
ccc6cda3 JA |
2543 | execute it. In other words, executing |
2544 | @example | |
2545 | filename @var{arguments} | |
2546 | @end example | |
2547 | @noindent | |
2548 | is equivalent to executing | |
2549 | @example | |
2550 | bash filename @var{arguments} | |
2551 | @end example | |
2552 | ||
2553 | @noindent | |
2554 | if @code{filename} is an executable shell script. | |
2555 | This subshell reinitializes itself, so that the effect is as if a | |
cce855bc JA |
2556 | new shell had been invoked to interpret the script, with the |
2557 | exception that the locations of commands remembered by the parent | |
2558 | (see the description of @code{hash} in @ref{Bourne Shell Builtins}) | |
2559 | are retained by the child. | |
ccc6cda3 | 2560 | |
bb70624e | 2561 | Most versions of Unix make this a part of the operating system's command |
ccc6cda3 JA |
2562 | execution mechanism. If the first line of a script begins with |
2563 | the two characters @samp{#!}, the remainder of the line specifies | |
bb70624e JA |
2564 | an interpreter for the program. |
2565 | Thus, you can specify Bash, @code{awk}, Perl, or some other | |
2566 | interpreter and write the rest of the script file in that language. | |
2567 | ||
2568 | The arguments to the interpreter | |
ccc6cda3 JA |
2569 | consist of a single optional argument following the interpreter |
2570 | name on the first line of the script file, followed by the name of | |
2571 | the script file, followed by the rest of the arguments. Bash | |
2572 | will perform this action on operating systems that do not handle it | |
2573 | themselves. Note that some older versions of Unix limit the interpreter | |
2574 | name and argument to a maximum of 32 characters. | |
2575 | ||
bb70624e JA |
2576 | Bash scripts often begin with @code{#! /bin/bash} (assuming that |
2577 | Bash has been installed in @file{/bin}), since this ensures that | |
2578 | Bash will be used to interpret the script, even if it is executed | |
2579 | under another shell. | |
2580 | ||
2581 | @node Shell Builtin Commands | |
2582 | @chapter Shell Builtin Commands | |
ccc6cda3 JA |
2583 | |
2584 | @menu | |
2585 | * Bourne Shell Builtins:: Builtin commands inherited from the Bourne | |
2586 | Shell. | |
bb70624e JA |
2587 | * Bash Builtins:: Table of builtins specific to Bash. |
2588 | * The Set Builtin:: This builtin is so overloaded it | |
2589 | deserves its own section. | |
2590 | * Special Builtins:: Builtin commands classified specially by | |
2591 | POSIX.2. | |
ccc6cda3 JA |
2592 | @end menu |
2593 | ||
bb70624e JA |
2594 | Builtin commands are contained within the shell itself. |
2595 | When the name of a builtin command is used as the first word of | |
2596 | a simple command (@pxref{Simple Commands}), the shell executes | |
2597 | the command directly, without invoking another program. | |
2598 | Builtin commands are necessary to implement functionality impossible | |
2599 | or inconvenient to obtain with separate utilities. | |
2600 | ||
2601 | This section briefly the builtins which Bash inherits from | |
2602 | the Bourne Shell, as well as the builtin commands which are unique | |
2603 | to or have been extended in Bash. | |
2604 | ||
2605 | Several builtin commands are described in other chapters: builtin | |
2606 | commands which provide the Bash interface to the job control | |
2607 | facilities (@pxref{Job Control Builtins}), the directory stack | |
2608 | (@pxref{Directory Stack Builtins}), the command history | |
2609 | (@pxref{Bash History Builtins}), and the programmable completion | |
2610 | facilities (@pxref{Programmable Completion Builtins}). | |
2611 | ||
2612 | Many of the builtins have been extended by @sc{posix} or Bash. | |
ccc6cda3 | 2613 | |
b80f6443 JA |
2614 | Unless otherwise noted, each builtin command documented as accepting |
2615 | options preceded by @samp{-} accepts @samp{--} | |
2616 | to signify the end of the options. | |
95732b49 JA |
2617 | For example, the @code{:}, @code{true}, @code{false}, and @code{test} |
2618 | builtins do not accept options. | |
b80f6443 | 2619 | |
ccc6cda3 JA |
2620 | @node Bourne Shell Builtins |
2621 | @section Bourne Shell Builtins | |
2622 | ||
bb70624e JA |
2623 | The following shell builtin commands are inherited from the Bourne Shell. |
2624 | These commands are implemented as specified by the @sc{posix} 1003.2 standard. | |
ccc6cda3 JA |
2625 | |
2626 | @table @code | |
bb70624e | 2627 | @item : @r{(a colon)} |
ccc6cda3 JA |
2628 | @btindex : |
2629 | @example | |
2630 | : [@var{arguments}] | |
2631 | @end example | |
2632 | Do nothing beyond expanding @var{arguments} and performing redirections. | |
cce855bc | 2633 | The return status is zero. |
ccc6cda3 | 2634 | |
bb70624e | 2635 | @item . @r{(a period)} |
ccc6cda3 JA |
2636 | @btindex . |
2637 | @example | |
b72432fd | 2638 | . @var{filename} [@var{arguments}] |
ccc6cda3 JA |
2639 | @end example |
2640 | Read and execute commands from the @var{filename} argument in the | |
cce855bc | 2641 | current shell context. If @var{filename} does not contain a slash, |
28ef6c31 JA |
2642 | the @env{PATH} variable is used to find @var{filename}. |
2643 | When Bash is not in @sc{posix} mode, the current directory is searched | |
2644 | if @var{filename} is not found in @env{$PATH}. | |
b72432fd JA |
2645 | If any @var{arguments} are supplied, they become the positional |
2646 | parameters when @var{filename} is executed. Otherwise the positional | |
2647 | parameters are unchanged. | |
cce855bc JA |
2648 | The return status is the exit status of the last command executed, or |
2649 | zero if no commands are executed. If @var{filename} is not found, or | |
2650 | cannot be read, the return status is non-zero. | |
bb70624e | 2651 | This builtin is equivalent to @code{source}. |
ccc6cda3 JA |
2652 | |
2653 | @item break | |
2654 | @btindex break | |
2655 | @example | |
2656 | break [@var{n}] | |
2657 | @end example | |
2658 | Exit from a @code{for}, @code{while}, @code{until}, or @code{select} loop. | |
2659 | If @var{n} is supplied, the @var{n}th enclosing loop is exited. | |
cce855bc JA |
2660 | @var{n} must be greater than or equal to 1. |
2661 | The return status is zero unless @var{n} is not greater than or equal to 1. | |
ccc6cda3 JA |
2662 | |
2663 | @item cd | |
2664 | @btindex cd | |
2665 | @example | |
7117c2d2 | 2666 | cd [-L|-P] [@var{directory}] |
ccc6cda3 | 2667 | @end example |
b80f6443 JA |
2668 | Change the current working directory to @var{directory}. |
2669 | If @var{directory} is not given, the value of the @env{HOME} shell | |
2670 | variable is used. | |
2671 | If the shell variable @env{CDPATH} exists, it is used as a search path. | |
2672 | If @var{directory} begins with a slash, @env{CDPATH} is not used. | |
2673 | ||
2674 | The @option{-P} option means to not follow symbolic links; symbolic | |
2675 | links are followed by default or with the @option{-L} option. | |
28ef6c31 | 2676 | If @var{directory} is @samp{-}, it is equivalent to @env{$OLDPWD}. |
b80f6443 JA |
2677 | |
2678 | If a non-empty directory name from @env{CDPATH} is used, or if | |
2679 | @samp{-} is the first argument, and the directory change is | |
2680 | successful, the absolute pathname of the new working directory is | |
2681 | written to the standard output. | |
2682 | ||
cce855bc JA |
2683 | The return status is zero if the directory is successfully changed, |
2684 | non-zero otherwise. | |
ccc6cda3 JA |
2685 | |
2686 | @item continue | |
2687 | @btindex continue | |
2688 | @example | |
2689 | continue [@var{n}] | |
2690 | @end example | |
2691 | Resume the next iteration of an enclosing @code{for}, @code{while}, | |
2692 | @code{until}, or @code{select} loop. | |
cce855bc JA |
2693 | If @var{n} is supplied, the execution of the @var{n}th enclosing loop |
2694 | is resumed. | |
2695 | @var{n} must be greater than or equal to 1. | |
2696 | The return status is zero unless @var{n} is not greater than or equal to 1. | |
ccc6cda3 JA |
2697 | |
2698 | @item eval | |
2699 | @btindex eval | |
2700 | @example | |
2701 | eval [@var{arguments}] | |
2702 | @end example | |
cce855bc JA |
2703 | The arguments are concatenated together into a single command, which is |
2704 | then read and executed, and its exit status returned as the exit status | |
2705 | of @code{eval}. | |
2706 | If there are no arguments or only empty arguments, the return status is | |
2707 | zero. | |
ccc6cda3 JA |
2708 | |
2709 | @item exec | |
2710 | @btindex exec | |
2711 | @example | |
cce855bc | 2712 | exec [-cl] [-a @var{name}] [@var{command} [@var{arguments}]] |
ccc6cda3 JA |
2713 | @end example |
2714 | If @var{command} | |
cce855bc | 2715 | is supplied, it replaces the shell without creating a new process. |
28ef6c31 | 2716 | If the @option{-l} option is supplied, the shell places a dash at the |
bb70624e | 2717 | beginning of the zeroth arg passed to @var{command}. |
ccc6cda3 | 2718 | This is what the @code{login} program does. |
28ef6c31 | 2719 | The @option{-c} option causes @var{command} to be executed with an empty |
cce855bc | 2720 | environment. |
28ef6c31 | 2721 | If @option{-a} is supplied, the shell passes @var{name} as the zeroth |
cce855bc | 2722 | argument to @var{command}. |
ccc6cda3 | 2723 | If no @var{command} is specified, redirections may be used to affect |
cce855bc JA |
2724 | the current shell environment. If there are no redirection errors, the |
2725 | return status is zero; otherwise the return status is non-zero. | |
ccc6cda3 JA |
2726 | |
2727 | @item exit | |
2728 | @btindex exit | |
2729 | @example | |
2730 | exit [@var{n}] | |
2731 | @end example | |
2732 | Exit the shell, returning a status of @var{n} to the shell's parent. | |
bb70624e | 2733 | If @var{n} is omitted, the exit status is that of the last command executed. |
cce855bc | 2734 | Any trap on @code{EXIT} is executed before the shell terminates. |
ccc6cda3 JA |
2735 | |
2736 | @item export | |
2737 | @btindex export | |
2738 | @example | |
2739 | export [-fn] [-p] [@var{name}[=@var{value}]] | |
2740 | @end example | |
2741 | Mark each @var{name} to be passed to child processes | |
28ef6c31 | 2742 | in the environment. If the @option{-f} option is supplied, the @var{name}s |
cce855bc | 2743 | refer to shell functions; otherwise the names refer to shell variables. |
28ef6c31 JA |
2744 | The @option{-n} option means to no longer mark each @var{name} for export. |
2745 | If no @var{names} are supplied, or if the @option{-p} option is given, a | |
ccc6cda3 | 2746 | list of exported names is displayed. |
28ef6c31 | 2747 | The @option{-p} option displays output in a form that may be reused as input. |
b80f6443 JA |
2748 | If a variable name is followed by =@var{value}, the value of |
2749 | the variable is set to @var{value}. | |
2750 | ||
cce855bc | 2751 | The return status is zero unless an invalid option is supplied, one of |
28ef6c31 | 2752 | the names is not a valid shell variable name, or @option{-f} is supplied |
cce855bc | 2753 | with a name that is not a shell function. |
ccc6cda3 JA |
2754 | |
2755 | @item getopts | |
2756 | @btindex getopts | |
2757 | @example | |
2758 | getopts @var{optstring} @var{name} [@var{args}] | |
2759 | @end example | |
2760 | @code{getopts} is used by shell scripts to parse positional parameters. | |
bb70624e JA |
2761 | @var{optstring} contains the option characters to be recognized; if a |
2762 | character is followed by a colon, the option is expected to have an | |
ccc6cda3 | 2763 | argument, which should be separated from it by white space. |
bb70624e JA |
2764 | The colon (@samp{:}) and question mark (@samp{?}) may not be |
2765 | used as option characters. | |
ccc6cda3 JA |
2766 | Each time it is invoked, @code{getopts} |
2767 | places the next option in the shell variable @var{name}, initializing | |
2768 | @var{name} if it does not exist, | |
2769 | and the index of the next argument to be processed into the | |
28ef6c31 JA |
2770 | variable @env{OPTIND}. |
2771 | @env{OPTIND} is initialized to 1 each time the shell or a shell script | |
cce855bc JA |
2772 | is invoked. |
2773 | When an option requires an argument, | |
28ef6c31 JA |
2774 | @code{getopts} places that argument into the variable @env{OPTARG}. |
2775 | The shell does not reset @env{OPTIND} automatically; it must be manually | |
cce855bc JA |
2776 | reset between multiple calls to @code{getopts} within the same shell |
2777 | invocation if a new set of parameters is to be used. | |
2778 | ||
2779 | When the end of options is encountered, @code{getopts} exits with a | |
2780 | return value greater than zero. | |
28ef6c31 | 2781 | @env{OPTIND} is set to the index of the first non-option argument, |
cce855bc JA |
2782 | and @code{name} is set to @samp{?}. |
2783 | ||
2784 | @code{getopts} | |
2785 | normally parses the positional parameters, but if more arguments are | |
2786 | given in @var{args}, @code{getopts} parses those instead. | |
ccc6cda3 JA |
2787 | |
2788 | @code{getopts} can report errors in two ways. If the first character of | |
2789 | @var{optstring} is a colon, @var{silent} | |
2790 | error reporting is used. In normal operation diagnostic messages | |
cce855bc | 2791 | are printed when invalid options or missing option arguments are |
ccc6cda3 | 2792 | encountered. |
28ef6c31 | 2793 | If the variable @env{OPTERR} |
cce855bc | 2794 | is set to 0, no error messages will be displayed, even if the first |
ccc6cda3 JA |
2795 | character of @code{optstring} is not a colon. |
2796 | ||
cce855bc | 2797 | If an invalid option is seen, |
ccc6cda3 | 2798 | @code{getopts} places @samp{?} into @var{name} and, if not silent, |
28ef6c31 | 2799 | prints an error message and unsets @env{OPTARG}. |
ccc6cda3 | 2800 | If @code{getopts} is silent, the option character found is placed in |
28ef6c31 | 2801 | @env{OPTARG} and no diagnostic message is printed. |
ccc6cda3 JA |
2802 | |
2803 | If a required argument is not found, and @code{getopts} | |
2804 | is not silent, a question mark (@samp{?}) is placed in @var{name}, | |
2805 | @code{OPTARG} is unset, and a diagnostic message is printed. | |
2806 | If @code{getopts} is silent, then a colon (@samp{:}) is placed in | |
28ef6c31 | 2807 | @var{name} and @env{OPTARG} is set to the option character found. |
ccc6cda3 | 2808 | |
ccc6cda3 JA |
2809 | @item hash |
2810 | @btindex hash | |
2811 | @example | |
7117c2d2 | 2812 | hash [-'r] [-p @var{filename}] [-dt] [@var{name}] |
ccc6cda3 | 2813 | @end example |
cce855bc JA |
2814 | Remember the full pathnames of commands specified as @var{name} arguments, |
2815 | so they need not be searched for on subsequent invocations. | |
2816 | The commands are found by searching through the directories listed in | |
28ef6c31 JA |
2817 | @env{$PATH}. |
2818 | The @option{-p} option inhibits the path search, and @var{filename} is | |
cce855bc | 2819 | used as the location of @var{name}. |
28ef6c31 | 2820 | The @option{-r} option causes the shell to forget all remembered locations. |
7117c2d2 JA |
2821 | The @option{-d} option causes the shell to forget the remembered location |
2822 | of each @var{name}. | |
f73dda09 JA |
2823 | If the @option{-t} option is supplied, the full pathname to which each |
2824 | @var{name} corresponds is printed. If multiple @var{name} arguments are | |
2825 | supplied with @option{-t} the @var{name} is printed before the hashed | |
2826 | full pathname. | |
7117c2d2 JA |
2827 | The @option{-l} option causes output to be displayed in a format |
2828 | that may be reused as input. | |
2829 | If no arguments are given, or if only @option{-l} is supplied, | |
2830 | information about remembered commands is printed. | |
cce855bc JA |
2831 | The return status is zero unless a @var{name} is not found or an invalid |
2832 | option is supplied. | |
ccc6cda3 JA |
2833 | |
2834 | @item pwd | |
2835 | @btindex pwd | |
2836 | @example | |
2837 | pwd [-LP] | |
2838 | @end example | |
bb70624e | 2839 | Print the absolute pathname of the current working directory. |
28ef6c31 | 2840 | If the @option{-P} option is supplied, the pathname printed will not |
cce855bc | 2841 | contain symbolic links. |
28ef6c31 | 2842 | If the @option{-L} option is supplied, the pathname printed may contain |
cce855bc JA |
2843 | symbolic links. |
2844 | The return status is zero unless an error is encountered while | |
2845 | determining the name of the current directory or an invalid option | |
2846 | is supplied. | |
ccc6cda3 JA |
2847 | |
2848 | @item readonly | |
2849 | @btindex readonly | |
2850 | @example | |
b80f6443 | 2851 | readonly [-apf] [@var{name}[=@var{value}]] @dots{} |
ccc6cda3 | 2852 | @end example |
cce855bc JA |
2853 | Mark each @var{name} as readonly. |
2854 | The values of these names may not be changed by subsequent assignment. | |
28ef6c31 | 2855 | If the @option{-f} option is supplied, each @var{name} refers to a shell |
cce855bc | 2856 | function. |
28ef6c31 JA |
2857 | The @option{-a} option means each @var{name} refers to an array variable. |
2858 | If no @var{name} arguments are given, or if the @option{-p} | |
ccc6cda3 | 2859 | option is supplied, a list of all readonly names is printed. |
28ef6c31 | 2860 | The @option{-p} option causes output to be displayed in a format that |
cce855bc | 2861 | may be reused as input. |
b80f6443 JA |
2862 | If a variable name is followed by =@var{value}, the value of |
2863 | the variable is set to @var{value}. | |
cce855bc JA |
2864 | The return status is zero unless an invalid option is supplied, one of |
2865 | the @var{name} arguments is not a valid shell variable or function name, | |
28ef6c31 | 2866 | or the @option{-f} option is supplied with a name that is not a shell function. |
ccc6cda3 JA |
2867 | |
2868 | @item return | |
2869 | @btindex return | |
2870 | @example | |
2871 | return [@var{n}] | |
2872 | @end example | |
cce855bc | 2873 | Cause a shell function to exit with the return value @var{n}. |
bb70624e JA |
2874 | If @var{n} is not supplied, the return value is the exit status of the |
2875 | last command executed in the function. | |
cce855bc | 2876 | This may also be used to terminate execution of a script being executed |
bb70624e JA |
2877 | with the @code{.} (or @code{source}) builtin, returning either @var{n} or |
2878 | the exit status of the last command executed within the script as the exit | |
2879 | status of the script. | |
b80f6443 JA |
2880 | Any command associated with the @code{RETURN} trap is executed |
2881 | before execution resumes after the function or script. | |
f73dda09 | 2882 | The return status is non-zero if @code{return} is used outside a function |
bb70624e | 2883 | and not during the execution of a script by @code{.} or @code{source}. |
ccc6cda3 JA |
2884 | |
2885 | @item shift | |
2886 | @btindex shift | |
2887 | @example | |
2888 | shift [@var{n}] | |
2889 | @end example | |
cce855bc JA |
2890 | Shift the positional parameters to the left by @var{n}. |
2891 | The positional parameters from @var{n}+1 @dots{} @code{$#} are | |
2892 | renamed to @code{$1} @dots{} @code{$#}-@var{n}+1. | |
2893 | Parameters represented by the numbers @code{$#} to @var{n}+1 are unset. | |
2894 | @var{n} must be a non-negative number less than or equal to @code{$#}. | |
2895 | If @var{n} is zero or greater than @code{$#}, the positional parameters | |
2896 | are not changed. | |
bb70624e | 2897 | If @var{n} is not supplied, it is assumed to be 1. |
cce855bc JA |
2898 | The return status is zero unless @var{n} is greater than @code{$#} or |
2899 | less than zero, non-zero otherwise. | |
ccc6cda3 JA |
2900 | |
2901 | @item test | |
2902 | @itemx [ | |
2903 | @btindex test | |
2904 | @btindex [ | |
cce855bc JA |
2905 | Evaluate a conditional expression @var{expr}. |
2906 | Each operator and operand must be a separate argument. | |
2907 | Expressions are composed of the primaries described below in | |
2908 | @ref{Bash Conditional Expressions}. | |
95732b49 JA |
2909 | @code{test} does not accept any options, nor does it accept and ignore |
2910 | an argument of @option{--} as signifying the end of options. | |
cce855bc | 2911 | |
bb70624e JA |
2912 | When the @code{[} form is used, the last argument to the command must |
2913 | be a @code{]}. | |
2914 | ||
cce855bc JA |
2915 | Expressions may be combined using the following operators, listed in |
2916 | decreasing order of precedence. | |
2917 | ||
2918 | @table @code | |
2919 | @item ! @var{expr} | |
2920 | True if @var{expr} is false. | |
2921 | ||
2922 | @item ( @var{expr} ) | |
2923 | Returns the value of @var{expr}. | |
2924 | This may be used to override the normal precedence of operators. | |
2925 | ||
2926 | @item @var{expr1} -a @var{expr2} | |
2927 | True if both @var{expr1} and @var{expr2} are true. | |
2928 | ||
2929 | @item @var{expr1} -o @var{expr2} | |
2930 | True if either @var{expr1} or @var{expr2} is true. | |
2931 | @end table | |
2932 | ||
2933 | The @code{test} and @code{[} builtins evaluate conditional | |
2934 | expressions using a set of rules based on the number of arguments. | |
2935 | ||
2936 | @table @asis | |
2937 | @item 0 arguments | |
2938 | The expression is false. | |
2939 | ||
2940 | @item 1 argument | |
2941 | The expression is true if and only if the argument is not null. | |
2942 | ||
2943 | @item 2 arguments | |
2944 | If the first argument is @samp{!}, the expression is true if and | |
2945 | only if the second argument is null. | |
2946 | If the first argument is one of the unary conditional operators | |
2947 | (@pxref{Bash Conditional Expressions}), the expression | |
2948 | is true if the unary test is true. | |
2949 | If the first argument is not a valid unary operator, the expression is | |
2950 | false. | |
2951 | ||
2952 | @item 3 arguments | |
2953 | If the second argument is one of the binary conditional | |
2954 | operators (@pxref{Bash Conditional Expressions}), the | |
2955 | result of the expression is the result of the binary test using the | |
2956 | first and third arguments as operands. | |
2957 | If the first argument is @samp{!}, the value is the negation of | |
2958 | the two-argument test using the second and third arguments. | |
2959 | If the first argument is exactly @samp{(} and the third argument is | |
2960 | exactly @samp{)}, the result is the one-argument test of the second | |
2961 | argument. | |
2962 | Otherwise, the expression is false. | |
2963 | The @samp{-a} and @samp{-o} operators are considered binary operators | |
2964 | in this case. | |
2965 | ||
2966 | @item 4 arguments | |
2967 | If the first argument is @samp{!}, the result is the negation of | |
2968 | the three-argument expression composed of the remaining arguments. | |
2969 | Otherwise, the expression is parsed and evaluated according to | |
2970 | precedence using the rules listed above. | |
2971 | ||
2972 | @item 5 or more arguments | |
2973 | The expression is parsed and evaluated according to precedence | |
2974 | using the rules listed above. | |
2975 | @end table | |
ccc6cda3 JA |
2976 | |
2977 | @item times | |
2978 | @btindex times | |
2979 | @example | |
2980 | times | |
2981 | @end example | |
2982 | Print out the user and system times used by the shell and its children. | |
cce855bc | 2983 | The return status is zero. |
ccc6cda3 JA |
2984 | |
2985 | @item trap | |
2986 | @btindex trap | |
2987 | @example | |
d166f048 | 2988 | trap [-lp] [@var{arg}] [@var{sigspec} @dots{}] |
ccc6cda3 JA |
2989 | @end example |
2990 | The commands in @var{arg} are to be read and executed when the | |
b80f6443 JA |
2991 | shell receives signal @var{sigspec}. If @var{arg} is absent (and |
2992 | there is a single @var{sigspec}) or | |
2993 | equal to @samp{-}, each specified signal's disposition is reset | |
2994 | to the value it had when the shell was started. | |
d166f048 JA |
2995 | If @var{arg} is the null string, then the signal specified by |
2996 | each @var{sigspec} is ignored by the shell and commands it invokes. | |
28ef6c31 | 2997 | If @var{arg} is not present and @option{-p} has been supplied, |
bb70624e JA |
2998 | the shell displays the trap commands associated with each @var{sigspec}. |
2999 | If no arguments are supplied, or | |
28ef6c31 | 3000 | only @option{-p} is given, @code{trap} prints the list of commands |
cce855bc JA |
3001 | associated with each signal number in a form that may be reused as |
3002 | shell input. | |
b80f6443 JA |
3003 | The @option{-l} option causes the shell to print a list of signal names |
3004 | and their corresponding numbers. | |
3005 | Each @var{sigspec} is either a signal name or a signal number. | |
3006 | Signal names are case insensitive and the @code{SIG} prefix is optional. | |
d166f048 JA |
3007 | If a @var{sigspec} |
3008 | is @code{0} or @code{EXIT}, @var{arg} is executed when the shell exits. | |
3009 | If a @var{sigspec} is @code{DEBUG}, the command @var{arg} is executed | |
b80f6443 JA |
3010 | before every simple command, @code{for} command, @code{case} command, |
3011 | @code{select} command, every arithmetic @code{for} command, and before | |
3012 | the first command executes in a shell function. | |
3013 | Refer to the description of the @code{extglob} option to the | |
3014 | @code{shopt} builtin (@pxref{Bash Builtins}) for details of its | |
3015 | effect on the @code{DEBUG} trap. | |
f73dda09 | 3016 | If a @var{sigspec} is @code{ERR}, the command @var{arg} |
b80f6443 JA |
3017 | is executed whenever a simple command has a non-zero exit status, |
3018 | subject to the following conditions. | |
3019 | The @code{ERR} trap is not executed if the failed command is part of the | |
3020 | command list immediately following an @code{until} or @code{while} keyword, | |
3021 | part of the test in an @code{if} statement, | |
f73dda09 JA |
3022 | part of a @code{&&} or @code{||} list, or if the command's return |
3023 | status is being inverted using @code{!}. | |
b80f6443 JA |
3024 | These are the same conditions obeyed by the @code{errexit} option. |
3025 | If a @var{sigspec} is @code{RETURN}, the command @var{arg} is executed | |
3026 | each time a shell function or a script executed with the @code{.} or | |
3027 | @code{source} builtins finishes executing. | |
ccc6cda3 JA |
3028 | |
3029 | Signals ignored upon entry to the shell cannot be trapped or reset. | |
3030 | Trapped signals are reset to their original values in a child | |
3031 | process when it is created. | |
3032 | ||
cce855bc JA |
3033 | The return status is zero unless a @var{sigspec} does not specify a |
3034 | valid signal. | |
3035 | ||
ccc6cda3 JA |
3036 | @item umask |
3037 | @btindex umask | |
3038 | @example | |
cce855bc | 3039 | umask [-p] [-S] [@var{mode}] |
ccc6cda3 JA |
3040 | @end example |
3041 | Set the shell process's file creation mask to @var{mode}. If | |
3042 | @var{mode} begins with a digit, it is interpreted as an octal number; | |
3043 | if not, it is interpreted as a symbolic mode mask similar | |
3044 | to that accepted by the @code{chmod} command. If @var{mode} is | |
28ef6c31 | 3045 | omitted, the current value of the mask is printed. If the @option{-S} |
ccc6cda3 JA |
3046 | option is supplied without a @var{mode} argument, the mask is printed |
3047 | in a symbolic format. | |
28ef6c31 | 3048 | If the @option{-p} option is supplied, and @var{mode} |
cce855bc JA |
3049 | is omitted, the output is in a form that may be reused as input. |
3050 | The return status is zero if the mode is successfully changed or if | |
3051 | no @var{mode} argument is supplied, and non-zero otherwise. | |
ccc6cda3 | 3052 | |
bb70624e JA |
3053 | Note that when the mode is interpreted as an octal number, each number |
3054 | of the umask is subtracted from @code{7}. Thus, a umask of @code{022} | |
3055 | results in permissions of @code{755}. | |
3056 | ||
ccc6cda3 JA |
3057 | @item unset |
3058 | @btindex unset | |
3059 | @example | |
3060 | unset [-fv] [@var{name}] | |
3061 | @end example | |
3062 | Each variable or function @var{name} is removed. | |
28ef6c31 | 3063 | If no options are supplied, or the @option{-v} option is given, each |
ccc6cda3 | 3064 | @var{name} refers to a shell variable. |
28ef6c31 | 3065 | If the @option{-f} option is given, the @var{name}s refer to shell |
ccc6cda3 | 3066 | functions, and the function definition is removed. |
cce855bc | 3067 | Readonly variables and functions may not be unset. |
b80f6443 | 3068 | The return status is zero unless a @var{name} is readonly. |
ccc6cda3 JA |
3069 | @end table |
3070 | ||
bb70624e JA |
3071 | @node Bash Builtins |
3072 | @section Bash Builtin Commands | |
ccc6cda3 | 3073 | |
bb70624e JA |
3074 | This section describes builtin commands which are unique to |
3075 | or have been extended in Bash. | |
3076 | Some of these commands are specified in the @sc{posix} 1003.2 standard. | |
ccc6cda3 | 3077 | |
bb70624e | 3078 | @table @code |
ccc6cda3 | 3079 | |
bb70624e JA |
3080 | @item alias |
3081 | @btindex alias | |
3082 | @example | |
3083 | alias [@code{-p}] [@var{name}[=@var{value}] @dots{}] | |
3084 | @end example | |
ccc6cda3 | 3085 | |
28ef6c31 | 3086 | Without arguments or with the @option{-p} option, @code{alias} prints |
bb70624e JA |
3087 | the list of aliases on the standard output in a form that allows |
3088 | them to be reused as input. | |
3089 | If arguments are supplied, an alias is defined for each @var{name} | |
3090 | whose @var{value} is given. If no @var{value} is given, the name | |
3091 | and value of the alias is printed. | |
3092 | Aliases are described in @ref{Aliases}. | |
ccc6cda3 | 3093 | |
bb70624e JA |
3094 | @item bind |
3095 | @btindex bind | |
3096 | @example | |
3097 | bind [-m @var{keymap}] [-lpsvPSV] | |
3098 | bind [-m @var{keymap}] [-q @var{function}] [-u @var{function}] [-r @var{keyseq}] | |
3099 | bind [-m @var{keymap}] -f @var{filename} | |
3100 | bind [-m @var{keymap}] -x @var{keyseq:shell-command} | |
3101 | bind [-m @var{keymap}] @var{keyseq:function-name} | |
7117c2d2 | 3102 | bind @var{readline-command} |
bb70624e | 3103 | @end example |
ccc6cda3 | 3104 | |
bb70624e | 3105 | Display current Readline (@pxref{Command Line Editing}) |
7117c2d2 JA |
3106 | key and function bindings, |
3107 | bind a key sequence to a Readline function or macro, | |
3108 | or set a Readline variable. | |
3109 | Each non-option argument is a command as it would appear in a | |
bb70624e | 3110 | a Readline initialization file (@pxref{Readline Init File}), |
7117c2d2 | 3111 | but each binding or command must be passed as a separate argument; e.g., |
bb70624e JA |
3112 | @samp{"\C-x\C-r":re-read-init-file}. |
3113 | Options, if supplied, have the following meanings: | |
cce855bc | 3114 | |
bb70624e JA |
3115 | @table @code |
3116 | @item -m @var{keymap} | |
3117 | Use @var{keymap} as the keymap to be affected by | |
3118 | the subsequent bindings. Acceptable @var{keymap} | |
3119 | names are | |
3120 | @code{emacs}, | |
3121 | @code{emacs-standard}, | |
3122 | @code{emacs-meta}, | |
3123 | @code{emacs-ctlx}, | |
3124 | @code{vi}, | |
28ef6c31 | 3125 | @code{vi-move}, |
bb70624e JA |
3126 | @code{vi-command}, and |
3127 | @code{vi-insert}. | |
3128 | @code{vi} is equivalent to @code{vi-command}; | |
3129 | @code{emacs} is equivalent to @code{emacs-standard}. | |
cce855bc | 3130 | |
bb70624e JA |
3131 | @item -l |
3132 | List the names of all Readline functions. | |
cce855bc | 3133 | |
bb70624e JA |
3134 | @item -p |
3135 | Display Readline function names and bindings in such a way that they | |
3136 | can be used as input or in a Readline initialization file. | |
cce855bc | 3137 | |
bb70624e JA |
3138 | @item -P |
3139 | List current Readline function names and bindings. | |
ccc6cda3 | 3140 | |
bb70624e JA |
3141 | @item -v |
3142 | Display Readline variable names and values in such a way that they | |
3143 | can be used as input or in a Readline initialization file. | |
ccc6cda3 | 3144 | |
bb70624e JA |
3145 | @item -V |
3146 | List current Readline variable names and values. | |
ccc6cda3 | 3147 | |
bb70624e JA |
3148 | @item -s |
3149 | Display Readline key sequences bound to macros and the strings they output | |
3150 | in such a way that they can be used as input or in a Readline | |
3151 | initialization file. | |
ccc6cda3 | 3152 | |
bb70624e JA |
3153 | @item -S |
3154 | Display Readline key sequences bound to macros and the strings they output. | |
ccc6cda3 | 3155 | |
bb70624e JA |
3156 | @item -f @var{filename} |
3157 | Read key bindings from @var{filename}. | |
ccc6cda3 | 3158 | |
bb70624e JA |
3159 | @item -q @var{function} |
3160 | Query about which keys invoke the named @var{function}. | |
ccc6cda3 | 3161 | |
bb70624e JA |
3162 | @item -u @var{function} |
3163 | Unbind all keys bound to the named @var{function}. | |
cce855bc | 3164 | |
bb70624e JA |
3165 | @item -r @var{keyseq} |
3166 | Remove any current binding for @var{keyseq}. | |
ccc6cda3 | 3167 | |
bb70624e JA |
3168 | @item -x @var{keyseq:shell-command} |
3169 | Cause @var{shell-command} to be executed whenever @var{keyseq} is | |
3170 | entered. | |
ccc6cda3 | 3171 | |
bb70624e | 3172 | @end table |
ccc6cda3 | 3173 | |
bb70624e JA |
3174 | @noindent |
3175 | The return status is zero unless an invalid option is supplied or an | |
3176 | error occurs. | |
ccc6cda3 | 3177 | |
bb70624e JA |
3178 | @item builtin |
3179 | @btindex builtin | |
3180 | @example | |
3181 | builtin [@var{shell-builtin} [@var{args}]] | |
3182 | @end example | |
3183 | Run a shell builtin, passing it @var{args}, and return its exit status. | |
3184 | This is useful when defining a shell function with the same | |
3185 | name as a shell builtin, retaining the functionality of the builtin within | |
3186 | the function. | |
3187 | The return status is non-zero if @var{shell-builtin} is not a shell | |
3188 | builtin command. | |
ccc6cda3 | 3189 | |
b80f6443 JA |
3190 | @item caller |
3191 | @btindex caller | |
3192 | @example | |
3193 | caller [@var{expr}] | |
3194 | @end example | |
3195 | Returns the context of any active subroutine call (a shell function or | |
3196 | a script executed with the @code{.} or @code{source} builtins). | |
3197 | ||
3198 | Without @var{expr}, @code{caller} displays the line number and source | |
3199 | filename of the current subroutine call. | |
3200 | If a non-negative integer is supplied as @var{expr}, @code{caller} | |
3201 | displays the line number, subroutine name, and source file corresponding | |
3202 | to that position in the current execution call stack. This extra | |
3203 | information may be used, for example, to print a stack trace. The | |
3204 | current frame is frame 0. | |
3205 | ||
3206 | The return value is 0 unless the shell is not executing a subroutine | |
3207 | call or @var{expr} does not correspond to a valid position in the | |
3208 | call stack. | |
3209 | ||
bb70624e JA |
3210 | @item command |
3211 | @btindex command | |
3212 | @example | |
3213 | command [-pVv] @var{command} [@var{arguments} @dots{}] | |
3214 | @end example | |
3215 | Runs @var{command} with @var{arguments} ignoring any shell function | |
3216 | named @var{command}. | |
3217 | Only shell builtin commands or commands found by searching the | |
28ef6c31 | 3218 | @env{PATH} are executed. |
bb70624e JA |
3219 | If there is a shell function named @code{ls}, running @samp{command ls} |
3220 | within the function will execute the external command @code{ls} | |
3221 | instead of calling the function recursively. | |
28ef6c31 | 3222 | The @option{-p} option means to use a default value for @env{PATH} |
bb70624e JA |
3223 | that is guaranteed to find all of the standard utilities. |
3224 | The return status in this case is 127 if @var{command} cannot be | |
3225 | found or an error occurred, and the exit status of @var{command} | |
3226 | otherwise. | |
cce855bc | 3227 | |
28ef6c31 JA |
3228 | If either the @option{-V} or @option{-v} option is supplied, a |
3229 | description of @var{command} is printed. The @option{-v} option | |
bb70624e | 3230 | causes a single word indicating the command or file name used to |
28ef6c31 | 3231 | invoke @var{command} to be displayed; the @option{-V} option produces |
bb70624e JA |
3232 | a more verbose description. In this case, the return status is |
3233 | zero if @var{command} is found, and non-zero if not. | |
cce855bc | 3234 | |
bb70624e JA |
3235 | @item declare |
3236 | @btindex declare | |
3237 | @example | |
b80f6443 | 3238 | declare [-afFirtx] [-p] [@var{name}[=@var{value}] @dots{}] |
bb70624e | 3239 | @end example |
ccc6cda3 | 3240 | |
bb70624e JA |
3241 | Declare variables and give them attributes. If no @var{name}s |
3242 | are given, then display the values of variables instead. | |
ccc6cda3 | 3243 | |
28ef6c31 | 3244 | The @option{-p} option will display the attributes and values of each |
b80f6443 JA |
3245 | @var{name}. |
3246 | When @option{-p} is used, additional options are ignored. | |
28ef6c31 | 3247 | The @option{-F} option inhibits the display of function definitions; |
b80f6443 JA |
3248 | only the function name and attributes are printed. |
3249 | If the @code{extdebug} shell option is enabled using @code{shopt} | |
3250 | (@pxref{Bash Builtins}), the source file name and line number where | |
3251 | the function is defined are displayed as well. | |
3252 | @option{-F} implies @option{-f}. | |
3253 | The following options can be used to restrict output to variables with | |
3254 | the specified attributes or to give variables attributes: | |
cce855bc | 3255 | |
bb70624e JA |
3256 | @table @code |
3257 | @item -a | |
3258 | Each @var{name} is an array variable (@pxref{Arrays}). | |
cce855bc | 3259 | |
bb70624e JA |
3260 | @item -f |
3261 | Use function names only. | |
ccc6cda3 | 3262 | |
bb70624e JA |
3263 | @item -i |
3264 | The variable is to be treated as | |
3265 | an integer; arithmetic evaluation (@pxref{Shell Arithmetic}) is | |
3266 | performed when the variable is assigned a value. | |
ccc6cda3 | 3267 | |
bb70624e JA |
3268 | @item -r |
3269 | Make @var{name}s readonly. These names cannot then be assigned values | |
3270 | by subsequent assignment statements or unset. | |
ccc6cda3 | 3271 | |
7117c2d2 JA |
3272 | @item -t |
3273 | Give each @var{name} the @code{trace} attribute. | |
95732b49 JA |
3274 | Traced functions inherit the @code{DEBUG} and @code{RETURN} traps from |
3275 | the calling shell. | |
7117c2d2 JA |
3276 | The trace attribute has no special meaning for variables. |
3277 | ||
bb70624e JA |
3278 | @item -x |
3279 | Mark each @var{name} for export to subsequent commands via | |
3280 | the environment. | |
3281 | @end table | |
ccc6cda3 | 3282 | |
bb70624e JA |
3283 | Using @samp{+} instead of @samp{-} turns off the attribute instead. |
3284 | When used in a function, @code{declare} makes each @var{name} local, | |
b80f6443 JA |
3285 | as with the @code{local} command. If a variable name is followed by |
3286 | =@var{value}, the value of the variable is set to @var{value}. | |
ccc6cda3 | 3287 | |
bb70624e JA |
3288 | The return status is zero unless an invalid option is encountered, |
3289 | an attempt is made to define a function using @samp{-f foo=bar}, | |
3290 | an attempt is made to assign a value to a readonly variable, | |
3291 | an attempt is made to assign a value to an array variable without | |
3292 | using the compound assignment syntax (@pxref{Arrays}), | |
3293 | one of the @var{names} is not a valid shell variable name, | |
3294 | an attempt is made to turn off readonly status for a readonly variable, | |
3295 | an attempt is made to turn off array status for an array variable, | |
28ef6c31 | 3296 | or an attempt is made to display a non-existent function with @option{-f}. |
ccc6cda3 | 3297 | |
bb70624e JA |
3298 | @item echo |
3299 | @btindex echo | |
3300 | @example | |
3301 | echo [-neE] [@var{arg} @dots{}] | |
3302 | @end example | |
3303 | Output the @var{arg}s, separated by spaces, terminated with a | |
3304 | newline. | |
3305 | The return status is always 0. | |
28ef6c31 JA |
3306 | If @option{-n} is specified, the trailing newline is suppressed. |
3307 | If the @option{-e} option is given, interpretation of the following | |
bb70624e | 3308 | backslash-escaped characters is enabled. |
28ef6c31 | 3309 | The @option{-E} option disables the interpretation of these escape characters, |
bb70624e JA |
3310 | even on systems where they are interpreted by default. |
3311 | The @code{xpg_echo} shell option may be used to | |
3312 | dynamically determine whether or not @code{echo} expands these | |
3313 | escape characters by default. | |
95732b49 JA |
3314 | @code{echo} does not interpret @option{--} to mean the end of options. |
3315 | ||
bb70624e JA |
3316 | @code{echo} interprets the following escape sequences: |
3317 | @table @code | |
3318 | @item \a | |
3319 | alert (bell) | |
3320 | @item \b | |
3321 | backspace | |
3322 | @item \c | |
3323 | suppress trailing newline | |
3324 | @item \e | |
3325 | escape | |
3326 | @item \f | |
3327 | form feed | |
3328 | @item \n | |
3329 | new line | |
3330 | @item \r | |
3331 | carriage return | |
3332 | @item \t | |
3333 | horizontal tab | |
3334 | @item \v | |
3335 | vertical tab | |
3336 | @item \\ | |
3337 | backslash | |
7117c2d2 JA |
3338 | @item \0@var{nnn} |
3339 | the eight-bit character whose value is the octal value @var{nnn} | |
3340 | (zero to three octal digits) | |
bb70624e | 3341 | @item \@var{nnn} |
f73dda09 | 3342 | the eight-bit character whose value is the octal value @var{nnn} |
7117c2d2 | 3343 | (one to three octal digits) |
f73dda09 JA |
3344 | @item \x@var{HH} |
3345 | the eight-bit character whose value is the hexadecimal value @var{HH} | |
3346 | (one or two hex digits) | |
bb70624e | 3347 | @end table |
ccc6cda3 | 3348 | |
bb70624e JA |
3349 | @item enable |
3350 | @btindex enable | |
3351 | @example | |
3352 | enable [-n] [-p] [-f @var{filename}] [-ads] [@var{name} @dots{}] | |
3353 | @end example | |
3354 | Enable and disable builtin shell commands. | |
3355 | Disabling a builtin allows a disk command which has the same name | |
3356 | as a shell builtin to be executed without specifying a full pathname, | |
3357 | even though the shell normally searches for builtins before disk commands. | |
28ef6c31 | 3358 | If @option{-n} is used, the @var{name}s become disabled. Otherwise |
bb70624e | 3359 | @var{name}s are enabled. For example, to use the @code{test} binary |
28ef6c31 | 3360 | found via @env{$PATH} instead of the shell builtin version, type |
bb70624e | 3361 | @samp{enable -n test}. |
ccc6cda3 | 3362 | |
28ef6c31 | 3363 | If the @option{-p} option is supplied, or no @var{name} arguments appear, |
bb70624e JA |
3364 | a list of shell builtins is printed. With no other arguments, the list |
3365 | consists of all enabled shell builtins. | |
28ef6c31 | 3366 | The @option{-a} option means to list |
bb70624e | 3367 | each builtin with an indication of whether or not it is enabled. |
ccc6cda3 | 3368 | |
28ef6c31 | 3369 | The @option{-f} option means to load the new builtin command @var{name} |
bb70624e | 3370 | from shared object @var{filename}, on systems that support dynamic loading. |
28ef6c31 | 3371 | The @option{-d} option will delete a builtin loaded with @option{-f}. |
ccc6cda3 | 3372 | |
bb70624e | 3373 | If there are no options, a list of the shell builtins is displayed. |
28ef6c31 JA |
3374 | The @option{-s} option restricts @code{enable} to the @sc{posix} special |
3375 | builtins. If @option{-s} is used with @option{-f}, the new builtin becomes | |
bb70624e | 3376 | a special builtin (@pxref{Special Builtins}). |
ccc6cda3 | 3377 | |
bb70624e JA |
3378 | The return status is zero unless a @var{name} is not a shell builtin |
3379 | or there is an error loading a new builtin from a shared object. | |
cce855bc | 3380 | |
bb70624e JA |
3381 | @item help |
3382 | @btindex help | |
3383 | @example | |
3384 | help [-s] [@var{pattern}] | |
3385 | @end example | |
3386 | Display helpful information about builtin commands. | |
3387 | If @var{pattern} is specified, @code{help} gives detailed help | |
3388 | on all commands matching @var{pattern}, otherwise a list of | |
3389 | the builtins is printed. | |
28ef6c31 | 3390 | The @option{-s} option restricts the information displayed to a short |
bb70624e JA |
3391 | usage synopsis. |
3392 | The return status is zero unless no command matches @var{pattern}. | |
ccc6cda3 | 3393 | |
bb70624e JA |
3394 | @item let |
3395 | @btindex let | |
3396 | @example | |
3397 | let @var{expression} [@var{expression}] | |
3398 | @end example | |
3399 | The @code{let} builtin allows arithmetic to be performed on shell | |
3400 | variables. Each @var{expression} is evaluated according to the | |
3401 | rules given below in @ref{Shell Arithmetic}. If the | |
3402 | last @var{expression} evaluates to 0, @code{let} returns 1; | |
3403 | otherwise 0 is returned. | |
ccc6cda3 | 3404 | |
bb70624e JA |
3405 | @item local |
3406 | @btindex local | |
3407 | @example | |
b80f6443 | 3408 | local [@var{option}] @var{name}[=@var{value}] @dots{} |
bb70624e JA |
3409 | @end example |
3410 | For each argument, a local variable named @var{name} is created, | |
3411 | and assigned @var{value}. | |
3412 | The @var{option} can be any of the options accepted by @code{declare}. | |
3413 | @code{local} can only be used within a function; it makes the variable | |
3414 | @var{name} have a visible scope restricted to that function and its | |
3415 | children. The return status is zero unless @code{local} is used outside | |
3416 | a function, an invalid @var{name} is supplied, or @var{name} is a | |
3417 | readonly variable. | |
ccc6cda3 | 3418 | |
bb70624e JA |
3419 | @item logout |
3420 | @btindex logout | |
3421 | @example | |
3422 | logout [@var{n}] | |
3423 | @end example | |
3424 | Exit a login shell, returning a status of @var{n} to the shell's | |
3425 | parent. | |
cce855bc | 3426 | |
bb70624e JA |
3427 | @item printf |
3428 | @btindex printf | |
3429 | @example | |
95732b49 | 3430 | @code{printf} [-v @var{var}] @var{format} [@var{arguments}] |
bb70624e JA |
3431 | @end example |
3432 | Write the formatted @var{arguments} to the standard output under the | |
3433 | control of the @var{format}. | |
3434 | The @var{format} is a character string which contains three types of objects: | |
3435 | plain characters, which are simply copied to standard output, character | |
3436 | escape sequences, which are converted and copied to the standard output, and | |
3437 | format specifications, each of which causes printing of the next successive | |
3438 | @var{argument}. | |
3439 | In addition to the standard @code{printf(1)} formats, @samp{%b} causes | |
3440 | @code{printf} to expand backslash escape sequences in the corresponding | |
b80f6443 JA |
3441 | @var{argument}, |
3442 | (except that @samp{\c} terminates output, backslashes in | |
3443 | @samp{\'}, @samp{\"}, and @samp{\?} are not removed, and octal escapes | |
3444 | beginning with @samp{\0} may contain up to four digits), | |
3445 | and @samp{%q} causes @code{printf} to output the | |
bb70624e | 3446 | corresponding @var{argument} in a format that can be reused as shell input. |
cce855bc | 3447 | |
95732b49 JA |
3448 | The @option{-v} option causes the output to be assigned to the variable |
3449 | @var{var} rather than being printed to the standard output. | |
3450 | ||
bb70624e JA |
3451 | The @var{format} is reused as necessary to consume all of the @var{arguments}. |
3452 | If the @var{format} requires more @var{arguments} than are supplied, the | |
3453 | extra format specifications behave as if a zero value or null string, as | |
3454 | appropriate, had been supplied. The return value is zero on success, | |
3455 | non-zero on failure. | |
ccc6cda3 | 3456 | |
bb70624e JA |
3457 | @item read |
3458 | @btindex read | |
3459 | @example | |
7117c2d2 | 3460 | read [-ers] [-a @var{aname}] [-d @var{delim}] [-n @var{nchars}] [-p @var{prompt}] [-t @var{timeout}] [-u @var{fd}] [@var{name} @dots{}] |
bb70624e | 3461 | @end example |
7117c2d2 JA |
3462 | One line is read from the standard input, or from the file descriptor |
3463 | @var{fd} supplied as an argument to the @option{-u} option, and the first word | |
bb70624e JA |
3464 | is assigned to the first @var{name}, the second word to the second @var{name}, |
3465 | and so on, with leftover words and their intervening separators assigned | |
3466 | to the last @var{name}. | |
7117c2d2 | 3467 | If there are fewer words read from the input stream than names, |
bb70624e | 3468 | the remaining names are assigned empty values. |
28ef6c31 | 3469 | The characters in the value of the @env{IFS} variable |
bb70624e JA |
3470 | are used to split the line into words. |
3471 | The backslash character @samp{\} may be used to remove any special | |
3472 | meaning for the next character read and for line continuation. | |
3473 | If no names are supplied, the line read is assigned to the | |
28ef6c31 | 3474 | variable @env{REPLY}. |
7117c2d2 JA |
3475 | The return code is zero, unless end-of-file is encountered, @code{read} |
3476 | times out, or an invalid file descriptor is supplied as the argument to | |
3477 | @option{-u}. | |
bb70624e | 3478 | Options, if supplied, have the following meanings: |
ccc6cda3 | 3479 | |
bb70624e JA |
3480 | @table @code |
3481 | @item -a @var{aname} | |
3482 | The words are assigned to sequential indices of the array variable | |
3483 | @var{aname}, starting at 0. | |
3484 | All elements are removed from @var{aname} before the assignment. | |
3485 | Other @var{name} arguments are ignored. | |
ccc6cda3 | 3486 | |
bb70624e JA |
3487 | @item -d @var{delim} |
3488 | The first character of @var{delim} is used to terminate the input line, | |
3489 | rather than newline. | |
ccc6cda3 | 3490 | |
bb70624e JA |
3491 | @item -e |
3492 | Readline (@pxref{Command Line Editing}) is used to obtain the line. | |
ccc6cda3 | 3493 | |
bb70624e JA |
3494 | @item -n @var{nchars} |
3495 | @code{read} returns after reading @var{nchars} characters rather than | |
3496 | waiting for a complete line of input. | |
ccc6cda3 | 3497 | |
bb70624e JA |
3498 | @item -p @var{prompt} |
3499 | Display @var{prompt}, without a trailing newline, before attempting | |
3500 | to read any input. | |
3501 | The prompt is displayed only if input is coming from a terminal. | |
ccc6cda3 | 3502 | |
bb70624e JA |
3503 | @item -r |
3504 | If this option is given, backslash does not act as an escape character. | |
3505 | The backslash is considered to be part of the line. | |
3506 | In particular, a backslash-newline pair may not be used as a line | |
3507 | continuation. | |
ccc6cda3 | 3508 | |
bb70624e JA |
3509 | @item -s |
3510 | Silent mode. If input is coming from a terminal, characters are | |
3511 | not echoed. | |
ccc6cda3 | 3512 | |
bb70624e JA |
3513 | @item -t @var{timeout} |
3514 | Cause @code{read} to time out and return failure if a complete line of | |
3515 | input is not read within @var{timeout} seconds. | |
3516 | This option has no effect if @code{read} is not reading input from the | |
3517 | terminal or a pipe. | |
ccc6cda3 | 3518 | |
7117c2d2 JA |
3519 | @item -u @var{fd} |
3520 | Read input from file descriptor @var{fd}. | |
3521 | ||
bb70624e | 3522 | @end table |
ccc6cda3 | 3523 | |
bb70624e JA |
3524 | @item shopt |
3525 | @btindex shopt | |
3526 | @example | |
3527 | shopt [-pqsu] [-o] [@var{optname} @dots{}] | |
3528 | @end example | |
3529 | Toggle the values of variables controlling optional shell behavior. | |
28ef6c31 | 3530 | With no options, or with the @option{-p} option, a list of all settable |
bb70624e | 3531 | options is displayed, with an indication of whether or not each is set. |
28ef6c31 | 3532 | The @option{-p} option causes output to be displayed in a form that |
bb70624e JA |
3533 | may be reused as input. |
3534 | Other options have the following meanings: | |
cce855bc | 3535 | |
bb70624e JA |
3536 | @table @code |
3537 | @item -s | |
3538 | Enable (set) each @var{optname}. | |
cce855bc | 3539 | |
bb70624e JA |
3540 | @item -u |
3541 | Disable (unset) each @var{optname}. | |
ccc6cda3 | 3542 | |
bb70624e JA |
3543 | @item -q |
3544 | Suppresses normal output; the return status | |
3545 | indicates whether the @var{optname} is set or unset. | |
28ef6c31 | 3546 | If multiple @var{optname} arguments are given with @option{-q}, |
bb70624e JA |
3547 | the return status is zero if all @var{optnames} are enabled; |
3548 | non-zero otherwise. | |
ccc6cda3 | 3549 | |
bb70624e JA |
3550 | @item -o |
3551 | Restricts the values of | |
28ef6c31 | 3552 | @var{optname} to be those defined for the @option{-o} option to the |
bb70624e JA |
3553 | @code{set} builtin (@pxref{The Set Builtin}). |
3554 | @end table | |
cce855bc | 3555 | |
28ef6c31 | 3556 | If either @option{-s} or @option{-u} |
bb70624e JA |
3557 | is used with no @var{optname} arguments, the display is limited to |
3558 | those options which are set or unset, respectively. | |
ccc6cda3 | 3559 | |
bb70624e JA |
3560 | Unless otherwise noted, the @code{shopt} options are disabled (off) |
3561 | by default. | |
cce855bc | 3562 | |
bb70624e JA |
3563 | The return status when listing options is zero if all @var{optnames} |
3564 | are enabled, non-zero otherwise. When setting or unsetting options, | |
3565 | the return status is zero unless an @var{optname} is not a valid shell | |
3566 | option. | |
ccc6cda3 | 3567 | |
bb70624e JA |
3568 | The list of @code{shopt} options is: |
3569 | @table @code | |
3570 | @item cdable_vars | |
3571 | If this is set, an argument to the @code{cd} | |
3572 | builtin command that | |
3573 | is not a directory is assumed to be the name of a variable whose | |
3574 | value is the directory to change to. | |
ccc6cda3 | 3575 | |
bb70624e JA |
3576 | @item cdspell |
3577 | If set, minor errors in the spelling of a directory component in a | |
3578 | @code{cd} command will be corrected. | |
3579 | The errors checked for are transposed characters, | |
3580 | a missing character, and a character too many. | |
3581 | If a correction is found, the corrected path is printed, | |
3582 | and the command proceeds. | |
3583 | This option is only used by interactive shells. | |
ccc6cda3 | 3584 | |
bb70624e JA |
3585 | @item checkhash |
3586 | If this is set, Bash checks that a command found in the hash | |
3587 | table exists before trying to execute it. If a hashed command no | |
3588 | longer exists, a normal path search is performed. | |
ccc6cda3 | 3589 | |
bb70624e JA |
3590 | @item checkwinsize |
3591 | If set, Bash checks the window size after each command | |
3592 | and, if necessary, updates the values of | |
28ef6c31 | 3593 | @env{LINES} and @env{COLUMNS}. |
ccc6cda3 | 3594 | |
bb70624e JA |
3595 | @item cmdhist |
3596 | If set, Bash | |
3597 | attempts to save all lines of a multiple-line | |
3598 | command in the same history entry. This allows | |
3599 | easy re-editing of multi-line commands. | |
cce855bc | 3600 | |
bb70624e JA |
3601 | @item dotglob |
3602 | If set, Bash includes filenames beginning with a `.' in | |
3603 | the results of filename expansion. | |
ccc6cda3 | 3604 | |
bb70624e JA |
3605 | @item execfail |
3606 | If this is set, a non-interactive shell will not exit if | |
3607 | it cannot execute the file specified as an argument to the @code{exec} | |
3608 | builtin command. An interactive shell does not exit if @code{exec} | |
3609 | fails. | |
cce855bc | 3610 | |
bb70624e JA |
3611 | @item expand_aliases |
3612 | If set, aliases are expanded as described below under Aliases, | |
3613 | @ref{Aliases}. | |
3614 | This option is enabled by default for interactive shells. | |
ccc6cda3 | 3615 | |
b80f6443 JA |
3616 | @item extdebug |
3617 | If set, behavior intended for use by debuggers is enabled: | |
3618 | ||
3619 | @enumerate | |
3620 | @item | |
3621 | The @option{-F} option to the @code{declare} builtin (@pxref{Bash Builtins}) | |
3622 | displays the source file name and line number corresponding to each function | |
3623 | name supplied as an argument. | |
3624 | ||
3625 | @item | |
3626 | If the command run by the @code{DEBUG} trap returns a non-zero value, the | |
3627 | next command is skipped and not executed. | |
3628 | ||
3629 | @item | |
3630 | If the command run by the @code{DEBUG} trap returns a value of 2, and the | |
3631 | shell is executing in a subroutine (a shell function or a shell script | |
3632 | executed by the @code{.} or @code{source} builtins), a call to | |
3633 | @code{return} is simulated. | |
95732b49 JA |
3634 | |
3635 | @item | |
3636 | @code{BASH_ARGC} and @code{BASH_ARGV} are updated as described in their | |
3637 | descriptions (@pxref{Bash Variables}). | |
3638 | ||
3639 | @item | |
3640 | Function tracing is enabled: command substitution, shell functions, and | |
3641 | subshells invoked with @code{( @var{command} )} inherit the | |
3642 | @code{DEBUG} and @code{RETURN} traps. | |
3643 | ||
3644 | @item | |
3645 | Error tracing is enabled: command substitution, shell functions, and | |
3646 | subshells invoked with @code{( @var{command} )} inherit the | |
3647 | @code{ERROR} trap. | |
b80f6443 JA |
3648 | @end enumerate |
3649 | ||
bb70624e JA |
3650 | @item extglob |
3651 | If set, the extended pattern matching features described above | |
3652 | (@pxref{Pattern Matching}) are enabled. | |
ccc6cda3 | 3653 | |
b80f6443 JA |
3654 | @item extquote |
3655 | If set, @code{$'@var{string}'} and @code{$"@var{string}"} quoting is | |
3656 | performed within @code{$@{@var{parameter}@}} expansions | |
3657 | enclosed in double quotes. This option is enabled by default. | |
3658 | ||
3659 | @item failglob | |
3660 | If set, patterns which fail to match filenames during pathname expansion | |
3661 | result in an expansion error. | |
3662 | ||
3663 | @item force_fignore | |
3664 | If set, the suffixes specified by the @env{FIGNORE} shell variable | |
3665 | cause words to be ignored when performing word completion even if | |
3666 | the ignored words are the only possible completions. | |
3667 | @xref{Bash Variables}, for a description of @env{FIGNORE}. | |
3668 | This option is enabled by default. | |
3669 | ||
3670 | @item gnu_errfmt | |
3671 | If set, shell error messages are written in the standard @sc{gnu} error | |
3672 | message format. | |
3673 | ||
bb70624e JA |
3674 | @item histappend |
3675 | If set, the history list is appended to the file named by the value | |
28ef6c31 | 3676 | of the @env{HISTFILE} |
bb70624e | 3677 | variable when the shell exits, rather than overwriting the file. |
ccc6cda3 | 3678 | |
bb70624e JA |
3679 | @item histreedit |
3680 | If set, and Readline | |
3681 | is being used, a user is given the opportunity to re-edit a | |
3682 | failed history substitution. | |
ccc6cda3 | 3683 | |
bb70624e JA |
3684 | @item histverify |
3685 | If set, and Readline | |
3686 | is being used, the results of history substitution are not immediately | |
3687 | passed to the shell parser. Instead, the resulting line is loaded into | |
3688 | the Readline editing buffer, allowing further modification. | |
ccc6cda3 | 3689 | |
bb70624e JA |
3690 | @item hostcomplete |
3691 | If set, and Readline is being used, Bash will attempt to perform | |
3692 | hostname completion when a word containing a @samp{@@} is being | |
3693 | completed (@pxref{Commands For Completion}). This option is enabled | |
3694 | by default. | |
ccc6cda3 | 3695 | |
bb70624e JA |
3696 | @item huponexit |
3697 | If set, Bash will send @code{SIGHUP} to all jobs when an interactive | |
3698 | login shell exits (@pxref{Signals}). | |
cce855bc | 3699 | |
bb70624e JA |
3700 | @item interactive_comments |
3701 | Allow a word beginning with @samp{#} | |
3702 | to cause that word and all remaining characters on that | |
3703 | line to be ignored in an interactive shell. | |
3704 | This option is enabled by default. | |
ccc6cda3 | 3705 | |
bb70624e JA |
3706 | @item lithist |
3707 | If enabled, and the @code{cmdhist} | |
3708 | option is enabled, multi-line commands are saved to the history with | |
3709 | embedded newlines rather than using semicolon separators where possible. | |
ccc6cda3 | 3710 | |
f73dda09 JA |
3711 | @item login_shell |
3712 | The shell sets this option if it is started as a login shell | |
3713 | (@pxref{Invoking Bash}). | |
3714 | The value may not be changed. | |
3715 | ||
bb70624e JA |
3716 | @item mailwarn |
3717 | If set, and a file that Bash is checking for mail has been | |
3718 | accessed since the last time it was checked, the message | |
3719 | @code{"The mail in @var{mailfile} has been read"} is displayed. | |
ccc6cda3 | 3720 | |
bb70624e JA |
3721 | @item no_empty_cmd_completion |
3722 | If set, and Readline is being used, Bash will not attempt to search | |
28ef6c31 | 3723 | the @env{PATH} for possible completions when completion is attempted |
bb70624e | 3724 | on an empty line. |
ccc6cda3 | 3725 | |
bb70624e JA |
3726 | @item nocaseglob |
3727 | If set, Bash matches filenames in a case-insensitive fashion when | |
3728 | performing filename expansion. | |
ccc6cda3 | 3729 | |
95732b49 JA |
3730 | @item nocasematch |
3731 | If set, Bash matches patterns in a case-insensitive fashion when | |
3732 | performing matching while executing @code{case} or @code{[[} | |
3733 | conditional commands. | |
3734 | ||
bb70624e JA |
3735 | @item nullglob |
3736 | If set, Bash allows filename patterns which match no | |
3737 | files to expand to a null string, rather than themselves. | |
ccc6cda3 | 3738 | |
bb70624e JA |
3739 | @item progcomp |
3740 | If set, the programmable completion facilities | |
3741 | (@pxref{Programmable Completion}) are enabled. | |
3742 | This option is enabled by default. | |
ccc6cda3 | 3743 | |
bb70624e | 3744 | @item promptvars |
b80f6443 JA |
3745 | If set, prompt strings undergo |
3746 | parameter expansion, command substitution, arithmetic | |
3747 | expansion, and quote removal after being expanded | |
3748 | as described below (@pxref{Printing a Prompt}). | |
bb70624e | 3749 | This option is enabled by default. |
ccc6cda3 | 3750 | |
bb70624e JA |
3751 | @item restricted_shell |
3752 | The shell sets this option if it is started in restricted mode | |
3753 | (@pxref{The Restricted Shell}). | |
3754 | The value may not be changed. | |
3755 | This is not reset when the startup files are executed, allowing | |
3756 | the startup files to discover whether or not a shell is restricted. | |
ccc6cda3 | 3757 | |
bb70624e JA |
3758 | @item shift_verbose |
3759 | If this is set, the @code{shift} | |
3760 | builtin prints an error message when the shift count exceeds the | |
3761 | number of positional parameters. | |
ccc6cda3 | 3762 | |
bb70624e | 3763 | @item sourcepath |
28ef6c31 | 3764 | If set, the @code{source} builtin uses the value of @env{PATH} |
bb70624e JA |
3765 | to find the directory containing the file supplied as an argument. |
3766 | This option is enabled by default. | |
ccc6cda3 | 3767 | |
bb70624e JA |
3768 | @item xpg_echo |
3769 | If set, the @code{echo} builtin expands backslash-escape sequences | |
3770 | by default. | |
ccc6cda3 | 3771 | |
bb70624e | 3772 | @end table |
ccc6cda3 | 3773 | |
bb70624e JA |
3774 | @noindent |
3775 | The return status when listing options is zero if all @var{optnames} | |
3776 | are enabled, non-zero otherwise. | |
3777 | When setting or unsetting options, the return status is zero unless an | |
3778 | @var{optname} is not a valid shell option. | |
ccc6cda3 | 3779 | |
bb70624e JA |
3780 | @item source |
3781 | @btindex source | |
3782 | @example | |
3783 | source @var{filename} | |
3784 | @end example | |
3785 | A synonym for @code{.} (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 3786 | |
bb70624e JA |
3787 | @item type |
3788 | @btindex type | |
3789 | @example | |
7117c2d2 | 3790 | type [-afptP] [@var{name} @dots{}] |
bb70624e JA |
3791 | @end example |
3792 | For each @var{name}, indicate how it would be interpreted if used as a | |
3793 | command name. | |
ccc6cda3 | 3794 | |
28ef6c31 | 3795 | If the @option{-t} option is used, @code{type} prints a single word |
bb70624e JA |
3796 | which is one of @samp{alias}, @samp{function}, @samp{builtin}, |
3797 | @samp{file} or @samp{keyword}, | |
3798 | if @var{name} is an alias, shell function, shell builtin, | |
3799 | disk file, or shell reserved word, respectively. | |
3800 | If the @var{name} is not found, then nothing is printed, and | |
3801 | @code{type} returns a failure status. | |
ccc6cda3 | 3802 | |
28ef6c31 JA |
3803 | If the @option{-p} option is used, @code{type} either returns the name |
3804 | of the disk file that would be executed, or nothing if @option{-t} | |
bb70624e JA |
3805 | would not return @samp{file}. |
3806 | ||
7117c2d2 JA |
3807 | The @option{-P} option forces a path search for each @var{name}, even if |
3808 | @option{-t} would not return @samp{file}. | |
3809 | ||
3810 | If a command is hashed, @option{-p} and @option{-P} print the hashed value, | |
3811 | not necessarily the file that appears first in @code{$PATH}. | |
3812 | ||
28ef6c31 | 3813 | If the @option{-a} option is used, @code{type} returns all of the places |
bb70624e | 3814 | that contain an executable named @var{file}. |
28ef6c31 | 3815 | This includes aliases and functions, if and only if the @option{-p} option |
bb70624e JA |
3816 | is not also used. |
3817 | ||
7117c2d2 JA |
3818 | If the @option{-f} option is used, @code{type} does not attempt to find |
3819 | shell functions, as with the @code{command} builtin. | |
3820 | ||
bb70624e JA |
3821 | The return status is zero if any of the @var{names} are found, non-zero |
3822 | if none are found. | |
ccc6cda3 | 3823 | |
bb70624e JA |
3824 | @item typeset |
3825 | @btindex typeset | |
ccc6cda3 | 3826 | @example |
b80f6443 | 3827 | typeset [-afFrxi] [-p] [@var{name}[=@var{value}] @dots{}] |
ccc6cda3 | 3828 | @end example |
bb70624e JA |
3829 | The @code{typeset} command is supplied for compatibility with the Korn |
3830 | shell; however, it has been deprecated in favor of the @code{declare} | |
3831 | builtin command. | |
ccc6cda3 | 3832 | |
bb70624e JA |
3833 | @item ulimit |
3834 | @btindex ulimit | |
3835 | @example | |
3836 | ulimit [-acdflmnpstuvSH] [@var{limit}] | |
3837 | @end example | |
3838 | @code{ulimit} provides control over the resources available to processes | |
3839 | started by the shell, on systems that allow such control. If an | |
3840 | option is given, it is interpreted as follows: | |
ccc6cda3 | 3841 | @table @code |
bb70624e JA |
3842 | @item -S |
3843 | Change and report the soft limit associated with a resource. | |
ccc6cda3 | 3844 | |
bb70624e JA |
3845 | @item -H |
3846 | Change and report the hard limit associated with a resource. | |
ccc6cda3 | 3847 | |
bb70624e JA |
3848 | @item -a |
3849 | All current limits are reported. | |
ccc6cda3 | 3850 | |
bb70624e JA |
3851 | @item -c |
3852 | The maximum size of core files created. | |
ccc6cda3 | 3853 | |
bb70624e JA |
3854 | @item -d |
3855 | The maximum size of a process's data segment. | |
ccc6cda3 | 3856 | |
bb70624e JA |
3857 | @item -f |
3858 | The maximum size of files created by the shell. | |
ccc6cda3 | 3859 | |
bb70624e JA |
3860 | @item -l |
3861 | The maximum size that may be locked into memory. | |
ccc6cda3 | 3862 | |
bb70624e JA |
3863 | @item -m |
3864 | The maximum resident set size. | |
ccc6cda3 | 3865 | |
bb70624e JA |
3866 | @item -n |
3867 | The maximum number of open file descriptors. | |
ccc6cda3 | 3868 | |
bb70624e JA |
3869 | @item -p |
3870 | The pipe buffer size. | |
ccc6cda3 | 3871 | |
bb70624e JA |
3872 | @item -s |
3873 | The maximum stack size. | |
ccc6cda3 | 3874 | |
bb70624e JA |
3875 | @item -t |
3876 | The maximum amount of cpu time in seconds. | |
ccc6cda3 | 3877 | |
bb70624e JA |
3878 | @item -u |
3879 | The maximum number of processes available to a single user. | |
ccc6cda3 | 3880 | |
bb70624e JA |
3881 | @item -v |
3882 | The maximum amount of virtual memory available to the process. | |
ccc6cda3 | 3883 | |
bb70624e | 3884 | @end table |
ccc6cda3 | 3885 | |
f73dda09 JA |
3886 | If @var{limit} is given, it is the new value of the specified resource; |
3887 | the special @var{limit} values @code{hard}, @code{soft}, and | |
3888 | @code{unlimited} stand for the current hard limit, the current soft limit, | |
3889 | and no limit, respectively. | |
bb70624e | 3890 | Otherwise, the current value of the soft limit for the specified resource |
28ef6c31 JA |
3891 | is printed, unless the @option{-H} option is supplied. |
3892 | When setting new limits, if neither @option{-H} nor @option{-S} is supplied, | |
bb70624e | 3893 | both the hard and soft limits are set. |
28ef6c31 JA |
3894 | If no option is given, then @option{-f} is assumed. Values are in 1024-byte |
3895 | increments, except for @option{-t}, which is in seconds, @option{-p}, | |
3896 | which is in units of 512-byte blocks, and @option{-n} and @option{-u}, which | |
bb70624e | 3897 | are unscaled values. |
ccc6cda3 | 3898 | |
f73dda09 JA |
3899 | The return status is zero unless an invalid option or argument is supplied, |
3900 | or an error occurs while setting a new limit. | |
ccc6cda3 | 3901 | |
bb70624e JA |
3902 | @item unalias |
3903 | @btindex unalias | |
3904 | @example | |
3905 | unalias [-a] [@var{name} @dots{} ] | |
3906 | @end example | |
ccc6cda3 | 3907 | |
28ef6c31 | 3908 | Remove each @var{name} from the list of aliases. If @option{-a} is |
bb70624e JA |
3909 | supplied, all aliases are removed. |
3910 | Aliases are described in @ref{Aliases}. | |
cce855bc | 3911 | |
ccc6cda3 JA |
3912 | @end table |
3913 | ||
bb70624e JA |
3914 | @node The Set Builtin |
3915 | @section The Set Builtin | |
ccc6cda3 | 3916 | |
bb70624e | 3917 | This builtin is so complicated that it deserves its own section. |
ccc6cda3 | 3918 | |
bb70624e JA |
3919 | @table @code |
3920 | @item set | |
3921 | @btindex set | |
3922 | @example | |
3923 | set [--abefhkmnptuvxBCHP] [-o @var{option}] [@var{argument} @dots{}] | |
3924 | @end example | |
ccc6cda3 | 3925 | |
bb70624e JA |
3926 | If no options or arguments are supplied, @code{set} displays the names |
3927 | and values of all shell variables and functions, sorted according to the | |
95732b49 JA |
3928 | current locale, in a format that may be reused as input |
3929 | for setting or resetting the currently-set variables. | |
3930 | Read-only variables cannot be reset. | |
3931 | In @sc{posix} mode, only shell variables are listed. | |
ccc6cda3 | 3932 | |
bb70624e JA |
3933 | When options are supplied, they set or unset shell attributes. |
3934 | Options, if specified, have the following meanings: | |
ccc6cda3 | 3935 | |
bb70624e JA |
3936 | @table @code |
3937 | @item -a | |
28ef6c31 JA |
3938 | Mark variables and function which are modified or created for export |
3939 | to the environment of subsequent commands. | |
ccc6cda3 | 3940 | |
bb70624e JA |
3941 | @item -b |
3942 | Cause the status of terminated background jobs to be reported | |
3943 | immediately, rather than before printing the next primary prompt. | |
ccc6cda3 | 3944 | |
bb70624e JA |
3945 | @item -e |
3946 | Exit immediately if a simple command (@pxref{Simple Commands}) exits | |
b80f6443 JA |
3947 | with a non-zero status, unless the command that fails is part of the |
3948 | command list immediately following a @code{while} or @code{until} | |
3949 | keyword, part of the test in an @code{if} statement, | |
bb70624e JA |
3950 | part of a @code{&&} or @code{||} list, or if the command's return |
3951 | status is being inverted using @code{!}. | |
f73dda09 | 3952 | A trap on @code{ERR}, if set, is executed before the shell exits. |
ccc6cda3 | 3953 | |
bb70624e JA |
3954 | @item -f |
3955 | Disable file name generation (globbing). | |
ccc6cda3 | 3956 | |
bb70624e JA |
3957 | @item -h |
3958 | Locate and remember (hash) commands as they are looked up for execution. | |
3959 | This option is enabled by default. | |
ccc6cda3 | 3960 | |
bb70624e JA |
3961 | @item -k |
3962 | All arguments in the form of assignment statements are placed | |
3963 | in the environment for a command, not just those that precede | |
3964 | the command name. | |
ccc6cda3 | 3965 | |
bb70624e JA |
3966 | @item -m |
3967 | Job control is enabled (@pxref{Job Control}). | |
ccc6cda3 | 3968 | |
bb70624e JA |
3969 | @item -n |
3970 | Read commands but do not execute them; this may be used to check a | |
3971 | script for syntax errors. | |
3972 | This option is ignored by interactive shells. | |
b72432fd | 3973 | |
bb70624e | 3974 | @item -o @var{option-name} |
ccc6cda3 | 3975 | |
bb70624e | 3976 | Set the option corresponding to @var{option-name}: |
ccc6cda3 | 3977 | |
bb70624e JA |
3978 | @table @code |
3979 | @item allexport | |
3980 | Same as @code{-a}. | |
ccc6cda3 | 3981 | |
bb70624e JA |
3982 | @item braceexpand |
3983 | Same as @code{-B}. | |
ccc6cda3 | 3984 | |
bb70624e JA |
3985 | @item emacs |
3986 | Use an @code{emacs}-style line editing interface (@pxref{Command Line Editing}). | |
d166f048 | 3987 | |
bb70624e JA |
3988 | @item errexit |
3989 | Same as @code{-e}. | |
d166f048 | 3990 | |
b80f6443 JA |
3991 | @item errtrace |
3992 | Same as @code{-E}. | |
3993 | ||
3994 | @item functrace | |
3995 | Same as @code{-T}. | |
3996 | ||
bb70624e JA |
3997 | @item hashall |
3998 | Same as @code{-h}. | |
ccc6cda3 | 3999 | |
bb70624e JA |
4000 | @item histexpand |
4001 | Same as @code{-H}. | |
ccc6cda3 | 4002 | |
bb70624e JA |
4003 | @item history |
4004 | Enable command history, as described in @ref{Bash History Facilities}. | |
4005 | This option is on by default in interactive shells. | |
ccc6cda3 | 4006 | |
bb70624e JA |
4007 | @item ignoreeof |
4008 | An interactive shell will not exit upon reading EOF. | |
ccc6cda3 | 4009 | |
bb70624e JA |
4010 | @item keyword |
4011 | Same as @code{-k}. | |
ccc6cda3 | 4012 | |
bb70624e JA |
4013 | @item monitor |
4014 | Same as @code{-m}. | |
ccc6cda3 | 4015 | |
bb70624e JA |
4016 | @item noclobber |
4017 | Same as @code{-C}. | |
ccc6cda3 | 4018 | |
bb70624e JA |
4019 | @item noexec |
4020 | Same as @code{-n}. | |
ccc6cda3 | 4021 | |
bb70624e JA |
4022 | @item noglob |
4023 | Same as @code{-f}. | |
ccc6cda3 | 4024 | |
f73dda09 JA |
4025 | @item nolog |
4026 | Currently ignored. | |
4027 | ||
bb70624e JA |
4028 | @item notify |
4029 | Same as @code{-b}. | |
ccc6cda3 | 4030 | |
bb70624e JA |
4031 | @item nounset |
4032 | Same as @code{-u}. | |
ccc6cda3 | 4033 | |
bb70624e JA |
4034 | @item onecmd |
4035 | Same as @code{-t}. | |
ccc6cda3 | 4036 | |
bb70624e JA |
4037 | @item physical |
4038 | Same as @code{-P}. | |
ccc6cda3 | 4039 | |
b80f6443 JA |
4040 | @item pipefail |
4041 | If set, the return value of a pipeline is the value of the last | |
4042 | (rightmost) command to exit with a non-zero status, or zero if all | |
4043 | commands in the pipeline exit successfully. | |
4044 | This option is disabled by default. | |
4045 | ||
bb70624e JA |
4046 | @item posix |
4047 | Change the behavior of Bash where the default operation differs | |
4048 | from the @sc{posix} 1003.2 standard to match the standard | |
4049 | (@pxref{Bash POSIX Mode}). | |
4050 | This is intended to make Bash behave as a strict superset of that | |
4051 | standard. | |
ccc6cda3 | 4052 | |
bb70624e JA |
4053 | @item privileged |
4054 | Same as @code{-p}. | |
cce855bc | 4055 | |
bb70624e JA |
4056 | @item verbose |
4057 | Same as @code{-v}. | |
ccc6cda3 | 4058 | |
bb70624e JA |
4059 | @item vi |
4060 | Use a @code{vi}-style line editing interface. | |
ccc6cda3 | 4061 | |
bb70624e JA |
4062 | @item xtrace |
4063 | Same as @code{-x}. | |
ccc6cda3 JA |
4064 | @end table |
4065 | ||
bb70624e JA |
4066 | @item -p |
4067 | Turn on privileged mode. | |
28ef6c31 | 4068 | In this mode, the @env{$BASH_ENV} and @env{$ENV} files are not |
bb70624e | 4069 | processed, shell functions are not inherited from the environment, |
28ef6c31 | 4070 | and the @env{SHELLOPTS} variable, if it appears in the environment, |
bb70624e JA |
4071 | is ignored. |
4072 | If the shell is started with the effective user (group) id not equal to the | |
4073 | real user (group) id, and the @code{-p} option is not supplied, these actions | |
4074 | are taken and the effective user id is set to the real user id. | |
4075 | If the @code{-p} option is supplied at startup, the effective user id is | |
4076 | not reset. | |
4077 | Turning this option off causes the effective user | |
4078 | and group ids to be set to the real user and group ids. | |
cce855bc | 4079 | |
bb70624e JA |
4080 | @item -t |
4081 | Exit after reading and executing one command. | |
ccc6cda3 | 4082 | |
bb70624e JA |
4083 | @item -u |
4084 | Treat unset variables as an error when performing parameter expansion. | |
4085 | An error message will be written to the standard error, and a non-interactive | |
4086 | shell will exit. | |
ccc6cda3 | 4087 | |
bb70624e JA |
4088 | @item -v |
4089 | Print shell input lines as they are read. | |
ccc6cda3 | 4090 | |
bb70624e | 4091 | @item -x |
b80f6443 JA |
4092 | Print a trace of simple commands, \fBfor\fP commands, \fBcase\fP |
4093 | commands, \fBselect\fP commands, and arithmetic \fBfor\fP commands | |
4094 | and their arguments or associated word lists after they are | |
4095 | expanded and before they are executed. The value of the @env{PS4} | |
4096 | variable is expanded and the resultant value is printed before | |
4097 | the command and its expanded arguments. | |
ccc6cda3 | 4098 | |
bb70624e JA |
4099 | @item -B |
4100 | The shell will perform brace expansion (@pxref{Brace Expansion}). | |
4101 | This option is on by default. | |
ccc6cda3 | 4102 | |
bb70624e JA |
4103 | @item -C |
4104 | Prevent output redirection using @samp{>}, @samp{>&}, and @samp{<>} | |
4105 | from overwriting existing files. | |
ccc6cda3 | 4106 | |
b80f6443 JA |
4107 | @item -E |
4108 | If set, any trap on @code{ERR} is inherited by shell functions, command | |
4109 | substitutions, and commands executed in a subshell environment. | |
4110 | The @code{ERR} trap is normally not inherited in such cases. | |
4111 | ||
bb70624e JA |
4112 | @item -H |
4113 | Enable @samp{!} style history substitution (@pxref{History Interaction}). | |
4114 | This option is on by default for interactive shells. | |
ccc6cda3 | 4115 | |
bb70624e JA |
4116 | @item -P |
4117 | If set, do not follow symbolic links when performing commands such as | |
4118 | @code{cd} which change the current directory. The physical directory | |
4119 | is used instead. By default, Bash follows | |
4120 | the logical chain of directories when performing commands | |
4121 | which change the current directory. | |
ccc6cda3 | 4122 | |
bb70624e JA |
4123 | For example, if @file{/usr/sys} is a symbolic link to @file{/usr/local/sys} |
4124 | then: | |
4125 | @example | |
4126 | $ cd /usr/sys; echo $PWD | |
4127 | /usr/sys | |
4128 | $ cd ..; pwd | |
4129 | /usr | |
4130 | @end example | |
ccc6cda3 | 4131 | |
bb70624e JA |
4132 | @noindent |
4133 | If @code{set -P} is on, then: | |
4134 | @example | |
4135 | $ cd /usr/sys; echo $PWD | |
4136 | /usr/local/sys | |
4137 | $ cd ..; pwd | |
4138 | /usr/local | |
4139 | @end example | |
ccc6cda3 | 4140 | |
b80f6443 | 4141 | @item -T |
95732b49 JA |
4142 | If set, any trap on @code{DEBUG} and @code{RETURN} are inherited by |
4143 | shell functions, command substitutions, and commands executed | |
4144 | in a subshell environment. | |
4145 | The @code{DEBUG} and @code{RETURN} traps are normally not inherited | |
4146 | in such cases. | |
b80f6443 | 4147 | |
bb70624e JA |
4148 | @item -- |
4149 | If no arguments follow this option, then the positional parameters are | |
4150 | unset. Otherwise, the positional parameters are set to the | |
4151 | @var{arguments}, even if some of them begin with a @samp{-}. | |
4152 | ||
4153 | @item - | |
4154 | Signal the end of options, cause all remaining @var{arguments} | |
28ef6c31 JA |
4155 | to be assigned to the positional parameters. The @option{-x} |
4156 | and @option{-v} options are turned off. | |
bb70624e | 4157 | If there are no arguments, the positional parameters remain unchanged. |
ccc6cda3 JA |
4158 | @end table |
4159 | ||
bb70624e JA |
4160 | Using @samp{+} rather than @samp{-} causes these options to be |
4161 | turned off. The options can also be used upon invocation of the | |
4162 | shell. The current set of options may be found in @code{$-}. | |
cce855bc | 4163 | |
bb70624e JA |
4164 | The remaining N @var{arguments} are positional parameters and are |
4165 | assigned, in order, to @code{$1}, @code{$2}, @dots{} @code{$N}. | |
4166 | The special parameter @code{#} is set to N. | |
ccc6cda3 | 4167 | |
bb70624e | 4168 | The return status is always zero unless an invalid option is supplied. |
ccc6cda3 JA |
4169 | @end table |
4170 | ||
bb70624e JA |
4171 | @node Special Builtins |
4172 | @section Special Builtins | |
4173 | @cindex special builtin | |
cce855bc | 4174 | |
bb70624e JA |
4175 | For historical reasons, the @sc{posix} 1003.2 standard has classified |
4176 | several builtin commands as @emph{special}. | |
4177 | When Bash is executing in @sc{posix} mode, the special builtins | |
4178 | differ from other builtin commands in three respects: | |
ccc6cda3 | 4179 | |
bb70624e JA |
4180 | @enumerate |
4181 | @item | |
4182 | Special builtins are found before shell functions during command lookup. | |
cce855bc | 4183 | |
bb70624e JA |
4184 | @item |
4185 | If a special builtin returns an error status, a non-interactive shell exits. | |
cce855bc | 4186 | |
bb70624e JA |
4187 | @item |
4188 | Assignment statements preceding the command stay in effect in the shell | |
4189 | environment after the command completes. | |
4190 | @end enumerate | |
ccc6cda3 | 4191 | |
bb70624e JA |
4192 | When Bash is not executing in @sc{posix} mode, these builtins behave no |
4193 | differently than the rest of the Bash builtin commands. | |
4194 | The Bash @sc{posix} mode is described in @ref{Bash POSIX Mode}. | |
ccc6cda3 | 4195 | |
bb70624e | 4196 | These are the @sc{posix} special builtins: |
ccc6cda3 | 4197 | @example |
bb70624e JA |
4198 | @w{break : . continue eval exec exit export readonly return set} |
4199 | @w{shift trap unset} | |
ccc6cda3 | 4200 | @end example |
ccc6cda3 | 4201 | |
bb70624e JA |
4202 | @node Shell Variables |
4203 | @chapter Shell Variables | |
cce855bc | 4204 | |
bb70624e JA |
4205 | @menu |
4206 | * Bourne Shell Variables:: Variables which Bash uses in the same way | |
4207 | as the Bourne Shell. | |
4208 | * Bash Variables:: List of variables that exist in Bash. | |
4209 | @end menu | |
cce855bc | 4210 | |
bb70624e JA |
4211 | This chapter describes the shell variables that Bash uses. |
4212 | Bash automatically assigns default values to a number of variables. | |
ccc6cda3 | 4213 | |
bb70624e JA |
4214 | @node Bourne Shell Variables |
4215 | @section Bourne Shell Variables | |
ccc6cda3 | 4216 | |
bb70624e JA |
4217 | Bash uses certain shell variables in the same way as the Bourne shell. |
4218 | In some cases, Bash assigns a default value to the variable. | |
ccc6cda3 | 4219 | |
bb70624e | 4220 | @vtable @code |
ccc6cda3 | 4221 | |
bb70624e JA |
4222 | @item CDPATH |
4223 | A colon-separated list of directories used as a search path for | |
4224 | the @code{cd} builtin command. | |
ccc6cda3 | 4225 | |
bb70624e JA |
4226 | @item HOME |
4227 | The current user's home directory; the default for the @code{cd} builtin | |
4228 | command. | |
4229 | The value of this variable is also used by tilde expansion | |
4230 | (@pxref{Tilde Expansion}). | |
ccc6cda3 | 4231 | |
bb70624e JA |
4232 | @item IFS |
4233 | A list of characters that separate fields; used when the shell splits | |
4234 | words as part of expansion. | |
ccc6cda3 | 4235 | |
bb70624e | 4236 | @item MAIL |
28ef6c31 | 4237 | If this parameter is set to a filename and the @env{MAILPATH} variable |
bb70624e JA |
4238 | is not set, Bash informs the user of the arrival of mail in |
4239 | the specified file. | |
ccc6cda3 | 4240 | |
bb70624e JA |
4241 | @item MAILPATH |
4242 | A colon-separated list of filenames which the shell periodically checks | |
4243 | for new mail. | |
4244 | Each list entry can specify the message that is printed when new mail | |
4245 | arrives in the mail file by separating the file name from the message with | |
4246 | a @samp{?}. | |
4247 | When used in the text of the message, @code{$_} expands to the name of | |
4248 | the current mail file. | |
ccc6cda3 | 4249 | |
bb70624e JA |
4250 | @item OPTARG |
4251 | The value of the last option argument processed by the @code{getopts} builtin. | |
ccc6cda3 | 4252 | |
bb70624e JA |
4253 | @item OPTIND |
4254 | The index of the last option argument processed by the @code{getopts} builtin. | |
ccc6cda3 | 4255 | |
bb70624e JA |
4256 | @item PATH |
4257 | A colon-separated list of directories in which the shell looks for | |
4258 | commands. | |
b80f6443 JA |
4259 | A zero-length (null) directory name in the value of @code{PATH} indicates the |
4260 | current directory. | |
4261 | A null directory name may appear as two adjacent colons, or as an initial | |
4262 | or trailing colon. | |
4263 | ||
ccc6cda3 | 4264 | |
bb70624e JA |
4265 | @item PS1 |
4266 | The primary prompt string. The default value is @samp{\s-\v\$ }. | |
4267 | @xref{Printing a Prompt}, for the complete list of escape | |
28ef6c31 | 4268 | sequences that are expanded before @env{PS1} is displayed. |
ccc6cda3 | 4269 | |
bb70624e JA |
4270 | @item PS2 |
4271 | The secondary prompt string. The default value is @samp{> }. | |
ccc6cda3 | 4272 | |
bb70624e | 4273 | @end vtable |
ccc6cda3 | 4274 | |
bb70624e JA |
4275 | @node Bash Variables |
4276 | @section Bash Variables | |
ccc6cda3 | 4277 | |
bb70624e JA |
4278 | These variables are set or used by Bash, but other shells |
4279 | do not normally treat them specially. | |
ccc6cda3 | 4280 | |
bb70624e JA |
4281 | A few variables used by Bash are described in different chapters: |
4282 | variables for controlling the job control facilities | |
4283 | (@pxref{Job Control Variables}). | |
ccc6cda3 | 4284 | |
bb70624e | 4285 | @vtable @code |
cce855bc | 4286 | |
bb70624e JA |
4287 | @item BASH |
4288 | The full pathname used to execute the current instance of Bash. | |
cce855bc | 4289 | |
b80f6443 JA |
4290 | @item BASH_ARGC |
4291 | An array variable whose values are the number of parameters in each | |
4292 | frame of the current bash execution call stack. The number of | |
4293 | parameters to the current subroutine (shell function or script executed | |
4294 | with @code{.} or @code{source}) is at the top of the stack. When a | |
4295 | subroutine is executed, the number of parameters passed is pushed onto | |
4296 | @code{BASH_ARGC}. | |
95732b49 JA |
4297 | The shell sets @code{BASH_ARGC} only when in extended debugging mode |
4298 | (see @ref{Bash Builtins} | |
4299 | for a description of the @code{extdebug} option to the @code{shopt} | |
4300 | builtin). | |
b80f6443 JA |
4301 | |
4302 | @item BASH_ARGV | |
4303 | An array variable containing all of the parameters in the current bash | |
4304 | execution call stack. The final parameter of the last subroutine call | |
4305 | is at the top of the stack; the first parameter of the initial call is | |
4306 | at the bottom. When a subroutine is executed, the parameters supplied | |
4307 | are pushed onto @code{BASH_ARGV}. | |
95732b49 JA |
4308 | The shell sets @code{BASH_ARGV} only when in extended debugging mode |
4309 | (see @ref{Bash Builtins} | |
4310 | for a description of the @code{extdebug} option to the @code{shopt} | |
4311 | builtin). | |
b80f6443 JA |
4312 | |
4313 | @item BASH_COMMAND | |
4314 | The command currently being executed or about to be executed, unless the | |
4315 | shell is executing a command as the result of a trap, | |
4316 | in which case it is the command executing at the time of the trap. | |
4317 | ||
bb70624e JA |
4318 | @item BASH_ENV |
4319 | If this variable is set when Bash is invoked to execute a shell | |
4320 | script, its value is expanded and used as the name of a startup file | |
4321 | to read before executing the script. @xref{Bash Startup Files}. | |
ccc6cda3 | 4322 | |
b80f6443 JA |
4323 | @item BASH_EXECUTION_STRING |
4324 | The command argument to the @option{-c} invocation option. | |
4325 | ||
4326 | @item BASH_LINENO | |
4327 | An array variable whose members are the line numbers in source files | |
4328 | corresponding to each member of @var{FUNCNAME}. | |
4329 | @code{$@{BASH_LINENO[$i]@}} is the line number in the source file where | |
95732b49 JA |
4330 | @code{$@{FUNCNAME[$i]@}} was called. |
4331 | The corresponding source file name is @code{$@{BASH_SOURCE[$i]@}}. | |
b80f6443 JA |
4332 | Use @code{LINENO} to obtain the current line number. |
4333 | ||
4334 | @item BASH_REMATCH | |
4335 | An array variable whose members are assigned by the @samp{=~} binary | |
4336 | operator to the @code{[[} conditional command | |
4337 | (@pxref{Conditional Constructs}). | |
4338 | The element with index 0 is the portion of the string | |
4339 | matching the entire regular expression. | |
4340 | The element with index @var{n} is the portion of the | |
4341 | string matching the @var{n}th parenthesized subexpression. | |
4342 | This variable is read-only. | |
4343 | ||
4344 | @item BASH_SOURCE | |
4345 | An array variable whose members are the source filenames corresponding | |
4346 | to the elements in the @code{FUNCNAME} array variable. | |
4347 | ||
4348 | @item BASH_SUBSHELL | |
4349 | Incremented by one each time a subshell or subshell environment is spawned. | |
4350 | The initial value is 0. | |
ccc6cda3 | 4351 | |
bb70624e JA |
4352 | @item BASH_VERSINFO |
4353 | A readonly array variable (@pxref{Arrays}) | |
4354 | whose members hold version information for this instance of Bash. | |
4355 | The values assigned to the array members are as follows: | |
ccc6cda3 | 4356 | |
bb70624e | 4357 | @table @code |
ccc6cda3 | 4358 | |
bb70624e JA |
4359 | @item BASH_VERSINFO[0] |
4360 | The major version number (the @var{release}). | |
cce855bc | 4361 | |
bb70624e JA |
4362 | @item BASH_VERSINFO[1] |
4363 | The minor version number (the @var{version}). | |
ccc6cda3 | 4364 | |
bb70624e JA |
4365 | @item BASH_VERSINFO[2] |
4366 | The patch level. | |
ccc6cda3 | 4367 | |
bb70624e JA |
4368 | @item BASH_VERSINFO[3] |
4369 | The build version. | |
ccc6cda3 | 4370 | |
bb70624e JA |
4371 | @item BASH_VERSINFO[4] |
4372 | The release status (e.g., @var{beta1}). | |
cce855bc | 4373 | |
bb70624e | 4374 | @item BASH_VERSINFO[5] |
28ef6c31 | 4375 | The value of @env{MACHTYPE}. |
ccc6cda3 | 4376 | |
bb70624e | 4377 | @end table |
ccc6cda3 | 4378 | |
b80f6443 JA |
4379 | @item BASH_VERSION |
4380 | The version number of the current instance of Bash. | |
4381 | ||
f73dda09 JA |
4382 | @item COLUMNS |
4383 | Used by the @code{select} builtin command to determine the terminal width | |
4384 | when printing selection lists. Automatically set upon receipt of a | |
4385 | @code{SIGWINCH}. | |
bb70624e JA |
4386 | |
4387 | @item COMP_CWORD | |
28ef6c31 | 4388 | An index into @env{$@{COMP_WORDS@}} of the word containing the current |
bb70624e JA |
4389 | cursor position. |
4390 | This variable is available only in shell functions invoked by the | |
4391 | programmable completion facilities (@pxref{Programmable Completion}). | |
4392 | ||
4393 | @item COMP_LINE | |
4394 | The current command line. | |
4395 | This variable is available only in shell functions and external | |
4396 | commands invoked by the | |
4397 | programmable completion facilities (@pxref{Programmable Completion}). | |
4398 | ||
4399 | @item COMP_POINT | |
4400 | The index of the current cursor position relative to the beginning of | |
4401 | the current command. | |
4402 | If the current cursor position is at the end of the current command, | |
4403 | the value of this variable is equal to @code{$@{#COMP_LINE@}}. | |
4404 | This variable is available only in shell functions and external | |
4405 | commands invoked by the | |
4406 | programmable completion facilities (@pxref{Programmable Completion}). | |
4407 | ||
b80f6443 JA |
4408 | @item COMP_WORDBREAKS |
4409 | The set of characters that the Readline library treats as word | |
4410 | separators when performing word completion. | |
4411 | If @code{COMP_WORDBREAKS} is unset, it loses its special properties, | |
4412 | even if it is subsequently reset. | |
4413 | ||
f73dda09 JA |
4414 | @item COMP_WORDS |
4415 | An array variable consisting of the individual | |
4416 | words in the current command line. | |
4417 | This variable is available only in shell functions invoked by the | |
4418 | programmable completion facilities (@pxref{Programmable Completion}). | |
4419 | ||
bb70624e JA |
4420 | @item COMPREPLY |
4421 | An array variable from which Bash reads the possible completions | |
4422 | generated by a shell function invoked by the programmable completion | |
4423 | facility (@pxref{Programmable Completion}). | |
b72432fd | 4424 | |
bb70624e JA |
4425 | @item DIRSTACK |
4426 | An array variable containing the current contents of the directory stack. | |
4427 | Directories appear in the stack in the order they are displayed by the | |
4428 | @code{dirs} builtin. | |
4429 | Assigning to members of this array variable may be used to modify | |
4430 | directories already in the stack, but the @code{pushd} and @code{popd} | |
4431 | builtins must be used to add and remove directories. | |
4432 | Assignment to this variable will not change the current directory. | |
28ef6c31 | 4433 | If @env{DIRSTACK} is unset, it loses its special properties, even if |
bb70624e | 4434 | it is subsequently reset. |
ccc6cda3 | 4435 | |
b80f6443 JA |
4436 | @item EMACS |
4437 | If Bash finds this variable in the environment when the shell | |
4438 | starts with value @samp{t}, it assumes that the shell is running in an | |
4439 | emacs shell buffer and disables line editing. | |
4440 | ||
bb70624e JA |
4441 | @item EUID |
4442 | The numeric effective user id of the current user. This variable | |
4443 | is readonly. | |
ccc6cda3 | 4444 | |
bb70624e | 4445 | @item FCEDIT |
28ef6c31 | 4446 | The editor used as a default by the @option{-e} option to the @code{fc} |
bb70624e | 4447 | builtin command. |
cce855bc | 4448 | |
bb70624e JA |
4449 | @item FIGNORE |
4450 | A colon-separated list of suffixes to ignore when performing | |
4451 | filename completion. | |
4452 | A file name whose suffix matches one of the entries in | |
28ef6c31 | 4453 | @env{FIGNORE} |
bb70624e JA |
4454 | is excluded from the list of matched file names. A sample |
4455 | value is @samp{.o:~} | |
cce855bc | 4456 | |
f73dda09 | 4457 | @item FUNCNAME |
b80f6443 JA |
4458 | An array variable containing the names of all shell functions |
4459 | currently in the execution call stack. | |
4460 | The element with index 0 is the name of any currently-executing | |
4461 | shell function. | |
4462 | The bottom-most element is "main". | |
f73dda09 JA |
4463 | This variable exists only when a shell function is executing. |
4464 | Assignments to @env{FUNCNAME} have no effect and return an error status. | |
4465 | If @env{FUNCNAME} is unset, it loses its special properties, even if | |
4466 | it is subsequently reset. | |
4467 | ||
bb70624e JA |
4468 | @item GLOBIGNORE |
4469 | A colon-separated list of patterns defining the set of filenames to | |
4470 | be ignored by filename expansion. | |
4471 | If a filename matched by a filename expansion pattern also matches one | |
28ef6c31 | 4472 | of the patterns in @env{GLOBIGNORE}, it is removed from the list |
bb70624e | 4473 | of matches. |
ccc6cda3 | 4474 | |
bb70624e JA |
4475 | @item GROUPS |
4476 | An array variable containing the list of groups of which the current | |
4477 | user is a member. | |
28ef6c31 JA |
4478 | Assignments to @env{GROUPS} have no effect and return an error status. |
4479 | If @env{GROUPS} is unset, it loses its special properties, even if it is | |
bb70624e | 4480 | subsequently reset. |
ccc6cda3 | 4481 | |
bb70624e JA |
4482 | @item histchars |
4483 | Up to three characters which control history expansion, quick | |
4484 | substitution, and tokenization (@pxref{History Interaction}). | |
4485 | The first character is the | |
4486 | @var{history expansion} character, that is, the character which signifies the | |
4487 | start of a history expansion, normally @samp{!}. The second character is the | |
4488 | character which signifies `quick substitution' when seen as the first | |
4489 | character on a line, normally @samp{^}. The optional third character is the | |
4490 | character which indicates that the remainder of the line is a comment when | |
4491 | found as the first character of a word, usually @samp{#}. The history | |
4492 | comment character causes history substitution to be skipped for the | |
4493 | remaining words on the line. It does not necessarily cause the shell | |
4494 | parser to treat the rest of the line as a comment. | |
ccc6cda3 | 4495 | |
bb70624e JA |
4496 | @item HISTCMD |
4497 | The history number, or index in the history list, of the current | |
28ef6c31 | 4498 | command. If @env{HISTCMD} is unset, it loses its special properties, |
bb70624e | 4499 | even if it is subsequently reset. |
ccc6cda3 | 4500 | |
bb70624e | 4501 | @item HISTCONTROL |
b80f6443 JA |
4502 | A colon-separated list of values controlling how commands are saved on |
4503 | the history list. | |
4504 | If the list of values includes @samp{ignorespace}, lines which begin | |
4505 | with a space character are not saved in the history list. | |
4506 | A value of @samp{ignoredups} causes lines which match the previous | |
4507 | history entry to not be saved. | |
4508 | A value of @samp{ignoreboth} is shorthand for | |
4509 | @samp{ignorespace} and @samp{ignoredups}. | |
4510 | A value of @samp{erasedups} causes all previous lines matching the | |
4511 | current line to be removed from the history list before that line | |
4512 | is saved. | |
4513 | Any value not in the above list is ignored. | |
4514 | If @env{HISTCONTROL} is unset, or does not include a valid value, | |
4515 | all lines read by the shell parser are saved on the history list, | |
4516 | subject to the value of @env{HISTIGNORE}. | |
bb70624e JA |
4517 | The second and subsequent lines of a multi-line compound command are |
4518 | not tested, and are added to the history regardless of the value of | |
28ef6c31 | 4519 | @env{HISTCONTROL}. |
ccc6cda3 | 4520 | |
f73dda09 JA |
4521 | @item HISTFILE |
4522 | The name of the file to which the command history is saved. The | |
4523 | default value is @file{~/.bash_history}. | |
4524 | ||
4525 | @item HISTFILESIZE | |
4526 | The maximum number of lines contained in the history file. When this | |
4527 | variable is assigned a value, the history file is truncated, if | |
4528 | necessary, to contain no more than that number of lines. | |
4529 | The history file is also truncated to this size after | |
4530 | writing it when an interactive shell exits. | |
4531 | The default value is 500. | |
4532 | ||
bb70624e JA |
4533 | @item HISTIGNORE |
4534 | A colon-separated list of patterns used to decide which command | |
4535 | lines should be saved on the history list. Each pattern is | |
4536 | anchored at the beginning of the line and must match the complete | |
4537 | line (no implicit @samp{*} is appended). Each pattern is tested | |
28ef6c31 | 4538 | against the line after the checks specified by @env{HISTCONTROL} |
bb70624e JA |
4539 | are applied. In addition to the normal shell pattern matching |
4540 | characters, @samp{&} matches the previous history line. @samp{&} | |
4541 | may be escaped using a backslash; the backslash is removed | |
4542 | before attempting a match. | |
4543 | The second and subsequent lines of a multi-line compound command are | |
4544 | not tested, and are added to the history regardless of the value of | |
28ef6c31 | 4545 | @env{HISTIGNORE}. |
ccc6cda3 | 4546 | |
28ef6c31 | 4547 | @env{HISTIGNORE} subsumes the function of @env{HISTCONTROL}. A |
bb70624e JA |
4548 | pattern of @samp{&} is identical to @code{ignoredups}, and a |
4549 | pattern of @samp{[ ]*} is identical to @code{ignorespace}. | |
4550 | Combining these two patterns, separating them with a colon, | |
4551 | provides the functionality of @code{ignoreboth}. | |
ccc6cda3 | 4552 | |
bb70624e JA |
4553 | @item HISTSIZE |
4554 | The maximum number of commands to remember on the history list. | |
4555 | The default value is 500. | |
ccc6cda3 | 4556 | |
b80f6443 JA |
4557 | @item HISTTIMEFORMAT |
4558 | If this variable is set and not null, its value is used as a format string | |
4559 | for @var{strftime} to print the time stamp associated with each history | |
4560 | entry displayed by the @code{history} builtin. | |
4561 | If this variable is set, time stamps are written to the history file so | |
4562 | they may be preserved across shell sessions. | |
4563 | ||
bb70624e JA |
4564 | @item HOSTFILE |
4565 | Contains the name of a file in the same format as @file{/etc/hosts} that | |
4566 | should be read when the shell needs to complete a hostname. | |
4567 | The list of possible hostname completions may be changed while the shell | |
4568 | is running; | |
4569 | the next time hostname completion is attempted after the | |
4570 | value is changed, Bash adds the contents of the new file to the | |
4571 | existing list. | |
28ef6c31 | 4572 | If @env{HOSTFILE} is set, but has no value, Bash attempts to read |
bb70624e | 4573 | @file{/etc/hosts} to obtain the list of possible hostname completions. |
28ef6c31 | 4574 | When @env{HOSTFILE} is unset, the hostname list is cleared. |
ccc6cda3 | 4575 | |
bb70624e JA |
4576 | @item HOSTNAME |
4577 | The name of the current host. | |
ccc6cda3 | 4578 | |
bb70624e JA |
4579 | @item HOSTTYPE |
4580 | A string describing the machine Bash is running on. | |
ccc6cda3 | 4581 | |
bb70624e JA |
4582 | @item IGNOREEOF |
4583 | Controls the action of the shell on receipt of an @code{EOF} character | |
4584 | as the sole input. If set, the value denotes the number | |
4585 | of consecutive @code{EOF} characters that can be read as the | |
4586 | first character on an input line | |
4587 | before the shell will exit. If the variable exists but does not | |
4588 | have a numeric value (or has no value) then the default is 10. | |
4589 | If the variable does not exist, then @code{EOF} signifies the end of | |
4590 | input to the shell. This is only in effect for interactive shells. | |
ccc6cda3 | 4591 | |
bb70624e JA |
4592 | @item INPUTRC |
4593 | The name of the Readline initialization file, overriding the default | |
4594 | of @file{~/.inputrc}. | |
ccc6cda3 | 4595 | |
bb70624e JA |
4596 | @item LANG |
4597 | Used to determine the locale category for any category not specifically | |
4598 | selected with a variable starting with @code{LC_}. | |
ccc6cda3 | 4599 | |
bb70624e | 4600 | @item LC_ALL |
28ef6c31 | 4601 | This variable overrides the value of @env{LANG} and any other |
bb70624e | 4602 | @code{LC_} variable specifying a locale category. |
ccc6cda3 | 4603 | |
bb70624e JA |
4604 | @item LC_COLLATE |
4605 | This variable determines the collation order used when sorting the | |
4606 | results of filename expansion, and | |
4607 | determines the behavior of range expressions, equivalence classes, | |
4608 | and collating sequences within filename expansion and pattern matching | |
4609 | (@pxref{Filename Expansion}). | |
ccc6cda3 | 4610 | |
bb70624e JA |
4611 | @item LC_CTYPE |
4612 | This variable determines the interpretation of characters and the | |
4613 | behavior of character classes within filename expansion and pattern | |
4614 | matching (@pxref{Filename Expansion}). | |
ccc6cda3 | 4615 | |
bb70624e JA |
4616 | @item LC_MESSAGES |
4617 | This variable determines the locale used to translate double-quoted | |
4618 | strings preceded by a @samp{$} (@pxref{Locale Translation}). | |
ccc6cda3 | 4619 | |
bb70624e JA |
4620 | @item LC_NUMERIC |
4621 | This variable determines the locale category used for number formatting. | |
ccc6cda3 | 4622 | |
f73dda09 JA |
4623 | @item LINENO |
4624 | The line number in the script or shell function currently executing. | |
4625 | ||
28ef6c31 JA |
4626 | @item LINES |
4627 | Used by the @code{select} builtin command to determine the column length | |
4628 | for printing selection lists. Automatically set upon receipt of a | |
4629 | @code{SIGWINCH}. | |
4630 | ||
bb70624e JA |
4631 | @item MACHTYPE |
4632 | A string that fully describes the system type on which Bash | |
4633 | is executing, in the standard @sc{gnu} @var{cpu-company-system} format. | |
ccc6cda3 | 4634 | |
bb70624e JA |
4635 | @item MAILCHECK |
4636 | How often (in seconds) that the shell should check for mail in the | |
28ef6c31 JA |
4637 | files specified in the @env{MAILPATH} or @env{MAIL} variables. |
4638 | The default is 60 seconds. When it is time to check | |
4639 | for mail, the shell does so before displaying the primary prompt. | |
4640 | If this variable is unset, or set to a value that is not a number | |
4641 | greater than or equal to zero, the shell disables mail checking. | |
ccc6cda3 | 4642 | |
bb70624e JA |
4643 | @item OLDPWD |
4644 | The previous working directory as set by the @code{cd} builtin. | |
ccc6cda3 | 4645 | |
bb70624e JA |
4646 | @item OPTERR |
4647 | If set to the value 1, Bash displays error messages | |
4648 | generated by the @code{getopts} builtin command. | |
ccc6cda3 | 4649 | |
bb70624e JA |
4650 | @item OSTYPE |
4651 | A string describing the operating system Bash is running on. | |
cce855bc | 4652 | |
bb70624e JA |
4653 | @item PIPESTATUS |
4654 | An array variable (@pxref{Arrays}) | |
4655 | containing a list of exit status values from the processes | |
4656 | in the most-recently-executed foreground pipeline (which may | |
4657 | contain only a single command). | |
cce855bc | 4658 | |
f73dda09 JA |
4659 | @item POSIXLY_CORRECT |
4660 | If this variable is in the environment when @code{bash} starts, the shell | |
4661 | enters @sc{posix} mode (@pxref{Bash POSIX Mode}) before reading the | |
4662 | startup files, as if the @option{--posix} invocation option had been supplied. | |
4663 | If it is set while the shell is running, @code{bash} enables @sc{posix} mode, | |
4664 | as if the command | |
4665 | @example | |
4666 | @code{set -o posix} | |
4667 | @end example | |
4668 | @noindent | |
4669 | had been executed. | |
4670 | ||
bb70624e JA |
4671 | @item PPID |
4672 | The process @sc{id} of the shell's parent process. This variable | |
4673 | is readonly. | |
ccc6cda3 | 4674 | |
bb70624e JA |
4675 | @item PROMPT_COMMAND |
4676 | If set, the value is interpreted as a command to execute | |
28ef6c31 | 4677 | before the printing of each primary prompt (@env{$PS1}). |
ccc6cda3 | 4678 | |
bb70624e JA |
4679 | @item PS3 |
4680 | The value of this variable is used as the prompt for the | |
4681 | @code{select} command. If this variable is not set, the | |
4682 | @code{select} command prompts with @samp{#? } | |
ccc6cda3 | 4683 | |
bb70624e JA |
4684 | @item PS4 |
4685 | The value is the prompt printed before the command line is echoed | |
28ef6c31 JA |
4686 | when the @option{-x} option is set (@pxref{The Set Builtin}). |
4687 | The first character of @env{PS4} is replicated multiple times, as | |
bb70624e JA |
4688 | necessary, to indicate multiple levels of indirection. |
4689 | The default is @samp{+ }. | |
ccc6cda3 | 4690 | |
bb70624e JA |
4691 | @item PWD |
4692 | The current working directory as set by the @code{cd} builtin. | |
ccc6cda3 | 4693 | |
bb70624e JA |
4694 | @item RANDOM |
4695 | Each time this parameter is referenced, a random integer | |
4696 | between 0 and 32767 is generated. Assigning a value to this | |
4697 | variable seeds the random number generator. | |
ccc6cda3 | 4698 | |
bb70624e JA |
4699 | @item REPLY |
4700 | The default variable for the @code{read} builtin. | |
ccc6cda3 | 4701 | |
bb70624e JA |
4702 | @item SECONDS |
4703 | This variable expands to the number of seconds since the | |
4704 | shell was started. Assignment to this variable resets | |
4705 | the count to the value assigned, and the expanded value | |
4706 | becomes the value assigned plus the number of seconds | |
4707 | since the assignment. | |
ccc6cda3 | 4708 | |
b80f6443 JA |
4709 | @item SHELL |
4710 | The full pathname to the shell is kept in this environment variable. | |
4711 | If it is not set when the shell starts, | |
4712 | Bash assigns to it the full pathname of the current user's login shell. | |
4713 | ||
bb70624e JA |
4714 | @item SHELLOPTS |
4715 | A colon-separated list of enabled shell options. Each word in | |
28ef6c31 | 4716 | the list is a valid argument for the @option{-o} option to the |
bb70624e | 4717 | @code{set} builtin command (@pxref{The Set Builtin}). |
28ef6c31 | 4718 | The options appearing in @env{SHELLOPTS} are those reported |
bb70624e JA |
4719 | as @samp{on} by @samp{set -o}. |
4720 | If this variable is in the environment when Bash | |
4721 | starts up, each shell option in the list will be enabled before | |
4722 | reading any startup files. This variable is readonly. | |
ccc6cda3 | 4723 | |
bb70624e JA |
4724 | @item SHLVL |
4725 | Incremented by one each time a new instance of Bash is started. This is | |
4726 | intended to be a count of how deeply your Bash shells are nested. | |
ccc6cda3 | 4727 | |
bb70624e JA |
4728 | @item TIMEFORMAT |
4729 | The value of this parameter is used as a format string specifying | |
4730 | how the timing information for pipelines prefixed with the @code{time} | |
4731 | reserved word should be displayed. | |
4732 | The @samp{%} character introduces an | |
4733 | escape sequence that is expanded to a time value or other | |
4734 | information. | |
4735 | The escape sequences and their meanings are as | |
4736 | follows; the braces denote optional portions. | |
ccc6cda3 | 4737 | |
bb70624e | 4738 | @table @code |
ccc6cda3 | 4739 | |
bb70624e JA |
4740 | @item %% |
4741 | A literal @samp{%}. | |
ccc6cda3 | 4742 | |
bb70624e JA |
4743 | @item %[@var{p}][l]R |
4744 | The elapsed time in seconds. | |
ccc6cda3 | 4745 | |
bb70624e JA |
4746 | @item %[@var{p}][l]U |
4747 | The number of CPU seconds spent in user mode. | |
ccc6cda3 | 4748 | |
bb70624e JA |
4749 | @item %[@var{p}][l]S |
4750 | The number of CPU seconds spent in system mode. | |
ccc6cda3 | 4751 | |
bb70624e JA |
4752 | @item %P |
4753 | The CPU percentage, computed as (%U + %S) / %R. | |
4754 | @end table | |
ccc6cda3 | 4755 | |
bb70624e JA |
4756 | The optional @var{p} is a digit specifying the precision, the number of |
4757 | fractional digits after a decimal point. | |
4758 | A value of 0 causes no decimal point or fraction to be output. | |
4759 | At most three places after the decimal point may be specified; values | |
4760 | of @var{p} greater than 3 are changed to 3. | |
4761 | If @var{p} is not specified, the value 3 is used. | |
ccc6cda3 | 4762 | |
bb70624e JA |
4763 | The optional @code{l} specifies a longer format, including minutes, of |
4764 | the form @var{MM}m@var{SS}.@var{FF}s. | |
4765 | The value of @var{p} determines whether or not the fraction is included. | |
ccc6cda3 | 4766 | |
bb70624e JA |
4767 | If this variable is not set, Bash acts as if it had the value |
4768 | @example | |
4769 | @code{$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'} | |
4770 | @end example | |
4771 | If the value is null, no timing information is displayed. | |
4772 | A trailing newline is added when the format string is displayed. | |
ccc6cda3 | 4773 | |
bb70624e | 4774 | @item TMOUT |
7117c2d2 JA |
4775 | If set to a value greater than zero, @code{TMOUT} is treated as the |
4776 | default timeout for the @code{read} builtin (@pxref{Bash Builtins}). | |
4777 | The @code{select} command (@pxref{Conditional Constructs}) terminates | |
4778 | if input does not arrive after @code{TMOUT} seconds when input is coming | |
4779 | from a terminal. | |
4780 | ||
4781 | In an interative shell, the value is interpreted as | |
bb70624e JA |
4782 | the number of seconds to wait for input after issuing the primary |
4783 | prompt when the shell is interactive. | |
4784 | Bash terminates after that number of seconds if input does | |
4785 | not arrive. | |
ccc6cda3 | 4786 | |
95732b49 JA |
4787 | @item TMPDIR |
4788 | If set, Bash uses its value as the name of a directory in which | |
4789 | Bash creates temporary files for the shell's use. | |
4790 | ||
bb70624e JA |
4791 | @item UID |
4792 | The numeric real user id of the current user. This variable is readonly. | |
ccc6cda3 | 4793 | |
bb70624e | 4794 | @end vtable |
ccc6cda3 | 4795 | |
bb70624e JA |
4796 | @node Bash Features |
4797 | @chapter Bash Features | |
ccc6cda3 | 4798 | |
bb70624e | 4799 | This section describes features unique to Bash. |
ccc6cda3 | 4800 | |
bb70624e JA |
4801 | @menu |
4802 | * Invoking Bash:: Command line options that you can give | |
4803 | to Bash. | |
4804 | * Bash Startup Files:: When and how Bash executes scripts. | |
4805 | * Interactive Shells:: What an interactive shell is. | |
4806 | * Bash Conditional Expressions:: Primitives used in composing expressions for | |
4807 | the @code{test} builtin. | |
4808 | * Shell Arithmetic:: Arithmetic on shell variables. | |
4809 | * Aliases:: Substituting one command for another. | |
4810 | * Arrays:: Array Variables. | |
4811 | * The Directory Stack:: History of visited directories. | |
4812 | * Printing a Prompt:: Controlling the PS1 string. | |
4813 | * The Restricted Shell:: A more controlled mode of shell execution. | |
4814 | * Bash POSIX Mode:: Making Bash behave more closely to what | |
4815 | the POSIX standard specifies. | |
4816 | @end menu | |
ccc6cda3 | 4817 | |
bb70624e JA |
4818 | @node Invoking Bash |
4819 | @section Invoking Bash | |
ccc6cda3 | 4820 | |
bb70624e | 4821 | @example |
f73dda09 JA |
4822 | bash [long-opt] [-ir] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}] |
4823 | bash [long-opt] [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] -c @var{string} [@var{argument} @dots{}] | |
4824 | bash [long-opt] -s [-abefhkmnptuvxdBCDHP] [-o @var{option}] [-O @var{shopt_option}] [@var{argument} @dots{}] | |
bb70624e | 4825 | @end example |
ccc6cda3 | 4826 | |
bb70624e JA |
4827 | In addition to the single-character shell command-line options |
4828 | (@pxref{The Set Builtin}), there are several multi-character | |
4829 | options that you can use. These options must appear on the command | |
7117c2d2 | 4830 | line before the single-character options to be recognized. |
ccc6cda3 | 4831 | |
bb70624e | 4832 | @table @code |
b80f6443 JA |
4833 | @item --debugger |
4834 | Arrange for the debugger profile to be executed before the shell | |
4835 | starts. Turns on extended debugging mode (see @ref{Bash Builtins} | |
4836 | for a description of the @code{extdebug} option to the @code{shopt} | |
4837 | builtin) and shell function tracing | |
4838 | (see @ref{The Set Builtin} for a description of the @code{-o functrace} | |
4839 | option). | |
4840 | ||
bb70624e JA |
4841 | @item --dump-po-strings |
4842 | A list of all double-quoted strings preceded by @samp{$} | |
95732b49 | 4843 | is printed on the standard output |
bb70624e | 4844 | in the @sc{gnu} @code{gettext} PO (portable object) file format. |
28ef6c31 | 4845 | Equivalent to @option{-D} except for the output format. |
ccc6cda3 | 4846 | |
bb70624e | 4847 | @item --dump-strings |
28ef6c31 | 4848 | Equivalent to @option{-D}. |
ccc6cda3 | 4849 | |
bb70624e JA |
4850 | @item --help |
4851 | Display a usage message on standard output and exit sucessfully. | |
ccc6cda3 | 4852 | |
28ef6c31 JA |
4853 | @item --init-file @var{filename} |
4854 | @itemx --rcfile @var{filename} | |
4855 | Execute commands from @var{filename} (instead of @file{~/.bashrc}) | |
4856 | in an interactive shell. | |
4857 | ||
bb70624e | 4858 | @item --login |
7117c2d2 | 4859 | Equivalent to @option{-l}. |
ccc6cda3 | 4860 | |
bb70624e JA |
4861 | @item --noediting |
4862 | Do not use the @sc{gnu} Readline library (@pxref{Command Line Editing}) | |
4863 | to read command lines when the shell is interactive. | |
ccc6cda3 | 4864 | |
bb70624e JA |
4865 | @item --noprofile |
4866 | Don't load the system-wide startup file @file{/etc/profile} | |
4867 | or any of the personal initialization files | |
4868 | @file{~/.bash_profile}, @file{~/.bash_login}, or @file{~/.profile} | |
4869 | when Bash is invoked as a login shell. | |
ccc6cda3 | 4870 | |
bb70624e JA |
4871 | @item --norc |
4872 | Don't read the @file{~/.bashrc} initialization file in an | |
4873 | interactive shell. This is on by default if the shell is | |
4874 | invoked as @code{sh}. | |
ccc6cda3 | 4875 | |
bb70624e JA |
4876 | @item --posix |
4877 | Change the behavior of Bash where the default operation differs | |
4878 | from the @sc{posix} 1003.2 standard to match the standard. This | |
4879 | is intended to make Bash behave as a strict superset of that | |
4880 | standard. @xref{Bash POSIX Mode}, for a description of the Bash | |
4881 | @sc{posix} mode. | |
ccc6cda3 | 4882 | |
bb70624e JA |
4883 | @item --restricted |
4884 | Make the shell a restricted shell (@pxref{The Restricted Shell}). | |
ccc6cda3 | 4885 | |
bb70624e | 4886 | @item --verbose |
28ef6c31 | 4887 | Equivalent to @option{-v}. Print shell input lines as they're read. |
ccc6cda3 | 4888 | |
bb70624e JA |
4889 | @item --version |
4890 | Show version information for this instance of | |
4891 | Bash on the standard output and exit successfully. | |
ccc6cda3 | 4892 | |
ccc6cda3 JA |
4893 | @end table |
4894 | ||
bb70624e JA |
4895 | There are several single-character options that may be supplied at |
4896 | invocation which are not available with the @code{set} builtin. | |
cce855bc | 4897 | |
bb70624e JA |
4898 | @table @code |
4899 | @item -c @var{string} | |
4900 | Read and execute commands from @var{string} after processing the | |
4901 | options, then exit. Any remaining arguments are assigned to the | |
4902 | positional parameters, starting with @code{$0}. | |
ccc6cda3 | 4903 | |
bb70624e JA |
4904 | @item -i |
4905 | Force the shell to run interactively. Interactive shells are | |
4906 | described in @ref{Interactive Shells}. | |
ccc6cda3 | 4907 | |
7117c2d2 JA |
4908 | @item -l |
4909 | Make this shell act as if it had been directly invoked by login. | |
4910 | When the shell is interactive, this is equivalent to starting a | |
4911 | login shell with @samp{exec -l bash}. | |
4912 | When the shell is not interactive, the login shell startup files will | |
4913 | be executed. | |
4914 | @samp{exec bash -l} or @samp{exec bash --login} | |
4915 | will replace the current shell with a Bash login shell. | |
4916 | @xref{Bash Startup Files}, for a description of the special behavior | |
4917 | of a login shell. | |
4918 | ||
bb70624e JA |
4919 | @item -r |
4920 | Make the shell a restricted shell (@pxref{The Restricted Shell}). | |
ccc6cda3 | 4921 | |
bb70624e JA |
4922 | @item -s |
4923 | If this option is present, or if no arguments remain after option | |
4924 | processing, then commands are read from the standard input. | |
4925 | This option allows the positional parameters to be set | |
4926 | when invoking an interactive shell. | |
4927 | ||
4928 | @item -D | |
4929 | A list of all double-quoted strings preceded by @samp{$} | |
95732b49 | 4930 | is printed on the standard output. |
bb70624e JA |
4931 | These are the strings that |
4932 | are subject to language translation when the current locale | |
4933 | is not @code{C} or @code{POSIX} (@pxref{Locale Translation}). | |
28ef6c31 | 4934 | This implies the @option{-n} option; no commands will be executed. |
bb70624e | 4935 | |
f73dda09 JA |
4936 | @item [-+]O [@var{shopt_option}] |
4937 | @var{shopt_option} is one of the shell options accepted by the | |
4938 | @code{shopt} builtin (@pxref{Shell Builtin Commands}). | |
4939 | If @var{shopt_option} is present, @option{-O} sets the value of that option; | |
4940 | @option{+O} unsets it. | |
4941 | If @var{shopt_option} is not supplied, the names and values of the shell | |
4942 | options accepted by @code{shopt} are printed on the standard output. | |
4943 | If the invocation option is @option{+O}, the output is displayed in a format | |
4944 | that may be reused as input. | |
4945 | ||
bb70624e JA |
4946 | @item -- |
4947 | A @code{--} signals the end of options and disables further option | |
4948 | processing. | |
4949 | Any arguments after the @code{--} are treated as filenames and arguments. | |
4950 | ||
4951 | @end table | |
4952 | ||
f73dda09 JA |
4953 | @cindex login shell |
4954 | A @emph{login} shell is one whose first character of argument zero is | |
4955 | @samp{-}, or one invoked with the @option{--login} option. | |
4956 | ||
bb70624e JA |
4957 | @cindex interactive shell |
4958 | An @emph{interactive} shell is one started without non-option arguments, | |
28ef6c31 JA |
4959 | unless @option{-s} is specified, |
4960 | without specifying the @option{-c} option, and whose input and output are both | |
bb70624e | 4961 | connected to terminals (as determined by @code{isatty(3)}), or one |
28ef6c31 | 4962 | started with the @option{-i} option. @xref{Interactive Shells}, for more |
bb70624e JA |
4963 | information. |
4964 | ||
4965 | If arguments remain after option processing, and neither the | |
28ef6c31 | 4966 | @option{-c} nor the @option{-s} |
bb70624e JA |
4967 | option has been supplied, the first argument is assumed to |
4968 | be the name of a file containing shell commands (@pxref{Shell Scripts}). | |
4969 | When Bash is invoked in this fashion, @code{$0} | |
4970 | is set to the name of the file, and the positional parameters | |
4971 | are set to the remaining arguments. | |
4972 | Bash reads and executes commands from this file, then exits. | |
4973 | Bash's exit status is the exit status of the last command executed | |
4974 | in the script. If no commands are executed, the exit status is 0. | |
4975 | ||
4976 | @node Bash Startup Files | |
4977 | @section Bash Startup Files | |
4978 | @cindex startup files | |
4979 | ||
4980 | This section describs how Bash executes its startup files. | |
4981 | If any of the files exist but cannot be read, Bash reports an error. | |
4982 | Tildes are expanded in file names as described above under | |
4983 | Tilde Expansion (@pxref{Tilde Expansion}). | |
4984 | ||
4985 | Interactive shells are described in @ref{Interactive Shells}. | |
4986 | ||
28ef6c31 | 4987 | @subsubheading Invoked as an interactive login shell, or with @option{--login} |
bb70624e JA |
4988 | |
4989 | When Bash is invoked as an interactive login shell, or as a | |
28ef6c31 | 4990 | non-interactive shell with the @option{--login} option, it first reads and |
bb70624e JA |
4991 | executes commands from the file @file{/etc/profile}, if that file exists. |
4992 | After reading that file, it looks for @file{~/.bash_profile}, | |
4993 | @file{~/.bash_login}, and @file{~/.profile}, in that order, and reads | |
4994 | and executes commands from the first one that exists and is readable. | |
28ef6c31 | 4995 | The @option{--noprofile} option may be used when the shell is started to |
bb70624e JA |
4996 | inhibit this behavior. |
4997 | ||
4998 | When a login shell exits, Bash reads and executes commands from | |
4999 | the file @file{~/.bash_logout}, if it exists. | |
5000 | ||
5001 | @subsubheading Invoked as an interactive non-login shell | |
5002 | ||
5003 | When an interactive shell that is not a login shell is started, Bash | |
5004 | reads and executes commands from @file{~/.bashrc}, if that file exists. | |
28ef6c31 JA |
5005 | This may be inhibited by using the @option{--norc} option. |
5006 | The @option{--rcfile @var{file}} option will force Bash to read and | |
bb70624e JA |
5007 | execute commands from @var{file} instead of @file{~/.bashrc}. |
5008 | ||
5009 | So, typically, your @file{~/.bash_profile} contains the line | |
5010 | @example | |
5011 | @code{if [ -f ~/.bashrc ]; then . ~/.bashrc; fi} | |
5012 | @end example | |
5013 | @noindent | |
5014 | after (or before) any login-specific initializations. | |
5015 | ||
5016 | @subsubheading Invoked non-interactively | |
5017 | ||
5018 | When Bash is started non-interactively, to run a shell script, | |
28ef6c31 | 5019 | for example, it looks for the variable @env{BASH_ENV} in the environment, |
bb70624e JA |
5020 | expands its value if it appears there, and uses the expanded value as |
5021 | the name of a file to read and execute. Bash behaves as if the | |
5022 | following command were executed: | |
5023 | @example | |
5024 | @code{if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi} | |
5025 | @end example | |
5026 | @noindent | |
28ef6c31 | 5027 | but the value of the @env{PATH} variable is not used to search for the |
bb70624e JA |
5028 | file name. |
5029 | ||
28ef6c31 JA |
5030 | As noted above, if a non-interactive shell is invoked with the |
5031 | @option{--login} option, Bash attempts to read and execute commands from the | |
5032 | login shell startup files. | |
5033 | ||
bb70624e JA |
5034 | @subsubheading Invoked with name @code{sh} |
5035 | ||
5036 | If Bash is invoked with the name @code{sh}, it tries to mimic the | |
5037 | startup behavior of historical versions of @code{sh} as closely as | |
5038 | possible, while conforming to the @sc{posix} standard as well. | |
5039 | ||
5040 | When invoked as an interactive login shell, or as a non-interactive | |
28ef6c31 | 5041 | shell with the @option{--login} option, it first attempts to read |
bb70624e JA |
5042 | and execute commands from @file{/etc/profile} and @file{~/.profile}, in |
5043 | that order. | |
28ef6c31 | 5044 | The @option{--noprofile} option may be used to inhibit this behavior. |
bb70624e | 5045 | When invoked as an interactive shell with the name @code{sh}, Bash |
28ef6c31 | 5046 | looks for the variable @env{ENV}, expands its value if it is defined, |
bb70624e JA |
5047 | and uses the expanded value as the name of a file to read and execute. |
5048 | Since a shell invoked as @code{sh} does not attempt to read and execute | |
28ef6c31 | 5049 | commands from any other startup files, the @option{--rcfile} option has |
bb70624e JA |
5050 | no effect. |
5051 | A non-interactive shell invoked with the name @code{sh} does not attempt | |
5052 | to read any other startup files. | |
5053 | ||
5054 | When invoked as @code{sh}, Bash enters @sc{posix} mode after | |
5055 | the startup files are read. | |
5056 | ||
5057 | @subsubheading Invoked in @sc{posix} mode | |
5058 | ||
5059 | When Bash is started in @sc{posix} mode, as with the | |
28ef6c31 | 5060 | @option{--posix} command line option, it follows the @sc{posix} standard |
bb70624e | 5061 | for startup files. |
28ef6c31 | 5062 | In this mode, interactive shells expand the @env{ENV} variable |
bb70624e JA |
5063 | and commands are read and executed from the file whose name is the |
5064 | expanded value. | |
5065 | No other startup files are read. | |
5066 | ||
5067 | @subsubheading Invoked by remote shell daemon | |
5068 | ||
5069 | Bash attempts to determine when it is being run by the remote shell | |
5070 | daemon, usually @code{rshd}. If Bash determines it is being run by | |
5071 | rshd, it reads and executes commands from @file{~/.bashrc}, if that | |
5072 | file exists and is readable. | |
5073 | It will not do this if invoked as @code{sh}. | |
28ef6c31 JA |
5074 | The @option{--norc} option may be used to inhibit this behavior, and the |
5075 | @option{--rcfile} option may be used to force another file to be read, but | |
bb70624e JA |
5076 | @code{rshd} does not generally invoke the shell with those options or |
5077 | allow them to be specified. | |
5078 | ||
5079 | @subsubheading Invoked with unequal effective and real @sc{uid/gid}s | |
5080 | ||
5081 | If Bash is started with the effective user (group) id not equal to the | |
5082 | real user (group) id, and the @code{-p} option is not supplied, no startup | |
5083 | files are read, shell functions are not inherited from the environment, | |
28ef6c31 | 5084 | the @env{SHELLOPTS} variable, if it appears in the environment, is ignored, |
bb70624e JA |
5085 | and the effective user id is set to the real user id. |
5086 | If the @code{-p} option is supplied at invocation, the startup behavior is | |
5087 | the same, but the effective user id is not reset. | |
5088 | ||
5089 | @node Interactive Shells | |
5090 | @section Interactive Shells | |
5091 | @cindex interactive shell | |
5092 | @cindex shell, interactive | |
5093 | ||
5094 | @menu | |
5095 | * What is an Interactive Shell?:: What determines whether a shell is Interactive. | |
5096 | * Is this Shell Interactive?:: How to tell if a shell is interactive. | |
5097 | * Interactive Shell Behavior:: What changes in a interactive shell? | |
5098 | @end menu | |
5099 | ||
5100 | @node What is an Interactive Shell? | |
5101 | @subsection What is an Interactive Shell? | |
5102 | ||
5103 | An interactive shell | |
28ef6c31 JA |
5104 | is one started without non-option arguments, unless @option{-s} is |
5105 | specified, without specifiying the @option{-c} option, and | |
b80f6443 | 5106 | whose input and error output are both |
bb70624e | 5107 | connected to terminals (as determined by @code{isatty(3)}), |
28ef6c31 | 5108 | or one started with the @option{-i} option. |
bb70624e JA |
5109 | |
5110 | An interactive shell generally reads from and writes to a user's | |
5111 | terminal. | |
5112 | ||
28ef6c31 | 5113 | The @option{-s} invocation option may be used to set the positional parameters |
bb70624e JA |
5114 | when an interactive shell is started. |
5115 | ||
5116 | @node Is this Shell Interactive? | |
5117 | @subsection Is this Shell Interactive? | |
5118 | ||
5119 | To determine within a startup script whether or not Bash is | |
5120 | running interactively, | |
5121 | test the value of the @samp{-} special parameter. | |
5122 | It contains @code{i} when the shell is interactive. For example: | |
5123 | ||
5124 | @example | |
5125 | case "$-" in | |
5126 | *i*) echo This shell is interactive ;; | |
5127 | *) echo This shell is not interactive ;; | |
5128 | esac | |
5129 | @end example | |
5130 | ||
5131 | Alternatively, startup scripts may examine the variable | |
28ef6c31 | 5132 | @env{PS1}; it is unset in non-interactive shells, and set in |
bb70624e JA |
5133 | interactive shells. Thus: |
5134 | ||
5135 | @example | |
5136 | if [ -z "$PS1" ]; then | |
5137 | echo This shell is not interactive | |
5138 | else | |
5139 | echo This shell is interactive | |
5140 | fi | |
5141 | @end example | |
5142 | ||
5143 | @node Interactive Shell Behavior | |
5144 | @subsection Interactive Shell Behavior | |
5145 | ||
5146 | When the shell is running interactively, it changes its behavior in | |
5147 | several ways. | |
5148 | ||
5149 | @enumerate | |
5150 | @item | |
5151 | Startup files are read and executed as described in @ref{Bash Startup Files}. | |
5152 | ||
5153 | @item | |
5154 | Job Control (@pxref{Job Control}) is enabled by default. When job | |
5155 | control is in effect, Bash ignores the keyboard-generated job control | |
5156 | signals @code{SIGTTIN}, @code{SIGTTOU}, and @code{SIGTSTP}. | |
5157 | ||
5158 | @item | |
28ef6c31 JA |
5159 | Bash expands and displays @env{PS1} before reading the first line |
5160 | of a command, and expands and displays @env{PS2} before reading the | |
bb70624e JA |
5161 | second and subsequent lines of a multi-line command. |
5162 | ||
5163 | @item | |
28ef6c31 JA |
5164 | Bash executes the value of the @env{PROMPT_COMMAND} variable as a command |
5165 | before printing the primary prompt, @env{$PS1} | |
bb70624e JA |
5166 | (@pxref{Bash Variables}). |
5167 | ||
5168 | @item | |
5169 | Readline (@pxref{Command Line Editing}) is used to read commands from | |
5170 | the user's terminal. | |
5171 | ||
5172 | @item | |
5173 | Bash inspects the value of the @code{ignoreeof} option to @code{set -o} | |
5174 | instead of exiting immediately when it receives an @code{EOF} on its | |
5175 | standard input when reading a command (@pxref{The Set Builtin}). | |
5176 | ||
5177 | @item | |
5178 | Command history (@pxref{Bash History Facilities}) | |
5179 | and history expansion (@pxref{History Interaction}) | |
5180 | are enabled by default. | |
28ef6c31 | 5181 | Bash will save the command history to the file named by @env{$HISTFILE} |
bb70624e JA |
5182 | when an interactive shell exits. |
5183 | ||
5184 | @item | |
5185 | Alias expansion (@pxref{Aliases}) is performed by default. | |
5186 | ||
5187 | @item | |
5188 | In the absence of any traps, Bash ignores @code{SIGTERM} | |
5189 | (@pxref{Signals}). | |
5190 | ||
5191 | @item | |
5192 | In the absence of any traps, @code{SIGINT} is caught and handled | |
5193 | ((@pxref{Signals}). | |
5194 | @code{SIGINT} will interrupt some shell builtins. | |
5195 | ||
5196 | @item | |
5197 | An interactive login shell sends a @code{SIGHUP} to all jobs on exit | |
5198 | if the @code{hupoxexit} shell option has been enabled (@pxref{Signals}). | |
5199 | ||
5200 | @item | |
28ef6c31 | 5201 | The @option{-n} invocation option is ignored, and @samp{set -n} has |
bb70624e JA |
5202 | no effect (@pxref{The Set Builtin}). |
5203 | ||
5204 | @item | |
5205 | Bash will check for mail periodically, depending on the values of the | |
28ef6c31 | 5206 | @env{MAIL}, @env{MAILPATH}, and @env{MAILCHECK} shell variables |
bb70624e JA |
5207 | (@pxref{Bash Variables}). |
5208 | ||
5209 | @item | |
5210 | Expansion errors due to references to unbound shell variables after | |
5211 | @samp{set -u} has been enabled will not cause the shell to exit | |
5212 | (@pxref{The Set Builtin}). | |
5213 | ||
5214 | @item | |
5215 | The shell will not exit on expansion errors caused by @var{var} being unset | |
5216 | or null in @code{$@{@var{var}:?@var{word}@}} expansions | |
5217 | (@pxref{Shell Parameter Expansion}). | |
5218 | ||
5219 | @item | |
5220 | Redirection errors encountered by shell builtins will not cause the | |
5221 | shell to exit. | |
5222 | ||
5223 | @item | |
5224 | When running in @sc{posix} mode, a special builtin returning an error | |
5225 | status will not cause the shell to exit (@pxref{Bash POSIX Mode}). | |
95732b49 | 5226 | |
bb70624e JA |
5227 | @item |
5228 | A failed @code{exec} will not cause the shell to exit | |
5229 | (@pxref{Bourne Shell Builtins}). | |
5230 | ||
5231 | @item | |
5232 | Parser syntax errors will not cause the shell to exit. | |
5233 | ||
5234 | @item | |
5235 | Simple spelling correction for directory arguments to the @code{cd} | |
5236 | builtin is enabled by default (see the description of the @code{cdspell} | |
5237 | option to the @code{shopt} builtin in @ref{Bash Builtins}). | |
5238 | ||
5239 | @item | |
28ef6c31 | 5240 | The shell will check the value of the @env{TMOUT} variable and exit |
bb70624e | 5241 | if a command is not read within the specified number of seconds after |
28ef6c31 | 5242 | printing @env{$PS1} (@pxref{Bash Variables}). |
bb70624e JA |
5243 | |
5244 | @end enumerate | |
5245 | ||
5246 | @node Bash Conditional Expressions | |
5247 | @section Bash Conditional Expressions | |
5248 | @cindex expressions, conditional | |
5249 | ||
5250 | Conditional expressions are used by the @code{[[} compound command | |
5251 | and the @code{test} and @code{[} builtin commands. | |
5252 | ||
5253 | Expressions may be unary or binary. | |
5254 | Unary expressions are often used to examine the status of a file. | |
5255 | There are string operators and numeric comparison operators as well. | |
5256 | If the @var{file} argument to one of the primaries is of the form | |
5257 | @file{/dev/fd/@var{N}}, then file descriptor @var{N} is checked. | |
5258 | If the @var{file} argument to one of the primaries is one of | |
5259 | @file{/dev/stdin}, @file{/dev/stdout}, or @file{/dev/stderr}, file | |
5260 | descriptor 0, 1, or 2, respectively, is checked. | |
ccc6cda3 | 5261 | |
95732b49 JA |
5262 | Unless otherwise specified, primaries that operate on files follow symbolic |
5263 | links and operate on the target of the link, rather than the link itself. | |
5264 | ||
ccc6cda3 | 5265 | @table @code |
cce855bc JA |
5266 | @item -a @var{file} |
5267 | True if @var{file} exists. | |
5268 | ||
ccc6cda3 JA |
5269 | @item -b @var{file} |
5270 | True if @var{file} exists and is a block special file. | |
5271 | ||
5272 | @item -c @var{file} | |
5273 | True if @var{file} exists and is a character special file. | |
5274 | ||
5275 | @item -d @var{file} | |
5276 | True if @var{file} exists and is a directory. | |
5277 | ||
5278 | @item -e @var{file} | |
5279 | True if @var{file} exists. | |
5280 | ||
5281 | @item -f @var{file} | |
5282 | True if @var{file} exists and is a regular file. | |
5283 | ||
5284 | @item -g @var{file} | |
cce855bc | 5285 | True if @var{file} exists and its set-group-id bit is set. |
ccc6cda3 | 5286 | |
b72432fd JA |
5287 | @item -h @var{file} |
5288 | True if @var{file} exists and is a symbolic link. | |
5289 | ||
ccc6cda3 | 5290 | @item -k @var{file} |
cce855bc | 5291 | True if @var{file} exists and its "sticky" bit is set. |
ccc6cda3 JA |
5292 | |
5293 | @item -p @var{file} | |
cce855bc | 5294 | True if @var{file} exists and is a named pipe (FIFO). |
ccc6cda3 JA |
5295 | |
5296 | @item -r @var{file} | |
5297 | True if @var{file} exists and is readable. | |
5298 | ||
5299 | @item -s @var{file} | |
5300 | True if @var{file} exists and has a size greater than zero. | |
5301 | ||
ccc6cda3 | 5302 | @item -t @var{fd} |
cce855bc | 5303 | True if file descriptor @var{fd} is open and refers to a terminal. |
ccc6cda3 JA |
5304 | |
5305 | @item -u @var{file} | |
5306 | True if @var{file} exists and its set-user-id bit is set. | |
5307 | ||
5308 | @item -w @var{file} | |
5309 | True if @var{file} exists and is writable. | |
5310 | ||
5311 | @item -x @var{file} | |
5312 | True if @var{file} exists and is executable. | |
5313 | ||
5314 | @item -O @var{file} | |
5315 | True if @var{file} exists and is owned by the effective user id. | |
5316 | ||
5317 | @item -G @var{file} | |
5318 | True if @var{file} exists and is owned by the effective group id. | |
5319 | ||
cce855bc JA |
5320 | @item -L @var{file} |
5321 | True if @var{file} exists and is a symbolic link. | |
5322 | ||
5323 | @item -S @var{file} | |
5324 | True if @var{file} exists and is a socket. | |
5325 | ||
5326 | @item -N @var{file} | |
5327 | True if @var{file} exists and has been modified since it was last read. | |
5328 | ||
ccc6cda3 | 5329 | @item @var{file1} -nt @var{file2} |
7117c2d2 JA |
5330 | True if @var{file1} is newer (according to modification date) |
5331 | than @var{file2}, or if @var{file1} exists and @var{file2} does not. | |
ccc6cda3 JA |
5332 | |
5333 | @item @var{file1} -ot @var{file2} | |
7117c2d2 JA |
5334 | True if @var{file1} is older than @var{file2}, |
5335 | or if @var{file2} exists and @var{file1} does not. | |
ccc6cda3 JA |
5336 | |
5337 | @item @var{file1} -ef @var{file2} | |
7117c2d2 | 5338 | True if @var{file1} and @var{file2} refer to the same device and |
ccc6cda3 JA |
5339 | inode numbers. |
5340 | ||
5341 | @item -o @var{optname} | |
5342 | True if shell option @var{optname} is enabled. | |
28ef6c31 | 5343 | The list of options appears in the description of the @option{-o} |
ccc6cda3 JA |
5344 | option to the @code{set} builtin (@pxref{The Set Builtin}). |
5345 | ||
5346 | @item -z @var{string} | |
5347 | True if the length of @var{string} is zero. | |
5348 | ||
5349 | @item -n @var{string} | |
5350 | @itemx @var{string} | |
5351 | True if the length of @var{string} is non-zero. | |
5352 | ||
cce855bc JA |
5353 | @item @var{string1} == @var{string2} |
5354 | True if the strings are equal. | |
7117c2d2 | 5355 | @samp{=} may be used in place of @samp{==} for strict @sc{posix} compliance. |
ccc6cda3 JA |
5356 | |
5357 | @item @var{string1} != @var{string2} | |
5358 | True if the strings are not equal. | |
5359 | ||
5360 | @item @var{string1} < @var{string2} | |
cce855bc JA |
5361 | True if @var{string1} sorts before @var{string2} lexicographically |
5362 | in the current locale. | |
ccc6cda3 JA |
5363 | |
5364 | @item @var{string1} > @var{string2} | |
cce855bc JA |
5365 | True if @var{string1} sorts after @var{string2} lexicographically |
5366 | in the current locale. | |
ccc6cda3 JA |
5367 | |
5368 | @item @var{arg1} OP @var{arg2} | |
5369 | @code{OP} is one of | |
5370 | @samp{-eq}, @samp{-ne}, @samp{-lt}, @samp{-le}, @samp{-gt}, or @samp{-ge}. | |
5371 | These arithmetic binary operators return true if @var{arg1} | |
5372 | is equal to, not equal to, less than, less than or equal to, | |
5373 | greater than, or greater than or equal to @var{arg2}, | |
5374 | respectively. @var{Arg1} and @var{arg2} | |
5375 | may be positive or negative integers. | |
5376 | ||
5377 | @end table | |
5378 | ||
bb70624e JA |
5379 | @node Shell Arithmetic |
5380 | @section Shell Arithmetic | |
5381 | @cindex arithmetic, shell | |
5382 | @cindex shell arithmetic | |
5383 | @cindex expressions, arithmetic | |
5384 | @cindex evaluation, arithmetic | |
5385 | @cindex arithmetic evaluation | |
ccc6cda3 | 5386 | |
bb70624e | 5387 | The shell allows arithmetic expressions to be evaluated, as one of |
b80f6443 JA |
5388 | the shell expansions or by the @code{let} and the @option{-i} option |
5389 | to the @code{declare} builtins. | |
ccc6cda3 | 5390 | |
7117c2d2 | 5391 | Evaluation is done in fixed-width integers with no check for overflow, |
bb70624e | 5392 | though division by 0 is trapped and flagged as an error. |
b80f6443 JA |
5393 | The operators and their precedence, associativity, and values |
5394 | are the same as in the C language. | |
bb70624e JA |
5395 | The following list of operators is grouped into levels of |
5396 | equal-precedence operators. | |
5397 | The levels are listed in order of decreasing precedence. | |
cce855bc | 5398 | |
bb70624e | 5399 | @table @code |
d166f048 | 5400 | |
bb70624e JA |
5401 | @item @var{id}++ @var{id}-- |
5402 | variable post-increment and post-decrement | |
cce855bc | 5403 | |
bb70624e JA |
5404 | @item ++@var{id} --@var{id} |
5405 | variable pre-increment and pre-decrement | |
ccc6cda3 | 5406 | |
bb70624e JA |
5407 | @item - + |
5408 | unary minus and plus | |
ccc6cda3 | 5409 | |
bb70624e JA |
5410 | @item ! ~ |
5411 | logical and bitwise negation | |
ccc6cda3 | 5412 | |
bb70624e JA |
5413 | @item ** |
5414 | exponentiation | |
ccc6cda3 | 5415 | |
bb70624e JA |
5416 | @item * / % |
5417 | multiplication, division, remainder | |
ccc6cda3 | 5418 | |
bb70624e JA |
5419 | @item + - |
5420 | addition, subtraction | |
cce855bc | 5421 | |
bb70624e JA |
5422 | @item << >> |
5423 | left and right bitwise shifts | |
cce855bc | 5424 | |
bb70624e JA |
5425 | @item <= >= < > |
5426 | comparison | |
ccc6cda3 | 5427 | |
bb70624e JA |
5428 | @item == != |
5429 | equality and inequality | |
ccc6cda3 | 5430 | |
bb70624e JA |
5431 | @item & |
5432 | bitwise AND | |
ccc6cda3 | 5433 | |
bb70624e JA |
5434 | @item ^ |
5435 | bitwise exclusive OR | |
ccc6cda3 | 5436 | |
bb70624e JA |
5437 | @item | |
5438 | bitwise OR | |
cce855bc | 5439 | |
bb70624e JA |
5440 | @item && |
5441 | logical AND | |
cce855bc | 5442 | |
bb70624e JA |
5443 | @item || |
5444 | logical OR | |
cce855bc | 5445 | |
bb70624e | 5446 | @item expr ? expr : expr |
b80f6443 | 5447 | conditional operator |
cce855bc | 5448 | |
bb70624e JA |
5449 | @item = *= /= %= += -= <<= >>= &= ^= |= |
5450 | assignment | |
cce855bc | 5451 | |
bb70624e JA |
5452 | @item expr1 , expr2 |
5453 | comma | |
5454 | @end table | |
ccc6cda3 | 5455 | |
bb70624e JA |
5456 | Shell variables are allowed as operands; parameter expansion is |
5457 | performed before the expression is evaluated. | |
5458 | Within an expression, shell variables may also be referenced by name | |
5459 | without using the parameter expansion syntax. | |
b80f6443 JA |
5460 | A shell variable that is null or unset evaluates to 0 when referenced |
5461 | by name without using the parameter expansion syntax. | |
bb70624e | 5462 | The value of a variable is evaluated as an arithmetic expression |
b80f6443 JA |
5463 | when it is referenced, or when a variable which has been given the |
5464 | @var{integer} attribute using @samp{declare -i} is assigned a value. | |
5465 | A null value evaluates to 0. | |
bb70624e JA |
5466 | A shell variable need not have its integer attribute turned on |
5467 | to be used in an expression. | |
ccc6cda3 | 5468 | |
bb70624e JA |
5469 | Constants with a leading 0 are interpreted as octal numbers. |
5470 | A leading @samp{0x} or @samp{0X} denotes hexadecimal. Otherwise, | |
5471 | numbers take the form [@var{base}@code{#}]@var{n}, where @var{base} | |
5472 | is a decimal number between 2 and 64 representing the arithmetic | |
5473 | base, and @var{n} is a number in that base. If @var{base}@code{#} is | |
5474 | omitted, then base 10 is used. | |
5475 | The digits greater than 9 are represented by the lowercase letters, | |
f73dda09 | 5476 | the uppercase letters, @samp{@@}, and @samp{_}, in that order. |
bb70624e | 5477 | If @var{base} is less than or equal to 36, lowercase and uppercase |
95732b49 | 5478 | letters may be used interchangeably to represent numbers between 10 |
bb70624e | 5479 | and 35. |
ccc6cda3 | 5480 | |
bb70624e JA |
5481 | Operators are evaluated in order of precedence. Sub-expressions in |
5482 | parentheses are evaluated first and may override the precedence | |
5483 | rules above. | |
ccc6cda3 | 5484 | |
bb70624e JA |
5485 | @node Aliases |
5486 | @section Aliases | |
5487 | @cindex alias expansion | |
ccc6cda3 | 5488 | |
bb70624e JA |
5489 | @var{Aliases} allow a string to be substituted for a word when it is used |
5490 | as the first word of a simple command. | |
5491 | The shell maintains a list of aliases that may be set and unset with | |
5492 | the @code{alias} and @code{unalias} builtin commands. | |
ccc6cda3 | 5493 | |
bb70624e JA |
5494 | The first word of each simple command, if unquoted, is checked to see |
5495 | if it has an alias. | |
5496 | If so, that word is replaced by the text of the alias. | |
b80f6443 JA |
5497 | The characters @samp{/}, @samp{$}, @samp{`}, @samp{=} and any of the |
5498 | shell metacharacters or quoting characters listed above may not appear | |
5499 | in an alias name. | |
5500 | The replacement text may contain any valid | |
5501 | shell input, including shell metacharacters. | |
bb70624e JA |
5502 | The first word of the replacement text is tested for |
5503 | aliases, but a word that is identical to an alias being expanded | |
b80f6443 JA |
5504 | is not expanded a second time. |
5505 | This means that one may alias @code{ls} to @code{"ls -F"}, | |
bb70624e JA |
5506 | for instance, and Bash does not try to recursively expand the |
5507 | replacement text. If the last character of the alias value is a | |
5508 | space or tab character, then the next command word following the | |
5509 | alias is also checked for alias expansion. | |
ccc6cda3 | 5510 | |
bb70624e JA |
5511 | Aliases are created and listed with the @code{alias} |
5512 | command, and removed with the @code{unalias} command. | |
ccc6cda3 | 5513 | |
bb70624e JA |
5514 | There is no mechanism for using arguments in the replacement text, |
5515 | as in @code{csh}. | |
5516 | If arguments are needed, a shell function should be used | |
5517 | (@pxref{Shell Functions}). | |
ccc6cda3 | 5518 | |
bb70624e JA |
5519 | Aliases are not expanded when the shell is not interactive, |
5520 | unless the @code{expand_aliases} shell option is set using | |
5521 | @code{shopt} (@pxref{Bash Builtins}). | |
ccc6cda3 | 5522 | |
bb70624e JA |
5523 | The rules concerning the definition and use of aliases are |
5524 | somewhat confusing. Bash | |
5525 | always reads at least one complete line | |
5526 | of input before executing any | |
5527 | of the commands on that line. Aliases are expanded when a | |
5528 | command is read, not when it is executed. Therefore, an | |
5529 | alias definition appearing on the same line as another | |
5530 | command does not take effect until the next line of input is read. | |
5531 | The commands following the alias definition | |
5532 | on that line are not affected by the new alias. | |
5533 | This behavior is also an issue when functions are executed. | |
5534 | Aliases are expanded when a function definition is read, | |
5535 | not when the function is executed, because a function definition | |
5536 | is itself a compound command. As a consequence, aliases | |
5537 | defined in a function are not available until after that | |
5538 | function is executed. To be safe, always put | |
5539 | alias definitions on a separate line, and do not use @code{alias} | |
5540 | in compound commands. | |
ccc6cda3 | 5541 | |
bb70624e | 5542 | For almost every purpose, shell functions are preferred over aliases. |
ccc6cda3 | 5543 | |
bb70624e JA |
5544 | @node Arrays |
5545 | @section Arrays | |
5546 | @cindex arrays | |
d166f048 | 5547 | |
bb70624e JA |
5548 | Bash provides one-dimensional array variables. Any variable may be used as |
5549 | an array; the @code{declare} builtin will explicitly declare an array. | |
5550 | There is no maximum | |
5551 | limit on the size of an array, nor any requirement that members | |
5552 | be indexed or assigned contiguously. Arrays are zero-based. | |
ccc6cda3 | 5553 | |
bb70624e JA |
5554 | An array is created automatically if any variable is assigned to using |
5555 | the syntax | |
5556 | @example | |
5557 | name[@var{subscript}]=@var{value} | |
5558 | @end example | |
ccc6cda3 | 5559 | |
bb70624e JA |
5560 | @noindent |
5561 | The @var{subscript} | |
5562 | is treated as an arithmetic expression that must evaluate to a number | |
5563 | greater than or equal to zero. To explicitly declare an array, use | |
5564 | @example | |
5565 | declare -a @var{name} | |
5566 | @end example | |
5567 | @noindent | |
5568 | The syntax | |
5569 | @example | |
5570 | declare -a @var{name}[@var{subscript}] | |
5571 | @end example | |
5572 | @noindent | |
5573 | is also accepted; the @var{subscript} is ignored. Attributes may be | |
5574 | specified for an array variable using the @code{declare} and | |
5575 | @code{readonly} builtins. Each attribute applies to all members of | |
5576 | an array. | |
ccc6cda3 | 5577 | |
bb70624e JA |
5578 | Arrays are assigned to using compound assignments of the form |
5579 | @example | |
5580 | name=(value@var{1} @dots{} value@var{n}) | |
5581 | @end example | |
5582 | @noindent | |
5583 | where each | |
5584 | @var{value} is of the form @code{[[@var{subscript}]=]}@var{string}. If | |
5585 | the optional subscript is supplied, that index is assigned to; | |
5586 | otherwise the index of the element assigned is the last index assigned | |
5587 | to by the statement plus one. Indexing starts at zero. | |
5588 | This syntax is also accepted by the @code{declare} | |
5589 | builtin. Individual array elements may be assigned to using the | |
5590 | @code{name[}@var{subscript}@code{]=}@var{value} syntax introduced above. | |
cce855bc | 5591 | |
bb70624e JA |
5592 | Any element of an array may be referenced using |
5593 | @code{$@{name[}@var{subscript}@code{]@}}. | |
5594 | The braces are required to avoid | |
5595 | conflicts with the shell's filename expansion operators. If the | |
5596 | @var{subscript} is @samp{@@} or @samp{*}, the word expands to all members | |
5597 | of the array @var{name}. These subscripts differ only when the word | |
95732b49 JA |
5598 | appears within double quotes. |
5599 | If the word is double-quoted, | |
bb70624e JA |
5600 | @code{$@{name[*]@}} expands to a single word with |
5601 | the value of each array member separated by the first character of the | |
28ef6c31 | 5602 | @env{IFS} variable, and @code{$@{name[@@]@}} expands each element of |
bb70624e | 5603 | @var{name} to a separate word. When there are no array members, |
95732b49 JA |
5604 | @code{$@{name[@@]@}} expands to nothing. |
5605 | If the double-quoted expansion occurs within a word, the expansion of | |
5606 | the first parameter is joined with the beginning part of the original | |
5607 | word, and the expansion of the last parameter is joined with the last | |
5608 | part of the original word. | |
5609 | This is analogous to the | |
bb70624e JA |
5610 | expansion of the special parameters @samp{@@} and @samp{*}. |
5611 | @code{$@{#name[}@var{subscript}@code{]@}} expands to the length of | |
5612 | @code{$@{name[}@var{subscript}@code{]@}}. | |
5613 | If @var{subscript} is @samp{@@} or | |
5614 | @samp{*}, the expansion is the number of elements in the array. | |
5615 | Referencing an array variable without a subscript is equivalent to | |
5616 | referencing element zero. | |
ccc6cda3 | 5617 | |
bb70624e | 5618 | The @code{unset} builtin is used to destroy arrays. |
f73dda09 | 5619 | @code{unset} @var{name}[@var{subscript}] |
bb70624e | 5620 | destroys the array element at index @var{subscript}. |
95732b49 JA |
5621 | Care must be taken to avoid unwanted side effects caused by filename |
5622 | generation. | |
bb70624e JA |
5623 | @code{unset} @var{name}, where @var{name} is an array, removes the |
5624 | entire array. A subscript of @samp{*} or @samp{@@} also removes the | |
5625 | entire array. | |
ccc6cda3 | 5626 | |
bb70624e | 5627 | The @code{declare}, @code{local}, and @code{readonly} |
28ef6c31 | 5628 | builtins each accept a @option{-a} |
bb70624e | 5629 | option to specify an array. The @code{read} |
28ef6c31 | 5630 | builtin accepts a @option{-a} |
bb70624e JA |
5631 | option to assign a list of words read from the standard input |
5632 | to an array, and can read values from the standard input into | |
5633 | individual array elements. The @code{set} and @code{declare} | |
5634 | builtins display array values in a way that allows them to be | |
5635 | reused as input. | |
ccc6cda3 | 5636 | |
bb70624e JA |
5637 | @node The Directory Stack |
5638 | @section The Directory Stack | |
5639 | @cindex directory stack | |
ccc6cda3 | 5640 | |
bb70624e JA |
5641 | @menu |
5642 | * Directory Stack Builtins:: Bash builtin commands to manipulate | |
5643 | the directory stack. | |
5644 | @end menu | |
ccc6cda3 | 5645 | |
bb70624e JA |
5646 | The directory stack is a list of recently-visited directories. The |
5647 | @code{pushd} builtin adds directories to the stack as it changes | |
5648 | the current directory, and the @code{popd} builtin removes specified | |
5649 | directories from the stack and changes the current directory to | |
5650 | the directory removed. The @code{dirs} builtin displays the contents | |
5651 | of the directory stack. | |
ccc6cda3 | 5652 | |
bb70624e | 5653 | The contents of the directory stack are also visible |
28ef6c31 | 5654 | as the value of the @env{DIRSTACK} shell variable. |
ccc6cda3 | 5655 | |
bb70624e JA |
5656 | @node Directory Stack Builtins |
5657 | @subsection Directory Stack Builtins | |
ccc6cda3 | 5658 | |
bb70624e | 5659 | @table @code |
ccc6cda3 | 5660 | |
bb70624e JA |
5661 | @item dirs |
5662 | @btindex dirs | |
5663 | @example | |
5664 | dirs [+@var{N} | -@var{N}] [-clpv] | |
5665 | @end example | |
5666 | Display the list of currently remembered directories. Directories | |
5667 | are added to the list with the @code{pushd} command; the | |
5668 | @code{popd} command removes directories from the list. | |
5669 | @table @code | |
5670 | @item +@var{N} | |
5671 | Displays the @var{N}th directory (counting from the left of the | |
5672 | list printed by @code{dirs} when invoked without options), starting | |
5673 | with zero. | |
5674 | @item -@var{N} | |
5675 | Displays the @var{N}th directory (counting from the right of the | |
5676 | list printed by @code{dirs} when invoked without options), starting | |
5677 | with zero. | |
5678 | @item -c | |
5679 | Clears the directory stack by deleting all of the elements. | |
5680 | @item -l | |
5681 | Produces a longer listing; the default listing format uses a | |
5682 | tilde to denote the home directory. | |
5683 | @item -p | |
5684 | Causes @code{dirs} to print the directory stack with one entry per | |
5685 | line. | |
5686 | @item -v | |
5687 | Causes @code{dirs} to print the directory stack with one entry per | |
5688 | line, prefixing each entry with its index in the stack. | |
5689 | @end table | |
5690 | ||
5691 | @item popd | |
5692 | @btindex popd | |
5693 | @example | |
5694 | popd [+@var{N} | -@var{N}] [-n] | |
5695 | @end example | |
5696 | ||
5697 | Remove the top entry from the directory stack, and @code{cd} | |
5698 | to the new top directory. | |
5699 | When no arguments are given, @code{popd} | |
5700 | removes the top directory from the stack and | |
5701 | performs a @code{cd} to the new top directory. The | |
5702 | elements are numbered from 0 starting at the first directory listed with | |
5703 | @code{dirs}; i.e., @code{popd} is equivalent to @code{popd +0}. | |
5704 | @table @code | |
5705 | @item +@var{N} | |
5706 | Removes the @var{N}th directory (counting from the left of the | |
5707 | list printed by @code{dirs}), starting with zero. | |
5708 | @item -@var{N} | |
5709 | Removes the @var{N}th directory (counting from the right of the | |
5710 | list printed by @code{dirs}), starting with zero. | |
5711 | @item -n | |
5712 | Suppresses the normal change of directory when removing directories | |
5713 | from the stack, so that only the stack is manipulated. | |
5714 | @end table | |
5715 | ||
5716 | @btindex pushd | |
5717 | @item pushd | |
5718 | @example | |
5719 | pushd [@var{dir} | @var{+N} | @var{-N}] [-n] | |
5720 | @end example | |
5721 | ||
5722 | Save the current directory on the top of the directory stack | |
5723 | and then @code{cd} to @var{dir}. | |
5724 | With no arguments, @code{pushd} exchanges the top two directories. | |
5725 | ||
5726 | @table @code | |
5727 | @item +@var{N} | |
5728 | Brings the @var{N}th directory (counting from the left of the | |
5729 | list printed by @code{dirs}, starting with zero) to the top of | |
5730 | the list by rotating the stack. | |
5731 | @item -@var{N} | |
5732 | Brings the @var{N}th directory (counting from the right of the | |
5733 | list printed by @code{dirs}, starting with zero) to the top of | |
5734 | the list by rotating the stack. | |
5735 | @item -n | |
5736 | Suppresses the normal change of directory when adding directories | |
5737 | to the stack, so that only the stack is manipulated. | |
5738 | @item @var{dir} | |
5739 | Makes the current working directory be the top of the stack, and then | |
5740 | executes the equivalent of `@code{cd} @var{dir}'. | |
5741 | @code{cd}s to @var{dir}. | |
5742 | @end table | |
5743 | ||
5744 | @end table | |
5745 | ||
5746 | @node Printing a Prompt | |
5747 | @section Controlling the Prompt | |
5748 | @cindex prompting | |
ccc6cda3 | 5749 | |
28ef6c31 JA |
5750 | The value of the variable @env{PROMPT_COMMAND} is examined just before |
5751 | Bash prints each primary prompt. If @env{PROMPT_COMMAND} is set and | |
bb70624e JA |
5752 | has a non-null value, then the |
5753 | value is executed just as if it had been typed on the command line. | |
ccc6cda3 | 5754 | |
bb70624e JA |
5755 | In addition, the following table describes the special characters which |
5756 | can appear in the prompt variables: | |
ccc6cda3 | 5757 | |
bb70624e JA |
5758 | @table @code |
5759 | @item \a | |
5760 | A bell character. | |
5761 | @item \d | |
5762 | The date, in "Weekday Month Date" format (e.g., "Tue May 26"). | |
7117c2d2 JA |
5763 | @item \D@{@var{format}@} |
5764 | The @var{format} is passed to @code{strftime}(3) and the result is inserted | |
5765 | into the prompt string; an empty @var{format} results in a locale-specific | |
5766 | time representation. The braces are required. | |
bb70624e JA |
5767 | @item \e |
5768 | An escape character. | |
5769 | @item \h | |
5770 | The hostname, up to the first `.'. | |
5771 | @item \H | |
5772 | The hostname. | |
5773 | @item \j | |
5774 | The number of jobs currently managed by the shell. | |
5775 | @item \l | |
5776 | The basename of the shell's terminal device name. | |
5777 | @item \n | |
5778 | A newline. | |
5779 | @item \r | |
5780 | A carriage return. | |
5781 | @item \s | |
5782 | The name of the shell, the basename of @code{$0} (the portion | |
5783 | following the final slash). | |
5784 | @item \t | |
5785 | The time, in 24-hour HH:MM:SS format. | |
5786 | @item \T | |
5787 | The time, in 12-hour HH:MM:SS format. | |
5788 | @item \@@ | |
5789 | The time, in 12-hour am/pm format. | |
f73dda09 JA |
5790 | @item \A |
5791 | The time, in 24-hour HH:MM format. | |
bb70624e JA |
5792 | @item \u |
5793 | The username of the current user. | |
5794 | @item \v | |
5795 | The version of Bash (e.g., 2.00) | |
5796 | @item \V | |
5797 | The release of Bash, version + patchlevel (e.g., 2.00.0) | |
5798 | @item \w | |
b80f6443 | 5799 | The current working directory, with @env{$HOME} abbreviated with a tilde. |
bb70624e | 5800 | @item \W |
b80f6443 | 5801 | The basename of @env{$PWD}, with @env{$HOME} abbreviated with a tilde. |
bb70624e JA |
5802 | @item \! |
5803 | The history number of this command. | |
5804 | @item \# | |
5805 | The command number of this command. | |
5806 | @item \$ | |
5807 | If the effective uid is 0, @code{#}, otherwise @code{$}. | |
5808 | @item \@var{nnn} | |
5809 | The character whose ASCII code is the octal value @var{nnn}. | |
5810 | @item \\ | |
5811 | A backslash. | |
5812 | @item \[ | |
5813 | Begin a sequence of non-printing characters. This could be used to | |
5814 | embed a terminal control sequence into the prompt. | |
5815 | @item \] | |
5816 | End a sequence of non-printing characters. | |
5817 | @end table | |
ccc6cda3 | 5818 | |
bb70624e JA |
5819 | The command number and the history number are usually different: |
5820 | the history number of a command is its position in the history | |
5821 | list, which may include commands restored from the history file | |
5822 | (@pxref{Bash History Facilities}), while the command number is | |
5823 | the position in the sequence of commands executed during the current | |
5824 | shell session. | |
ccc6cda3 | 5825 | |
bb70624e JA |
5826 | After the string is decoded, it is expanded via |
5827 | parameter expansion, command substitution, arithmetic | |
5828 | expansion, and quote removal, subject to the value of the | |
5829 | @code{promptvars} shell option (@pxref{Bash Builtins}). | |
ccc6cda3 | 5830 | |
bb70624e JA |
5831 | @node The Restricted Shell |
5832 | @section The Restricted Shell | |
5833 | @cindex restricted shell | |
ccc6cda3 | 5834 | |
bb70624e | 5835 | If Bash is started with the name @code{rbash}, or the |
28ef6c31 | 5836 | @option{--restricted} |
7117c2d2 JA |
5837 | or |
5838 | @option{-r} | |
bb70624e JA |
5839 | option is supplied at invocation, the shell becomes restricted. |
5840 | A restricted shell is used to | |
5841 | set up an environment more controlled than the standard shell. | |
5842 | A restricted shell behaves identically to @code{bash} | |
7117c2d2 JA |
5843 | with the exception that the following are disallowed or not performed: |
5844 | ||
bb70624e JA |
5845 | @itemize @bullet |
5846 | @item | |
5847 | Changing directories with the @code{cd} builtin. | |
5848 | @item | |
28ef6c31 JA |
5849 | Setting or unsetting the values of the @env{SHELL}, @env{PATH}, |
5850 | @env{ENV}, or @env{BASH_ENV} variables. | |
bb70624e JA |
5851 | @item |
5852 | Specifying command names containing slashes. | |
5853 | @item | |
5854 | Specifying a filename containing a slash as an argument to the @code{.} | |
5855 | builtin command. | |
5856 | @item | |
28ef6c31 | 5857 | Specifying a filename containing a slash as an argument to the @option{-p} |
bb70624e JA |
5858 | option to the @code{hash} builtin command. |
5859 | @item | |
5860 | Importing function definitions from the shell environment at startup. | |
5861 | @item | |
28ef6c31 | 5862 | Parsing the value of @env{SHELLOPTS} from the shell environment at startup. |
bb70624e JA |
5863 | @item |
5864 | Redirecting output using the @samp{>}, @samp{>|}, @samp{<>}, @samp{>&}, | |
5865 | @samp{&>}, and @samp{>>} redirection operators. | |
5866 | @item | |
5867 | Using the @code{exec} builtin to replace the shell with another command. | |
5868 | @item | |
5869 | Adding or deleting builtin commands with the | |
28ef6c31 | 5870 | @option{-f} and @option{-d} options to the @code{enable} builtin. |
bb70624e | 5871 | @item |
7117c2d2 JA |
5872 | Using the @code{enable} builtin command to enable disabled shell builtins. |
5873 | @item | |
28ef6c31 | 5874 | Specifying the @option{-p} option to the @code{command} builtin. |
bb70624e JA |
5875 | @item |
5876 | Turning off restricted mode with @samp{set +r} or @samp{set +o restricted}. | |
5877 | @end itemize | |
cce855bc | 5878 | |
7117c2d2 JA |
5879 | These restrictions are enforced after any startup files are read. |
5880 | ||
5881 | When a command that is found to be a shell script is executed | |
5882 | (@pxref{Shell Scripts}), @code{rbash} turns off any restrictions in | |
5883 | the shell spawned to execute the script. | |
5884 | ||
bb70624e JA |
5885 | @node Bash POSIX Mode |
5886 | @section Bash POSIX Mode | |
5887 | @cindex POSIX Mode | |
cce855bc | 5888 | |
28ef6c31 | 5889 | Starting Bash with the @option{--posix} command-line option or executing |
bb70624e JA |
5890 | @samp{set -o posix} while Bash is running will cause Bash to conform more |
5891 | closely to the @sc{posix} 1003.2 standard by changing the behavior to | |
5892 | match that specified by @sc{posix} in areas where the Bash default differs. | |
ccc6cda3 | 5893 | |
7117c2d2 JA |
5894 | When invoked as @code{sh}, Bash enters @sc{posix} mode after reading the |
5895 | startup files. | |
5896 | ||
bb70624e | 5897 | The following list is what's changed when `@sc{posix} mode' is in effect: |
ccc6cda3 | 5898 | |
bb70624e JA |
5899 | @enumerate |
5900 | @item | |
5901 | When a command in the hash table no longer exists, Bash will re-search | |
28ef6c31 | 5902 | @env{$PATH} to find the new location. This is also available with |
bb70624e | 5903 | @samp{shopt -s checkhash}. |
ccc6cda3 | 5904 | |
bb70624e | 5905 | @item |
28ef6c31 JA |
5906 | The message printed by the job control code and builtins when a job |
5907 | exits with a non-zero status is `Done(status)'. | |
ccc6cda3 | 5908 | |
bb70624e JA |
5909 | @item |
5910 | The message printed by the job control code and builtins when a job | |
28ef6c31 JA |
5911 | is stopped is `Stopped(@var{signame})', where @var{signame} is, for |
5912 | example, @code{SIGTSTP}. | |
ccc6cda3 | 5913 | |
bb70624e | 5914 | @item |
95732b49 JA |
5915 | The @code{bg} builtin uses the required format to describe each job placed |
5916 | in the background, which does not include an indication of whether the job | |
5917 | is the current or previous job. | |
5918 | ||
5919 | @item | |
5920 | Reserved words appearing in a context where reserved words are recognized | |
5921 | do not undergo alias expansion. | |
ccc6cda3 | 5922 | |
bb70624e | 5923 | @item |
28ef6c31 | 5924 | The @sc{posix} 1003.2 @env{PS1} and @env{PS2} expansions of @samp{!} to |
bb70624e | 5925 | the history number and @samp{!!} to @samp{!} are enabled, |
28ef6c31 JA |
5926 | and parameter expansion is performed on the values of @env{PS1} and |
5927 | @env{PS2} regardless of the setting of the @code{promptvars} option. | |
ccc6cda3 | 5928 | |
bb70624e | 5929 | @item |
28ef6c31 | 5930 | The @sc{posix} 1003.2 startup files are executed (@env{$ENV}) rather than |
bb70624e | 5931 | the normal Bash files. |
ccc6cda3 | 5932 | |
bb70624e JA |
5933 | @item |
5934 | Tilde expansion is only performed on assignments preceding a command | |
5935 | name, rather than on all assignment statements on the line. | |
cce855bc | 5936 | |
bb70624e JA |
5937 | @item |
5938 | The default history file is @file{~/.sh_history} (this is the | |
28ef6c31 | 5939 | default value of @env{$HISTFILE}). |
ccc6cda3 | 5940 | |
bb70624e JA |
5941 | @item |
5942 | The output of @samp{kill -l} prints all the signal names on a single line, | |
b80f6443 JA |
5943 | separated by spaces, without the @samp{SIG} prefix. |
5944 | ||
5945 | @item | |
5946 | The @code{kill} builtin does not accept signal names with a @samp{SIG} | |
5947 | prefix. | |
ccc6cda3 | 5948 | |
bb70624e JA |
5949 | @item |
5950 | Non-interactive shells exit if @var{filename} in @code{.} @var{filename} | |
5951 | is not found. | |
ccc6cda3 | 5952 | |
bb70624e JA |
5953 | @item |
5954 | Non-interactive shells exit if a syntax error in an arithmetic expansion | |
5955 | results in an invalid expression. | |
ccc6cda3 | 5956 | |
bb70624e JA |
5957 | @item |
5958 | Redirection operators do not perform filename expansion on the word | |
5959 | in the redirection unless the shell is interactive. | |
ccc6cda3 | 5960 | |
bb70624e JA |
5961 | @item |
5962 | Redirection operators do not perform word splitting on the word in the | |
5963 | redirection. | |
ccc6cda3 | 5964 | |
bb70624e JA |
5965 | @item |
5966 | Function names must be valid shell @code{name}s. That is, they may not | |
5967 | contain characters other than letters, digits, and underscores, and | |
5968 | may not start with a digit. Declaring a function with an invalid name | |
5969 | causes a fatal syntax error in non-interactive shells. | |
ccc6cda3 | 5970 | |
bb70624e | 5971 | @item |
95732b49 | 5972 | @sc{posix} 1003.2 special builtins are found before shell functions |
bb70624e | 5973 | during command lookup. |
cce855bc | 5974 | |
bb70624e JA |
5975 | @item |
5976 | If a @sc{posix} 1003.2 special builtin returns an error status, a | |
5977 | non-interactive shell exits. The fatal errors are those listed in | |
5978 | the POSIX.2 standard, and include things like passing incorrect options, | |
5979 | redirection errors, variable assignment errors for assignments preceding | |
5980 | the command name, and so on. | |
ccc6cda3 | 5981 | |
bb70624e | 5982 | @item |
28ef6c31 | 5983 | If @env{CDPATH} is set, the @code{cd} builtin will not implicitly |
bb70624e JA |
5984 | append the current directory to it. This means that @code{cd} will |
5985 | fail if no valid directory name can be constructed from | |
28ef6c31 | 5986 | any of the entries in @env{$CDPATH}, even if the a directory with |
bb70624e JA |
5987 | the same name as the name given as an argument to @code{cd} exists |
5988 | in the current directory. | |
ccc6cda3 | 5989 | |
bb70624e JA |
5990 | @item |
5991 | A non-interactive shell exits with an error status if a variable | |
5992 | assignment error occurs when no command name follows the assignment | |
5993 | statements. | |
5994 | A variable assignment error occurs, for example, when trying to assign | |
5995 | a value to a readonly variable. | |
ccc6cda3 | 5996 | |
bb70624e JA |
5997 | @item |
5998 | A non-interactive shell exits with an error status if the iteration | |
5999 | variable in a @code{for} statement or the selection variable in a | |
6000 | @code{select} statement is a readonly variable. | |
ccc6cda3 | 6001 | |
bb70624e JA |
6002 | @item |
6003 | Process substitution is not available. | |
ccc6cda3 | 6004 | |
bb70624e JA |
6005 | @item |
6006 | Assignment statements preceding @sc{posix} 1003.2 special builtins | |
6007 | persist in the shell environment after the builtin completes. | |
ccc6cda3 | 6008 | |
28ef6c31 JA |
6009 | @item |
6010 | Assignment statements preceding shell function calls persist in the | |
6011 | shell environment after the function returns, as if a @sc{posix} | |
6012 | special builtin command had been executed. | |
6013 | ||
bb70624e JA |
6014 | @item |
6015 | The @code{export} and @code{readonly} builtin commands display their | |
6016 | output in the format required by @sc{posix} 1003.2. | |
ccc6cda3 | 6017 | |
28ef6c31 JA |
6018 | @item |
6019 | The @code{trap} builtin displays signal names without the leading | |
6020 | @code{SIG}. | |
6021 | ||
b80f6443 JA |
6022 | @item |
6023 | The @code{trap} builtin doesn't check the first argument for a possible | |
6024 | signal specification and revert the signal handling to the original | |
eb873671 JA |
6025 | disposition if it is, unless that argument consists solely of digits and |
6026 | is a valid signal number. If users want to reset the handler for a given | |
b80f6443 JA |
6027 | signal to the original disposition, they should use @samp{-} as the |
6028 | first argument. | |
6029 | ||
28ef6c31 JA |
6030 | @item |
6031 | The @code{.} and @code{source} builtins do not search the current directory | |
6032 | for the filename argument if it is not found by searching @env{PATH}. | |
6033 | ||
6034 | @item | |
6035 | Subshells spawned to execute command substitutions inherit the value of | |
6036 | the @option{-e} option from the parent shell. When not in @sc{posix} mode, | |
6037 | Bash clears the @option{-e} option in such subshells. | |
6038 | ||
6039 | @item | |
6040 | Alias expansion is always enabled, even in non-interactive shells. | |
6041 | ||
b80f6443 JA |
6042 | @item |
6043 | When the @code{alias} builtin displays alias definitions, it does not | |
6044 | display them with a leading @samp{alias } unless the @option{-p} option | |
6045 | is supplied. | |
6046 | ||
28ef6c31 JA |
6047 | @item |
6048 | When the @code{set} builtin is invoked without options, it does not display | |
6049 | shell function names and definitions. | |
6050 | ||
f73dda09 JA |
6051 | @item |
6052 | When the @code{set} builtin is invoked without options, it displays | |
6053 | variable values without quotes, unless they contain shell metacharacters, | |
6054 | even if the result contains nonprinting characters. | |
7117c2d2 JA |
6055 | |
6056 | @item | |
6057 | When the @code{cd} builtin is invoked in @var{logical} mode, and the pathname | |
6058 | constructed from @code{$PWD} and the directory name supplied as an argument | |
6059 | does not refer to an existing directory, @code{cd} will fail instead of | |
6060 | falling back to @var{physical} mode. | |
95732b49 JA |
6061 | |
6062 | @item | |
6063 | When the @code{pwd} builtin is supplied the @option{-P} option, it resets | |
6064 | @code{$PWD} to a pathname containing no symlinks. | |
6065 | ||
6066 | @item | |
6067 | The @code{pwd} builtin verifies that the value it prints is the same as the | |
6068 | current directory, even if it is not asked to check the file system with the | |
6069 | @option{-P} option. | |
6070 | ||
6071 | @item | |
6072 | When listing the history, the @code{fc} builtin does not include an | |
6073 | indication of whether or not a history entry has been modified. | |
6074 | ||
6075 | @item | |
6076 | The default editor used by @code{fc} is @code{ed}. | |
6077 | ||
6078 | @item | |
6079 | The @code{type} and @code{command} builtins will not report a non-executable | |
6080 | file as having been found, though the shell will attempt to execute such a | |
6081 | file if it is the only so-named file found in @code{$PATH}. | |
6082 | ||
6083 | @item | |
6084 | The @code{vi} editing mode will invoke the @code{vi} editor directly when | |
6085 | the @samp{v} command is run, instead of checking @code{$FCEDIT} and | |
6086 | @code{$EDITOR}. | |
6087 | ||
6088 | @item | |
6089 | When the @code{xpg_echo} option is enabled, Bash does not attempt to interpret | |
6090 | any arguments to @code{echo} as options. Each argument is displayed, after | |
6091 | escape characters are converted. | |
6092 | ||
bb70624e | 6093 | @end enumerate |
ccc6cda3 | 6094 | |
95732b49 JA |
6095 | There is other @sc{posix} 1003.2 behavior that Bash does not implement by |
6096 | default even when in @sc{posix} mode. | |
bb70624e | 6097 | Specifically: |
ccc6cda3 | 6098 | |
bb70624e | 6099 | @enumerate |
28ef6c31 JA |
6100 | |
6101 | @item | |
95732b49 JA |
6102 | The @code{fc} builtin checks @code{$EDITOR} as a program to edit history |
6103 | entries if @code{FCEDIT} is unset, rather than defaulting directly to | |
6104 | @code{ed}. @code{fc} uses @code{ed} if @code{EDITOR} is unset. | |
28ef6c31 JA |
6105 | |
6106 | @item | |
95732b49 JA |
6107 | As noted above, Bash requires the @code{xpg_echo} option to be enabled for |
6108 | the @code{echo} builtin to be fully conformant. | |
28ef6c31 | 6109 | |
bb70624e | 6110 | @end enumerate |
ccc6cda3 | 6111 | |
95732b49 JA |
6112 | Bash can be configured to be @sc{posix}-conformant by default, by specifying |
6113 | the @option{--enable-strict-posix-default} to @code{configure} when building | |
6114 | (@pxref{Optional Features}). | |
6115 | ||
bb70624e JA |
6116 | @node Job Control |
6117 | @chapter Job Control | |
ccc6cda3 | 6118 | |
bb70624e JA |
6119 | This chapter discusses what job control is, how it works, and how |
6120 | Bash allows you to access its facilities. | |
ccc6cda3 | 6121 | |
bb70624e JA |
6122 | @menu |
6123 | * Job Control Basics:: How job control works. | |
6124 | * Job Control Builtins:: Bash builtin commands used to interact | |
6125 | with job control. | |
6126 | * Job Control Variables:: Variables Bash uses to customize job | |
6127 | control. | |
6128 | @end menu | |
ccc6cda3 | 6129 | |
bb70624e JA |
6130 | @node Job Control Basics |
6131 | @section Job Control Basics | |
6132 | @cindex job control | |
6133 | @cindex foreground | |
6134 | @cindex background | |
6135 | @cindex suspending jobs | |
ccc6cda3 | 6136 | |
bb70624e JA |
6137 | Job control |
6138 | refers to the ability to selectively stop (suspend) | |
6139 | the execution of processes and continue (resume) | |
6140 | their execution at a later point. A user typically employs | |
6141 | this facility via an interactive interface supplied jointly | |
6142 | by the system's terminal driver and Bash. | |
ccc6cda3 | 6143 | |
bb70624e JA |
6144 | The shell associates a @var{job} with each pipeline. It keeps a |
6145 | table of currently executing jobs, which may be listed with the | |
6146 | @code{jobs} command. When Bash starts a job | |
6147 | asynchronously, it prints a line that looks | |
6148 | like: | |
6149 | @example | |
6150 | [1] 25647 | |
6151 | @end example | |
6152 | @noindent | |
6153 | indicating that this job is job number 1 and that the process @sc{id} | |
6154 | of the last process in the pipeline associated with this job is | |
6155 | 25647. All of the processes in a single pipeline are members of | |
6156 | the same job. Bash uses the @var{job} abstraction as the | |
6157 | basis for job control. | |
ccc6cda3 | 6158 | |
bb70624e JA |
6159 | To facilitate the implementation of the user interface to job |
6160 | control, the operating system maintains the notion of a current terminal | |
6161 | process group @sc{id}. Members of this process group (processes whose | |
6162 | process group @sc{id} is equal to the current terminal process group | |
6163 | @sc{id}) receive keyboard-generated signals such as @code{SIGINT}. | |
6164 | These processes are said to be in the foreground. Background | |
6165 | processes are those whose process group @sc{id} differs from the | |
6166 | terminal's; such processes are immune to keyboard-generated | |
6167 | signals. Only foreground processes are allowed to read from or | |
6168 | write to the terminal. Background processes which attempt to | |
6169 | read from (write to) the terminal are sent a @code{SIGTTIN} | |
6170 | (@code{SIGTTOU}) signal by the terminal driver, which, unless | |
6171 | caught, suspends the process. | |
ccc6cda3 | 6172 | |
bb70624e JA |
6173 | If the operating system on which Bash is running supports |
6174 | job control, Bash contains facilities to use it. Typing the | |
6175 | @var{suspend} character (typically @samp{^Z}, Control-Z) while a | |
6176 | process is running causes that process to be stopped and returns | |
6177 | control to Bash. Typing the @var{delayed suspend} character | |
6178 | (typically @samp{^Y}, Control-Y) causes the process to be stopped | |
6179 | when it attempts to read input from the terminal, and control to | |
6180 | be returned to Bash. The user then manipulates the state of | |
6181 | this job, using the @code{bg} command to continue it in the | |
6182 | background, the @code{fg} command to continue it in the | |
6183 | foreground, or the @code{kill} command to kill it. A @samp{^Z} | |
6184 | takes effect immediately, and has the additional side effect of | |
6185 | causing pending output and typeahead to be discarded. | |
ccc6cda3 | 6186 | |
bb70624e JA |
6187 | There are a number of ways to refer to a job in the shell. The |
6188 | character @samp{%} introduces a job name. | |
cce855bc | 6189 | |
bb70624e | 6190 | Job number @code{n} may be referred to as @samp{%n}. |
95732b49 JA |
6191 | The symbols @samp{%%} and @samp{%+} refer to the shell's notion of the |
6192 | current job, which is the last job stopped while it was in the foreground | |
6193 | or started in the background. | |
6194 | A single @samp{%} (with no accompanying job specification) also refers | |
6195 | to the current job. | |
6196 | The previous job may be referenced using @samp{%-}. In output | |
bb70624e JA |
6197 | pertaining to jobs (e.g., the output of the @code{jobs} command), |
6198 | the current job is always flagged with a @samp{+}, and the | |
6199 | previous job with a @samp{-}. | |
cce855bc | 6200 | |
bb70624e JA |
6201 | A job may also be referred to |
6202 | using a prefix of the name used to start it, or using a substring | |
6203 | that appears in its command line. For example, @samp{%ce} refers | |
6204 | to a stopped @code{ce} job. Using @samp{%?ce}, on the | |
6205 | other hand, refers to any job containing the string @samp{ce} in | |
6206 | its command line. If the prefix or substring matches more than one job, | |
6207 | Bash reports an error. | |
cce855bc | 6208 | |
bb70624e JA |
6209 | Simply naming a job can be used to bring it into the foreground: |
6210 | @samp{%1} is a synonym for @samp{fg %1}, bringing job 1 from the | |
6211 | background into the foreground. Similarly, @samp{%1 &} resumes | |
6212 | job 1 in the background, equivalent to @samp{bg %1} | |
cce855bc | 6213 | |
bb70624e JA |
6214 | The shell learns immediately whenever a job changes state. |
6215 | Normally, Bash waits until it is about to print a prompt | |
6216 | before reporting changes in a job's status so as to not interrupt | |
f73dda09 JA |
6217 | any other output. |
6218 | If the @option{-b} option to the @code{set} builtin is enabled, | |
bb70624e | 6219 | Bash reports such changes immediately (@pxref{The Set Builtin}). |
f73dda09 JA |
6220 | Any trap on @code{SIGCHLD} is executed for each child process |
6221 | that exits. | |
ccc6cda3 | 6222 | |
b80f6443 | 6223 | If an attempt to exit Bash is made while jobs are stopped, the |
bb70624e JA |
6224 | shell prints a message warning that there are stopped jobs. |
6225 | The @code{jobs} command may then be used to inspect their status. | |
6226 | If a second attempt to exit is made without an intervening command, | |
6227 | Bash does not print another warning, and the stopped jobs are terminated. | |
ccc6cda3 | 6228 | |
bb70624e JA |
6229 | @node Job Control Builtins |
6230 | @section Job Control Builtins | |
ccc6cda3 JA |
6231 | |
6232 | @table @code | |
6233 | ||
bb70624e JA |
6234 | @item bg |
6235 | @btindex bg | |
ccc6cda3 | 6236 | @example |
95732b49 | 6237 | bg [@var{jobspec} @dots{}] |
ccc6cda3 | 6238 | @end example |
95732b49 | 6239 | Resume each suspended job @var{jobspec} in the background, as if it |
bb70624e JA |
6240 | had been started with @samp{&}. |
6241 | If @var{jobspec} is not supplied, the current job is used. | |
6242 | The return status is zero unless it is run when job control is not | |
95732b49 JA |
6243 | enabled, or, when run with job control enabled, any |
6244 | @var{jobspec} was not found or specifies a job | |
6245 | that was started without job control. | |
cce855bc | 6246 | |
bb70624e JA |
6247 | @item fg |
6248 | @btindex fg | |
6249 | @example | |
6250 | fg [@var{jobspec}] | |
6251 | @end example | |
6252 | Resume the job @var{jobspec} in the foreground and make it the current job. | |
6253 | If @var{jobspec} is not supplied, the current job is used. | |
6254 | The return status is that of the command placed into the foreground, | |
6255 | or non-zero if run when job control is disabled or, when run with | |
6256 | job control enabled, @var{jobspec} does not specify a valid job or | |
6257 | @var{jobspec} specifies a job that was started without job control. | |
cce855bc | 6258 | |
bb70624e JA |
6259 | @item jobs |
6260 | @btindex jobs | |
cce855bc | 6261 | @example |
bb70624e JA |
6262 | jobs [-lnprs] [@var{jobspec}] |
6263 | jobs -x @var{command} [@var{arguments}] | |
cce855bc JA |
6264 | @end example |
6265 | ||
bb70624e JA |
6266 | The first form lists the active jobs. The options have the |
6267 | following meanings: | |
6268 | ||
6269 | @table @code | |
6270 | @item -l | |
6271 | List process @sc{id}s in addition to the normal information. | |
6272 | ||
6273 | @item -n | |
6274 | Display information only about jobs that have changed status since | |
6275 | the user was last notified of their status. | |
6276 | ||
6277 | @item -p | |
6278 | List only the process @sc{id} of the job's process group leader. | |
6279 | ||
6280 | @item -r | |
6281 | Restrict output to running jobs. | |
6282 | ||
6283 | @item -s | |
6284 | Restrict output to stopped jobs. | |
ccc6cda3 JA |
6285 | @end table |
6286 | ||
bb70624e JA |
6287 | If @var{jobspec} is given, |
6288 | output is restricted to information about that job. | |
6289 | If @var{jobspec} is not supplied, the status of all jobs is | |
6290 | listed. | |
ccc6cda3 | 6291 | |
28ef6c31 | 6292 | If the @option{-x} option is supplied, @code{jobs} replaces any |
bb70624e JA |
6293 | @var{jobspec} found in @var{command} or @var{arguments} with the |
6294 | corresponding process group @sc{id}, and executes @var{command}, | |
6295 | passing it @var{argument}s, returning its exit status. | |
ccc6cda3 | 6296 | |
bb70624e JA |
6297 | @item kill |
6298 | @btindex kill | |
ccc6cda3 | 6299 | @example |
bb70624e JA |
6300 | kill [-s @var{sigspec}] [-n @var{signum}] [-@var{sigspec}] @var{jobspec} or @var{pid} |
6301 | kill -l [@var{exit_status}] | |
ccc6cda3 | 6302 | @end example |
bb70624e JA |
6303 | Send a signal specified by @var{sigspec} or @var{signum} to the process |
6304 | named by job specification @var{jobspec} or process @sc{id} @var{pid}. | |
b80f6443 JA |
6305 | @var{sigspec} is either a case-insensitive signal name such as |
6306 | @code{SIGINT} (with or without the @code{SIG} prefix) | |
6307 | or a signal number; @var{signum} is a signal number. | |
bb70624e | 6308 | If @var{sigspec} and @var{signum} are not present, @code{SIGTERM} is used. |
28ef6c31 JA |
6309 | The @option{-l} option lists the signal names. |
6310 | If any arguments are supplied when @option{-l} is given, the names of the | |
bb70624e JA |
6311 | signals corresponding to the arguments are listed, and the return status |
6312 | is zero. | |
6313 | @var{exit_status} is a number specifying a signal number or the exit | |
6314 | status of a process terminated by a signal. | |
6315 | The return status is zero if at least one signal was successfully sent, | |
6316 | or non-zero if an error occurs or an invalid option is encountered. | |
ccc6cda3 | 6317 | |
bb70624e JA |
6318 | @item wait |
6319 | @btindex wait | |
ccc6cda3 | 6320 | @example |
95732b49 | 6321 | wait [@var{jobspec} or @var{pid} ...] |
ccc6cda3 | 6322 | @end example |
95732b49 JA |
6323 | Wait until the child process specified by each process @sc{id} @var{pid} |
6324 | or job specification @var{jobspec} exits and return the exit status of the | |
6325 | last command waited for. | |
bb70624e JA |
6326 | If a job spec is given, all processes in the job are waited for. |
6327 | If no arguments are given, all currently active child processes are | |
6328 | waited for, and the return status is zero. | |
6329 | If neither @var{jobspec} nor @var{pid} specifies an active child process | |
6330 | of the shell, the return status is 127. | |
6331 | ||
6332 | @item disown | |
6333 | @btindex disown | |
ccc6cda3 | 6334 | @example |
bb70624e | 6335 | disown [-ar] [-h] [@var{jobspec} @dots{}] |
ccc6cda3 | 6336 | @end example |
bb70624e JA |
6337 | Without options, each @var{jobspec} is removed from the table of |
6338 | active jobs. | |
28ef6c31 | 6339 | If the @option{-h} option is given, the job is not removed from the table, |
bb70624e JA |
6340 | but is marked so that @code{SIGHUP} is not sent to the job if the shell |
6341 | receives a @code{SIGHUP}. | |
28ef6c31 | 6342 | If @var{jobspec} is not present, and neither the @option{-a} nor @option{-r} |
bb70624e | 6343 | option is supplied, the current job is used. |
28ef6c31 JA |
6344 | If no @var{jobspec} is supplied, the @option{-a} option means to remove or |
6345 | mark all jobs; the @option{-r} option without a @var{jobspec} | |
bb70624e | 6346 | argument restricts operation to running jobs. |
ccc6cda3 | 6347 | |
bb70624e JA |
6348 | @item suspend |
6349 | @btindex suspend | |
ccc6cda3 | 6350 | @example |
bb70624e | 6351 | suspend [-f] |
ccc6cda3 | 6352 | @end example |
bb70624e | 6353 | Suspend the execution of this shell until it receives a |
28ef6c31 | 6354 | @code{SIGCONT} signal. The @option{-f} option means to suspend |
bb70624e | 6355 | even if the shell is a login shell. |
ccc6cda3 | 6356 | |
bb70624e | 6357 | @end table |
ccc6cda3 | 6358 | |
bb70624e JA |
6359 | When job control is not active, the @code{kill} and @code{wait} |
6360 | builtins do not accept @var{jobspec} arguments. They must be | |
6361 | supplied process @sc{id}s. | |
ccc6cda3 | 6362 | |
bb70624e JA |
6363 | @node Job Control Variables |
6364 | @section Job Control Variables | |
ccc6cda3 | 6365 | |
bb70624e | 6366 | @vtable @code |
cce855bc | 6367 | |
bb70624e JA |
6368 | @item auto_resume |
6369 | This variable controls how the shell interacts with the user and | |
6370 | job control. If this variable exists then single word simple | |
6371 | commands without redirections are treated as candidates for resumption | |
6372 | of an existing job. There is no ambiguity allowed; if there is | |
6373 | more than one job beginning with the string typed, then | |
6374 | the most recently accessed job will be selected. | |
6375 | The name of a stopped job, in this context, is the command line | |
6376 | used to start it. If this variable is set to the value @samp{exact}, | |
6377 | the string supplied must match the name of a stopped job exactly; | |
6378 | if set to @samp{substring}, | |
6379 | the string supplied needs to match a substring of the name of a | |
6380 | stopped job. The @samp{substring} value provides functionality | |
6381 | analogous to the @samp{%?} job @sc{id} (@pxref{Job Control Basics}). | |
6382 | If set to any other value, the supplied string must | |
6383 | be a prefix of a stopped job's name; this provides functionality | |
6384 | analogous to the @samp{%} job @sc{id}. | |
cce855bc | 6385 | |
bb70624e | 6386 | @end vtable |
cce855bc | 6387 | |
bb70624e JA |
6388 | @set readline-appendix |
6389 | @set history-appendix | |
6390 | @cindex Readline, how to use | |
b80f6443 | 6391 | @include rluser.texi |
bb70624e | 6392 | @cindex History, how to use |
b80f6443 | 6393 | @include hsuser.texi |
bb70624e JA |
6394 | @clear readline-appendix |
6395 | @clear history-appendix | |
cce855bc | 6396 | |
bb70624e JA |
6397 | @node Installing Bash |
6398 | @chapter Installing Bash | |
cce855bc | 6399 | |
bb70624e JA |
6400 | This chapter provides basic instructions for installing Bash on |
6401 | the various supported platforms. The distribution supports the | |
6402 | @sc{gnu} operating systems, nearly every version of Unix, and several | |
6403 | non-Unix systems such as BeOS and Interix. | |
6404 | Other independent ports exist for | |
b80f6443 | 6405 | @sc{ms-dos}, @sc{os/2}, and Windows platforms. |
cce855bc | 6406 | |
bb70624e JA |
6407 | @menu |
6408 | * Basic Installation:: Installation instructions. | |
bb70624e JA |
6409 | * Compilers and Options:: How to set special options for various |
6410 | systems. | |
bb70624e JA |
6411 | * Compiling For Multiple Architectures:: How to compile Bash for more |
6412 | than one kind of system from | |
6413 | the same source tree. | |
bb70624e | 6414 | * Installation Names:: How to set the various paths used by the installation. |
bb70624e | 6415 | * Specifying the System Type:: How to configure Bash for a particular system. |
bb70624e JA |
6416 | * Sharing Defaults:: How to share default configuration values among GNU |
6417 | programs. | |
bb70624e | 6418 | * Operation Controls:: Options recognized by the configuration program. |
bb70624e JA |
6419 | * Optional Features:: How to enable and disable optional features when |
6420 | building Bash. | |
6421 | @end menu | |
ccc6cda3 | 6422 | |
bb70624e JA |
6423 | @node Basic Installation |
6424 | @section Basic Installation | |
6425 | @cindex installation | |
6426 | @cindex configuration | |
6427 | @cindex Bash installation | |
6428 | @cindex Bash configuration | |
ccc6cda3 | 6429 | |
bb70624e | 6430 | These are installation instructions for Bash. |
ccc6cda3 | 6431 | |
bb70624e | 6432 | The simplest way to compile Bash is: |
ccc6cda3 | 6433 | |
bb70624e | 6434 | @enumerate |
ccc6cda3 | 6435 | @item |
bb70624e JA |
6436 | @code{cd} to the directory containing the source code and type |
6437 | @samp{./configure} to configure Bash for your system. If you're | |
6438 | using @code{csh} on an old version of System V, you might need to | |
6439 | type @samp{sh ./configure} instead to prevent @code{csh} from trying | |
6440 | to execute @code{configure} itself. | |
6441 | ||
6442 | Running @code{configure} takes some time. | |
6443 | While running, it prints messages telling which features it is | |
6444 | checking for. | |
6445 | ||
ccc6cda3 | 6446 | @item |
bb70624e JA |
6447 | Type @samp{make} to compile Bash and build the @code{bashbug} bug |
6448 | reporting script. | |
6449 | ||
ccc6cda3 | 6450 | @item |
bb70624e JA |
6451 | Optionally, type @samp{make tests} to run the Bash test suite. |
6452 | ||
ccc6cda3 | 6453 | @item |
bb70624e JA |
6454 | Type @samp{make install} to install @code{bash} and @code{bashbug}. |
6455 | This will also install the manual pages and Info file. | |
ccc6cda3 | 6456 | |
bb70624e | 6457 | @end enumerate |
ccc6cda3 | 6458 | |
bb70624e JA |
6459 | The @code{configure} shell script attempts to guess correct |
6460 | values for various system-dependent variables used during | |
6461 | compilation. It uses those values to create a @file{Makefile} in | |
6462 | each directory of the package (the top directory, the | |
6463 | @file{builtins}, @file{doc}, and @file{support} directories, | |
6464 | each directory under @file{lib}, and several others). It also creates a | |
6465 | @file{config.h} file containing system-dependent definitions. | |
6466 | Finally, it creates a shell script named @code{config.status} that you | |
6467 | can run in the future to recreate the current configuration, a | |
6468 | file @file{config.cache} that saves the results of its tests to | |
6469 | speed up reconfiguring, and a file @file{config.log} containing | |
6470 | compiler output (useful mainly for debugging @code{configure}). | |
6471 | If at some point | |
6472 | @file{config.cache} contains results you don't want to keep, you | |
6473 | may remove or edit it. | |
ccc6cda3 | 6474 | |
bb70624e JA |
6475 | To find out more about the options and arguments that the |
6476 | @code{configure} script understands, type | |
ccc6cda3 | 6477 | |
bb70624e JA |
6478 | @example |
6479 | bash-2.04$ ./configure --help | |
6480 | @end example | |
ccc6cda3 | 6481 | |
bb70624e JA |
6482 | @noindent |
6483 | at the Bash prompt in your Bash source directory. | |
ccc6cda3 | 6484 | |
bb70624e JA |
6485 | If you need to do unusual things to compile Bash, please |
6486 | try to figure out how @code{configure} could check whether or not | |
6487 | to do them, and mail diffs or instructions to | |
6488 | @email{bash-maintainers@@gnu.org} so they can be | |
6489 | considered for the next release. | |
ccc6cda3 | 6490 | |
bb70624e JA |
6491 | The file @file{configure.in} is used to create @code{configure} |
6492 | by a program called Autoconf. You only need | |
6493 | @file{configure.in} if you want to change it or regenerate | |
6494 | @code{configure} using a newer version of Autoconf. If | |
f73dda09 | 6495 | you do this, make sure you are using Autoconf version 2.50 or |
bb70624e | 6496 | newer. |
ccc6cda3 | 6497 | |
bb70624e JA |
6498 | You can remove the program binaries and object files from the |
6499 | source code directory by typing @samp{make clean}. To also remove the | |
6500 | files that @code{configure} created (so you can compile Bash for | |
6501 | a different kind of computer), type @samp{make distclean}. | |
ccc6cda3 | 6502 | |
bb70624e JA |
6503 | @node Compilers and Options |
6504 | @section Compilers and Options | |
ccc6cda3 | 6505 | |
bb70624e JA |
6506 | Some systems require unusual options for compilation or linking |
6507 | that the @code{configure} script does not know about. You can | |
6508 | give @code{configure} initial values for variables by setting | |
6509 | them in the environment. Using a Bourne-compatible shell, you | |
6510 | can do that on the command line like this: | |
ccc6cda3 | 6511 | |
bb70624e JA |
6512 | @example |
6513 | CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure | |
6514 | @end example | |
ccc6cda3 | 6515 | |
bb70624e | 6516 | On systems that have the @code{env} program, you can do it like this: |
ccc6cda3 | 6517 | |
bb70624e JA |
6518 | @example |
6519 | env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure | |
6520 | @end example | |
ccc6cda3 | 6521 | |
bb70624e JA |
6522 | The configuration process uses GCC to build Bash if it |
6523 | is available. | |
cce855bc | 6524 | |
bb70624e JA |
6525 | @node Compiling For Multiple Architectures |
6526 | @section Compiling For Multiple Architectures | |
ccc6cda3 | 6527 | |
bb70624e JA |
6528 | You can compile Bash for more than one kind of computer at the |
6529 | same time, by placing the object files for each architecture in their | |
6530 | own directory. To do this, you must use a version of @code{make} that | |
6531 | supports the @code{VPATH} variable, such as GNU @code{make}. | |
6532 | @code{cd} to the | |
6533 | directory where you want the object files and executables to go and run | |
6534 | the @code{configure} script from the source directory. You may need to | |
28ef6c31 | 6535 | supply the @option{--srcdir=PATH} argument to tell @code{configure} where the |
bb70624e JA |
6536 | source files are. @code{configure} automatically checks for the |
6537 | source code in the directory that @code{configure} is in and in `..'. | |
ccc6cda3 | 6538 | |
bb70624e JA |
6539 | If you have to use a @code{make} that does not supports the @code{VPATH} |
6540 | variable, you can compile Bash for one architecture at a | |
6541 | time in the source code directory. After you have installed | |
6542 | Bash for one architecture, use @samp{make distclean} before | |
6543 | reconfiguring for another architecture. | |
ccc6cda3 | 6544 | |
bb70624e JA |
6545 | Alternatively, if your system supports symbolic links, you can use the |
6546 | @file{support/mkclone} script to create a build tree which has | |
6547 | symbolic links back to each file in the source directory. Here's an | |
6548 | example that creates a build directory in the current directory from a | |
6549 | source directory @file{/usr/gnu/src/bash-2.0}: | |
6550 | ||
6551 | @example | |
6552 | bash /usr/gnu/src/bash-2.0/support/mkclone -s /usr/gnu/src/bash-2.0 . | |
6553 | @end example | |
6554 | ||
6555 | @noindent | |
6556 | The @code{mkclone} script requires Bash, so you must have already built | |
6557 | Bash for at least one architecture before you can create build | |
6558 | directories for other architectures. | |
6559 | ||
6560 | @node Installation Names | |
6561 | @section Installation Names | |
ccc6cda3 | 6562 | |
bb70624e JA |
6563 | By default, @samp{make install} will install into |
6564 | @file{/usr/local/bin}, @file{/usr/local/man}, etc. You can | |
6565 | specify an installation prefix other than @file{/usr/local} by | |
f73dda09 JA |
6566 | giving @code{configure} the option @option{--prefix=@var{PATH}}, |
6567 | or by specifying a value for the @code{DESTDIR} @samp{make} | |
6568 | variable when running @samp{make install}. | |
ccc6cda3 | 6569 | |
bb70624e JA |
6570 | You can specify separate installation prefixes for |
6571 | architecture-specific files and architecture-independent files. | |
6572 | If you give @code{configure} the option | |
28ef6c31 | 6573 | @option{--exec-prefix=@var{PATH}}, @samp{make install} will use |
bb70624e JA |
6574 | @var{PATH} as the prefix for installing programs and libraries. |
6575 | Documentation and other data files will still use the regular prefix. | |
cce855bc | 6576 | |
bb70624e JA |
6577 | @node Specifying the System Type |
6578 | @section Specifying the System Type | |
ccc6cda3 | 6579 | |
bb70624e | 6580 | There may be some features @code{configure} can not figure out |
f73dda09 | 6581 | automatically, but need to determine by the type of host Bash |
bb70624e JA |
6582 | will run on. Usually @code{configure} can figure that |
6583 | out, but if it prints a message saying it can not guess the host | |
28ef6c31 | 6584 | type, give it the @option{--host=TYPE} option. @samp{TYPE} can |
bb70624e JA |
6585 | either be a short name for the system type, such as @samp{sun4}, |
6586 | or a canonical name with three fields: @samp{CPU-COMPANY-SYSTEM} | |
f73dda09 | 6587 | (e.g., @samp{i386-unknown-freebsd4.2}). |
ccc6cda3 | 6588 | |
bb70624e JA |
6589 | See the file @file{support/config.sub} for the possible |
6590 | values of each field. | |
ccc6cda3 | 6591 | |
bb70624e JA |
6592 | @node Sharing Defaults |
6593 | @section Sharing Defaults | |
ccc6cda3 | 6594 | |
bb70624e JA |
6595 | If you want to set default values for @code{configure} scripts to |
6596 | share, you can create a site shell script called | |
6597 | @code{config.site} that gives default values for variables like | |
6598 | @code{CC}, @code{cache_file}, and @code{prefix}. @code{configure} | |
6599 | looks for @file{PREFIX/share/config.site} if it exists, then | |
6600 | @file{PREFIX/etc/config.site} if it exists. Or, you can set the | |
6601 | @code{CONFIG_SITE} environment variable to the location of the site | |
6602 | script. A warning: the Bash @code{configure} looks for a site script, | |
6603 | but not all @code{configure} scripts do. | |
d166f048 | 6604 | |
bb70624e JA |
6605 | @node Operation Controls |
6606 | @section Operation Controls | |
ccc6cda3 | 6607 | |
bb70624e JA |
6608 | @code{configure} recognizes the following options to control how it |
6609 | operates. | |
ccc6cda3 | 6610 | |
bb70624e | 6611 | @table @code |
ccc6cda3 | 6612 | |
bb70624e JA |
6613 | @item --cache-file=@var{file} |
6614 | Use and save the results of the tests in | |
6615 | @var{file} instead of @file{./config.cache}. Set @var{file} to | |
6616 | @file{/dev/null} to disable caching, for debugging | |
6617 | @code{configure}. | |
ccc6cda3 | 6618 | |
bb70624e JA |
6619 | @item --help |
6620 | Print a summary of the options to @code{configure}, and exit. | |
ccc6cda3 | 6621 | |
bb70624e JA |
6622 | @item --quiet |
6623 | @itemx --silent | |
6624 | @itemx -q | |
6625 | Do not print messages saying which checks are being made. | |
ccc6cda3 | 6626 | |
bb70624e JA |
6627 | @item --srcdir=@var{dir} |
6628 | Look for the Bash source code in directory @var{dir}. Usually | |
6629 | @code{configure} can determine that directory automatically. | |
ccc6cda3 | 6630 | |
bb70624e JA |
6631 | @item --version |
6632 | Print the version of Autoconf used to generate the @code{configure} | |
6633 | script, and exit. | |
6634 | @end table | |
ccc6cda3 | 6635 | |
bb70624e JA |
6636 | @code{configure} also accepts some other, not widely used, boilerplate |
6637 | options. @samp{configure --help} prints the complete list. | |
ccc6cda3 | 6638 | |
bb70624e JA |
6639 | @node Optional Features |
6640 | @section Optional Features | |
ccc6cda3 | 6641 | |
28ef6c31 | 6642 | The Bash @code{configure} has a number of @option{--enable-@var{feature}} |
bb70624e | 6643 | options, where @var{feature} indicates an optional part of Bash. |
28ef6c31 | 6644 | There are also several @option{--with-@var{package}} options, |
bb70624e JA |
6645 | where @var{package} is something like @samp{bash-malloc} or @samp{purify}. |
6646 | To turn off the default use of a package, use | |
28ef6c31 JA |
6647 | @option{--without-@var{package}}. To configure Bash without a feature |
6648 | that is enabled by default, use @option{--disable-@var{feature}}. | |
ccc6cda3 | 6649 | |
28ef6c31 JA |
6650 | Here is a complete list of the @option{--enable-} and |
6651 | @option{--with-} options that the Bash @code{configure} recognizes. | |
ccc6cda3 | 6652 | |
bb70624e JA |
6653 | @table @code |
6654 | @item --with-afs | |
6655 | Define if you are using the Andrew File System from Transarc. | |
ccc6cda3 | 6656 | |
bb70624e JA |
6657 | @item --with-bash-malloc |
6658 | Use the Bash version of | |
95732b49 | 6659 | @code{malloc} in the directory @file{lib/malloc}. This is not the same |
bb70624e | 6660 | @code{malloc} that appears in @sc{gnu} libc, but an older version |
95732b49 JA |
6661 | originally derived from the 4.2 @sc{bsd} @code{malloc}. This @code{malloc} |
6662 | is very fast, but wastes some space on each allocation. | |
bb70624e JA |
6663 | This option is enabled by default. |
6664 | The @file{NOTES} file contains a list of systems for | |
6665 | which this should be turned off, and @code{configure} disables this | |
6666 | option automatically for a number of systems. | |
ccc6cda3 | 6667 | |
bb70624e JA |
6668 | @item --with-curses |
6669 | Use the curses library instead of the termcap library. This should | |
6670 | be supplied if your system has an inadequate or incomplete termcap | |
6671 | database. | |
ccc6cda3 | 6672 | |
bb70624e JA |
6673 | @item --with-gnu-malloc |
6674 | A synonym for @code{--with-bash-malloc}. | |
ccc6cda3 | 6675 | |
f73dda09 | 6676 | @item --with-installed-readline[=@var{PREFIX}] |
bb70624e JA |
6677 | Define this to make Bash link with a locally-installed version of Readline |
6678 | rather than the version in @file{lib/readline}. This works only with | |
95732b49 | 6679 | Readline 5.0 and later versions. If @var{PREFIX} is @code{yes} or not |
f73dda09 JA |
6680 | supplied, @code{configure} uses the values of the make variables |
6681 | @code{includedir} and @code{libdir}, which are subdirectories of @code{prefix} | |
6682 | by default, to find the installed version of Readline if it is not in | |
6683 | the standard system include and library directories. | |
6684 | If @var{PREFIX} is @code{no}, Bash links with the version in | |
6685 | @file{lib/readline}. | |
6686 | If @var{PREFIX} is set to any other value, @code{configure} treats it as | |
6687 | a directory pathname and looks for | |
6688 | the installed version of Readline in subdirectories of that directory | |
6689 | (include files in @var{PREFIX}/@code{include} and the library in | |
6690 | @var{PREFIX}/@code{lib}). | |
ccc6cda3 | 6691 | |
bb70624e JA |
6692 | @item --with-purify |
6693 | Define this to use the Purify memory allocation checker from Rational | |
6694 | Software. | |
ccc6cda3 | 6695 | |
bb70624e JA |
6696 | @item --enable-minimal-config |
6697 | This produces a shell with minimal features, close to the historical | |
6698 | Bourne shell. | |
6699 | @end table | |
ccc6cda3 | 6700 | |
28ef6c31 | 6701 | There are several @option{--enable-} options that alter how Bash is |
bb70624e | 6702 | compiled and linked, rather than changing run-time features. |
ccc6cda3 JA |
6703 | |
6704 | @table @code | |
f73dda09 JA |
6705 | @item --enable-largefile |
6706 | Enable support for @uref{http://www.sas.com/standards/large_file/x_open.20Mar96.html, | |
6707 | large files} if the operating system requires special compiler options | |
7117c2d2 JA |
6708 | to build programs which can access large files. This is enabled by |
6709 | default, if the operating system provides large file support. | |
f73dda09 | 6710 | |
bb70624e JA |
6711 | @item --enable-profiling |
6712 | This builds a Bash binary that produces profiling information to be | |
6713 | processed by @code{gprof} each time it is executed. | |
ccc6cda3 | 6714 | |
bb70624e JA |
6715 | @item --enable-static-link |
6716 | This causes Bash to be linked statically, if @code{gcc} is being used. | |
6717 | This could be used to build a version to use as root's shell. | |
6718 | @end table | |
6719 | ||
6720 | The @samp{minimal-config} option can be used to disable all of | |
6721 | the following options, but it is processed first, so individual | |
6722 | options may be enabled using @samp{enable-@var{feature}}. | |
6723 | ||
6724 | All of the following options except for @samp{disabled-builtins} and | |
6725 | @samp{xpg-echo-default} are | |
6726 | enabled by default, unless the operating system does not provide the | |
6727 | necessary support. | |
6728 | ||
6729 | @table @code | |
6730 | @item --enable-alias | |
6731 | Allow alias expansion and include the @code{alias} and @code{unalias} | |
6732 | builtins (@pxref{Aliases}). | |
ccc6cda3 | 6733 | |
bb70624e JA |
6734 | @item --enable-arith-for-command |
6735 | Include support for the alternate form of the @code{for} command | |
6736 | that behaves like the C language @code{for} statement | |
6737 | (@pxref{Looping Constructs}). | |
ccc6cda3 | 6738 | |
bb70624e JA |
6739 | @item --enable-array-variables |
6740 | Include support for one-dimensional array shell variables | |
6741 | (@pxref{Arrays}). | |
ccc6cda3 | 6742 | |
bb70624e JA |
6743 | @item --enable-bang-history |
6744 | Include support for @code{csh}-like history substitution | |
6745 | (@pxref{History Interaction}). | |
ccc6cda3 | 6746 | |
bb70624e JA |
6747 | @item --enable-brace-expansion |
6748 | Include @code{csh}-like brace expansion | |
6749 | ( @code{b@{a,b@}c} @expansion{} @code{bac bbc} ). | |
6750 | See @ref{Brace Expansion}, for a complete description. | |
ccc6cda3 | 6751 | |
bb70624e JA |
6752 | @item --enable-command-timing |
6753 | Include support for recognizing @code{time} as a reserved word and for | |
6754 | displaying timing statistics for the pipeline following @code{time} | |
6755 | (@pxref{Pipelines}). | |
6756 | This allows pipelines as well as shell builtins and functions to be timed. | |
ccc6cda3 | 6757 | |
bb70624e | 6758 | @item --enable-cond-command |
b80f6443 JA |
6759 | Include support for the @code{[[} conditional command. |
6760 | (@pxref{Conditional Constructs}). | |
6761 | ||
6762 | @item --enable-cond-regexp | |
6763 | Include support for matching POSIX regular expressions using the | |
6764 | @samp{=~} binary operator in the @code{[[} conditional command. | |
bb70624e | 6765 | (@pxref{Conditional Constructs}). |
ccc6cda3 | 6766 | |
95732b49 JA |
6767 | @item --enable-debugger |
6768 | Include support for the bash debugger (distributed separately). | |
6769 | ||
bb70624e JA |
6770 | @item --enable-directory-stack |
6771 | Include support for a @code{csh}-like directory stack and the | |
6772 | @code{pushd}, @code{popd}, and @code{dirs} builtins | |
6773 | (@pxref{The Directory Stack}). | |
ccc6cda3 | 6774 | |
bb70624e JA |
6775 | @item --enable-disabled-builtins |
6776 | Allow builtin commands to be invoked via @samp{builtin xxx} | |
6777 | even after @code{xxx} has been disabled using @samp{enable -n xxx}. | |
6778 | See @ref{Bash Builtins}, for details of the @code{builtin} and | |
6779 | @code{enable} builtin commands. | |
ccc6cda3 | 6780 | |
bb70624e JA |
6781 | @item --enable-dparen-arithmetic |
6782 | Include support for the @code{((@dots{}))} command | |
6783 | (@pxref{Conditional Constructs}). | |
ccc6cda3 | 6784 | |
bb70624e JA |
6785 | @item --enable-extended-glob |
6786 | Include support for the extended pattern matching features described | |
6787 | above under @ref{Pattern Matching}. | |
ccc6cda3 | 6788 | |
bb70624e JA |
6789 | @item --enable-help-builtin |
6790 | Include the @code{help} builtin, which displays help on shell builtins and | |
6791 | variables (@pxref{Bash Builtins}). | |
ccc6cda3 | 6792 | |
bb70624e JA |
6793 | @item --enable-history |
6794 | Include command history and the @code{fc} and @code{history} | |
6795 | builtin commands (@pxref{Bash History Facilities}). | |
ccc6cda3 | 6796 | |
bb70624e JA |
6797 | @item --enable-job-control |
6798 | This enables the job control features (@pxref{Job Control}), | |
6799 | if the operating system supports them. | |
ccc6cda3 | 6800 | |
b80f6443 JA |
6801 | @item --enable-multibyte |
6802 | This enables support for multibyte characters if the operating | |
6803 | system provides the necessary support. | |
6804 | ||
bb70624e JA |
6805 | @item --enable-net-redirections |
6806 | This enables the special handling of filenames of the form | |
6807 | @code{/dev/tcp/@var{host}/@var{port}} and | |
6808 | @code{/dev/udp/@var{host}/@var{port}} | |
6809 | when used in redirections (@pxref{Redirections}). | |
ccc6cda3 | 6810 | |
bb70624e JA |
6811 | @item --enable-process-substitution |
6812 | This enables process substitution (@pxref{Process Substitution}) if | |
6813 | the operating system provides the necessary support. | |
ccc6cda3 | 6814 | |
95732b49 JA |
6815 | @item --enable-progcomp |
6816 | Enable the programmable completion facilities | |
6817 | (@pxref{Programmable Completion}). | |
6818 | If Readline is not enabled, this option has no effect. | |
6819 | ||
bb70624e JA |
6820 | @item --enable-prompt-string-decoding |
6821 | Turn on the interpretation of a number of backslash-escaped characters | |
28ef6c31 | 6822 | in the @env{$PS1}, @env{$PS2}, @env{$PS3}, and @env{$PS4} prompt |
bb70624e JA |
6823 | strings. See @ref{Printing a Prompt}, for a complete list of prompt |
6824 | string escape sequences. | |
ccc6cda3 | 6825 | |
bb70624e JA |
6826 | @item --enable-readline |
6827 | Include support for command-line editing and history with the Bash | |
6828 | version of the Readline library (@pxref{Command Line Editing}). | |
ccc6cda3 | 6829 | |
bb70624e JA |
6830 | @item --enable-restricted |
6831 | Include support for a @dfn{restricted shell}. If this is enabled, Bash, | |
6832 | when called as @code{rbash}, enters a restricted mode. See | |
6833 | @ref{The Restricted Shell}, for a description of restricted mode. | |
ccc6cda3 | 6834 | |
bb70624e JA |
6835 | @item --enable-select |
6836 | Include the @code{select} builtin, which allows the generation of simple | |
6837 | menus (@pxref{Conditional Constructs}). | |
ccc6cda3 | 6838 | |
95732b49 JA |
6839 | @item --enable-separate-helpfiles |
6840 | Use external files for the documentation displayed by the @code{help} builtin | |
6841 | instead of storing the text internally. | |
6842 | ||
6843 | @item --enable-single-help-strings | |
6844 | Store the text displayed by the @code{help} builtin as a single string for | |
6845 | each help topic. This aids in translating the text to different languages. | |
6846 | You may need to disable this if your compiler cannot handle very long string | |
6847 | literals. | |
6848 | ||
6849 | @item --enable-strict-posix-default | |
6850 | Make Bash @sc{posix}-conformant by default (@pxref{Bash POSIX Mode}). | |
6851 | ||
bb70624e JA |
6852 | @item --enable-usg-echo-default |
6853 | A synonym for @code{--enable-xpg-echo-default}. | |
ccc6cda3 | 6854 | |
bb70624e JA |
6855 | @item --enable-xpg-echo-default |
6856 | Make the @code{echo} builtin expand backslash-escaped characters by default, | |
28ef6c31 | 6857 | without requiring the @option{-e} option. |
bb70624e JA |
6858 | This sets the default value of the @code{xpg_echo} shell option to @code{on}, |
6859 | which makes the Bash @code{echo} behave more like the version specified in | |
95732b49 | 6860 | the Single Unix Specification, version 3. |
bb70624e JA |
6861 | @xref{Bash Builtins}, for a description of the escape sequences that |
6862 | @code{echo} recognizes. | |
ccc6cda3 | 6863 | |
bb70624e | 6864 | @end table |
ccc6cda3 | 6865 | |
bb70624e JA |
6866 | The file @file{config-top.h} contains C Preprocessor |
6867 | @samp{#define} statements for options which are not settable from | |
6868 | @code{configure}. | |
6869 | Some of these are not meant to be changed; beware of the consequences if | |
6870 | you do. | |
6871 | Read the comments associated with each definition for more | |
6872 | information about its effect. | |
ccc6cda3 | 6873 | |
bb70624e JA |
6874 | @node Reporting Bugs |
6875 | @appendix Reporting Bugs | |
ccc6cda3 | 6876 | |
bb70624e JA |
6877 | Please report all bugs you find in Bash. |
6878 | But first, you should | |
6879 | make sure that it really is a bug, and that it appears in the latest | |
b80f6443 JA |
6880 | version of Bash. |
6881 | The latest version of Bash is always available for FTP from | |
6882 | @uref{ftp://ftp.gnu.org/pub/bash/}. | |
ccc6cda3 | 6883 | |
bb70624e JA |
6884 | Once you have determined that a bug actually exists, use the |
6885 | @code{bashbug} command to submit a bug report. | |
6886 | If you have a fix, you are encouraged to mail that as well! | |
6887 | Suggestions and `philosophical' bug reports may be mailed | |
6888 | to @email{bug-bash@@gnu.org} or posted to the Usenet | |
6889 | newsgroup @code{gnu.bash.bug}. | |
ccc6cda3 | 6890 | |
bb70624e JA |
6891 | All bug reports should include: |
6892 | @itemize @bullet | |
6893 | @item | |
6894 | The version number of Bash. | |
6895 | @item | |
6896 | The hardware and operating system. | |
6897 | @item | |
6898 | The compiler used to compile Bash. | |
6899 | @item | |
6900 | A description of the bug behaviour. | |
6901 | @item | |
6902 | A short script or `recipe' which exercises the bug and may be used | |
6903 | to reproduce it. | |
6904 | @end itemize | |
ccc6cda3 | 6905 | |
bb70624e JA |
6906 | @noindent |
6907 | @code{bashbug} inserts the first three items automatically into | |
6908 | the template it provides for filing a bug report. | |
ccc6cda3 | 6909 | |
bb70624e JA |
6910 | Please send all reports concerning this manual to |
6911 | @email{chet@@po.CWRU.Edu}. | |
ccc6cda3 | 6912 | |
bb70624e JA |
6913 | @node Major Differences From The Bourne Shell |
6914 | @appendix Major Differences From The Bourne Shell | |
d166f048 | 6915 | |
bb70624e JA |
6916 | Bash implements essentially the same grammar, parameter and |
6917 | variable expansion, redirection, and quoting as the Bourne Shell. | |
6918 | Bash uses the @sc{posix} 1003.2 standard as the specification of | |
6919 | how these features are to be implemented. There are some | |
6920 | differences between the traditional Bourne shell and Bash; this | |
6921 | section quickly details the differences of significance. A | |
6922 | number of these differences are explained in greater depth in | |
6923 | previous sections. | |
95732b49 JA |
6924 | This section uses the version of @code{sh} included in SVR4.2 (the |
6925 | last version of the historical Bourne shell) as the baseline reference. | |
ccc6cda3 | 6926 | |
bb70624e | 6927 | @itemize @bullet |
ccc6cda3 | 6928 | |
ccc6cda3 | 6929 | @item |
bb70624e | 6930 | Bash is @sc{posix}-conformant, even where the @sc{posix} specification |
28ef6c31 | 6931 | differs from traditional @code{sh} behavior (@pxref{Bash POSIX Mode}). |
ccc6cda3 JA |
6932 | |
6933 | @item | |
bb70624e | 6934 | Bash has multi-character invocation options (@pxref{Invoking Bash}). |
ccc6cda3 JA |
6935 | |
6936 | @item | |
bb70624e JA |
6937 | Bash has command-line editing (@pxref{Command Line Editing}) and |
6938 | the @code{bind} builtin. | |
ccc6cda3 JA |
6939 | |
6940 | @item | |
bb70624e JA |
6941 | Bash provides a programmable word completion mechanism |
6942 | (@pxref{Programmable Completion}), and two builtin commands, | |
6943 | @code{complete} and @code{compgen}, to manipulate it. | |
ccc6cda3 | 6944 | |
bb70624e JA |
6945 | @item |
6946 | Bash has command history (@pxref{Bash History Facilities}) and the | |
6947 | @code{history} and @code{fc} builtins to manipulate it. | |
b80f6443 JA |
6948 | The Bash history list maintains timestamp information and uses the |
6949 | value of the @code{HISTTIMEFORMAT} variable to display it. | |
ccc6cda3 | 6950 | |
bb70624e JA |
6951 | @item |
6952 | Bash implements @code{csh}-like history expansion | |
6953 | (@pxref{History Interaction}). | |
ccc6cda3 | 6954 | |
bb70624e JA |
6955 | @item |
6956 | Bash has one-dimensional array variables (@pxref{Arrays}), and the | |
6957 | appropriate variable expansions and assignment syntax to use them. | |
6958 | Several of the Bash builtins take options to act on arrays. | |
6959 | Bash provides a number of built-in array variables. | |
ccc6cda3 | 6960 | |
bb70624e JA |
6961 | @item |
6962 | The @code{$'@dots{}'} quoting syntax, which expands ANSI-C | |
6963 | backslash-escaped characters in the text between the single quotes, | |
6964 | is supported (@pxref{ANSI-C Quoting}). | |
ccc6cda3 | 6965 | |
bb70624e JA |
6966 | @item |
6967 | Bash supports the @code{$"@dots{}"} quoting syntax to do | |
6968 | locale-specific translation of the characters between the double | |
28ef6c31 | 6969 | quotes. The @option{-D}, @option{--dump-strings}, and @option{--dump-po-strings} |
bb70624e JA |
6970 | invocation options list the translatable strings found in a script |
6971 | (@pxref{Locale Translation}). | |
ccc6cda3 | 6972 | |
bb70624e JA |
6973 | @item |
6974 | Bash implements the @code{!} keyword to negate the return value of | |
6975 | a pipeline (@pxref{Pipelines}). | |
6976 | Very useful when an @code{if} statement needs to act only if a test fails. | |
95732b49 JA |
6977 | The Bash @samp{-o pipefail} option to @code{set} will cause a pipeline to |
6978 | return a failure status if any command fails. | |
ccc6cda3 | 6979 | |
bb70624e JA |
6980 | @item |
6981 | Bash has the @code{time} reserved word and command timing (@pxref{Pipelines}). | |
6982 | The display of the timing statistics may be controlled with the | |
28ef6c31 | 6983 | @env{TIMEFORMAT} variable. |
ccc6cda3 | 6984 | |
bb70624e JA |
6985 | @item |
6986 | Bash implements the @code{for (( @var{expr1} ; @var{expr2} ; @var{expr3} ))} | |
6987 | arithmetic for command, similar to the C language (@pxref{Looping Constructs}). | |
ccc6cda3 | 6988 | |
bb70624e JA |
6989 | @item |
6990 | Bash includes the @code{select} compound command, which allows the | |
6991 | generation of simple menus (@pxref{Conditional Constructs}). | |
ccc6cda3 | 6992 | |
bb70624e JA |
6993 | @item |
6994 | Bash includes the @code{[[} compound command, which makes conditional | |
95732b49 JA |
6995 | testing part of the shell grammar (@pxref{Conditional Constructs}), including |
6996 | optional regular expression matching. | |
6997 | ||
6998 | @item | |
6999 | Bash provides optional case-insensitive matching for the @code{case} and | |
7000 | @code{[[} constructs. | |
ccc6cda3 | 7001 | |
bb70624e JA |
7002 | @item |
7003 | Bash includes brace expansion (@pxref{Brace Expansion}) and tilde | |
7004 | expansion (@pxref{Tilde Expansion}). | |
ccc6cda3 | 7005 | |
bb70624e JA |
7006 | @item |
7007 | Bash implements command aliases and the @code{alias} and @code{unalias} | |
7008 | builtins (@pxref{Aliases}). | |
ccc6cda3 | 7009 | |
bb70624e JA |
7010 | @item |
7011 | Bash provides shell arithmetic, the @code{((} compound command | |
7012 | (@pxref{Conditional Constructs}), | |
7013 | and arithmetic expansion (@pxref{Shell Arithmetic}). | |
ccc6cda3 | 7014 | |
bb70624e JA |
7015 | @item |
7016 | Variables present in the shell's initial environment are automatically | |
7017 | exported to child processes. The Bourne shell does not normally do | |
7018 | this unless the variables are explicitly marked using the @code{export} | |
7019 | command. | |
ccc6cda3 | 7020 | |
95732b49 JA |
7021 | @item |
7022 | Bash supports the @samp{+=} assignment operator, which appends to the value | |
7023 | of the variable named on the left hand side. | |
7024 | ||
bb70624e JA |
7025 | @item |
7026 | Bash includes the @sc{posix} pattern removal @samp{%}, @samp{#}, @samp{%%} | |
7027 | and @samp{##} expansions to remove leading or trailing substrings from | |
7028 | variable values (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7029 | |
bb70624e JA |
7030 | @item |
7031 | The expansion @code{$@{#xx@}}, which returns the length of @code{$@{xx@}}, | |
7032 | is supported (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7033 | |
bb70624e JA |
7034 | @item |
7035 | The expansion @code{$@{var:}@var{offset}@code{[:}@var{length}@code{]@}}, | |
7036 | which expands to the substring of @code{var}'s value of length | |
7037 | @var{length}, beginning at @var{offset}, is present | |
7038 | (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7039 | |
bb70624e JA |
7040 | @item |
7041 | The expansion | |
7042 | @code{$@{var/[/]}@var{pattern}@code{[/}@var{replacement}@code{]@}}, | |
7043 | which matches @var{pattern} and replaces it with @var{replacement} in | |
7044 | the value of @code{var}, is available (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7045 | |
bb70624e JA |
7046 | @item |
7047 | The expansion @code{$@{!@var{prefix@}*}} expansion, which expands to | |
7048 | the names of all shell variables whose names begin with @var{prefix}, | |
7049 | is available (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7050 | |
bb70624e JA |
7051 | @item |
7052 | Bash has @var{indirect} variable expansion using @code{$@{!word@}} | |
7053 | (@pxref{Shell Parameter Expansion}). | |
ccc6cda3 | 7054 | |
bb70624e JA |
7055 | @item |
7056 | Bash can expand positional parameters beyond @code{$9} using | |
7057 | @code{$@{@var{num}@}}. | |
ccc6cda3 | 7058 | |
bb70624e JA |
7059 | @item |
7060 | The @sc{posix} @code{$()} form of command substitution | |
7061 | is implemented (@pxref{Command Substitution}), | |
7062 | and preferred to the Bourne shell's @code{``} (which | |
7063 | is also implemented for backwards compatibility). | |
ccc6cda3 | 7064 | |
bb70624e JA |
7065 | @item |
7066 | Bash has process substitution (@pxref{Process Substitution}). | |
ccc6cda3 | 7067 | |
bb70624e JA |
7068 | @item |
7069 | Bash automatically assigns variables that provide information about the | |
28ef6c31 JA |
7070 | current user (@env{UID}, @env{EUID}, and @env{GROUPS}), the current host |
7071 | (@env{HOSTTYPE}, @env{OSTYPE}, @env{MACHTYPE}, and @env{HOSTNAME}), | |
7072 | and the instance of Bash that is running (@env{BASH}, | |
7073 | @env{BASH_VERSION}, and @env{BASH_VERSINFO}). @xref{Bash Variables}, | |
bb70624e | 7074 | for details. |
ccc6cda3 | 7075 | |
bb70624e | 7076 | @item |
28ef6c31 | 7077 | The @env{IFS} variable is used to split only the results of expansion, |
bb70624e JA |
7078 | not all words (@pxref{Word Splitting}). |
7079 | This closes a longstanding shell security hole. | |
ccc6cda3 | 7080 | |
bb70624e JA |
7081 | @item |
7082 | Bash implements the full set of @sc{posix} 1003.2 filename expansion operators, | |
7083 | including @var{character classes}, @var{equivalence classes}, and | |
7084 | @var{collating symbols} (@pxref{Filename Expansion}). | |
ccc6cda3 | 7085 | |
bb70624e JA |
7086 | @item |
7087 | Bash implements extended pattern matching features when the @code{extglob} | |
7088 | shell option is enabled (@pxref{Pattern Matching}). | |
ccc6cda3 | 7089 | |
bb70624e JA |
7090 | @item |
7091 | It is possible to have a variable and a function with the same name; | |
7092 | @code{sh} does not separate the two name spaces. | |
ccc6cda3 | 7093 | |
bb70624e JA |
7094 | @item |
7095 | Bash functions are permitted to have local variables using the | |
7096 | @code{local} builtin, and thus useful recursive functions may be written | |
7097 | (@pxref{Bash Builtins}). | |
ccc6cda3 | 7098 | |
bb70624e JA |
7099 | @item |
7100 | Variable assignments preceding commands affect only that command, even | |
7101 | builtins and functions (@pxref{Environment}). | |
7102 | In @code{sh}, all variable assignments | |
7103 | preceding commands are global unless the command is executed from the | |
7104 | file system. | |
ccc6cda3 | 7105 | |
bb70624e JA |
7106 | @item |
7107 | Bash performs filename expansion on filenames specified as operands | |
7108 | to input and output redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 7109 | |
bb70624e JA |
7110 | @item |
7111 | Bash contains the @samp{<>} redirection operator, allowing a file to be | |
7112 | opened for both reading and writing, and the @samp{&>} redirection | |
7113 | operator, for directing standard output and standard error to the same | |
7114 | file (@pxref{Redirections}). | |
ccc6cda3 | 7115 | |
95732b49 JA |
7116 | @item |
7117 | Bash includes the @samp{<<<} redirection operator, allowing a string to | |
7118 | be used as the standard input to a command. | |
7119 | ||
7120 | @item | |
7121 | Bash implements the @samp{[n]<&@var{word}} and @samp{[n]>&@var{word}} | |
7122 | redirection operators, which move one file descriptor to another. | |
7123 | ||
bb70624e JA |
7124 | @item |
7125 | Bash treats a number of filenames specially when they are | |
7126 | used in redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 7127 | |
bb70624e JA |
7128 | @item |
7129 | Bash can open network connections to arbitrary machines and services | |
7130 | with the redirection operators (@pxref{Redirections}). | |
ccc6cda3 | 7131 | |
bb70624e JA |
7132 | @item |
7133 | The @code{noclobber} option is available to avoid overwriting existing | |
7134 | files with output redirection (@pxref{The Set Builtin}). | |
7135 | The @samp{>|} redirection operator may be used to override @code{noclobber}. | |
d166f048 | 7136 | |
bb70624e JA |
7137 | @item |
7138 | The Bash @code{cd} and @code{pwd} builtins (@pxref{Bourne Shell Builtins}) | |
28ef6c31 | 7139 | each take @option{-L} and @option{-P} options to switch between logical and |
bb70624e | 7140 | physical modes. |
d166f048 | 7141 | |
bb70624e JA |
7142 | @item |
7143 | Bash allows a function to override a builtin with the same name, and provides | |
7144 | access to that builtin's functionality within the function via the | |
7145 | @code{builtin} and @code{command} builtins (@pxref{Bash Builtins}). | |
d166f048 | 7146 | |
bb70624e JA |
7147 | @item |
7148 | The @code{command} builtin allows selective disabling of functions | |
7149 | when command lookup is performed (@pxref{Bash Builtins}). | |
ccc6cda3 | 7150 | |
bb70624e JA |
7151 | @item |
7152 | Individual builtins may be enabled or disabled using the @code{enable} | |
7153 | builtin (@pxref{Bash Builtins}). | |
b72432fd | 7154 | |
bb70624e JA |
7155 | @item |
7156 | The Bash @code{exec} builtin takes additional options that allow users | |
7157 | to control the contents of the environment passed to the executed | |
7158 | command, and what the zeroth argument to the command is to be | |
7159 | (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 7160 | |
bb70624e JA |
7161 | @item |
7162 | Shell functions may be exported to children via the environment | |
7163 | using @code{export -f} (@pxref{Shell Functions}). | |
ccc6cda3 | 7164 | |
bb70624e JA |
7165 | @item |
7166 | The Bash @code{export}, @code{readonly}, and @code{declare} builtins can | |
28ef6c31 | 7167 | take a @option{-f} option to act on shell functions, a @option{-p} option to |
bb70624e | 7168 | display variables with various attributes set in a format that can be |
28ef6c31 | 7169 | used as shell input, a @option{-n} option to remove various variable |
bb70624e JA |
7170 | attributes, and @samp{name=value} arguments to set variable attributes |
7171 | and values simultaneously. | |
cce855bc | 7172 | |
bb70624e JA |
7173 | @item |
7174 | The Bash @code{hash} builtin allows a name to be associated with | |
7175 | an arbitrary filename, even when that filename cannot be found by | |
28ef6c31 | 7176 | searching the @env{$PATH}, using @samp{hash -p} |
bb70624e | 7177 | (@pxref{Bourne Shell Builtins}). |
cce855bc | 7178 | |
bb70624e JA |
7179 | @item |
7180 | Bash includes a @code{help} builtin for quick reference to shell | |
7181 | facilities (@pxref{Bash Builtins}). | |
cce855bc | 7182 | |
bb70624e JA |
7183 | @item |
7184 | The @code{printf} builtin is available to display formatted output | |
7185 | (@pxref{Bash Builtins}). | |
ccc6cda3 | 7186 | |
bb70624e JA |
7187 | @item |
7188 | The Bash @code{read} builtin (@pxref{Bash Builtins}) | |
7189 | will read a line ending in @samp{\} with | |
28ef6c31 | 7190 | the @option{-r} option, and will use the @env{REPLY} variable as a |
bb70624e JA |
7191 | default if no non-option arguments are supplied. |
7192 | The Bash @code{read} builtin | |
28ef6c31 JA |
7193 | also accepts a prompt string with the @option{-p} option and will use |
7194 | Readline to obtain the line when given the @option{-e} option. | |
bb70624e | 7195 | The @code{read} builtin also has additional options to control input: |
28ef6c31 JA |
7196 | the @option{-s} option will turn off echoing of input characters as |
7197 | they are read, the @option{-t} option will allow @code{read} to time out | |
bb70624e | 7198 | if input does not arrive within a specified number of seconds, the |
28ef6c31 JA |
7199 | @option{-n} option will allow reading only a specified number of |
7200 | characters rather than a full line, and the @option{-d} option will read | |
bb70624e | 7201 | until a particular character rather than newline. |
ccc6cda3 | 7202 | |
bb70624e JA |
7203 | @item |
7204 | The @code{return} builtin may be used to abort execution of scripts | |
7205 | executed with the @code{.} or @code{source} builtins | |
7206 | (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 7207 | |
bb70624e JA |
7208 | @item |
7209 | Bash includes the @code{shopt} builtin, for finer control of shell | |
f73dda09 JA |
7210 | optional capabilities (@pxref{Bash Builtins}), and allows these options |
7211 | to be set and unset at shell invocation (@pxref{Invoking Bash}). | |
ccc6cda3 | 7212 | |
bb70624e JA |
7213 | @item |
7214 | Bash has much more optional behavior controllable with the @code{set} | |
7215 | builtin (@pxref{The Set Builtin}). | |
ccc6cda3 | 7216 | |
b80f6443 JA |
7217 | @item |
7218 | The @samp{-x} (@code{xtrace}) option displays commands other than | |
7219 | simple commands when performing an execution trace | |
7220 | (@pxref{The Set Builtin}). | |
7221 | ||
bb70624e JA |
7222 | @item |
7223 | The @code{test} builtin (@pxref{Bourne Shell Builtins}) | |
7224 | is slightly different, as it implements the @sc{posix} algorithm, | |
7225 | which specifies the behavior based on the number of arguments. | |
d166f048 | 7226 | |
b80f6443 JA |
7227 | @item |
7228 | Bash includes the @code{caller} builtin, which displays the context of | |
7229 | any active subroutine call (a shell function or a script executed with | |
7230 | the @code{.} or @code{source} builtins). This supports the bash | |
7231 | debugger. | |
7232 | ||
bb70624e | 7233 | @item |
f73dda09 JA |
7234 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a |
7235 | @code{DEBUG} pseudo-signal specification, similar to @code{EXIT}. | |
b80f6443 JA |
7236 | Commands specified with a @code{DEBUG} trap are executed before every |
7237 | simple command, @code{for} command, @code{case} command, | |
7238 | @code{select} command, every arithmetic @code{for} command, and before | |
7239 | the first command executes in a shell function. | |
7117c2d2 | 7240 | The @code{DEBUG} trap is not inherited by shell functions unless the |
b80f6443 JA |
7241 | function has been given the @code{trace} attribute or the |
7242 | @code{functrace} option has been enabled using the @code{shopt} builtin. | |
7243 | The @code{extdebug} shell option has additional effects on the | |
7244 | @code{DEBUG} trap. | |
f73dda09 JA |
7245 | |
7246 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows an | |
7247 | @code{ERR} pseudo-signal specification, similar to @code{EXIT} and @code{DEBUG}. | |
7248 | Commands specified with an @code{ERR} trap are executed after a simple | |
7249 | command fails, with a few exceptions. | |
b80f6443 JA |
7250 | The @code{ERR} trap is not inherited by shell functions unless the |
7251 | @code{-o errtrace} option to the @code{set} builtin is enabled. | |
7252 | ||
7253 | The @code{trap} builtin (@pxref{Bourne Shell Builtins}) allows a | |
7254 | @code{RETURN} pseudo-signal specification, similar to | |
7255 | @code{EXIT} and @code{DEBUG}. | |
7256 | Commands specified with an @code{RETURN} trap are executed before | |
7257 | execution resumes after a shell function or a shell script executed with | |
7258 | @code{.} or @code{source} returns. | |
95732b49 JA |
7259 | The @code{RETURN} trap is not inherited by shell functions unless the |
7260 | function has been given the @code{trace} attribute or the | |
7261 | @code{functrace} option has been enabled using the @code{shopt} builtin. | |
d166f048 | 7262 | |
bb70624e JA |
7263 | @item |
7264 | The Bash @code{type} builtin is more extensive and gives more information | |
7265 | about the names it finds (@pxref{Bash Builtins}). | |
cce855bc | 7266 | |
bb70624e | 7267 | @item |
28ef6c31 | 7268 | The Bash @code{umask} builtin permits a @option{-p} option to cause |
bb70624e JA |
7269 | the output to be displayed in the form of a @code{umask} command |
7270 | that may be reused as input (@pxref{Bourne Shell Builtins}). | |
ccc6cda3 | 7271 | |
bb70624e JA |
7272 | @item |
7273 | Bash implements a @code{csh}-like directory stack, and provides the | |
7274 | @code{pushd}, @code{popd}, and @code{dirs} builtins to manipulate it | |
7275 | (@pxref{The Directory Stack}). | |
7276 | Bash also makes the directory stack visible as the value of the | |
28ef6c31 | 7277 | @env{DIRSTACK} shell variable. |
d166f048 | 7278 | |
bb70624e JA |
7279 | @item |
7280 | Bash interprets special backslash-escaped characters in the prompt | |
7281 | strings when interactive (@pxref{Printing a Prompt}). | |
cce855bc | 7282 | |
bb70624e JA |
7283 | @item |
7284 | The Bash restricted mode is more useful (@pxref{The Restricted Shell}); | |
7285 | the SVR4.2 shell restricted mode is too limited. | |
d166f048 | 7286 | |
bb70624e JA |
7287 | @item |
7288 | The @code{disown} builtin can remove a job from the internal shell | |
7289 | job table (@pxref{Job Control Builtins}) or suppress the sending | |
7290 | of @code{SIGHUP} to a job when the shell exits as the result of a | |
7291 | @code{SIGHUP}. | |
d166f048 | 7292 | |
95732b49 JA |
7293 | @item |
7294 | Bash includes a number of features to support a separate debugger for | |
7295 | shell scripts. | |
7296 | ||
bb70624e JA |
7297 | @item |
7298 | The SVR4.2 shell has two privilege-related builtins | |
7299 | (@code{mldmode} and @code{priv}) not present in Bash. | |
d166f048 | 7300 | |
bb70624e JA |
7301 | @item |
7302 | Bash does not have the @code{stop} or @code{newgrp} builtins. | |
ccc6cda3 | 7303 | |
bb70624e | 7304 | @item |
28ef6c31 | 7305 | Bash does not use the @env{SHACCT} variable or perform shell accounting. |
ccc6cda3 | 7306 | |
bb70624e | 7307 | @item |
28ef6c31 JA |
7308 | The SVR4.2 @code{sh} uses a @env{TIMEOUT} variable like Bash uses |
7309 | @env{TMOUT}. | |
ccc6cda3 | 7310 | |
bb70624e | 7311 | @end itemize |
d166f048 | 7312 | |
bb70624e JA |
7313 | @noindent |
7314 | More features unique to Bash may be found in @ref{Bash Features}. | |
d166f048 | 7315 | |
ccc6cda3 | 7316 | |
bb70624e | 7317 | @appendixsec Implementation Differences From The SVR4.2 Shell |
ccc6cda3 | 7318 | |
bb70624e JA |
7319 | Since Bash is a completely new implementation, it does not suffer from |
7320 | many of the limitations of the SVR4.2 shell. For instance: | |
ccc6cda3 | 7321 | |
bb70624e | 7322 | @itemize @bullet |
ccc6cda3 | 7323 | |
bb70624e JA |
7324 | @item |
7325 | Bash does not fork a subshell when redirecting into or out of | |
7326 | a shell control structure such as an @code{if} or @code{while} | |
7327 | statement. | |
ccc6cda3 | 7328 | |
bb70624e JA |
7329 | @item |
7330 | Bash does not allow unbalanced quotes. The SVR4.2 shell will silently | |
7331 | insert a needed closing quote at @code{EOF} under certain circumstances. | |
7332 | This can be the cause of some hard-to-find errors. | |
ccc6cda3 | 7333 | |
bb70624e JA |
7334 | @item |
7335 | The SVR4.2 shell uses a baroque memory management scheme based on | |
7336 | trapping @code{SIGSEGV}. If the shell is started from a process with | |
7337 | @code{SIGSEGV} blocked (e.g., by using the @code{system()} C library | |
7338 | function call), it misbehaves badly. | |
ccc6cda3 | 7339 | |
ccc6cda3 | 7340 | @item |
bb70624e | 7341 | In a questionable attempt at security, the SVR4.2 shell, |
28ef6c31 | 7342 | when invoked without the @option{-p} option, will alter its real |
bb70624e JA |
7343 | and effective @sc{uid} and @sc{gid} if they are less than some |
7344 | magic threshold value, commonly 100. | |
7345 | This can lead to unexpected results. | |
7346 | ||
ccc6cda3 | 7347 | @item |
bb70624e JA |
7348 | The SVR4.2 shell does not allow users to trap @code{SIGSEGV}, |
7349 | @code{SIGALRM}, or @code{SIGCHLD}. | |
7350 | ||
ccc6cda3 | 7351 | @item |
28ef6c31 JA |
7352 | The SVR4.2 shell does not allow the @env{IFS}, @env{MAILCHECK}, |
7353 | @env{PATH}, @env{PS1}, or @env{PS2} variables to be unset. | |
bb70624e | 7354 | |
ccc6cda3 | 7355 | @item |
bb70624e JA |
7356 | The SVR4.2 shell treats @samp{^} as the undocumented equivalent of |
7357 | @samp{|}. | |
7358 | ||
ccc6cda3 | 7359 | @item |
bb70624e JA |
7360 | Bash allows multiple option arguments when it is invoked (@code{-x -v}); |
7361 | the SVR4.2 shell allows only one option argument (@code{-xv}). In | |
7362 | fact, some versions of the shell dump core if the second argument begins | |
7363 | with a @samp{-}. | |
ccc6cda3 | 7364 | |
bb70624e JA |
7365 | @item |
7366 | The SVR4.2 shell exits a script if any builtin fails; Bash exits | |
7367 | a script only if one of the @sc{posix} 1003.2 special builtins fails, and | |
7368 | only for certain failures, as enumerated in the @sc{posix} 1003.2 standard. | |
ccc6cda3 | 7369 | |
bb70624e JA |
7370 | @item |
7371 | The SVR4.2 shell behaves differently when invoked as @code{jsh} | |
7372 | (it turns on job control). | |
7373 | @end itemize | |
ccc6cda3 | 7374 | |
b80f6443 JA |
7375 | @node Copying This Manual |
7376 | @appendix Copying This Manual | |
7377 | ||
7378 | @menu | |
7379 | * GNU Free Documentation License:: License for copying this manual. | |
7380 | @end menu | |
7381 | ||
7382 | @include fdl.texi | |
7383 | ||
ccc6cda3 | 7384 | @node Builtin Index |
bb70624e | 7385 | @unnumbered Index of Shell Builtin Commands |
ccc6cda3 JA |
7386 | @printindex bt |
7387 | ||
7388 | @node Reserved Word Index | |
bb70624e | 7389 | @unnumbered Index of Shell Reserved Words |
ccc6cda3 JA |
7390 | @printindex rw |
7391 | ||
7392 | @node Variable Index | |
bb70624e | 7393 | @unnumbered Parameter and Variable Index |
ccc6cda3 JA |
7394 | @printindex vr |
7395 | ||
7396 | @node Function Index | |
bb70624e | 7397 | @unnumbered Function Index |
ccc6cda3 JA |
7398 | @printindex fn |
7399 | ||
7400 | @node Concept Index | |
bb70624e | 7401 | @unnumbered Concept Index |
ccc6cda3 JA |
7402 | @printindex cp |
7403 | ||
ccc6cda3 | 7404 | @bye |