bash redirect output STDOUT STDERR

HOWTO redirect output: STDOUT / STDERR

Examples:

output or stdout to screen
$ echo "test"

redirects stdout to afile.txt
$ echo "test" > afile.txt

  • 0 is stdin.
  • 1 is stdout.
  • 2 is stderr.

also redirects stdout to afile.txt
$ echo "test" 1> afile.txt

redirect stderr to afile.txt
$ echo "test" 2> afile.txt

>& is the syntax to redirect a stream to another file descriptor

redirect stdin to stderr
$ echo "test" 1>&2
# or
$ echo "test" >&2

..or vice versa:

$ echo "test" 2>&1

So, in short.. 2> redirects STDERR to an (unspecified) file, appending &1 redirects STDERR to STDOUT

One thought on “bash redirect output STDOUT STDERR”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.