bash function return value

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

function my_function {
    echo "Return value."
}

result=$(my_function)

return statement

Beware! Do not use the return statement to return a value. The return statement is used to return exit codes, usually 0 for a normal exit, and 1 if an error occurred within the function. Instead, use the format above.

From help return:

return: return [n]

Return from a shell function.

Causes a function or sourced script to exit with the return value
specified by N.  If N is omitted, the return status is that of the
last command executed within the function or script.

Exit Status:
Returns N, or failure if the shell is not executing a function or script.