Shell substitution operators and pattern matching operators

Source: Internet
Author: User

Shell substitution operators and pattern matching operators

Substitution operators:
(1) ${varname:-word}: If varname exists and is not NULL, its value is returned, otherwise, word is returned.
(2) ${varname:=word}: If varname exists and is not NULL, its value is returned, otherwise, it is set to Word and returned.
(3) ${varname:?message}: Returns the value if VarName exists and is not NULL, otherwise, returns varname:message and exits the current command or script. Omitting the message will show the default information, parameter null or not set. Often used to catch errors that result from undefined variables.
(4) ${varname:+word}: Returns word if varname exists and is not NULL, otherwise NULL is returned. The existence of a use test variable, such as ${count:+1},count if it exists and is not NULL, returns 1 (also true)
The colon (:) inside the above operator is optional. If omitted, the existence of the above interpretation and non-null are changed to exist.

Pattern matching operators:
(1) ${variable#pattern}: If the pattern matches the beginning of the variable, the shortest part of the match is deleted and the remainder is returned.
(2) ${variable# #pattern}: If the pattern matches the beginning of the variable, the longest portion of the match is deleted and the remaining portion is returned.
(3) ${variable%pattern}: If the pattern matches at the end of the variable, the shortest part of the match is deleted and the remainder is returned.
(4) ${variable%%pattern}: If the pattern matches the end of the variable, the longest portion of the match is deleted and the remainder is returned.
Pattern matching operators are commonly used to slice parts of a path name, such as a directory prefix and a file name suffix.

Shell substitution operators and pattern matching operators

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.