Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>"When referencing a variable, it is generally advisable to enclose its name in double quotes"</em> -- <a href="http://tldp.org/LDP/abs/html/quotingvar.html" rel="nofollow">http://tldp.org/LDP/abs/html/quotingvar.html</a></p> <p><code>if [ $step -ge 1 ] &amp;&amp; [ $step -le 52 ]</code> can be replaced as </p> <p><code>if [ "$step" -ge 1 -a "$step" -le 52 ]</code></p> <p><code>if [[ "$step" -eq 0 ]]</code> can be replaced as <code>if [ "$step" -eq 0 ]</code></p> <p>Also, suppose you have the following script:</p> <pre><code>#!/bin/bash if [ $x -eq 0 ] then echo "hello" fi </code></pre> <p>You get this error when you run the script -- <code>example.sh: line 2: [: -eq: unary operator expected</code></p> <p>But using <code>if [ "$x" -eq 0 ]</code></p> <p>You get a different error when you run the script -- <code>example.sh: line 2: [: : integer expression expected</code></p> <p>Thus, it is always best to put variables inside quotes...</p> <p><code>if [[ .... ]]</code> syntax is particularly useful when you have <code>regex</code> in the condition statement -- <a href="http://honglus.blogspot.com/2010/03/regular-expression-in-condition.html" rel="nofollow">http://honglus.blogspot.com/2010/03/regular-expression-in-condition.html</a></p> <p>EDIT: When we deal with strings --</p> <pre><code>#!/bin/bash if [ $x = "name" ] then echo "hello" fi </code></pre> <p>You get this error when you run the script -- <code>example.sh: line 2: [: =: unary operator expected</code></p> <p>But, if you use <code>if [ "$x" = "name" ]</code> it runs fine (i.e. no errors ) and <code>if</code> statement is evaluated as <code>false</code>, as value of <code>x</code> is <code>null</code> which does not match <code>name</code>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload