1.Calculate the length of the variable
[root@dong ~]# CHAR="I am dong"
[root@dong ~]# echo $CHAR
I am dong
[root@dong ~]# echo ${#CHAR}
9
[root@dong ~]# echo $CHAR|wc -L
9
2.Number of character from the second to the end(including spaces)
[root@dong ~]# echo ${CHAR:2}
am dong
3.Number of character from the second character to the N character(including spaces)
[root@dong ~]# echo ${CHAR:2:2}
am
[root@dong ~]# echo ${CHAR:2:4}
am d
[root@dong ~]# echo $CHAR|cut -c 3-9
am dong
4.匹配变量内容替换
[root@dong ~]# CHAR="I am dong dong I am dong"
[root@dong ~]# echo ${CHAR/dong/ling}
I am ling dong I am dong
Suffix Matching
[root@dong ~]# echo ${CHAR/%dong/ling}
I am dong dong I am ling
[root@dong ~]# echo $CHAR
abcABC123ABCabc
[root@dong ~]# echo ${CHAR#a*C}
123ABCabc
[root@dong ~]# echo ${CHAR##a*C}
abc
[root@dong ~]# echo ${CHAR%%a*c}
[root@dong ~]# echo ${CHAR%a*c}
abcABC123ABC
Because variable subtrings are rarely used,BAIDU can be used if necessary
Use Skills
rename file
echo $file
adfa.jpg
mv $file ${file%jpg/png}
转载于:https://blog.51cto.com/6207422/2411175