Oh my, that's embarrasing. I seem to have copy-pasted badly - what I get when I make my testing overly complicated. Here's a simpler testing suite:
I would think that by asking for 2 matching lines and 3 lines of trailing context, I would get 3 lines no matter what, even if there was another coincidental match? #! /bin/bash test=$'1 search 2 search 3 search 4 context 5 context 6 context' wanted=$'1 search 2 search 3 search 4 context 5 context' received=$'1 search 2 search' [[ $( echo "$test" | grep -m 2 -A 3 search ) == $wanted ]] && echo Pass || echo Fail [[ $( echo "$test" | grep -m 2 -A 3 search ) == $received ]] && echo Should not happen but it does # I'm using: # grep (GNU grep) 2.27