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. |
Quote:
Quote:
|
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?
|
[ is a command, not syntax for an if statement.
Quote:
Code:
if test "foo" = "foo"; then Please use code tags when you post code. |
Script
Thanks everyone for your help
|
|
Thanks Chrism
|
That Rute's Tutorial was def an interesting read
|
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. |
BASH Programming
Thanks David. That was very informative.
|
All times are GMT -5. The time now is 08:44 AM. |