Trying to use 'compound variable' in a script

Hi there - am newish to shell scripting and would appreciate some advice on this...

Am trying to use what I have seen called 'compound variables' in other langs but with no success in my shell script. This is the kind of thing I'm trying to do:

base_val=123
stop=3
x=1
while [ $x -le $stop ]
do
array_var[$x] = `expr $base_val + $x`
echo $x $array_var[$x]
x=`expr $x + 1`
done

The value of x is being incremented, but it is not being substituted as an index in the array variable called array_var. Here's the output:

./t2.ksh[8]: array_var[1]: not found
1 [1]
./t2.ksh[8]: array_var[2]: not found
2 [2]
./t2.ksh[8]: array_var[3]: not found
3 [3]

Is this completely the wrong approach, or is it an issue with the syntax?. What's the best way to achieve this?
Thanks in advance,

Neemic........:

You need to remove the spaces either side of the equals sign during the array assignment. Also, you have to use curly brackets {} when referencing the array elements.

base_val=123
stop=3
x=1
while [ $x -le $stop ]
do
  array_var[$x]=`expr $base_val + $x`
  echo $x ${array_var[$x]}
  x=`expr $x + 1`
done

I think you should leave the spaces out in this line before and after the = .

Ygor, Tine,

Thanks very much for the help - and thanks for not laughing!

:slight_smile:

Cheers,
neemic