BASH Programming

Discussion in 'General Linux' started by Rory Glenn Pascua, Jan 9, 2012.

  1. Rory Glenn Pascua

    Rory Glenn Pascua New Member

    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    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.
  2. ehansen

    ehansen New Member Staff Writer

    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    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.
    Rob likes this.
  3. Rory Glenn Pascua

    Rory Glenn Pascua New Member

    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
  4. tux.think

    tux.think New Member

    Messages:
    4
    Likes Received:
    2
    Trophy Points:
    0
    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.
    Rob likes this.
  5. Rory Glenn Pascua

    Rory Glenn Pascua New Member

    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Script

    Thanks Tux
  6. Rory Glenn Pascua

    Rory Glenn Pascua New Member

    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    TUX: it works but now I want to know why you don't need a space between "if" and "("

    Thanks
  7. Rory Glenn Pascua

    Rory Glenn Pascua New Member

    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Got it now.... thanks.

Share This Page