Shell
The first type:
${parameter%word} minimal truncation of word from behind
${parameter%%word} to maximize the truncation of word from the back
${parameter#word} minimal truncation of word from the front
${parameter# #word} to maximize the truncation of word from the front
Word can be a specific string, or it can be a pattern string.
Example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str%/*}
Results: http://www. your domain name. com
Echo ${str%%/*}
Result: http:
Echo ${str#*//}
Result: www. your domain name. com/cut-string.html
Echo ${str##*/}
Results: cut-string.html
The second type:
${VARIABLE:N1:N2}: Intercept variable variable the N2 character starting at index N1 from the left. N1 represents the index, which starts at 0, and N2 indicates the number of characters intercepted.
Variants are as follows:
${VARIABLE:N1}: Intercept variable variable all characters starting at index N1 from the left.
${VARIABLE:0-N1:N2}: Intercept variable variable the N2 character starting with the N1 character from the right.
${VARIABLE:0-N1:N2}: Intercept variable variable all characters starting with the N1 character from the right.
Example:
Variable= ' http://www. your domain name. com/cut-string.html '
Echo ${variable:0:4}
Result: http
Echo ${variable:7}
Result: www. your domain name. com/cut-string.html
Echo ${variable:0-15:10}
Results: cut-string
Echo ${variable:0-15}
Results: cut-string.html
The third type:
With other shell commands, such as cut
There are several main options for the Cut command:
echo $variable | Cut-c1-4
Result: http
echo $variable | cut-c8-
Result: www. your domain name. com/cut-string.html
echo $variable | Cut-d ":"-f1
Result: http
Summary of Shell,awk,sed interception string method in Linux