如何在 Bash 中隐藏命令输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18062778/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:00:50  来源:igfitidea点击:

How to hide command output in Bash

bashshell

提问by user2650277

I want to make my Bash scripts more elegant for the end user. How do I hide the output when Bash is executing commands?

我想让我的 Bash 脚本对最终用户更优雅。Bash 执行命令时如何隐藏输出?

For example, when Bash executes

例如,当 Bash 执行时

yum install nano

The following will show up to the user who executed the Bash:

以下将显示给执行 Bash 的用户:

Loaded plugins: fastestmirror
base                                                     | 3.7 kB     00:00
base/primary_db                                          | 4.4 MB     00:03
extras                                                   | 3.4 kB     00:00
extras/primary_db                                        |  18 kB     00:00
updates                                                  | 3.4 kB     00:00
updates/primary_db                                       | 3.8 MB     00:02
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nano.x86_64 0:2.0.9-7.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch              Version                Repository       Size
================================================================================
Installing:
 nano            x86_64            2.0.9-7.el6            base            436 k

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 436 k
Installed size: 1.5 M
Downloading Packages:
nano-2.0.9-7.el6.x86_64.rpm                              | 436 kB     00:00
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <[email protected]>
 Package: centos-release-6-4.el6.centos.10.x86_64 (@anaconda-CentOS-201303020151.x86_64/6.4)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : nano-2.0.9-7.el6.x86_64                                      1/1
  Verifying  : nano-2.0.9-7.el6.x86_64                                      1/1

Installed:
  nano.x86_64 0:2.0.9-7.el6

Complete!

Now I want to hide this from the user and instead show:

现在我想对用户隐藏它,而是显示:

    Installing nano ......

How can I accomplish this task? I will definitely help to make the script more user friendly. In case an error occurs then it should be shown to the user.

我怎样才能完成这个任务?我一定会帮助使脚本更加用户友好。如果发生错误,则应向用户显示。

I would like to know how to show same message while a set of commands are being executed.

我想知道如何在执行一组命令时显示相同的消息。

回答by Jeff Bowman

Use this.

用这个。

{
  /your/first/command
  /your/second/command
} &> /dev/null

Explanation

解释

To eliminate output from commands, you have two options:

要消除命令的输出,您有两个选择:

  • Close the output descriptor file, which keeps it from accepting any more input. That looks like this:

    your_command "Is anybody listening?" >&-
    

    Usually, output goes either to file descriptor 1 (stdout) or 2 (stderr). If you close a file descriptor, you'll have to do so for every numbered descriptor, as &>(below) is a special BASH syntax incompatible with >&-:

    /your/first/command >&- 2>&-
    

    Be careful to note the order: >&-closes stdout, which is what you want to do; &>-redirects stdout and stderr to a file named -(hyphen), which is not what what you want to do. It'll look the same at first, but the latter creates a stray file in your working directory. It's easy to remember: >&2redirects stdout to descriptor 2 (stderr), >&3redirects stdout to descriptor 3, and >&-redirects stdout to a dead end (i.e. it closes stdout).

    Also beware that some commands may not handle a closed file descriptor particularly well ("write error: Bad file descriptor"), which is why the better solution may be to...

  • Redirect output to /dev/null, which accepts all output and does nothing with it. It looks like this:

    your_command "Hello?" > /dev/null
    

    For output redirection to a file, you can direct both stdout and stderr to the same place very concisely, but only in bash:

    /your/first/command &> /dev/null
    
  • 关闭输出描述符文件,使其不再接受任何输入。看起来像这样:

    your_command "Is anybody listening?" >&-
    

    通常,输出到文件描述符 1 (stdout) 或 2 (stderr)。如果您关闭文件描述符,则必须对每个编号的描述符都这样做,因为&>(如下)是一种特殊的 BASH 语法,与>&-以下内容不兼容:

    /your/first/command >&- 2>&-
    

    注意顺序:>&-closes stdout,这就是你想要做的;&>-将 stdout 和 stderr 重定向到名为-(连字符)的文件,这不是您想要做的。一开始看起来是一样的,但后者会在您的工作目录中创建一个杂散文件。很容易记住:>&2将标准输出重定向到描述符 2 (stderr),>&3将标准输出重定向到描述符 3,>&-并将标准输出重定向到死胡同(即它关闭标准输出)。

    另请注意,某些命令可能无法特别好地处理关闭的文件描述符(“写入错误:错误的文件描述符”),这就是为什么更好的解决方案可能是...

  • 将输出重定向到/dev/null,它接受所有输出并且不执行任何操作。它看起来像这样:

    your_command "Hello?" > /dev/null
    

    要将输出重定向到文件,您可以非常简洁地将 stdout 和 stderr 定向到同一个位置,但仅限于 bash:

    /your/first/command &> /dev/null
    

Finally, to do the same for a number of commands at once, surround the whole thing in curly braces. Bash treats this as a group of commands, aggregating the output file descriptors so you can redirect all at once. If you're familiar instead with subshells using ( command1; command2; )syntax, you'll find the braces behave almost exactly the same way, except that unless you involve them in a pipe the braces will not create a subshell and thus will allow you to set variables inside.

最后,要同时对多个命令执行相同操作,请将整个内容括在花括号中。Bash 将其视为一组命令,聚合输出文件描述符,以便您可以一次重定向所有内容。如果您熟悉使用( command1; command2; )语法的子shell ,您会发现大括号的行为方式几乎完全相同,除非您将它们包含在管道中,否则大括号不会创建子shell,从而允许您在内部设置变量。

{
  /your/first/command
  /your/second/command
} &> /dev/null

See the bash manual on redirectionsfor more details, options, and syntax.

有关更多详细信息、选项和语法,请参阅有关重定向bash 手册

回答by ktm5124

You can redirect stdout to /dev/null.

您可以将标准输出重定向到 /dev/null。

yum install nano > /dev/null

yum install nano > /dev/null

Or you can redirect both stdout and stderr,

或者你可以重定向 stdout 和 stderr,

yum install nano &> /dev/null.

yum install nano &> /dev/null.

But if the program has a quiet option, that's even better.

但如果该程序有一个安静的选项,那就更好了。

回答by LeOn - Han Li

A process normally has two outputs to screen: stdout(standard out), and stderr(standard error).

一个进程通常有两个输出要筛选:stdout(标准输出)和stderr(标准错误)。

Normally informational messages go to sdout, and errors and alerts go to stderr.

通常信息性消息转到sdout,错误和警报转到stderr

You can turn off stdoutfor a command by doing

您可以stdout通过执行以下操作来关闭命令

MyCommand >/dev/null

and turn off stderrby doing:

stderr通过执行以下操作关闭:

MyCommand 2>/dev/null

If you want both off, you can do:

如果你想要两者都关闭,你可以这样做:

MyCommand 2>&1 >/dev/null

The 2>&1says send stderr to the same place as stdout.

2>&1说标准错误发送到相同的地方标准输出。

回答by Giuseppe Pes

You can redirect the output to /dev/null. For more info regarding /dev/null read this link.

您可以将输出重定向到/dev/null. 有关 /dev/null 的更多信息,请阅读此链接

You can hide the output of a comand in the following ways :

您可以通过以下方式隐藏命令的输出:

echo -n "Installing nano ......"; yum install nano > /dev/null; echo " done."; 

Redirect the standard output to /dev/null, but not the standard error. This will show the errors occurring during the installation, for example if yumcannot find a package.

将标准输出重定向到/dev/null,而不是标准错误。这将显示安装过程中发生的错误,例如如果yum找不到包。

echo -n "Installing nano ......"; yum install nano &> /dev/null; echo " done.";

While this code will not show anything in the terminal since both standard error and standard output are redirected and thus nullified to /dev/null.

虽然此代码不会在终端中显示任何内容,因为标准错误和标准输出都被重定向并因此无效到/dev/null.

回答by Quanlong

>/dev/null 2>&1will mute both stdoutand stderr

>/dev/null 2>&1将静音stdoutstderr

yum install nano >/dev/null 2>&1

回答by kkeller

You should not use bash in this case to get rid of the output. Yum does have an option -qwhich suppresses the output.

在这种情况下,您不应该使用 bash 来消除输出。Yum 确实有一个-q抑制输出的选项。

You'll most certainly also want to use -y

你肯定也想使用 -y

echo "Installing nano..."
yum -y -q install nano

To see all the options for yum, use man yum.

要查看 yum 的所有选项,请使用man yum.

回答by Arif

.SILENT:

Type " .SILENT: " in the beginning of your script without colons.

在脚本的开头输入“.SILENT:”,不带冒号。