There is a say no difference between $@ and $* in Shell! What ?!!
Have you ever thought if there is no difference, why there are two of them? it could be one $@ or $* , Do You Agree?
So Let’s see What the difference is . Create a script and try it one by one
First One $@ :
#! /bin/bash
MAIN()
{
echo "First Parameter is $1"
}
MAIN $@
Execute it like below and give the script some arguments:
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon
Second One $* :
#! /bin/bash
MAIN()
{
echo "First Parameter is $1"
}
MAIN $*
The result will be :
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon
Far Now There is no difference, maybe those people were right!
Let us go further and try them with double quotes "$@" :
#! /bin/bash
MAIN()
{
echo "First Parameter is $1"
}
MAIN "$@"
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon Smith
The second one in double quotes "$*" :
#! /bin/bash
MAIN()
{
echo "First Parameter is $1"
}
MAIN "$*"
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon Smith Marry Ann
So be careful when you use double quotes and tell those people there is a difference, explain to them shell is sensitive to double quotes.
Top comments (2)
Double Quotes are necessary.
I learned something here.