Silent Mode With a Forced-Output Helper
When VERBOSE is false, send stdout and stderr to /dev/null, while still allowing a forced-output helper to print essential lines.
set_verbose_mode() {
exec 3>&1
exec 4>&2
if [[ "${VERBOSE}" = true ]]; then
echo "Verbose output enabled"
else
exec 1>/dev/null
exec 2>/dev/null
fi
}
output() {
if [[ -n $1 ]]; then
if [[ -n $2 ]] && [[ "${2}" = forced ]]; then
echo "$1" 1>&3 2>&4
else
echo "$1"
fi
fi
}
Caveats
Set VERBOSE before calling set_verbose_mode. Use output msg forced for lines that must always show.