stderr

From NetBSD Wiki

Jump to: navigation, search

stderr is the third filedescriptor. It has the number 2. For more information, see File descriptors. It is normally used to write error messages and warnings. Separating those messages from normal output data, makes it possible to see them even if stdout is redirected to a file or another process' stdin.

Common usage patterns

To ignore errors from stderr, you can write in bourne shells (sh and ksh in NetBSD):

$ some_tool 2>/dev/null

In C shells (csh in NetBSD) you can write:

$ some_tool >& /dev/null

Unfortunately, the latter example also redirects stdout to /dev/null. There is no way to only ignore stderr.

Sometimes you want to output everything a command outputs to a pager or a file. In those cases, you need to redirect stderr to the same file as stdout. You can do this in bourne shells like this:

$ some_tool 2>&1 | less

In C shells, there's a hack to do this, there is no generic way to assign descriptors to other descriptors' files. It works like this:

$ some_tool |& less

This bundles stdout and stderr to the same pipe and pipes it through less.

Note: Descriptors are assigned to different files/descriptors in the order you type them. This means that

$ some_tool 2>&1 >/dev/null

would first set file descriptor 2 (stderr) to point to the same file as file descriptor 1 (stdout), and after that redirect file descriptor 1 to /dev/null. File descriptor 2 still points to where file descriptor 1 used to point, it doesn't change along with file descriptor 1. This is a source of common confusion.

See also

Personal tools