Redirect bash output and error for all commands

How to redirect all commands executed in bash to / dev / null?

Obviously, for the team we need to do:

command > /dev/null 2>&1 

What about all the commands that will be executed next?

+7
source share
3 answers

Just launch a new shell:

 bash >/dev/null 2>&1 

Now you can enter commands blind :) If you want to leave this type of mode: exit

But blind input commands are usually not what you want. Therefore, I would suggest creating a text file, for example script.sh , putting my commands into it:

 command1 foo command2 bar 

and execute it using:

 bash script.sh >/dev/null 2>&1 

The output of all the commands in this script will be redirected to / dev / null now

+5
source

Use exec without command:

 exec > /dev/null 2>&1 

As hex2mgl pointed out, if you do this in an interactive shell, you will no longer see your prompt, since the shell prints this standard error. I assume this is intended for the script, as it doesn’t make much sense to ignore all the output from the commands being run interactively :)

+3
source

for scenarios or other practical purposes, grouping a team is a good solution. check http://www.gnu.org/software/bash/manual/html_node/Compound-Commands.html It indicates Any redirections (see Redirections) associated with a compound command apply to all commands within that compound command unless explicitly overridden.

0
source

All Articles