如何使用 Bash 抑制命令的所有输出? [英] How can I suppress all output from a command using Bash?

查看:35
本文介绍了如何使用 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 的 "echo off".

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆