0

I have a bash script ex.sh that calls another script path.sh

sh path.sh

I have a variable in path.sh that I would like to use in ex.sh

I tried

export var

and then called it in ex.sh using

echo 'export HOME=${var}' >> /etc/profile.d/user_env.sh

This is not working, Any help is greatly appreciated

2
  • 1
    You can't use single-quotes here if you want expansion to occur. Commented Sep 5, 2014 at 21:54
  • @jquerynoob you are supposed to accept the answer which is best suited for you as a courtesy to others who spend some time helping you. If you are not satisfied then feel free to add comments on the provided answers Commented Sep 6, 2014 at 1:03

2 Answers 2

3

export recognizes a variable or function to its sub-shells.
Instead, inside ex.sh, source the path.sh script like below ->

. ./path.sh

or like this ->

source ./path.sh

That way, all declared variables and functions of path.sh becomes available in current script (ex.sh in this case)

If path.sh is doing some more work other than declaring variables and functions then have a separate third script set_var.sh and source that in both path.sh and ex.sh

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

9 Comments

This would be necessary if you wanted path.sh -- the child process -- to expose variables to the caller. It is not necessary going in the other direction when all variables necessary are exported -- which is to say, when environment variables from the caller to be visible to its child processes.
Well he can customize it using by having third script whatever he wants to expose it to ex.sh
(By the way -- if the downvote on my answer was from you, a comment explaining why and how its contents are inaccurate would be appreciated).
nod. As nothing you say here is inaccurate, I'm staying neutral on yours as well. Accept my apologies for the accusation.
Thx. feel free to edit. Anyhow, the problem is the attempt to use export. since export works down i.e. recognizes to sub-shells and the question tries to recognize variables up i.e. to a parent shell.
|
2

The parent process can't get variable from the child. You should to use some form of communication between processes to exchange data. You can use:

  • file
    • the child write the info into an file, like: newvar=someval
    • when the child exits, the parent source the file and get the newvar variable

or

  • named pipe
    • the parent create a fifo
    • runs the child in the background and start read from the pipe
    • the child write the information to the pipe
    • parent read it...

demo:

file: parent.sh

commfile="./.ex.$$"
echo "the newvar before child: =$newvar="
bash child.sh "$commfile"
source "$commfile"
echo "the newvar after source: =$newvar="
rm -f "$commfile"

file: child.sh

commfile="$1"
#do something
sleep 2
echo "newvar='some value'" >"$commfile"

running parent.sh prints:

the newvar before child: ==
the newvar after source: =some value=

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.