9

I would like to dispay the ouput (STDOUT) and also pipe to the next command. I know "tee" to display the result and write to a file, but instead to write to a file, I want to pipe to another command.

Example :

$ command1 --option1 --option2 | MAGICCOMMAND | jq -e '.returnCode'

I would have the output from "command1 --option1 --option2" and "jq -e ..."

Kusalananda
  • 333,661
LMJ
  • 93

2 Answers2

7

In the bash shell, you could use tee with a process substitution like this:

command1 --option1 --option2 | tee >( jq -e '.returnCode' )

This would write the output of command1 to tee, which would duplicate it to standard output and also to the standard input of jq.

You could also do

command1 --option1 --option2 | tee /dev/stderr | jq -e '.returnCode'

which would put the output of command1 onto the standard error stream, while piping its duplicated output to jq. It depends on what you want to achieve.

Kusalananda
  • 333,661
3

| tee /dev/tty | will write standard input to the console (on most systems) while still passing it on as standard output to the next command.

You might sometimes prefer /dev/stderr to write to standard error instead. For jq specifically, this may be sufficient:

| jq 'stderr | ...' 

The stderr/0 function writes its input to standard error and passes it on to the next filter. It will format the object (condensed, no highlighting), however, so that may or may not be what you want.

Michael Homer
  • 76,565