Link Search Menu Expand Document

If condition

  • Basic syntax
if [ xxx ]
    echo " xxx "
fi 
    # space in the brackets and semi colon is important
  • Example
#! /bin/bash 

echo "hello ..."

if [ 3 -eq 3 ]; then 
    echo "yes, boolean is ture!"
fi 
# make sure space on both side of the content [ xx ]

if [ 3 -eq 5 ]; then 
    echo "this boolean is not ture, it won't output in the terminal"
fi

echo "bye for now"

# interpretation
# 3 -eq 3 => 3 = 3
# 3 -ne 4 => 3 is not 4 
# 3 -gt 1 => 3 greater than 1 
# 3 -lt 7 => 3 less than 7 
# 3 -ge 3 => 3 greater equal 3 
# 3 -le 3 => 3 less equal 3