开发者

Bash parameter reference

开发者 https://www.devze.com 2023-03-16 16:52 出处:网络
I want to refer to a parameter in bash by using a number generated by a sum. How do I do this? More specifically:

I want to refer to a parameter in bash by using a number generated by a sum. How do I do this?

More specifically:

    #!/bin/bash

    $sum=${$1}
    echo $sum

When I execute the script ./script.bash A B C D, if $sum = 3, then I want to开发者_Python百科 return C. How do I do this, does anyone know?


You can use the indirect expansion syntax:

${!parameter}

as described here.

(Also note that variable assignments should not have a $ on the left-hand side!)

Example:

$ cat script.bash 
#!/bin/bash

sum=3
arg=${!sum}
echo $arg
$ ./script.bash A B C D
C
$
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号