Compare 2 Strings Ignore Case In Bash

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

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

Solution:

Please try this:

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

or

Try similar, it still worked.

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

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

Note:

Do be aware in the 2nd 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"

Have a Linux Problem
Linux Forum - Do you have a Linux Question?

Linux Books
Linux Certification, System Administration, Programming, Networking Books

Linux Home: Linux System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.