我有一个参数$1,例如设置为字符串operations/software/tools-manifest,我想将其转换为字符串operations-software-tools-manifest,即用破折号(/)替换所有斜杠(-)。是否可以单独使用bash,而不调用例如sed(1)
我试过(不成功地):

[tim@passepartout ~]$ testparam=operations/software/tools-manifest
[tim@passepartout ~]$ echo "${testparam////-/}"
operations-/software-/tools-manifest
[tim@passepartout ~]$ echo "${testparam///-/}"
operations/software/tools-manifest
[tim@passepartout ~]$ echo "${testparam//\//-/}"
operations-/software-/tools-manifest
[tim@passepartout ~]$ echo "${testparam//\\//-/}"
operations/software/tools-manifest
[tim@passepartout ~]$ echo "${testparam//[/]/-/}"
operations/software/tools-manifest
[tim@passepartout ~]$ echo "${testparam//\x2f/-/}"
operations/software/tools-manifest
[tim@passepartout ~]$ echo "${testparam//\57/-/}"
operations/software/tools-manifest
[tim@passepartout ~]$

最佳答案

您可以使用"${testparam//\//-}"将所有正斜杠替换为-

echo "${testparam//\//-}"
operations-software-tools-manifest

关于bash - 将字符串中的“/”替换为“$ {parameter/pattern/string}”吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42032865/

10-13 05:06