Modify Bash variable content in Linux

Source: Internet
Author: User

We know two ways to win a variable:

Echo $ HOME echo $ {HOME}

In the echo $ {variable} method, you can also modify the content of the variable. You only need to add some character signs and then use the comparison string to modify the content of the variable. Assume that the variable name is vbird and the content is/home/vbird/testing. x. sh.

◆ 1. Completely present the vbird variable:

Vbird = "/home/vbird/testing. x. sh"

Echo $ {vbird}

/Home/vbird/testing. x. sh

◆ 2. In the vbird variable, compare from the beginning

If it starts with/, all data between two/s is deleted, that is /*/

Echo $ {vbird ##/*/}

Testing. x. sh --------- deleted/home/vbird/testing/

Echo $ {vbird #/*/}

Vbird/testing. x. sh ---------- only/home/is deleted.

------ In these two examples, if the variable name is followed by two ##, it indicates that the string following ## gets the "longest" segment; if there is only one #, indicates the segment with the "smallest" value. Note: "The smallest segment" indicates the content between the characters closest to the first character from left to right and the same as the first character. The longest part is the opposite)

◆ 3. Answer the question. If it is from the end, delete?

Echo $ {vbird % /*/}

/Home/vbird/testing. x. sh ----------- are not deleted

Echo $ {vbird % /*}

-------- All deleted

Echo $ {vbird % /*}

/Home/vbird/testing -------- Delete only the/testing. x. sh part

In this example, we should pay special attention that % compares the meaning of "the last character", so the first one is of course incorrect, because the content of the vbird variable is h rather /. As for %/*, the "Longest/*" is deleted, and %/* is the shortest. Self-note: the shortest is the same as above. Although % indicates reverse comparison, note that the input in $ {variable} still follows the order from left to right, that is, the character sequence is the same as that in the variable content. For example:

Echo $ {vbird % t * h}

/Home/vbird/testing/tes ------ t. ing. x. sh is deleted

If yes

Echo $ {vbird % h * t}

/Home/vbird/testing. x. sh ---- the output is not changed because the comparison is compared with the first character. the first character of the variable content is h, and the first character given in the command is t, does not match the actual variable content .)

◆ 4. Replace testing in the vbird variable with TEST

Echo $ {vbird/testing/TEST}

/Home/vbird/TEST/testing. x. sh

Echo $ {vbird // testing/TEST}

/Home/vbird/TEST. x. sh

If the variable is followed by a slash (/), it indicates that the variable is followed by "replace" and only replaces "first". If it is //, all strings are replaced.

Related Articles]

  • BASH Shell Script Programming
  • Understanding BASH Shell
  • Introduction to bash shell programming

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.