I'm building a shell script to automatically configure a specific computer with the best settings. I would like to hide all output from this script except for echo output. Is it possible?
2 Answers
Start your script with:
exec 3>&1 1>/dev/null 2>&1
That will save your original file descriptor for stdout to &3, and then redirect stdout and stderr to /dev/null. Whenever you want to print something, redirect its output to &3, like:
echo "This message won't be output"
echo "But this one will" >&3
And if you want to hide that detail, you can just define a function that echoes to fd 3:
say() {
echo >&3 "$@"
}
say "This goes to the log"
You can redirect stdout to /dev/null
by adding
1>/dev/null
at the end of each line of your script that will produce output.
You can redirect errors the same way by adding
2>/dev/null
Most commands also have an option to turn off normal output to the console like -q
or something like that. Check the manpages of the commands you use.
&> /dev/null
except the echos.