> /dev/null 2>&1
saw someone write " > /dev/null 2> &1" after commands
I know > /dev/null send it to a blackhole. But why add 2 > &1. I remembered it might have to do with standard error or something. What does it mean. and what other arguments do people put /dev/null. |
The "2>&1" is NOT an argument to /dev/null!
Just as "> /dev/null" redirect standard output to /dev/null, "2> &1" redirect standard error to the same file as standard output (in this case to /dev/null). So ">/dev/null 2>&1" is added after commands if you are not interested in the output from the command. |
/dev/null is a special file that automatically (and efficiently) deletes whatever is written to it.
The > operator tells the shell to take the output (known as the standard output stream) of a command, and save it to the given file. 2> is another form of this, and means to take the error messages from the command (the standard error stream) and save it to the given file instead. &1 is a special file that means the standard output. So 2>&1 means to redirect the standard error stream to the standard output, in this case /dev/null which causes the streams to be deleted. You can also have other variations like 1>&2, which would take the standard output and redirect it to standard error. 2>&1 > also has the short-hand form &>, meaning merge standard output and standard error and redirect to You can also use >> instead of > meaning to append to a file instead of deleting it (not useful with /dev/null). Also, none of the above are actually arguments to the program; they are all interpreted by the shell, meaning that they can be used with any program. See Code:
man bash |
All times are GMT -5. The time now is 05:10 PM. |