bash脚本中的源文件 [英] Source files in a bash script

查看:151
本文介绍了bash脚本中的源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用彼此相邻的两个版本的ROS.要使用它,我必须为特定版本提供一些环境变量.我想创建一个执行此操作的脚本. 但是,如果我像下面那样创建脚本,则未设置变量,它们可能是在子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.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

推荐答案

使用执行Shell脚本. ./(点空格点斜杠)

当使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆