Learn Bash

Arguments | Another way to work with user inputs – Part 7

Arguments Another way to work with user inputs

Welcome to the Arguments chapter of the BASH scripting series. This chapter will introduce you to another new method of getting user inputs for your script. In the previous chapter, we’ve used the read command to get user inputs. Our bash interpreter invokes the read command works after our bash program, i.e., user input is prompted after the program is run. Still, we are interested in getting input parameters or arguments, so our program will process them later.

What are arguments

Arguments are additional data passed as inputs to a bash program.

A very common example is the execution of our bash script where we first typed in the shell interpreter name, i.e., bash, which is the program name, and then succeeding it with our bash script file name. So the bash script file name is the argument to the program bash.

bash bash-file.sh

Moreover, you must have performed some simple file and folder manipulation commands like touch, mv, rm, etc., on your terminal program. The commands are the program, and the corresponding file/folder name, along with options like -r, -f, etc., are additional data or arguments to the program.

Arguments in BASH

$0

$0 is the program name or our bash script file name.

Computers have a peculiar way of counting numbers starting from 0 and not 1, as we did in our high school mathematics class. Because they were designed that way. So the next time you are exploring further into the computer engineering field, remember to start counting from 0 to blend in with this soulless dude 😛 Trust me, it will appreciate you.

#!/bin/bash
# print $0 parameter
echo The script file name is: $0
$0 output

$1 and so on

After $0, we have a limitless argument list that is $1, $2, $3,… And each represents a particular argument based on its number or index position. So $1 refers to the first argument after the bash script file name, $2 refers to the second argument, $3 to the third argument, and so on.

An example command that fits this criterion is mv command. mv has the syntax

mv source destination

Looking at the above syntax, we can categorize their argument position as:

$0 – mv

$1 – source

$2 – destination

Copy and save the below sample script as a parameter.sh, and try running it as

bash parameter.sh one two three

In case you omitted the argument “three” and ran the program, you’d notice that nothing was displayed for Argument 3: instead, it’s blank. Try omitting all the arguments, i.e., one two three, and notice the output on your terminal screen.

$*

$* refers to all the arguments specified starting from $1 till the end. This parameter is useful if you wish to print out all the arguments in one go. Below is another sample script to try, and don’t forget to pass some arguments when running the script.

#!/bin/bash
# print $0 parameter
echo The script file name is: $0
# print all arguments
echo Arguments are: $*
$* output

$#

Another cool BASH parameter is $#, which counts all the total number of arguments provided in the list.

Imagine you wish to make sure there are exactly three argument lists before proceeding with the program execution; otherwise, we abort the program to avoid undesired output. We can relate to our second BASH program above, where we printed all three arguments directly. When we failed to provide one or more arguments, the program printed a blank output for the corresponding argument list, which would not make sense to an ordinary user in a real-life scenario. So below is an improved script that will first determine if it is ok to print the arguments based on the argument count.

#!/bin/bash
# print $0 parameter
echo The script file name is: $0
# make sure argument count is equal to 3
if [[ $# -eq 3 ]] ; then
    # print all arguments
    echo Argument 1: $1
    echo Argument 2: $2
    echo Argument 3: $3
fi

Practice

Below is a tad-bit complex BASH script that reinforces all the above concepts and a while loop in the program. Try to analyze what the code is doing by reading the steps line by line. Comments are also provided to help you understand faster 🙂

#!/bin/bash
# print an empty newline so the output looks good
echo
# print greetings
echo Welcome to $0 program
# inform the total number of arguments
echo You have a total of $# arguments
# print all the arguments upto $# times
echo They are
i=0
while [ $i -lt $# ]
do
    echo $*
    i=$((i+1))
done
practice output

Conclusion

Arguments are another way to provide inputs to our BASH program apart from the read command. Both are useful and have advantages/disadvantages over the other based on the type of program being implemented. Finally, this chapter ends the fundamental theories of this scripting language. But before concluding this series, we will work on a mini project in the next chapter. We will learn how to build a simple basic calculator program by reinforcing all the concepts learned in the previous chapters. Until then, see you next time.



Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.