bash 从字符串中删除所有出现的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20783187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:05:58  来源:igfitidea点击:

Remove all occurrences of a word from a string

stringbashshell

提问by MOHAMED

I have the following string

我有以下字符串

str="toto1 toto2 toto3 toto4 toto2 toto5"

the toto2occurs twice in the string. How can I remove all toto2occurrences from the string?

toto2字符串中出现了两次。如何toto2从字符串中删除所有出现的内容?

I tried this:

我试过这个:

echo ${str/toto2/}

But this will remove only the first occurrence of toto2.

但这只会删除第一次出现的toto2.

toto1 toto3 toto4 toto2 toto5 # output

How can I remove all toto2occurrences from the string?

如何toto2从字符串中删除所有出现的内容?

回答by MOHAMED

Found it. the solution is:

找到了。解决办法是:

echo ${str//toto2/}