Common $ symbol methods in Linux

Source: Internet
Author: User
Transferred from: http://linux.vbird.org/## I 've just mentioned the latest usage, and the Shard is:
[root@linux ~]# echo $HOME[root@linux ~]# echo ${HOME}

So, in the $ {Variable} usage method, we can also perform some repair work! You only need to add some character labels and then use the comparison strings to modify the content of the variable! Let's take the following example to illustrate: In the following example, I assume that my data name is vbird and the content is/home/vbird/testing. x. sh.

1. Completely show the content of vbird. [root @ Linux ~] # Vbird = "/home/vbird/testing. X. Sh" [root @ Linux ~] # Echo $ {vbird}/home/vbird/testing. x. sh2. in vbird data changes, the ratio starts from the beginning. If the start is/, all the resources between the two are divided, that is,/*/[root @ Linux ~] # Echo $ {vbird ###/*/} testing. X. Sh <= except/home/vbird/testing/[root @ Linux ~] # Echo $ {vbird #/*/} vbird/testing. X. Sh <= except/home/# interesting examples ~ If there are two ##s after the data name change, it indicates that the string following ## takes the "longest" segment; if there is one #, it indicates taking the "smallest segment! 3. What should I do if I start from the back and divide? [Root @ Linux ~] # Echo $ {vbird %/*/}/home/vbird/testing. X. Sh <= none of them have been deleted [root @ Linux ~] # Echo $ {vbird %/*} <= deleted! [Root @ Linux ~] # Echo $ {vbird %/*}/home/vbird/testing <= only remove/testing. x. sh part # in this example, note that the % ratio is the meaning of the "last character". # therefore, the first method is not correct ~ This is because the last content of vbird is H rather! # %/* Indicates the longest/* In addition to %/*. Of course, it is all! %/* Is the shortest! 4. Replace testing in vbird data with test [root @ Linux ~] # Echo $ {vbird/testing/test}/home/vbird/test/testing. X. Sh [root @ Linux ~] # Echo $ {vbird // testing/test}/home/vbird/test. x. sh # if the result is at a time, it indicates that the result is a "replace" operation ~ In addition, tokens Replace "first" #. However, if yes, all strings are replaced!

Please pay attention to it ~ In any case, it can be followed by #, #, %, %,/,/, and their meanings are not the same ~

In addition, you can determine the number of different changes! For example, I need to use two variations: VaR and Str, then I want to determine whether the STR variable content is set to a string, that is, "expr" to determine the VaR content. So what method can I use for further determination? If you want to write shell scripts, you can simply use shell scripts. If you don't want to write them, we can judge them through simple changes!

TIPS:
In the following example, the VaR and STR are variable values. We want to determine whether the value of VaR has been set for Str! Generally, 'str' indicates the time when 'str' is not set or is null. 'As to 'str, there is no such variable 』.

Change Data settings STR is not set STR is a Null String STR has been set to a non-null string
VaR =$ {str-expr} Var = expr Var = Var = $ Str
VaR =$ {STR:-expr} Var = expr Var = expr Var = $ Str
VaR =$ {STR + expr} Var = Var = expr Var = expr
VaR =$ {STR: + expr} Var = Var = Var = expr
VaR =$ {STR = expr} STR = expr
Var = expr
STR unchanged
Var =
STR unchanged
Var = $ Str
VaR =$ {STR: = expr} STR = expr
Var = expr
STR = expr
Var = expr
STR unchanged
Var = $ Str
VaR =$ {Str? Expr} Expr returns to stderr Var = Var = Str
VaR =$ {STR :? Expr} Expr returns to stderr Expr returns to stderr Var = Str

In the above table as the root user, let's introduce the example of another example! Pai_^

Example 1: If the STR variable content exists, VaR is set to str. Otherwise, VaR is set to "newvar" [root @ Linux ~]. # Unset STR; var =$ {str-newvar} [root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str" Var = newvar, STR = <== because STR does not exist, VaR is newvar [root @ Linux ~] # STR = "oldvar"; var =$ {str-newvar} [root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str" Var = oldvar, STR = oldvar <= Because STR exists, so VaR is equal to STR's content. Example 2: If STR does not exist, VaR and STR are both set to newvar. Otherwise, VaR and STR are the same [root @ Linux ~]. # Unset STR; var =$ {STR = newvar} [root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str" Var = newvar, STR = newvar <= Because STR does not exist, so var/STR are all newvar [root @ Linux ~] # STR = "oldvar"; var =$ {STR = newvar} [root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str" Var = oldvar, STR = oldvar <= Because STR exists, therefore, VaR is equal to STR content. Example 3: If STR changes exist, VaR is equal to str. Otherwise, "novar" [root @ Linux ~] is output. # Unset STR; var =$ {Str? Novar}-Bash: Str: novar <= Because STR does not exist, the system returns the Response Message [root @ Linux ~]. # STR = "oldvar"; var =$ {Str? Novar} [root @ Linux ~] # Echo Var = "$ Var", STR = "$ Str" Var = oldvar, STR = oldvar <= Because STR exists, so VaR is equal to STR's content # none of the above three cases mentioned that when STR has been set and it is null! # You can renew it on your own!

Suddenly, it seems that there is nothing strange about the data, but if you look at it, hey! A bunch of historical changes and changes in the system's data source, but it may affect our ability to leverage their own business in bash! For example, path or ulimit ~ So, you still have to understand this thing! Pai_^

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.