1830241 Members
9831 Online
109999 Solutions
New Discussion

Runtime Variable Value

 
uform
Frequent Advisor

Runtime Variable Value

Hi,

i have the following variables,

pri_running=0
var_running=pri_running

how can i set value of pri_running = 5
using the variable "var_running" ?

is it possible?
2 REPLIES 2
Hein van den Heuvel
Honored Contributor

Re: Runtime Variable Value

No that can not be done.

But why don't you try to explain the problem you are really trying to solve.

Also, in what context is that variable used?

I guess you intent a SHELL variable (which shell?)

But it could be C or AWK or whatever based on the limited description you provided.

In Perl things along those line can be done using REFERENCES.

Regards,
Hein.

Bob E Campbell
Honored Contributor

Re: Runtime Variable Value

Sure! That can be a handy trick at times. The secret is the "eval" built-in command:

$ var_running=pri_running
$ eval ${var_running}=5
$ echo $pri_running
5

The eval basically tells the shell to expand the variables before evaluating the expressions. To use a variable within an eval you would need to escape the $:

$ eval new_var=\$${var_running}
$ echo $new_var
5

I will warn you that self-modifying shell scripts can lead to madness (or at least hair loss).

Bob