Return Values and Exit Codes
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.In this page:
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
square() {
echo $(( $1 * $1 ))
}
result=$(square 6)
echo "The square is $result"
Login to try C/C++/Java/PHP code in the editor
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
#!/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"
Login to try C/C++/Java/PHP code in the editor
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
#!/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: $?"
Login to try C/C++/Java/PHP code in the editor
- Using
returnto try to hand back a string or a large computed value;returnonly accepts an integer from 0 to 255 and sets the function's exit status, it is not a general-purpose return channel for data. - 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. - Trying to capture a function's 'return value' with
$?when it actually printed the result viaecho; you needresult=$(my_function)to capture printed output, and$?separately if you also want the exit status.
return Nsets the function's exit status toN(0-255) and immediately stops executing the function -- it does not return arbitrary data.- To return computed data,
echo(orprintf) the value from the function and capture it in the caller withresult=$(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:
echothe data and usereturn 0/return 1to also signal success or failure.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: