LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [ ] OR [[ ]] That is the question. (https://www.linuxquestions.org/questions/programming-9/%5B-%5D-or-%5B%5B-%5D%5D-that-is-the-question-858218/)

cryingthug 01-23-2011 08:34 PM

[ ] OR [[ ]] That is the question.
 
I have two bash scripting books. One says that [[ ]] is the preferred test method and gives no explanation why. The other says that [ ] is better than [[ ]] because the double brackets are non standard.

What do you guys say?

everToulouse 01-23-2011 09:14 PM

Hi,

does your script need to be portable ?
then don't use [[ ]].

if you're 100% sure shells your script is gonna be executed on support [[ ]], then you can use it.

binary_pearl 01-23-2011 09:19 PM

ugh, this is why I like perl instead of bash...but that's besides the point ;)

I think, but can't say for sure, that arithmetic expressions may need to be in [[ ]] . Honestly the best thing to is to try one situation. If it works, your good. Otherwise try the other. Sometimes you may just have to find the one that works without understanding why. Yes it's nice to know why, but what it really boils down to...does the script work?

--Shaun

grail 01-23-2011 10:26 PM

Have a look about half way down the this page and you will see a section
starting with - Tests supported by [ (also known as test):

Julian Andrews 01-25-2011 01:59 PM

To make what the others are saying more explicit:

The '[' syntax is a standard shell scripting syntax supported by a wide range of shells, not only by bash. If there's any chance that you'll want to run the script on a system running a different shell, you shouldn't use the '[[' syntax. On the other hand, the '[[' syntax is more versatile and powerful, and might allow you to write simpler code. If you know you'll always be running a script under bash, you may find the added expressive power useful.

My own policy is to only use '[[' where I can't easily achieve the result with more universal syntax.

cryingthug 01-25-2011 10:13 PM

[[ ]]
 
Thank you guys/girls. Julian Andrews, that was a nice clear explanation. You sound like you could be a teacher. Grail I have bookmarked the site. You guys/girls rock!

wje_lq 01-25-2011 11:23 PM

Quote:

Originally Posted by binary_pearl (Post 4235517)
I think, but can't say for sure, that arithmetic expressions may need to be in [[ ]] .

Before guessing publicly, why not just try it?
Code:

#!/bin/bash

cat $0

six=6
forty=40

if [ $six -lt $forty ]
then
  echo we got the right answer
else
  echo we got the wrong answer
fi

we got the right answer


grail 01-26-2011 02:47 AM

IMHO the actual correct parenthesis to use for arithmetic in bash are - (())
I also find they are clearer as you can use the symbols you would understand:
Code:

#!/bin/bash

cat $0

six=6
forty=40

if (( six < forty ))
then
  echo we got the right answer
else
  echo we got the wrong answer
fi



All times are GMT -5. The time now is 08:14 PM.