Is there a way to pipe the output of a command and direct it to the stdout as well?
So for example, fortune prints a fortune cookie to stdout and also pipes it to next command:
$ fortune | tee >(?stdout?) | pbcopy "...Unix, MS-DOS, and Windows NT (also known as the Good, the Bad, and the Ugly)." (By Matt Welsh)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
tee always writes to its standard output. If you want to send the data to a command in addition to the terminal where the standard output is already going, just use process substitution with that command. (Note that in spite of starting with >, process substitution does not redirect standard output, the tee command sees it as a parameter.)
fortune | tee >(pbcopy)
Method 2
Your assumption:
fortune | tee >(?stdout?) | pbcopy
won’t work because the fortune output will be written to standard out twice, so you will double the output to pbcopy.
In OSX (and other systems support /dev/std{out,err,in}), you can check it:
$ echo 1 | tee /dev/stdout | sed 's/1/2/' 2 2
output 2 twice instead of 1 and 2. tee outputs twice to stdout, and tee process’s stdout is redirected to sed by the pipe, so all these outputs run through sed and you see double 2 here.
You must use other file descriptors, example standard error through /dev/stderr:
$ echo 1 | tee /dev/stderr | sed 's/1/2/' 1 2
or use tty to get the connected pseudo terminal:
$ echo 1 | tee "$(tty)" | sed 's/1/2/' 1 2
With zsh and multios option set, you don’t need tee at all:
$ echo 1 >/dev/stderr | sed 's/1/2/' 1 2
Method 3
cuonglm said it all.
Just try:
fortune | tee "$(tty)" | pbcopy
tty should resolve to actual pseudo terminal (like /dev/pts/99) in interactive session (i.e. in terminal), or no a tty in batch, at and daemon.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0