Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior of the 'return' statement in Bash functions

Tags:

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.

like image 406
A B Avatar asked Aug 18 '11 15:08

A B


People also ask

What does a bash function return?

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.

What does return 0 do in bash?

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.

How does a function return a value in Linux?

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.

Can a bash function return a string?

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.


2 Answers

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 } 
like image 59
glenn jackman Avatar answered Oct 03 '22 05:10

glenn jackman


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 
like image 36
Derek Soike Avatar answered Oct 03 '22 05:10

Derek Soike