OS: Ubuntu 18.04
Bash
我正在尝试让一个 shell 脚本将变量传递给另一个脚本并执行它。这是我尝试过的:
mainscript.sh
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")
subscript1.sh:
#!/bin/bash
# subscript1.sh
echo $test_string
但是,当我这样做时:
bash mainscript.sh
我一无所获。关于如何做到这一点的任何想法?
默认情况下,Shell 变量在子进程中不可见。要将它们传递给孩子,请使用export
关键字:
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
export test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")