I'm having trouble understanding the behavior of the return built-in in Bash. Here is a sample script.
#!/bin/bash  dostuff() {     date | while true; do         echo returning 0         return 0         echo really-notreached     done      echo notreached     return 3 }  dostuff echo returncode: $?  The output of this script is:
returning 0 notreached returncode: 3  If, however, the date |  is removed from line 4, the output is as I expected:
returning 0 returncode: 0  It seems like the return statement as used above is acting the way I thought the break statement ought to behave, but only when the loop is on the right hand side of a pipe. Why is this the case? I couldn't find anything to explain this behavior in the Bash man page or online. The script acts the same way in Bash 4.1.5 and Dash 0.5.5.
Function Return A bash function can return a value via its exit status after execution. By default, a function returns the exit code from the last executed command inside the function. It will stop the function execution once it is called. You can use the return builtin command to return an arbitrary number instead.
The exit code of the function (within the function) is set by using return . So when in a function return 0 is run, the function execution terminates, giving an exit code of 0.
To return values, you can set a global variable with the result, or use command substitution, or you can pass in the name of a variable to use as the result variable.
Bash function can return a string value by using a global variable. In the following example, a global variable, 'retval' is used. A string value is assigned and printed in this global variable before and after calling the function. The value of the global variable will be changed after calling the function.
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 after 'return' }  function doSomethingElse {     echo "d"     exit 0     echo "e"  # this will not execute because the program has exited }  doSomething echo "c" doSomethingElse echo "f"  # this will not execute because the program exited in 'doSomethingElse'  Running the above code will output:
a c d 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With