${#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