Bash scripting is a vital tool for developers to automate tasks in Linux.
Bash scripts can be used to automate development tasks locally (like uploading files for deployment, compiling apps, or resizing images in bulk), as well as for server-side tasks (sending scheduled emails, collecting data at intervals, or sending notifications to devices).
You’ll want to run some tasks dependent on the outcome of another task or variable, and that’s where if … else will help you. This decision making process in Bash scripts is called using conditionals.
This tutorial will provide the details and example usage for the if… else statements in Bash.
Using The if Statement
A simple if statement will execute its contained actions if the conditions are true, and looks as follows:
if CONDITIONS; then ACTIONS fi
A full example:
#!/bin/bash if [ 2 -lt 3 ]; then echo "2 is less than 3!" fi
Note that:
- All bash scripts begin with the line #!/bin/bash
- The -lt operator checks if the first value is less than the second. If we wanted to check if the second value was greater than the first, we’d use -gt
- Read on for a list of more logical operators.
- We’re using the echo command to output text to the command line
- if statements always conclude with fi to terminate them
Using The else Statement
If you wish to perform an action based on a set of conditions, and then a different action ONLY if those conditions fails, you can use the else statement:
#!/bin/bash echo -n "Enter number: " read number if [ $number -lt 3 ]; then echo "$number is less than 3!" else echo "$number is not less than 3!" fi
Note that:
- We’re still using the same elements as above for comparing values and outputting text
- We’re using the read command to get user input
- We’re using a variable to store a value for later use
Using the elif (Else If) Statement
To check multiple sets of conditions and perform different actions if each tests as true, use the elif (else if) statement:
#!/bin/bash echo -n "Enter number: " read number if [ $number -lt 3 ]; then echo "$number is less than 3!" elif [ $number -eq 3]; then echo "$number is equal to 3!" else echo "$number is not less than 3!" fi
Multiple Conditions in if Statements
If you have multiple conditions you want to check, you can use the && (AND) and || (OR) operators to decide whether actions should be performed
#!/bin/bash echo -n "Enter number: " read number if [[ $number -lt 3 ]] && [[ $number -lt 0 ]]; then echo "$number is less than three and is negative" elif [[ $number -lt 3 ]] || [[ $number -gt 6 ]]; then echo "$number is not negative, but is less than 3 or greater than 6" else echo "$number is greater than3 and less than 6" fi
Statement Nesting
You can place if statements inside other if statements:
#!/bin/bash echo -n "Enter number: " read number if [$number -gt 3]; then echo "$number is greater than 3" if [$number -gt 6]; then echo "$number is also greater than 6" else echo "$number is not also greater than 6" fi fi
Comparison Operators
In the examples above, we have used the -lt (less than), -gt (greater than), and = (equals) operators. Here are some other commonly used logical operators:
Comparing Numbers
$number1 -eq $number2 : Returns true if the variables are equal $number1 -gt $number2 : Returns true if $number1 is greater than $number2 $number1 -ge $number2 : Returns true if $number1 is equal to or greater than $number2 $number1 -lt $number2 : Returns true if $number1 is less than $number2 $number1 -le $number2 : Returns true if $number1 is equal to or less than $number2
Comparing Strings
$string1 = $string2 : Returns true if $string1 and $string2 are equal $string1 != $string2 : Returns true if $string1 and $string2 are not equal
As shown above, to reverse the outcome of a comparison expression, place the ! (NOT) operator in front of it, for example, *!= means NOT EQUALS.
When comparing strings, always wrap them in double quotes so that any spaces aren’t misinterpreted by your bash script.
You should also wrap variables that contain strings in quotes to make sure they aren’t misinterpreted:
#!/bin/bash echo -n "Enter string: " read string if ["$string" = "hello there!"] echo "You said hello there!" fi
Checking the status of files or directories
-d $filepath : Returns true if the $filepath exists and is a directory -e $filepath : Returns true if the $filepath exists regardless of whether it's a file or directory -f $filepath : Returns true if the $filepath exists and is a file -h $filepath : Returns true if the $filepath exists and is a symbolic link -r $filepath : Returns true if the $filepath exists and is readable -w $filepath : Returns true if the $filepath exists and is writable -x $filepath : Returns true if the $filepath exists and is executable
Conclusion
Creating your own Bash scripts can save you immense amounts of time, and are quite simple to write once you know the basic building blocks.
if… else statements allow you to control how your script responds to user input or the status of files.