title: Bash function with grep, using if and then statements
body: <p>I have been trying to build a function that will allow me to perform a dynamic grep search - dynamic in that if more than one variable is specified upon calling the function, then it will pass the additional variable through grep.  I should never really need more than 2 variables, but would be interested in learning how to do that as well.</p>
  
  <p>example of desired functionality:</p>
  
  <pre><code>&gt; function word1 word2
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>(
     cd /path/folder;
     less staticfile | grep -i word1 | grep -i --color word2
  )
  </code></pre>
  
  <p>The above works great if there will always be two words.  i'm trying to learn how to use if and then statements to allow me to be flexible in the number of variables.</p>
  
  <pre><code>function () 
  {
     if [ ! "$#" -gt "1" ];
          then
             (
                cd ~/path/folder;
                less staticfile | grep -i $1 | grep --color -i $2
             )
          else
             (
                cd ~/path/folder;
                less staticfile | grep --color $1
             )
     fi
  }
  </code></pre>
  
  <p>When I run this function, it seems to do the opposite - </p>
  
  <pre><code>&gt; function word1
  </code></pre>
  
  <p>returns an error because it is using the "then" statement for some reason but has nothing to insert into the second grep call.</p>
  
  <pre><code>&gt; function word1 word2
  </code></pre>
  
  <p>only greps "word1" - therefore is using the "else" statement.
  What am i doing wrong?  Is there any easier way to do this?</p>