Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to get a variable from another script, as demonstrated in

However, the answer uses source command only available in bash. I want to do this in a portable way.

I have also tried

export VAR="foo"
echo "executing a"

echo $VAR

But of course that does not work either. How to do this?

share|improve this question
up vote 5 down vote accepted

First of all, be aware that var and VAR are different variables.

To answer your question the . command is not bash-specific:

. ./
echo $num

The variables in "a" do not need to be exported.

share|improve this answer
Thank you and excuse my typo. I was pretty surprised with the difference between ./ and . ./ Care to explain the difference? – Eero Aaltonen Jun 12 '13 at 7:29
The dot command (. or source) evaluates the script in your current shell. Executing the script first spawns a subshell, and any environment changes in the subshell are lost when the subshell exits -- a child process cannot alter the environment of its parent. – glenn jackman Jun 12 '13 at 9:01
bash has a handy help builtin to access the manual for a specific command -- see help . – glenn jackman Jun 12 '13 at 9:02

Environment variables are only inherited from parent to child and not the other way round. In your example, calls, so a runs as a child of b. When exports var, it won't be seen by Amend the logic so that the parent process exports the variable, e.g.

echo In
export VAR

echo In
echo $VAR
share|improve this answer
Thank you, but I actually have and two separate targets depending on it, so I need to have the dependencies this way. – Eero Aaltonen Jun 12 '13 at 7:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.