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?
Succinctly, you can't have a child script set a variable in the parent script unless you do something like:
. ./child.sh "$dictionary"
(or in Bash, mimicking the C shell, source ./child.sh "$dictionary"
). This reads and executes the script in the environment of the current shell, but could alter any other variable in the parent.sh
script; there is no isolation between the scripts. Otherwise, a child process cannot sanely set the environment of the parent shell. (If you want to do it insanely, you can have the child shell run a debugger, attach to the parent shell process and set the environment that way — but calling it 'insane' is being polite).
Arguably, the best way to get output from the child stashed in a variable in the parent script is to have child.sh
echo the value you want in $create_sql
, and then you use
create_sql=$(./child.sh "$dictionary")
echo "The resulting create script is: $create_sql"
with no spaces around the assignment operator. Note that the echo
includes the variable inside the double quotes; this will preserve internal spacing (including newlines) in the variable's value. As written in the question, the variable is flattened into a space-separated stream of 'words' (sequences of non-spaces), losing any internal spacing.
Use the source Luke ! source word ( or . ) allows to run a script like if it was in current shell. beware, your script and the source one are 'married' :-) , sourced script have full access to all variables for the better or the worse...
Other ways implies to modify called script to put is result in a stream format that will be read by caller using script params | read VAR
script params | read VAR
or RESULT=$(SCRIPT params)
and echoing results from your called script
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.