Trying to understand what is happening here and why?
The function named “why” gets called first then then the results should be either another call to the function or a complete exit. The expected results is never being able to run the loop below the if statement, which could be an if-else-fi statement or if-elif-else-fi and does not matter.
The question is why does it echo end?
I do not understand why the last echo is ever able to be called.
why () {
read -p 'Loop now? [y/n]' answer
case $answer in
y | Y | yes | YES ) answer="y";;
*) exit;;
esac
}
if [ $answer = 'y' ]
then
why
fi
why
echo "End"
The if statement considers it self done at the time the function completes. It is better shown in this example and the script will not loop back to the echo start.
So to loop back and forth a function to wrap the if statement is used as shown here:
So the answer is it has completed the function and now moves on through the script as expected, which in the example provided would have echoed end as the next task to complete.