我如何在每行空白处分割带有文本内容的变量?也许是数组或类似的东西……
示例:
line1
line2
line3
line4
line5
line6
line7
line8
line9
也许会进入:
array[0]: line1
line2
line3
array[1]: line4
line5
...
谢谢!
答案 0 :(得分:-1)
用数据中未包含的内容代替\n\n
,将IFS设置为它,填充数组。
#! /bin/bash
in='line1
line2
line3
line4
line5
line6
line7
line8
line9'
separator=$'\xff' # or whatever char not appearing in the data
${IFS+"false"} && unset oldifs || oldifs="$IFS"
IFS=$separator
arr=(${in//$'\n\n'/$separator})
${oldifs+"false"} && unset IFS || IFS="$oldifs"
printf '<%s>\n' "${arr[@]}"