Penguin
Blame: bash(1)Part5
EditPageHistoryDiffInfoLikePages
Annotated edit history of bash(1)Part5 version 2, including all changes. View license author blame.
Rev Author # Line
1 JohnMcPherson 1 !!BASH MAN PAGE (Part 5)
2
3 __Navigation__%%%
4 This man page has been split into 7 pages:%%%
5 bash(1)
6 * NAME
7 * SYNOPSIS
8 * COPYRIGHT
9 * DESCRIPTION
10 * OPTIONS
11 * ARGUMENTS
12 * INVOCATION
13
14 [bash(1)Part2]
15 * DEFINITIONS
16 * RESERVED WORDS
17 * SHELL GRAMMAR
18 * COMMENTS
19 * QUOTING
20
21 [bash(1)Part3]
22 * PARAMETERS
23
24 [bash(1)Part4]
25 * EXPANSION
26
27 __Part 5__
28 * __REDIRECTION__
29 * __ALIASES__
30 * __FUNCTIONS__
31 * __ARITHMETIC EVALUATION__
32 * __CONDITIONAL EXPRESSIONS__
33 * __SIMPLE COMMAND EXPANSION__
34 * __COMMAND EXECUTION__
35 * __COMMAND EXECUTION ENVIRONMENT__
36 * __ENVIRONMENT__
37 * __EXIT STATUS__
38 * __SIGNALS__
39 * __JOB CONTROL__
40 * __PROMPTING__
41
42 [bash(1)Part6]
43 * READLINE
44 * HISTORY
45 * HISTORY EXPANSION
46
47
48 [bash(1)Part7]
49 * SHELL BUILTIN COMMANDS
50 * RESTRICTED SHELL
51 * SEE ALSO
52 * FILES
53 * AUTHORS
54 * BUG REPORTS
55 * BUGS
56
57 ----
58 !!REDIRECTION
59 Before a command is executed, its input and output may be ''redirected'' using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment. The following redirection operators may precede or appear anywhere within a ''simple command'' or may follow a ''command'' . Redirections are processed in the order they appear, from left to right.
60
61 In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is __<__ , the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is __>__ , the redirection refers to the standard output (file descriptor 1).
62
63 The word following the redirection operator in the following descriptions, unless otherwise noted, is subjected to brace expansion, tilde expansion, parameter expansion, command substitution, arithmetic expansion, quote removal, pathname expansion, and word splitting. If it expands to more than one word, __bash__ reports an error.
64
65 Note that the order of redirections is significant. For example, the command
66
67 ls __>__ dirlist 2__>&__1
68
69
70 directs both standard output and standard error to the file ''dirlist'' , while the command
71
72 ls 2__>&__1 __>__ dirlist
73
74
75 directs only the standard output to file ''dirlist'' , because the standard error was duplicated as standard output before the standard output was redirected to ''dirlist'' .
76
77 __Bash__ handles several filenames specially when they are used in redirections, as described in the following table:
78
79
80
81 ;__/dev/fd/''fd''__ : If ''fd'' is a valid integer, file descriptor ''fd'' is duplicated.
82 ;__/dev/stdin__ : File descriptor 0 is duplicated.
83 ;__/dev/stdout__ : File descriptor 1 is duplicated.
84 ;__/dev/stderr__ : File descriptor 2 is duplicated.
85 ;__/dev/tcp/''host''/''port''__ : If ''host'' is a valid hostname or Internet address, and ''port'' is an integer port number, __bash__ attempts to open a TCP connection to the corresponding socket.
86 ;__/dev/udp/''host''/''port''__ : If ''host'' is a valid hostname or Internet address, and ''port'' is an integer port number, __bash__ attempts to open a UDP connection to the corresponding socket.
87
88
89
90
91 A failure to open or create a file causes the redirection to fail.
92
93 !Redirecting Input
94
95
96 Redirection of input causes the file whose name results from the expansion of ''word'' to be opened for reading on file descriptor ''n'' , or the standard input (file descriptor 0) if ''n'' is not specified.
97
98 The general format for redirecting input is:
99
100 [[''n'']__<__''word''
101
102
103 !Redirecting Output
104
105
106 Redirection of output causes the file whose name results from the expansion of ''word'' to be opened for writing on file descriptor ''n'' , or the standard output (file descriptor 1) if ''n'' is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size.
107
108 The general format for redirecting output is:
109
110 [[''n'']__>__''word''
111
112
113 If the redirection operator is __>__ , and the __noclobber__ option to the __set__ builtin has been enabled, the redirection will fail if the file whose name results from the expansion of ''word'' exists and is a regular file. If the redirection operator is __>|__ , or the redirection operator is __>__ and the __noclobber__ option to the __set__ builtin command is not enabled, the redirection is attempted even if the file named by ''word'' exists.
114
115 !Appending Redirected Output
116
117
118 Redirection of output in this fashion causes the file whose name results from the expansion of ''word'' to be opened for appending on file descriptor ''n'' , or the standard output (file descriptor 1) if ''n'' is not specified. If the file does not exist it is created.
119
120 The general format for appending output is:
121
122 [[''n'']__>>__''word''
123
124
125
126
127 !Redirecting Standard Output and Standard Error
128
129
130 __Bash__ allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of ''word'' with this construct.
131
132 There are two formats for redirecting standard output and standard error:
133
134 __&>__''word''
135 and
136 __>&__''word''
137
138
139 Of the two forms, the first is preferred. This is semantically equivalent to
140
141 __>__''word'' 2__>&__1
142
143
144 !Here Documents
145
146
147 This type of redirection instructs the shell to read input from the current source until a line containing only ''word'' (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.
148
149 The format of here-documents is as follows:
150
151
152 __<<__[[__-__]''word'' %%%
153
154 ''here-document'' %%%
155 ''delimiter'' %%%
156
157
158
159
160 No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on ''word'' . If any characters in ''word'' are quoted, the ''delimiter'' is the result of quote removal on ''word'' , and the lines in the here-document are not expanded. If ''word'' is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence __\<newline>__ is ignored, and __\__ must be used to quote the characters __\__ , __$__ , and __`__ .
161
162 If the redirection operator is __<<-__ , then all leading tab characters are stripped from input lines and the line containing ''delimiter'' . This allows here-documents within shell scripts to be indented in a natural fashion.
163
164 !"Duplicating File Descriptors"
165
166
167 The redirection operator
168
169 [[''n'']__<&__''word''
170
171
172 is used to duplicate input file descriptors. If ''word'' expands to one or more digits, the file descriptor denoted by ''n'' is made to be a copy of that file descriptor. If the digits in ''word'' do not specify a file descriptor open for input, a redirection error occurs. If ''word'' evaluates to __-__ , file descriptor ''n'' is closed. If ''n'' is not specified, the standard input (file descriptor 0) is used.
173
174 The operator
175
176 [[''n'']__>&__''word''
177
178
179 is used similarly to duplicate output file descriptors. If ''n'' is not specified, the standard output (file descriptor 1) is used. If the digits in ''word'' do not specify a file descriptor open for output, a redirection error occurs. As a special case, if ''n'' is omitted, and ''word'' does not expand to one or more digits, the standard output and standard error are redirected as described previously.
180
181 !"Opening File Descriptors for Reading and Writing"
182
183
184 The redirection operator
185
186 [[''n'']__<>__''word''
187
188
189 causes the file whose name is the expansion of ''word'' to be opened for both reading and writing on file descriptor ''n'' , or on file descriptor 0 if ''n'' is not specified. If the file does not exist, it is created.
190
191 !!ALIASES
192 ''Aliases'' allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the __alias__ and __unalias__ builtin commands (see __SHELL BUILTIN COMMANDS__ below). The first word of each command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias. The alias name and the replacement text may contain any valid shell input, including the ''metacharacters'' listed above, with the exception that the alias name may not contain ''=''. The first word of the replacement text is tested for aliases, but a word that is identical to an alias being expanded is not expanded a second time. This means that one may alias __ls__ to __ls -F__ , for instance, and __bash__ does not try to recursively expand the replacement text. If the last character of the alias value is a ''blank'' , then the next command word following the alias is also checked for alias expansion.
193
194 Aliases are created and listed with the __alias__ command, and removed with the __unalias__ command.
195
196 There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see __FUNCTIONS__ below).
197
198 Aliases are not expanded when the shell is not interactive, unless the __expand_aliases__ shell option is set using __shopt__ (see the description of __shopt__ under __SHELL BUILTIN COMMANDS__ below).
199
200 The rules concerning the definition and use of aliases are somewhat confusing. __Bash__ always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed. To be safe, always put alias definitions on a separate line, and do not use __alias__ in compound commands.
201
202 For almost every purpose, aliases are superseded by shell functions.
203
204 !!FUNCTIONS
205 A shell function, defined as described above under __SHELL GRAMMAR__ , stores a series of commands for later execution. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Functions are executed in the context of the current shell; no new process is created to interpret them (contrast this with the execution of a shell script). When a function is executed, the arguments to the function become the positional parameters during its execution. The special parameter __#__ is updated to reflect the change. Positional parameter 0 is unchanged. The __FUNCNAME__ variable is set to the name of the function while the function is executing. All other aspects of the shell execution environment are identical between a function and its caller with the exception that the __DEBUG__ trap (see the description of the __trap__ builtin under __SHELL BUILTIN COMMANDS__ below) is not inherited.
206
207 Variables local to the function may be declared with the __local__ builtin command. Ordinarily, variables and their values are shared between the function and its caller.
208
209 If the builtin command __return__ is executed in a function, the function completes and execution resumes with the next command after the function call. When a function completes, the values of the positional parameters and the special parameter __#__ are restored to the values they had prior to the function's execution.
210
211 Function names and definitions may be listed with the __-f__ option to the __declare__ or __typeset__ builtin commands. The __-F__ option to __declare__ or __typeset__ will list the function names only. Functions may be exported so that subshells automatically have them defined with the __-f__ option to the __export__ builtin.
212
213 Functions may be recursive. No limit is imposed on the number of recursive calls.
214
215 !!ARITHMETIC EVALUATION
216 The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the __let__ builtin command and __Arithmetic Expansion__). Evaluation is done in long integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence and associativity are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
217
218
219 ;__''id''++ ''id''-__ : variable post-increment and post-decrement
220 ;__++''id'' -''id''__ : variable pre-increment and pre-decrement
221 ;__- +__ : unary minus and plus
222 ;__! ~__ : logical and bitwise negation
223 ;__**__ : exponentiation
224 ;__* / %__ : multiplication, division, remainder
225 ;__+ -__ : addition, subtraction
226 ;__<< >>__ : left and right bitwise shifts
227 ;__<= >= < >__ : comparison
228 ;__== !=__ : equality and inequality
229 ;__&__ : bitwise AND
230 ;__^__ : bitwise exclusive OR
231 ;__|__ : bitwise OR
232 ;__&&__ : logical AND
233 ;__||__ : logical OR
234
235 __''expr''?''expr'':''expr''__
236 ;: conditional evaluation
237 ;__= *= /= %= += -= <<= >>= &= ^= |=__ : assignment
238 ;__''expr1'' , ''expr2''__ : comma
239
240
241
242 Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is referenced. A shell variable need not have its integer attribute turned on to be used in an expression.
243
244 Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the form [[''base#'']n, where ''base'' is a decimal number between 2 and 64 representing the arithmetic base, and ''n'' is a number in that base. If ''base#'' is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, _, and @, in that order. If ''base'' is less than or equal to 36, lowercase and uppercase letters may be used interchangably to represent numbers between 10 and 35.
245
246 Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
247
248 !!CONDITIONAL EXPRESSIONS
249 Conditional expressions are used by the __[[[[__ compound command and the __test__ and __[[__ builtin commands to test file attributes and perform string and arithmetic comparisons. Expressions are formed from the following unary or binary primaries. If any ''file'' argument to one of the primaries is of the form ''/dev/fd/n'', then file descriptor ''n'' is checked. If the ''file'' argument to one of the primaries is one of ''/dev/stdin'', ''/dev/stdout'', or ''/dev/stderr'', file descriptor 0, 1, or 2, respectively, is checked.
250
251
252
253 ;__-a ''file''__ : True if ''file'' exists.
254 ;__-b ''file''__ : True if ''file'' exists and is a block special file.
255 ;__-c ''file''__ : True if ''file'' exists and is a character special file.
256 ;__-d ''file''__ : True if ''file'' exists and is a directory.
257 ;__-e ''file''__ : True if ''file'' exists.
258 ;__-f ''file''__ : True if ''file'' exists and is a regular file.
259 ;__-g ''file''__ : True if ''file'' exists and is set-group-id.
260 ;__-h ''file''__ : True if ''file'' exists and is a symbolic link.
261 ;__-k ''file''__ : True if ''file'' exists and its "sticky" bit is set.
262 ;__-p ''file''__ : True if ''file'' exists and is a named pipe (FIFO).
263 ;__-r ''file''__ : True if ''file'' exists and is readable.
264 ;__-s ''file''__ : True if ''file'' exists and has a size greater than zero.
265 ;__-t ''fd''__ : True if file descriptor ''fd'' is open and refers to a terminal.
266 ;__-u ''file''__ : True if ''file'' exists and its set-user-id bit is set.
267 ;__-w ''file''__ : True if ''file'' exists and is writable.
268 ;__-x ''file''__ : True if ''file'' exists and is executable.
269 ;__-O ''file''__ : True if ''file'' exists and is owned by the effective user id.
270 ;__-G ''file''__ : True if ''file'' exists and is owned by the effective group id.
271 ;__-L ''file''__ : True if ''file'' exists and is a symbolic link.
272 ;__-S ''file''__ : True if ''file'' exists and is a socket.
273 ;__-N ''file''__ : True if ''file'' exists and has been modified since it was last read.
274 ;''file1'' -__nt__ ''file2'' : True if ''file1'' is newer (according to modification date) than ''file2''.
275 ;''file1'' -__ot__ ''file2'' : True if ''file1'' is older than ''file2''.
276 ;''file1'' __-ef__ ''file2'' : True if ''file1'' and ''file2'' have the same device and inode numbers.
277 ;__-o ''optname''__ : True if shell option ''optname'' is enabled. See the list of options under the description of the __-o__ option to the __set__ builtin below.
278 ;__-z ''string''__ : True if the length of ''string'' is zero.
279 ;__-n ''string''__ :
280 ;''string'' : True if the length of ''string'' is non-zero.
281 ;''string1'' __==__ ''string2'' : True if the strings are equal. __=__ may be used in place of __==__.
282 ;''string1'' __!=__ ''string2'' : True if the strings are not equal.
283 ;''string1'' __<__ ''string2'' : True if ''string1'' sorts before ''string2'' lexicographically in the current locale.
284 ;''string1'' __>__ ''string2'' : True if ''string1'' sorts after ''string2'' lexicographically in the current locale.
285 ;''''arg1'' __OP__ ''arg2'''' : __OP__ is one of __-eq__ , __-ne__ , __-lt__ , __-le__ , __-gt__ , or __-ge__ . These arithmetic binary operators return true if ''arg1'' is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to ''arg2'', respectively. ''Arg1'' and ''arg2'' may be positive or negative integers.
286
287
288
289 !!SIMPLE COMMAND EXPANSION
290 When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.
291 ;1.: The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.
292 ;2.: The words that are not variable assignments or redirections are expanded. If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.
293 ;3.: Redirections are performed as described above under __REDIRECTION__ .
294 ;4.: The text after the __=__ in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.
295
296 If no command name results, the variable assignments affect the current shell environment. Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment. If any of the assignments attempts to assign a value to a readonly variable, an error occurs, and the command exits with a non-zero status.
297
298 If no command name results, redirections are performed, but do not affect the current shell environment. A redirection error causes the command to exit with a non-zero status.
299
300 If there is a command name left after expansion, execution proceeds as described below. Otherwise, the command exits. If one of the expansions contained a command substitution, the exit status of the command is the exit status of the last command substitution performed. If there were no command substitutions, the command exits with a status of zero.
301
302 !!COMMAND EXECUTION
303 After a command has been split into words, if it results in a simple command and an optional list of arguments, the following actions are taken.
304
305 If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, that function is invoked as described above in __FUNCTIONS__ . If the name does not match a function, the shell searches for it in the list of shell builtins. If a match is found, that builtin is invoked.
306
307 If the name is neither a shell function nor a builtin, and contains no slashes, __bash__ searches each element of the __PATH__ for a directory containing an executable file by that name. __Bash__ uses a hash table to remember the full pathnames of executable files (see __hash__ under __SHELL BUILTIN COMMANDS__ below). A full search of the directories in __PATH__ is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell prints an error message and returns an exit status of 127.
308
309 If the search is successful, or if the command name contains one or more slashes, the shell executes the named program in a separate execution environment. Argument 0 is set to the name given, and the remaining arguments to the command are set to the arguments given, if any.
310
311 If this execution fails because the file is not in executable format, and the file is not a directory, it is assumed to be a ''shell script'', a file containing shell commands. A subshell is spawned to execute it. This subshell reinitializes itself, so that the effect is as if a new shell had been invoked to handle the script, with the exception that the locations of commands remembered by the parent (see __hash__ below under __SHELL BUILTIN COMMANDS__) are retained by the child.
312
313 If the program is a file beginning with __#!__ , the remainder of the first line specifies an interpreter for the program. The shell executes the specified interpreter on operating systems that do not handle this executable format themselves. The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the program, followed by the name of the program, followed by the command arguments, if any.
314
315 !!COMMAND EXECUTION ENVIRONMENT
316 The shell has an ''execution environment'', which consists of the following:
317 * open files inherited by the shell at invocation, as modified by redirections supplied to the __exec__ builtin
318 * the current working directory as set by __cd__, __pushd__, or __popd__, or inherited by the shell at invocation
319 * the file creation mode mask as set by __umask__ or inherited from the shell's parent
320 * current traps set by __trap__
321 * shell parameters that are set by variable assignment or with __set__ or inherited from the shell's parent in the environment
322 * shell functions defined during execution or inherited from the shell's parent in the environment
323 * options enabled at invocation (either by default or with command-line arguments) or by __set__
324 * options enabled by __shopt__
325 * shell aliases defined with __alias__
326 * various process IDs, including those of background jobs, the value of __$$__, and the value of __$PPID__
327
328 When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment that consists of the following. Unless otherwise noted, the values are inherited from the shell.
329 * the shell's open files, plus any modifications and additions specified by redirections to the command
330 * the current working directory
331 * the file creation mode mask
332 * shell variables marked for export, along with variables exported for the command, passed in the environment
333 * traps caught by the shell are reset to the values the inherited from the shell's parent, and traps ignored by the shell are ignored
334
335 A command invoked in this separate environment cannot affect the shell's execution environment.
336
337 Command substitution and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation. Builtin commands that are invoked as part of a pipeline are also executed in a subshell environment. Changes made to the subshell environment cannot affect the shell's execution environment.
338
339 !!ENVIRONMENT
340 When a program is invoked it is given an array of strings called the ''environment'' . This is a list of ''name''-''value'' pairs, of the form ''name=value'' .
341
342 The shell provides several ways to manipulate the environment. On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for ''export'' to child processes. Executed commands inherit the environment. The __export__ and __declare -x__ commands allow parameters and functions to be added to and deleted from the environment. If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old. The environment inherited by any executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the __unset__ command, plus any additions via the __export__ and __declare -x__ commands.
343
344 The environment for any ''simple command'' or function may be augmented temporarily by prefixing it with parameter assignments, as described above in __PARAMETERS__ . These assignment statements affect only the environment seen by that command.
345
346 If the __-k__ option is set (see the __set__ builtin command below), then ''all'' parameter assignments are placed in the environment for a command, not just those that precede the command name.
347
348 When __bash__ invokes an external command, the variable _____ is set to the full file name of the command and passed to that command in its environment.
349
350 !!EXIT STATUS
351 For the shell's purposes, a command which exits with a zero exit status has succeeded. An exit status of zero indicates success. A non-zero exit status indicates failure. When a command terminates on a fatal signal ''N'', __bash__ uses the value of 128+''N'' as the exit status.
352
353 If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.
354
355 If a command fails because of an error during expansion or redirection, the exit status is greater than zero.
356
357 Shell builtin commands return a status of 0 (''true'') if successful, and non-zero (''false'') if an error occurs while they execute. All builtins return an exit status of 2 to indicate incorrect usage.
358
359 __Bash__ itself returns the exit status of the last command executed, unless a syntax error occurs, in which case it exits with a non-zero value. See also the __exit__ builtin command below.
360
361 !!SIGNALS
2 JohnMcPherson 362 When __bash__ is interactive, in the absence of any traps, it ignores __SIGTERM__ (so that __kill 0__ does not kill an interactive shell), and __SIGINT__ is caught and handled (so that the __wait__ builtin is interruptible). In all cases, __bash__ ignores [SIGQUIT]. If job control is in effect, __bash__ ignores [SIGTTIN], [SIGTTOU], and [SIGTSTP].
1 JohnMcPherson 363
2 JohnMcPherson 364 Synchronous jobs started by __bash__ have signal handlers set to the values inherited by the shell from its parent. When job control is not in effect, asynchronous commands ignore [SIGINT] and [SIGQUIT] as well. Commands run as a result of command substitution ignore the keyboard-generated job control signals [SIGTTIN], [SIGTTOU], and [SIGTSTP].
1 JohnMcPherson 365
2 JohnMcPherson 366 The shell exits by default upon receipt of a [SIGHUP]. Before exiting, it resends the [SIGHUP] to all jobs, running or stopped. Stopped jobs are sent [SIGCONT] to ensure that they receive the [SIGHUP]. To prevent the shell from sending the signal to a particular job, it should be removed from the jobs table with the __disown__ builtin (see __SHELL BUILTIN COMMANDS__ below) or marked to not receive [SIGHUP] using __disown -h__ .
1 JohnMcPherson 367
2 JohnMcPherson 368 If the __huponexit__ shell option has been set with __shopt__ , __bash__ sends a [SIGHUP] to all jobs when an interactive login shell exits.
1 JohnMcPherson 369
370 When __bash__ receives a signal for which a trap has been set while waiting for a command to complete, the trap will not be executed until the command completes. When __bash__ is waiting for an asynchronous command via the __wait__ builtin, the reception of a signal for which a trap has been set will cause the __wait__ builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.
371
372 !!JOB CONTROL
373 ''Job control'' refers to the ability to selectively stop (''suspend'') the execution of processes and continue (''resume'') their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and __bash__ .
374
375
376 The shell associates a ''job'' with each pipeline. It keeps a table of currently executing jobs, which may be listed with the __jobs__ command. When __bash__ starts a job asynchronously (in the ''background'' ), it prints a line that looks like:
377
378 [[1] 25647
379
380
381 indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. __Bash__ uses the ''job'' abstraction as the basis for job control.
382
2 JohnMcPherson 383 To facilitate the implementation of the user interface to job control, the operating system maintains the notion of a ''current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as [SIGINT]. These processes are said to be in the ''foreground'' . ''Background'' processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a [SIGTTIN] ([SIGTTOU]) signal by the terminal driver, which, unless caught, suspends the process.
1 JohnMcPherson 384
385 If the operating system on which __bash__ is running supports job control, __bash__ contains facilities to use it. Typing the ''suspend'' character (typically __^Z__ , Control-Z) while a process is running causes that process to be stopped and returns control to __bash__ . Typing the ''delayed suspend'' character (typically __^Y__ , Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to __bash__ . The user may then manipulate the state of this job, using the __bg__ command to continue it in the background, the __fg__ command to continue it in the foreground, or the __kill__ command to kill it. A __^Z__ takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.
386
387 There are a number of ways to refer to a job in the shell. The character __%__ introduces a job name. Job number ''n'' may be referred to as __%n__ . A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, __%ce__ refers to a stopped __ce__ job. If a prefix matches more than one job, __bash__ reports an error. Using __%?ce__ , on the other hand, refers to any job containing the string __ce__ in its command line. If the substring matches more than one job, __bash__ reports an error. The symbols __%%__ and __%+__ refer to the shell's notion of the ''current job'' , which is the last job stopped while it was in the foreground or started in the background. The ''previous job'' may be referenced using __%-__ . In output pertaining to jobs (e.g., the output of the __jobs__ command), the current job is always flagged with a __+__ , and the previous job with a __-__ .
388
389 Simply naming a job can be used to bring it into the foreground: __%1__ is a synonym for __"fg %1"__, bringing job 1 from the background into the foreground. Similarly, __%1 &__ resumes job 1 in the background, equivalent to __"bg %1"__.
390
391 The shell learns immediately whenever a job changes state. Normally, __bash__ waits until it is about to print a prompt before reporting changes in a job's status so as to not interrupt any other output. If the __-b__ option to the __set__ builtin command is enabled, __bash__ reports such changes immediately.
392
393 If an attempt to exit __bash__ is made while jobs are stopped, the shell prints a warning message. The __jobs__ command may then be used to inspect their status. If a second attempt to exit is made without an intervening command, the shell does not print another warning, and the stopped jobs are terminated.
394
395 !!PROMPTING
396 When executing interactively, __bash__ displays the primary prompt __PS1__ when it is ready to read a command, and the secondary prompt __PS2__ when it needs more input to complete a command. __Bash__ allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:
397
398
399 ;__\a__ : an ASCII bell character (07)
400 ;__\d__ : the date in "Weekday Month Date" format (e.g., "Tue May 26")
401 ;__\e__ : an ASCII escape character (033)
402 ;__\h__ : the hostname up to the first `.'
403 ;__\H__ : the hostname
404 ;__\j__ : the number of jobs currently managed by the shell
405 ;__\l__ : the basename of the shell's terminal device name
406 ;__\n__ : newline
407 ;__\r__ : carriage return
408 ;__\s__ : the name of the shell, the basename of __$0__ (the portion following the final slash)
409 ;__\t__ : the current time in 24-hour HH:MM:SS format
410 ;__\T__ : the current time in 12-hour HH:MM:SS format
411 ;__\@__ : the current time in 12-hour am/pm format
412 ;__\u__ : the username of the current user
413 ;__\v__ : the version of __bash__ (e.g., 2.00)
414 ;__\V__ : the release of __bash__, version + patchlevel (e.g., 2.00.0)
415 ;__\w__ : the current working directory
416 ;__\W__ : the basename of the current working directory
417 ;__\!__ : the history number of this command
418 ;__\#__ : the command number of this command
419 ;__\$__ : if the effective UID is 0, a __#__ , otherwise a __$__
420 ;__\''nnn''__ : the character corresponding to the octal number ''nnn''
421 ;__\\e__ : a backslash
422 ;__\[[__ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
423 ;__\]__ : end a sequence of non-printing characters
424
425
426
427
428 The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see __HISTORY__ below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the __promptvars__ shell option (see the description of the __shopt__ command under __SHELL BUILTIN COMMANDS__ below).
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.