Quantcast
Channel: What is the difference between $* and $@? - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 12

Answer by Wojtek Rzepala for What is the difference between $* and $@?

$
0
0

The code you provided will give the same result. To understand it better, try this:

foo () {
    for i in "$*"; do
        echo "$i"
    done
}

bar () {
    for i in "$@"; do
        echo "$i"
    done
}

The output should now be different. Here's what I get:

$ foo() 1 2 3 4
1 2 3 4
$ bar() 1 2 3 4
1
2
3
4

This worked for me on bash. As far as I know, ksh should not differ much. Essentially, quoting $* will treat everything as one word, and quoting $@ will treat the list as separate words, as can be seen in the example above.

As an example of using the IFS variable with $*, consider this

fooifs () {
    IFS="c"            
    for i in "$*"; do
        echo "$i"
    done
    unset IFS          # reset to the original value
}

I get this as a result:

$ fooifs 1 2 3 4
1c2c3c4

Also, I've just confirmed it works the same in ksh. Both bash and ksh tested here were under OSX but I can't see how that would matter much.


Viewing all articles
Browse latest Browse all 12

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>