bash - Sharing variables between shell scripts -
i have shell script (let's call parent.sh) calls shell script (child.sh), passing argument.
the child script work , sets value in variable called create_sql
. want access create_sql
variable parent script invoked it.
i calling child script within parent script this:
./child.sh "$dictionary"
and straight afterwards have line:
echo "the resulting create script is: "$create_sql
however, there no value being output, however, in child script doing same thing , variable set.
how can work can read variables created child script?
succinctly, can't have child script set variable in parent script unless like:
. ./child.sh "$dictionary"
(or in bash, mimicking c shell, source ./child.sh "$dictionary"
). reads , executes script in environment of current shell, alter other variable in parent.sh
script; there no isolation between scripts. otherwise, child process cannot sanely set environment of parent shell. (if want insanely, can have child shell run debugger, attach parent shell process , set environment way — calling 'insane' being polite).
arguably, best way output child stashed in variable in parent script have child.sh
echo value want in $create_sql
, , use
create_sql=$(./child.sh "$dictionary") echo "the resulting create script is: $create_sql"
with no spaces around assignment operator. note echo
includes variable inside double quotes; preserve internal spacing (including newlines) in variable's value. written in question, variable flattened space-separated stream of 'words' (sequences of non-spaces), losing internal spacing.
Comments
Post a Comment