title: How to get the longest and shortest lines from a standard input in bash
body: <p>I'm writing a script to get the longest and shortest lines from a standard input without success. </p>
  
  <p>To invoke the program you should use something like:</p>
  
  <pre><code>LongShort.sh /tmp/filetest.txt
  </code></pre>
  
  <p>My script currently look like this.</p>
  
  <pre><code>#! /bin/bash LongShort.sh
  
  longest=0
  shortest=0
  
  
  while read line
  do
     linelength=${#line}
     if [ "$linelength" -gt "$longest" ]
     then
       $longest = $line
       $longestlength = $linelength
     elif [ "$linelength" -lt "$shortest" ]
       then
          $shortest = $line
          $shortestlength = $linelength
     fi
  done &lt; $2
  
  echo $longestlength $longest
  echo $shortestlength $shortest
  </code></pre>
  
  <p>Does anyone see what's wrong?</p>