Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想将冒号分隔的字符串拆分为一个数组。每个循环我ONE:TWO:THREE现在都有字符串,我怎样才能将它拆分成一个数组,这样我就可以像访问它一样访问它string[1] //ONE, string[2] //TWO, string[3] //THREE?
ONE:TWO:THREE
string[1] //ONE, string[2] //TWO, string[3] //THREE
这是我对数组内容使用的循环:
WORDS=(ONE:TWO:THREE FIVE:FOUR:THREE) for i in ${WORDS[@]} ; do [..] done
以下应该做到这一点:
IFS=':' read -a arr <<< "$i"
在这之后你会看到:
echo ${#arr} # <-- 3
所以你的代码看起来像:
for word in "${WORDS[@]}"; do IFS=':' read -a arr <<< "$word" for part in "${arr[@]}"; do # do something with the word done done