title: Speed up bash script while loop
body: <p>This script redirects the output of a hexdump to a text file and then loops through it printing out rows. There are exactly 374371 rows of data in the file that look like this:</p>
  <pre><code>1a 03 1a 03 4a 03 57 03
  4b 03 44 03 1e 03 09 04
  10 03 19 03 40 03 ae 03
  1e 03 26 03 33 03 ad 03
  10 03 84 03 43 03 62 03
  </code></pre>
  <p>Here is the script:</p>
  <pre><code>if [ ! -d ./temp ]; then
    mkdir ./temp
    mount -t tmpfs -o size=128m tmpfs ./temp
  
    if [[ -s ./temp/samples.txt || ! -f ./temp/samples.txt ]]; then
      hexdump -e '8/1 &quot;%02x &quot; &quot;\n&quot;' samples.bin &gt; ./temp/samples.txt
      echo &quot;              ch0    ch1    ch2    ch3&quot;
      x=0
      while read line;do
        echo &quot;Sample &quot;$x&quot;:        &quot;$(echo $line | awk '{print &quot;0x&quot;$1$2&quot;  0x&quot;$3$4&quot;  0x&quot;$5$6&quot;  0x&quot;$7$8}')
        x=$((x+1))
      done&lt;./temp/samples.txt
    fi
    umount ./temp
    rm -Rf ./temp
  fi
  </code></pre>
  <p>The output looks like:</p>
  <pre><code>                ch0    ch1    ch2    ch3
  Sample 0:       0x1a03 0x1a03 0x4a03 0x5703
  Sample 1:       0x4b03 0x4403 0x1e03 0x0904
  Sample 2:       0x1003 0x1903 0x4003 0xae03
  Sample 3:       0x1e03 0x2603 0x3303 0xad03
  Sample 4:       0x1003 0x8403 0x4303 0x6203
  Sample 5:       0xe003 0x1603 0x3403 0xc403
  Sample 6:       0xf802 0x3b03 0x5303 0x6103
  Sample 7:       0x1003 0x1503 0x4203 0x5803
  Sample 8:       0x2303 0x1f03 0x5703 0x6203
  Sample 9:       0x1703 0x7303 0x3103 0x3303
  Sample 10:      0x1403 0xff02 0x3003 0x5103
  Sample 11:      0x5f03 0x4203 0x4703 0x7e03
  Sample 12:      0xba03 0x2603 0x3503 0xa003
  </code></pre>
  <p>Even with tmpfs, the script doesn't run any faster than just reading from disk. Is there a way to make this script run any faster?</p>