IN="[email protected];[email protected]" mapfile -td \; fields <<<"$IN" fields[-1]=${fields[-1]%$'\n'}# drop '\n' added on last field, by '<<<' for x in"${fields[@]}"; do echo"> [$x]" done
方法六:适用于多种shell的通用方法
使用字符串替换语法提取子字符串。
示例代码:
1 2 3 4 5 6 7 8 9
IN="[email protected];[email protected];Full Name <[email protected]>" while [ "$IN" != "$iter" ]; do # extract the substring from start of string up to delimiter. iter=${IN%%;*} # delete this first "element" AND his separator, from $IN. IN="${IN#$iter;}" # Print (or doing anything with) the first "element". printf'> [%s]\n'"$iter" done