string compare

December 16, 2010 at 03:01:21
Specs: Linux x86_64
Hi...

I want to compare 2 strings with ignore case in bash shell...

suppose, if [ "test" = "TEst" ] ... should be true and enter into if loop.

could any body please help me

Thanks


See More: string compare

Report •

#1
December 16, 2010 at 05:02:28
Please try this:

if [ `echo "TeSt" | tr -s '[:upper:]' '[:lower:]` = `echo "Test" | tr -s '[:upper:]' '[:lower:]` ]
then
echo "true"
else
echo "false"
fi


Report •

#2
December 16, 2010 at 20:45:05
Thank you for your response. I tried similar, it worked.

input=TESt
input=`echo $input | tr '[A-Z]' '[a-z]'`

if [[ "$input" =~ "test" ]]; then
echo "equal"
else
echo "not equal"
fi


Report •

#3
December 16, 2010 at 23:05:07
Are you aware in your example above, that, in the bash shell, the =~ is a binary operator, and everything to the right is a regular expression. Strictly speaking, it is not an equality check, but a matching operator?

This string would also be "equal" in your script above:

input="myTEStube"


Report •

Related Solutions

#4
December 16, 2010 at 23:46:36
Ah yeah!! it is like search for a substring!!

I changed like below just to check only "ignore case"

input=TESt
input=`echo $input | tr '[A-Z]' '[a-z]'`

if [ "$input" = "test" ]; then
echo "equal"
else
echo "not equal"
fi


Report •

Ask Question