How to dereference a variable in CSH

一曲冷凌霜 提交于 2019-12-10 23:03:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!