So I'm trying to use ;;&
in a bash case
statement and I'm getting syntax errors.
(macOS 10.14.5, Darwin 18.6.0)
The back story is that I have some scripts that set up unit tests. Different tests need slightly different data sets to work on, so I wrote (essentially) the following (with the details removed, as it doesn't seem to matter):
case $TEST in
( 1 )
# no setup needed
;;
( 2 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 )
# these tests need a simple set of captured data
echo "Capture 1 2 3"
;;&
( 12 )
# in addition, test 12 needs some unique data
echo "Capture 4 non duplicate"
;;
( 3 | 5 )
# these tests need data captured in seperate operations
echo "Capture 1"
echo "Capture 2"
echo "Capture 3"
;;
esac
When I run this, I get the following error:
Tests/prepare.sh: line 26: syntax error near unexpected token `&'
Tests/prepare.sh: line 26: ` ;;&'
According to man bash
:
If the ;; operator is used, no subsequent matches are attempted after the first pattern match. Using ;& in place of ;; causes execution to continue with the list associated with the next set of patterns. Using ;;& in place of ;; causes the shell to test the next pattern list in the statement, if any, and execute any associated list on a successful match.
My interpretation is thus: using ;;
is essentially a break
that skips over all subsequent patterns in the statement. A ;&
is essentially a no-op and just falls through to the next list (ignoring the pattern), and the ;;&
continues testing patterns starting with the next one.
I've tried using all ;&
and ;;&
but get a similar result. Given that bash
dates to the 1980's, I'm shocked this does't work. So I assume I'm just missing something, but I can't figure out what.
I can work around this for now (by duplicating the code in some steps so all patterns are unique), but this still haunts me and I would love to know the answer.