When working with bash scripting, you may encounter situations where you need to pass arguments to your script via the command line. In this article, we’ll explore the different ways to read command line arguments in Bash and provide examples of each method.
What are Command Line Arguments in Bash
Command line arguments in bash are the parameters that are passed to a bash script or command when it is executed from the command line. They are used to customize the behavior of the script or command, and can include options, flags, or values that are used by the script or command to perform specific tasks.
How to Read Command Line Arguments in a Bash
To modify the behavior of the script Command line arguments are mainly used that include options or flags. For example, you might have a script that performs a backup operation, and you want to include an option to specify the destination folder for the backup. Here are the three ways by which one can read command line arguments in bash script:
How to Read Command Line Arguments Using $ Command in Bash
The most basic way to read command line arguments in Bash is to use the variables $0, $1, $2, etc. These variables represent the script name ($0) and the positional parameters passed to the script ($1, $2, etc.). Here’s an example bash code that uses the $ command to read command line arguments:
echo "Script name: $0"
echo "Argument1: $1"
echo "Argument2: $2"
This script reads command line arguments in bash using the $0, $1, and $2 variables. The $0 variable contains the name of the script itself, while $1 and $2 contain the first and second command line arguments, respectively and to pass arguments to the script here is the syntax:
Here the file name can be included as an argument if they start from $0 and so on, this script reads command line arguments in Bash using the $0, $1, and $2 variables. The $0 variable contains the name of the script itself, while $1 and $2 contain the first and second command line arguments, respectively and below is the output for it:
How to Read Command Line Arguments Using Shift Command in Bash
The “shift” command in Bash allows you to shift the positional parameters to the left, discarding the first argument and moving the rest down by one position. This can be useful if you need to process arguments in a loop or if you want to handle arguments in a specific order. Here’s an example:
echo "Script name: $0"
while [ "$#" -gt 0 ]; do
echo "Argument1": $1
shift
done
The script starts by printing the name of the script using the $0 variable, which contains the name of the script as it was called from the command line. The next section of the script is a while loop that checks whether there are any remaining command line arguments to process. It does this by checking the value of the special $# variable, which contains the number of command line arguments.
The loop then prints the current argument using the $1 variable, which contains the first argument, and then uses the shift command to remove the first argument from the list of command line arguments. This shifts all the remaining arguments down by one position, so that $1 becomes the second argument, $2 becomes the third argument, and so on.
How to Read Command Line Arguments Using getopts
The “getopts’ command in Bash allows you to parse command line arguments and options, this is helpful when you need to give optional arguments or flags to your script. Here’s an example code that uses the getopts command and read two arguments from command line:
while getopts ":a:b:" opt; do
case $opt in
a) arg1="$OPTARG"
;;
b) arg2="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
echo "Argument 1: $arg1"
echo "Argument 2: $arg2"
The getopts command is used to iterate over the command line options and arguments. It takes two arguments: one is a string of all the expected options, and the other is the variable name that will hold the value of the currently processed option.
In this script, the expected options are -a and -b, which are followed by their corresponding arguments. The getopts command parses these options and arguments and assigns them to the $opt variable.
The case statement is then used to process each option. If the option is -a, the value of the corresponding argument is assigned to the $arg1 variable. If the option is -b, the value of the corresponding argument is assigned to the $arg2 variable. If an invalid option is detected, an error message is printed to the console.
After all options and arguments have been processed, the script prints the values of $arg1 and $arg2 to the console using the echo command, here is the output for the code:
Conclusion
Command line arguments in Bash are used to pass parameters to a script or command when it is executed from the command line. They can include values, options, or flags that modify the behavior of the script, and are essential for customizing the behavior of Bash scripts and commands. There are three ways to read command line arguments and those are: using $ sign, using getops command and using shift and all these are mentioned in this guide.