bash scripting Q
Just wanted to know what the difference is between
if() and if[] |
() wraps up a sub-shell, [ ] is an alias for 'test'.
|
So, is it best to use [] instead of (), when test for a condition??
|
It depends on what you're testing ...
|
You might also want to look at this page http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
|
First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
Here are a few useful bash scripting references: http://mywiki.wooledge.org/BashGuide http://mywiki.wooledge.org/BashFAQ http://mywiki.wooledge.org/BashPitfalls http://www.linuxcommand.org/index.php http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/index.html http://www.gnu.org/software/bash/manual/bashref.html http://wiki.bash-hackers.org/start http://ss64.com/bash/ The first link in particular will cover all the scripting basics you need to know. It's important to understand and use the exact syntax as described in the documentation, as everything has very precise meanings. That said, there are several intricacies involved here. Code:
foo() { text="This is a function called foo." ; echo "$text" ;} Code:
$ ( text="This is an echo command running in a subshell." ; echo "$text" ) Code:
Pay attention to how the comparisons work in the above examples. Inside the square brackets, ">,<,=", etc. are considered string comparisons, and so 99 can indeed be "greater than" 100, depending on the sorting order set in your locale. To do numeric comparisons, use "-gt,-lt,-eq", etc. Even better, use "((..))" which is a bash keyword structure for evaluating arithmetic operations (integer only). Finally... Code:
x=foo So to sum it up, when using bash (or ksh), integer comparisons should be done with "((..))" and string and file comparisons with "[[..]]". Only use "[..]" if you need posix compliance, which for the average user is rarely. And "()" itself is for other purposes and isn't directly used in tests. Here are few more links concerning conditional expressions: http://mywiki.wooledge.org/ArithmeticExpression http://mywiki.wooledge.org/BashFAQ/031 http://wiki.bash-hackers.org/commands/classictest http://wiki.bash-hackers.org/syntax/...nal_expression |
Quote:
As advised on the course, I always use them in ksh (or bash), instead of [ ] or 'test'. |
Yes, of course it's in ksh too. I believe they probably even initiated it. In fact, quite a few ksh innovations have been subsequently adopted by bash, and vice-versa. There's a lot of similarity between the two shells due to this.
When I said "bash-specific", what I really meant was that it wasn't a posix-compliant or otherwise universally-available feature, and that you can't just assume it being there if you use another shell. But since the topic question was about bash specifically, I didn't want to further clutter up my post with relatively unimportant details like that. Even so, I did mention it in passing in my final paragraph. Thanks for allowing the clarification. :hattip: |
No worries :)
Actually, the course was so long ago, Linux had barely made it out of Finland :) I was working on Sequent (later IBM) https://en.wikipedia.org/wiki/Non-Uniform_Memory_Access systems. |
All times are GMT -5. The time now is 12:33 PM. |