I want to use a variable initialized into a subscript, in order to avoid returning it with echo or return.
For example, my goal is to output $myvar with this script:
eval "./script.sh"
echo -n $myvar
declared inside script.sh like this:
declare -gx myvar # global and exported var
myvar=42
Output:
nothing
Is there another command or flag to add in replacement to declare -gx ? Or moving it before eval is the solution ?
source "./script.sh". Or, even shorter,". ./script.sh".evaldo you wanted, it'd need to beeval "$(<./script.sh)", oreval "$(cat ./script.sh)"-- but those are both very silly, assourceDoes The Right Thing out-of-the-box.declare -gxat all here.declare -gis only needed when you're inside a function.declare -xis only needed when you want the variable exported to subprocesses. Neither situation applies.