Bash Expressions: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * bash =Difference between <tt>-a</tt> and <tt>&&</tt>= If want to combine the results of multiple command executions in an <tt>if</tt> conditi...")
 
Line 5: Line 5:
=Difference between <tt>-a</tt> and <tt>&&</tt>=
=Difference between <tt>-a</tt> and <tt>&&</tt>=


If want to combine the results of multiple command executions in an <tt>if</tt> condition, use <tt>&&</t>, not <tt>-a</tt>.
If want to combine the results of multiple command executions in an <tt>if</tt> condition, use <tt>&&</tt>, not <tt>-a</tt>.


Example:
Example:
Line 12: Line 12:
if grep "something" /file1.txt && grep "something" /file2.txt' then
if grep "something" /file1.txt && grep "something" /file2.txt' then
     # "something" exists in both files ...
     # "something" exists in both files ...
fi
</pre>
<tt>-a</tt> should be used in <tt>test</tt> expressions:
<pre>
if [ -f /file.txt -a -x /file.txt ]; then
  ...
fi
fi
</pre>
</pre>

Revision as of 02:25, 28 May 2016

Internal

Difference between -a and &&

If want to combine the results of multiple command executions in an if condition, use &&, not -a.

Example:

if grep "something" /file1.txt && grep "something" /file2.txt' then
    # "something" exists in both files ...
fi

-a should be used in test expressions:

if [ -f /file.txt -a -x /file.txt ]; then 
  ...
fi