Linux Bash Scripting Part3 – Parameters and Options

So far you’ve seen how to write Linux bash scripts that do the job without user inputs. Today we will continue our series about Linux bash scripting.

I recommend you to review the previous posts if you want to know what we are talking about.

bash scripting part1

bash scripting part2

Today we will know how to retrieve input from the user and deal with that input, so our script becomes more interactive.

To pass data to your shell script, you should use command line parameters.

$ ./myscript 3 5

Here we send 2 parameters (3 and 5) to the script. So how to read these parameters in our bash script?

Read parameters

The shell gives you some read to use variables to process input parameters:

  • $0 is the script’s name.
  • $1 is the 1st parameter.
  • $2 is the 2nd parameter.

Until the 9th parameter which is $9.

Let’s see those variables in action:

Check the results of the following command:

./myscript 5 10 15

linux bash scripting parameters

Here is another example of how we can use two parameters and calculate the sum of them.

$ ./myscript 5 10


The parameters are not restricted to numbers, they could be strings like this:

./myscript Adam

And the result is as expected.


What if our parameter contains a space and we want to pass it as one value? I guess you know the answer from the previous posts. The answer is to use quotations.

If your script requires over nine parameters, you should use braces like this ${10}.

Check parameters

If you don’t pass parameters and your code expecting it, your script will exit with an error.

That’s why we should a Linux if statement to make sure that they exist.


Counting parameters

To get how many parameters passed, you can use this variable ($#).

./myscript 1 2 3 4 5


How awesome is Linux bash scripting? This variable also provides a geeky way to get the last parameter. Look at this trick:


Iterate over parameters

The $* variable holds all the parameters as one value.

The [email protected] variable holds all the parameters as separate values, so you can iterate over them.

This code shows how to use them:


The same result, but if you want to know the difference look at the following example:

Check the result to see the difference:


The result is pretty clear. You can use any one of them according to your needs.

Shifting parameter variable

The shift command moves every parameter variable to the left:

variable $3 ==> variable $2.

variable $2 ==> variable $1.

variable $1 ==> dropped.

variable $0 ==> (the script name) as it is.

You can use the shift command to iterate over parameters like this:

Here, we have a while loop checking $1 length. If $1 becomes zero, the loop stops. And the shift command is shifting all passed parameters to the left.


Careful when using shift command, the shifted parameter is gone and cannot be recovered.

Bash scripting options

Options are single letters with a dash before it.

$ ./myscript -op1 -op2 -op3 -op4


The code check for one of the correct options. If you typed one of them, the suitable commands will run.

Separate options from parameters

Sometimes you need to use options and parameters in the same script. You have to separate them. By doing this, you are telling the bash where are the parameters and where are the options.

The double dash (–) is used to end the options list. After the shell sees the double dash, the remaining inputs are treated as parameters and not as options.

$ ./myscript -a -b -c -- 5 10 15


As you can see from the result, all the parameters after the double dash treated as parameters and not options.

Process options with values

When you dig deep into Linux bash scripting, sometimes you need options with additional parameter values like this:

./myscript -a value1 -b -c value2

There should be a way to identify any additional parameter for the options and be able to process it.

And if we run it with these options:

$ ./myscript -a -b test1 -d


From the results, you can see that we get the parameter for the -b option using the $2 variable.

Standard options

When you start your Linux bash scripting, you are free to choose which letter is suitable for your option.

However, there are some letters that are commonly used in Linux programs.

And here is the list of the common options:

-a            List all items.

-c            Get count of items.

-d            Output directory.

-e            Expands items.

-f             To specify a file.

-h            To show help page.

-i             To ignore character case.

-l             To list a text.

-n            To say no for a question.

-o            To send output to a file or so.

-q            Keep silent don’t ask the user.

-r             To process something recursively.

-s            Stealth mode.

-v           Verbose mode.

-x            Specify executable.

-y            To say yes without prompting the user.

If you work with Linux, many of these options may look familiar to you.

it’s good to follow standards.

Getting user input using the read command

Sometimes you need data from the user while the bash scripting is running.

The bash shell uses the read command for this purpose.

The read command reads input from standard input (the keyboard) or from a file descriptor and stores it in a variable:

The -n option is used to disable the newline, so you can type your text in the same line.


You can specify multiple inputs like this:


If you don’t specify variables for the read command, all incoming inputs are saved in REPLY variable.


You can use the -t option to specify a timeout for input in seconds.

If you do not enter data for five seconds the script will execute the else clause and print a sorry message.


Reading password

In Linux bash scripting, sometimes you don’t want the user input to be displayed on the screen, like entering a password.

The -s option suppresses the output from appearing on the screen.


Read files

The read command can read files one line on each call.

Now, if you want to get all file data, you can get the content using the cat command, then send it to the read command using while loop like this:

Or we can make it simpler by redirecting the file content to the while loop like this:


We just pass the file content to the while loop and iterate over every line and print the line number and the content, and each time you increase the count by one.

I hope you find this post interesting. Keep coming back.

Thank you.

Mokhtar Ebrahim
I'm working as a Linux system administrator since 2010. I'm responsible for maintaining, securing, and troubleshooting Linux servers for multiple clients around the world. I love writing shell and Python scripts to automate my work.

8 thoughts on “Linux Bash Scripting Part3 – Parameters and Options

  1. Hello.
    I have a question about “Reading password” part:
    – read command makes the text color as the background color of the shell.

    I thought it works in a different way, it hides all that you typed. Otherwise you could select all that you typed and copy your hidden text. Can someone clarify this part? 🙂

    Thank you!

    1. Correct.
      This is because the -s only hides what you entered by making the text color as the terminal background color as mentioned above.

Leave a Reply

Your email address will not be published. Required fields are marked *