Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-13-2008, 03:12 PM   #1
LQ Newbie
Registered: Oct 2008
Posts: 4

Rep: Reputation: 0
Evaluate strings as case-insensitive

In DOS batch scripts, I used to be able to compare strings in a case-insensitive way such as:

IF /I "TEST" EQU "test" echo Matched!
IF /I "Test" EQU "test" echo Matched!

But in bash, this will always fail:

if [ "TEST" = "test" ]; then echo "Matched"; fi
if [ "Test" = "test" ]; then echo "Matched"; fi

Is there a straightforward way to make "if" evaluate the strings as case-insensitive?

Old 10-13-2008, 03:32 PM   #2
Registered: Aug 2008
Distribution: opensuse, RHEL
Posts: 374
Blog Entries: 1

Rep: Reputation: 32
the 'tr' (transform) command can do this. I don't know how the structure of your if statement is, but it seems you may be comparing two variables, so you can do a tr statement like so:

if [ `echo $VAR1 | tr [:upper:] [:lower:]` =  `echo $VAR2 | tr [:upper:] [:lower:]` ]; then
echo "Matches"
That should do the trick.
Old 10-13-2008, 03:33 PM   #3
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
man bash | grep -A 4 -B 4 -i case
This will turn up references in man bash relasting to nocasematch and nocaseglob.

I'm a bit rushed now, so I'll leave it to you to research the bash man pages for more info.
Old 10-13-2008, 03:36 PM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another way is to force the shell to do a case insensitive comparison:
shopt -s nocasematch

if [[ "TEST" = "test" ]]
  echo "Matched"

shopt -u nocasematch
shopt -s sets the option, whereas shopt -u disables it.

Edit: beaten by bigrigdriver!
Old 10-13-2008, 04:27 PM   #5
LQ Newbie
Registered: Oct 2008
Posts: 4

Original Poster
Rep: Reputation: 0
tr works perfectly

Thanks, CRC123

Your solution worked perfectly for my needed syntax:

if [ `echo $VAR1 | tr [:upper:] [:lower:]` = `echo $VAR2 | tr [:upper:] [:lower:]` ]
then CHECK=1

I kind of knew I had to use tr, but couldn't quite get the syntax right. Thanks for your helpful response.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to search in VI wih case insensitive lordofring Programming 7 11-16-2011 08:11 AM
Copying files from case-sensitive Linux to case-insensitive Windows via CIFS? SlowCoder Linux - General 4 05-07-2008 07:03 PM
Case-insensitive CVS? sundialsvcs Programming 2 06-25-2006 10:01 AM
SuSE's less(1) is always case insensitive: how to disable? wirawan0 SUSE / openSUSE 1 01-25-2006 12:10 PM
case insensitive search command ColKurtz Linux - Newbie 4 01-20-2005 07:02 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:16 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration