1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

BUG? -> sh: [ "$@" ] -> "eats" it's quotes!

Discussion in 'Userland Programming & Scripting' started by Seeker, Nov 10, 2011.

  1. Seeker

    Seeker New Member

    Messages:
    856
    Likes Received:
    0
    If [ "$@" ] is alone, err won't show up, but if you compare it ...

    Code:
    debug_fun ()
    {
        local -
        set -x
    
        [ "$1" ] && echo '$1 - Empty!'
        [ "$*" ] && echo '$* - Empty!'
        [ "$@" ] && echo '$@ - Empty!'
    }
    
    
    debug_fun_compare ()
    {
        local -
        set -x
    
        [ "$1" = ro ] && echo '$1 - Empty!'
        [ "$*" = ro ] && echo '$* - Empty!'
        [ "$@" = ro ] && echo '$@ - Empty!'
    }
    
    
    debug_fun
    
    debug_fun_compare
     
  2. SirDice

    SirDice Moderator Staff Member

    Messages:
    17,358
    Likes Received:
    0
  3. Seeker

    Seeker New Member

    Messages:
    856
    Likes Received:
    0
    I know all this and it doesn't give an answer to this.
    I'm talking about 0 args here in quotes (as $@ and $*, both behave same, when NOT surrounded with "").
     
  4. jilles@

    jilles@ New Member Developer

    Messages:
    35
    Likes Received:
    0
    [ is expanded like any other simple command. The three tests in your first function are not all expanded the same but all are valid tests returning false. The third test in your second function causes a test(1)() syntax error because "$@" expands to nothing at all if there are no positional parameters.
     
  5. Seeker

    Seeker New Member

    Messages:
    856
    Likes Received:
    0
    That is exactly, why do I use double quotes around vars, to prevent syntax errors, in case when var isn't set/is undefined.
    Problem is, "$@" acts like it was written as $@. It's double quotes disappear (Exposed via set -x in both functions)