Reputation: 2514
Maybe I've just triggered an error in bash's parser, but before filing a bugreport I wan't to ask anyways, maybe I'm just getting blind and it's not a bash bug after all ...
This is the script (ok, stripped down version of the actual one):
$ cat bash-parse-error.1.sh
#! /bin/sh
echo "$(
if false
then
exit 0
fi
# echo "("
case FOO in
FOO)
echo "("
;;
esac
)"
$ ./bash-parse-error.1.sh
./bash-parse-error.1.sh: line 12: syntax error near unexpected token `('
./bash-parse-error.1.sh: line 12: ` echo "("'
Now, If I de-comment the extra echo command, the script works as you would expect, printing two opening parenthesises:
$ cat bash-parse-error.2.sh
#! /bin/sh
echo "$(
if false
then
exit 0
fi
echo "("
case FOO in
FOO)
echo "("
;;
esac
)"
$ ./bash-parse-error.2.sh
(
(
Alternatively, removing the if-false-then-exit block (doing whatever I want with the commented out echo command) will make the error go away as well:
$ cat bash-parse-error.3.sh
#! /bin/sh
echo "$(
case FOO in
FOO)
echo "("
;;
esac
)"
$ ./bash-parse-error.3.sh
(
So, is it me or is it bash?
/edit: a) no workaround needed, already got one, thx anyways
b) #! /usr/bash obviously exhibits same problem, because
c) versions tested: 4.3.33(1) and 4.3.39(1)
Upvotes: 3
Views: 658
Reputation: 80921
What version of bash are you seeing this with? (I see it with 3.2.25(1)-release
but not with 4.1.2(1)-release
or 4.3.42(1)-release
.)
Using the optional (
on the case statement works around the problem as well.
An issue about this was filed for shellcheck as https://github.com/koalaman/shellcheck/issues/482 which references the Command Substitution Bash Hackers Wiki page which discusses it too (as a "Construct to avoid").
Though technically that issue is with a closing )
not an opening one as you've found here.
bash 4.2.46(1)-release from CentOS 7 fails on .1.sh
but works on the other two. And adding the (
fixes .1.sh
there as well.
Upvotes: 3