While not a direct answer to your specific question, you will need to run something like
read -t 3 variable < <( tail -f logfile.log | grep "something" )
in order for the newly set value of variable
to be visible after the pipeline completes. See if this times out as expected.
Since you are simply using read
as a way of exiting the pipeline after a fixed amount of time, you don't have to worry about the scope of variable
. However, grep
may find a match without printing it within your timeout due to its own internal buffering. You can disable that (with GNU grep
, at least), using the --line-buffered
option:
tail -f logfile.log | grep --line-buffered "something" | read -t 3
Another option, if available, is the timeout
command as a replacement for the read
:
timeout 3 tail -f logfile.log | grep -q --line-buffered "something"
Here, we kill tail
after 3 seconds, and use the exit status of grep
in the usual way.