I have the following shell script:
error=$(mkdir test 2>&1)
I know that the variable 'error' will get the error result of the mkdir command if there is an error, but I can't understand how 2>&1 works, could someone explain it? Thanks!
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
some_command and the output of that is returned and stored in the variable
Now, normally, programs send "normal output" to the "standard out" stream (
stdout, file handle #1) and error messages to the "standard error" stream (
stderr, file handle #2).
The redirection semantic
2>&1 means (roughly speaking; it's a little more complicated under the covers) "send
stdout". So error messages and output messages are mixed together.
So we can combine the two:
will return the output and the error messages and put them into
In your case
error="$(mkdir test 2>&1)"
$error will contain the output (which is empty) and the error (which may contain a string if an error occurs). The result is that
$error will contain any error message from the
We can see this in action.
$ error="$(mkdir /)" mkdir: cannot create directory '/': File exists $ echo "$error" $ error="$(mkdir / 2>&1)" $ echo "$error" mkdir: cannot create directory '/': File exists
In the first case the error message is printed immediately because it's sent to
stderr, and the variable is empty. In the second case we redirect stderr to stdout and so it is captured and stored in the
It means, poetically speaking: send the error stream to the same "channel" as the normal output.
2 stands for error(s).
1 is the main-line, or the standard output.
&1 indicates the way, where the standard output goes. By default, it goes to the screen, and in this substitution context:
$( ), it goes to the variable, becomes its value. And
> is redirection. All together
2>&1 means: redirect errors the way of standard output.
It captures stderr and stdout, by redirecting stderr to stdout (taking effect after the
$() command substitution has connected stdout to a pipe to the parent shell).
>&1 makes the difference between referencing another file descriptor instead opening a file called
2> means to redirect stderr, as usual.