Skip to main content
5 of 8
deleted 84 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. First we pipe stdout to /dev/null, then we convert stderr to stdout, because Unix pipes will only operate on stdout. You can still grep the text.

$ { ./stdout-stderr.sh >/dev/null ; } 2>&1  | grep 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