What is the difference between $ @ and $ * in shell learning?
Before talking about $ @ and $ *, you must start with the positional parameter of shell script...
We all know how variables are defined and replaced.
However, we also need to know that some variables are set in shell, and their names cannot be modified at will,
Positional parameter is included. In shell script, variables such as $0, $1, $2, $3... can be used to extract the parameters in the command line,
For example, if ls-l ls is $0 and-l is $1
First, it is $ #: it can capture the number of positional parameter.
The previous my. sh p1 "p2 p3" is used as an example:
Because the IFS between p2 and p3 is in soft quote, $ # can get the value of 2.
Next is $ @ and $ *:
Specifically, the two are only different in soft quotes. Otherwise, they all represent "All Parameters" (except $0 ).
For example, if you run my. sh p1 "p2 p3" p4 on command line,
For $ @ or $ *, you can get p1 p2 p3 p4.
However, if it is placed in soft quotes:
"$ @", You can get the three different word segments "p1" "p2 p3" "p4 );
"$ *", You can obtain the entire string of "p1 p2 p3 p4.
Modify my. sh to make the content as follows:
Code:
#! /Bin/bash
My_fun (){
Echo "$ #"
}
Echo 'the number of parameter in "$ @" is '$ (my_fun "$ @")
Echo 'the number of parameter in "$ *" is '$ (my_fun "$ *")
Then run./my. sh p1 "p2 p3" p4 to know where the difference between $ @ and $ * is... ^_^.