Home > database >  Update global variable from while loop
Update global variable from while loop

Time:05-08

Having trouble updating my global variable in this shell script. I have read that the variables inside the loops run on a sub shell. Can someone clarify how this works and what steps I should take.

USER_NonRecursiveSum=0.0

while [ $lineCount -le $(($USER_Num)) ] 
do
    thisTime="$STimeDuration.$NTimeDuration"
    USER_NonRecursiveSum=`echo "$USER_NonRecursiveSum   $thisTime" | bc`
done

CodePudding user response:

That particular style of loop does not run in a sub-shell, it will update the variable just fine.

While the echo command does run in a sub-shell, that's not an issue as the backticks explicitly capture the output from it and "deliver" it to the current shell.

  • Related