Learn Bash New

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

learn bash featured image 1

Welcome to Arguments chapter of 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. The read command works after our bash program is invoked by our bash interpreter ie., user input is prompted after the program is run, but this time we are interested in getting inputs as parameters or arguments so it will later be processed by our program.

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 ie., 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 with counting numbers starting from 0 and not 1 like we did in our high school mathematics class. Because they were designed that way. So the next time you are exploring further into computer engineering field, remember to start counting from 0 just 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 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 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 its blank. Try omitting all the arguments ie., 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 the three arguments directly. When we failed to provide one or more argument, the program printed a blank output for the corresponding argument list, which in a real-life scenario would not make sense to an ordinary user. 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 along with 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 of them are useful and has advantages/disadvantages over the other based on the type of the 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.

Our sponsor - Deploy your server within 3 minutes


Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More