I have many shell and python scripts on my crontab that ending either with:
command.sh > /dev/null 2>&1
or
command.sh 2>&1 >/dev/null
I know that:
>
is for redirect
/dev/null
is a black hole where any data sent, will be discarded
2
is the file descriptor for Standard Error
>
is for redirect
&
is the symbol for file descriptor (without it, the following 1 would be considered a filename)
1
is the file descriptor for Standard Out
Therefore command.sh >/dev/null 2>&1
redirects the output of my program to /dev/null. Include both the Standard Error and Standard Out.
both have the same result and work fine, but why do some use the first type and some use the other?
The commands are performing two redirects:
> /dev/null
redirects Standard Output to/dev/null
2>&1
redirects Standard Error to Standard OutputAs you correctly guessed, the global effect is that both Standard Output and Standard Error are redirected to
/dev/null
.The two redirects are both meant to be interpreted by the shell, not by the actual program called; thus they are appended to the end of the command line. They are logically distinct, although their effects are cumulative; thus they can be specified in any order you prefer.
TL;DR: the two commands are completely equivalent, the difference is purely cosmetical.