What `2>/dev/null` Does

I kept seeing terminal commands that end with 2>/dev/null written by coding agents. I know this might has something to do with the shell output, but I was not sure what it meant.

My first guess was that 2 meant something like “run this in the background” or “send this to the second stage of the command”. It does not.

Turns out 2 is just the file descriptor for stderr. That is the stream where programs usually write errors, warnings, and diagnostic noise.

The common three are 0 for stdin, 1 for stdout and 2 for stderr.

Then the rest is simpler:

  • > means redirect what comes before it to somewhere else
  • and /dev/null is a special file that discards all data written to it, basically means throw it away

So 2>/dev/null means: send stderr to the trash (if any). Not to the background. Not into a pipe. Just away from my screen.

It is easier to see with a contrast:

  • | feeds stdout into the next command
  • & at the end runs the command in the background

Neither of those is what 2>/dev/null does.

A concrete example:

find . -name node_modules 2>/dev/null

If find walks into a directory you cannot read, it normally floods the output with “Permission denied” messages. With 2>/dev/null, those disappear while the real matches still print.

One more form worth knowing: >/dev/null 2>&1 drops both stdout and stderr. The 2>&1 part means “send stderr to wherever stdout is currently going” — useful when you want to run something completely silently.

← prev post next post →