如何在由 / 分隔的 bash 中拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9479814/
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 01:40:32  来源:igfitidea点击:

How to split a string in bash delimited by /

bash

提问by pedr0

I have to split a string in a bash script by "/" but I tried:

我必须在 bash 脚本中用“/”分割一个字符串,但我试过:

for i in $(echo $www | tr "/")

But it did not work, any suggestion ?

但它没有用,有什么建议吗?

Thanks a lot.

非常感谢。

回答by Ignacio Vazquez-Abrams

$ IFS=/ read -a foo <<< 'abc def/ghi/jkl'
$ echo "${foo[0]}*${foo[1]}*${foo[2]}"
abc def*ghi*jkl

回答by user unknown

Your trick would work, if you would tell tr, what slash should be translated into:

你的伎俩会奏效,如果你告诉tr,应该将什么斜线翻译成:

for w in $(echo "what/the/heck" | tr "/" " ") ; do echo $w; done 
what
the
heck