[geeks] bash string matching

Shannon Hendrix shannon at widomaker.com
Wed Jun 4 17:44:30 CDT 2008


Some posix shells like bash offer some interesting string matching  
abilities, but it seems really easy to throw them off.

For example, you can use globbing or regular expressions in bash like  
this:

STRING="this is a geek test"

[[ "$STRING" == *geek* ]] && echo geek

[[ "$STRING" =~ .*geek.* ]] && echo geek

[[ "$STRING" =~ "*is a geek*" ]] && echo geek

[[ "$STRING" =~ ".*is a geek.*" ]] && echo geek

The first two work fine.

However, as soon as you need to quote to enclose spaces or other  
delimiters, the matching breaks.

How do you input complex globs or regex's in bash conditional  
statements?  Surely there has to be some way of doing it?

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


	


-- 
Shannon Hendrix
shannon at widomaker.com



More information about the geeks mailing list