1827893 Members
1722 Online
109969 Solutions
New Discussion

Variable of variable

 
SOLVED
Go to solution
Sylvain CROUET
Respected Contributor

Variable of variable

Hi!

I want to use a for loop within a shell script to display the value of several variables, but I can't manage it. It shoul be something like that:

for VAR in VAR1 VAR2 VAR3 ; do
echo
done

13 REPLIES 13
Sanjay Kumar Suri
Honored Contributor

Re: Variable of variable

Is this what you need?

a=5
b=5
for var in a b
do
echo $var
done

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Denver Osborn
Honored Contributor

Re: Variable of variable

Are the variables numeric and are you trying to get thier total? or create a loop that takes VAR and add it's value to VAR1 then VAR2 then VAR3??

Post additional info pls. Can you give us an example of what VAR, VAR1, VAR2 and VAR3 could be and your anticipated result.

-denver
Sylvain CROUET
Respected Contributor

Re: Variable of variable

No. I do not need the script to display the names of the variables (a and b), but their values (5 and 5).
KapilRaj
Honored Contributor

Re: Variable of variable

As I read ,

you have the following setup in place

export KAPS=KAPILRAJ
export KK=KAPS

using KK , you want to get KAPILRAJ i.e. something like echo ${$KK} .. ?

you may try the following

echo $KK|awk '{print "echo","$"$0}' |ksh

Please make sure that all the variables are exported i.e. export VAR1 VAR2

Regds,

Kaps
Nothing is impossible
Sylvain CROUET
Respected Contributor

Re: Variable of variable

I just want to display the values of my variables, which are "strings".
KapilRaj
Honored Contributor

Re: Variable of variable

Can u explain in detail please .......

An echo $VAR will show the value only !!

Regds,

Kaps
Nothing is impossible
Sanjay Kumar Suri
Honored Contributor

Re: Variable of variable

Check the following:

a=abc
b=def
for var in $a $b
do
echo $var
done


sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Denver Osborn
Honored Contributor

Re: Variable of variable

VAR1=is
VAR2=this
VAR3=it

for VAR in $VAR1 $VAR2 $VAR3; do
echo $VAR
done


or??

VAR1=is
VAR2=this
VAR3=it

VAR="$VAR1 $VAR2 $VAR3"
echo $VAR

Jeroen Peereboom
Honored Contributor

Re: Variable of variable

What about the following:

A="this is 1"
B="This is 2"

for i in "$A" "$B"
do
echo $i
done

JP
Jeroen Peereboom
Honored Contributor

Re: Variable of variable

Many replies at the same time.
Note that quotes are essential if your variable values contain multiple words.

JP
Sylvain CROUET
Respected Contributor

Re: Variable of variable

The solution using $VAR1, $VAR2... directly within the "in" statement is quite interesting, but what if I want to print:
=
curt larson_1
Honored Contributor
Solution

Re: Variable of variable

for var in var1 var2
do
eval print "$var = \$$var"
done
Sylvain CROUET
Respected Contributor

Re: Variable of variable

Thanks Curt! That was the solution I was looking for.