1

I cannot find the difference between:

[ $foo = b* ]
[ $foo == b* ]

[[ $foo = b* ]]
[[ $foo == b* ]]
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
t3hcakeman
  • 2,289
  • 4
  • 25
  • 27

2 Answers2

0

[] vs [[ ]] in general was covered at Is [[ ]] preferable over [ ] in bash scripts? so let's not touch it.

= vs ==:

Inside [ ]

So only use = as it is both more portable and shorter.

Inside [[ ]]

[[ ]] is not POSIX, so we refer only to man bash.

It says under CONDITIONAL EXPRESSIONS that = and == are the same inside [[ ]] and that both do pattern matching as described at "Compound Commands".

Then "Compound Commands" is the same pattern used for glob expansion, e.g.:

[[ 'abcde' = ?b[cC]* ]] && echo true

possibly with extglob extensions.

Note that you must not quote the pattern: it will not glob expand to files as usual. If you quote it, you lose the pattern matching magic and get literal characters.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
-2

Difference between [] and [[]] check this

Difference between = and ==

= is assignment operator

== equality check operator OR conditional operator

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • 1
    Not quite. In both `[ ]` and `[[ ]]`, `=` is a string equality comparison not an assignment. `[[ ]]` and some implementations of `[ ]` also accept `==` as a synonym. (p.s. I'm not sure what you mean by "conditional operator".) – Gordon Davisson Dec 01 '11 at 15:46