Photo by Tim Mossholder / Unsplash

Redirection Operators for Testers

Tools Oct 1, 2025 (Oct 2, 2025) Loading...

Redirection operators (|, >, >>, <) let you chain Unix commandline commands together, and move information in and out of the chain.

  • | chains commands i.e. sends the output of one to the input of the next. More precisely, it sends the stdout of one the the stdin of the next, sending stderr to the terminal.
  • < sends input into commands – so you can (generally)
    • doathing < file.txt and
    • < file.txt doathing
    • which works similarly to the common chain cat file.txt | doathing
  • > and >> and >& redirects the output. (> overwrites a file while >> appends to a file, and >& redirects to a file descriptor – typically a stream).

💡
If the > has a space before it, it redirects stdout, leaving stderr to throw errors at you in the terminal.
💡
While > and >> are related,
|(pipe) and || (OR) are not.
&(file descriptor) and && (AND) are not, either.

Outputs and File descriptors

tl;dr 2> sends stderr to a file and >&2 sends an output to stderr

Commands output (what you expect) to stdout, which is typically the terminal window, and (problems) to stderr, which is also the terminal window. These are streams, and they're mixed as they are produced. find's splurge of mingled filenames and permission errors is a perfect example of this.

You can separate and redirect these. stdout is file 1 within the command, so you catch stdout in file.txt with 1> file.txt . stderr is file 2, and you catch stderr in fubar with 2> fubar.

Numbered file descriptors let you refer to these on the output: stdin &0, stdout &1, stderr &2 . Note that while N> means send N somewhere, >N means send to a file called N, and you need >&N to tell the machine to use the descriptor.

Common patterns:

  • > something sends stdout to something, leaving stderr on-screen in your terminal.
  • 2>something sends stderr to something.
  • 2>&1 sends stderr to stdout
  • &>something redirects both stdout and stderr to something
  • > /dev/null or 1> /dev/null to throw stdout away, 2> /dev/null to throw stderr away.
⚠️
curl 's approach to stderr is unusual. If stderr is the terminal (i.e. if you're using it manually) it will report !progress (dynamically)! and errors. If not, (i.e. you're using it within a command) it'll just report errors.

The numbers mean...

  • 0 represents standard input stdin
  • 1 represents standard output stdout, > is equivalent to 1>. &1 is equivalent to /dev/stderr
  • 2 represents standard error stderr. &2 is equivalent to /dev/stdout.

Higher numbers only make sense if they’ve been opened explicitly

More thoughts

What does 2>>&1 do? Nothing: it is bad syntax

While 2>&1 sends errors to stdout, the order is not guaranteed: streams may be interleaved, but one does not overwrite the other.

Member reactions

Reactions are loading...

Sign in to leave reactions on posts

Tags

Comments

Sign in or become a Workroom Productions member to read and leave comments.

James Lyndsay

Getting better at software testing. Singing in Bulgarian. Staying in. Going out. Listening. Talking. Writing. Making.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.