I have a script. I would like to give this script a quiet mode and a verbose mode.
This is the equivalent of:
if $verbose
then
redirect="> /dev/null"
fi
echo "Verbose mode enabled" $redirect # This doesn't work because the redirect isn't evaluated.
I’d really like a better way of doing this than writing if-elses for every statement affected.
eval could work, but has obvious side effects on other variables.
You could write a wrapper function:
You can then use it to run any command with the redirect:
(If all you need to do is echo with this, you can of course make the function simpler, just echoing the first argument instead of running them all as a command)