LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2012, 10:40 AM   #1
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Rep: Reputation: Disabled
BASH Programming


Can anyone help me understand the script below?

#!/bin/bash
if [ "foo" = "foo" ]; then
echo expression evaluated as true
fi

My question is how come there's space between the brackets and "foo"?
Also can I use curly brackets or parentheses instead of the square brackets?

Thanks in advance.
 
Old 01-09-2012, 11:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
My question is how come there's space between the brackets and "foo"?
Because '[' is a command, so like any other command you need a space after it.
Quote:
Also can I use curly brackets or parentheses instead of the square brackets?
No
 
Old 01-09-2012, 11:06 AM   #3
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Script

Thanks Grail. One more thing.. is there a particular reason why the square bracket is the bracket of choice here or is it one of those things that I just have to know?
 
Old 01-09-2012, 01:47 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
[ is a command, not syntax for an if statement.

Quote:
Originally Posted by Rory Glenn Pascua View Post
#!/bin/bash
if [ "foo" = "foo" ]; then
echo expression evaluated as true
fi
It is exactly the same as if you had written this.

Code:
if test "foo" = "foo"; then
Since you intend the script to be interpreted by Bash, you should consider using Bash's more reliable [[ syntax.

Please use code tags when you post code.

Last edited by Telengard; 01-09-2012 at 01:48 PM. Reason: processed -> interpreted
 
Old 01-09-2012, 01:58 PM   #5
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Script

Thanks everyone for your help
 
Old 01-09-2012, 06:38 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 01-20-2012, 01:53 PM   #7
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thanks Chrism
 
Old 01-20-2012, 02:03 PM   #8
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
That Rute's Tutorial was def an interesting read
 
Old 01-21-2012, 07:46 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Shell syntax, as with most programming, tends to be very exact and sensitive to error. Various characters are assigned very specific meanings and can generally accept very little deviation. When you get an example form like the above, try to follow it to the letter...at least until you fully understand what can and can't be done with it.

Incidentally, while [ .. ] is the traditional bourne-compatible test command, supported by all posix shells, bash and ksh also provide a newer test: the [[ .. ]], the expanded test keyword, which provides some additional features and corrects some of the flaws of the original.

http://mywiki.wooledge.org/BashFAQ/031

There's also (( .. )), the arithmetic evaluation brackets. Since they also output a true/false exit status, they can be used in place of the square bracket tests when comparing integer values.

http://mywiki.wooledge.org/ArithmeticExpression

Unless you really need portability to old, non-posix shells, it's generally a good idea to use the [[..]] test for all of your string evaluations, and ((..)) for numerical tests.
 
Old 01-21-2012, 07:53 AM   #10
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
BASH Programming

Thanks David. That was very informative.
 
  


Reply

Tags
bash, conditional, test



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
Bash programming winterhunter Linux - Newbie 6 11-26-2007 07:14 AM
bash programming drisay Programming 6 12-20-2004 09:09 AM
bash programming bulliver Programming 2 04-05-2003 09:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:30 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
Open Source Consulting | Domain Registration