Use of $ {},#and % in Shell

Source: Internet
Author: User

Suppose we define a variable: file =/dir1/dir2/dir3/my.file.txt

You can replace $ {} with different values: $ {file # */}: Delete the first/and its left string: dir1/dir2/dir3/my.file.txt $ {file # */}: Delete the last/and its left string: my.file.txt $ {file #*.}: Delete the first one. and the string on the left: file.txt $ {file ##*.}: Delete the last one. and the left string: TXT $ {file %/*}: Delete the last/and the right string:/dir1/dir2/dir3 $ {file % /*}: delete the first/and its right string: (null) $ {file %. *}: Delete the last one. and its right string:/dir1/dir2/dir3/My. file $ {file %. *}: Delete the first one. and its right string:/dir1/dir2/dir3/My
The memory method is as follows: # Remove the left side (on the keyboard # on the left side of $) % remove the right side (on the keyboard % on the right side of $). A single symbol is the minimum match; the two symbols are the maximum match $ {file: 0: 5}: extract the leftmost five Bytes:/dir1 $ {file: 5: 5 }: extract 5 consecutive bytes to the right of 5th Bytes:/dir2

You can also replace the string in the variable value: $ {file/DIR/path}: Replace the first dir with Path: /path1/dir2/dir3/my.file.txt $ {file/DIR/path}: replace all dir with Path:/path1/path2/path3/my.file.txt
You can use $ {} to assign values to different variable states (not set, null, or non-null): Invalid variable file-my.file.txt}: If $ file is not set, use my.file.txt as the return value. (Null and non-null values are not processed) $ {file: -my.file.txt}: If $ file is not set or is null, use my.file.txt as the callback value. (When the value is not null, It is not processed.) When file1_my.file.txt}: If $ file is set to a null value or a non-null value, my.file.txt is used as the return value. (Not processed when not set) $ {file: Your my.file.txt}: If $ file is not a null value, use my.file.txt as the callback value. (When no value is set and null values are not processed) when using file=my.file.txt}: If $ file is not set, use my.file.txt as the response value. At the same time, set $ file to my.file.txt. (Null and non-null values are not processed) $ {file: Your my.file.txt}: If $ file is not set or is null, use my.file.txt as the callback value, at the same time, the value of $ file encoding is my.file.txt. (Not processed when the value is not null) $ {file? My.file.txt}: If $ file is not set, export my.file.txt to stderr. (Null and non-null values are not processed)

$ {File :? My.file.txt}: If $ file is not set or is null, output my.file.txt to stderr. (Not processed when the value is not null)

The length of the variable value can be calculated from $ {# var:

$ {# File} can get 27 because/dir1/dir2/dir3/my.file.txt is 27 bytes.
From: http://space.baidu.com.cn/ugo5/blog/item/c550bbc54d1644079c163dbd.html

Use of $ {},#and % in Shell

Related Article

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.