Purchase Linux CDs / DVDs / Flash Drives at OSDisc.com

Welcome to Our Community

While Linux.org has been around for a while, we recently changed management and had to purge most of the content (including users). If you signed up before April 23rd please sign up again. Thanks!

BASH Programming

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

  1. 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 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.
     
    1 person likes this.
  3. tux.think

    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.
     
    1 person likes this.
  4. TUX: it works but now I want to know why you don't need a space between "if" and "("

    Thanks
     
  5. Got it now.... thanks.
     

Share This Page