Skip to content
Advertisement

How to simplify the comparison in the bash?

how to make several comparisons in the bash by placing the condition and comparison points next to instead of the long queue ?

that something like this

before :

if [[ $var == "aaa" || $var == "bbb" || $var == "ccc" || $var == "ddd" ]];
then
 echo "good";
fi

after (what I want):

if [[ $var==["aaa","bbb","ccc","ddd"] ]];
then
 echo "good";
fi

Advertisement

Answer

Try this using bash regex with the keywork =~:

if [[ $var =~ ^(aaa|bbb|ccc|ddd)$ ]];
then
 echo "good";
fi

Edit :

As seen in comments, for real you need to compare int, not strings, so :

((var%3 == 0)) && echo "ok"

Using bash arithmetic

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement