Redirect only stderr to /dev/null while redirecting stdout to a file (zsh)

536 Views Asked by At

I have a command cmd that I want to use in a zsh script in the form:

cmd -opt val > info.txt

but I want to redirect stderr from that one line to /dev/null.

Evidently the following does not work:

cmd -opt val > info.txt > /dev/null

How to do it?

Added: Note that I do not want any regular output written to the terminal; all regular output should be written into the specified file info.txt.

1

There are 1 best solutions below

0
chepner On BEST ANSWER

> and 2> are independent and can appear in the same command.

cmd -opt val > info.txt 2> /dev/null

Standard output is redirected to info.txt, and standard error is redirected to /dev/null.