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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHere is some code that does something different in POSIX sh and Bash:
hello &> world
Whether that is "invalid" for you I don't know.
In Bash, it redirects both standard output and standard error from hello
into the file world
. In POSIX sh
, it runs hello
in the background and then makes an empty redirection into world
, 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 POSIX sh
. 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 [[
and time
) 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:
x=$(cat <<'EOF'
`
EOF
)
bash: line 2: unexpected EOF while looking for matching ``'
bash: line 5: syntax error: unexpected end of file
Syntax-errors-only is a pretty dangerous definition of "invalid" for any circumstance where it matters, but there it is.
{fd}>file
a similar issue), so that bash can be conformant again (brace expansion would stay unspecified, but compliant scripts would need to do echo "{a,b}"
is they want {a,b}
to be output). See discussion that started it
May 11, 2018 at 8:39
/bin/sh
is bash, so if I weren't actively aware of this bashism, writing a command line this could easily happen. The spacing in the answer makes it look unlikely, but hello&>world
is less far-fetched.
May 11, 2018 at 15:21
&>
difference just last month!
May 13, 2018 at 8:12
A short example:
time()(:)
time
in Bash is a reserved word, and behaves differently from the time
program. It's quite likely you'll break some practical scripts trying to parse the result of time
by using bash. But technically it is not a syntax error. Redefining time
as a function would be rare but causes a syntax error as this question specifies.
A shorter example:
a():
Valid in dash
, but not POSIX compliant.
time
, there's stuff like declare
, function
, select
, and coproc
. Though some of those are explicitly marked as unspecified in the standard (keywords and builtins/utilities), but I can't see e.g. time
or coproc
in the list. And using --posix
doesn't seem to help.
((
comes to mind./usr/bin/sh
is just a sym-link to/usr/bin/bash
(I'm using CentOS 7.3 and it is). You should check to see ifsh
is reallybash
for your distro.#!/bin/sh
to#!/bin/bash
. Then everything worked again, so you do really have to be careful. It may have happened when they started using dash instead of bash for sh.