LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-23-2014, 09:28 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 353

Rep: Reputation: 0
Unhappy 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...............
 
Old 05-23-2014, 09:46 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 05-23-2014, 11:17 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
[[]] 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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration