问题
I have a variable $var that has values as another variable $env . $env has some value . how can I use $var to print the value of $env.
> set xyz = 'abc' set env = 'xyz' set v = '$' set var = "$v""$env"
>
> echo $var o/p: $xyz
now I want to print the value the of $ xyz using $var
回答1:
Use eval
, like this:
set xyz='abc'
set var='xyz'
eval echo \$$var
This is more commonly known as an indirect reference.
Here is an example script:
set var = 'xyz'
set xyz = 'abc'
if ( $?var ) then
echo '$var is set to '"$var"
endif
if ( $?xyz ) then
echo '$xyz is set to '"$xyz"
endif
if ( `eval echo \$\?$var` ) then
echo '$$var is set to '`eval echo \$$var`
endif
Output:
> csh test
$var is set to xyz
$xyz is set to abc
$$var is set to abc
来源:https://stackoverflow.com/questions/19997629/how-to-dereference-a-variable-in-csh