Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-31-2003, 09:21 AM   #1
Registered: Dec 2003
Posts: 68

Rep: Reputation: 15
why does if [ ${var:0:2}='AA' ] always return true

Hi and happy new year !
can someone tell me why does

if [ ${var:0:2}='AA' ]

always return true, and tell me how to change if to test the first two characters in variable var ?

Old 12-31-2003, 09:51 AM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Happy new year to you too.

That happens because you need to have spaces around the '='.
It's also better (at least in this case) to have double quotes around ${var:0:2}. Without the double quotes you'll get an error if "var" happens to be emtpy.

So, this works:


if [ "${var:0:2}" = "AA" ] ; then
    echo True


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
seeing if this is true or not. LunarEagle Linux - Newbie 5 02-18-2005 01:33 PM
Return true or false if I have ping reply Menestrel Programming 4 11-29-2004 01:40 AM
Moving /var/adm and /var/lib - why does it hurt? J_Szucs Linux - General 1 09-15-2004 07:46 PM
rm cannot remove /var/run/ and /var/run/ danishmr Linux - Software 1 05-04-2004 09:01 AM
is this true? chrismiceli General 6 10-12-2003 08:00 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration