{"id":291,"date":"2010-12-10T15:18:51","date_gmt":"2010-12-10T15:18:51","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/2010\/12\/10\/shell-script-parameters\/"},"modified":"2017-12-27T18:18:59","modified_gmt":"2017-12-27T18:18:59","slug":"shell-script-parameters","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/shell-script-parameters\/","title":{"rendered":"Understand Shell Script Parameters &#8211; Reference"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4336\" src=\"http:\/\/www.scmgalaxy.com\/tutorials\/wp-content\/uploads\/2010\/12\/shell-script-parameters.png\" alt=\"shell-script-parameters\" width=\"600\" height=\"400\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/12\/shell-script-parameters.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2010\/12\/shell-script-parameters-300x200.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p><span style=\"font-size: 9pt; line-height: 115%;\">A parameter is an entity that stores values. It can be a name, a number or some special characters. <\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 9pt; line-height: 115%;\">Bash shell provides two kind of parameters.<\/span><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 9pt; line-height: 115%;\">Positional Parameter and Special Parameter<\/span><\/p>\n<p class=\"MsoNormal\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt; line-height: 115%;\">Bash Positional Parameter \u2013 $0, $1, $2 ..<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\"><span style=\"font-size: 9pt; line-height: 115%;\">Positional parameters\u00a0are 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}.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The <strong>variable $0 is the basename<\/strong> of the program as it was called.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The following example gets two arguments and provides arithmetic operations result between those two integers.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">First, create the arithmetic.sh shell script as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat arithmetic.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"><br \/>\n<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let add=$1+$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let sub=$1-$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let mul=$1*$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let div=$1\/$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;Addition=$add\\nSubtraction=$sub\\nMultiplication=$mul\\nDivision=$div\\n&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">Next, execute the arithmetic.sh with proper parameters as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/arithmetic.sh 12 10<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$1=12<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$2=10<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Addition=22<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Subtraction=2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Multiplication=120<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Division=1<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">In the above output $1 has the value 12, and $2 has 10.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">Shell built-in \u2018let\u2019 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.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt;\">Set \/ Unset Bash Positional Parameters<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The built in set command is used to set and unset the positional parameter.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">First, create the positional.sh shell script as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat positional.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># From command line<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;Basename=$0&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$3=$3&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># From Set builtin<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">set First Second Third<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$3=$3&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># Store positional parameters with -(hyphen)<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">set &#8211; -f -s -t<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$3=$3&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># Unset positional parameter<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">set &#8212;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$3=$3&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The above script prints the command line arguments first, then set command sets the positional parameter explicitly. Set with the \u2013 refers end of options, all following arguments are positional parameter even they can begin with \u2018-\u2019. Set with \u2018\u2013\u2019 without any other arguments unset all the positional parameters.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">Next, execute the positional.sh as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/positional.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Basename=positional.sh<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$1=12<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$2=10<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$3=<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$1=First<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$2=Second<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$3=Third<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$1=-f<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$2=-s<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$3=-t<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$1=<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$2=<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$3=<br \/>\n<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt;\">Use Bash $* and $@ to Expand Positional Parameters<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">This example shows the value available in <strong>$*<\/strong> and <strong>$@<\/strong>.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">First, create the expan.sh as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat expan.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">export IFS=&#8217;-&#8216;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">cnt=1<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># Printing the data available in $*<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo &#8220;Values of \\&#8221;\\$*\\&#8221;:&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">for arg in &#8220;$*&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">do<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> echo &#8220;Arg #$cnt= $arg&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> let &#8220;cnt+=1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">done<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">cnt=1<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"># Printing the data available in $@<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo &#8220;Values of \\&#8221;\\$@\\&#8221;:&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">for arg in &#8220;$@&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">do<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> echo &#8220;Arg #$cnt= $arg&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> let &#8220;cnt+=1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">done<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">Next, execute the expan.sh as shown below to see how $* and $@ works.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/expan.sh &#8220;This is&#8221; 2 3<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Values of &#8220;$*&#8221;:<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Arg #1= This is-2-3<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Values of &#8220;$@&#8221;:<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Arg #1= This is<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Arg #2= 2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Arg #3= 3<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><span style=\"font-size: 9pt;\">The above script exported the value of IFS (Internal Field Separator) with the \u2018-\u2019.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><span style=\"font-size: 9pt;\">There are three parameter passed to the script expan.sh $1=\u201dThis is\u201d,$2=\u201d2\u2033 and $3=\u201d3\u2033.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><span style=\"font-size: 9pt;\">When printing the each value of special parameter \u201c$*\u201d, it gives only one value which is the whole positional parameter delimited by IFS.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><span style=\"font-size: 9pt;\">Whereas \u201c$@\u201d gives you each parameter as a separate word.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt;\">Use $# to Count Positional Parameters<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><span style=\"font-size: 9pt;\">$#<\/span><\/strong><span style=\"font-size: 9pt;\"> is the special parameter in bash which gives you the number of positional parameter in decimal.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">First, create the arithmetic.sh as shown below.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat arithmetic.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">if [ $# -lt 2 ]<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">then<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> echo &#8220;Usage: $0 arg1 arg2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> exit<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">fi<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$1=$1&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;\\$2=$2&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let add=$1+$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let sub=$1-$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let mul=$1*$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">let div=$1\/$2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;Addition=$add\\nSubtraction=$sub\\nMultiplication=$mul\\nDivision=$div\\n&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">If the number of positional parameters is less than 2, it will throw the usage information as shown below,<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/arithemetic.sh 10<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Usage: .\/arithemetic.sh arg1 arg2<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt;\">Process related Parameters \u2013 $$ and $!<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The special parameter <strong>$$<\/strong> will give the process ID of the shell. <strong>$!<\/strong> gives you the process id of the most recently executed background process.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">The following script prints the process id of the shell and last execute background process ID.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat proc.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;Process ID=$$&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">sleep 1000 &amp;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;Background Process ID=$!&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">Now, execute the above script, and check the process id which its printing.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/proc.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Process ID=9502<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Background Process ID=9503<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$ ps<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> PID TTY TIME CMD<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> 5970 pts\/1 00:00:00 bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> 9503 pts\/1 00:00:00 sleep<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> 9504 pts\/1 00:00:00 ps<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">$<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><strong><em><span style=\"text-decoration: underline;\"><span style=\"font-size: 9pt;\">Other Bash Special Parameters \u2013 $?, $-, $_<\/span><\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><strong><span style=\"font-size: 9pt;\">$?<\/span><\/strong><span style=\"font-size: 9pt;\"> Gives the exit status of the most recently executed command.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><strong><span style=\"font-size: 9pt;\">$-<\/span><\/strong><span style=\"font-size: 9pt;\"> Options set using set builtin command<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-left: 0.25in; text-indent: -0.25in; line-height: normal;\"><span style=\"font-size: 10pt; font-family: Symbol;\">\u00b7 <\/span><strong><span style=\"font-size: 9pt;\">$_<\/span><\/strong><span style=\"font-size: 9pt;\"> Gives the last argument to the previous command. At the shell startup, it gives the absolute filename of the shell script being executed.<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ cat others.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#!\/bin\/bash<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;$_&#8221;; ## Absolute name of the file which is being executed<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">\/usr\/local\/bin\/dbhome # execute the command.<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">#check the exit status of dbhome<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">if [ &#8220;$?&#8221; -ne &#8220;0&#8221; ]; then<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\"> echo &#8220;Sorry, Command execution failed !&#8221;<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">fi<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e &#8220;$-&#8220;; #Set options &#8211; hB<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">echo -e $_ # Last argument of the previous command.<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;\"><span style=\"font-size: 9pt;\">In the above script, the last echo statement \u201cecho -e $_\u201d ($ underscore) also prints hB which is the value of last argument of the previous command. So $_ will give the value after expansion<\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><strong><em><span style=\"font-size: 9pt;\">$ .\/others.sh<\/span><\/em><\/strong><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">.\/others.sh<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">\/home\/oracle<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">Sorry, Command execution failed !<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">hB<\/span><\/em><\/p>\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\"><em><span style=\"font-size: 9pt;\">hB<\/span><\/em><\/p>\n<p class=\"MsoNormal\">\n<p class=\"MsoNormal\" style=\"margin-bottom: 0.0001pt; line-height: normal;\">\n<p class=\"MsoNormal\">\n","protected":false},"excerpt":{"rendered":"<p>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 \u2013 $0, $1, $2 .. Positional parameters\u00a0are the arguments given to your scripts when it is invoked. It could be from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4336,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[454],"tags":[137,4034,4033,4036,4035,4031,278,138,4038,3046,4037,4032,4040,4041,4043,4039,4042],"class_list":["post-291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-shell-script","tag-bash","tag-bash-positional-parameter","tag-bash-shell","tag-bash-shell-script-parameters","tag-bash-special-parameters","tag-parameters","tag-script","tag-shell","tag-shell-parameter-values","tag-shell-script","tag-shell-script-arguments-example","tag-shell-script-parameters","tag-shell-script-parameters-guide","tag-shell-script-parameters-instructions","tag-shell-script-parameters-reference","tag-shell-script-pass-parameter","tag-understand-shell-script-parameters"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":4337,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/291\/revisions\/4337"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/4336"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}