Bash is a powerful scripting language provides by various Linux distributions, Unix and BSD. If we need to make our script dynamic we generally use arguments. Arguments are provided to the script through the command line. In this tutorial, we will examine different use cases of argument passing and examples.
Syntax
In order to use externally provided values inside the bash script, we should provide them after the script name. Following syntax assumes the script is executable.
myscript.sh PARAM1 PARAM2 ...
Or we can use bash
in order to interpret our script like below and provide parameters.
bash myscript.sh PARAM1 PARAM2 ...
Provide Arguments
We have learned the syntax of the arguments. In this example, we will look use cases about argument passing. We will provide two arguments for example.
$ myscript.sh 1 3
If we need to provide a string with multiple words we can use single or double-quotes. In this example, we will prove 10
and Hello Poftut
as two separate arguments.
$ myscript.sh 10 "Hello Poftut"
Read Argument Inside Script
Now the real work starts now. We have learned how to provide arguments from the shell or command line. In this part, we will look at how to read them inside the script. Bash provides $1
, $2
, … like usage for reading input arguments inside script. the first argument can be accessed from the variable name $1
, the second one $2
and so … In this example, we will provide two-argument Hello
and Poftut
to script.
#!/bin/bash echo $1 $2

Detecting Command Line Arguments
If some command-line arguments are provided for our script we can use the $1, $2, $3, … variables in order to detect command line arguments. We can use the if statement to check specific input like below. Below we will check the positional parameter 1 and if it contains some value the value will be printed to the screen.
#!/bin/bash if [ "$1" != "" ]; then echo "Positional parameter 1 contains value"
echo $1 else echo "Positional parameter 1 is empty" fi
Assign Provided Arguments To Bash Variable
Another use case for bash variables is assigning them new variables with meaningful names. This will give us the ability to change provided values and use them properly with their names. In this example, we will put Hello
and Poftut
into variables named salute
and name
.
#!/bin/bash salute=$1 name=$2 echo $salute $name

Read Multiple Arguments with For or While
Some times some arguments will not fit our condition and we may need a lot of arguments to provide and use them in an iterative way. In this condition, we can use bash loop mechanisms for
and while
. We can iterate over given arguments like an array or list with for
and while
. In this example, we will iterate over provided arguments and print them to the shell. We will use $@
to specify the list of provided arguments and put each item into a variable named var
in each step.
#!/bin/bash for var in "$@" do echo $var done

Read With Parameter Names
As regular Linux applications, we can use parameter names for arguments and parse them accordingly. We will use getops
function which will parse provided options in an array. In this example, we will use while
loop with case
structure. We will parse the provided parameter names u
which is username and p
password.
#!/bin/bash while getopts u:p: option do case "${option}" in u) USER=${OPTARG};; p) PASSWORD=${OPTARG};; esac done echo "User:"$USER echo "Password:"$PASSWORD

Read Bash Parameters with getopts Function
Bash provides different functions to make reading bash input parameters. getopts
is a function where it can be used to read specified named parameters and set into the bash variables in a easy way. getopst will read every input parameter and look for the options to match and if match occrus the parameter value set to given variable name.
while getopts u:a:l: option
do
case "${option}"
in
u) USER=${OPTARG};;
a) AGE=${OPTARG};;
l) LOCATION=${OPTARG};;
esac
done
echo "Given user name is: "$USER
echo "Given age is: "$DATE
echo "Given location is: "$LOCATION
We can run this script like below but before running is we have to make the script runable with the chmod command.
$ chmod u+x getopts_example.sh
$ ./getopts_example.sh -u ismail -a 36 -l Turkey

Get The Number Of Arguments Passed
Bash provides the number of the arguments passed with the $#
variable. We can get the number of the arguments passed and use for different cases where below we will print the number of the arguments passed to the terminal.
#!/bin/bash
echo "You provided $# arguments"

Print Values Of All Arguments
We can also print all provided arguments or values with a single variable $@
.
#!/bin/bash
echo "All Arguments values:" $@

1 thought on “How To Pass and Parse Linux Bash Script Arguments and Parameters”