Shell: Difference in Output Forwarding
To remind the magic behind: 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 is not explicitly given, then number 1 is assumed by bash
(shell).
Functions
2>&-
The general form of this one is
M>&-
, whereM
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
, whereM
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
, whereM
andN
are file descriptor numbers. It combines the output of file descriptorsM
andN
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 descriptor2
(stderr
) and the descriptor1
(stdout
) to/dev/null
.
>/dev/null
This is just an abbreviation for
1>/dev/null
, which redirects the file descriptor1
(stdout
) to/dev/null
.
Reference and Study Materials
For further details, please see the Advanced Bash-Scripting Guide and stackexchange.com.