Question

I have a oneliner that inevitably generates some errors that I want to ignore, so i'm trying to redirect it to a file, but it keeps appearing on the screen. This is how it looks like :

command1 | command2 | command3 1> stdout.txt 2> stderr.txt

I also tried sending them both to the same file(with &>), but stderr just doesn't want to and it's very frustrating.

Was it helpful?

Solution 2

You need to redirect stderr for each stage of your pipeline. You can do this as follows:

command1 2>&1 | command 2

or (at least with bash):

 command1 |& command2

(See the pipelines section of the bash man page).

OTHER TIPS

The stderr file descriptor does not go through the pipe, only stdout. You can try that by

commandThatDoesNotExist | less

You'll see nothing in less, but the error will be written on the terminal. To pipe the stderr file descriptor, redirect it to where stdout points

commandThatDoesNotExist 2>&1 | less

Now you see the error in less.

In order to have no stderr on the terminal, you have to redirect the file descriptor of every command to your stderr file:

command 1 2>stderr.txt | command 2 2>stderr.txt | command 3 2>stderr.txt 1>stdout.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top