]>
Commit | Line | Data |
---|---|---|
f539a756 CR |
1 | 6.11 Bash and POSIX |
2 | =================== | |
3 | ||
4 | 6.11.1 What is POSIX? | |
5 | --------------------- | |
6 | ||
7 | POSIX is the name for a family of standards based on Unix. A number of | |
8 | Unix services, tools, and functions are part of the standard, ranging | |
9 | from the basic system calls and C library functions to common | |
10 | applications and tools to system administration and management. | |
11 | ||
12 | The POSIX Shell and Utilities standard was originally developed by IEEE | |
13 | Working Group 1003.2 (POSIX.2). The first edition of the 1003.2 | |
14 | standard was published in 1992. It was merged with the original IEEE | |
15 | 1003.1 Working Group and is currently maintained by the Austin Group (a | |
16 | joint working group of the IEEE, The Open Group and ISO/IEC SC22/WG15). | |
17 | Today the Shell and Utilities are a volume within the set of documents | |
18 | that make up IEEE Std 1003.1-2017, and thus the former POSIX.2 (from | |
19 | 1992) is now part of the current unified POSIX standard. | |
20 | ||
21 | The Shell and Utilities volume concentrates on the command interpreter | |
22 | interface and utility programs commonly executed from the command line | |
23 | or by other programs. The standard is freely available on the web at | |
24 | <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html>. | |
25 | ||
26 | Bash is concerned with the aspects of the shell's behavior defined by | |
27 | the POSIX Shell and Utilities volume. The shell command language has of | |
28 | course been standardized, including the basic flow control and program | |
29 | execution constructs, I/O redirection and pipelines, argument handling, | |
30 | variable expansion, and quoting. | |
31 | ||
32 | The special builtins, which must be implemented as part of the shell to | |
33 | provide the desired functionality, are specified as being part of the | |
e6795c05 | 34 | shell; examples of these are ‘eval’ and ‘export’. Other utilities |
f539a756 CR |
35 | appear in the sections of POSIX not devoted to the shell which are |
36 | commonly (and in some cases must be) implemented as builtin commands, | |
e6795c05 | 37 | such as ‘read’ and ‘test’. POSIX also specifies aspects of the shell's |
f539a756 CR |
38 | interactive behavior, including job control and command line editing. |
39 | Only vi-style line editing commands have been standardized; emacs | |
40 | editing commands were left out due to objections. | |
41 | ||
42 | 6.11.2 Bash POSIX Mode | |
43 | ---------------------- | |
44 | ||
45 | Although Bash is an implementation of the POSIX shell specification, | |
46 | there are areas where the Bash default behavior differs from the | |
e6795c05 | 47 | specification. The Bash “posix mode” changes the Bash behavior in these |
f539a756 | 48 | areas so that it conforms to the standard more closely. |
726f6388 | 49 | |
e6795c05 CR |
50 | Starting Bash with the ‘--posix’ command-line option or executing ‘set |
51 | -o posix’ while Bash is running will cause Bash to conform more closely | |
d3ad40de CR |
52 | to the POSIX standard by changing the behavior to match that specified |
53 | by POSIX in areas where the Bash default differs. | |
726f6388 | 54 | |
e6795c05 | 55 | When invoked as ‘sh’, Bash enters POSIX mode after reading the startup |
7117c2d2 JA |
56 | files. |
57 | ||
2171061f | 58 | The following list is what's changed when 'POSIX mode' is in effect: |
726f6388 | 59 | |
e6795c05 | 60 | 1. Bash ensures that the ‘POSIXLY_CORRECT’ variable is set. |
b52e30b8 CR |
61 | |
62 | 2. When a command in the hash table no longer exists, Bash will | |
e6795c05 CR |
63 | re-search ‘$PATH’ to find the new location. This is also available |
64 | with ‘shopt -s checkhash’. | |
726f6388 | 65 | |
ce1a3c07 CR |
66 | 3. Bash will not insert a command without the execute bit set into the |
67 | command hash table, even if it returns it as a (last-ditch) result | |
e6795c05 | 68 | from a ‘$PATH’ search. |
726f6388 | 69 | |
b52e30b8 | 70 | 4. The message printed by the job control code and builtins when a job |
ce1a3c07 CR |
71 | exits with a non-zero status is 'Done(status)'. |
72 | ||
73 | 5. The message printed by the job control code and builtins when a job | |
2171061f | 74 | is stopped is 'Stopped(SIGNAME)', where SIGNAME is, for example, |
e6795c05 | 75 | ‘SIGTSTP’. |
726f6388 | 76 | |
ce1a3c07 | 77 | 6. Alias expansion is always enabled, even in non-interactive shells. |
726f6388 | 78 | |
ce1a3c07 | 79 | 7. Reserved words appearing in a context where reserved words are |
7027abcb CR |
80 | recognized do not undergo alias expansion. |
81 | ||
780d665c CR |
82 | 8. Alias expansion is performed when initially parsing a command |
83 | substitution. The default mode generally defers it, when enabled, | |
84 | until the command substitution is executed. This means that | |
85 | command substitution will not expand aliases that are defined after | |
86 | the command substitution is initially parsed (e.g., as part of a | |
87 | function definition). | |
88 | ||
e6795c05 CR |
89 | 9. The POSIX ‘PS1’ and ‘PS2’ expansions of ‘!’ to the history number |
90 | and ‘!!’ to ‘!’ are enabled, and parameter expansion is performed | |
91 | on the values of ‘PS1’ and ‘PS2’ regardless of the setting of the | |
92 | ‘promptvars’ option. | |
726f6388 | 93 | |
e6795c05 | 94 | 10. The POSIX startup files are executed (‘$ENV’) rather than the |
d3ad40de | 95 | normal Bash files. |
726f6388 | 96 | |
780d665c | 97 | 11. Tilde expansion is only performed on assignments preceding a |
ccc6cda3 | 98 | command name, rather than on all assignment statements on the line. |
726f6388 | 99 | |
e6795c05 CR |
100 | 12. The default history file is ‘~/.sh_history’ (this is the default |
101 | value the shell assigns to ‘$HISTFILE’). | |
726f6388 | 102 | |
780d665c | 103 | 13. Redirection operators do not perform filename expansion on the |
2171061f | 104 | word in the redirection unless the shell is interactive. |
726f6388 | 105 | |
780d665c | 106 | 14. Redirection operators do not perform word splitting on the word in |
bb70624e JA |
107 | the redirection. |
108 | ||
e6795c05 | 109 | 15. Function names must be valid shell ‘name’s. That is, they may not |
ccc6cda3 | 110 | contain characters other than letters, digits, and underscores, and |
cce855bc | 111 | may not start with a digit. Declaring a function with an invalid |
ccc6cda3 | 112 | name causes a fatal syntax error in non-interactive shells. |
726f6388 | 113 | |
780d665c | 114 | 16. Function names may not be the same as one of the POSIX special |
ad4aef08 CR |
115 | builtins. |
116 | ||
f539a756 CR |
117 | 17. Even if a shell function whose name contains a slash was defined |
118 | before entering POSIX mode, the shell will not execute a function | |
119 | whose name contains one or more slashes. | |
120 | ||
121 | 18. POSIX special builtins are found before shell functions during | |
e6795c05 CR |
122 | command lookup, including output printed by the ‘type’ and |
123 | ‘command’ builtins. | |
726f6388 | 124 | |
e6795c05 CR |
125 | 19. When printing shell function definitions (e.g., by ‘type’), Bash |
126 | does not print the ‘function’ keyword. | |
1a5fa30b | 127 | |
f539a756 | 128 | 20. Literal tildes that appear as the first character in elements of |
e6795c05 | 129 | the ‘PATH’ variable are not expanded as described above under *note |
2171061f CR |
130 | Tilde Expansion::. |
131 | ||
e6795c05 | 132 | 21. The ‘time’ reserved word may be used by itself as a command. When |
06dff54a | 133 | used in this way, it displays timing statistics for the shell and |
e6795c05 | 134 | its completed children. The ‘TIMEFORMAT’ variable controls the |
06dff54a CR |
135 | format of the timing information. |
136 | ||
f539a756 | 137 | 22. When parsing and expanding a ${...} expansion that appears within |
06dff54a CR |
138 | double quotes, single quotes are no longer special and cannot be |
139 | used to quote a closing brace or other special character, unless | |
140 | the operator is one of those defined to perform pattern removal. | |
141 | In this case, they do not have to appear as matched pairs. | |
142 | ||
e6795c05 CR |
143 | 23. The parser does not recognize ‘time’ as a reserved word if the |
144 | next token begins with a ‘-’. | |
06dff54a | 145 | |
e6795c05 CR |
146 | 24. The ‘!’ character does not introduce history expansion within a |
147 | double-quoted string, even if the ‘histexpand’ option is enabled. | |
037a8b7f | 148 | |
f539a756 | 149 | 25. If a POSIX special builtin returns an error status, a |
ccc6cda3 | 150 | non-interactive shell exits. The fatal errors are those listed in |
d3ad40de | 151 | the POSIX standard, and include things like passing incorrect |
ccc6cda3 JA |
152 | options, redirection errors, variable assignment errors for |
153 | assignments preceding the command name, and so on. | |
726f6388 | 154 | |
e6795c05 CR |
155 | 26. The ‘unset’ builtin with the ‘-v’ option specified returns a fatal |
156 | error if it attempts to unset a ‘readonly’ or ‘non-unsettable’ | |
f539a756 CR |
157 | variable, or encounters a variable name argument that is an invalid |
158 | identifier, which causes a non-interactive shell to exit. | |
159 | ||
d5ab50bc | 160 | 27. When asked to unset a variable that appears in an assignment |
e6795c05 | 161 | statement preceding the command, the ‘unset’ builtin attempts to |
d5ab50bc CR |
162 | unset a variable of the same name in the current or previous scope |
163 | as well. This implements the required "if an assigned variable is | |
164 | further modified by the utility, the modifications made by the | |
165 | utility shall persist" behavior. | |
166 | ||
167 | 28. A non-interactive shell exits with an error status if a variable | |
ccc6cda3 JA |
168 | assignment error occurs when no command name follows the assignment |
169 | statements. A variable assignment error occurs, for example, when | |
cce855bc | 170 | trying to assign a value to a readonly variable. |
ccc6cda3 | 171 | |
d5ab50bc | 172 | 29. A non-interactive shell exits with an error status if a variable |
06dff54a | 173 | assignment error occurs in an assignment statement preceding a |
1fff64ac CR |
174 | special builtin, but not with any other simple command. For any |
175 | other simple command, the shell aborts execution of that command, | |
176 | and execution continues at the top level ("the shell shall not | |
177 | perform any further processing of the command in which the error | |
178 | occurred"). | |
06dff54a | 179 | |
d5ab50bc | 180 | 30. A non-interactive shell exits with an error status if the |
e6795c05 CR |
181 | iteration variable in a ‘for’ statement or the selection variable |
182 | in a ‘select’ statement is a readonly variable or has an invalid | |
2eb9a369 | 183 | name. |
ccc6cda3 | 184 | |
e6795c05 | 185 | 31. Non-interactive shells exit if FILENAME in ‘.’ FILENAME is not |
037a8b7f | 186 | found. |
ccc6cda3 | 187 | |
d5ab50bc | 188 | 32. Non-interactive shells exit if a syntax error in an arithmetic |
037a8b7f CR |
189 | expansion results in an invalid expression. |
190 | ||
d5ab50bc | 191 | 33. Non-interactive shells exit if a parameter expansion error occurs. |
037a8b7f | 192 | |
d5ab50bc | 193 | 34. Non-interactive shells exit if there is a syntax error in a script |
e6795c05 CR |
194 | read with the ‘.’ or ‘source’ builtins, or in a string processed by |
195 | the ‘eval’ builtin. | |
037a8b7f | 196 | |
d5ab50bc | 197 | 35. While variable indirection is available, it may not be applied to |
e6795c05 | 198 | the ‘#’ and ‘?’ special parameters. |
ad4aef08 | 199 | |
e6795c05 CR |
200 | 36. Expanding the ‘*’ special parameter in a pattern context where the |
201 | expansion is double-quoted does not treat the ‘$*’ as if it were | |
e9c75143 | 202 | double-quoted. |
1a5fa30b | 203 | |
d5ab50bc | 204 | 37. Assignment statements preceding POSIX special builtins persist in |
d3ad40de | 205 | the shell environment after the builtin completes. |
ccc6cda3 | 206 | |
e6795c05 | 207 | 38. The ‘command’ builtin does not prevent builtins that take |
037a8b7f CR |
208 | assignment statements as arguments from expanding them as |
209 | assignment statements; when not in POSIX mode, assignment builtins | |
210 | lose their assignment statement expansion properties when preceded | |
e6795c05 | 211 | by ‘command’. |
037a8b7f | 212 | |
e6795c05 | 213 | 39. The ‘bg’ builtin uses the required format to describe each job |
037a8b7f CR |
214 | placed in the background, which does not include an indication of |
215 | whether the job is the current or previous job. | |
216 | ||
e6795c05 CR |
217 | 40. The output of ‘kill -l’ prints all the signal names on a single |
218 | line, separated by spaces, without the ‘SIG’ prefix. | |
037a8b7f | 219 | |
e6795c05 | 220 | 41. The ‘kill’ builtin does not accept signal names with a ‘SIG’ |
037a8b7f CR |
221 | prefix. |
222 | ||
e6795c05 | 223 | 42. The ‘export’ and ‘readonly’ builtin commands display their output |
d3ad40de | 224 | in the format required by POSIX. |
d166f048 | 225 | |
e6795c05 | 226 | 43. If the ‘export’ and ‘readonly’ builtin commands get an argument |
fa0b0029 CR |
227 | that is not a valid identifier, and they are not operating on shell |
228 | functions, they return an error. This will cause a non-interactive | |
229 | shell to exit because these are special builtins. | |
230 | ||
e6795c05 CR |
231 | 44. The ‘trap’ builtin displays signal names without the leading |
232 | ‘SIG’. | |
28ef6c31 | 233 | |
e6795c05 | 234 | 45. The ‘trap’ builtin doesn't check the first argument for a possible |
d3a24ed2 | 235 | signal specification and revert the signal handling to the original |
7027abcb CR |
236 | disposition if it is, unless that argument consists solely of |
237 | digits and is a valid signal number. If users want to reset the | |
2171061f | 238 | handler for a given signal to the original disposition, they should |
e6795c05 | 239 | use ‘-’ as the first argument. |
d3a24ed2 | 240 | |
e6795c05 | 241 | 46. ‘trap -p’ without arguments displays signals whose dispositions |
237c32ba CR |
242 | are set to SIG_DFL and those that were ignored when the shell |
243 | started, not just trapped signals. | |
ce1a3c07 | 244 | |
e6795c05 CR |
245 | 47. The ‘.’ and ‘source’ builtins do not search the current directory |
246 | for the filename argument if it is not found by searching ‘PATH’. | |
28ef6c31 | 247 | |
fa0b0029 | 248 | 48. Enabling POSIX mode has the effect of setting the |
e6795c05 CR |
249 | ‘inherit_errexit’ option, so subshells spawned to execute command |
250 | substitutions inherit the value of the ‘-e’ option from the parent | |
251 | shell. When the ‘inherit_errexit’ option is not enabled, Bash | |
252 | clears the ‘-e’ option in such subshells. | |
d3a24ed2 | 253 | |
e6795c05 CR |
254 | 49. Enabling POSIX mode has the effect of setting the ‘shift_verbose’ |
255 | option, so numeric arguments to ‘shift’ that exceed the number of | |
a6ae8f35 CR |
256 | positional parameters will result in an error message. |
257 | ||
e6795c05 CR |
258 | 50. When the ‘alias’ builtin displays alias definitions, it does not |
259 | display them with a leading ‘alias ’ unless the ‘-p’ option is | |
d3a24ed2 | 260 | supplied. |
28ef6c31 | 261 | |
e6795c05 | 262 | 51. When the ‘set’ builtin is invoked without options, it does not |
28ef6c31 JA |
263 | display shell function names and definitions. |
264 | ||
e6795c05 | 265 | 52. When the ‘set’ builtin is invoked without options, it displays |
7117c2d2 JA |
266 | variable values without quotes, unless they contain shell |
267 | metacharacters, even if the result contains nonprinting characters. | |
268 | ||
e6795c05 CR |
269 | 53. When the ‘cd’ builtin is invoked in logical mode, and the pathname |
270 | constructed from ‘$PWD’ and the directory name supplied as an | |
271 | argument does not refer to an existing directory, ‘cd’ will fail | |
1fff64ac | 272 | instead of falling back to physical mode. |
ccc6cda3 | 273 | |
e6795c05 CR |
274 | 54. When the ‘cd’ builtin cannot change a directory because the length |
275 | of the pathname constructed from ‘$PWD’ and the directory name | |
276 | supplied as an argument exceeds ‘PATH_MAX’ when canonicalized, ‘cd’ | |
277 | will attempt to use the supplied directory name. | |
a6ae8f35 | 278 | |
e6795c05 | 279 | 55. The ‘pwd’ builtin verifies that the value it prints is the same as |
7027abcb | 280 | the current directory, even if it is not asked to check the file |
e6795c05 | 281 | system with the ‘-P’ option. |
7027abcb | 282 | |
e6795c05 | 283 | 56. When listing the history, the ‘fc’ builtin does not include an |
7027abcb CR |
284 | indication of whether or not a history entry has been modified. |
285 | ||
e6795c05 | 286 | 57. The default editor used by ‘fc’ is ‘ed’. |
f539a756 | 287 | |
e6795c05 CR |
288 | 58. ‘fc’ treats extra arguments as an error instead of ignoring them. |
289 | ||
290 | 59. If there are too many arguments supplied to ‘fc -s’, ‘fc’ prints | |
f539a756 | 291 | an error message and returns failure. |
7027abcb | 292 | |
e6795c05 | 293 | 60. The ‘type’ and ‘command’ builtins will not report a non-executable |
2171061f | 294 | file as having been found, though the shell will attempt to execute |
e6795c05 | 295 | such a file if it is the only so-named file found in ‘$PATH’. |
7027abcb | 296 | |
e6795c05 CR |
297 | 61. The ‘vi’ editing mode will invoke the ‘vi’ editor directly when |
298 | the ‘v’ command is run, instead of checking ‘$VISUAL’ and | |
299 | ‘$EDITOR’. | |
7027abcb | 300 | |
e6795c05 CR |
301 | 62. When the ‘xpg_echo’ option is enabled, Bash does not attempt to |
302 | interpret any arguments to ‘echo’ as options. Each argument is | |
7027abcb CR |
303 | displayed, after escape characters are converted. |
304 | ||
e6795c05 CR |
305 | 63. The ‘ulimit’ builtin uses a block size of 512 bytes for the ‘-c’ |
306 | and ‘-f’ options. | |
8e1a6eaa | 307 | |
e6795c05 CR |
308 | 64. The arrival of ‘SIGCHLD’ when a trap is set on ‘SIGCHLD’ does not |
309 | interrupt the ‘wait’ builtin and cause it to return immediately. | |
d5362af8 CR |
310 | The trap command is run once for each child that exits. |
311 | ||
e6795c05 | 312 | 65. The ‘read’ builtin may be interrupted by a signal for which a trap |
ad4aef08 | 313 | has been set. If Bash receives a trapped signal while executing |
e6795c05 | 314 | ‘read’, the trap handler executes and ‘read’ returns an exit status |
2171061f | 315 | greater than 128. |
7027abcb | 316 | |
e6795c05 | 317 | 66. The ‘printf’ builtin uses ‘double’ (via ‘strtod’) to convert |
3be2a2ca | 318 | arguments corresponding to floating point conversion specifiers, |
e6795c05 CR |
319 | instead of ‘long double’ if it's available. The ‘L’ length |
320 | modifier forces ‘printf’ to use ‘long double’ if it's available. | |
3be2a2ca | 321 | |
e6795c05 CR |
322 | 67. Bash removes an exited background process's status from the list |
323 | of such statuses after the ‘wait’ builtin is used to obtain it. | |
0fcb3344 | 324 | |
e6795c05 | 325 | 68. A double quote character (‘"’) is treated specially when it |
f539a756 CR |
326 | appears in a backquoted command substitution in the body of a |
327 | here-document that undergoes expansion. That means, for example, | |
328 | that a backslash preceding a double quote character will escape it | |
329 | and the backslash will be removed. | |
330 | ||
e6795c05 CR |
331 | 69. The ‘test’ builtin compares strings using the current locale when |
332 | processing the ‘<’ and ‘>’ binary operators. | |
293916f8 | 333 | |
e6795c05 CR |
334 | 70. The ‘test’ builtin's ‘-t’ unary primary requires an argument. |
335 | Historical versions of ‘test’ made the argument optional in certain | |
2eb9a369 CR |
336 | cases, and Bash attempts to accommodate those for backwards |
337 | compatibility. | |
338 | ||
e6795c05 | 339 | 71. Command substitutions don't set the ‘?’ special parameter. The |
d052bd6f CR |
340 | exit status of a simple command without a command word is still the |
341 | exit status of the last command substitution that occurred while | |
342 | evaluating the variable assignments and redirections in that | |
343 | command, but that does not happen until after all of the | |
344 | assignments and redirections. | |
345 | ||
d3ad40de CR |
346 | There is other POSIX behavior that Bash does not implement by default |
347 | even when in POSIX mode. Specifically: | |
ccc6cda3 | 348 | |
e6795c05 CR |
349 | 1. The ‘fc’ builtin checks ‘$EDITOR’ as a program to edit history |
350 | entries if ‘FCEDIT’ is unset, rather than defaulting directly to | |
351 | ‘ed’. ‘fc’ uses ‘ed’ if ‘EDITOR’ is unset. | |
28ef6c31 | 352 | |
f539a756 | 353 | 2. A non-interactive shell does not exit if a variable assignment |
e6795c05 | 354 | preceding the ‘command’ builtin or another non-special builtin |
f539a756 CR |
355 | fails. |
356 | ||
e6795c05 CR |
357 | 3. As noted above, Bash requires the ‘xpg_echo’ option to be enabled |
358 | for the ‘echo’ builtin to be fully conformant. | |
28ef6c31 | 359 | |
7027abcb | 360 | Bash can be configured to be POSIX-conformant by default, by specifying |
e6795c05 | 361 | the ‘--enable-strict-posix-default’ to ‘configure’ when building (*note |
7027abcb | 362 | Optional Features::). |
28ef6c31 | 363 |