The following command processes the output of the pipe twice by using tee
:
echo -e "ALPHA\nBRAVO" | tee >(head -n 1) >(tail) >/dev/null
As expected it outputs:
ALPHA
ALPHA
BRAVO
When trying to call it with watch like this:
watch 'echo -e "ALPHA\nBRAVO" | tee >(head -n 1) >(tail) >/dev/null'
It returns:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `echo -e "ALPHA\nBRAVO" | tee >(head -n 1) >(tail) >/dev/null'
How should I escape my command to use it with watch?
Process substitutions are an extension, not all sh implementations support them. You can use redirections to circumvent this restriction though. Like
watch '{ { printf '\''ALPHA\nBRAVO\n'\'' |
tee /proc/self/fd/3 |
head -n 1 >&4
} 3>&1 | tail >&4
} 4>&1'
Just note that this is no more portable than doing watch 'bash -c ...'
.