Indirect parameter substitution in shell script

后端 未结 4 648
庸人自扰
庸人自扰 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

    You could use eval, though using direct indirection as suggested by SiegeX is probably nicer if you can use bash.

    #!/bin/sh
    
    foo=bar
    print_arg () {
        arg=$1
        eval argval=\"\$$arg\"
        echo "$argval"
    }
    print_arg foo
    

提交回复
热议问题