Lookup variable value from string in shell script

后端 未结 2 1339
南方客
南方客 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:51

    See Parameter Expansion in bash manual:

    echo ${!x}
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题