title: array assignment from command output in ksh
body: <p>I wanted to assign the output of a command to an array, but came across the following problem:</p>
  
  <pre><code>temp=`&lt;some_command&gt;`
  eval set -A array $temp
  print $temp
  print "***${array[0]},${array[1]}***"
  </code></pre>
  
  <p>The command's output is something like:</p>
  
  <pre><code>""
  "D"
  "M"
  "N"
  </code></pre>
  
  <p>In my expectation, <code>${array[0]}</code> should be <code>""</code>, and <code>${array[1]}</code> should be "D".</p>
  
  <p>But, the following is the result:</p>
  
  <pre><code>" "D" "M" "N" # $temp
  *** D M N,*** # ${array[0]},${array[1]}
  </code></pre>
  
  <p>Just can't figure out how the first element of the array sweeps all of the output. And how to fix it?</p>
  
  <p>Update:
  Today, I took another look at the script, and found that the command's output is:</p>
  
  <pre><code>" "
  "D"
  "M"
  "N"
  </code></pre>
  
  <p>Note the first line is <code>" "</code> (there is a space character in it), instead of a pure empty string <code>""</code>.</p>
  
  <p>To start with, the original output was:</p>
  
  <pre><code>&lt;some_command&gt; |sort |uniq -c
  6421 " "
  1090 "D"
     1 "DPV_VALID"
     3 "M"
   588 "N"
    16 "S"
  6382 "Y"
  </code></pre>
  
  <p>I always thought the first line was a pure empty string, e.g. <code>""</code>, didn't realize that there is a space character in the double quotes.</p>
  
  <p>Then, I used <code>awk</code> to get the 2nd field:</p>
  
  <pre><code>&lt;some_command&gt; |sort |uniq -c |awk '{print $2}'
  </code></pre>
  
  <p>The output became:</p>
  
  <pre><code>"
  "D"
  "M"
  "N"
  </code></pre>
  
  <p>That's the cause of later mess-up of the command <code>eval set -A</code> (<code>awk</code> uses space character as the delimiter by default). If it were a pure empty string, it would have worked.</p>
  
  <p>It also works if I re-write the command as:</p>
  
  <pre><code>temp=`&lt;some_command&gt; |sort |uniq -c |sed 's/" "$/""/' |awk '{print $2}'`
  eval set -A array $temp
  </code></pre>
  
  <p>Still, glenn's solution is also pretty neat. Thanks!</p>