Home   Profile   Fun
#142 Linux  07.01.2008

The if statement in bash scripts and as a oneliner


I get lots of requests concerning the if statement syntax. So here are two examples as a reminder. There is nothing special about it of course, just remember the "fi" at the end.

if, then, elif, else within a shell script.
#!/bin/bash

a=20

if [ $a -eq 10 ]
then
  echo "a is equal to 10."

elif [ $a -eq 20 ]
 then
  echo "a is equal to 20."

else
  echo "a is neither 10 nor 20."
fi

if, then, else as a oneliner.
a=10; b=20; if [ $a -eq $b ]; then echo "a is equal to b"; else echo "a is not equal to b"; fi;