Operating System - HP-UX
1827808 Members
2615 Online
109969 Solutions
New Discussion

Re: How does one set a variable variable?

 
SOLVED
Go to solution
Seth Parker
Trusted Contributor

How does one set a variable variable?

Is there a way to have a value of a shell variable be dependent on that of another?

For example:

MAIN=Sad
DEP=$MAIN" times"

echo $DEP would return "Sad times"

But what I'm looking for is if I were to next enter:

MAIN=Happy
echo $DEP would now return "Happy times"

I realize the above code won't give me what I want, but can something to this effect be accomplished? Any ideas?

Thanks,
Seth
10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: How does one set a variable variable?

The above is possible, yes.

In a script, something like this:

#!/usr/bin/sh

MAIN="Sad"
OTHER=" times"

DEP=${MAIN}${OTHER}

echo ${DEP}

MAIN="Happy"

DEP=${MAIN}${OTHER}

echo ${DEP}
Anil C. Sedha
Trusted Contributor

Re: How does one set a variable variable?

Seth,

It is very simple to do this.

You just have to specify the parameter every time you want a value out of it. Only care you have to take is that you don't re-specify the variable value till you have got the result of the first one.

The earlier post explains it nicely.

-Anil
If you need to learn, now is the best opportunity
Rajeev  Shukla
Honored Contributor

Re: How does one set a variable variable?

Ya as said its very simple. Just assign both values to variables and play with them.
like
MAIN=Sad
TIM=times
then
echo $MAIN $TIM (will give Sad times)
and then
MAIN=happy

and echo $MAIN $TIM
would return happy times

Just give a try

Cheers
Rajeev
Seth Parker
Trusted Contributor

Re: How does one set a variable variable?

I guess I should have expanded my original post a little. What I'm looking for is the exact behavior I described. When MAIN changes, so should the value of DEP. I don't want to have to reassign any variables but MAIN.

Does this make sense?

Thanks for the replies so far!

Seth
Patrick Wallek
Honored Contributor

Re: How does one set a variable variable?

I'm not sure you can have the type of behavior you require.

If you can, I don't know how to do it.
MANOJ SRIVASTAVA
Honored Contributor

Re: How does one set a variable variable?

Hi Seth

Since you are treating DEP as a vraible too you would need to reset the same in case you need to change $MAIN , the other was would be to use a shell scirpt to attaint eh same which can have if with 2 conditons.In the shell it wont be possible as the youy are dealing with two variables , and to set that you would need 2 statements not one.


Manoj Srivastava
Martin Robinson
Frequent Advisor
Solution

Re: How does one set a variable variable?

This can be done using the eval command.

Set DEP to a string that includes the name of the variable.
$ DEP="\$MAIN times"
This effectively defers evaluation of $MAIN.
$ echo $DEP
$MAIN times
MAIN=Sad
$ eval echo $DEP
Sad times
$ MAIN=Happy
$ eval echo $DEP
Happy times

Hope this helps.
Carlos Fernandez Riera
Honored Contributor

Re: How does one set a variable variable?

10 points for Martin:

Another example using awk:

export BOX1=yellow
export BOX2=lightgreen
export BOX3="#0080FF"

awk '{ color=ENVIRON[$1]
print "" $1 ""} ' <BOX1
BOX2
BOX3
END
unsupported
Seth Parker
Trusted Contributor

Re: How does one set a variable variable?

Nice, Martin! I'm going to see how I can fit that in to what I'm doing.

Thanks to you as well, Carlos. Nice example, but not quite what I was looking for.

And thanks to everyone else that took the time to reply.

Regards,
Seth
Mike Stroyan
Honored Contributor

Re: How does one set a variable variable?

There is a sneaky way to get that effect without the eval syntax-
trap 'DEP=$MAIN" times"' DEBUG

That will execute the assignment after every shell command. It is obviously high overhead. And there is only one debug hook, so it is difficult to use independently for multiple associations. You could use some kind of array of commands to aid with updates-
% trap 'for e in "${todo[@]}";do eval "$e";done;' DEBUG
% MAIN=sad
% echo MAIN=$MAIN DEP=$DEP
MAIN=sad DEP=
% todo[0]='DEP="$MAIN times"'
% echo MAIN=$MAIN DEP=$DEP
MAIN=sad DEP=sad times
% MAIN=happy
% echo MAIN=$MAIN DEP=$DEP
MAIN=happy DEP=happy times