array in sh

Hello!

Can I use array in sh? or only in ksh and bash?

Thanks!

Yes, this is a basic sh example.

#!/bin/sh
set -- 1 2 3 4 5 6 7 8 9 10 11 12 13
while [ $# -gt 0 ]
do
        echo $1
        shift;
done

For more example please search the forum or post your questions on UNIX for Dummies Questions & Answers

Ok, thanks!

but if i wanna passa an array as a parameter like: sh script array, how i do this?
and how use this array in my script?

Pass array element as parameter.

# cat test.sh
#!/bin/sh
count=1
while [ $count -le $# ]
do
        echo $count;
        count=`expr $count + 1`;
done
# ./test.sh 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3
4
5
6
7
8
9
10
11
12

Ok, but if i do in my command line:
$ array[0]=zero
$ array[1]=one
$ array[2]=two

and i pass the array like this:
sh script ${array
[*]}

i don't want that array[0] is my $1, array[1] is my $2...

does exists other form for do this?

i wanna use in my sh code my array, like:

if i wanna print "one" i wanna do something like this
echo $array[1]

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

*****************************************************

ksh and bash have different kinds of arrays. First you should make clear which shell you want to use please.

Ok, sorry!
I modify my post

Ok, but if i do in my command line:

#array[0]=zero
#array[1]=one
 #array[2]=two

and i pass the array like this:
sh script ${array
[*]}

i don't want that array[0] is my $1, array[1] is my $2...

does exists other form for do this?

i wanna use in my sh code my array, like:

if i wanna print "one" i wanna do something like this

 echo $array[1] 	 

shell arrays are 0-based.