Split string with zsh as in Python

社会主义新天地 提交于 2019-12-02 20:21:06
Olivier Verdier

The solution is to use the @ modifier, as indicated in the zsh docs:

string="1::3"
a=("${(@s/:/)string}") # @ modifier

By the way, if one has the choice of the delimiter, it's much easier and less error prone to use a newline as a delimiter. The right way to split the lines with zsh is then:

a=("${(f)string}")

I don't know whether or not the quotes are necessary here as well...

This will work in both zsh (with setopt shwordsplit or zsh -y) and Bash (zero-based arrays):

s="1::3"
saveIFS="$IFS"
IFS=':'
a=(${s})
IFS="$saveIFS"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!