"Go" shell programming: a colon followed by an equal sign, plus, minus, a question mark meaning

Source: Internet
Author: User

Original URL: http://blog.csdn.net/trochiluses/article/details/9048539

default value (:-)  

If the variable is followed by a colon and a minus sign, then the variable is followed by the default value of the variable.

$ company=

$ printf "%s/n" "${company:-unknown Company}"

Unknown Company

The actual value of the variable can remain unchanged.

Colons can also be omitted without:

$ company=

$ printf "%s/n" "${company-nightlight Inc"

$

Specify default value (: =)

If the variable is followed by a colon and an equal sign, a default value is given to the empty variable.

$ printf "%s/n" "${company:=nightlight Inc"

Nightlight INC.

$ printf "%s/n" "$COMPANY"

Nightlight INC.

The actual value of the variable has changed.

When a colon is removed, no default value is specified.

Whether the variable exists check (:?)

If the variable is followed by a colon and a question mark, different information is displayed depending on whether the variable exists. The information is not required.

printf "Company is%s/n"/

"${company:?" Error:company has notbeen defined-aborting} "

If there is no colon, the check is not performed.

overriding default values (: +)

If the variable is followed by a colon and a plus sign, add the following string to replace the default string.

$ company= "Nightlight Inc."

$ printf "%s/n" "${company:+company has been overridden}"

Company have been overridden

If there is no colon, the variable is replaced by the string, and the value of the variable itself does not change.

Replace partial string (: N)

If a variable is followed by a colon and a number, a substring that starts with the number is returned, if followed by a colon and a number. The first number represents the starting character, followed by the number that represents the length of the character.

$ printf "%s/n" "${company:5}"

Light Inc.

$ printf "%s/n" "${company:5:5}"

Light

Delete a string based on a template (%,#,%%,##)

If the variable is followed by a pound sign, the string after which the matching template is deleted is returned. A well number is a match for the smallest possibility, and two well numbers are a match for the possibility of arrogance. An expression returns the character to the right of the template.

$ printf "%s/n" "${company#ni*}"

Ghtlight INC.

$ printf "%s/n" "${company# #Ni *}"

$ printf "%s/n" "${company##*t}"

Inc.

$ printf "%s/n" "${company#*t}"

Light Inc.

Using a percent semicolon, the expression returns the character to the left of the template

$ printf "%s/n" "${company%t*}"

Nightligh

$ printf "%s/n" "${company%%t*}"

Nigh

(The above commands are not available in my Linux system)

To replace a substring with a template (//)

If there is only one slash behind the variable, the string in the middle of the two slash is the string to be replaced, and the string following the second slash is the string to replace. If the variable is followed by two slashes, all characters that appear in the middle of the two slash are replaced with the characters that follow the last slash.

$ printf "%s/n" "${company/inc./incorporated}"

Nightlight Incorporated

$ printf "You is the I in%s" "${company//i/i}"

You is the I in Nightlight Inc.

If the template one # starts, matches the character starting with the template and matches the character ending with the template if the template ends with a% number.

$ company= "Nightlight Night Lighting Inc."

$ printf "%s/n" "$COMPANY"

Nightlight Night Lighting Inc.

$ printf "%s" "${company//night/night}"

Nightlight Night Lighting Inc.

$ printf "%s" "${company//#Night/night}"

Nightlight Night Lighting Inc.

(This doesn't work in my Linux)

If no new value is specified, the matching character is deleted.

$ company= "Nightlight Inc."

$ printf "%s/n" "${company/light}"

Night Inc.

You can also use the range symbol. For example: Remove punctuation from all strings, use range [:p UNCT:].

$ printf "%s" "${company//[[:p UNCT:]]}"

Nightlight INC

Replacing a variable with an asterisk or an @ symbol replaces all parameters in the shell script, as well as replacing all elements in an array with an asterisk or an @ symbol.

"Go" shell programming: a colon followed by an equal sign, plus, minus, a question mark meaning

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.