Linux shell programming string compare syntax

What is the difference between = and == to compare strings in Linux shell programming?

Maybe the following code works:

if [ "$NAME" = "user" ]
    echo "your name is user"

But I think it's not a correct syntax. It would be used to compare string by == statement.

What is correct?


These pages explain the various comparison operators in bash:

On the second linked page, you will find:


    is equal to

    if [ "$a" == "$b" ]

    This is a synonym for =.

The single equal is correct

string1 == string2

string1 = string2

True if the strings are equal. ‘=’ should be used with the test command for POSIX conformance

if [ "$NAME" = "$USER" ]; then
    echo "Hello"

In general, the = operator works the same as == when comparing strings.

Note: The == comparison operator behaves differently within a double-brackets test than within single brackets.

[[ $a == z* ]]   # True if $a starts with an "z" (pattern matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).

[ $a == z* ]     # File globbing and word splitting take place.
[ "$a" == "z*" ] # True if $a is equal to z* (literal matching).


you can take a look here or here. Personally, to compare strings, I use case

case "$string1" in
  "$string2" ) echo "matched";;
  *) echo "not matched";;

I do not have to know which operator i should use

Need Your Help

Rails - FATAL: database "myapp_development" does not exist

ruby-on-rails postgresql ruby-on-rails-4

I am trying to set up my Env to use PostgreSQL with Rails and i followed the steps to install PostgreSQL from this Article

How do I correctly use ABAddressBookCreateWithOptions method in iOS 6?

iphone objective-c ios cocoa-touch abaddressbook

I am trying to understand the ABAdressBookCreateWithOptions and ABAddressBookRequestAccessWithCompletion methods in iOS 6.