Indirect parameter substitution in shell script

后端 未结 4 638
庸人自扰
庸人自扰 2020-12-21 17:16

I\'m having a problem with a shell script (POSIX shell under HP-UX, FWIW). I have a function called print_arg into which I\'m passing the name of a parameter as $1. Given

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 17:40

    In bash (but not in other sh implementations), indirection is done by: ${!arg}

    Input

    foo=bar
    bar=baz
    
    echo $foo
    echo ${!foo}
    

    Output

    bar
    baz
    

提交回复
热议问题