如何使用Bash禁止命令的所有输出? [英] How can I suppress all output from a command using Bash?
问题描述
我有一个Bash脚本,该脚本运行带有参数的程序.该程序输出一些状态(执行此操作,执行此操作...).这个程序没有任何选择可以保持安静.如何防止脚本显示任何内容?
I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There isn't any option for this program to be quiet. How can I prevent the script from displaying anything?
我正在寻找类似Windows的回声" .
I am looking for something like Windows' "echo off".
推荐答案
以下内容将标准输出发送到空设备(位存储桶).
The following sends standard output to the null device (bit bucket).
scriptname >/dev/null
如果您还希望将错误消息发送到那里,请使用以下命令之一(第一个消息可能不适用于所有shell):
And if you also want error messages to be sent there, use one of (the first may not work in all shells):
scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null
并且,如果您想记录消息但看不到消息,请用实际文件替换/dev/null
,例如:
And, if you want to record the messages, but not see them, replace /dev/null
with an actual file, such as:
scriptname &>scriptname.out
为了完整起见,在Windows cmd.exe(其中"nul"等同于"/dev/null")下,它是:
For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is:
scriptname >nul 2>nul
这篇关于如何使用Bash禁止命令的所有输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!