变量替换总结:

1 - 4:变量删除,5 - 6:变量替换

variable_1="I love you, do you love me?"

1.${变量#匹配规则} -- 从头开始匹配,最短删除

IN:echo ${variable_1#*ov}
OUT:e you, do you love me?

2.${变量##匹配规则} -- 从头开始匹配,最长删除

IN:echo ${variable_1##*ov}
OUT:e me?

3.${变量%匹配规则} -- 从尾开始匹配,最短删除

IN:echo ${variable_1%ov*}
OUT:I love you, do you l

4.${变量%%匹配规则} -- 从尾开始匹配,最长删除

IN:echo ${variable_1%%ov*}
OUT:I l

5.${变量/旧字符串/新字符串} -- 替换变量内的旧字符串为新字符串,只替换第一个

IN:echo ${variable_1/love/want}
OUT:I want you, do you love me?

6.${变量//旧字符串/新字符串} -- 替换变量内的旧字符串为新字符串,全部替换

IN:echo ${variable_1//love/want}
OUT:I want you, do you want me?

标签: 变量, shell, 删除, 替换