LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices



Reply
 
Search this Thread
Old 09-17-2009, 11:00 AM   #1
Willy Fog
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Compare string (capitol letters) abc to ABC


I want compare two strings

if [ "$string1" == "$string2" ] ...

It works. But I want to compare without distinction between "abc" and "ABC". I mean...

¿abc = ABC? YEs!!!!
¿aBc = ABC? Yes !!!!.

How can i change a expresión to capitol letters.

:-)
 
Old 09-17-2009, 11:44 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
If you're using bash v.4, it's easy. It has some new parameter substitution rules for case conversion.
Code:
$ string1="FoObAr"

$ echo ${string1,,}     #echo as lowercase
foobar

$ echo ${string1^^}     #echo as uppercase
FOOBAR
The "declare" command in bash4 also has options for forcing a variable to always be upper or lower case.

Code:
$ declare -u string1="FoObAr"
$ echo $string1
FOOBAR

$ declare -l string1="FoObAr"
$ echo $string1
foobar
For previous versions of bash, or other shells, you can always use tr or a similar tool to convert the strings to one case before checking.
Code:
string1="$(echo $string1 | tr [a-z][A-Z])"     # converts string to uppercase

or

string1="$(echo $string1 | tr [A-Z] [a-z])"    # converts string to lowercase
If you're using non-ascii characters, make sure your tr command is set to handle them.
And of course, if you want to keep the original strings intact, then create two new variables and test them instead.

Last edited by David the H.; 09-17-2009 at 12:04 PM. Reason: fixed typo
 
Old 09-17-2009, 11:52 AM   #3
le37
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
To change a string to uppercase (or lowercase) you can use translate (abbreviated as tr). For example:

echo 'linux' | tr "a-z" "A-Z"

will give the output:

LINUX

so tr "variable" "range 1" "range 2" takes everything it finds in variable from range 1 and translates it to its partner in range 2. I haven't used it in the context your describing before but it should work ok.

Hope this helps,

Luke.
 
Old 09-17-2009, 12:21 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by le37 View Post
To change a string to uppercase (or lowercase) you can use translate (abbreviated as tr). For example:

echo 'linux' | tr "a-z" "A-Z"
Under some locale settings A-Z can give unexpected results. See "We use the fancy range notation, because tr can behave very strangely when using the A-Z range on some locales" on Greg's WIKI Bash FAQ. Safer to set the locale to C or use POSIX ranges
Code:
echo 'linux' | LC_ALL=C tr 'A-Z' 'a-z'
echo 'linux' | tr '[:upper:]' '[:lower:]'
 
Old 09-17-2009, 12:36 PM   #5
le37
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
Interesting stuff. Thanks :-)

Luke.
 
  


Reply


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 set linux environment variable like abc.abc (With DOT in the string) super_hill Linux - General 1 12-03-2008 08:37 PM
abc for linux arunmongoose LinuxQuestions.org Member Intro 3 11-19-2007 01:16 PM
how to install abc with python davidleroux1 Mandriva 3 01-22-2006 11:34 PM
directories with capitol letters davidleroux1 Linux - Newbie 2 12-31-2005 09:25 PM
ABC help?! setlec_seta Linux - Software 2 04-24-2004 08:07 PM


All times are GMT -5. The time now is 12:24 PM.

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