将命令输出行存储到基于新行字符的数组中

我有一个variables,如下所示,我执行某些操作来逐一打印输出,如下所述。

a="My name is A. Her Name is B. His Name is C" echo "$a" | awk -F '[nN]ame |\\.' '{for (i=2; i<=NF; i+=2) print $i}' 

输出是

 is A is B is C 

当我将结果存储到数组中时,它将空间视为数组分隔符并存储值。 但我想要将输出的每一行存储到每个数组索引值如下

 x=($(awk -F '[nN]ame |\\.' '{for (i=2; i<=NF; i+=2) print $i}' <<< "$a")) 

出放,

 ${x[0]} = is ${x[1]} = A ..and so on... 

我期望的是

 ${x[0]} = is A ${x[1]} = is B ${x[2]} = is C 

echo ${#x[@]} = 6 ; 应该是= 3

 OK try below: i=0 while read v; do x[i]="$v" (( i++ )) done < <(awk -F '[nN]ame |\\.' '{for (i=2; i<=NF; i+=2) print $i}' <<< "$a") 

您也可以使用mapfile命令(bash版本4或更高版本):

 tempX=$(awk -F '[nN]ame |\\.' '{for (i=2; i<=NF; i+=2) print $i}' <<< "$a") mapfile -tx <<< "$tempX" ~$ echo "${x[0]}" is A