I have a line of code
eval echo \$$var
which prints a string. How can I store this string into a variable?
I have a line of code
eval echo \$$var
which prints a string. How can I store this string into a variable?
像这样:
eval c=\$$var
更好、更安全的方法是使用间接:
c=${!var}
newVariable=$(eval echo \$$var)
你可以试试这个:它在 bash 上对我有用。
c=$(eval echo \${$var})
示例:如果HOME
是包含您的主目录的环境变量/export/users/john
,则将变量嵌入到另一个变量中,然后解压缩该组合将像这样工作:
var=HOME
c=$(eval echo \${$var})
echo $c
/export/users/john
虽然这可能看起来不必要地令人费解,但它在某些情况下会很有用。
这可以通过使用反引号语法来实现:` `
c=`$var`