bash脚本中的源文件

时间:2021-07-31 00:14:42

I am using two versions of ROS next to each other. To use one I have to source some environment variables for the specific version. I would like to create a script that does this. But if I create a script like below the variables are not set, they are probably set in a subshell. How can I source the files to the main terminal shell?

我使用两个版本的ROS彼此相邻。要使用一个我必须为特定版本获取一些环境变量。我想创建一个执行此操作的脚本。但是如果我创建一个类似于下面的脚本,则不会设置变量,它们可能设置在子shell中。如何将文件源到主终端shell?

source.sh:

source.sh:

source /opt/ros/fuerte/setup.bash;
source  ~/fuerte_workspace/setup.bash;

Here is how i am calling source.sh:

这是我如何调用source.sh:

./source.sh
# This does not echo anything, but I expect it should
echo $ros_config

Update: By sourcing source.sh as suggested in the answer, I can now see the variables being set.

更新:通过采购答案中建议的source.sh,我现在可以看到正在设置的变量。

source ./source.sh
# This works now
echo $ros_config

2 个解决方案

#1


65  

Execute Shell Script Using . ./ (dot space dot slash)

使用执行Shell脚本。 ./(点空间点斜线)

While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.

在使用“点空间点斜杠”执行shell脚本时,如下所示,它将在当前shell中执行脚本而不会分支子shell。

$ . ./setup.bash

In other words, this executes the commands specified in the setup.bash in the current shell, and prepares the environment for you.

换句话说,这将执行当前shell中setup.bash中指定的命令,并为您准备环境。

#2


5  

Use dot notation to source in the script file in the current shell i.e. without creating a sub-shell:

使用点表示法来源当前shell中的脚本文件,即不创建子shell:

. /opt/ros/fuerte/setup.bash
. ~/fuerte_workspace/setup.bash

#1


65  

Execute Shell Script Using . ./ (dot space dot slash)

使用执行Shell脚本。 ./(点空间点斜线)

While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.

在使用“点空间点斜杠”执行shell脚本时,如下所示,它将在当前shell中执行脚本而不会分支子shell。

$ . ./setup.bash

In other words, this executes the commands specified in the setup.bash in the current shell, and prepares the environment for you.

换句话说,这将执行当前shell中setup.bash中指定的命令,并为您准备环境。

#2


5  

Use dot notation to source in the script file in the current shell i.e. without creating a sub-shell:

使用点表示法来源当前shell中的脚本文件,即不创建子shell:

. /opt/ros/fuerte/setup.bash
. ~/fuerte_workspace/setup.bash