Linux variable Operation record

Source: Internet
Author: User

Variable Operation Summary:


${var:-value} variable Var is unset or null to output Value. A value that outputs the var value of the variable.

${var:=value} variable var is unset or null to output value and is assigned to variable var. Ditto.

${var:+value} variable var is unset or null then the variable var is output. Value is output if there are values.

${var:? Value} variable var is unset or null to output error message value. A value that outputs the var value of the variable.


${var-value} variable Var is unset to output Value. If there is a value or is empty, the value of the variable var is output.

${var=value} variable var is unset output value and is assigned to variable var. Ditto.

${var+value} variable var is unset output variable var. Has a value or is empty, the output value

${var? Value} variable var is unset output error message value. Has a value or is empty, the output variable is var.


In this operation, there are only three cases: 1. The variable has a value, 2. The variable has no value, 3. No variables

In the above add: operator, NULL does not represent a value, in the following do not: The number of operators, NULL is also represented as a value.


${variable} generic variable reference. {} General conditions can be omitted.

${#变量} takes the number of characters (length) in the variable.




first, the value of the variable edit:

1. Variable character matching is not output. 2. Variable content character offset and truncate the remaining part of the character.

3. Substitution of variable characters.



1. The content of the variable to be matched will not be displayed.

Operator: # ${variable # match character}

$ do= "Hello World" $ echo $doHello world$ echo ${do#he}llo World #He没有显示 $ echo ${do#*l}lo World #从左到右匹配

# in the above variable application, the value of the left variable, according to the right side of the string from left to right the shortest area ,

The character to which the area is to be printed is not output. Wildcard characters are supported.


Operator: # #

$ echo ${do##*l}d #变量的值是Hello world, matching from left to last second character L.

# # #, this is the longest area to match . Remember that it is a left-to-right area order.


operator:% ${variable% match character}

$ echo $doHello world$ echo ${do%ld}hello wor$ echo ${do%o*}hello W

The% number is the same as the # above, except for right-to-left matching. The same% number is the shortest area with


operator: percent

$ echo ${do%%o*}hell

Two percent, maximum size. Wildcard characters are supported. After all, it is under the shell to interpret the output through the shell.




2. The above # percent is used to define the action that matches the contents of the variable. The following is an operation to define a fixed character.


${variable: number}

$ echo $doHello world$ echo ${do:3}lo World #变量内容的前三个字符没有显示.

The preceding 3 characters have no output, the professional is called the offset, and the right offset is 3 characters.


$ echo ${do:3:2}lo$ echo ${do:3:5}lo Wo

On top of the offset above, it shows several characters that are truncated backwards. Spaces can be characters, too.




3. Replace the variable contents.

${variable/match character/substitution character}

$ echo ${do/l/l}hello World

Replace the first matched character, which is L, with the uppercase L.

Here, only the first match to the character is replaced. If you change the back of the variable to two, it becomes the replacement.


${variable//match character/substitution character}

$ echo ${do//l/l}hello World

Instead of all L, you can use wildcards here, but there is no longest and shortest match, only one replacement or all of the characters. As for the effect, the parents can try it on their own.




Second, the variable ...., do not know what the name.

In this operation, there are only three cases: 1. The variable has a value, 2. The variable has no value, 3. No variables

Note: In general, a variable is null and also a value. Unless there is a special operation.

The do= do variable has a value.

DECLARE var generates a VAR variable, but the var variable has no value.


1. ${variable-value}


Inside is a minus sign. That value can also be used as a variable, but with $, the equivalent of a variable reference. -The left and right sides of the number are completely different two parts.


$ echo  $doHello  World$ echo ${do-kaka}                #在do有值的情况下, output the value of do. hello world                             $ unset do                        #删除变量do $ echo ${do-kaka}                #在没有do变量的情况下, i.e. no value. The following value is output. kaka$ do=                            #在do为空的情况下. $ echo ${do-kaka}                #在do有值的情况下, output do value, do is null value.                                                   #所以输出空.

The other way to do this is generally used when judging the default value. such as: Do=${do:-kaka}


2. ${variable = value} is an equal sign.

$ echo  $doHello  World$ echo ${do=kaka}                  #在do有值的情况下, output the value of do. hello world                             $ unset do$ echo  ${do=kaka}                  #在没有do变量的情况下, or no value. The value behind the output kaka                                     $ echo  $dokaka                                #并且把kaka赋给变量do. 

More than one function, as long as the variable, you will assign the Kaka to do.



3. ${variable + value} inside is the plus sign.

$ echo $doHello world$ Echo ${do+kaka} #do有值的情况下, output kakakaka$ unset do$ echo ${do+kaka} #在没有do这个变 The amount of the case or no value.                                        That's not right. $ do=$ echo ${do+kaka} #空值, Output Kakakaka

It looks exactly the same as-instead, this is a variable with a value followed by the value, and the-number is no value then use the following value.


4. ${variable? Value}

$ echo $doHello world$ echo ${do?kaka}hello world$ unset do$ Echo ${do?kaka} # See no, in the absence of do this variable or no value. # hint error message is kakabash:do:kaka$ do=$ Echo ${do?kaka}

In addition to the absence of the value of the case will be error, and the error message is Kaka, there is nothing else.



The following is still about-= +?   , but it becomes:-: =: +:? 。


Not the same is the empty value to erase, since then empty is really empty, empty no longer represents the value of the existence,

but like unset.



Here's a description, in the case of the shell. In fact, unset is the memory space of the variable address to be revoked,

The variable null value is the memory space address of the variable, but there is no data in the memory space.



1. ${variable:-value}

$ echo $doHello world$ echo ${do:-kaka}hello world$ unset do$ Echo ${do:-kaka} #没有do变量, equal to no value. Output kakakaka$ do=$ echo ${do:-kaka} #空不再代表值, do not have a value #输出kakakaka

What, the difference is only a little, null no longer represents a value.


2. ${variable: = value}

$ echo $doHello world$ echo ${do:=kaka}hello world$ echo $doHello world$ unset do$ echo ${do:=kaka}kaka$ Echo $dokaka $ do= $ echo ${do:=kaka} #空不再代表值, so the output kakakaka$ echo $dokaka

Or that means, Kaka output, and assign to do. Null no longer represents a value.


3. ${variable: + value}

$ echo  $doHello  World$ echo ${do:+kaka}                  #在do有值的情况下, Output Kakakaka$ unset do$ echo  ${do:+kaka}                  #在do不存在的时候, i.e. no value.   Output do value,                                   # The value of do does not exist, of course, and the output blank line. $ do=$ echo ${do:+kaka}                  #do为空, null no longer represents a value.   No value will not output the following value.                                     #同样的空白行.

The + number is a value that will use the value behind it. The exact opposite of the number.


4. ${variable:? Value}

$ echo $doHello world$ echo ${do:?kaka}hello world$ unset do$ Echo ${do:?kaka} #没有do变量, of course no value, reported Kaka error. bash:do:kaka$ do=$ Echo ${do:?kaka} #do为空, represents no value. reported Kaka errors. Bash:do:kaka

This is the same as the number, but also the output of the error message.






Good boy, nice girl.



This article is from the "on Foot" blog, make sure to keep this source http://fanqie.blog.51cto.com/9382669/1649669

Linux variable Operation record

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.