Reputation: 11861
I have found a case statement in a Bash script which looks like this:
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
I've never seen the use of a starting (
, I've only ever seen SOME_CASE) command list ;;
. I can't find any reference to this other format - does this starting parenthesis have any special significance?
Upvotes: 6
Views: 1012
Reputation: 182
According to:
man 1 bash
the format of the case
operator is:
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac
The opening bracket is in square brackets, which means it's not required.
Upvotes: 3
Reputation: 754520
The starting parenthesis is optional, but it (usually) leaves balanced parentheses which makes it easier in editors like vim
which can jump from a close bracket (such as )
, ]
, }
) to the matching open bracket, accounting for nested brackets. It has no other significance.
The original Bourne shell did not support the leading open parenthesis.
See also the POSIX shell specification for case
under Compound commands or the Bash manual under Conditional Constructs.
It also looks as if your script is crying out to use a Bash array (see also Shell Parameters).
declare -a args
args+=("argument 0")
args+=("argument 1")
args+=("argument 2" "argument 3" "argument 4 with spaces")
set -- "${args[@]}"
No case
; no limit on the number of arguments. How you build the array is open to discussion, but the one line instead of a 10-way case
is compelling.
Upvotes: 8