Give me a chestnut:
V=jfedu.shiyiwen.comecho ${v%.*}
A% is a non-greedy match, which matches the shortest result. % a non-greedy match from right to left, what does it match? Matches characters that have. *. Then of course it matches to. com and then executes the delete match to the character. The result is (the "." In this case equals the delimiter.) and "*" is a wildcard character)
Jfedu.shiyiwen
Echo ${v%%.*}
The greedy match is executed here, which matches the longest match. Shiyiwen.com result is
jfedu
Here is saying a # just as opposed to the% order, #是从左到右来进行匹配的. The usual: Give me a chestnut.
V=jfedu.shiyiwen.comecho ${v#*.} Shiyiwen.comecho ${v##*.} Com
There are many uses, the more common is very convenient to get the file suffix name and file name. Give me a chestnut.
01.#!/bin/bash . 03.num=1 04.for i in *.tar *.tar.gz 05.do 06.new=new_$num.${i#*.} 07.MV $i $new 2>/dev/null . 09.if [$?-eq 0];then . echo "Remove $i to $new" . Let num++ 12.fi . 14.done
In addition to some:
${#VALUE}: Calculates the number of characters in the VALUE string.
- ${value%. *} or ${valuepercent. *}: Remove the VALUE string with the delimiter "." Matches the right character, preserving the left character.
- ${value#*.} or ${value# #*.} : Remove the value string with the delimiter "." Matches the left character, preserving the right character.
- ${value/old/new} or ${value//old/new} : Replaces the old substring in the value string with the new substring.
add:"*" indicates a wildcard character used to match strings that will be deleted. “.” Represents a delimiter in a string, which can be any one or more characters. "%" means right-to-left matching, "#" means matching from left to right, "\" for substitution, all of which are non-greedy matches, which match the shortest result matching the wildcard character. "percent", "# #" and "//" are similar to "%", "#" and "/", all of which are greedy matches, that is, match the longest result that matches the wildcard character.
- ${value: OFFSET} or ${value: offset:length}: Intercepts substrings from the left of the VALUE string.
- ${value: 0-offset} or ${value: 0-offset:length} : Intercepts the substring from the right start of the value string.
added: The first character on the left starts with "0" and the first character on the right begins with "0-1". Represents the offset at which a character is to be truncated and length. If there is no length variable, the offset is shifted from the beginning of the character to the end of the string.
- ${value:-word}: When the variable is undefined or the value is empty, the return value is the contents of WORD, otherwise the value of the variable is returned.
- ${value: =word}: When the variable is undefined or the value is empty, returns the value of Word and assigns word to value, otherwise returns the value of the variable.
- ${value: +word}: When a variable is assigned a value, its value is replaced with WORD, otherwise no substitution is made.
- ${value:? MESSAGE}: Normal replacement When the variable is assigned a value. Otherwise, message messages are sent to the standard error output (if this substitution appears in the shell program, the program terminates).
Add: Word can be a string, or it can be a variable. When you are a variable, you need to refer to the variable with "$".
For additional ${} use, please see another blog post.
Greedy and non-greedy matches in the shell