My Bash script calls a lot of commands, most of them output something. I want to silence them all. Right now, I'm adding &>/dev/null
at the end of most command invocations, like this:
some_command &>/dev/null
another_command &>/dev/null
command3 &>/dev/null
Some of the commands have flags like --quiet
or similar, still, I'd need to work it out for all of them and I'd just rather silence all of them by default and only allow output explicitly, e.g., via echo
.
To silence the output of a command, we redirect either stdout or stderr — or both — to /dev/null. To select which stream to redirect, we need to provide the FD number to the redirection operator.
press control S to stop output, control Q to resume (this is called XON/XOFF) redirect your output to a pager such as less , e.g., strace date | less. redirect your output to a file, e.g., strace -o foo date , and browse it later.
You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
You can use the exec
command to redirect everything for the rest of the script.
You can use 3>&1
to save the old stdout stream on FD 3, so you can redirect output to that if you want to see the output.
exec 3>&1 &>/dev/null
some_command
another_command
command_you_want_to_see >&3
command3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With