I would like to know what the &
means in the statement:
2>&1 > /dev/null
It's redirecting standard error to standard output and then to Bitbucket, but what's &
in it?
Can I use it like the following?
2>1 >/dev/null
It means that stderr ( 2 - containing error messages from the executed command or script) is redirected ( >& ) to stdout ( 1 - the output of the command) and that the latter is being redirected to /dev/null (the null device). This way you can suppress all messages that might be issued by the executed command.
1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").
/dev/null is a standard file that discards all you write to it, but reports that the write operation succeeded. 1 is standard output and 2 is standard error. 2>&1 redirects standard error to standard output.
/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading. This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.
The &
means file descriptor1. So 2>&1
redirects standard error to whatever standard output currently points at, while 2>1
redirects standard error into a file called 1
.
Also, the redirects happen in order. So if you say 2>&1 >/dev/null
, it redirects standard error to point at what standard output currently points at (which is probably a noop), then redirects stdout to /dev/null. You probably want >/dev/null 2>&1
.
1In the context of a file redirect -- when it is the next token immediately after a >
or <
. In other contexts it means something else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With