Unix Shell Arguments

For the past few weeks, I have been working on Unix Shell, to accomplish variety of tasks such as parsing files, transferring data, updating databases etc. Today is the day, i primarily worked on Arguments. Arguments are inputs to the program, and are also referred as parameters interchangeably.

Lets see how to use the arguments in the korn shell script, beginning with unix shell annotation for arguments.

$1, $2, $3 … $n First, Second, Third… and nth Argument
$# (hash)    Count of Arguments
$* (Asterisk)    Arguments concatenated by space
$@ (at the rate)    Array of Arguments

Here is an sample shell script “accessArguments.ksh”.

$ cat accessArguments.ksh
# accessArguments.ksh
echo Zeroth Argument: $0
echo First Argument: $1
echo Second Argument: $2
echo Total No. Of Arguments: $#
echo Arguments: $*
for var in $@
do
echo Argument $var
done

Now lets see the output of the above script, with inputs as 10 and 15

$ accessArguments.ksh 10 15
Zeroth Argument : accessArguments.ksh
First Argument: 10
Second Argument: 15
Total No. Of Arguments: 2
Arguments: 10 15
Argument 10
Argument 15
$

Hope this example envisions you with the knowledge of using Arguments in Unix Shell.

Reblog this post [with Zemanta]
  • Digg
  • del.icio.us
  • Facebook
  • MySpace
  • StumbleUpon
  • Technorati
  • Slashdot
  • Yahoo! Bookmarks
  • Yahoo! Buzz

No related posts.

You can leave a response, or trackback from your own site.

Leave a Reply

Subscribe to RSS Feed Follow me on Twitter!