tags:

views:

371

answers:

2

I have a program that outputs to stdout and would like to silence that output in a bashscript 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 command-line:

#!/bin/bash
myprogram > sample.s

How would I do this? Thanks

+5  A: 

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 sample.s, stderr to sample.err
myprogram > sample.s 2> sample.err

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

# Log output, hide errors.
myprogram > sample.s 2> /dev/null
John Kugelman
chradcliffe
It's new in Bash 4.
Dennis Williamson
Ignacio Vazquez-Abrams
A: 

2>&1

with this you will be redirecting the stderr (which is descriptor 2) to the dile descriptor 1 which is the the stdout

myprogram > sample.s

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

myprogram > sample.s 2>&1

Combining the two command will result by redirecting both stderr and stdout to sample.s

myprogram 2>&1 /dev/null

If you want to completly silent your application