Skip to main content
3 of 8
added 10 characters in body
Stefan Lasiewski
  • 20.8k
  • 25
  • 72
  • 86

See below for the script used in these tests.

Grep can only operate on stdin, so therefore you must convert the stderr stream in a form that Grep can parse.

Normally, output is printed to both stdout and stderr:

$ ./stdout-stderr.sh
./stdout-stderr.sh: Printing to stdout
./stdout-stderr.sh: Printing to stderr

So we hide output to stdout:

$ { ./stdout-stderr.sh >/dev/null ; } 2>&1
./stdout-stderr.sh: Printing to stderr

The following Bash syntax will hide output to stdout, but will still show stderr. You must convert stderr to stdout, because Unix pipes will only operate on stdout. You can still grep the standard error. The --color flag helps you to see that grep will find a match with 'err'.

$ { ./stdout-stderr.sh >/dev/null ; } 2>&1  | grep --color err
./stdout-stderr.sh: Printing to stderr

Here's the script I use for testing. This prints one line to stdout and one line to stderr:

#!/bin/sh

# Print a message to stdout
echo "$0: Printing to stdout"
# Print a message to stderr
echo "$0: Printing to stderr" >&2

exit 0
Stefan Lasiewski
  • 20.8k
  • 25
  • 72
  • 86