What does "2>&1" do in command line?
The 1
denotes standard output (stdout). The 2
denotes standard error (stderr).
So 2>&1
says to send standard error to where ever standard output is being redirected as well. Which since it's being sent to /dev/null
is akin to ignoring any output at all.
tl;dr
Fetch http://www.google.com
in the background and discard both the stdout
and stderr
.
curl http://www.google.com > /dev/null 2>&1 &
is the same as
curl http://www.google.com > /dev/null 2>/dev/null &
Basics
0
, 1
and 2
represent the standard file descriptors in POSIX operating systems. A file descriptor is a system reference to (basically) a file or socket.
0 stdin
1 stdout
2 stderr
Creating a new file descriptor in C can look something like this:
fd = open("data.dat", O_RDONLY)
Most Unix system commands take some input and output the result to the terminal. curl
will fetch whatever is at the specified url (google dot com) and display the result to stdout
.
Redirection
Like you said <
and >
are used to redirect the output from a command to somewhere else, like a file.
For example, in ls > myfiles.txt
, ls
gets the current directory's contents and >
redirects its output to myfiles.txt
(if the file doesn't exist it is created, otherwise overwritten, but you can use >>
instead of >
to append to the file instead). If you run the command above, you will notice nothing is displayed to the terminal. That usually means success in Unix systems. To check this cat myfiles.txt
to display the file contents to the screen.
> /dev/null 2>&1
The first part > /dev/null
redirects the stdout
, that is curl
's output to /dev/null
(more on this ahead) and 2>&1
redirects the stderr
to the stdout
(which was just redirected to /dev/null
so everything will be sent to /dev/null
).
The left side of 2>&1
tells you what will be redirected, and the right side tells you where to. The &
is used on the right side to distinguish stdout (1)
or stderr (2)
from files named 1
or 2
. So, 2>1
would end up creating a new file (if it doesn't exist already) named 1
and dump the stderr
result in there.
/dev/null
/dev/null
is an empty file, a mechanism used to discard everything written to it. So,
curl http://www.google.com > /dev/null
is effectively suppressing curl
's output.
But why is there some stuff still displayed on the terminal?. This is not curl
's regular output, but data sent to the stderr
, used here for displaying progress and diagnostic information and not just errors.
curl http://www.google.com > /dev/null 2>&1
ignores both curl
's output and curl
s progress information. The result is nothing is displayed on the terminal.
Finally
The &
at the end is how you tell the shell to run the command as a job in the background. This causes the prompt to return immediately while the command is run asynchronously behind the scenes. To see the current jobs type jobs
in your terminal. Note this is different from the processes running in your system. To see those type top
in the terminal.
References
- When to use STDERR instead of STDOUT
- Unix - Shell Input/Output Redirections
- Standard Input and Output Redirection