Reputation: 97
While using case statement whose syntax is
case $variable-name in
pattern1) command;;
patternN) command;;
*) command;;
esac
Here " *)" is meant for default case whose patterns doesn’t match for any case. What if I want to match for the pattern " * " and do multiplication and should have default case also?
Upvotes: 1
Views: 121
Reputation: 2382
You can either escape the *
character using \*
or if you are doing certain operations such as addition, multiplication, etc. based on the choice of pattern/character it is better to use numbers to indicate the operation. For example:
So by doing this your case statement can be something like this:
case $variable-name in
1)sum=$((n1 + $n2))
echo "Sum = $sum";;
2)difference=$((n1 - n2))
echo "Sub = $difference";;
3)product=$(( n1 * n2 ))
echo "Mul = $product";;
4)quotient=$(( n1 / n2 ))
echo "Div = $quotient";;
*)echo "Invalid choice";;
esac
Upvotes: 1
Reputation: 131
Just escape it like so:
#!/bin/bash
variable-name="*"
case $variable-name in
\*) echo "Star";;
*) echo "Default";;
esac
Or you could put the * in quotes.
Upvotes: 5