Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to silence output of all commands in a Bash script?

Tags:

bash

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.

like image 465
Borek Bernard Avatar asked Oct 12 '20 21:10

Borek Bernard


People also ask

How do you silent a bash script?

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.

How do I stop output in Linux?

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.

How do I redirect all output to Dev Null?

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.


1 Answers

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
like image 150
Barmar Avatar answered Sep 21 '22 21:09

Barmar