Dynamic variable names in Bash

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

    I've been looking for better way of doing it recently. Associative array sounded like overkill for me. Look what I found:

    suffix=bzz
    declare prefix_$suffix=mystr
    

    ...and then...

    varname=prefix_$suffix
    echo ${!varname}
    

提交回复
热议问题