[ ] 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? |
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. |
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 |
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): |
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. |
[[ ]]
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!
|
Quote:
Code:
#!/bin/bash |
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 |
All times are GMT -5. The time now is 08:14 PM. |