How to silence output in a Bash script?

Bash

Bash Problem Overview


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:

% myprogram
% WELCOME TO MY PROGRAM
% Done.

I want the following script to not output anything to the terminal:

#!/bin/bash
myprogram > sample.s

Bash Solutions


Solution 1 - Bash

If it outputs to stderr as well you'll want to silence that. You can do that by redirecting file descriptor 2:

# Send stdout to out.log, stderr to err.log
myprogram > out.log 2> err.log

# Send both stdout and stderr to out.log
myprogram &> out.log      # New bash syntax
myprogram > out.log 2>&1  # Older sh syntax

# Log output, hide errors.
myprogram > out.log 2> /dev/null

Solution 2 - Bash

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

Solution 3 - Bash

All output:

scriptname &>/dev/null

Portable:

scriptname >/dev/null 2>&1

Portable:

scriptname >/dev/null 2>/dev/null

For newer bash (no portable):

scriptname &>-

Solution 4 - Bash

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 :) "

Solution 5 - Bash

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

Solution 6 - Bash

Try with:

myprogram &>/dev/null

to get no output

Solution 7 - Bash

Useful variations:


  • Get only the STDERR in a file, while hiding any STDOUT even if the program to hide isn't existing at all. (does not ever hang):

    stty -echo && ./programMightNotExist 2> errors.log && stty echo
    

  • Detach completely and silence everything, even killing the parent script won't abort ./prog (Does behave just like nohup):

    ./prog </dev/null >/dev/null 2>&1 &
    

  • nohup can be used as well to fully detach, as follow:

    nohup ./prog &
    

    A log file nohup.out will be created aside of the script, use tail -f nohup.out to read it.

Solution 8 - Bash

Note: This answer is related to the question "How to turn off echo while executing a shell script Linux" which was in turn marked as duplicated to this one.

To actually turn off the echo the command is: > stty -echo

(this is, for instance; when you want to enter a password and you don't want it to be readable. Remember to turn echo on at the end of your script, otherwise the person that runs your script won't see what he/she types in from then on. To turn echo on run: > stty echo

Solution 9 - Bash

For output only on error:

> so [command]

Logo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamView Question on Stackoverflow
Solution 1 - BashJohn KugelmanView Answer on Stackoverflow
Solution 2 - BashDebuggerView Answer on Stackoverflow
Solution 3 - BashEduardo CuomoView Answer on Stackoverflow
Solution 4 - BashHenry H.View Answer on Stackoverflow
Solution 5 - BashMattView Answer on Stackoverflow
Solution 6 - BashSwathiView Answer on Stackoverflow
Solution 7 - BashNVRMView Answer on Stackoverflow
Solution 8 - BashBerniView Answer on Stackoverflow
Solution 9 - BashAlberto Salvia NovellaView Answer on Stackoverflow