The shell string operator

Source: Internet
Author: User
Tags truncated

${varname:-word}

If varname exists and is not nil, returns the value of VarName, otherwise returns word. This is commonly used to return a default value when VarName is undefined

${varname:=word}

If varname exists and is not nil, return the value of varname, otherwise set the value of varname to Word and return the value. This is used to set default values for variables

${varname:?message}

If varname exists and is not nil, then return the value of varname, otherwise print varname:message, and interrupt the script (only established under non-interactive shell)

${varname:+word}

If VarName is present and is not nil, return to Word, otherwise nil

${varname:offset:length}

Starting with offset, the length of the substring is truncated, where the sequence number of the string is calculated from 0, there are several special cases:

1 if length is not specified, it is truncated to the end of the entire string

2 If offset is negative, the number of offset characters from the end of the string is followed, and the length is truncated, and if length is not specified, it is truncated to the end of the entire string, such as:

Name=-3:2-3} output def

It is important to note that there is a space between this and: to let the shell differentiate ${varname:-word} form

3 if VarName is * or @, then it is starting from the first offset position parameter, intercept length, if not specified length, then it is intercepted to the last position parameter

${varname#pattern}

Match from the beginning of the varname, then delete the shortest match and return the remaining string

${varname# #pattern}

Start the match from the beginning of the varname, and then delete the longest match, returning the remaining string

${varname%pattern}

Matches from the end of the varname, and then deletes the shortest match, returning the remaining string

${varnam%%pattern}

Matches from the end of the varname, and then deletes the longest match, returning the remaining string

${varname/pattern/string}

Replaces the first longest occurrence with a string, or if the string is nil, deletes the match

${varname//pattern/string}

Replaces all the longest occurrences with a string, or if the string is nil, deletes the match

In the above two substitution modes, if pattern starts with #, the match starts with varname, and if varname is * or @, then the substitution is done sequentially for each position parameter.

The shell string operator

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.