linux环境变量设置消失

duc*_*cin 2 bash environment-variables

我有以下 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`"
Run Code Online (Sandbox Code Playgroud)

当我运行它时,打印以下输出:

PyCached environment variables set to: PYCACHED_HOST=localhost, PYCACHED_PORT=8001
Run Code Online (Sandbox Code Playgroud)

当我再次运行相同的回显行时:

echo "PyCached environment variables set to: PYCACHED_HOST=`echo $PYCACHED_HOST`, PYCACHED_PORT=`echo $PYCACHED_PORT`"
Run Code Online (Sandbox Code Playgroud)

我得到:

PyCached environment variables set to: PYCACHED_HOST=, PYCACHED_PORT=
Run Code Online (Sandbox Code Playgroud)

我知道环境变量是为脚本上下文设置的,但是我该怎么做才能使它们在脚本执行结束后可用?

anu*_*ava 6

what can I do to make them available after the script execution is over?

运行你的脚本;

source ./script.sh
Run Code Online (Sandbox Code Playgroud)

或者

. ./script.sh
Run Code Online (Sandbox Code Playgroud)

这将在当前 shell 中运行脚本,而无需创建新进程,因此脚本结束后,环境变量将在当前 shell 中可用。

  • 是的,它们实际上是相同的。点是快捷语法。 (2认同)