Usually, I see questions about people not being able to access variables from outside their scope. However, I seem to be experiencing the opposite: I am seeing variables still having values from inner scopes they should have given up afterwards. For example (making svn aliases similar to git aliases):
function svn() {
case $@ in
alias*) shift 1;
for i in "$@"; do
if [[ "$i" == "-t" ]];
then
j="$i,$j"
elif [[ "$i" == "-f" ]];
k="$i,$j"
fi
done
echo "i = $i"
echo "j = $j"
echo "k = $k"
;;
esac
}
I put this in a script and source it, so its function is made into an alias for bash (I think). Try running this with various combinations of "-t" and "-f", and you'll see that the variables "$i", "$j", and "$k" all keep their values when running the script again, and that they stay the same in the outer shell, after the script has exited. I am using an Ubuntu 15.04 laptop, and when I type Ctrl-X
Ctrl-V
my shell outputs GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu).
Everything I have read about bash tells me this should not happen (admittedly, I am somewhat of a beginner in this area). Variables should not stay set after the script (or function) exits, unless you use export
on them, which I have not. So why is this happening?