Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between $@ and $* in bash scripting [duplicate]

Tags:

bash

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.

like image 267
Kyrol Avatar asked Jul 31 '13 09:07

Kyrol


People also ask

What is the difference between $@ and $* in bash?

There is no difference if you do not put $* or $@ in quotes. But if you put them inside quotes (which you should, as a general good practice), then $@ will pass your parameters as separate parameters, whereas $* will just pass all params as a single parameter.

What is $@ and $* in shell script?

• $* - It stores complete set of positional parameter in a single string. • $@ - Quoted string treated as separate arguments. • $? - exit status of command.

What does $@ do in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What is the role of $0 $@ $* variable is shell script?

Used to reference the name of the current shell or current shell script.


2 Answers

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.

like image 161
Aaron Cronin Avatar answered Oct 15 '22 00:10

Aaron Cronin


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 --
like image 3
fedorqui 'SO stop harming' Avatar answered Oct 15 '22 00:10

fedorqui 'SO stop harming'