5

I'm new in bash and I'm learning it, and I have a doubt about the real difference between the use of $@ and S*.

I red here Bash Special Parameters

I understand that both expand to the positional parameters, but the difference occurs within double quotes. By the way "$@" = "$1" "$2"..."$n" could be different than "S*" = "$1$2...$n".

I try to understand it with a simple script:

if [ $# -gt 0 ]; then
       echo "Your command line contains $# arguments" 
else
       echo "Your command line contains no arguments"
       exit  fi

echo "Params are: "
echo $@  
echo $* 
echo "$@"    
echo "$*"

if I execute my script in the terminal like this ~./my_script par1 par2 par3

the result is always the same:

Params are:
par1 par2 par3
par1 par2 par3
par1 par2 par3
par1 par2 par3

Maybe I don't understand the real use of both special variables and If my example is correct or not. I'd like to figure out this point also with a good example.

Kyrol
  • 3,475
  • 7
  • 34
  • 46
  • Also see http://stackoverflow.com/questions/3008695/what-the-difference-between-and-in-bash – devnull Jul 31 '13 at 09:54
  • Ah ok. Sorry for duplicate question, but I'd like to understand also in my example. – Kyrol Jul 31 '13 at 10:00
  • 1
    The linked questions have pretty *detailed* explanations. See http://stackoverflow.com/a/3008734/2235132 and http://stackoverflow.com/a/2761739/2235132 and http://stackoverflow.com/a/14247889/2235132 and http://stackoverflow.com/a/14247867/2235132 – devnull Jul 31 '13 at 10:06
  • Tons of explanations! Thank you! – Kyrol Jul 31 '13 at 10:11

2 Answers2

4

They may appear the same when you are using echo but this is due to them being treated the same by echo and not being equivalent.

If pass three command-line arguments given to a bash script to a C program using ./my_c $@,

you get the result ARGV[1] == "par1" ARGV[2] == "par2" ARGV[3] == "par3".

If you pass three command-line arguments given to a bash script to a C program using ./my_c $*,

you get the result ARGV[1] == "par1 par2 par3".

(ARGV is the array of supplied arguments in C, the first element is always the command-name the program was invoked with)

It's to allow greater flexibility with what you do with the given parameters later in the script.

Aaron Cronin
  • 2,093
  • 14
  • 13
3

From http://tldp.org/LDP/abs/html/refcards.html:

"$*" All the positional parameters (as a single word) *

"$@" All the positional parameters (as separate strings)

This code shows it: given a string with items separated by spaces, $@ considers every word as a new item, while $* considers them all together the same parameter.

echo "Params for: \$@"
for item in "${@}"
do
        echo $item --
done

echo "Params for : \$*"
for item in "${*}"
do
        echo $item --
done

Test:

$ ./a par1 par2 par3
Your command line contains 3 arguments
Params for: $@
par1 --
par2 --
par3 --
Params for : $*
par1 par2 par3 --
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • So the use of echo in my example is not explicative to understand the differece. – Kyrol Jul 31 '13 at 10:08
  • 1
    Exactly, @Kyrol . You can get more proper information when looping in the variable to show which "pieces" it is getting. – fedorqui Jul 31 '13 at 10:09
  • Just another doubt: in this case there are differences between `"${@}"` and `"$@"`in the `for` loops that you used? I think is the same thing. – Kyrol Jul 31 '13 at 13:51
  • Yes, @Kyrol, `${var_name}` and `$var_name` are exactly the same. I put this one because I was checking my answer in http://stackoverflow.com/a/14588210/1983854, where spaces did matter and where taken into account with `"${x[@]}"`. – fedorqui Jul 31 '13 at 13:59
  • I saw the question you answered. Thanks a lot for explanation again! ;) – Kyrol Jul 31 '13 at 14:09