views:

27

answers:

1

Consider the following:

$ cat t.sh
echo "This is from t.sh"
eval "t2.sh"
echo "\$FROM_t2=$FROM_t2"

$ cat t2.sh
echo "This is from t2.sh"
export FROM_t2="env_var_from_t2"

I want to read the value of "FROM_t2" created by t2.sh into t.sh. Is this possible?

+1  A: 

The usual way of doing this, if you're just using shell scripts, is to "source" t2.sh using . t2.sh (note the . and a space at the start of the command). This runs t2.sh without starting a new process, as if you just pasted t2.sh into t1.sh, so t1.sh can see all the variables that were changed. The "." is the name of a shell built-in command, and you need a space after it, like any other command. You can also write source instead of . if you want to be a bit more explicit.

Doug