Behavior Of Return Statement In Bash Functions
Answer : In the date | while ... scenario, that while loop is executed in a subshell due to the presence of the pipe. Thus, the return statement breaks the loop and the subshell ends, leaving your function to carry on. You'll have to reconstruct the code to remove the pipeline so that no subshells are created: dostuff() { # redirect from a process substitution instead of a pipeline while true; do echo returning 0 return 0 echo really-notreached done < <(date) echo notreached return 3 } If you return inside a function, that function will stop executing but the overall program will not exit. If you exit inside a function, the overall program will exit. You cannot return in the main body of a bash script, you can only return inside a function or sourced script. For example: #!/usr/bin/env bash function doSomething { echo "a" return echo "b" # this will not execute because it is...