Shell: Differences in Output Forwarding

SysAdminTechnology



Differences between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1…

I/O Redirection Theory

For background:

number 1 = standard out (i.e. STDOUT) number 2 = standard error (i.e. STDERR) If a number isn’t explicitly given, then number 1 is assumed by bash.

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.

|&

Abbreviation for 2>&1 |, which was added in Bash 4.

&>/dev/null

Abbreviation for >/dev/null 2>&1, which redirects the file descriptor 2 (STDERR) and the descriptor 1 (STDOUT) to /dev/null.

>/dev/null

Abbreviation for 1>/dev/null, which redirects the file descriptor 1 (STDOUT) to /dev/null.


Trackbacks

Trackback specific URI for this entry

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Submitted comments will be subject to moderation before being displayed.

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Gravatar, Twitter, Pavatar, Identica author images supported.
Markdown format allowed