$ @ Refers to all command line parameters. Each parameter is enclosed ""
$ * Indicates all command line parameters
The following procedure:
Test1.sh
-------------------
Sum = 0
For VaR
In "$ @"
Do
Let sum = sum + $ VaR
Done
Echo "sum = $ sum"
./Test1.sh 1 2 3
The result is sum = 6.
Test2.sh
--------------------
Sum = 0
For VaR
In "$ *"
Do
Let sum = sum + $ VaR
Done
Echo "sum = $ sum"
./Test2.sh 1 2 3
The result is sum = 1.
Analysis 1:
Bash-x test1.sh
The result is as follows:
+ Sum = 0
+ For VAR in '"$ @"'
+ Let sum = sum + 1
+ For VAR in '"$ @"'
+ Let sum = sum + 2
+ For VAR in '"$ @"'
+ Let sum = sum + 3
+ Echo sum = 6
Sum = 6
It can be seen that for is executed three times, indicating that for is bounded by "", because each parameter is surrounded ""
Analysis 2:
Bash-x test2.sh
The result is as follows:
+ Sum = 0
+ For VAR in '"$ *"'
+ Let sum = sum + 1 2 3
+ Echo sum = 1
Sum = 1
It can be seen that the loop is executed only once, indicating that $ * is surrounded by "" as a whole, not every parameter is surrounded by "", so it is only executed once
A more direct example is:
For VaR
In "a B C"
Do
Echo "$ {var }@"
Done
Print:
A B C @
For VaR
In "A" "B" "C"
Do
Echo "$ {var }@"
Done
Print:
A @
B @
C @