3

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)

17
  • Note you are using #! /bin/sh (a space before /bin). Commented Nov 6, 2015 at 11:05
  • 1
    @fedorqui: The space is allowed there. sh != bash with regard to the tag. Commented Nov 6, 2015 at 11:11
  • @choroba really? I would've thought #! /bin/sh wouldn't be resolved properly. Commented Nov 6, 2015 at 11:15
  • @fedorqui: en.wikipedia.org/wiki/Shebang_(Unix) Commented Nov 6, 2015 at 11:17
  • 2
    Yes. Keeping questions and answers separate allows them to be voted on, selected, and moderated individually; it's very much encouraged for the person who asked a question to add an answer themselves (with the "add answer" button) when they come on a solution that no 3rd-party answer includes (and if you get rep from separate upvoting of your question and your answer, then it's taken as given that you earned both parts). Commented Nov 6, 2015 at 15:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.