Redirect and append output to different files

Hi,

I need to redirect the command output to file1 and append the output to file2 at the same time.

As of now am using below,

command | tee -a file2 | tee file1

problem with the above is, the status is always 0, irrespective of "command"

For example,

ls -l file | tee -a file2 | tee file1

If file does not exist also the return status is 0.

Any suggestions ..

TIA

As ususal: what OS and shell versions?

In (recent) bash , set the pipefail option:

$ set -o pipefail
$ ls -l fxxxx | tee -a file2 | tee file1
ls: cannot access 'fxxxx': No such file or directory
$ echo $?
2

 
2 Likes

Thanks a lot. Its working fine..

OS: RHEL7.6