What is the difference between double and single square brackets [ and [[ as well as equal signs = and ==?

I cannot find the difference between:

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

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

Answers


Difference between [] and [[]] check this

Difference between = and ==

= is assignment operator

== equality check operator OR conditional operator


[] 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.


Need Your Help

Make a YUI asyncRequest call which is not async

javascript yui

I want to make a YAHOO.util.Connect.asyncRequest call, which not is async. Just like open(method, url, async) where false is passed by async.

If statement always returning true?

javascript jquery tampermonkey

I have this function to add text to a textarea when it gains focus ONLY when it's empty but for some reason, it will add the text when there is already content and sometimes it won't add anything w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.