Enchanted Code

Selection In Bash

4 minutes read

Intro

This tutorial is aimed to provide knowledge on how you can create if statements in a bash script and the power they hold.

Requirements

Why

If statements allow you to add logic into your script. This allows you to create scripts that will react differently depending on inputs given.

Creation

Before using the more advanced features of if statements, we first need to know how to create a basic one.

It is possible to make nested if statements

If

Let’s start with the basics, just a plain-old if statement. This will execute code contained between the “then” and the “fi” if the test is true. A test is defined inside the double square brackets [[ ]]. There are lots of different tests we can place, these will be discussed later in this tutorial.

“fi” is means the end of an “if block”.

Square brackets must have a space instead separating the tests.

Only one if is allowed per block

Double brackets [[ ]] are bash’s improvement to [ ] from sh

1
2
3
4
if [[ <test> ]]
then
    <code to run>
fi

Else

The next is adding a “else”, this will run the code between “else” and “fi” only when the above statement is not true.

Only one else is allowed.

1
2
3
4
5
6
if [[ <test> ]]
then
    <code to run>
else
    <other code to run>
fi

Elif

An “elif” allows us to add more logic other than else’s. It allows for another test to run notice how now the elif statement does not end with “fi” but instead “else”.

An elif/if statement can end with fi or elif/else.

Multiple elif’s are allowed

1
2
3
4
5
6
7
8
9
if [[ <test> ]]
then
    <code to run>
elif [[ <other test> ]]
then
    <other code to run>
else
    <other other code to run>
fi

Tests

In bash we can test for things in many different ways.

String

If we want to compare strings we can use four different methods.

Operator Description
= check if equal
!= check if not equal
-n if length is greater than 0
-z if length is 0

When using -n or -z we don’t specify two strings.

This example will compare whether “001” is equal to “1”.

1
2
3
4
5
6
if [[ 001 = 1 ]]
then
    echo equal
else
    echo not equal
fi

As you should see when run we get that they are not equal.

Numeric

If we wanted to compare numbers, we have a completely different syntax than the string method.

Operator Description
-eq check if equal
-gt check if greater than
-lt check if less than

We will use the same example as in the string section, but this time will use a numeric comparison.

1
2
3
4
5
6
if [[ 001 -eq 1 ]]
then
    echo equal
else
    echo not equal
fi

When run we should see that now they are shown as equal.

File

If we are working with files we can do various checks.

Operator Description
-d Check if path is a directory
-f Check if path is a file
-e Check if path exists
-s Check if filesize is not zero
-r Check if path is readable
-w Check if path is writeable
-x Check if path is executable

There are many more.

1
2
3
4
5
6
if [[ -e /usr/bin/bash ]]
then
    echo bash exists
else
    echo bash does not exist
fi

When we run this we should see that indeed our bash executable is present.

If on a different system the path may need to be changed.

Multiple Tests

What happens when we want to test for several outcomes in one statement. Well we can do this using boolean conditions.

Operator Description
&& boolean and
|| boolean or

For example this is how we could have two tests:

1
2
3
4
if [[ <test-1> ]] && [[ <test-2> ]]
then
    <code to run>
fi

Conclusion

As you can see we can do lot’s with bash if statements. However this tutorial we have only scratched the surface, there is much more. The following is a list of other things we can do:

  • Use regular expressions
  • More file checks
  • More string checks

References

See Also

Buy Me A Coffee