A parameter is an entity that stores values. It can be a name, a number or some special characters.
Bash shell provides two kind of parameters.
Positional Parameter and Special Parameter
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 built-in ‘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.
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 ‘–’ without any other arguments unset all the positional parameters.
Next, execute the positional.sh as shown below.
$ ./positional.sh
Basename=positional.sh
$1=12
$2=10
$3=
$1=First
$2=Second
$3=Third
$1=-f
$2=-s
$3=-t
$1=
$2=
$3=
Use Bash $* and $@ to Expand Positional Parameters
This example shows the value available in $* and $@.
First, create the expan.sh as shown below.
$ cat expan.sh
#!/bin/bash
export IFS=’-‘
cnt=1
# Printing the data available in $*
echo “Values of \”\$*\”:”
for arg in “$*”
do
echo “Arg #$cnt= $arg”
let “cnt+=1”
done
cnt=1
# Printing the data available in $@
echo “Values of \”\$@\”:”
for arg in “$@”
do
echo “Arg #$cnt= $arg”
let “cnt+=1”
done
Next, execute the expan.sh as shown below to see how $* and $@ works.
$ ./expan.sh “This is” 2 3
Values of “$*”:
Arg #1= This is-2-3
Values of “$@”:
Arg #1= This is
Arg #2= 2
Arg #3= 3
· The above script exported the value of IFS (Internal Field Separator) with the ‘-’.
· There are three parameter passed to the script expan.sh $1=”This is”,$2=”2″ and $3=”3″.
· When printing the each value of special parameter “$*”, it gives only one value which is the whole positional parameter delimited by IFS.
· Whereas “$@” gives you each parameter as a separate word.
Use $# to Count Positional Parameters
$# is the special parameter in bash which gives you the number of positional parameter in decimal.
First, create the arithmetic.sh as shown below.
$ cat arithmetic.sh
#!/bin/bash
if [ $# -lt 2 ]
then
echo “Usage: $0 arg1 arg2”
exit
fi
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”
If the number of positional parameters is less than 2, it will throw the usage information as shown below,
$ ./arithemetic.sh 10
Usage: ./arithemetic.sh arg1 arg2
Process related Parameters – $$ and $!
The special parameter $$ will give the process ID of the shell. $! gives you the process id of the most recently executed background process.
The following script prints the process id of the shell and last execute background process ID.
$ cat proc.sh
#!/bin/bash
echo -e “Process ID=$$”
sleep 1000 &
echo -e “Background Process ID=$!”
Now, execute the above script, and check the process id which its printing.
$ ./proc.sh
Process ID=9502
Background Process ID=9503
$ ps
PID TTY TIME CMD
5970 pts/1 00:00:00 bash
9503 pts/1 00:00:00 sleep
9504 pts/1 00:00:00 ps
$
Other Bash Special Parameters – $?, $-, $_
· $? Gives the exit status of the most recently executed command.
· $- Options set using set builtin command
· $_ Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.
$ cat others.sh
#!/bin/bash
echo -e “$_”; ## Absolute name of the file which is being executed
/usr/local/bin/dbhome # execute the command.
#check the exit status of dbhome
if [ “$?” -ne “0” ]; then
echo “Sorry, Command execution failed !”
fi
echo -e “$-“; #Set options – hB
echo -e $_ # Last argument of the previous command.
In the above script, the last echo statement “echo -e $_” ($ underscore) also prints hB which is the value of last argument of the previous command. So $_ will give the value after expansion
$ ./others.sh
./others.sh
/home/oracle
Sorry, Command execution failed !
hB
hB
- Best AI tools for Software Engineers - November 4, 2024
- Installing Jupyter: Get up and running on your computer - November 2, 2024
- An Introduction of SymOps by SymOps.com - October 30, 2024