Common operation of variable quantum strings

Source: Internet
Author: User


${#variable} Returns the length of the $string

${variable:position} in $string, start extracting substrings from position $position

${variable:position:length} in $string, start extracting substrings of length $length from the position $position

${variable#substring} deletes the shortest match $substring substring starting at the beginning of the variable $string

${variable# #substring} deletes the longest matching $substring substring starting at the beginning of the variable $string

${variable%substring} deletes the shortest match $substring substring starting at the end of the variable $string

[[email protected] ~]$ echo ${cc%t*}

Value not set

${variable%%substring} deletes the longest matching $substring substring starting at the end of the variable $string

[[email protected] ~]$ echo ${cc%%t*}

Value not set

${variable/substring/replace} replaces the first matching $substring with replace

#!/bin/shfor i in $ (ls |grep "jpg$");d o i=${i/finished/};echo $idone

Results:

[Email protected] ~]# sh for2.sh stu_10299_1_.jpgstu_10299_2_.jpgstu_10299_3_.jpgstu_10299_4_.jpgstu_10299_5_.jpg

${variable/#substring/replace} if the $string prefix matches $substring, $replace is used instead of matching $substring

${variable/%substring/replace} if $string end matches $substring, use $replace instead of match $substring

#!/bin/shfor i in $ (ls |grep "jpg$");d Oi=${i/%jpg/mp4}done

Results:

[Email protected] ~]# sh for2.sh stu_10299_1_.mp4stu_10299_2_.mp4stu_10299_3_.mp4stu_10299_4_.mp4stu_10299_5_.mp4

V. Substitution of variables


${value:-word} If the variable value has an assignment, use the value of value, and if value is not assigned, use the following word to assign value

${value-word} If the variable value has an assignment, use the value of value, and if value is not assigned or empty, then Word is returned with Word and assigned value

${value:=word} If the variable value has an assignment, use the value of value, and if value is not assigned, use the following word to assign value

${value:+word} If value has an assignment, the value is re-assigned in Word and NULL is returned if value is not assigned.

${value:? " Not defined "} if value is not assigned, then the error defined later is returned, and if value is assigned, nothing is returned. Used to test if a variable has a value


This article from "Beginner's mind, always" blog, declined reprint!

Common operation of variable quantum strings

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.