Special usage of shell variables

Source: Internet
Author: User
Suppose we have defined a variable: file =/dir1/dir2/dir3/my.file.txt, which can be replaced with $ {} to get 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

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. and its left string: file.txt
$ {File ### *.}: delete the last. and its left string: txt
$ {File %/*}: delete the last/and its right string:/dir1/dir2/dir3
$ {File %/*}: deletes the first/and its right string: (null)
$ {File %. *}: delete the last. and its right string:/dir1/dir2/dir3/my. file
$ {File %. *}: delete the first. and its right string:/dir1/dir2/dir3/my

 

The memory is as follows:
# Remove the left side (on the keyboard # on the left side of $)
% Is to remove the right side (% on the keyboard is on the right side of $)
A single symbol is the minimum match; two symbols are the maximum match.
$ {File: 0: 5}: extract the leftmost five bytes:/dir1
$ {File: 5: 5}: extract 5 consecutive bytes from 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 (?] Set, Null, non-null ):
When using file-my.file.txt}: suppose $ file?] There are settings ,? T use my.file.txt as the return value. (Null and non-null? R is not processed)
$ {File: -my.file.txt}: suppose $ file?] Yes? Set or? Shield cover? T use my.file.txt? Idle idle (non-null? R is not processed)
When using file=my.file.txt}: suppose $ file ?? What is the upper cover ?? Using my.file.txt? Why?]? OK? R is not processed)
$ {File: export my.file.txt}: if $ file? Beam cover? T use my.file.txt? Why? Set and null? R is not processed)
When using file=my.file.txt}: if $ file?]? Yes ,? T use my.file.txt? What is the workload ?? R? $ File? Value? My.file.txt. (Null and non-null? R is not processed)
$ {File: export my.file.txt}: if $ file?]? Set or? Shield cover? T use my.file.txt? What is the workload ?? R? $ File? Value ?? Y.file.txt. (Non-null? R is not processed)
$ {File? My.file.txt}: if $ file?]? Yes ,? T? My.file.txt? Output to STDERR. (Null and non-null? R is not processed)
 

$ {File :? My.file.txt}: if $ file is not set or is null, output my.file.txt to STDERR. (Non-null? R is not processed)

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.

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.