Is there any sh
code that is not syntactically valid bash code (won't barf on syntax)?
I am thinking of overwriting sh
with bash
for certain commands.
bashposixshell-script
Is there any sh
code that is not syntactically valid bash code (won't barf on syntax)?
I am thinking of overwriting sh
with bash
for certain commands.
Best Answer
Here is some code that does something different in POSIX sh and Bash:
Whether that is "invalid" for you I don't know.
In Bash, it redirects both standard output and standard error from
hello
into the fileworld
. In POSIXsh
, it runshello
in the background and then makes an empty redirection intoworld
, truncating it (i.e. it's treated as& >
).There are plenty of other cases where Bash extensions will do their thing when run under
bash
, and would have different effects in a pure POSIXsh
. For example, brace expansion is another, and it too operates the same under Bash's POSIX mode and not.As far as static syntax errors go, Bash has both reserved words (like
[[
andtime
) not specified by POSIX, such that[[ x
is valid POSIX shell code but a Bash syntax error, and a history of various POSIX incompatibility bugs that may result in syntax errors, such as the one from this question:Syntax-errors-only is a pretty dangerous definition of "invalid" for any circumstance where it matters, but there it is.