1. Overview

We’ve previously learned how to pass command-line arguments and use those arguments inside a Bash script.

In this brief tutorial, we’ll see the different ways to check for the existence of input arguments in a Bash script. Of course, we’ll use Bash for our examples, so there might be slight differences with other shells.

2. Why Check the Existence of Input Arguments

Generally, when we use input arguments inside a Bash script, we may work under the assumption that they’ll always be supplied when invoking the script. Yet, that’s not always the case. Sometimes the person invoking the script might miss one or all of the arguments, resulting in unexpected and erratic behavior.

We should check for the existence of input arguments as well as their contents inside the script before using any of them to ensure our code works as expected. Let’s see the different ways to do that.

3. Check Whether No Arguments Are Supplied

Sometimes, when we call a script, we may miss the arguments completely.

Let’s write a script that checks the number of input arguments and exits the script with exit code 1 using the exit command if there are no arguments:

#!/bin/bash
if [ "$#" -eq 0 ]
then
  echo "No arguments supplied"
  exit 1
fi

The $# variable gives us the number of input arguments the script receives. The -eq argument to the test built-in checks whether the value of its two operands is equal. If yes, then the condition becomes true.

4. Check if at Least One Argument Is Supplied

Let’s see how to check if there’s at least one argument and exit the script if not:

#!/bin/bash
if [ "$#" -lt 1 ]
then
  echo "No arguments supplied"
  exit 1
fi

The -lt argument to test checks whether the value of the left operand is less than that of the right operand. If yes, then the condition becomes true. In this case, we compare the number of arguments to 1 and, if less than 1, we terminate the script with an exit status of 1.

5. Check if the Input Arguments Count Is as Expected

We’ll see how to check if the number of arguments supplied is the same as the expected count.

Let’s say the expected argument count is three. Now let’s check if the supplied arguments count equals three and exit the script if not:

#!/bin/bash
if [ "$#" -ne 3 ]
then
  echo "Incorrect number of arguments"
  exit 1
fi

The -ne argument of test checks whether the value of the left operand is not equal to that of the right operand. If yes, then the condition becomes true. In this case, we compare the number of arguments to 3 and, if not equal, we terminate the script with an exit status of 1.

6. Check if a Specific Argument Is Supplied

We’ll see how to check if a particular argument is present.

Let’s check whether the length of the first argument is non-zero:

#!/bin/bash
if [ -n "$1" ]
then
  echo "First argument present"
fi

The -n test operator checks whether the given string operand has a non-zero length. If it does, then the expression returns true.

Now, let’s do a negative size check of the first argument and exit if it’s zero:

#!/bin/bash
if [ -z "$1" ]
then
  echo "No argument supplied"
  exit 1
fi

The -z test operator checks if the given string operand has a zero length. If it does, then it returns true, meaning our argument ($1) has a length of zero.

7. Conclusion

In this article, we’ve discussed how to check for the existence of input arguments inside a Bash script during runtime.

Using a combination of the test operator and $#, we can check the number of arguments received and decide whether to continue the script execution. Further, we can use a combination of the test operator and positional command-line arguments ($1, $2, $3, etc.) to check whether the script receives a specific argument and decide whether to continue the script execution.