≡ Menu

Bash Positional Parameters Explained with 2 Example Shell Scripts

A parameter is an entity that stores values. It can be a name, a number or some special characters. A variable is a parameter denoted by a name. Some variables are set for you already, and most of these cannot have values assigned to them.

These variables contain useful information, which can be used by a shell script to know about the environment in which it is running.

Bash provides two kind of parameters.

  • Positional Parameter
  • Special Parameter

In this article, let us discuss about bash positional parameter with the examples.

This article is part of our on-going bash tutorial series.

Example 1: Bash Positional Parameter – $0, $1, $2 ..

Positional parameters  are the arguments given to your scripts when it is invoked. It could be from $1 to $N. When N consists of more than a single digit, it must be enclosed in a braces like ${N}.

The variable $0 is the basename of the program as it was called.

The following example gets two arguments and provides arithmetic operations result between those two integers.

First, create the arithmetic.sh shell script as shown below.

$ cat arithmetic.sh
#!/bin/bash

echo -e  "\$1=$1"
echo -e "\$2=$2"

let add=$1+$2
let sub=$1-$2
let mul=$1*$2
let div=$1/$2

echo -e "Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n"

Next, execute the arithmetic.sh with proper parameters as shown below.

$ ./arithmetic.sh 12 10
$1=12
$2=10
Addition=22
Subtraction=2
Multiplication=120
Division=1

In the above output $1 has the value 12, and $2 has 10.

Shell builtin ‘let’ allows arithmetic operation to be performed on shell variables. The above script does the arithmetic operations such as addition, subtraction, multiplication and division on the given parameters.

Example 2: Set / Unset Bash Positional Parameters

The built in set command is used to set and unset the positional parameter.

First, create the positional.sh shell script as shown below.

$ cat positional.sh
#!/bin/bash

# From command line
echo -e "Basename=$0"
echo -e  "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"

# From Set builtin
set First Second Third
echo -e  "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"

# Store positional parameters with -(hyphen)
set - -f -s -t
echo -e  "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"

# Unset positional parameter
set --
echo -e  "\$1=$1"
echo -e "\$2=$2"
echo -e "\$3=$3"

The above script prints the command line arguments first, then set command sets the positional parameter explicitly. Set with the – refers end of options, all following arguments are positional parameter even they can begin with ‘-‘. Set with ‘–‘ with out any other arguments unset all the positional parameters.

Next, execute the positional.sh as shown below.

$ ./positional.sh
Basename=t.sh
$1=12
$2=10
$3=
$1=First
$2=Second
$3=Third
$1=-f
$2=-s
$3=-t
$1=
$2=
$3=

In the next article, let us discuss about bash special parameters with examples.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Chris F.A. Johnson May 10, 2010, 6:18 pm

    Bash provides three types of parameters, not two: Positional parameters, special parameters and variables. These parameters are denoted by:
    numbers, starting at 1;
    specials characters, * @ # ? – $ ! 0 and _; and
    variables names
    (See the man page under PARAMETERS.)

    The variable $0 is a special parameter, not a positional parameter, and is NOT normally the basename of the program as it was called. It is the full path to the script.

  • G. Rapagnani May 31, 2010, 4:39 am

    there are errors in the last example:
    You should read:
    $ ./positional.sh 12 10
    Basename=./positional.sh
    $1=12
    $2=10
    $3=
    $1=First
    $2=Second
    $3=Third
    $1=-f
    $2=-s
    $3=-t
    $1=
    $2=
    $3=

  • Dennis July 12, 2012, 10:06 am

    You needn’t have written the -e option in the line: echo -e “Basename=$0”, since it “enables interpretation of backslash escapes”.

  • Prabh August 7, 2012, 7:38 am

    Hi
    I am writing a script where I have entered 3-digit number under a positional parameter named $digit but now I have to compare the 3 digits entered and find out if they are same (:222,333…) and create files with the same number.Even if they are not (123,234. …) I need to make that many files( file1 ,file2,file3).Now ,what can I do to split the parameter ,so that I can compare it .Or may be some other option to try it.Plz reply..

  • Chris F.A. Johnson August 7, 2012, 9:00 pm

    There is no positional parameter named $digit.

    Use parameter expansion.

    digit=222
    d1=${digit:0:1}
    d2=${digit:1:1}
    d3=${digit:2:1}

  • Alameen June 19, 2015, 8:57 pm

    Could you please explain , what is $$ and $_ in shell script?