LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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, 11: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, 12:02 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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, 12:06 PM   #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, 02:47 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
[ 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 02:48 PM. Reason: processed -> interpreted
 
Old 01-09-2012, 02: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, 07:38 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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, 02:53 PM   #7
Rory Glenn Pascua
Member
 
Registered: Jan 2012
Posts: 35

Original Poster
Rep: Reputation: Disabled
Thanks Chrism
 
Old 01-20-2012, 03: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, 08:46 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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, 08: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


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


All times are GMT -5. The time now is 10:56 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration