How To Check Existence of Input Argument in a Bash Shell Script

Introduction

Before we start talking about how to check existence of input argument in a bash shell script, let's first understand-What is Bash Shell Script ?

An effective tool for automating repetitive activities and carrying out complex commands on the command line is bash shell scripting. When writing a Bash shell script, it's often necessary to check whether an input argument has been provided or not. To prevent unexpected script behavior, it is crucial to verify the existence of input arguments while working with them.

This tutorial will walk you through the process of checking the existence of an input argument in a Bash script.

There are three ways to check the existence of an input argument in a Bash Shell Script:

  1. Using the “test” command
  2. Using the “$#” variable
  3. Using the “-n” option

Method 1: Using the “test” Command

The built-in command "test" in Bash checks for certain conditions. It is also referred to as the "[" command. Using the "test" command, we can check whether a variable exists as one of the conditions. Here is an example of code that uses the "test" command to see if an input argument is present:

#!/bin/bash

if [ -z "$1" ]

then

echo "Input argument is missing."

exit 1

fi

echo "Input argument exists."

The "-z" option is used in this case with the "test" command to determine whether the input argument is an empty string. If the input argument is an empty string, the script will output an error and exit with a status code of 1. If not, the script will continue to execute. I've included a code below that accepts arguments, and when it does, it will display a message indicating existence of input of argument:

Method 2: Using the “$#” Variable

The number of input arguments given to a script is kept in the "$#" variable. We can determine if the "$#" variable is bigger than zero if the script requires at least one input argument. Here is an example of code that uses the "$#" variable to see if there are any input arguments at all:

#!/bin/bash

if [ $# -eq 0 ]

then

echo "Input argument is missing."

exit 1

fi

echo "Input argument exists."

In this case, the "-eq" operator is used to determine whether the "$#" variable is equal to zero or not. If it is, the script will display an error message and exit with a status code of 1, and vice versa. Otherwise, the script will continue to execute. I've included a code below that accepts arguments, and when it does, it will display a message indicating the existence of input of argument:

Method 3: Using the “-n” Option

To determine whether a variable is not empty, use the "-n" option. This option can be used to determine whether the input argument is present or not. I've provided an example of code that uses the "-n" option to see if an input argument is present, below:

#!/bin/bash

if [ -n "$1" ]

then

echo "Input argument exists."

else

echo "Input argument is missing."

exit 1

fi

The "-n" option is used in this case to determine whether the input argument is not empty. If it is, the script will display a success message. Without an argument, the script will quit with a status code of 1 and display an error message. I've provided an argument for the code below so that it will display the message of existence of input of argument:

FAQs to Check Existence of Input Argument in a Bash Shell Script

What is an input argument in a Bash script? 

Input arguments are values passed to a Bash script when it is executed. They allow you to provide input or parameters to the script to control its behavior or perform specific actions.

How can I access input arguments in a Bash script? 

Input arguments are stored in special variables: $1 refers to the first argument, $2 refers to the second argument, and so on. $0 represents the script's name itself.

How do I check if an input argument exists in a Bash script? 

You can check the existence of an input argument by checking whether the corresponding variable is empty or not. Use conditional statements like if, [[ -n "$1" ]], or [[ -z "$1" ]] to check if the argument exists.

What is the difference between [[ -n "$1" ]] and [[ -z "$1" ]]? 

[[ -n "$1" ]] checks if the input argument is not empty, while [[ -z "$1" ]] checks if the input argument is empty.

How do I print an error message if an input argument is missing? 

You can use the echo command to print an error message when an input argument is missing. For example, echo "Please provide the required argument.".

What is the purpose of the $# variable in Bash? 

The $# variable holds the total number of input arguments passed to the script, regardless of their content. It can be useful to check if a specific number of arguments have been provided.

What if I need to process an unknown number of arguments dynamically? 

If the number of input arguments can vary, you can use a loop construct like for or while to iterate over the arguments and take the necessary action.

Conclusion

Checking for input arguments is a crucial step in shell scripting to make sure the script executes as intended. To verify if input arguments are there, we can employ a variety of methods, including the "test" command, the "$#" variable, and the "-n" option. We use these methods to produce stronger, more reliable shell scripts that can handle input arguments.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.