How to silence output in a Bash script?


Question

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
1
127
3/9/2017 11:23:01 PM

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
169
4/3/2017 7:58:20 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon