preloader
image

Remembering the bash if statement

Are you using bash scripting rarely, but you always need to google even the simplest statements?
Do you want to remember the bash if statement syntax?
If yes, then here is a way to remember the correct syntax!

Bash’s if statement can be cumbersome to remember. Is it parentheses or brackets? What was about that semicolon? Why doesn’t it work, and why there is no syntax error? Oh, the spaces. And this is when you start to “google bash if condition”.

First of all, this is one of the ways it could be written:

if [ "$1" != "foo" ]; then
    echo "bar"
fi

Our brain tends to be very good in remembering things if there is association and imagination involved, so i’ve coined up this phrase, imagine it as vividly as possible in your mind:

If you have space in your square bracket put more space around and into your condition, and at the end of your square bracket write a semicolon then you can continue!

To help commit this even deeper into our memories, here is a mental picture for it:

Let me know, which one helped you the most!