I have a function, and inside that function is a while loop.
When I try to set a non local variable inside the while loop with an IF statement, then exit the entire function, suddenly the variable is no longer set?
function EXAMPLE {
cat test.txt | while read LINE; do
if [ "$LINE" = "FAIL" ]; then
echo "Detected FAIL in file! Setting RETURN=fail and exiting function."
RETURN="fail"
return
fi
done
}
### START SCRIPT ###
EXAMPLE (Call example function)
echo "$RETURN"
For some reason, RETURN is empty. I have done this many many many times in the past though. Something about the while loop is preventing RETURN from being passed out of the function. Is "return" causing the script to break the loop and not the function?
Thanks
something | otherthing: in bash,otherthingruns in a subshell. When it ends, the subshell exits. Any variable creater or modified in the subshell is not available in the parent shell, which still have their former values.