Dynamic variable names in Bash

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

    As per BashFAQ/006, you can use read with here string syntax for assigning indirect variables:

    function grep_search() {
      read "$1" <<<$(ls | tail -1);
    }
    

    Usage:

    $ grep_search open_box
    $ echo $open_box
    stack-overflow.txt
    

提交回复
热议问题