Score:0

whats the diffrence between "command.sh > /dev/null 2>&1" vs "command.sh 2>&1 >/dev/null"

us flag

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?

Score:1
ng flag

The commands are performing two redirects:

  • > /dev/null redirects Standard Output to /dev/null
  • 2>&1 redirects Standard Error to Standard Output

As 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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.