Usage of ${} in Shell

Source: Internet
Author: User

The following content is basically extracted from the Shell13 question.
1. Truncation function
${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. and its left string: file.txt
${file##*.}: Take out the last one. and its left string: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take out the last one. and the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out the first. and its right string:/dir1/dir2/dir3/my
The methods of memory are:
[List] #是去掉左边, # #最后一个
% is removed right, percent of the first
2. String Extraction
The single symbol is the minimum match, and the two symbol is 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
3. String substitution
${file/dir/path}: Change the first dir to Path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Change all dir to Path:/path1/path2/path3/my.file.txt
4. Assign values to different variable states (no settings, null values, non-null values):
${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 return value. (Non-null value is not processed)
${file+my.file.txt}: Use My.file.txt as the return value if $file is set to a null or non-null value. (not processed when not set)
${file:+my.file.txt}: If $file is a non-null value, use My.file.txt as the return value. (No processing when not set and null value)
${file=my.file.txt}: If $file is not set, My.file.txt is used as the return value, and $file is assigned my.file.txt. (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 return value and assign the $file to My.file.txt. (Non-null value is not processed)
${file?my.file.txt}: If $file is not set, the My.file.txt 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 output to stderr. (Non-null value is not processed)
Attention:
The ": +" case does not contain a null value.
":-", ": =" as long as there is a number is a null value (NULL).
5. Length of the variable
${#file}
6. Array Operations
A= (a b c def)
${a[@]} or ${a[*]} to get A b C def (total number of groups)
${a[0]} can get a (number of first group), ${a[1]} is the second group number ...
${#A [@]} or ${#A [*]} to get 4 (total number of groups)
${#A [0]} can get 1 (that is, the length of the first group (a), ${#A [3]} can get 3 (fourth group number (def) length)

Usage of ${} in 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.