1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – If-else

Bash if else Statment

If-else and elif are the conditional statements in bash scripting. If evaluates a condition, If a condition is true then if block code is executed. On false condition, you can execute else block code but its optional.

Syntax:

1. Bash – If Statement Example

In this example, take a user input of any number and check if the given value is greater than 10.

2. Bash – If-else Statement Example

Using the same script as above. Only if user entered value is greater than 10 then print “OK”. If the value equals to 10 or less then print “Not OK”

3. Bash – If-elif-else Statement Example

elif (else if) is used for multiple if conditions. In case one if the condition goes false then check another if conditions. For example, input the marks of student and check if marks are greater or equal to 80 then print “Very Good”. If marks are less than 80 and greater or equal to 50 then print 50 and so on. Check the below script and execute it on the shell with different-2 inputs.

4. Bash – Nested if Statement Example

With nested if one condition goes true then only check another condition. For example, take 3 numeric values as input and check the greatest value.

Tags , ,
Was this article helpful to you? Yes 14 No 2
Advertisement