Operating System - HP-UX
1834150 Members
2063 Online
110064 Solutions
New Discussion

Setting value for multiple variables

 
SOLVED
Go to solution
Sylvain CROUET
Respected Contributor

Setting value for multiple variables

Hi!

I want to initialize several variables with the same value. I try to use a for loop

for VAR in A B C ; do
$VAR=xxx
done

but I get error messages :
./myscript[21]: A=: not found
5 REPLIES 5
Mark Grant
Honored Contributor

Re: Setting value for multiple variables

That's because you are thinking "perl"

It should be "VAR=xxx" not "$VAR=xxx"
Never preceed any demonstration with anything more predictive than "watch this"
Nicolas Dumeige
Esteemed Contributor
Solution

Re: Setting value for multiple variables

That won't work either.

This work but it's ugly !
#!/bin/ksh
for VAR in A B C ; do
eval $VAR=xxx
done
echo $A $B $C

There must be a better way.

Cheers,

Nicolas
All different, all Unix
Marvin Strong
Honored Contributor

Re: Setting value for multiple variables


for V in A B C
do
export $V=xxx
done

is another way. gotta be a cleaner way.



Mark Grant
Honored Contributor

Re: Setting value for multiple variables

Thanks Nicolas,

Didn't read that one correctly :)
Never preceed any demonstration with anything more predictive than "watch this"
Nicolas Dumeige
Esteemed Contributor

Re: Setting value for multiple variables

You could use an array, if you use numerous variable.

Also, I was looking at set and typeset builtin command but I can't make anthing of it.


All different, all Unix