[geeks] bash string matching

Anthony Ortenzi geeks at litfire.com
Wed Jun 4 17:59:06 CDT 2008


Shannon Hendrix said:
> BTW: for those who don't know, the [[ ]] above is just a shortened if
> statement, the same as:
> 
> if [[ "$STRING" == *geek* ]]
> then
> 	echo geek
> fi
> 

Two things...

First, the [[ ]] isn't the "if", the && is.

It represents the logical AND.  If the operand before the && evaluates to
false, it's impossible for the AND statement to be true, so the shell
doesn't execute the operand after the &&.  If the first evaluates to 1
(true), then in order to find out if the AND is true overall, it's necessary
to evaluate the operand after the &&, so in your example, the echo is
evaluated.

|| represents the logical OR, and the second operand will only be evaluated
if the first operand evaluates to 0 (false)

Second, regarding quotes, my man page for bash 2.05 contains mention of
quote removal.

 [[ expression ]]
    Return  a  status  of  0 or 1 depending on the evaluation of the
    conditional expression expression.  Expressions are composed  of
    the  primaries  described  below  under CONDITIONAL EXPRESSIONS.
    Word splitting and pathname expansion are not performed  on  the
    words  between  the  [[  and  ]]; tilde expansion, parameter and
    variable expansion, arithmetic expansion, command  substitution,
    process substitution, and quote removal are performed.


-Anthony



More information about the geeks mailing list