Reputation: 633
I have a bash script, but it gives me annoying output which I don't want to see. Of course I can hide it in that way:
./script.sh >/dev/null 2>&1
but I want to put the script in "rc.local" or "cron job", so it will be really bad if it received the output every 5 minutes for example, or on boot. It will be great if there is a way to tell the whole script to hide the output.
Upvotes: 1
Views: 1970
Reputation: 295288
If you want to redirect all output to /dev/null
within the script, that can be done like so (in this case, only performing the redirection if the environment variable DEBUG
is not set):
#!/bin/bash
[[ $DEBUG ]] || exec >/dev/null 2>&1
# ...continue with execution here.
You could also check for whether your input is from a TTY to detect interactive use:
if [ -t 0 ]; then
# being run by a human, be extra verbose
PS4=':$LINENO+'
set -x
else
# being run by a daemon, be outright silent
exec >/dev/null 2>&1
fi
See the bash-hackers page on the exec
builtin.
Upvotes: 6