Dynamic variable names in Bash

后端 未结 14 2044
清酒与你
清酒与你 2020-11-21 05:38

I am confused about a bash script.

I have the following code:

function grep_search() {
    magic_way_to_define_magic_variable_$1=`ls | tail -1`
    e         


        
14条回答
  •  一个人的身影
    2020-11-21 06:22

    This will work too

    my_country_code="green"
    x="country"
    
    eval z='$'my_"$x"_code
    echo $z                 ## o/p: green
    

    In your case

    eval final_val='$'magic_way_to_define_magic_variable_"$1"
    echo $final_val
    

提交回复
热议问题