string of Shell intercept variable

Source: Internet
Author: User
Tags arithmetic first string

Suppose there is a variable var=http://www.linuxidc.com/test.htm
A # Intercept, delete the left character, and leave the right character.
Echo ${var#*//}
where Var is the variable name and the # is an operator, *//indicates that the first//number and all characters on the left are deleted from the left.
Delete http://
The result is: www.linuxidc.com/test.htm

Two # # Intercept, delete the left character, leave the right character.
Echo ${var##*/}
##*/indicates that the last (rightmost) one/number and all characters to the left are deleted from the left.
That is, delete http://www.linuxidc.com/
The result is test.htm.
Triple Intercept, delete the right character, leave the left character
Echo ${var%} #显示结果sbin/ntpdate
3.2 Delete the longest match by starting with the preceding string
Syntax: ${variables to be tested # #样式}
Instance:
Filename= "/usr/sbin/ntpdate"
Echo ${filename##} #显示结果sbin/ntpdate
3.2 Delete the longest match by starting with the preceding string
Syntax: ${variables to be tested # #样式}
Instance:
Filename= "/usr/sbin/ntpdate"
Echo ${filename##/*/} #显示结果ntpdate
3.3 Starting from behind the string delete the shortest match
Syntax: ${the variable to be measured% style}
Instance:
Filename= "/usr/sbin/ntpdate"
Echo ${filename%/*} #输出结果/usr/sbin
3.4 Delete the longest match by starting from behind the string
Syntax: ${the variable to be measured in percent style}
Instance:
Filename= "Hello/usr/sbin/ntpdate"
Echo ${filename%%/*} #输出结果hello

4. Replace or delete a partial string

4.1 Replace only the first string found
Syntax: ${variables to be tested/style/replace}
Instance:
str= "Hello hello"
Echo ${str/hello/world} #结果为world Hello
4.2 Replace all found style strings
Syntax: ${variables to be tested//style/replace}
Instance:
str= "Hello hello"
Echo ${str/hello/world} #结果为world World
4.3 Delete the first specified string
Syntax: ${the variable to be tested/the string to delete/}
Instance:
str= "Hello hello"
Echo ${str/hello/} #结果为hello
4.4 Delete all the specified strings
Syntax: ${variables to be tested//variables to be deleted/}
Instance:
str= "Hello World Hello"
Echo ${str//hello/} #输出结果为world
5. Command Extensions
Syntax: $ (command)
Example: time=$ (date) equivalent time= ' Date '
6. Arithmetic expansion
Syntax: $ ((arithmetic))
Instance:
echo "a=$ ((5*5))" #结果为a =25

string of Shell intercept variable

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.