The -q flag was causing grep to close the stdin as the first match is found, causing the exit code of the programs in the pipe to return non-zero, as stdout is closed.