How to hide command output in Bash

后端 未结 7 1394
天涯浪人
天涯浪人 2020-12-02 05:42

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

For example, when Bash executes



        
相关标签:
7条回答
  • 2020-12-02 05:55

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

    You'll most certainly also want to use -y

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

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

    0 讨论(0)
  • 2020-12-02 05:59

    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: >&2 redirects stdout to descriptor 2 (stderr), >&3 redirects 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
      

    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.

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

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

    0 讨论(0)
  • 2020-12-02 06:05

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

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

    You can turn off stdout for a command by doing

    MyCommand >/dev/null
    

    and turn off stderr by doing:

    MyCommand 2>/dev/null
    

    If you want both off, you can do:

    MyCommand 2>&1 >/dev/null
    

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

    0 讨论(0)
  • 2020-12-02 06:08

    You can redirect stdout to /dev/null.

    yum install nano > /dev/null

    Or you can redirect both stdout and stderr,

    yum install nano &> /dev/null.

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

    0 讨论(0)
  • 2020-12-02 06:12

    >/dev/null 2>&1 will mute both stdout and stderr

    yum install nano >/dev/null 2>&1
    
    0 讨论(0)
  • 2020-12-02 06:12
    .SILENT:
    

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

    0 讨论(0)
提交回复
热议问题