${in Shell}, # #和%% use example _linux shell

Source: Internet
Author: User

Today read a script file when there are some places do not understand, find an article to read some, feel good, keep down.

Let's say we've defined a variable as:

Copy Code code as follows:

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

You can replace each of the different values with ${}:
Copy Code code as follows:

${file#*/}: Delete the first/and left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Delete the last/and left string: my.file.txt
${file#*.}  : Delete the first one. And the string to the left: file.txt
${file##*.}  : Delete the last one. And the string to the left: txt
${file%/*}: Delete the last/And right string:/dir1/dir2/dir3
${file%%/*}: Delete the first/and the right string: (null value)
${file%.*}: Delete the last one. And the string to the right:/dir1/dir2/dir3/my.file
${file%%.*}: Delete the first one. And the string to the right:/dir1/dir2/dir3/my

The method of memory is:
Copy Code code as follows:

# is to remove the left (on the keyboard # on the left of the $)
% is to remove the right (on the keyboard% on the right side of the $)
A single symbol is a minimum match; two symbols is the maximum match
${file:0:5}: Extract leftmost 5 bytes:/dir1
${file:5:5}: Fetches 5 consecutive bytes to the right of the 5th byte:/dir2

You can also replace the string in the value of the variable:
Copy Code code as follows:

${file/dir/path}: Replaces the first dir with the Path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Replace all dir with Path:/path1/path2/path3/my.file.txt

${} can also be assigned to different variable state (no set, null value, Non-null value):

${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 null, use My.file.txt as the value of the return. (non-null values are not processed)
${file+my.file.txt}: If $file is set to null or Non-null values, use My.file.txt as the return value. (not to be processed when not set)
${file:+my.file.txt}: If the $file is a non-null value, the my.file.txt is used as the return value. (not set and null value when not processed)
${file=my.file.txt}: If the $file is not set, then use My.file.txt as the return value, at the same time will $file the value of My.file.txt. (null and Non-null values are not processed)
${file:=my.file.txt}: If $file is not set or NULL, the my.file.txt is used as the return value, and the $file value is my.file.txt. (non-null values are not processed)
${file?my.file.txt}: If $file is not set, the my.file.txt is output to STDERR. (null and Non-null values are not processed)

${file:?my.file.txt}: If $file is not set or NULL, the my.file.txt is exported to STDERR. (non-null values are not processed)
${#var} to calculate the length of the variable value:

${#file} can get 27 because/dir1/dir2/dir3/my.file.txt is 27 bytes
[/code]

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.