Redirecting stdout & stderr notes

Redirect stdout to stderr:

$ echo Oops, something went wrong >&2

Redirect stderr to /dev/null:

$ ls /home /qwerty 2> /dev/null
home:joe john

To get rid off all output:

$ ls /home /qwerty >/dev/null 2>/dev/null
# or
$ ls /home /qwerty >/dev/null 2>&1

One Response to “Redirecting stdout & stderr notes”

  1. Marc says:

    For more crazy redirections, check out some of the stuff that zsh can do:

    http://zsh.sourceforge.net/Guide/zshguide03.html#l60

    Some of these are not zsh-specific so you can use them in bash as well. I was really surprised to find that you could put redirections in other places besides the end of the command line. E.g.: > file echo hello

Leave a Reply