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 use${ }Replace each to get different values:
${file#*/}: Take out the first one./And the string to its left:Dir1/dir2/dir3/my.file.txt
${file##*/}: Take the last one off./And the string to its left:My.file.txt
${file#*.}: Take out the first one.And the string to its left:File.txt
${file##*.}: Take out the last one.And the string to its left:Txt
${file%/*}: Take out the last bar/And the string to the right:/dir1/dir2/dir3
${file%%/*}: Take out the first one./And the string to the right:(Null value)
${file%.*}: Take out the last one. /dir1/dir2/dir3/my.file 
${file%%.*} : Take out the first  /dir1/dir2/dir3/my 
< Span style= "LINE-HEIGHT:31PX;" > Memory method:

#is to remove the left(On the Kam disk #  in  $  to the left of Span style= "line-height:31px;" lang= "en-us" >)  
%  is to remove the right  %< Span class= "Apple-converted-space" >  in  $  right )  

 ${file:0:5}: Extracts the leftmost 5 bytes:/dir1
${file:5:5}: Extracts the 5 bytes to the right of the first 5 bytes :/dir2
We can also replace the string in the value of the variable:
${file/dir/path}: the firstDirExchange forPath/path1/dir2/dir3/my.file.txt
${file//dir/path}: Will AllDirExchange forPath/path1/path2/path3/my.file.txt

Use${ }You can also assign values for different variable states(no set, null value, non-null value):
${file-my.file.txt}: If$fileis a null value, use theMy.file.txt as the default value.(Reservation not set and non-null value)
${file:-my.file.txt} : If$fileIf there is no setting or null value, use theMy.file.txtAs the default value.(leave non-null values)
${file+my.file.txt}: No matter$fileWhy value, all useMy.file.txt as the default value.(No values are preserved)
${file:+my.file.txt}: Unless$fileis a null value, otherwise use theMy.file.txtAs the default value.(Leave empty values)
${file=my.file.txt}: If$fileNot set, then useMy.file.txtAs the default value, and the$fileDefined as a non-null value.(leave null and non-null values)
${file:=my.file.txt}: If$fileis not set or null, use theMy.file.txtAs the default value, and the$fileDefined as a non-null value.(leave non-null values)
${file?my.file.txt}: If$fileis not set, it will beMy.file.txtOutput toSTDERR.(leave null and non-null values))
${file:?my.file.txt}: If$fileIf no value is set or NULL, theMy.file.txtOutput toSTDERR.(leave non-null values) 

Also, ${#var}  to calculate the length of the variable value:  
${#file}  can get  , because  /dir1/dir2/dir3/my.file.txt  just 27   bytes

Some psychic 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.