Sharing variables between shell scripts [duplicate]
I have a shell script (let's call it parent.sh) which calls another shell script (child.sh), passing it an argument.
The child script does some work and sets a value in a variable called create_sql. I want to access this create_sql variable from the parent script which invoked it.
I am calling the child script from within the parent script like this:
./child.sh "$dictionary"
and straight afterwards I have the line:
echo "The resulting create script is: "$create_sql
However, there is no value being output, however, in the child script I am doing the same thing and the variable is definitely set.
How can I get this to work so that I can read variables created by the child script?
01 Answer
If you child.sh print on standard output $create_sql value, you could get it in parent script:
create_sql=`./child.sh "$dictionary"`After this line, you should test child.sh exit value with $? and if it returns 0 you could use $create_sql variable.
This approach is useful when script is like a function, otherwise is better follow muru suggested answer.