我有以下 bash 脚本:
#!/bin/bash
export PYCACHED_HOST='localhost'
export PYCACHED_PORT=8001
echo "PyCached environment variables set to: PYCACHED_HOST=`echo $PYCACHED_HOST`, PYCACHED_PORT=`echo $PYCACHED_PORT`"
当我运行它时,会打印以下输出:
PyCached environment variables set to: PYCACHED_HOST=localhost, PYCACHED_PORT=8001
当我再次运行相同的回显行时:
echo "PyCached environment variables set to: PYCACHED_HOST=`echo $PYCACHED_HOST`, PYCACHED_PORT=`echo $PYCACHED_PORT`"
我得到:
PyCached environment variables set to: PYCACHED_HOST=, PYCACHED_PORT=
我知道环境变量是为脚本上下文设置的,但是我该怎么做才能在脚本执行结束后使它们可用?
最佳答案
what can I do to make them available after the script execution is over?
运行你的脚本;
source ./script.sh
或
. ./script.sh
这将在当前 shell 中运行您的脚本而不创建新进程,因此环境变量将在脚本结束后在当前 shell 中可用。
关于bash - linux环境变量设置消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19752166/