Tag: Shell
Before talking about [email protected] and $ *, you need to 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.
[Email protected] 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,
Either [email protected] or $ *, you can get P1 P2 P3 P4.
However, if it is placed in soft quotes:
"[Email protected]", 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 "[email protected]" is '$ (my_fun "[email protected]")
Echo 'the number of parameter in "$ *" is '$ (my_fun "$ *")
Then run./My. Sh p1 "p2 P3" P4 to find out what is the difference between [email protected] and $ *... pai_^.
What is the difference between [email protected] and $ * in Shell learning?