1

when I compare two strings with single equal “=” ?

and when I compare two strings with double equal “==” ?

for example:

 [[  $STR = $STR1 ]]

OR

 [[  $STR == $STR1 ]]

Or maybe they are both do exactly the same thing?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions – Etan Reisner May 12 '15 at 18:23
  • 1
    They behave the same. The only difference is where `==` is allowed to be used. – chepner May 12 '15 at 18:23
  • In order not to wear out your keyboard and for sake of empty strings, I suggest something like `[ "$STR" = "$STR1" ] && echo "'$STR' is equal to '$STR1'"` – ott-- May 12 '15 at 18:28
  • @ott-- `[[` doesn't word split the arguments the way `[` does so double quotes aren't necessary (and can, in fact, be wrong as they will suppress pattern matching). – Etan Reisner May 12 '15 at 18:32
  • 1
    Not sure this is strictly a duplicate, but there are a lot of questions/answers on this already (http://stackoverflow.com/q/2188199/3076724, http://stackoverflow.com/q/669452/3076724, http://stackoverflow.com/q/13542832/3076724, etc...) – Reinstate Monica Please May 12 '15 at 18:43

2 Answers2

0

You right, both way does exactly the same thing, there is no difference at the execution.

In your conditions, take care to add quotes to your variable's name, bash could throws an error if one of them is null. Add double quotes will pass throught this error by setting variable to empty string "".

[[ "$STR1" = "$STR2" ]]

EDIT : (Thanks to comments below)

Prefer use this syntaxe [ "$STR1" == "$STR2" ] for test and shell convenience. Doubles quotes are better to use and make your condition usable with regular expression as "*.txt" but not even required.

Kapcash
  • 6,377
  • 2
  • 18
  • 40
  • 3
    Double quotes are not necessary when using `[[` as that is a bash built-in without the historical baggage of `[`. – Etan Reisner May 12 '15 at 18:31
  • In fact double quotes in `[[` may be erroneous as they will suppress the use of the RHS as a pattern (though using them may be intentional to prevent that). – Etan Reisner May 12 '15 at 18:32
  • 2
    For portability across different shells, prefer `[ "$STR1" = "$STR2" ]`. – jub0bs May 12 '15 at 18:33
  • 1
    There's right and wrong here. Double-quotes by default on the right-hand side are definitely the better practice unless someone explicitly wants to do a pattern match rather than a string comparison, and arguably good practice across-the-board to encourage good finger memory, but the text of this answer claiming that using them prevents a syntax error is (with `[[ ]]` as opposed to `[ ]`) incorrect outright. – Charles Duffy May 12 '15 at 19:49
  • 1
    @Kapcash, the edit to suggest `[ "$STR1" == "$STR2" ]` is a step in the wrong direction. `[ "$STR1" = "$STR2" ]` is the portable alternative, with only one `=`, not two. – Charles Duffy May 12 '15 at 20:32
  • 1
    Further correction: `*.txt` is not a valid regular expression; it's a glob-style pattern in the context of `[[ $foo = *.txt ]]` (which tests whether `$foo` ends in `.txt`), or a regular string (not treated as a pattern) in the context of `[[ $foo = "*.txt" ]]` or `[ "$foo" = "*.txt" ]` (in which case it tests whether `$foo` contains **exactly** the string `*.txt`). The regular-expression equivalent to `*.txt` is `.*[.]txt$`, which could be used in `[[ $foo =~ .*[.]txt$ ]]`. – Charles Duffy May 12 '15 at 23:31
0
[[ $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).

Everything about string comparison you can find here.

rogyvoje
  • 304
  • 3
  • 6
  • 1
    `[ "$a" == "z*" ]` violates POSIX sh, which only establishes `=` as a string comparison operator. GNU systems (and modern BSDs) add `==` as an operator to test, but this is an extension, not a mandated part of the standard. – Charles Duffy May 12 '15 at 19:19
  • 1
    It might also be worth describing a little more precisely why the effects of globbing are always undesirable in `[ $a = z* ]`. – Charles Duffy May 12 '15 at 19:20