Beginners Guide To BASH - Input Parameters

BASH Input Parameters
BASH Input Parameters.


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 as I build up my knowledge and hopefully by the end of it all we will be able to write some fairly clever scripts.

Last week I 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 week I am going to cover input parameters. There are other guides that teach this sort of thing but I find 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 "", "" and "".

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 by typing the following: (about touch command)


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


Enter the following text within nano:

echo "hello $@"

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

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

sh <yourname>

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

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 tim" the words "hello tim" would be displayed. If you typed " tim smith" then the words "hello tim smith" would be displayed.

It might be nice for the script to just say hello using just the first name. Nobody says "hello gary newell" when they meet me, they might say "hello gary" though.

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


Change the script so that it reads as follows:

echo "hello $1"

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


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

sh gary newell

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

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).

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 in later tutorials I will be showing how to use the parameter count for validation purposes.

To display the number of parameters entered open the script (nano and amend the text as follows:

echo "you entered $# names"
echo "hello $@"

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

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"? 

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 script and amend it as follows:

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.

Now Run the script (replace <yourname> with your name).

sh <yourname>

This time the script displays the following:

Process ID: 18595
you entered 2 names
hello gary newell

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 your 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 I finish with this topic I wanted to discuss about 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 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 >>.


You should now be able to write text to the screen and accept input parameters. In the next guide I will be discussing the use of variables and looking at logic to control program flow.

More From Us