How the shell intercepts strings

Source: Internet
Author: User

There are a number of ways to intercept strings in the shell, ${expression} There are 9 ways to use them. ${parameter:-word}${parameter:=word}${parameter:?word}${parameter:+word} The above 4 alternatives can be used for default values. ${#parameter} This can get the length of the string. ${parameter%word} minimum intercept from the back Word${parameter%%word} to maximize the interception from the back Word${parameter#word} minimal interception from the front word${parameter# #word} The best way to intercept word above 4 is to intercept the string. With four usages, you don't have to use the cut command to intercept a string. The first type can be divided into four cases, described below. 1. Use the # operator. The purpose is to delete the first occurrence of the substring, the left character, and the right character, starting from the left. Usage is #*substr, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str#*//} results from www. your domain name. com/ Cut-string.html, that is, delete all characters from the left to the first "//" and their left 2, using the # # operator. The purpose is to delete the last occurrence of the substring, which is the left character, from the left side, leaving the right character. Usage is ##*substr, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str##*/} results in cut-string.html, that is, delete the last occurrence of "/" And all the characters on the left 3, use the% number operator. The purpose is to delete the first occurrence of the substring, the right character, and the left character, starting from the right. Usage is%substr*, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str%/*} results in http://www. your domain name. com, i.e. delete from right to first "/" And its right all characters 4, use the percent sign operator. The purpose is to delete the last occurrence of the substring, which is the right character, from the right, leaving the left character. Usage is%%substr*, for example: Str= ' http://www. your domain name. com/cut-string.html ' echo ${str%%/*} results in http://www. your domain name. com, which is deleted from the right sideStart to Last "/" and all characters to the right of the second type is also divided into four types, respectively, the following: 1, starting from the left of the first few characters and the number of characters, using: Start:len, for example: Str= ' http://www. your domain name. com/cut-string.html ' Echo ${var:0:5} where 0 means the first character on the left begins, and 5 indicates the total number of characters. The result is: Http:2, starting from the first few characters on the left until the end, using: Start, for example: Str= ' http://www. your domain name. com/cut-string.html ' Echo ${var:7} of which 7 The 8th character on the left starts with the result: www. your domain name. COM/CUT-STRING.HTML3, starting with the first few characters on the right and the number of characters, usage: 0-start:len, for example: Str= ' http://www. your domain name. com/ Cut-string.html ' echo ${str:0-15:10} where 0-6 means the 6th character starts at the right, and 10 indicates the number of characters. The result is: Cut-string3, starting from the first few characters on the right to the end, using: 0-start, for example: Str= ' http://www. your domain name. com/cut-string.html ' Echo ${str:0-4} of which 0-6 Indicates that the 6th character starts at the right, and 10 indicates the number of characters. The result is: HTML Note: (the first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)

There are many ways to intercept strings in the shell,
${expression} A total of 9 ways to use.
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
The above 4 alternatives can be used for default values.
${#parameter}
The length of the string can be obtained from the above.
${parameter%word} minimum intercept word from behind
${parameter%%word} to maximize the interception of word from behind
${parameter#word} minimal truncation of word from the front
${parameter# #word} to maximize the interception of word from the front
The above 4 is a way to intercept a string.
With four usages, you don't have to use the cut command to intercept a string.
The first kind can be divided into four kinds of cases, described below.

1. Use the # operator. The purpose is to delete the first occurrence of the substring, the left character, and the right character, starting from the left. Usage is #*substr, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str#*//}
The result is www. your domain name. com/cut-string.html, that is, delete all characters from the left to the first "//" and their left 2, using the # # operator. The purpose is to delete the last occurrence of the substring, which is the left character, from the left side, leaving the right character. Usage is ##*substr, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str##*/}
The result is cut-string.html, which deletes the last "/" and all of its left characters.
3, use the% operator. The purpose is to delete the first occurrence of the substring, the right character, and the left character, starting from the right. Usage is%substr*, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str%/*}
The result is http://www. your domain name. com, which deletes all characters from the right to the first "/" and to the right of the
4. Use the percent sign operator. The purpose is to delete the last occurrence of the substring, which is the right character, from the right, leaving the left character. Usage is%%substr*, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str%%/*}
The result is http://www. your domain name. com, which deletes all characters from the right to the last "/" and to the right of the

The second type is also divided into four types, respectively, as follows:
1, starting from the first few characters on the left and the number of characters, use: Start:len, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${var:0:5}
0 represents the first character on the left and 5 indicates the total number of characters.
The result is: http:
2, starting from the first few characters on the left to the end, the usage is: start, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${var:7}
Where 7 means the 8th character on the left starts
The result is: www. your domain name. com/cut-string.html
3, starting from the right of the first few characters and the number of characters, usage: 0-start:len, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str:0-15:10}
0-6 means that the 6th character starts at the right, and 10 indicates the number of characters.
The result is: cut-string
3, from the right to the beginning of the first few characters until the end, usage: 0-start, for example:
Str= ' http://www. your domain name. com/cut-string.html '
Echo ${str:0-4}
0-6 means that the 6th character starts at the right, and 10 indicates the number of characters.
The result is: HTML
Note: (The first character on the left is denoted by 0, and the first character on the right is denoted by 0-1)

How the shell intercepts strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.