blob: 24114af144cc811ef999cd456fd4531a9df60d28 [file] [log] [blame]
Jari Aalto95732b42005-12-07 14:08:12 +000016.11 Bash POSIX Mode
2====================
Jari Aalto726f6381996-08-26 18:22:31 +00003
Chet Rameya0c0a002016-09-15 16:59:08 -04004Starting Bash with the '--posix' command-line option or executing 'set
Jari Aaltoccc6cda1996-12-23 17:02:34 +00005-o posix' while Bash is running will cause Bash to conform more closely
Jari Aalto06285672006-10-10 14:15:34 +00006to the POSIX standard by changing the behavior to match that specified
7by POSIX in areas where the Bash default differs.
Jari Aalto726f6381996-08-26 18:22:31 +00008
Chet Rameya0c0a002016-09-15 16:59:08 -04009When invoked as 'sh', Bash enters POSIX mode after reading the startup
Jari Aalto7117c2d2002-07-17 14:10:11 +000010files.
11
Chet Rameya0c0a002016-09-15 16:59:08 -040012The following list is what's changed when 'POSIX mode' is in effect:
Jari Aalto726f6381996-08-26 18:22:31 +000013
Chet Rameyd233b482019-01-07 09:27:52 -050014 1. Bash ensures that the 'POSIXLY_CORRECT' variable is set.
15
16 2. When a command in the hash table no longer exists, Bash will
Chet Rameya0c0a002016-09-15 16:59:08 -040017 re-search '$PATH' to find the new location. This is also available
18 with 'shopt -s checkhash'.
Jari Aalto726f6381996-08-26 18:22:31 +000019
Chet Ramey8868eda2020-12-06 15:51:17 -050020 3. Bash will not insert a command without the execute bit set into the
21 command hash table, even if it returns it as a (last-ditch) result
22 from a '$PATH' search.
Jari Aalto726f6381996-08-26 18:22:31 +000023
Chet Rameyd233b482019-01-07 09:27:52 -050024 4. The message printed by the job control code and builtins when a job
Chet Ramey8868eda2020-12-06 15:51:17 -050025 exits with a non-zero status is 'Done(status)'.
26
27 5. The message printed by the job control code and builtins when a job
Chet Rameya0c0a002016-09-15 16:59:08 -040028 is stopped is 'Stopped(SIGNAME)', where SIGNAME is, for example,
29 'SIGTSTP'.
Jari Aalto726f6381996-08-26 18:22:31 +000030
Chet Ramey8868eda2020-12-06 15:51:17 -050031 6. Alias expansion is always enabled, even in non-interactive shells.
Jari Aalto726f6381996-08-26 18:22:31 +000032
Chet Ramey8868eda2020-12-06 15:51:17 -050033 7. Reserved words appearing in a context where reserved words are
Jari Aalto95732b42005-12-07 14:08:12 +000034 recognized do not undergo alias expansion.
35
Chet Ramey8868eda2020-12-06 15:51:17 -050036 8. The POSIX 'PS1' and 'PS2' expansions of '!' to the history number
Chet Rameya0c0a002016-09-15 16:59:08 -040037 and '!!' to '!' are enabled, and parameter expansion is performed
38 on the values of 'PS1' and 'PS2' regardless of the setting of the
39 'promptvars' option.
Jari Aalto726f6381996-08-26 18:22:31 +000040
Chet Ramey8868eda2020-12-06 15:51:17 -050041 9. The POSIX startup files are executed ('$ENV') rather than the
Jari Aalto06285672006-10-10 14:15:34 +000042 normal Bash files.
Jari Aalto726f6381996-08-26 18:22:31 +000043
Chet Ramey8868eda2020-12-06 15:51:17 -050044 10. Tilde expansion is only performed on assignments preceding a
Jari Aaltoccc6cda1996-12-23 17:02:34 +000045 command name, rather than on all assignment statements on the line.
Jari Aalto726f6381996-08-26 18:22:31 +000046
Chet Ramey8868eda2020-12-06 15:51:17 -050047 11. The default history file is '~/.sh_history' (this is the default
Chet Rameya0c0a002016-09-15 16:59:08 -040048 value of '$HISTFILE').
Chet Rameyac50fba2014-02-26 09:36:43 -050049
Chet Ramey8868eda2020-12-06 15:51:17 -050050 12. Redirection operators do not perform filename expansion on the
Chet Rameya0c0a002016-09-15 16:59:08 -040051 word in the redirection unless the shell is interactive.
Jari Aalto726f6381996-08-26 18:22:31 +000052
Chet Ramey8868eda2020-12-06 15:51:17 -050053 13. Redirection operators do not perform word splitting on the word in
Jari Aaltobb706242000-03-17 21:46:59 +000054 the redirection.
55
Chet Ramey8868eda2020-12-06 15:51:17 -050056 14. Function names must be valid shell 'name's. That is, they may not
Jari Aaltoccc6cda1996-12-23 17:02:34 +000057 contain characters other than letters, digits, and underscores, and
Jari Aaltocce855b1998-04-17 19:52:44 +000058 may not start with a digit. Declaring a function with an invalid
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059 name causes a fatal syntax error in non-interactive shells.
Jari Aalto726f6381996-08-26 18:22:31 +000060
Chet Ramey8868eda2020-12-06 15:51:17 -050061 15. Function names may not be the same as one of the POSIX special
Chet Rameyac50fba2014-02-26 09:36:43 -050062 builtins.
63
Chet Ramey8868eda2020-12-06 15:51:17 -050064 16. POSIX special builtins are found before shell functions during
Jari Aalto06285672006-10-10 14:15:34 +000065 command lookup.
Jari Aalto726f6381996-08-26 18:22:31 +000066
Chet Ramey8868eda2020-12-06 15:51:17 -050067 17. When printing shell function definitions (e.g., by 'type'), Bash
Chet Rameyd233b482019-01-07 09:27:52 -050068 does not print the 'function' keyword.
69
Chet Ramey8868eda2020-12-06 15:51:17 -050070 18. Literal tildes that appear as the first character in elements of
Chet Rameya0c0a002016-09-15 16:59:08 -040071 the 'PATH' variable are not expanded as described above under *note
72 Tilde Expansion::.
73
Chet Ramey8868eda2020-12-06 15:51:17 -050074 19. The 'time' reserved word may be used by itself as a command. When
Chet Ramey495aee42011-11-22 19:11:26 -050075 used in this way, it displays timing statistics for the shell and
Chet Rameya0c0a002016-09-15 16:59:08 -040076 its completed children. The 'TIMEFORMAT' variable controls the
Chet Ramey495aee42011-11-22 19:11:26 -050077 format of the timing information.
78
Chet Ramey8868eda2020-12-06 15:51:17 -050079 20. When parsing and expanding a ${...} expansion that appears within
Chet Ramey495aee42011-11-22 19:11:26 -050080 double quotes, single quotes are no longer special and cannot be
81 used to quote a closing brace or other special character, unless
82 the operator is one of those defined to perform pattern removal.
83 In this case, they do not have to appear as matched pairs.
84
Chet Ramey8868eda2020-12-06 15:51:17 -050085 21. The parser does not recognize 'time' as a reserved word if the
Chet Rameya0c0a002016-09-15 16:59:08 -040086 next token begins with a '-'.
Chet Ramey495aee42011-11-22 19:11:26 -050087
Chet Ramey8868eda2020-12-06 15:51:17 -050088 22. The '!' character does not introduce history expansion within a
Chet Rameya0c0a002016-09-15 16:59:08 -040089 double-quoted string, even if the 'histexpand' option is enabled.
90
Chet Ramey8868eda2020-12-06 15:51:17 -050091 23. If a POSIX special builtin returns an error status, a
Jari Aaltoccc6cda1996-12-23 17:02:34 +000092 non-interactive shell exits. The fatal errors are those listed in
Jari Aalto06285672006-10-10 14:15:34 +000093 the POSIX standard, and include things like passing incorrect
Jari Aaltoccc6cda1996-12-23 17:02:34 +000094 options, redirection errors, variable assignment errors for
95 assignments preceding the command name, and so on.
Jari Aalto726f6381996-08-26 18:22:31 +000096
Chet Ramey8868eda2020-12-06 15:51:17 -050097 24. A non-interactive shell exits with an error status if a variable
Jari Aaltoccc6cda1996-12-23 17:02:34 +000098 assignment error occurs when no command name follows the assignment
99 statements. A variable assignment error occurs, for example, when
Jari Aaltocce855b1998-04-17 19:52:44 +0000100 trying to assign a value to a readonly variable.
Jari Aalto726f6381996-08-26 18:22:31 +0000101
Chet Ramey8868eda2020-12-06 15:51:17 -0500102 25. A non-interactive shell exits with an error status if a variable
Chet Ramey495aee42011-11-22 19:11:26 -0500103 assignment error occurs in an assignment statement preceding a
104 special builtin, but not with any other simple command.
105
Chet Ramey8868eda2020-12-06 15:51:17 -0500106 26. A non-interactive shell exits with an error status if the
Chet Rameya0c0a002016-09-15 16:59:08 -0400107 iteration variable in a 'for' statement or the selection variable
108 in a 'select' statement is a readonly variable.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109
Chet Ramey8868eda2020-12-06 15:51:17 -0500110 27. Non-interactive shells exit if FILENAME in '.' FILENAME is not
Chet Rameya0c0a002016-09-15 16:59:08 -0400111 found.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000112
Chet Ramey8868eda2020-12-06 15:51:17 -0500113 28. Non-interactive shells exit if a syntax error in an arithmetic
Chet Rameya0c0a002016-09-15 16:59:08 -0400114 expansion results in an invalid expression.
Chet Rameyac50fba2014-02-26 09:36:43 -0500115
Chet Ramey8868eda2020-12-06 15:51:17 -0500116 29. Non-interactive shells exit if a parameter expansion error occurs.
Chet Rameya0c0a002016-09-15 16:59:08 -0400117
Chet Ramey8868eda2020-12-06 15:51:17 -0500118 30. Non-interactive shells exit if there is a syntax error in a script
Chet Rameya0c0a002016-09-15 16:59:08 -0400119 read with the '.' or 'source' builtins, or in a string processed by
120 the 'eval' builtin.
121
Chet Rameyd233b482019-01-07 09:27:52 -0500122 31. While variable indirection is available, it may not be applied to
Chet Rameya0c0a002016-09-15 16:59:08 -0400123 the '#' and '?' special parameters.
124
Chet Rameyd233b482019-01-07 09:27:52 -0500125 32. When expanding the '*' special parameter in a pattern context
126 where the expansion is double-quoted does not treat the '$*' as if
127 it were double-quoted.
128
129 33. Assignment statements preceding POSIX special builtins persist in
Jari Aalto06285672006-10-10 14:15:34 +0000130 the shell environment after the builtin completes.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131
Chet Ramey8868eda2020-12-06 15:51:17 -0500132 34. The 'command' builtin does not prevent builtins that take
Chet Rameya0c0a002016-09-15 16:59:08 -0400133 assignment statements as arguments from expanding them as
134 assignment statements; when not in POSIX mode, assignment builtins
135 lose their assignment statement expansion properties when preceded
136 by 'command'.
137
Chet Ramey8868eda2020-12-06 15:51:17 -0500138 35. The 'bg' builtin uses the required format to describe each job
Chet Rameya0c0a002016-09-15 16:59:08 -0400139 placed in the background, which does not include an indication of
140 whether the job is the current or previous job.
141
Chet Ramey8868eda2020-12-06 15:51:17 -0500142 36. The output of 'kill -l' prints all the signal names on a single
Chet Rameya0c0a002016-09-15 16:59:08 -0400143 line, separated by spaces, without the 'SIG' prefix.
144
Chet Ramey8868eda2020-12-06 15:51:17 -0500145 37. The 'kill' builtin does not accept signal names with a 'SIG'
Chet Rameya0c0a002016-09-15 16:59:08 -0400146 prefix.
147
Chet Ramey8868eda2020-12-06 15:51:17 -0500148 38. The 'export' and 'readonly' builtin commands display their output
Jari Aalto06285672006-10-10 14:15:34 +0000149 in the format required by POSIX.
Jari Aaltod166f041997-06-05 14:59:13 +0000150
Chet Ramey8868eda2020-12-06 15:51:17 -0500151 39. The 'trap' builtin displays signal names without the leading
Chet Rameya0c0a002016-09-15 16:59:08 -0400152 'SIG'.
Jari Aalto28ef6c32001-04-06 19:14:31 +0000153
Chet Ramey8868eda2020-12-06 15:51:17 -0500154 40. The 'trap' builtin doesn't check the first argument for a possible
Jari Aaltob80f6442004-07-27 13:29:18 +0000155 signal specification and revert the signal handling to the original
Jari Aalto95732b42005-12-07 14:08:12 +0000156 disposition if it is, unless that argument consists solely of
157 digits and is a valid signal number. If users want to reset the
Chet Rameya0c0a002016-09-15 16:59:08 -0400158 handler for a given signal to the original disposition, they should
159 use '-' as the first argument.
Jari Aaltob80f6442004-07-27 13:29:18 +0000160
Chet Ramey8868eda2020-12-06 15:51:17 -0500161 41. 'trap -p' displays signals whose dispositions are set to SIG_DFL
162 and those that were ignored when the shell started.
163
Chet Rameyd233b482019-01-07 09:27:52 -0500164 42. The '.' and 'source' builtins do not search the current directory
Chet Rameya0c0a002016-09-15 16:59:08 -0400165 for the filename argument if it is not found by searching 'PATH'.
Jari Aalto28ef6c32001-04-06 19:14:31 +0000166
Chet Rameyd233b482019-01-07 09:27:52 -0500167 43. Enabling POSIX mode has the effect of setting the
Chet Rameya0c0a002016-09-15 16:59:08 -0400168 'inherit_errexit' option, so subshells spawned to execute command
169 substitutions inherit the value of the '-e' option from the parent
170 shell. When the 'inherit_errexit' option is not enabled, Bash
171 clears the '-e' option in such subshells.
Jari Aalto28ef6c32001-04-06 19:14:31 +0000172
Chet Rameyd233b482019-01-07 09:27:52 -0500173 44. Enabling POSIX mode has the effect of setting the 'shift_verbose'
174 option, so numeric arguments to 'shift' that exceed the number of
175 positional parameters will result in an error message.
176
177 45. When the 'alias' builtin displays alias definitions, it does not
Chet Rameya0c0a002016-09-15 16:59:08 -0400178 display them with a leading 'alias ' unless the '-p' option is
Jari Aaltob80f6442004-07-27 13:29:18 +0000179 supplied.
180
Chet Rameyd233b482019-01-07 09:27:52 -0500181 46. When the 'set' builtin is invoked without options, it does not
Jari Aalto28ef6c32001-04-06 19:14:31 +0000182 display shell function names and definitions.
183
Chet Rameyd233b482019-01-07 09:27:52 -0500184 47. When the 'set' builtin is invoked without options, it displays
Jari Aalto7117c2d2002-07-17 14:10:11 +0000185 variable values without quotes, unless they contain shell
186 metacharacters, even if the result contains nonprinting characters.
187
Chet Rameyd233b482019-01-07 09:27:52 -0500188 48. When the 'cd' builtin is invoked in LOGICAL mode, and the pathname
Chet Rameya0c0a002016-09-15 16:59:08 -0400189 constructed from '$PWD' and the directory name supplied as an
190 argument does not refer to an existing directory, 'cd' will fail
Jari Aalto7117c2d2002-07-17 14:10:11 +0000191 instead of falling back to PHYSICAL mode.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000192
Chet Rameyd233b482019-01-07 09:27:52 -0500193 49. When the 'cd' builtin cannot change a directory because the length
194 of the pathname constructed from '$PWD' and the directory name
195 supplied as an argument exceeds PATH_MAX when all symbolic links
196 are expanded, 'cd' will fail instead of attempting to use only the
197 supplied directory name.
198
199 50. The 'pwd' builtin verifies that the value it prints is the same as
Jari Aalto95732b42005-12-07 14:08:12 +0000200 the current directory, even if it is not asked to check the file
Chet Rameya0c0a002016-09-15 16:59:08 -0400201 system with the '-P' option.
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000202
Chet Rameyd233b482019-01-07 09:27:52 -0500203 51. When listing the history, the 'fc' builtin does not include an
Jari Aalto95732b42005-12-07 14:08:12 +0000204 indication of whether or not a history entry has been modified.
Jari Aalto28ef6c32001-04-06 19:14:31 +0000205
Chet Rameyd233b482019-01-07 09:27:52 -0500206 52. The default editor used by 'fc' is 'ed'.
Jari Aalto28ef6c32001-04-06 19:14:31 +0000207
Chet Rameyd233b482019-01-07 09:27:52 -0500208 53. The 'type' and 'command' builtins will not report a non-executable
Chet Rameya0c0a002016-09-15 16:59:08 -0400209 file as having been found, though the shell will attempt to execute
210 such a file if it is the only so-named file found in '$PATH'.
Jari Aalto95732b42005-12-07 14:08:12 +0000211
Chet Rameyd233b482019-01-07 09:27:52 -0500212 54. The 'vi' editing mode will invoke the 'vi' editor directly when
Chet Rameya0c0a002016-09-15 16:59:08 -0400213 the 'v' command is run, instead of checking '$VISUAL' and
214 '$EDITOR'.
Jari Aalto95732b42005-12-07 14:08:12 +0000215
Chet Rameyd233b482019-01-07 09:27:52 -0500216 55. When the 'xpg_echo' option is enabled, Bash does not attempt to
Chet Rameya0c0a002016-09-15 16:59:08 -0400217 interpret any arguments to 'echo' as options. Each argument is
Jari Aalto95732b42005-12-07 14:08:12 +0000218 displayed, after escape characters are converted.
219
Chet Rameyd233b482019-01-07 09:27:52 -0500220 56. The 'ulimit' builtin uses a block size of 512 bytes for the '-c'
Chet Rameya0c0a002016-09-15 16:59:08 -0400221 and '-f' options.
Jari Aalto31859422009-01-12 13:36:28 +0000222
Chet Rameyd233b482019-01-07 09:27:52 -0500223 57. The arrival of 'SIGCHLD' when a trap is set on 'SIGCHLD' does not
Chet Rameya0c0a002016-09-15 16:59:08 -0400224 interrupt the 'wait' builtin and cause it to return immediately.
Chet Ramey00018032011-11-21 20:51:19 -0500225 The trap command is run once for each child that exits.
226
Chet Rameyd233b482019-01-07 09:27:52 -0500227 58. The 'read' builtin may be interrupted by a signal for which a trap
Chet Rameyac50fba2014-02-26 09:36:43 -0500228 has been set. If Bash receives a trapped signal while executing
Chet Rameya0c0a002016-09-15 16:59:08 -0400229 'read', the trap handler executes and 'read' returns an exit status
230 greater than 128.
Chet Rameyac50fba2014-02-26 09:36:43 -0500231
Chet Rameyd233b482019-01-07 09:27:52 -0500232 59. Bash removes an exited background process's status from the list
Chet Rameya0c0a002016-09-15 16:59:08 -0400233 of such statuses after the 'wait' builtin is used to obtain it.
Jari Aalto95732b42005-12-07 14:08:12 +0000234
Jari Aalto06285672006-10-10 14:15:34 +0000235There is other POSIX behavior that Bash does not implement by default
236even when in POSIX mode. Specifically:
Jari Aalto95732b42005-12-07 14:08:12 +0000237
Chet Rameya0c0a002016-09-15 16:59:08 -0400238 1. The 'fc' builtin checks '$EDITOR' as a program to edit history
239 entries if 'FCEDIT' is unset, rather than defaulting directly to
240 'ed'. 'fc' uses 'ed' if 'EDITOR' is unset.
Jari Aalto95732b42005-12-07 14:08:12 +0000241
Chet Rameya0c0a002016-09-15 16:59:08 -0400242 2. As noted above, Bash requires the 'xpg_echo' option to be enabled
243 for the 'echo' builtin to be fully conformant.
Jari Aalto95732b42005-12-07 14:08:12 +0000244
245Bash can be configured to be POSIX-conformant by default, by specifying
Chet Rameya0c0a002016-09-15 16:59:08 -0400246the '--enable-strict-posix-default' to 'configure' when building (*note
Jari Aalto95732b42005-12-07 14:08:12 +0000247Optional Features::).
Jari Aalto28ef6c32001-04-06 19:14:31 +0000248