Functions and Scripts
Return Status
Functions as Tests
Bash functions return status codes, not rich values. That makes functions useful inside if statements.
Program
Play the script to see is_even return success and choose the then branch.
return_status.sh
#!/usr/bin/env bash
is_even() {
local value="$1"
(( value % 2 == 0 ))
}
number=8
if is_even "$number"; then
echo "even"
else
echo "odd"
fi
return status
A function's status is the status of its last command unless it calls `return`.
predicate
A predicate command answers a yes/no question through its exit status.