Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

String comparisons don't work in shell scripts?

I'm a bit baffled. I'm trying to compare string variables in a shell script to figure out if one value is lexically less (that is, would appear earlier in the dictionary) than another, but it doesn't seem to work at all? Help!


Dave's Answer:

I'm pretty sure you're asking about the conditional string expressions in the test command, typically denoted in shell scripts with the [ and ] symbols (the first of which, as a complete piece of trivia, is actually a hard link to the test binary. Do the following to see it: ls /usr/bin/? or /bin/? instead!)

Anyway, you're trying to do something like this:

if [ $stringvar1 > $stringvar2 ] ; then
  echo $stringvar1 is greater than $stringvar2
else
  echo $stringvar1 is not greater than $stringvar2
fi

The problem with this notation, one that surprisingly few shell script books and articles mention, is that you need to escape the angle brackets or the shell will interpret them as redirection symbols!

Indeed, if you have stringvar1=test and stringvar2=me then running that test would actually create an output file called "me", not at all what you want to accomplish!

Instead, here's a snippet of code to experiment with that demonstrates the correct use of these string conditionals:

#!/bin/sh

a="aardvark" # then try 'zebra'
b="banjo"

if [ $a \< $b ] ; then
  echo "$a is less than $b"
else
  echo "$a is NOT less than $b"
fi

exit 0

Hope that helps clear up the mystery!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

Thanks for the Hint , For whomever you wrote it it helped my purpose

Posted by: Athreya at July 30, 2007 6:12 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.