This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

In a programming language, conditionals let you decide whether to perform an action or not, this decision is taken by evaluating an expression.

Conditionals have many forms. The most basic form is: if <expression> then <statement> where ‘statement’ is only executed if ‘expression’ evaluates to true.

Conditionals have other forms such as: if <expression> then <statement1> else <statement2>. Here ‘statement1’ is executed if ‘expression’ is true,otherwise ‘statement2’ is executed.

General Syntax

The general syntax of if-else statement is given below

if [condition]
then
    <commands>
else
   <commands>
fi

File Based Conditions

Let us look at some example primary commands which are used mainly when working with file-based conditions:

ConditionMeaning
[ -a <FILE> ]Returns true when FILE exists.
[ -b <FILE> ]Returns true when FILE exists & is a block special file.
[ -c <FILE> ]Returns true when FILE exists & is a character special file.
[ -d <FILE> ]Returns true when FILE exists & is a directory.
[ -e <FILE> ]Returns true when FILE exists.
[ -f <FILE> ]Returns true when FILE exists & is a regular file.
[ -g <FILE> ]Returns true when FILE exists & its SGID bit is set.
[ -h <FILE> ]Returns true when FILE exists & is a symbolic link.
[ -k <FILE> ]Returns true when FILE exists & its sticky bit is set.
[ -p <FILE> ]Returns true when FILE exists & is a named pipe (FIFO).
[ -r <FILE> ]Returns true when FILE exists & is readable.
[ -s <FILE> ]Returns true when FILE exists and has a size greater than zero.
[ -t <FD> ]Returns true when file descriptor FD is open & refers to a terminal.
[ -u <FILE> ]Returns true when FILE exists & its SUID (set user ID) bit is set.
[ -w <FILE> ]Returns true when FILE exists & is writable.
[ -x <FILE> ]Returns true when FILE exists & is executable.
[ -O <FILE> ]Returns true when FILE exists & is owned by the effective user ID.
[ -G <FILE> ]Returns true when FILE exists & is owned by the effective group ID.
[ -L <FILE> ]Returns true when FILE exists & is a symbolic link.
[ -N <FILE> ]Returns true when FILE exists & has been modified since it was last read.
[ -S <FILE> ]Returns true when FILE exists & is a socket.

Simple if..then Example

Following is an example of using if..then statement

/tmp/ifelse.sh
#!/bin/bash
 
read -p "Enter a number: " var
 
if [ $var -gt 10 ]
then
    echo "Value is greater than 10"
fi
done

Simple if..then..else Example

Following is the simple if..then..else statement example


/tmp/ifelse.sh
#!/bin/bash
 
value=$( grep -ic "Darkcoder" /etc/database )
if [ $value -eq 1 ]
then
  echo "I found Darkcoder"
else
  echo "I didn't find Darkcoder"
fi
done

Simple if..elif..else Example

You can alse use elif statement with if statement. See the example below

/tmp/ifelse.sh
#!/bin/bash
 
echo -n "Enter a number: "
read value

if [[ $value -gt 10 ]]
then
  echo "Variable is greater than 10."
elif [[ $value -eq 10 ]]
then
  echo "Variable is equal to 10."
else
  echo "Variable is less than 10."
fi
done

elif condition will only be checked when the if statement is false. Otherwise it will be skipped.

Using Command-Line Arguments With if Statement

We can also use command-line arguments in our scripts and use the number of arguments and the values itself as a condition in the IF statement we define. We first define a text file with following content:

/tmp/test.txt
A quick brown fox jumps over a lazy dog.

Now, we can write a script which finds if a word occurs in a text file or not. Let’s define the script now:

/tmp/ifelse.sh
#!/bin/bash
echo "Finding $1 in $2"
grep $1 $2
if [ $? -ne 0 ]
then
  echo "$1 not found in file $2."
else
  echo "$1 found in file $2."
fi
echo "Script completed."
done

Now we can run the script in command-line like this:

root@codesposts:~$ bash ifelse.sh dog test.txt

Finding dog in test.txt
dog found in file test.txt.
Script completed.

Nested if Statements

if statement inside another if statement is called nested if statement. The following script will prompt you to enter three numbers and will print the largest number among the three numbers.

/tmp/ifelse.sh
#!/bin/bash

echo -n "Enter the first number: "
read value1
echo -n "Enter the second number: "
read value2
echo -n "Enter the third number: "
read value3

if [[ $value1 -ge $value2 ]]
then
  if [[ $value1 -ge $value3 ]]
  then
    echo "$value1 is the largest number"
  else
    echo "$value3 is the largest number"
  fi
else
  if [[ $value2 -ge $value3 ]]
  then
    echo "$value2 is the largest number"
  else
    echo "$value3 is the largest number"
  fi
fi
done
root@codesposts:~$ bash ifelse.sh

Enter the first number: 3
Enter the second number: 8
Enter the third number: 7
8 is the largest number

Multiple Conditions In if Statement

We can use multiple conditions with if statement using AND and OR operators.

/tmp/ifelse.sh
#!/bin/bash

echo -n "Enter the first number: "
read value1
echo -n "Enter the second number: "
read value2
echo -n "Enter the third number: "
read value3

if [[ $value1 -ge $value2 ]] && [[ $value1 -ge $value3 ]]
then
  echo "$value1 is the largest number"
elif [[ $value2 -ge $value1 ]] && [[ $value2 -ge $value3 ]]
then
  echo "$value2 is the largest number"
else
  echo "$value3 is the largest number"
fi
done
root@codesposts:~$ bash ifelse.sh

Enter the first number: 3
Enter the second number: 8
Enter the third number: 7
8 is the largest number