Usage of $ {} in shell

Source: Internet
Author: User

Usage of $ {} in shell 1. truncation function $ {file # */}: remove the first/and its left string: dir1/dir2/dir3/my.file.txt $ {file ##*/}: remove the last/and its left string: my.file.txt $ {file #*.}: remove the first one. and the string on the left: file.txt $ {file ##*.}: remove the last one. and the left string: txt $ {file %/*}: remove the last string/and the right string:/dir1/dir2/dir3 $ {file % /*}: remove the first/and its right string: (null) $ {file %. *}: remove the last one. and its right string:/dir1/dir2/dir3/my. file $ {file %. *}: remove the first one. and its right string:/dir1/dir2/dir3/my memory method: [list] # is to remove the left, # The last % is to remove the right, % first 2. string Extraction A single symbol is the minimum matching condition. The two symbols are the maximum matching. $ {File: 0: 5}: extract the leftmost 5 bytes:/dir1 $ {file: 5: 5}: extract 5 consecutive bytes to the right of 5th Bytes: /dir23. string replacement $ {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 4. assign values to different variable states (not set, null, non-null): $ {file-my.file.txt}: Use my.file.txt as the return value if the specified filepath is not set. (Null and non-null values are not processed) $ {file: -my.file.txt}: if the specified filepath is not set to a null value, use my.file.txt as the return value. (When non-empty value is used, ignore empty values file=my.file.txt}: If empty values are set to empty values or non-empty values, use my.file.txt as the return value. (Not set) $ {file: my.file.txt}: Use my.file.txt as the return value if the specified fileis a non-empty value. (When no value is set and no value is set, ignore variable file=my.file.txt}: If variable file.txt is not set, use my.file.txt as the return value, and assign $ file to my.file.txt. (Null and non-null values are not processed) $ {file: Empty my.file.txt}: if the specified filepath is not set to a null value, use my.file.txt to return the value, and assign $ file to my.file.txt. (Do not process non-null values) $ {file? My.file.txt}: if the specified filepath is not set, output my.file.txt to STDERR. (Null and non-null values are not processed) $ {file :? My.file.txt}: if the specified filepath is not set to an empty value, output my.file.txt to STDERR. (Do not handle non-null values.) Note: ": +" does not include null values. ":-", ": =" and so on, as long as there is a number, it contains a null value (null ). 5. variable Length $ {# file} 6. array Operation A = (a B c def) $ {A [@]} or $ {A [*]} can get a B c def (number of all groups) $ {A [0]} returns a (number of the first group), and $ {A [1]} returns the number of the second group... you can get 4 for $ {# A [@]} or $ {# A [*]} (number of all groups) $ {# A [0]} can get 1 (that is, the length of the first number of groups (), $ {# A [3]} can get 3 (length of the fourth number (def)

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.