How to dereference a variable in CSH

2019-07-15 10:09发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-07-15 10:28

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
查看更多
登录 后发表回答