12check_system_type() {
13 read -p "Is this a server? (y/n) " server
14 if [[ $server == "y" ]]; then
15 return "server" 16 else
17 echo "📱 Skipping server-specific configs..."
18 fi
In sh
/bash
, return
can only be used to signal success or failure (0 = success, 1-255 = failure), which is in a sense similar to throw/raise
in other languages.
Other results should instead be written to stdout and captured:
message=$(myfunc)
echo "The function wrote: $message"
Even if a function is meant to return a number, (E.g. a function that returns the current CPU temperature) the value must be written to stdout instead of returned. return
should be reserved for error conditions, such as when the function can't determine the CPU temperature. Error or failure messages should be written to stderr.
Note in particular that return -1
is equivalent to return 255
, but that return 1
is the more canonical way of expressing the most common error code.
myfunc() {
return "Hello $USER"
}
myfunc() {
echo "Hello $USER"
return 0
}