Simple script

I am trying to print my script arguments, but i am stuck at the arrow pointed lines..please help

#!/bin/bash
echo "Number of arguments $#"
count=1
while [ $count -le $# ]
do
echo ${$count} <========================
count = $(expr $count +1) <==================
done

Hi,

Try this...

#!/bin/bash
echo "Number of arguments $#"
count=1
while [ $count -le $# ]
do
echo $count
((count=count+1))
done

Hello

FlyingSquirrel's answer works great.

I thought 3 points might be useful:

1)
Going back to your orig attempt, your line 7:

count = $(expr $count +1)

would work if you used 'backticks' like this:

count = `expr $count + 1`

2)
Also - I am not sure why you have a double dereference in line 6:

echo ${$count}

You do not need the extra 'dollar sign'

3)

Maybe more of a stylistic issue... For the builtin variables (ie the positional variable $#) I like to throw that into my own variable - as it makes the script more legible and I dont have to worry about unintended changes.

yrs

Michael

Actually I am trying to print all the arguments to the script. $count would print the count variable, but i want to print values of $1, $2 etc

Aha - shift is your friend, I believe.

Also you want to use either "$*" (and slice it up) or "$@" - not "$#".

Check this out from the "Advanced Bash Scripting guide".

Go to:

Internal Variables

and do a search for "shift".

Notice how you can iterate using the $# variable (as you do) and use shift to pop off the $1 value off of $@ each time. (look at the ABS guide - iti s clearer than my explanation.

Also read up on 2 issues - spacing and the use of quotes with variables.

Enjoy...

Michael