Extended Brain Storage

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:

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>&-, 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 and 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 and Study Materials

For further details, please see the Advanced Bash-Scripting Guide and stackexchange.com.

Tags: #shell #bash #ksh #sh #zsh

⏴ Previous Post Next Post ⏵