This is mostly a shell question regarding standard descriptors (stdout, stderr) and redirections.
To achieve what you want, you should not write cmd 2>&1 /dev/null
nor cmd 2>&1 >/dev/null
but just write: cmd >/dev/null 2>&1
Mnemonics:
The intuition to easily think of this >
syntax is:
>/dev/null
can be read: STDOUT := /dev/null
2>&1
can be read: STDERR := STDOUT
This way, the fact that 2>&1
must be placed afterwards becomes clear.
(As an aside, redirecting both stderr to stdout to a pipe is a bit different, and would be written in the following order: cmd1 2>&1 | cmd2
)
Minimal complete example to test this:
$ cmd() { echo "stdout"; echo >&2 "stderr"; }
$ cmd 2>&1 >/dev/null # does no work as intended
stderr
$ cmd >/dev/null 2>&1 # ok
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…