Linux variable operation

Source: Internet
Author: User
Tags truncated



First, it is the editor of the variable:

1. The variable character matches the delete output. 2. The variable character is fixed offset and the character is truncated.

3. Substitution of variable characters.


1. Give an example first: about the # number. ${variable # match character}

$ do= "Hello World" $ echo $doHello world$ echo ${do#he}llo world$ 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 that the region is matching to deletes the output. Wildcard characters are supported.

$ echo ${do##*l}d

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


One more example: about the% number. ${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 match.

$ 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 is used to define the action of matching characters. The following is an operation to define a fixed character.


${variable: number}

$ echo $doHello world$ echo ${do:3}lo World

The first 3 characters delete the output, the professional is called the offset, and the right is offset by 3 characters.


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

Based on the above offset, the output several characters are then truncated. A space is a character.




3. Here is the character used to replace the variable. ${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.

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

Replace all of the L, this can also use wildcards, but there is no longest shortest match said, only replace one or replace all. As for the effect, the parents can try it on their own.





Second, the value of the variable, forget what is the name of the study, and then change.


The variable is empty and is also a value.




1. ${variable-value}, with a minus sign inside. 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}Hello World                              # outputs the Do value in the case where do has a value. $ unset do$ echo ${do-kaka}                       # in the absence of a do variable, there is no value.   Output Kaka. kaka$ do=$ echo ${do-kaka}                       # when do has a value, output do value, do is null value.                                                          # so the output is empty. 

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

To reveal a point, plus: will represent null no longer judged as a value, the last example above will output Kaka



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

$ echo  $doHello  World$ echo ${do=kaka}                       # outputs the Do value in the case where do has a value. hello world                             $ unset do$ echo  ${do=kaka}                       # without a Do variable, there is no value. Output Kaka. kaka                                    $  echo  $dokaka                                      # and give the Kaka to the variable do. 

The ratio-Adds an item, as long as the output Kaka, will assign the Kaka to do.



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

$ echo  $doHello  World$ echo ${do+kaka}                          #do with value, the output kakakaka$ unset do$ echo ${do+kaka}                          # without the Do variable, there is no value. That's not right.                                            #$ do=$ echo ${do+kaka}                         # null value, 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, no                                         There is a 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.

and bash (one of the shell's) variables are on-demand, so there is no such situation where no variables occur.


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} #空不再代表值, in case 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}                         # In the case where do has value, the output kakakaka$ unset do$ echo ${do:+kaka}                         # When do does not exist, that is, there is no value.   Output do value,                                             #而do的值当然是不存在的,  However, it will still output blank lines. $ do=$ echo ${do:+kaka}                          #do空着手来, the hand is no longer a value.  kaka is not going to ignore it.                                              #同样的空白行.

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, report 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.



Summary of variable values:


${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.






Do not ask to be able to help you how much, just don't mislead everyone. If any friend finds a mistake, please help to point it out.



This article is from the "Big Tomato" blog, be sure to keep this source http://opens.blog.51cto.com/989185/1564058

Linux variable operation

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.