What is the difference between "$*" and "$@" in a bash shell script?



  • Also see: What is the difference between $* and $@ in bash shell script?

    Adding double quotes to all positional parameter predefined variable, you see different results.

    echo "$*"
    echo "$@"
    echo ""
    for i in "$*"
    do
      echo $i
    done
    
    for i in "$@"
    do
      echo $i
    done
    

    The results are, the two echo's display the same thing. However when you pass it through a for loop, the two behave differently. $* displays the entire sentence as one string and $@ displays each word in the sentence individually.

    $ ./positional_parameters If you see a faded sign at the side of the road that says...
    If you see a faded sign at the side of the road that says...
    If you see a faded sign at the side of the road that says...
    
    If you see a faded sign at the side of the road that says...
    If
    you
    see
    a
    faded
    sign
    at
    the
    side
    of
    the
    road
    that
    says...
    


© Lightnetics 2024