Skip to content

How to use if in shell

Syntax

shell
if [ expression ]
then
  commands
elif [ expression ]
then
  commands
else
  commands
fi

Number check

OperatorMeaning
-eqequal
-nenot equal
-gtgreater than
-gegreater than or equal
-ltless than
-leless than or equal

String check

OperatorMeaning
-zempty
-nnot empty
=equal
!=not equal

File check

OperatorMeaning
-eexist
-ffile
-ddirectory
-rreadable
-wwritable
-xexecutable
-ssize > 0
-cchar file
-bblock file