title: Bash script error [: !=: unary operator expected
body: <p>In my script I am trying to error check if the first and only argument is equal to <code>-v</code>, but it is an optional argument. I use an <em>if</em> statement, but I keep getting the unary operator expected error.</p>
  <p>This is the code:</p>
  <pre><code>if [ $1 != -v ]; then
     echo &quot;usage: $0 [-v]&quot;
     exit
  fi
  </code></pre>
  <p>To be more specific:</p>
  <p>This part of the script above is checking an optional argument and then after, if the argument is not entered, it should run the rest of the program.</p>
  <pre><code>#!/bin/bash
  
  if [ &quot;$#&quot; -gt &quot;1&quot; ]; then
     echo &quot;usage: $0 [-v]&quot;
     exit
  fi
  
  if [ &quot;$1&quot; != -v ]; then
     echo &quot;usage: $0 [-v]&quot;
     exit
  fi
  
  if [ &quot;$1&quot; = -v ]; then
     echo &quot;`ps -ef | grep -v '\['`&quot;
  else
     echo &quot;`ps -ef | grep '\[' | grep root`&quot;
  fi
  </code></pre>