Dynamic variable names in Bash

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

    This should work:

    function grep_search() {
        declare magic_variable_$1="$(ls | tail -1)"
        echo "$(tmpvar=magic_variable_$1 && echo ${!tmpvar})"
    }
    grep_search var  # calling grep_search with argument "var"
    

提交回复
热议问题