Craziness with if statement in bash shell

0.00 avg. rating (0% score) - 0 votes

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"
    echo "Oh NO"

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!

0.00 avg. rating (0% score) - 0 votes


A tough developer who likes to work on just about anything, from software development to electronics, and share his knowledge with the rest of the world.

One thought on “Craziness with if statement in bash shell

  • February 11, 2019 at 8:59 am

    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..

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>