I want to make some actions for matched lines in a case switch. And because strings are long, I wanted to use bash curly braces. But it does not work.
This code without curly braces works as expected:
for i in longstr_one longstr_two; do
case $i in
longstr_one| longstr_five)
echo matched $i
;;
*)
echo no matches of $i
;;
esac
done
And I got expected result:
matched longstr_one
no matches of longstr_two
But the following code with curly braces does not:
for i in longstr_one longstr_two; do
case $i in
longstr_{one|,five})
echo matched $i
;;
*)
echo no matches of $i
;;
esac
done
And I got incorrect result:
no matches of longstr_one
no matches of longstr_two
Why it is not working? Is it possible to use curly braces in case selector in bash?
Since brace expansion isn't done in case
patterns, you could use bash's extended glob syntax instead:
shopt -s extglob
for i in longstr_one longstr_two; do
case $i in
longstr_@(one|five) )
echo "matched $i"
;;
*)
echo "no matches of $i"
;;
esac
done
The syntax @(this|that|theother|...)
matches any one of the subpatterns.