需要在Bash中拆分一个字符串,用冒号分隔并赋值给变量

Need to split a string in Bash separated by a colon and assign to variables

我的字符串总是有 4 'words'

Strings:With:Four:Words and spaces

需要在Bash中拆分成4个变量。 所以..

var1="Strings"
var2="With"
var3="Four"
var4="Words and spaces"

我该怎么做?

read之前使用IFS=::

s='Strings:With:Four:Words'
IFS=: read -r var1 var2 var3 var4 <<< "$s"
echo "[$var1] [$var2] [$var3 [$var4]"
[Strings] [With] [Four [Words]