93

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;

Here is how i am calling 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 ./source.sh
# This works now
echo $ros_config
1

2 Answers 2

161

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

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

. is an alias for source when delimited by space (like all bash commands). If you source you'll avoid common bugs associated with using . to mean the current directory, etc.
Note: source is an alias for . not the other way around.
This works fine for scripts in the current directory - but what do you do if the script is in /usr/local/bin? That's more convenient because then you don't have to cd to the directory where your script is located.
15

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

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

3 Comments

If I use ./source.sh it does not work. source source.sh does work!
SInce you're providing full path to setup.sh here therefore ./ is not needed since ./ means current dir.
"./source.sh" executes the script (in its own shell). ". ./source.sh" (or "source ./source.sh", or "source source.sh") will apply the script contents to the current shell environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.