Bash conditionals are an essential part of scripting in the Unix shell. They allow you to control the flow of your script based on conditions or variables that you specify. With bash conditionals, you can create complex scripts that can make decisions and execute commands based on a variety of criteria. In this blog, we’ll cover the basics of bash conditionals and take a deep dive into some of the more advanced features.
Prerequisites
Before diving into bash conditionals, it’s important to have a good understanding of the Unix shell. If you’re new to the Unix shell, I recommend checking out the following blog about Bash scripting.
Bash Conditional Syntax
The basic syntax for a bash conditional is as follows:
if [ condition ]; then
commands
fi
The condition is specified within square brackets ([ ]
) and can be any test that returns a success or failure status. For example, the following conditional tests for the existence of a file:
if [ -e file.txt ]; then
echo "file.txt exists"
fi
You can also specify multiple conditions using the &&
(and) and ||
(or) operators:
if [ condition1 ] && [ condition2 ]; then
commands
fi
if [ condition1 ] || [ condition2 ]; then
commands
fi
Advanced Bash Conditionals
Bash conditionals offer a wide range of options for testing conditions. Some of the more advanced features include:
- String tests (e.g.
=
,!=
,z
,n
) - File tests (e.g.
e
,f
,d
) - Arithmetic tests (e.g.
eq
,lt
,gt
)
For example, the following conditional tests for the equality of two strings:
if [ "$str1" = "$str2" ]; then
echo "str1 and str2 are equal"
fi
You can also use conditionals with the case
statement to match patterns in strings:
case "$string" in
pattern1)
commands
;;
pattern2)
commands
;;
*)
commands
;;
esac
Use Cases
Bash conditionals can be used in a wide variety of use cases, including:
- Automating tasks that require decision making (e.g. copying files only if they don’t already exist)
- Controlling the flow of a script based on user input
- Testing the environment before executing a command (e.g. checking for the existence of a command line tool before using it)
Here’s an example of a script that checks for the existence of a command line tool and executes it if it’s found:
#!/bin/bash
if command -v mytool >/dev/null 2>&1; then
mytool
else
echo "mytool not found"
fi
5 Tips for Using Bash Conditionals
- Always use quotes around variables to avoid issues with whitespace or other special characters in the values.
- Use the
&&
and||
operators to chain multiple conditions together and create complex logical expressions. - When testing the exit status of a command, use the
$?
special variable to get the status of the last executed command. - Make use of the
case
statement when you need to match patterns in strings, rather than testing a condition. - Always include an
else
clause with yourif
statements, even if it’s just an empty clause, to make the logic of your script clear.
Summary and Recommendations
In this blog, we’ve covered the basics of bash conditionals, including syntax, advanced features, and practical use cases. With the knowledge gained from this blog, you should be able to create complex and sophisticated scripts that can make decisions and execute commands based on a wide range of conditions.
To continue your learning journey, I recommend exploring the following topics:
- Shell variables and parameter expansion
- Command line arguments and options
- Loops and functions in bash
Challenge
To test your understanding of bash conditionals, try writing a script that takes a command line argument and performs the following actions based on its value:
- If the argument is equal to
"hello"
, print “Hello, World!” - If the argument is equal to
"goodbye"
, print “Goodbye, World!” - If the argument is anything else, print “I don’t understand.”
Post your solution in the comments below!