I ran into some strange (for me) problem in Cygwin bash version 4.3.42 (4). Shell variables declared in the called script are not saved in the calling script when the first is called from the function.
I have two scenarios to illustrate the problem. script1.sh calls script2.sh, which sets two variables. If script2 is called using a function in script1, the variables are lost, and if script2 is called without calling the function, the variables are saved as expected. All calls to script2 are made through the "source".
script1.sh:
#!/usr/bin/bash # # calling script # function sourceit() { source scripts/script2.sh } sval=1 echo "$0 before sourceit(); rval=$rval sval=$sval PID=$$" sourceit echo "$0 after sourceit(); rval=$rval sval=$sval PID=$$" sval=3 echo "$0 before source; rval=$rval sval=$sval PID=$$" source scripts/script2.sh echo "$0 after source; rval=$rval sval=$sval PID=$$"
script2.sh
#!/usr/bin/bash # # called script # echo "$0 before declare; rval=$rval sval=$sval PID=$$" sval=2 declare -r rval=2 echo "$0 after declare; rval=$rval sval=$sval PID=$$"
Results:
scripts/script1.sh before sourceit(); rval= sval=1 PID=1752 scripts/script1.sh before declare; rval= sval=1 PID=1752 scripts/script1.sh after declare; rval=2 sval=2 PID=1752 scripts/script1.sh after sourceit(); rval= sval=2 PID=1752 scripts/script1.sh before source; rval= sval=3 PID=1752 scripts/script1.sh before declare; rval= sval=3 PID=1752 scripts/script1.sh after declare; rval=2 sval=2 PID=1752 scripts/script1.sh after source; rval=2 sval=2 PID=1752
I do not see any subnets that are created (the same PID is shown everywhere).
Am I missing a thinner point in a bash script?
source share