Craziness with if statement in bash shell
You surely must have become mad with if statement in bash shell at some point. Like me now, with the trial-and-error method to figure out the syntax, I really hate the restriction of the syntax of the bash if statement.
My purpose is very simple: check if the string variable contains some substrings. for example, checking_fruits=”banana potato jerry grape lemon” and substrings are “banana”, “potato”, “grape”
Finally, I’ve found out the exact syntax as following:
if [[ $checking_fruits =~ "banana" ]] && [[ $checking_fruits =~ "potato" ]] && [[ $checking_fruits =~ "grape" ]] ; then echo "YEAH" else echo "Oh NO" fi
More important, I’ve also found out a very simple principle: make sure a single space (yes, only space. No tab please) exists among the elements of the if condition for everything to work happily together.
This is just insanely complicated!
I met same conditions outside of the shell – i needed more space – jk
Indeed, your example gives some insights on the frusty if in bash
Thank you..