Programming in bash can be fun sometimes. Know how to separate your ifs from your else-ifs or elif, as they are called in bash, it can also be fun. Find out how to get your bash conditionals right.
Bash Conditionals: if, then, else, elif
As good as all coding languages, there are conditionals – conditional statements that make it possible to test a range of situations. In most programming languages, a basic if
The declaration would allow to test the state or value of a given programming variable. As an example, you could test if a variable is empty or not.
To learn more about bash variables, you might want to check out our post Bash Functions and Local Variables.
Bash is a full-fledged Linux shell and complete programming language. It also includes many extensions to the most common conditional statements within its programming language. / scripting. As an example, one can test the presence of files, to see if a grep -q
statement was successful and so on.
At the same time, one can specify complex conditional statements, and even sublayers within the if
etc. conditional statement proper. This makes Bash very suitable for manipulation / big data manipulation, text analysis and many other DevOps-like tasks.
This post will mainly focus on getting the correct conditionals, using if...then
, else
, and elif
statements. A future post will discuss more complex test conditions, using sublayers within conditional statements, etc.
Bash conditional test: and … then … be
Writing a conditional statement in Bash is easy and straightforward. You can even write them directly on the Bash command line, without using a script:
if [ "1" == "1" ]; then echo 'true'; be
The result is certain, What 1
matches 1
. Note that the way to test for equality between items is to use ==
and no =
. This is the case in many languages and, often, is done to avoid or clearly separate from the "assignment" (In other words, determine a variable at a given value).
Also note that we terminate each conditional if
declaration with a termination fi
(the back of and) statement. this allows us to specify several lines after then. then
clause before ending the later section.
Bash conditional test: other And variables
Let's put this in a small script, let's add a else
section and add some variable checks.
Define test.sh
as follows:
#!/bin/bash VAR1=1 VAR2=1 if [ "${VAR1}" == "${VAR2}" ]; then echo 'true' else echo 'false' fi
Subsequently, we make this little script executable by issuing chmod +x test.sh
which sets the executable flag for the test.sh
text.
Within the script we established VAR1
and VAR2
to the value of 1. Next, we issue an if statement that compares the two variables, and we echo true
if the comparison is valid, and false
if the comparison failed. The result is a correct true
production.
Bash conditional test: and Advanced nesting and checking
We can expand the last example a little more and check the inequality using !=
instead of ==
, add nested loops and use some native Bash advanced variable checks at the same time.
Define test2.sh
as follows:
#!/bin/bash VAR1="${1}" VAR2="${2}" if [ ! -With "${VAR1}" -a ! -With "${VAR2}" ]; then if [ "${VAR1}" != "${VAR2}" ]; then echo 'true' else echo 'false' fi else echo "Assert: Either VAR1 (value: '${VAR1}'), or VAR2 (value: '${VAR2}'), or both, are empty!" be
In this script, we replace our coded values of 1
for both VAR1
and VAR2
with two special variables namely ${1}
and ${2}
, that represent the first and second option / parameter, passed from the command line to the script. We make our script executable again and run it with various combinations of incorrect alternatives.
the -z
the code means check if a parameter is empty or not. We deny the result (In other words, yes becomes no and he does not become yes, or rather / better, true becomes false and false becomes true) through the use of an exclamation point (!
) in front of -z
cheque. Later we also use a AND clause (In other words, both sides of the AND clause has to be proven true).
In other words, the way you could read the if [ ! -z "${VAR1}" -a ! -z "${VAR2}" ];
the line in natural language is Both VAR1 and VAR2 must not be empty. We can see that our variables are being checked correctly by this conditional statement, since every time we try to pass only one variable, or two variables where one is empty, the program jumps to the else
clause informing about the incorrect use of our script option.
In conclusion, inside the first if
conditional statement, we have a high school (computer jargon: nested) conditional sentence. This statement verifies our inequality using it is not the same (!=
). effectively, when we pass two different values, namely 1
and 2
for the script, the result is true: these numbers are unequal.
Bash conditional test: elif
When you start developing more complex and deeply nested statements in Bash, you will soon find that there is a case where you are branching deeper and deeper into the nested code., and the code begins to look more complex due to the multiple layers of depth. Often, even when not always, you can use a elif
declaration in such cases. As an example:
#!/bin/bash if [ "${1}" -lt 2 ]; then echo "less than 2" else if [ "${1}" -lt 4 ]; then echo "less than 4" else if [ "${1}" -lt 6 ]; then echo "less than 6" fi fi fi if [ "${1}" -lt 2 ]; then echo "less than 2" elif [ "${1}" -lt 4 ]; then echo "less than 4" elif [ "${1}" -lt 6 ]; then echo "less than 6" be
Having defined this script as test3.sh, we make it executable and run it.
The two code blocks do exactly the same: they check if the value passed as the first option to the script (1, 3 and 5 respectively) is less than (-lt
) The values 2, 4 and 6 in sequence. We see that the two blocks work exactly the same.
Despite this, the second block of code, that employs the use of elif
statements (which can also be read as but statements) instead of plus … and conditional statement blocks. The second block of code is not only shorter, it is also cleaner, clearer and more pleasing to the eye. Note that you can also combine else
and elif
statements in combination, nested way, etc.
conclusion
Writing bash code is, was and probably will be for quite some time an enjoyable exercise for many. Creating well-crafted conditional statements is an integral and common part of this.. In this short tutorial, we analyze if
, then
, else
, elif
and fi
statements. Using the various conditional statements, you will be able to create excellent and clean code. Enjoy!