Common script programming knowledge points

Source: Internet
Author: User

1, the length of the characters in the variable: ${#VARNAME}

#A = ' Hello World '

#echo ${#A}--Displays the length of the defined variable A, with an output of 11

#11

2, variable assignment value, etc.:

${parameter:-word}: If parameter is empty or undefined, the variable expands to "word"; otherwise, expands to the parameter value;

${parameter:+word}: If parameter is empty or undefined, do nothing, otherwise expand to Word value;

${parameter:=word}: If parameter is empty or undefined, the variable expands to "word" and assigns the expanded value to parameter;

${parameter:offset}: Remove the defined offset number

${parameter:offset:length}: Takes a substring, starts at the last character at the offset, and takes the lenth long substring

--------------------------------------------------------

The most commonly used is the first, such as:

${parameter:-word}

#A =3

#echo ${a:-30}---indicates that the variable A is not empty, the expression uses the value of the variable itself

#3

#unset A

#echo ${a:-30}---indicates that the variable A is empty, the expression uses its own value

#30

#A =${a:-30}---indicates that variable A is empty, it is assigned to the variable a with its own value

#echo $A---to indicate that the variable A is not empty, it is assigned to the variable a again using the value of variable a

#30

${parameter:+word}

#A =3

#echo ${a:+30}---indicates that the value of Word is used when variable A is not empty

#3

#unset A

#echo ${a:+30}---indicates that the variable A is empty and the value is null

#为空

${parameter:=word}

#unset A

#echo ${a:=30}--Indicates that the variable a is empty, not only expands the expression to this string, but assigns the string value to a

#30

${parameter:offset}

#A = ' Hello World '---define a variable

#echo ${a:2}---Number 2 indicates that the string in variable A is offset 2 from left to right, showing everything from the offset

#llo World

${parameter:offset:length}

#echo ${a:2:3}---Number 2 indicates that the string in variable A is offset from left to right 2, and the number 3 indicates that 3 characters are taken from the offset position

#llo

-----------------------------------------------------------

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.