What is the difference between Shell $* and $@
This article shows you the difference between Shell $* and $@. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.
The difference between Shell $* and $@
When $* and $@ are not surrounded by double quotation marks, there is no difference between them, each parameter received is treated as a piece of data, separated by spaces.
But when they are enclosed in double quotes, there is a difference:
"$*" treats all parameters as a data as a whole, rather than each parameter as a data.
"$@" still treats each parameter as a piece of data, independent of each other.
For example, if five parameters are passed, for "$*", the five parameters will be merged together to form a piece of data, which can not be separated; for "$@", the five parameters are independent of each other. They are five pieces of data.
If you use echo to output "$*" and "$@" directly, you can't see the difference; but if you use a for loop to output data one by one, you can immediately see the difference.
For the use of for loops, please click: Shell for loops and for int loops
Write the following code and save it as test.sh:
#! / bin/bashecho "print each param from\"\ $*\ "" for var in "$*" do echo "$var" doneecho "print each param from\"\ $@\ "" for var in "$@" do echo "$var" done
Run test.sh with parameters:
[mozhiyan@localhost demo] $. . / test.sh a b c d
Print each param from "$*"
A b c d
Print each param from "$@"
A
B
C
D
From the running results, you can see that for "$*", it only loops once because it has only 1 point of data; for "$@", it loops five times because it has five pieces of data.
What is the difference between Shell $* and $@? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.