Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    BASH Programming

    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. #2
    Join Date
    Jan 2012
    Posts
    124
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Rep Power
    2
    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.
    Information Server Management
    Linux server management, PCI consultation and affordable web hosting.

    Security For Us - Where security works for you

    Providing server security and PCI compliance for individuals and businesses.

  3. The Following User Says Thank You to ehansen For This Useful Post:

    Rob (01-17-2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Script

    Thanks

  5. #4
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    Rep Power
    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.

  6. The Following User Says Thank You to tux.think For This Useful Post:

    Rob (01-10-2012)

  7. #5
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Script

    Thanks Tux

  8. #6
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    TUX: it works but now I want to know why you don't need a space between "if" and "("

    Thanks

  9. #7
    Join Date
    Jan 2012
    Posts
    43
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Got it now.... thanks.

 

 

Similar Threads

  1. Replies: 0
    Last Post: 12-21-2011, 04:30 AM
  2. Replies: 0
    Last Post: 12-12-2011, 06:13 PM
  3. Me and Bash – The Bourne-Again SHell
    By Fred in forum Linux News
    Replies: 0
    Last Post: 12-11-2011, 11:57 PM
  4. Me and Bash – The Bourne-Again SHell
    By Fred in forum Linux News
    Replies: 0
    Last Post: 12-11-2011, 10:19 PM
  5. How to Debug Bash Scripts
    By Fred in forum Linux News
    Replies: 0
    Last Post: 11-11-2011, 03:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,582
Threads: 3,920
Posts: 9,447
Top Poster: Fred (1,486)
Welcome to our newest member, harfa

» Links



Powered by vBadvanced CMPS