Bash-Positional Parameters

姚子石
2023-12-01

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.


http://www.thegeekstuff.com/2010/05/bash-shell-positional-parameters/

 类似资料: