Beginners Guide To BASH - Input Parameters

Pass information to your BASH scripts

BASH Input Parameters screenshot

Lifewire 

Welcome to the 2nd part of the Beginners Guide To BASH series which is unique in that it is the only BASH tutorial written by a beginner for beginners.

Readers of this guide will build up their knowledge, and hopefully by the end, will be able to write some fairly clever scripts.

Last time we covered creating your first script which simply displayed the words "Hello World". It covers subjects such as text editors, how to open a terminal window, where to put your scripts, how to display the words "Hello World" and some finer points on escape characters such as quotes ("").

This section covers input parameters. There are other guides that teach this sort of thing but all too frequently, they jump into some fairly low-level stuff and maybe provide too much information.

What Is A Parameter?

In the "Hello World" script from the last tutorial it was all very static. The script didn't really do much at all.

How could we improve on the "Hello World" script?

What about a script that greets the person who runs it? Instead of saying "Hello World" it will say "Hello Gary", "Hello Tim" or "Hello Dolly". 

Without the ability to accept input parameters we would need to write three scripts "hellogary.sh", "hellotim.sh" and "hellodolly.sh".

By allowing our script to read input parameters we can use one script to greet anyone.

To do this open up a terminal window (CTRL + ALT + T) and navigate to your scripts folder by typing the following: (about cd command)

cd scripts

Create a new script called greetme.sh by typing the following: (about touch command)

touch greetme.sh

Open the script in your favorite editor by typing the following: (about nano command)

nano greetme.sh

Enter the following text within nano:

#!/bin/bash
echo "hello $@"

Press CTRL and O to save the file and then CTRL and X to close the file.

Linux basic Bash script with parameters

To run the script enter the following into the command line replacing with your name.

sh greetme.sh Tim

If I run the script with my name it displays the words "Hello Gary".

Linux run basic script with parameters

The first line has the #!/bin/bash line which is used to identify the file as a bash script.

The second line uses the echo statement to echo the word hello and then there is the strange $@ notation. (about echo command)

The $@ expands to display every parameter that was entered along with the script name. So if you typed "sh greetme.sh tim" the words "hello tim" would be displayed. If you typed "greetme.sh tim smith" then the words "hello tim smith" would be displayed.

It might be nice for the greetme.sh script to just say hello using just the first name. Nobody says, "Hello Gary Newell," when they meet someone, but they might say, "Hello Gary," though.

Let's change the script so that it just uses the first parameter. Open up the greetme.sh script in nano by typing the following:

nano greetme.sh

Change the script so that it reads as follows:

#!/bin/bash
echo "hello $1"

Save the script by pressing CTRL and O and then exit by pressing CTRL and X.

Linux Bash script with individual parameter

Run the script as shown below (replace the name with yours):

sh greetme.sh gary newell

When you run the script it will simply say, "Hello Gary" (or hopefully "hello " and whatever your name is).

Linux run Bash script with single parameter

The 1 after the $ symbol basically says to the echo command, use the first parameter. If you replace the $1 with a $2, then it would display "hello newell" (or whatever your surname is). You can also added $2 after $1 to get the full name.

Linux Bash script with multiple parameters

Running it with more than one parameter will display them in the places corresponding to the order they were entered.

Linux run script with multiple parameters

Incidentally, if you replaced the $2 with a $3 and ran the script with just 2 parameters, the output would simply be "Hello."

It is possible to display and handle the number of parameters actually entered, and later tutorials will show you how to use the parameter count for validation purposes.

To display the number of parameters entered, open the greetme.sh script (nano greetme.sh), and amend the text as follows:

#!/bin/bash
echo "you entered $# names"
echo "hello $@"

Press CTRL and O to save the script and CTRL and X to exit nano.

Linux Bash script count parameters

The $# on the 2nd line displays the number of parameters entered.

Thus far, all of this has been novel but not very useful. Who needs a script that simply displays "hello"? 

Linux run bash script counting parameters

The real usage for echo statements is to provide verbose and meaningful output to the user. If you can imagine that you want to do something complicated which involves some serious number crunching and file/folder manipulation, it would be useful to display to the user what is happening each step of the way.

By contrast, input parameters make your script interactive. Without input parameters you would need dozens of scripts all doing very similar things but with slightly different names.

With all of this in mind, there are some other useful input parameters that it is a good idea to know and I will include them all in one code snippet.

Open up your greetme.sh script and amend it as follows:

#!/bin/bash
echo "Filename: $0"
echo "Process ID: $$"
echo "-------------------------------"
echo "you entered $# names "
echo "hello $@"

Press CTRL and O to save the file and CTRL and X to exit.

Linux Bash script additional information

Now, run the script (replace with your name).

sh greetme.sh 

This time the script displays the following:

Filename: greetme.sh
Process ID: 18595
------------------------------
you entered 2 names
hello gary newell
Linux Bash run script with additional info

The $0 on the first line of the script displays the name of the script you are running. Note that it is dollar zero and not dollar o.

The $$ on the second line displays the process id of the script you are running. Why is this useful? If you are running a script in the foreground, you can cancel it by simply pressing CTRL and C. If you ran the script in the background and it started looping and doing the same thing over and over or started causing damage to your system you would need to kill it.

To kill a script running in the background, you need the process id of the script. Wouldn't it be good if the script gave the process id as part of its output? (about ps and kill commands)

Finally, before we finish with this topic, it's important to discuss where the output goes. Every time the script has run thus far the output has been displayed on the screen.

It is quite common for script output to be written to an output file. To do this run your script as follows:

sh greetme.sh gary > greetme.log

The > symbol in the above command outputs the text "hello gary" to a file called greetme.log.

Every time you run the script with the > symbol it overwrites the contents of the output file. If you would prefer to append to the file replace the > with >>.

Summary

You should now be able to write text to the screen and accept input parameters.