How do you read arguments in a shell program - $1, $2 ?

Showing Answers 1 - 15 of 15 Answers

Muthu

  • Jun 2nd, 2005
 

#!/bin/sh 
for i in $* 
do 
echo $i 
done 
 
On executig the above script with any number of command-line arguments it will display all the parametsrs.

joadavis

  • Jun 3rd, 2005
 

$1 would be the first command line argument, $2 the second, and so on 
$0 is the name of the script or function

abhay

  • Jul 5th, 2005
 

Shell script accepts parameters in following format... 
$1 : first 
$2 : second....so on upto  
$9 : 9th param 
whereas $0 : gives script/function name 
If your script has more than 9 params then accept in following way... 
${12} : 12th param 
${18} : 18th param

Tuku

  • Feb 21st, 2007
 

U can also use Shift for getting the behind 9th parameters.........

Mo

  • Apr 20th, 2012
 

Positional parameters are reads either by displaying there value or can be assigned to another variables.

echo $1 $2 $9 ${10} ${20}
shift operators can be used to move around the positional parameters.

example: test.sh 1 2 3 4 5

a=$1

while [ -z "$a" ]
do

echo $a
shift

done

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions