原文:Bash Tutorial => Replace pattern in string
Replace pattern in string
Example#
First match:
$ a='I am a string'
$ echo "${a/a/A}"
I Am a string
All matches:
$ echo "${a//a/A}"
I Am A string
Match at the beginning:
$ echo "${a/#I/y}"
y am a string
Match at the end:
$ echo "${a/%g/N}"
I am a strinN
Replace a pattern with nothing:
$ echo "${a/g/}"
I am a strin
Add prefix to array items:
$ A=(hello world)
$ echo "${A[@]/#/R}"
Rhello Rworld
这篇博客介绍了Bash shell中对字符串进行模式替换的方法,包括替换第一个匹配项、所有匹配项、开头和结尾的匹配项,以及如何用空字符串替换模式。还展示了如何为数组元素添加前缀。
394

被折叠的 条评论
为什么被折叠?



