重定向所有命令的bash输出和错误

时间:2021-07-14 15:44:16

How to redirect all commands executed on the bash to /dev/null ?

如何将bash上执行的所有命令重定向到/ dev / null?

It's obvious that for a command we have to do:

很明显,对于我们必须做的命令:

command > /dev/null  2>&1

How about all commands that will be executed further on ?

所有将要执行的命令怎么样?

3 个解决方案

#1


5  

Simply start a new shell:

只需启动一个新shell:

bash >/dev/null 2>&1

Now you can type commands blind :) If you want to leave that mode type: exit

现在你可以输入命令blind :)如果你想离开那个模式类型:退出

But typing commands blind will normally not what you want. So I would suggest to create a text file like script.sh, place your commands in it:

但盲目输入命令通常不是你想要的。所以我建议创建一个像script.sh这样的文本文件,将命令放在其中:

command1 foo 
command2 bar

and than execute it using:

而不是使用以下方式执行:

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

The ouput of all commands in that script will be redirected to /dev/null now

该脚本中所有命令的输出现在将重定向到/ dev / null

#2


3  

Use exec without a command:

在没有命令的情况下使用exec:

exec > /dev/null 2>&1

As hex2mgl pointed out, if you do this in an interactive shell, you won't even see your prompt anymore, as the shell prints that to standard error. I assume this is intended for a script, as it doesn't make a lot of sense to ignore all output from commands run interactively :)

正如hex2mgl指出的那样,如果你在交互式shell中执行此操作,则甚至不会再看到提示符,因为shell会将其打印到标准错误。我假设这是用于脚本,因为忽略来自交互式运行的命令的所有输出没有多大意义:)

#3


0  

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

对于脚本或其他实际目的,对命令进行分组是一个很好的解决方案。查看http://www.gnu.org/software/bash/manual/html_node/Compound-Commands.html具体说明任何与复合命令相关的重定向(请参阅重定向)都适用于该复合命令中的所有命令,除非明确覆盖。

#1


5  

Simply start a new shell:

只需启动一个新shell:

bash >/dev/null 2>&1

Now you can type commands blind :) If you want to leave that mode type: exit

现在你可以输入命令blind :)如果你想离开那个模式类型:退出

But typing commands blind will normally not what you want. So I would suggest to create a text file like script.sh, place your commands in it:

但盲目输入命令通常不是你想要的。所以我建议创建一个像script.sh这样的文本文件,将命令放在其中:

command1 foo 
command2 bar

and than execute it using:

而不是使用以下方式执行:

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

The ouput of all commands in that script will be redirected to /dev/null now

该脚本中所有命令的输出现在将重定向到/ dev / null

#2


3  

Use exec without a command:

在没有命令的情况下使用exec:

exec > /dev/null 2>&1

As hex2mgl pointed out, if you do this in an interactive shell, you won't even see your prompt anymore, as the shell prints that to standard error. I assume this is intended for a script, as it doesn't make a lot of sense to ignore all output from commands run interactively :)

正如hex2mgl指出的那样,如果你在交互式shell中执行此操作,则甚至不会再看到提示符,因为shell会将其打印到标准错误。我假设这是用于脚本,因为忽略来自交互式运行的命令的所有输出没有多大意义:)

#3


0  

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

对于脚本或其他实际目的,对命令进行分组是一个很好的解决方案。查看http://www.gnu.org/software/bash/manual/html_node/Compound-Commands.html具体说明任何与复合命令相关的重定向(请参阅重定向)都适用于该复合命令中的所有命令,除非明确覆盖。