bash 脚本中的源文件 [英] Source files in a bash script
问题描述
我正在使用两个相邻版本的 ROS.要使用一个,我必须为特定版本提供一些环境变量.我想创建一个执行此操作的脚本.但是如果我创建一个像下面这样的脚本,变量没有设置,它们可能是在子shell中设置的.如何将文件提供给主终端 shell?
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?
source.sh:
source /opt/ros/fuerte/setup.bash;
source ~/fuerte_workspace/setup.bash;
这是我调用 source.sh 的方式:
Here is how i am calling source.sh:
./source.sh
# This does not echo anything, but I expect it should
echo $ros_config
<小时>
更新:通过按照答案中的建议获取 source.sh,我现在可以看到正在设置的变量.
Update: By sourcing source.sh as suggested in the answer, I can now see the variables being set.
source ./source.sh
# This works now
echo $ros_config
推荐答案
使用 ../(点空间点斜线)
使用dot space dot slash"
执行shell脚本时,如下图所示,会在当前shell中执行脚本,不会分叉子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.
$ . ./setup.bash
也就是说,这会在当前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.
这篇关于bash 脚本中的源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!