# Simple script

**URL:** <https://community.unix.com/t/simple-script/225999>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [December 29, 2008, 10:41pm UTC](https://community.unix.com/t/simple-script/225999 "2008-12-29T22:41:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chvs2000](https://community.unix.com/letter_avatar/chvs2000/32/6_449c533f586939b6e5f246632a46421f.png) [@chvs2000](https://community.unix.com/u/chvs2000)\
**Post date:** [December 29, 2008, 10:41pm UTC](https://community.unix.com/t/simple-script/225999/1 "2008-12-29T22:41:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FlyingSquirrel](https://community.unix.com/user_avatar/community.unix.com/flyingsquirrel/32/936_2.png) [@FlyingSquirrel](https://community.unix.com/u/FlyingSquirrel)\
**Post date:** [December 29, 2008, 10:59pm UTC](https://community.unix.com/t/simple-script/225999/2 "2008-12-29T22:59:18Z")

</div>

Hi,

Try this...

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

---

<div class="post-metadata">

**Author:** ![mbubb](https://community.unix.com/user_avatar/community.unix.com/mbubb/32/4666_2.png) [@mbubb](https://community.unix.com/u/mbubb)\
**Post date:** [December 29, 2008, 11:45pm UTC](https://community.unix.com/t/simple-script/225999/3 "2008-12-29T23:45:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chvs2000](https://community.unix.com/letter_avatar/chvs2000/32/6_449c533f586939b6e5f246632a46421f.png) [@chvs2000](https://community.unix.com/u/chvs2000)\
**Post date:** [December 30, 2008, 9:25am UTC](https://community.unix.com/t/simple-script/225999/4 "2008-12-30T09:25:40Z")

</div>

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

> [@flyingsquirrel](#):
>
> Hi,
> 
> Try this...
> 
> #!/bin/bash  
> echo "Number of arguments $#"  
> count=1  
> while [$count -le $#]  
> do  
> echo $count  
> ((count=count+1))  
> done

---

<div class="post-metadata">

**Author:** ![mbubb](https://community.unix.com/user_avatar/community.unix.com/mbubb/32/4666_2.png) [@mbubb](https://community.unix.com/u/mbubb)\
**Post date:** [December 30, 2008, 11:39am UTC](https://community.unix.com/t/simple-script/225999/5 "2008-12-30T11:39:02Z")

</div>

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](http://tldp.org/LDP/abs/html/internalvariables.html)

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
