Logical test operators

The following are the various options for logical operations using test, which is taken from the Bash reference manual available at http://www.gnu.org/software/bash/:

Test Operator
Tests True If
[ string_1 -a string_1 ]
Both string_1 and string_2 are true
[ string_1 -o string_2 ]
Either string_1 or string_2 is true
[ ! string_1 ]
Not a string_1 match
[[ pattern_1 && pattern_2 ]]
Both pattern_1 and pattern_2 are true
[[ pattern_1 || pattern_2 ]]
Either pattern_1 or pattern_2 is true
[[ ! pattern ]]
Not a pattern match


We can use the test operator for strings along with pattern matching as follows:

$ name=Ganesh $ [[ $name == [Gg]anesh ]] # Wildcards allowed $ echo $? 0 ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.