How to Use if-else in Shell Scripts?

Published on August 3, 2022
How to Use if-else in Shell Scripts?

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Moving ahead from our previous tutorial on arrays in shell scripts, let’s understand how we can use if-else in shell scripts.

Conditional programming is an important part of any programming language because executing every single statement in our program is, more often than not, undesirable.

And we need a way to conditionally execute statements. The if-else in shell scripts serves this exact situation.

Conditions in Shell Scripts

One of the most important parts of conditional programming is the if-else statements. An if-else statement allows you to execute iterative conditional statements in your code.

We use if-else in shell scripts when we wish to evaluate a condition, then decide to execute one set between two or more sets of statements using the result.

This essentially allows us to choose a response to the result which our conditional expression evaluates to.

How does if-else in shell scripts work?

Now we know what is an if-else function and why is it important for any programmer, regardless of their domain. To understand if-else in shell scripts, we need to break down the working of the conditional function.

Let us have a look at the syntax of the if-else condition block.

if [condition]

Here we have four keywords, namely if, then, else and fi.

  1. The keyword if is followed by a condition.
  2. This condition is evaluated to decide which statement will be executed by the processor.
  3. If the condition evaluates to TRUE, the processor will execute the statement(s) followed by the keyword then. In the syntax, it is mentioned as statement1.
  4. In a case where the condition evaluates to FALSE, the processor will execute the statement(s) followed by the keyword else. This is denoted as statement2 in the function syntax.

An important thing to keep in mind is that, like C programming, shell scripting is case sensitive. Hence, you need to be careful while using the keywords in your code.

How to use if-else in shell script

It is easy to see the syntax of a function and believe you know how to use it. But it is always a better choice to understand a function through examples because they help you understand the role that different aspects of a function play.

Here are some useful examples of if-else in shell scripts to give you a better idea of how to use this tool.

Command Description
&& Logical AND
$0 Argument 0 i.e. the command that’s used to run the script
$1 First argument (change number to access further arguments)
-eq Equality check
-ne Inequality check
-lt Less Than
-le Less Than or Equal
-gt Greater Than
-ge Greater Than or Equal

1. Using if-else to check whether two numbers are equal

When trying to understand the working of a function like if-else in a shell script, it is good to start things simple. Here, we initialize two variables a and b, then use the if-else function to check if the two variables are equal. The bash script should look as follows for this task.


if [ $n -eq $m ]
        echo "Both variables are the same"
        echo "Both variables are different"


Both variables are different

2. Using if-else to compare two values

The more common use of if-else in shell scripts is for comparing two values. Comparing a variable against another variable or a fixed value helps is used in a variety of cases by all sorts of programmers.

For the sake of this example, we will be initializing two variables and using the if-else function to find the variable which is greater than the other.

if [ $a -ge $b ]
  echo "The variable 'a' is greater than the variable 'b'."
  echo "The variable 'b' is greater than the variable 'a'."


The variable 'b' is greater than the variable 'a'.

3. Using if-else to check whether a number is even

Sometimes we come across situations where we need to deal with and differentiate between even and odd numbers. This can be done with if-else in shell scripts if we take the help of the modulus operator.

The modulus operator divides a number with a divisor and returns the remainder.

As we know all even numbers are a multiple of 2, we can use the following shell script to check for us whether a number is even or odd.

if [ $((n%2))==0 ]
  echo "The number is even."
  echo "The number is odd."


The number is even

As you can see, we’ve enclosed a part of the condition within double brackets. That’s because we need the modulus operation to be performed before the condition is checked.

Also, enclosing in double brackets runs statements in C-style allowing you to process some C-style commands within bash scripts.

4. Using if-else as a simple password prompt

The if-else function is known for its versatility and range of application. In this example, we will use if-else in shell script to make the interface for a password prompt.

To do this, we will ask the user to enter the password and store it in the variable pass.

If it matches the pre-defined password, which is ‘password’ in this example, the user will get the output as -“The password is correct”.

Else, the shell script will tell the user that the password was incorrect and ask them to try again.

echo "Enter password"
read pass
if [ $pass="password" ]
  echo "The password is correct."
  echo "The password is incorrect, try again."

Bash Password
Bash Password


The function of if-else in shell script is an important asset for shell programmers. It is the best tool to use when you need to execute a set of statements based on pre-defined conditions.

The if-else block is one, if not the most essential part of conditional programming. By regulating the execution of specific statements you not only make your code more efficient but you also free up the precious time which the processor might have wasted executing statements which are unnecessary for a specific case.

We hope this tutorial was able to help you understand how to use the if-else function. If you have any queries, feedback or suggestions, feel free to reach out to us in the comments below.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel