title: using head -n1 to get first match of grep on osx
body: <p>I am trying to use <code>head -n1</code> to get just the first grep match (which is suggested a few places)</p>
  
  <p>I expect this to work</p>
  
  <pre><code>$ printf "x=0;\nwhile True:\n x+=1\n print x" | python | grep -w 333 | head -n1
  </code></pre>
  
  <p>(pipe something that goes on forever into a grep command that will select a single line, and then take the first line from that output)</p>
  
  <p>However, there is no output, and it never stops.</p>
  
  <p>This works as expected: (take first line of infinite output, without grep)</p>
  
  <pre><code>$ printf "x=0;\nwhile True:\n x+=1\n print x" | python | head -n1
  1
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 4, in &lt;module&gt;
  IOError: [Errno 32] Broken pipe
  </code></pre>
  
  <p>And this works: (grep the output and get a single match)</p>
  
  <pre><code>$ printf "x=0;\nwhile True:\n x+=1\n print x" | python | grep -w 333
  333
  </code></pre>
  
  <p>(and never exits)</p>
  
  <p>However, this combination doesn't give me what I would expect:</p>
  
  <pre><code>$ printf "x=0;\nwhile True:\n x+=1\n print x" | python | grep -w 333 | head -n1
  </code></pre>
  
  <p>(never prints anything and never exits)</p>