LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-31-2003, 08:21 AM   #1
blish_blash
Member
 
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 ?

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

Rep: Reputation: 108Reputation: 108
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:
Code:
#!/bin/bash

var=$1

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


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


All times are GMT -5. The time now is 11:30 AM.

Main Menu
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