Dynamic variable names in Bash

后端 未结 14 2020
清酒与你
清酒与你 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:18

    For indexed arrays, you can reference them like so:

    foo=(a b c)
    bar=(d e f)
    
    for arr_var in 'foo' 'bar'; do
        declare -a 'arr=("${'"$arr_var"'[@]}")'
        # do something with $arr
        echo "\$$arr_var contains:"
        for char in "${arr[@]}"; do
            echo "$char"
        done
    done
    

    Associative arrays can be referenced similarly but need the -A switch on declare instead of -a.

提交回复
热议问题