For loop; how to construct a array with variables

Hi everybody!!

Here is the thing; I have a trouble in this simple situation, I'm trying to write an array with all the arguments of a command. I mean, if I have:

./mycommand.sh aa bb cc dd

I need to take an array like this:
myarray=(aa bb cc dd)

So I use a simple for loop like this:

for i in `seq 1 $#`;
do
myarray=THISISMYQUESTION
done

I don't know how to construct the expression to take the i variable with its growing values (1, 2, 3, ... n) and become it in the variable who represents the argument ($1, $2, $3, ... $n) and save the arguments values into the array (aa, bb, cc, ... whatever).
If I do

myarray=$i

of course I'll have myarray=(1 2 3 4), not the needed array myarray=(aa bb cc dd)

Can someone help me?

Thanks

Andres

Hey!!!

For your question,

for i in `seq 1 $#`;
do
myarray=${i}
done

Here $i represents the arguments which is taken one by one and assigned to myarray in an index of argument possition.

You don't need to loop the arguments to create array, check this out,

#! /usr/bin/env bash

declare -a myarr

myarr=($@)
echo "${myarr[0]}"

For your information,

Just assume, if you assign the array like below,

myarray[0]='aa'
myarray[1]='bb'

You output array should be myarray=('aa' 'bb') and these 0,1 represent the indexes which represents the position of array element.

Cheers,
Ranga:)

Hi Rangarasan. Thanks a lot.

myarr=($@)

This works great!! Thanks!!

But This doesn't work as I need, see:

for i in `seq 1 $#`;
do
   echo ${i}
done

This is the output

$ ./wrapper.sh aa bb cc
1
2
3

I still can't use the argument's variables like $1, $2, $3, ...$n into the for loop.

Regards,

Andr�s

try this:

for i in {1..$#}
do
   eval echo $`echo $i`
done

Thanks a lot stuckinboredom !!
The code

eval echo $`echo $i`

works fine!!

Regards,