Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何使用eval强制更新变量_Shell_Unix_Eval - Fatal编程技术网

Shell 如何使用eval强制更新变量

Shell 如何使用eval强制更新变量,shell,unix,eval,Shell,Unix,Eval,我正在阅读bash高级脚本指南(如果内存对我来说合适的话),其中提到了可以使用eval强制变量更新的内容 所以我试了一下: randomPath="/path/$var/here/" # var is not defined at this point echo $randomPath /path//here/ var="is" # initially defining var eval $randomPath zsh: no such file or directory: /path//he

我正在阅读bash高级脚本指南(如果内存对我来说合适的话),其中提到了可以使用
eval
强制变量更新的内容

所以我试了一下:

randomPath="/path/$var/here/"  # var is not defined at this point
echo $randomPath
/path//here/
var="is" # initially defining var
eval $randomPath
zsh: no such file or directory: /path//here/
我不理解错误消息,我想知道我是否正确使用了eval

我期望的结果是:

eval $randomPath
echo $randomPath
/path/is/here

问题是,
randomPath=“/path/$var/here/”
中已经替换了
$var
,并且因为它是空的,
randomPath
被设置为
/path//here
。您希望使用单引号来防止早期替换:

randomPath='/path/$var/here/'
第二个问题是
evalx
作为命令运行
x
。您要做的是将新计算的变量作为字符串返回:

eval echo $randomPath
您可以按照通常的方式将其存储在变量中:

randomPath=`eval echo $randomPath`

问题是,
randomPath=“/path/$var/here/”
中已经替换了
$var
,并且因为它是空的,
randomPath
被设置为
/path//here
。您希望使用单引号来防止早期替换:

randomPath='/path/$var/here/'
第二个问题是
evalx
作为命令运行
x
。您要做的是将新计算的变量作为字符串返回:

eval echo $randomPath
您可以按照通常的方式将其存储在变量中:

randomPath=`eval echo $randomPath`