LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   when to use [ ] and [[ ]] (https://www.linuxquestions.org/questions/linux-newbie-8/when-to-use-%5B-%5D-and-%5B%5B-%5D%5D-4175505877/)

sysmicuser 05-23-2014 09:28 AM

when to use [ ] and [[ ]]
 
As the above subject line says when to use single brackets [ ] and when to use [[ ]] in if statement in bash?

Solid confusion...............

linosaurusroot 05-23-2014 09:46 AM

Have you read the bash man page? IIRC double square brackets are for arithmetic.

I'm of the inclination that I rarely write for bash -- I write for generic Bourne shell or in Perl. That's got me portability in an installation of thousands of hosts in multiple unix/linux flavours. If you're bothered about the inefficiency of an excess expr here or there you'd take a different tack.

grail 05-23-2014 11:17 AM

[[]] are newer and have a few more options as well as are a little safer to use, as in the case of your variable not being set. Hence if you are sure it will be running on a similar version of bash
to when you wrote the script then I would tend to use only these and never []

[] are generally better to use if you are looking at creating POSIX relevant scripts or as above cannot guarantee to a certain level that the users will not be using an older non-compatible
version of bash (here I refer to the use of the newer feature set) ... for me this is whenever I write anything for my brother as he uses a MAC and the version of bash is ridiculously old

As for the comment above, the correct brackets for arithmetic are (())


If you have not had a chance to read through :- http://mywiki.wooledge.org/TitleIndex .... I would suggest over time read through all entries (there is one that speaks specifically about this topic)


All times are GMT -5. The time now is 04:28 PM.