-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC2152
koalaman edited this page Jan 20, 2018
·
4 revisions
myfunc() {
return "Hello $USER"
}
myfunc() {
echo "Hello $USER"
return 0
}
In many languages, return
is used to return from the function with a final result.
In bash, return
can only be used to signal success or failure (0 = success, 1-255 = failure), more akin to throw/raise
in other languages.
Results should instead be written to stdout and captured:
message=$(myfunc)
echo "The function wrote: $message"
In functions that return small integers, such as getting the cpu temperature, the value should still be written to stdout. return
should be reserved for error conditions, such as "can't determine CPU temperature".
None
- BashFaq: How do I return a string (or large number, or negative number) from a function? "return" only lets me give a number from 0 to 255.