$ {} Usage

Source: Internet
Author: User

We define a variable:
File =/dir1/dir2/dir3/my.file.txt
We can use $ {} to calculate different values for the token:
$ {File # */}: remove the first/and its left outer strings: dir1/dir2/dir3/my.file.txt
$ {File ### */}: remove the last/and its left string: my.file.txt
$ {File # *.}: remove the first. And its left-side string: file.txt
$ {File ### *.}: remove the last. And its left string: txt
$ {File %/*}: remove the last line/and its right string:/dir1/dir2/dir3
$ {File %/*}: remove the first/and its right-hand string: (null)
$ {File %. *}: remove the last. And its right-hand string:/dir1/dir2/dir3/My. File
$ {File %. *}: remove the first. And its right-hand string:/dir1/dir2/dir3/My
Remember the method as follows:
# Remove the left sequence (on the sequence # On the left sequence of $)
% Is to remove the right cursor (% on the right margin of $)
Single-character matching is the minimum matching. The two single-character matching is the maximum matching.

$ {File: 0: 5}: extract the leftmost five words? :/Dir1
$ {File: 5: 5}: extract 5th words? Five characters in the upper right corner? :/Dir2

We can also replace the strings in the variable value:
$ {File/DIR/path}: extract the first dir to path:/path1/dir2/dir3/my.file.txt.
$ {File // DIR/path}: Extract all dir files as path:/path1/path2/path3/my.file.txt

You can use $ {} to modify variable values for different variables (unspecified, null, or non-null ):
Optional values file-my.file.txt}: If $ file is null, use my.file.txt as the default value. (Reserved unspecified and non-null values)
$ {File: -my.file.txt}: If $ file is not set or is null, use my.file.txt as the default value. (Keep non-null values)
When using file1_my.file.txt}: regardless of the $ file value, my.file.txt is used as the default value. (No value is retained)
$ {File: Invalid my.file.txt}: Unless $ file is null, my.file.txt is used as the default value. (Retain null values)
Optional values file1_my.file.txt}: If $ file is not set, use my.file.txt as the default value. At the same time, set $ file to a non-null value. (Retain null and non-null values)
$ {File: Your my.file.txt}: If $ file is not set or is null, use my.file.txt as the default value. At the same time, set $ file to a non-null value. (Keep non-null values)
$ {File? My.file.txt}: If $ file is not set, export my.file.txt to stderr. (Retain null and non-null values ))
$ {File :? My.file.txt}: If $ file is not set or is null, my.file.txt is exported to stderr. (Keep non-null values)

You can also calculate the length of the variable value by using $ {# var:
$ {# File} can get 27, because/dir1/dir2/dir3/my.file.txt contains 27 Characters ?...

Next? Zookeeper refers to the Array Processing Method of bash.
Generally, a = "a B C Def" only converts $ A into a single string,
But to a = (a B C DEF), it is to define $ A as a group number...
The following method can be used to test Bash's Data Group replacement method:
$ {A [@]} or $ {

  • } Obtain a B c def (all groups)
    $ {A [0]} can get a (first group number). $ {A [1]} is the second group number...
    $ {# A [@]} or $ {#
  • } Expected 4 (all groups)
    $ {# A [0]} can get 1 (that is, the length of the first group (), $ {A [3]} returns 3 (the length of the first group (DEF)
    A [3] = xyz is to redefine 4th groups as XYZ...

    Why? ....

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.