How to silence output in a Bash script?

后端 未结 9 2047
离开以前
离开以前 2020-11-29 17:15

I have a program that outputs to stdout and would like to silence that output in a Bash script while piping to a file.

For example, running the program will output:<

相关标签:
9条回答
  • 2020-11-29 17:57

    Redirect stderr to stdout

    This will redirect the stderr (which is descriptor 2) to the file descriptor 1 which is the the stdout.

    2>&1
    

    Redirect stdout to File

    Now when perform this you are redirecting the stdout to the file sample.s

    myprogram > sample.s
    

    Redirect stderr and stdout to File

    Combining the two commands will result in redirecting both stderr and stdout to sample.s

    myprogram > sample.s 2>&1
    

    Redirect stderr and stdout to /dev/null

    Redirect to /dev/null if you want to completely silent your application.

    myprogram >/dev/null 2>&1
    
    0 讨论(0)
  • 2020-11-29 18:02

    If you want STDOUT and STDERR both [everything], then the simplest way is:

    #!/bin/bash
    myprogram >& sample.s
    

    then run it like ./script, and you will get no output to your terminal. :)

    the ">&" means STDERR and STDOUT. the & also works the same way with a pipe: ./script |& sed that will send everything to sed

    0 讨论(0)
  • 2020-11-29 18:02

    If you are still struggling to find an answer, specially if you produced a file for the output, and you prefer a clear alternative: echo "hi" | grep "use this hack to hide the oputut :) "

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