ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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!
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.