Examples of ${}, #, # #,%, percent of% in the shell

Source: Internet
Author: User

Description

I believe that in the work, many friends in the use of shell scripts encountered ${},%, #等这些字符的使用, and occasionally they will be dizzy, and then I use the following example to let everyone very well remember the Shell ${}, # #和 percent use, this article gives the results obtained in different cases.


Example

Assuming that a variable is defined, "code as follows":

File=/dir1/dir2/dir3/my.file.txt

Different values can be replaced with ${}, respectively:

${file#*/}: Delete the first/its left string: dir1/dir2/dir3/my.file.txt

${file##*/}: Remove the last/and left string: my.file.txt

${file#*.}  : Remove the first one. And the string to the left: file.txt

${file##*.}  : Remove the last one. And the string to the left: txt

${file%/*}: Remove the last/And right string:/dir1/dir2/dir3

${file%%/*}: Delete the first/its right string: (null value)

${file%.*}: Remove the last one. And the string to the right:/dir1/dir2/dir3/my.file

${file%%.*}: Remove the first one. And the string to the right:/dir1/dir2/dir3/my


"The method of Memory is":

# is to remove the left side (on the keyboard # on the left side)

% is removed to the right (on the keyboard% on the right side)

The single symbol is the minimum match; two symbols are the maximum match

${file:0:5}: Extract the leftmost 5 bytes:/dir1

${file:5:5}: Extracts the 5th byte to the right of 5 consecutive bytes:/DIR2

You can also replace the string in the value of the variable:

${file/dir/path}: Replace the first dir with a path:/path1/dir2/dir3/my.file.txt

${file//dir/path}: Replace all dir with Path:/path1/path2/path3/my.file.txt


Look at the above example, you remember, like this article friends can be forwarded and collection, the question of friends in the comments below comment together!!!



Examples of ${}, #, # #,%, percent of% in the 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.