`tee` for commands
It's straightforward in shells that support process substitution, e.g. bash
$ echo foo | tee >(xsel)
foo
$ xsel -o
foo
Otherwise, you could use a FIFO (although it lacks convenience)
$ mkfifo _myfifo
$ xsel < _myfifo &
$ echo bar | tee _myfifo
bar
$ xsel -o
bar
[1] + Done xsel 0<_myfifo
$
The direct analogue of "tee for commands" is the pee
command from moreutils (tee
, but with pipes). Its arguments are used as commands to run, not as paths, and they get the input piped to them rather than written to file. All of the commands are given the standard input you piped to pee
as their own.
Using pee
, you can get the result you wanted by telling it to run both xsel
and cat
with the input.
echo foo | pee xsel cat
There is an extra cat
process floating around there to do the output, which isn't really a problem for your use case but could be less ideal other times. This works with any shell, unlike process substitution, but of course it does require an extra (probably less-common) tool installed.
You can give more complex commands by quoting them: pee "xsel --display :1" cat
. They're run with sh
, so you do have to be careful about shell metacharacters.