Bash Conditionals, Loops, and Functions/Return status from function

Return a status code from a function.

Section: Functions

Return status from function

bash
bash
validate() { [[ -n "$1" ]] || return 1; }
Explanation

return is for status codes, not arbitrary values.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Functions
Define a function
Define a shell function.
OpenIn sheetbashsame section
Call a function
Invoke a shell function with arguments.
OpenIn sheetbashsame section
Use local variable in function
Keep function internals from leaking into caller scope.
OpenIn sheetbashsame section
Return data via stdout
Emit data to stdout and capture it with command substitution.
OpenIn sheetbashsame section
Trace shell functions
Trace function execution during debugging.
OpenIn sheetbashsame section
Test whether file exists
Check whether a regular file exists.