Shell: Difference in Output Forwarding

To remind the difference among: 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

I/O Redirection Theory

For background:

  • a number 1 = standard out (i.e. STDOUT)
  • a number 2 = standard error (i.e. STDERR)

If a number isn’t explicitly given, then number 1 is assumed by bash (shell).

Functions

2>&-

The general form of this one is M>&-, where “M” is a file descriptor number. It closes output for whichever file descriptor is referenced, i.e. “M”.

2>/dev/null

The general form of this one is M>/dev/null, where “M” is a file descriptor number. It redirects the file descriptor, “M”, to /dev/null.

2>&1

The general form of this one is M>&N, where “M” & “N” are file descriptor numbers. It combines the output of file descriptors “M” and “N” into a single stream.

|&

This is just an abbreviation for 2>&1 |, which was added in Bash 4.

&>/dev/null

This is just an abbreviation for >/dev/null 2>&1, which redirects the file descriptor 2 (STDERR) and the descriptor 1 (STDOUT) to /dev/null.

>/dev/null

This is just an abbreviation for 1>/dev/null, which redirects the file descriptor 1 (STDOUT) to /dev/null.

Reference

For reference, see the Advanced Bash-Scripting Guide and stackexchange.com.

Advertisements
This entry was posted in BSD, Linux, Security, Server and tagged , , , , , , , , , . Bookmark the permalink.