How to redirect command line outputs to a file, but still show them in the command line?

StackOverflow https://stackoverflow.com/questions/22866027

  •  27-06-2023
  •  | 
  •  

Question

In tcsh I want to redirect command line outputs to a file, but I still want to show them in the command line.

Did a little bit search that

./MyCommand.sh 2>&1 | tee /tmp/Output.txt

should do the job. But I got an error like:

Ambiguous output redirect
Was it helpful?

Solution

Use of 2>&1 to combine stderr and stdout works only in bash and sh. It does not for csh or tcsh. A work around is suggested at Redirect stdout to stderr in tcsh.

OTHER TIPS

In bash instead of 2>&1 I use |& Not sure how this plays out for tcsh, but this question isn't currently tagged for it and hoping this helps someone else.

According to this redirect stderr to stdout in c shell you can't do this in csh which tcsh extends which could be related

It isn't clear from the question if you want to redirect stdout only, or stdout and stderr.

Using | will redirect stdout to tee (which outputs it to a file and to terminal), leaving stderr untouched (so it only goes to terminal):

./MyCommand.sh | tee /tmp/Output.txt

Using |& will "merge" stdout and stderr, and tee will redirect both to file and to terminal:

./MyCommand.sh |& tee /tmp/Output.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top