Shell – Why isn’t apt-cache policy output piped

aptpipeshell

Cannot get why

$ apt-cache policy foo
N: Unable to locate package foo

but

$ apt-cache policy foo 2>&1 | grep .

is empty.

Where in the latter call am I doing the wrong assumption?

The original task: I need to process the apt-cache policy output presumably 🙂

UPD:

foo used in my example may be substituted with any package name that does not exist in your apt-get index.

UPD 2:

there is an answer with a workaround. Additional +50 bounty will be awarded to anyone who explains why the 2>&1 solution does not work.

Best Answer

If stdout is not a tty (i.e. it's a regular file or a pipe) and if no --quiet option has been specified, apt-cache acts as if you had passed it --quiet=1. A workaround is to pass it a --quiet=0 option.

$ apt-cache --quiet=0 policy foo 2>&1 | grep .
N: Unable to locate package foo
Related Question