Some of the supernatural powers of ${} in the shell

Source: Internet
Author: User

Suppose we define a variable as: file=/dir1/dir2/dir3/My.file.txt We can replace each other with ${} to get different values: ${file#*/}: Take out the first/its left string: Dir1/dir2/dir3/my.file.txt${file##*/}: Take out the last/and left string: My.file.txt${file#*.} : Take out the first one. And the string to the left: file.txt${file##*.} : Take out the last one. And the string to the left: TXT${file%/*}: Take off the last bar/its right string:/dir1/dir2/Dir3${file%%/*}: Take out the first one/and the string to the right: (null value) ${file%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/My.file${file%%.*}: Take out the first one. And the string to the right:/dir1/dir2/dir3/the methods of my memory are:#is to remove the left side (on the plate # on the left of the $)% is removed to the right (on the plate%on the right of $) a single symbol is the minimum match; two symbols are the maximum match. ${file:0:5}: Extract the leftmost 5 bytes:/Dir1${file:5:5}: Extract the 5th byte to the right of the 5 consecutive bytes:/Dir2 We can also replace the string in the value of the variable: ${file/dir/path}: Change the first dir to path:/path1/dir2/dir3/My.file.txt${fileDir/path}: Change all dir to path:/path1/path2/path3/My.file.txt can also be assigned to different variable states (no setting, null value, non-null value) using ${}: ${file-My.file.txt}: If the $file is a null value, use My.file.txt as the default value. (Reservation not set and non-null value) ${file:-My.file.txt}: If the $file is not set or null, use My.file.txt as the default value. (leave non-null values) ${file+My.file.txt}: Use My.file.txt as the default value regardless of the $file value. (do not retain any values) ${file:+My.file.txt}: Use My.file.txt as the default value unless the $file is a null value. (leave null value) ${file=My.file.txt}: If $file not set, use My.file.txt as the default and define $file as a non-null value. (leave null and non-null values) ${file:=My.file.txt}: If $file is not set or null, use My.file.txt as the default and define the $file as a non-null value. (Reserved non-null value) ${file?my.file.txt}: If $file is not set, the My.file.txt output to STDERR. (leave null and non-null values)) ${file:?my.file.txt}: If the $file is not set or null, the my.file.txt output to STDERR. (leave non-null values) and, ${.#var} to calculate the length of the variable value:${#file} can get 27 because/dir1/dir2/dir3/my.file.txt is just 27 bytes ...

Some of the supernatural powers 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.