title: how to count the number of lines in a variable in a shell script
body: <p>Having some trouble here.  I want to capture output from ls command into variable.  Then later use that variable and count the number of lines in it.  I've tried a few variations</p>
  
  <p>This works, but then if there are NO .txt files, it says the count is 1:</p>
  
  <pre><code>testVar=`ls -1 *.txt`
  count=`wc -l &lt;&lt;&lt; $testVar`
  echo '$count'
  </code></pre>
  
  <p>This works for when there are no .txt files, but the count comes up short by 1 when there are .txt files:</p>
  
  <pre><code>testVar=`ls -1 *.txt`
  count=`printf '$testVar' | wc -l`
  echo '$count'
  </code></pre>
  
  <p>This variation also says the count is 1 when NO .txt files exist:</p>
  
  <pre><code>testVar=`ls -1 *.txt`
  count=`echo '$testVar' | wc -l`
  echo '$count'
  </code></pre>
  
  <p>Edit: I should mention this is korn shell.</p>