LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2011, 09:34 PM   #1
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Rep: Reputation: 18
[ ] 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?
 
Old 01-23-2011, 10:14 PM   #2
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
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.
 
Old 01-23-2011, 10:19 PM   #3
binary_pearl
Member
 
Registered: Jul 2007
Location: Chicago Illinois
Distribution: SLES 10 SP2/SP3, SLES 11 SP1, OpenSUSE, Sabayon, Gentoo, Fedora 14, RHEL 3/4/5/6
Posts: 98
Blog Entries: 3

Rep: Reputation: 23
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
 
Old 01-23-2011, 11:26 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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):
 
Old 01-25-2011, 02:59 PM   #5
Julian Andrews
LQ Newbie
 
Registered: Jan 2011
Distribution: Ubuntu
Posts: 21

Rep: Reputation: 13
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.
 
Old 01-25-2011, 11:13 PM   #6
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
[[ ]]

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!
 
Old 01-26-2011, 12:23 AM   #7
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by binary_pearl View Post
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
 
Old 01-26-2011, 03:47 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic html question - download link to files on my webpage question Davno Linux - Server 5 12-25-2009 08:24 AM
linux distro question & mysql install question natalie.aloi Linux - Newbie 5 07-19-2009 09:28 PM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 05:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 02:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

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