LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH Programming (http://www.linuxquestions.org/questions/linux-newbie-8/bash-programming-922876/)

Rory Glenn Pascua 01-09-2012 10:40 AM

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.

grail 01-09-2012 11:02 AM

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

Rory Glenn Pascua 01-09-2012 11:06 AM

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?

Telengard 01-09-2012 01:47 PM

[ is a command, not syntax for an if statement.

Quote:

Originally Posted by Rory Glenn Pascua (Post 4570261)
#!/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.

Rory Glenn Pascua 01-09-2012 01:58 PM

Script
 
Thanks everyone for your help

chrism01 01-09-2012 06:38 PM

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/

Rory Glenn Pascua 01-20-2012 01:53 PM

Thanks Chrism

Rory Glenn Pascua 01-20-2012 02:03 PM

That Rute's Tutorial was def an interesting read

David the H. 01-21-2012 07:46 AM

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.

Rory Glenn Pascua 01-21-2012 07:53 AM

BASH Programming
 
Thanks David. That was very informative.


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