The magical application of ${} in the shell

Source: Internet
Author: User

1. Truncation function

 file=dir1/dir2/dir3/my. file.txt   
${file #*/}: Remove the first/its left string: dir2/dir3/my. file .txt${ file ##*/}: Take out the last/and left string: my.< Span style= "color: #0000ff;" >file .txt${ file #*.}: Take out the first. and its left string: file .txt${ file ##* file %/* }: Take off the last bar/its right string:/dir1/dir2/dir3$ {file%%/*}: Remove the first/its right string: (null) ${file%.*}: Take off the last one. and its right string:/dir1/dir2/dir3/my.file${file%%.*}: Take off the first. and the string to the right:/ Dir1/dir2/dir3/my

The methods of memory are:

#是去掉左边 # #最后一个% is the first one to remove the right

2. String extraction

The single symbol is the minimum match, and the two symbol is the maximum match.
File=dir1/dir2/dir3/my. file.txt${file:0:55 bytes:/dir1${file :5:555 bytes:/dir2

3. String substitution

${file/dirdir for 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 if $file is set to a null or non-null value.file. txt as the return 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. (not processed when not set and null value) ${file=my.file. txt}: If $file is not set, use My.file. txt as the return value while the $fileAssign to 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 while the $fileAssign to My.file. txt. (Non-null value is not processed) ${file? My.file. txt}: If $file is not set, it will be 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, then my.file. txt output to stderr. (Non-null value is not processed)

Attention:

" :+ " is not included in the case of a null value. " :- " " := " as long as there is a number, it contains a null value (null).

5. Length of the variable

${#File}

6. Array operations

A=(a b C def) ${a[@]} or ${a[*]} can get a B C def (all groups) ${a[0]} can get a (first group number), ${a[1]} Number of second groups ... ${#A [@]} or ${#A [4  (number of total groups) ${#A [01 (that is, the length of the first group (a), ${#A [3 3 (length of fourth Group (DEF))

The magical application 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.