2

I'm trying to redirect stderr to stdout and then pipe it but I think I'm missing something basic here.

The command and output to be piped:

$ apt-cache show contractor
N: Can't select versions from package 'contractor' as it is purely virtual
N: No packages found

Grep does not work - must be outputting to stderr:

$ apt-cache show contractor |grep virtual

OK Let's redirect stderr to stdout:

$ apt-cache show contractor 2>&1 |grep virtual

Nope this does not work, why?

Confirm which file descriptor the command is using:

$ apt-cache show contractor 1>t ;cat t

$ apt-cache show contractor 2>t ;cat t
N: Can't select versions from package 'contractor' as it is purely virtual
N: No packages found

Confirmed it's using stderr.

Something to do with ordering of redirects?

$ apt-cache show contractor |cat 2>&1

Nope

$ apt-cache show contractor 2>&1 |cat 2>&1

Nope

How to redirect stderr to stdout then pipe?

1 Answer 1

5

That's because apt-cache will become "quiet" and not print those lines when its stdout is not a tty.

You can override that determination by setting its "quietness" to 0:

$ apt-cache -q=0 show contractor 2>&1 | grep virtual
N: Can't select versions from package 'contractor' as it is purely virtual

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .