Using bash i'm trying to grep a string coming from /dev/urandom something like this:

tr -cd '[:lower:]' < /dev/urandom | grep -o 'test'

Also tried:

tr -cd '[:lower:]' < /dev/urandom | grep -o 'test' | head -1

I guess grep keeps waiting for tr to finish, because even redirecting the output to a file like > /tmp/testfile nothing is written to the file.

However if parsed to head before grep, it works

tr -cd '[:lower:]' < /dev/urandom | head -c 10000 | grep -o 'test'

Is there a way to make it work while stdin is still running?, and if not, why is this behavior from grep, not to give an output if input hasn't finished?

  • 1
    What are you trying to achieve? You're reading a stream of random characters, and expecting to see some particular fixed string, why?
    – ilkkachu
    Commented Jul 25, 2019 at 8:34
  • 1
    A grep -o able to search through lines of infinite length could probably be implemented with pcre2partial, but I'm not aware of any grep-like tool making use of that.
    – user313992
    Commented Jul 25, 2019 at 8:55

1 Answer 1


As explained in the comments, grep processes lines:

The grep utility shall search the input files, selecting lines matching one or more patterns

Your input doesn’t contain any newline characters, so grep waits endlessly for a line to check against the pattern specified in its arguments.

head -c 10000 fixes that by “ending” the line after 10,000 characters. Another solution is to allow newlines in the input, as suggested by mosvy:

tr -cd '[:lower:]\n' < /dev/urandom | grep -o 'test'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .