1

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?

1
  • You can end each line in &> /dev/null except the echos.
    – LatinSuD
    Jun 24, 2014 at 13:32

2 Answers 2

5

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"
0
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .