PDA

View Full Version : Testing Strings for Equality in bash: using the * wildcard



flummoxed
September 17th, 2008, 07:15 PM
Hello,

I'm trying to figure out how to match part of a word in a shell script using the * wildcard... Here is some example code showing what I'm trying to achieve:



word=aSuperWord
if [ $word == *Super* ]
then
echo "Yes"
else
echo "No"
fi


I want the code to check to see if the substring "Super" part of the string variable that it's evaluating. However, I don't seem to be able to use the * wildcard to mean any other characters. Why doesn't this work?

adamkane
September 17th, 2008, 07:26 PM
You need to use regular expressions (regex). You won't use '='. Regex is like a filter. You pass the string, and check to see if you get 'super' at the end. I'm at school. I'll respond with a better answer tonight.

Martin Witte
September 17th, 2008, 08:50 PM
You need regular expresons indeed, e.g.


martin@toshibap200:~$ word=aSuperWord
martin@toshibap200:~$ echo $word
aSuperWord
martin@toshibap200:~$ [[ $word =~ .*Word ]]
martin@toshibap200:~$ echo $?
0
martin@toshibap200:~$

lswb
September 17th, 2008, 09:18 PM
Try using this form instead, note the double brackets:

if [[ $word == *Super* ]]

flummoxed
September 17th, 2008, 09:23 PM
Try using this form instead, note the double brackets:

if [[ $word == *Super* ]]

This method worked... what is it about the double brackets that makes this work?

adamkane
September 17th, 2008, 09:36 PM
It's just another regex. Regex uses [[ ]]. The problem with comparators like == != with wildcards like *, etc. is that you will get false matches. It's better avoid it, but it's up to you of course.

ghostdog74
September 18th, 2008, 12:41 AM
no need to use regular expression.


case $variable in
*Super* ) echo "found";;
* ) echo "not found";;
esac

lswb
September 18th, 2008, 04:07 AM
This method worked... what is it about the double brackets that makes this work?

The single [] will not use regular expressions, however, if anything inside it has a * or ? it will try to do filename expansion it it; probably not what you want in this case!

The [[ ]] form is used for regular expressions as adamkane noted above, and no filename expansion takes place inside [[ ]]