質問

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

役に立ちましたか?

解決

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top