Lookup variable value from string in shell script

后端 未结 2 1338
南方客
南方客 2021-01-21 08:20

In bash, if I have:

y=10
x=\'y\'
echo $x # prints \'y\'

Now I want to get $y via $x:

echo ${$x} # error: \"bad sub         


        
2条回答
  •  野的像风
    2021-01-21 08:54

    Use eval for indirect references while escaping the outer dollar sign

    eval echo "\${$x}"
    

    To assign to a variable

    eval "z=\${$x}"
    echo "$z"
    # 10
    

提交回复
热议问题