title: Getting head to display all but the last line of a file: command substitution and standard I/O redirection
body: <p>I have been trying to get the <code>head</code> utility to display all but the last line of standard input. The actual code that I needed is something along the lines of <code>cat myfile.txt | head -n $(($(wc -l)-1))</code>. But that didn't work. I'm doing this on Darwin/OS X which doesn't have the nice semantics of <code>head -n -1</code> that would have gotten me similar output.</p>
  
  <p>None of these variations work either.</p>
  
  <pre><code>cat myfile.txt | head -n $(wc -l | sed -E -e 's/\s//g')
  echo "hello" | head -n $(wc -l | sed -E -e 's/\s//g')
  </code></pre>
  
  <p>I tested out more variations and in particular found this to work:</p>
  
  <pre><code>cat &lt;&lt;EOF | echo $(($(wc -l)-1))
  &gt;Hola
  &gt;Raul
  &gt;Como Esta
  &gt;Bueno?
  &gt;EOF
  3
  </code></pre>
  
  <p>Here's something simpler that also works.</p>
  
  <pre><code>echo "hello world" | echo $(($(wc -w)+10))
  </code></pre>
  
  <p>This one understandably gives me an illegal line count error. But it at least tells me that the <code>head</code> program is not consuming the standard input before passing stuff on to the subshell/command substitution, a remote possibility, but one that I wanted to rule out anyway.</p>
  
  <pre><code>echo "hello" | head -n $(cat &amp;&amp; echo 1)
  </code></pre>
  
  <p>What explains the behavior of <code>head</code> and <code>wc</code> and their interaction through subshells here? Thanks for your help.</p>