Shell Functions
Defining a Function
Returning Results
Option #1 - Set Different Exit Codes
The following function checks to see if a given number is in range. An optional upper limit may be provided, which, if omitted, defaults to "10".
Example usage:
rangecheck 11 # Exit code=1 since 11 is NOT less than 10 (default)
rangecheck 5 8 # Exit code=0 since 5 is less than 8
Option #2 - Print the Result
Results may be printed to a terminal or to a file:
userinfo()
{
printf "%12s: %s\n" \
USER "${USER:-No value assigned}" \
PWD "${PWD:-No value assigned}"
} > ${1:-/dev/fd/1}
The output goes to the first argument, a file, if specified, or to /dev/fd/1 (stdout) if omitted.
Option #3 - Place Results in One or More Variables
Recommend to use the convention of using an output variable that is the name of the function name converted to Uppercase and preceded with an underscore.