bash-logo

In bash, we have many variables. Before learning what are positional parameters and special variables we should know what are variables and how to pass them into a shell script?

What are variables in Shell scripting?

A variable is a value holder which we can change when it is required. We can use these variables in our shell scripts so that we no need to hard code the values with the shell script.

  1. System variables
  2. User defined variables
  3. Special variables
    • Positional parameters
    • Special variables

We already discussed System variables, Some special variables, and command exit status in our other posts. Before learning positional we should know how we pass variables to a shell script.

How to pass variables in Shell scripting?

We can pass variables into shell scripts in different ways to avoid hard coding of values. We have different ways where we can pass variables to a shell script. Positional parameters are one kind of passing variables into shell scripting. Below are the way we can pass variables into shell scripting depending on what time you want to send them to a script.

  • Within shell script(Variables defined with the script)
  • Before start of shell script(Positional parameters)
  • At the time of executing a shell script(using read command)

What are positional parameters?

Positional parameters are also called as command line arguments which we pass to the script on the fly. To understand them, take ls command as an example. The ls command has many options like -l for long listing. The option -l is a positional parameter for ls command. Suppose when we use cp command show below.
cp test/ bash/
Where test/ is my first positional parameter and bash/ is my second positional parameter. In this way, we can send variables to a shell script on the fly.
For people who want to see all these variables in one place, below list is for you.
  • $0: contains the name of script as it is invoked
  • $1, $2, …, $n: indicates the position of the argument also called as positional parameters.
  • $*: contains all the arguments regrouped as one argument
  • $@: contains all the arguments, one argument per parameter
  • $#: contains the number of parameters passed to the script
  • $? : contains the return code of the previous command (it equaled 0 when the last command was executed successfully)
  • $$: contains the PID of shell executing the script
  • $! : contains the PID of last background process
  • $_ : Last argument of an executed command

Examples:

Let’s create the script test.sh like below:

#/bin/bash

function myFunction () {

echo -e "--------------------------------------------"
echo -e "Name of the script: $0"
echo -e "--------------------------------------------"
echo -e "the arguments regrouped as one argument: $*"
echo -e "--------------------------------------------"
echo -e "the arguments, one argument per parameter: $@"
echo -e "--------------------------------------------"
echo -e "the number of parameters: $#"
echo -e "--------------------------------------------"
echo -e "the first arg: $1"
echo -e "the second arg: $2"
echo -e "the third arg: $3"
echo -e "--------------------------------------------"
echo -e "the return code: $?"
echo -e "--------------------------------------------"
echo -e "the PID of shell executing the script: $$"
echo -e "--------------------------------------------"
echo -e "the PID of last background process: $!"
echo -e "--------------------------------------------"

}

# Launch a background process
# & is used to launch a process in background

sleep 5 &

# Call the function

myFunction $1 $2 $3

Let’s run the script

user@server:~ $ bash test.sh arg1 arg2 arg3
--------------------------------------------
Name of the script: test.sh
--------------------------------------------
the arguments regrouped as one argument: arg1 arg2 arg3
--------------------------------------------
the arguments, one argument per parameter: arg1 arg2 arg3
--------------------------------------------
the number of parameters: 3
--------------------------------------------
the first arg: arg1
the second arg: arg2
the third arg: arg3
--------------------------------------------
the return code: 0
--------------------------------------------
the PID of shell executing the script: 32117
--------------------------------------------
the PID of last background process: 32118
--------------------------------------------

How many positional parameters are there?

We can say they are infinity, But there is a slight change when calling positional parameters more than nine.

Positional parameters in Linux/Unix

We can divide positional parameters into batches. This segregation is done on basics of how we call them in the shell scripts.

Batch 1: $1, $2, $3, $4, $5, $6, $7, $8, $9

We already saw how to use these from above example. They are very straight forward where you can pass values at the time of execution and call those values using $1 to $9.
Batch 2: From $10 to $n We call them in a different way.
We can not pass $10 to above values as positional parameters by default, we have to pass in a different way. See below example.
My script abc.sh content
#!/bin/bash

echo "My first positional parameter is $1"

echo "My 10th positional parameter is $10"
Executing it:
bash abc.sh 24 surendra abc xyz red 12 as face 89 kumar
Output:
My first positional parameter is 24

My 10th positional parameter is 240
If you observe my $10 is return as 240 instead of kumar. This is because bash does not know how to read $10. For this, we have to use braces across variable as ${10}. The same example with this change.
#!/bin/bash

echo "My first positional parameter is $1"

echo "My 10th positional parameter is ${10}"
Executing it:
bash abc.sh 24 surendra abc xyz red 12 as face 89 kumar
Output:
My first positional parameter is 24

My 10th positional parameter is kumar

Difference between $@ and $*

When they are quoted they behave same. The $@ will consider spaces in variables
Positional parameters in for loops
Couple of for loop examples are
Example 1:for i in $@
do
echo $i
Done
Example 2:
for i
 Do
 Echo $i
 Done

I hope that this blog helped you. Please visit our website for other interesting blogs and feel free to leave your feedbacks and thoughts. Till next time!

The following two tabs change content below.
I am a hands-on, competent Linux system engineer with 9 years’ experience. I have a strong performance background in wide variety of professional Linux system support including monitoring, configuration, troubleshooting and maintenance. I have worked on numerous projects from concept to completion. A specialist in LAMP platforms, I take pride in administrating Linux systems and regularly refresh my skills to ensure I keep up with ongoing developments and new technologies.