array=( one two three ) echo ${array[@]} #one two three array=( ${array[@]} four ) echo ${array[@]} #one two three four while read line; do array=( ${array[@]} $line ) echo ${array[@]} done < <( echo <<EOM test1 test2 test3 test4 EOM )
array=( one two three ) echo ${array[@]} #one two three array=( ${array[@]} four ) echo ${array[@]} #one two three four while read line; do array=( ${array[@]} $line ) echo ${array[@]} done < <( echo <<EOM test1 test2 test3 test4 EOM )
while read line do array=( ${array[@]} $line ) echo ${array[@]} done <<EOM test1 test2 test3 test4 EOM
或者更有可能:
cat <<EOF | test1 test2 test3 test4 EOF while read line do array=( ${array[@]} $line ) echo ${array[@]} done
(请注意,具有管道的版本不一定适合于Bash,Bourne shell将在当前shell中运行while循环,但是Bash会在subshell中运行它,至少默认情况下,在Bourne shell中,在循环中可以在主shell中可用;在Bash中,它们不是,第一个版本总是设置数组变量,以便循环后可以使用.)
您也可以使用:
array+=( $line )
添加到数组.