Why Are There Single "[]" and Double "[[]]" Brackets In Bash? What's the Difference?

Well, since you asked, let's go over some of the differences. Here are the most important ones:

  1. [ is a builtin in Bash and many other modern shells. The builtin [ is similar to test with the additional requirement of a closing ]. The builtins [ and test imitate the functionality /bin/[ and /bin/test along with their limitations so that scripts would be backwards compatible. The original executables still exist mostly for POSIX compliance and backwards compatibility. Running the command type [ in Bash indicates that [ is interpreted as a builtin by default. (Note: which [ only looks for executables on the PATH and is equivalent to type -p [)
  2. [[ is not as compatible, it won't necessarily work with whatever /bin/sh points to. So [[ is the more modern Bash / Zsh / Ksh option.
  3. Because [[ is built into the shell and does not have legacy requirements, you don't need to worry about word splitting based on the IFS variable to mess up on variables that evaluate to a string with spaces. Therefore, you don't really need to put the variable in double quotes.

For the most part, the rest is just some nicer syntax. Generally double brackets "[[]]" are better to use if you're you don't need it to be portable. The main reasons are:

  1. You don't have to worry about quoting the left-hand side of the test so that it actually gets read as a variable.
  2. You don't have to escape less than and greater than < > with backslashes in order for them not to get evaluated as input redirection, which can really mess some stuff up by overwriting files. This again goes back to [[ being a builtin. If [ (test) is an external program the shell would have to make an exception in the way it evaluates < and > only if /bin/test is being called, which wouldn't really make sense.

If you have any comments, feel free to post them below or find us on Twitter and Facebook!

-Until next time!

January 20 2020

Add or review comments

Please leave your comment

Existing comments

Comments 27

Barry allen
thanks for information ! https://get-shareit.com https://get-vidmateapk.com
cialis on line
Ph And Amoxicillin haurrehigh [url=https://artsocialist.com/]buy online cialis[/url] Engeda Can Viagra 100mg Be Dangerous? Awagosoods <a href=https://artsocialist.com/#>cialis prices</a> noinly Medicina Propecia
We offer the very best SEO Services for different varieties of web resources. You can pick from our standard programs or inquire for tailor made SEO that'll be tailored specially for the website. Effects of our SEO efforts generally appear within 30 days since the beginning of our work. You might check it choosing our Trial SEO Plan follow link: https://shorl.com/kihuvokyjefru
Thanx for sharing this amazing article.
viagra cialis france <a href="https://pcialis.com/">tadalafil generic</a> manufacturer for cialis
cialis brand uk <a href="https://cialidl.com/">can i buy cialis in uk</a> cialis packungsgroesse
leg pain with cialis <a href="https://cialnt.com/">cialis purchase online</a> cialis lust
cialis generico guadalajara <a href="https://caciali.com/">cialis opisanie</a> better cialis or viagra
does cialis really help bph <a href="https://cialusorg.com/">cheap cialis canadian</a> cialis diario na pacheco
cialis mutuabile <a href="https://cialispos.com/">cialis rinnakkaisvalmiste</a> use cialis without ed
cash instant loan payday till <a href="https://loanspersonala.com/">bad credit payday loans mountain view</a> payday advance loans west valley city
We accept BitCoin - <a href="https://creamretin.com/">viagra and retinitis pigmentosa</a> buy cialis bodybuilding
We accept BitCoin - <a href="https://creamretin.com/">tretinoin uk</a> cialis podrobki
We accept BitCoin - <a href="https://creamretin.com/">levitra diabetic retinopathy</a> best use of cialis
fast cash advance riverside <a href="https://onlinequickloans.us.org/">Payday Loans Platte City Missouri</a> , <a href="http://cashadvanceonliner.com/">cash advance stores wooster ohio</a> bad credit payday loans biloxi
faxless payday loans prescott <a href="https://onlinequickloans.us.org/">Payday Loans That Dont Check Credit</a> , <a href="http://cashadvanceonliner.com/">cash advances on mercury blvd</a> best payday loans covina
what color is liquid cialis <a href="https://focialisaf.com/">cialis walmart</a> welche ist besser cialis oder viagra
does medicaid pay for cialis <a href="https://focialisaf.com/">cialis generic</a> buy cialis with priligy
cialis gente joven <a href="https://focialisaf.com/">does generic cialis exist</a> buying brand cialis online
pastillas cialis genericas <a href="https://focialisaf.com/">cialis price comparison</a> making cialis more effective
cialis yetkili eczane <a href="https://focialisaf.com/">generic cialis best price</a> ed viagra cialis levitra
<a href="https://uscialisuk.com/">brand cialis for sale</a> hipertenso pode usar cialis <a href="https://bmzcialis.com">cialis soft tabs no prescription</a> / <a href="https://cialics.com/">generic cialis daily use</a>
<a href="https://uscialisuk.com/">cialis 20 mg directions</a> cialis nerden bulabilirim <a href="https://bmzcialis.com">cialis soft tabs no prescription</a> / <a href="https://cialics.com/">cialis bathtub meaning</a>
<a href=http://vslevitrav.com/>levitra order online[/url]
tadalafil dosage <a href="https://elitadalafill.com/">buy cialis online</a> sublingual tadalafil
aralen buy online
who produces hydroxychloroquine win <a href="https://hydroxychloroquinex.com/#">chloroquine diphosphate</a> what is hydroxychlor used for
<a href=http://fcialisj.com>cheap generic cialis[/url]