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

标签 string bash shell

我有以下字符串

str="toto1 toto2 toto3 toto4 toto2 toto5"

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

我试过这个:

echo ${str/toto2/}

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

toto1 toto3 toto4 toto2 toto5 # output

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

最佳答案

Found it.解决方案是:

echo "${str//toto2/}"

关于string - 从字符串中删除所有出现的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20783187/

相关文章:

mysql - 如何为 MySQL 制作 Bash 插入脚本

shell - 如何像在 bash 中那样动态地重新加载 Fish 配置文件?

linux - Bash 将信息从脚本传递到脚本

python - 如何在numpy数组中拆分字符串?

linux - Bash - 在与当前终端分开的另一个终端中启动命令的新实例

ruby - 获取名字并删除字符串中的其余部分

bash - Crontab 不执行 bash 脚本

bash - 检查 Bash 中是否存在命令(包括 super 用户)

Java将看起来像Array的String转换成String Array或String ArrayList

c++ - 如何在二进制文件中查找 ANSI 字符串?