bash脚本中的源文件

时间:2013-04-15 08:57:43

标签: bash shell ubuntu

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

source.sh:

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

以下是我如何调用source.sh:

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

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

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

2 个答案:

答案 0 :(得分:75)

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

使用“dot space dot slash”执行shell脚本时,如下所示,它将在当前shell中执行脚本而不需要子shell。

$ . ./setup.bash

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

答案 1 :(得分:5)

使用点表示法在 当前 shell中的脚本文件中获取,即而不创建子shell

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