The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ksh: difference between $* and $@ JamesByars Shell Programming and Scripting 1 12-30-2007 10:08 AM
Difference between $* and $@ saneeshjose Shell Programming and Scripting 1 01-19-2006 08:03 AM
Difference between C and C++ hytechpro High Level Programming 2 11-29-2005 09:48 PM
difference rajashekaran UNIX for Advanced & Expert Users 1 04-23-2002 01:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2006
Vikas Sood Vikas Sood is offline
Registered User
  
 

Join Date: May 2006
Posts: 32
difference between == and =

This is probably a stupid question to ask. But could somebody help me clearly distinguish the difference between these two operators in unix '==' and '='?
  #2 (permalink)  
Old 05-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
In unix what?
  #3 (permalink)  
Old 05-20-2006
Vikas Sood Vikas Sood is offline
Registered User
  
 

Join Date: May 2006
Posts: 32
may be i was not clear

excuse me if i did not clearly stated the prob. Consider this piece of code:

if( $3 == "R001" )
print "xxx";
else
print "yyy";
fi

my question is why can't I use the following instead of above:

if [[ $3 = "R001" ]]
print "xxx"
else
print "yyy"
fi

Also, is there is a difference in () and [[ ]] in above two examples?

Thanks
  #4 (permalink)  
Old 05-20-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,928
UNIX is an operating system, not a commandline. You seem to be talking about the standard UNIX shell, sh or one of it's clones like bash, but could have as easily been using something like korn... or even been programming in C.... = and == are pretty common.

I've never seen code like your first example, probably because it doesn't work. ( ) brackets aren't statement grouping, they define arrays!
Code:
arr=(a = c)
echo ${arr[0]}
echo ${arr[1]}
echo ${arr[2]}
will print a, then =, then c. The = isn't even treated as an operator inside the ().

Inside the [[ ]] brackets, == is a pattern matching operator for strings, and = is a straight equality comparison. Outside of there, = is an assignment operator like variable="something" and I don't think == does anything.

Also, your if statements aren't quite right. Try this:

Code:
if [[ $this = "that" ]]
then
      echo "Something"
else
      echo "Something else"
fi

Last edited by Corona688; 05-20-2006 at 02:47 AM..
  #5 (permalink)  
Old 05-20-2006
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
It appears that the difference is that one command is in a sh based shell and the other a csh based shell.
  #6 (permalink)  
Old 05-20-2006
Vikas Sood Vikas Sood is offline
Registered User
  
 

Join Date: May 2006
Posts: 32
Question got some lead

Thanks both of you, for your replies. The examples that I cited were from ksh actually. I did come across some places and found out that == is equality operator used in arirthmatic operations (e.g. to compare two strings) whereas = is simply an assignment operator.

I guess I am still a little bit confused as far as the different brackets are concerned i.e. is my understand correct per following:

1. (commands...) - open a subshell and execute commands in that subshell ??
2. ((commands...)) - i have no idea about this one.

As far as [ $var1 -eq 0 ] and [[ $var1 -eq 0 ]] type of brackets are concerned, i guess they are equivalent and are used to test whether var1 is set to 0.

Thanks once again
Vikas
  #7 (permalink)  
Old 05-20-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
[[ ]] must be used when there are multiple tests:
Code:
if [[ "$a" = "one"  && "$b" = "two ]]; then
    do something
fi
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0