BASH Programming

  • Thread starter Rory Glenn Pascua
  • Start date
R

Rory Glenn Pascua

Guest
Can anyone help me understand the script below?

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

I'm trying to understand why there's space between the square brackets and "foo". Also I want to know if I can use curly brackets or parentheses instead of the square brackets. Thanks in advance.
 


E

ehansen

Guest
Basically, its the syntax of Bash. If you try to remove the spaces, the if statement will error out and your script just won't run. As for your other question, no, you cannot replace the syntax of the language. If you want to do that it'd probably be easier just to write the script into an actual programming language instead of a scripting language.
 
T

tux.think

Guest
The [ in bash programming is a command in itself which basically tests the condition and returns true or false. Hence the if and the [ have to be seperated by a space.
In case you do not want to use the [ you will have to use the command test as follows

if(test "foo" = "foo) then
echo "strings match"
fi

In this case there is no need for a space in between if and the (.

So basically the command "test" and [ are one and the same.
 
R

Rory Glenn Pascua

Guest
TUX: it works but now I want to know why you don't need a space between "if" and "("

Thanks
 


Members online

No members online now.

Top