变量设置方式 | 说 明 | 示例 | |
${变量#关键字} | 若变量内容从头开始的数据符合“关键字”,则将符合的最短数据删除 | A=abc123abc1234aa123bb123cc echo ${A#*abc} 结果:abc123abc1234aa123bb123cc |
|
##关键字”,则将符合的最长数据删除 | A=abc123abc1234aa123bb123cc echo ${A##*abc} 结果:abc123abc1234aa123bb123cc |
||
%关键字} | 若变量内容从尾向前的数据符合”,则将符合的最短数据删除 | A=abc123abc1234aa123bb123cc echo ${A%123*} 结果:abc123abc1234aa123bb123cc |
|
%%关键字”,则将符合的最长数据删除 | A=abc123abc1234aa123bb123cc echo ${A%%123*} 结果:abc123abc1234aa123bb123cc |
||
/旧字符串/新字符串} | 若变量内容符合“旧字符串”,则第一个旧字符串会被新字符串替换 | A=abc123abc1234aa123bb123cc echo ${A/abc/ABC} 结果:ABC//旧字符串”,则全部的旧字符串会被新字符串替换 |
A=abc123abc1234aa123bb123cc echo ${A//abc/ABC} 结果:123ABC1234aa123bb123cc |