如何使用 Bash 抑制命令的所有输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/617182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I suppress all output from a command using Bash?
提问by 6bytes
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?
我有一个运行带有参数的程序的 Bash 脚本。该程序输出一些状态(做这个,做那个......)。这个程序没有任何选择可以安静。如何防止脚本显示任何内容?
I am looking for something like Windows' "echo off".
我正在寻找类似 Windows 的"echo off" 的东西。
回答by andynormancx
The following sends standard output to the null device (bit bucket).
下面将标准输出发送到空设备(位桶)。
scriptname >/dev/null
and if you also want error messages to be sent there, use one of (the first may not work in all shells):
如果您还希望在那里发送错误消息,请使用其中一个(第一个可能不适用于所有 shell):
scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null
and, if you want to record the messages but not see them, replace /dev/null
with an actual file, such as:
并且,如果您想记录消息但看不到它们,请替换/dev/null
为实际文件,例如:
scriptname &>scriptname.out
For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is :
为了完整起见,在 Windows cmd.exe(其中“nul”相当于“/dev/null”)下,它是:
scriptname >nul 2>nul
回答by Diego Sevilla
Something like
就像是
script > /dev/null 2>&1
This will prevent standard output anderror output, redirecting them both to /dev/null
.
这将阻止标准输出和错误输出,将它们都重定向到/dev/null
.
回答by V0idSt4r
Try
尝试
: $(yourcommand)
:
is short for "do nothing".
:
是“什么都不做”的缩写。
$()
is just your command.
$()
只是你的命令。
回答by semente
An alternative that may fit in some situations is to assign the result of a command to a variable:
在某些情况下可能适合的替代方法是将命令的结果分配给变量:
$ DUMMY=$( grep root /etc/passwd 2>&1 )
$ echo $?
0
$ DUMMY=$( grep r00t /etc/passwd 2>&1 )
$ echo $?
1
Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command's return code is respected.
由于 Bash 和其他 POSIX 命令行解释器不将变量赋值视为命令,因此会考虑当前命令的返回码。
Note:assignement with the typeset
or declare
keyword is considered as a command, so the evaluated return code in case is the assignement itself and not the command executed in the sub-shell:
注意:使用typeset
ordeclare
关键字的赋值被视为命令,因此评估的返回码是赋值本身,而不是在子 shell 中执行的命令:
$ declare DUMMY=$( grep r00t /etc/passwd 2>&1 )
$ echo $?
0
回答by Lucas Gabriel Sánchez
Like andynormancx post use this: (if you're working in an Unix environment)
像 andynormancx 帖子一样使用这个:(如果你在 Unix 环境中工作)
scriptname > /dev/null
or you can use this: (if you're working in a Windows environment)
或者您可以使用它:(如果您在 Windows 环境中工作)
scriptname > nul
回答by ivanleoncz
Take a look at this example from The Linux Documentation Project:
从The Linux Documentation Project看看这个例子:
3.6 Sample: stderr and stdout 2 file
This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.
rm -f $(find / -name core) &> /dev/null
3.6 示例:stderr 和 stdout 2 文件
这会将程序的每个输出放置到一个文件中。这有时适用于 cron 条目,如果您希望命令在绝对静音中传递。
rm -f $(find / -name core) &> /dev/null
That said, you can use this simple redirection:
也就是说,您可以使用这个简单的重定向:
/path/to/command &>/dev/null
回答by Rohan Dsouza
In your script you can add the following to the lines that you know are going to give an output:
在您的脚本中,您可以将以下内容添加到您知道将提供输出的行中:
some_code 2>>/dev/null
Or else you can also try
或者你也可以试试
some_code >>/dev/null