I have piped a line in bash script and want to check if the pipe has data, before feeding it to a program.
Searching I found about
test -t 0 but it doesn't work here. Always returns false.
So how to be sure that the pipe has data?
echo "string" | [ -t 0 ] && echo "empty" || echo "fill"
echo "string" | tail -n+2 | [ -t 0 ] && echo "empty" || echo "fill"
Unlike Standard/canonical way to test whether foregoing pipeline produced output? the input needs to be preserved to pass it to the program. This generalizes How to pipe output from one process to another but only execute if the first has output? which focuses on sending email.