将变量传递给调用脚本的脚本
我有一个bash脚本,必须调用另一个bash脚本,该脚本因用户而异,以获取一些配置信息,如下所示:
somescript:
#!/bin/bash ${HOME}/.ssconfig echo "Hello, ${VARIABLE}!"
〜/ .ssconfig
#!/bin/bash VARIABLE=world
哪个,而不是给我Hello, world!
正如所料,给我Hello, !
如何将变量从脚本传递到调用它的变量?
执行脚本不会影响调用它的shell,因为脚本在自己的shell中运行。 要影响调用shell,您需要获取脚本:
$ help source source: source filename [arguments] Execute commands from a file in the current shell. Read and execute commands from FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.
因此,应该将执行配置文件的行更改为源它:
#!/bin/bash source ${HOME}/.ssconfig echo "Hello, ${VARIABLE}!"
请注意, source
是一个shell命令,因此它的帮助不在man source
,而是在help source
。