${0##*/} What does it mean?
We'll find out by doing an experiment.
Create a new directory--mkdir-p/usr/school/grade/class/
Create a new file--touch/usr/school/grade/class/student
Write the code in the student file and save it--
Echo
Echo ${0#*/}
Echo ${0##*/}
Run Code--sh/usr/school/grade/class/student
We're going to get three results, respectively.
/usr/school/grade/class/student
Usr/school/grade/class/student
Student
I'll explain the three instructions at this time.
$0,$1,$2 anyway there is a $ dollar sign in front of it, all symbolizing the variable, and the $ A symbol itself is the name of the shell script file, which is/usr/school/grade/class/student
The rightmost/, symbolizing what you are looking for, what you want to match, where to look? Look inside the file name/usr/school/grade/class/student.
#象征要寻找最左边的/
# #象征要寻找最右边的/
and * is a wildcard character, symbolizing any length of string
So in # #条件下的 * *, it's a symbol of/usr/school/grade/class/
After finding this section, we'll intercept it, throw it away, and the rest is student the file name.
So the role of ${0##*/} is to look for a specific file name under a path
/usr/school/grade/class/student, you get student.
Look at the official explanation again.
${parameter#word}
${parameter# #word}
The word is expanded to produce a pattern just as in filename expansion (see section 3.5.8 filename expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded Value of parameter with the shortest matching pattern (the ' # ' case) or the longest matching pattern (the ' # # ' case) delet Ed. If parameter is ' @ ' or ' * ', the pattern removal operation are applied to all positional parameter in turn, and the Expansi On is the resultant list. If parameter is a array variable subscripted with ' @ ' or ' * ', the pattern removal operation are applied to each member of The array in turn, and the expansion is the resultant list.
One more example.
On the SUSE system, this script/sbin/service
Root to be able to use service, otherwise it will error
[Email protected]:~>/sbin/service
Service:only Root can use service
Service this thing is from above/sbin/service through Echo ${0##*/} get
You can verify the code inside the cat
if test "$ (id-u)"-ne 0; Then
echo "${0##*/}: Only the root can use ${0##*/}" 1>&2
Exit 1
Fi
We'll see the next, ${0%/*}
Just post the official website to explain
${parameter%word}
${parameter%%word}
The word is expanded-produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion E of parameter with the shortest matching pattern (the '% ' case) or the longest matching pattern (the ' percent ' case) deleted. If parameter is ' @ ' or ' * ', the pattern removal operation are applied to all positional parameter in turn, and the Expansi On is the resultant list. If parameter is a array variable subscripted with ' @ ' or ' * ', the pattern removal operation are applied to each member of The array in turn, and the expansion is the resultant list.
${0##*/}
${0%/*}
What these two commands have in common is to intercept, throw away, the former throws out the part, and the latter throws the * * This Part
The difference is that, #是从左边头部开始寻找起, the% is looking up from the tail (If the pattern matches a trailing portion)
${0%/*} This command, for/usr/school/grade/class/student, from the right to find, get is/student this part, and then throw away
If it's ¥{0%%/*}, you'll find/usr/school/grade/class/student.
Sum up
For the same file/usr/school/grade/class/student
${0%/*} get Front/usr/school/grade/class
${0##*/} got the back student.
The Meaning of ${0##*/}