← Back to Bash Course | Chapter 5: Functions | Lesson 6 of 11

Return Values and Exit Codes

A function's return sets its exit status as a small number, but to hand back actual data (like text or a computed value), the standard trick is to echo it and have the caller capture that output.

return Sets Exit Status, Not Data

return N immediately stops the function and sets its exit status to N, which must be an integer between 0 and 255. This is meant for signaling success/failure, not for handing back computed values like strings or large numbers.

Example: return Sets Exit Status, Not Data

bash
#!/bin/bash
is_even() {
    if (( $1 % 2 == 0 )); then
        return 0
    else
        return 1
    fi
}
if is_even 4; then
    echo "4 is even"
fi
if ! is_even 7; then
    echo "7 is not even"
fi

Returning Data via echo and Capture

To hand back an actual computed value like a string or number, have the function echo it, and have the caller capture that output using command substitution, result=$(function_name). This is the standard Bash idiom for returning data since return cannot carry it.

Example: Returning Data via echo and Capture

bash
#!/bin/bash
square() {
    echo $(( $1 * $1 ))
}
result=$(square 6)
echo "The square is $result"

Combining Data Output with a Status Code

A function can both echo its computed result for the caller to capture and use return to separately signal whether it succeeded, giving the caller two independent pieces of information. This pattern is common for functions that might fail in a way the caller needs to check.

Example: Combining Data Output with a Status Code

bash
#!/bin/bash
divide() {
    if [ "$2" -eq 0 ]; then
        echo "0"
        return 1
    fi
    echo $(( $1 / $2 ))
    return 0
}
result=$(divide 10 2)
status=$?
echo "Result: $result, status: $status"
result=$(divide 10 0)
status=$?
echo "Result: $result, status: $status"

Implicit Exit Status from the Last Command

If a function never calls return explicitly, its exit status is simply whatever the last command inside it returned. This means the ending of a function matters even without an explicit return statement.

Example: Implicit Exit Status from the Last Command

bash
#!/bin/bash
check_file() {
    ls "$1" > /dev/null 2>&1
}
touch example.txt
check_file example.txt
echo "Exit status for existing file: $?"
check_file missing.txt
echo "Exit status for missing file: $?"
Common Mistakes
  1. Using return to try to hand back a string or a large computed value; return only accepts an integer from 0 to 255 and sets the function's exit status, it is not a general-purpose return channel for data.
  2. Forgetting that if a function has no explicit return, its exit status is simply the exit status of the last command it ran -- not automatically 0.
  3. Trying to capture a function's 'return value' with $? when it actually printed the result via echo; you need result=$(my_function) to capture printed output, and $? separately if you also want the exit status.
Chapter Summary
  • return N sets the function's exit status to N (0-255) and immediately stops executing the function -- it does not return arbitrary data.
  • To return computed data, echo (or printf) the value from the function and capture it in the caller with result=$(function_name args).
  • Without an explicit return, a function's exit status is that of its last executed command.
  • It's common and idiomatic to combine both: echo the data and use return 0/return 1 to also signal success or failure.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.