Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何访问作为参数传递给 bash 脚本的变量的内容?
示例:sh myscript.sh PATH。现在我想修改里面的字符串PATH,但是一直访问失败!
sh myscript.sh PATH
PATH
有任何想法吗?
编辑:我需要能够访问任何变量,而不仅仅是PATH
像这样的东西应该适合你:
#!/bin/bash ARG=$1 VAL=${!ARG} echo $VAL
如果你像这样执行你的脚本:
./myscript.sh PATH
$PATH它在 STDOUT 上打印值。
$PATH
#!/bin/bash FOO=$(eval echo \$$1) echo args: $FOO
给出:
$ foo.sh HOME args: /home/bos