Reading the same stdin with two commands in bash
I would like to pipe an output of to two separate commands <2,3> in bash. What is the best way of doing this? At the moment, I have following script:
command source > output
command2 output &
command3 output &
The output file is ~100G and a suboptimal way would be to pipe to commands 2 and 3 separately. I would think it is possible to do even more efficiently.
Solution 1:
In bash: command source | tee >(command2) >(command3)
From this stackoverflow question. I haven't tried this with ginormous outputs.
Solution 2:
Other answers introduce the concept. Here is an actual demonstration:
$ echo "Leeroy Jenkins" | tee >(md5sum > out1) >(sha1sum > out2) > out3
$ cat out1
11e001d91e4badcff8fe22aea05a7458 -
$ echo "Leeroy Jenkins" | md5sum
11e001d91e4badcff8fe22aea05a7458 -
$ cat out2
5ed25619ce04b421fab94f57438d6502c66851c1 -
$ echo "Leeroy Jenkins" | sha1sum
5ed25619ce04b421fab94f57438d6502c66851c1 -
$ cat out3
Leeroy Jenkins
Of course you can > /dev/null
instead of out3.