<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bash shell script parameters Archives - scmGalaxy</title>
	<atom:link href="https://www.scmgalaxy.com/tutorials/tag/bash-shell-script-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.scmgalaxy.com/tutorials/tag/bash-shell-script-parameters/</link>
	<description>A Platform for Software Engineers</description>
	<lastBuildDate>Sat, 27 Feb 2021 09:53:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>Understand Shell Script Parameters &#8211; Reference</title>
		<link>https://www.scmgalaxy.com/tutorials/shell-script-parameters/</link>
					<comments>https://www.scmgalaxy.com/tutorials/shell-script-parameters/#respond</comments>
		
		<dc:creator><![CDATA[scmgalaxy K]]></dc:creator>
		<pubDate>Fri, 10 Dec 2010 15:18:51 +0000</pubDate>
				<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Bash Positional Parameter]]></category>
		<category><![CDATA[Bash shell]]></category>
		<category><![CDATA[bash shell script parameters]]></category>
		<category><![CDATA[Bash Special Parameters]]></category>
		<category><![CDATA[Parameters]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[shell parameter values]]></category>
		<category><![CDATA[Shell script]]></category>
		<category><![CDATA[shell script arguments example]]></category>
		<category><![CDATA[Shell Script Parameters]]></category>
		<category><![CDATA[Shell Script Parameters Guide]]></category>
		<category><![CDATA[Shell Script Parameters Instructions]]></category>
		<category><![CDATA[Shell Script Parameters Reference]]></category>
		<category><![CDATA[shell script pass parameter]]></category>
		<category><![CDATA[Understand Shell Script Parameters]]></category>
		<guid isPermaLink="false">http://www.scmgalaxy.com/tutorials/2010/12/10/shell-script-parameters/</guid>

					<description><![CDATA[<p>A parameter is an entity that stores values. It can be a name, a number or some special characters. Bash [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/shell-script-parameters/">Understand Shell Script Parameters &#8211; Reference</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-4336" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2010/12/shell-script-parameters.png" alt="shell-script-parameters" width="600" height="400" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2010/12/shell-script-parameters.png 600w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2010/12/shell-script-parameters-300x200.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></p>
<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>
<p class="MsoNormal"><span style="font-size: 9pt; line-height: 115%;">Bash shell provides two kind of parameters.</span></p>
<p class="MsoNormal"><span style="font-size: 9pt; line-height: 115%;">Positional Parameter and Special Parameter</span></p>
<p class="MsoNormal"><strong><em><span style="text-decoration: underline;"><span style="font-size: 9pt; line-height: 115%;">Bash Positional Parameter – $0, $1, $2 ..</span></span></em></strong></p>
<p class="MsoNormal"><span style="font-size: 9pt; line-height: 115%;">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}.</span></p>
<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>
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">First, create the arithmetic.sh shell script as shown below.</span></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"><br />
</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let add=$1+$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let sub=$1-$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let mul=$1*$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let div=$1/$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$1=12</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$2=10</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Addition=22</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Subtraction=2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Multiplication=120</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Division=1</span></em></p>
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">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.</span></p>
<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>
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">First, create the positional.sh shell script as shown below.</span></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"># From command line</span></em></p>
<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>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"># From Set builtin</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">set First Second Third</span></em></p>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"># Unset positional parameter</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">set &#8212;</span></em></p>
<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>
<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>
<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>
<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 – 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.</span></p>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">Next, execute the positional.sh as shown below.</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><strong><em><span style="font-size: 9pt;">$ ./positional.sh</span></em></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Basename=positional.sh</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$1=12</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$2=10</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$3=</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$1=First</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$2=Second</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$3=Third</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$1=-f</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$2=-s</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$3=-t</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$1=</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$2=</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$3=<br />
</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">First, create the expan.sh as shown below.</span></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">export IFS=&#8217;-&#8216;</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">cnt=1</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">do</span></em></p>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">done</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">cnt=1</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">do</span></em></p>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">done</span></em></p>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Values of &#8220;$*&#8221;:</span></em></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Values of &#8220;$@&#8221;:</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Arg #1= This is</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Arg #2= 2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Arg #3= 3</span></em></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </span><span style="font-size: 9pt;">The above script exported the value of IFS (Internal Field Separator) with the ‘-’.</span></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </span><span style="font-size: 9pt;">There are three parameter passed to the script expan.sh $1=”This is”,$2=”2″ and $3=”3″.</span></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </span><span style="font-size: 9pt;">When printing the each value of special parameter “$*”, it gives only one value which is the whole positional parameter delimited by IFS.</span></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </span><span style="font-size: 9pt;">Whereas “$@” gives you each parameter as a separate word.</span></p>
<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>
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">First, create the arithmetic.sh as shown below.</span></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">if [ $# -lt 2 ]</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">then</span></em></p>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"> exit</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">fi</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let add=$1+$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let sub=$1-$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let mul=$1*$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">let div=$1/$2</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<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>
<p class="MsoNormal" style="line-height: normal;"><strong><em><span style="text-decoration: underline;"><span style="font-size: 9pt;">Process related Parameters – $$ and $!</span></span></em></strong></p>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">sleep 1000 &amp;</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><strong><em><span style="font-size: 9pt;">$ ./proc.sh</span></em></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Process ID=9502</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Background Process ID=9503</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$ ps</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;"> PID TTY TIME CMD</span></em></p>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">$</span></em></p>
<p class="MsoNormal" style="line-height: normal;"><strong><em><span style="text-decoration: underline;"><span style="font-size: 9pt;">Other Bash Special Parameters – $?, $-, $_</span></span></em></strong></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </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>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </span><strong><span style="font-size: 9pt;">$-</span></strong><span style="font-size: 9pt;"> Options set using set builtin command</span></p>
<p class="MsoNormal" style="margin-left: 0.25in; text-indent: -0.25in; line-height: normal;"><span style="font-size: 10pt; font-family: Symbol;">· </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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">#!/bin/bash</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<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>
<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>
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">fi</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<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>
<p class="MsoNormal" style="line-height: normal;"><span style="font-size: 9pt;">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</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><strong><em><span style="font-size: 9pt;">$ ./others.sh</span></em></strong></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">./others.sh</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">/home/oracle</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">Sorry, Command execution failed !</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">hB</span></em></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;"><em><span style="font-size: 9pt;">hB</span></em></p>
<p class="MsoNormal">
<p class="MsoNormal" style="margin-bottom: 0.0001pt; line-height: normal;">
<p class="MsoNormal">
<p>The post <a href="https://www.scmgalaxy.com/tutorials/shell-script-parameters/">Understand Shell Script Parameters &#8211; Reference</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.scmgalaxy.com/tutorials/shell-script-parameters/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
