这是我正在尝试的.我想要的是最后一个回声,说“一二三四测试1 …”,因为它循环.它不工作读取行是空的.这里有什么细微的东西,还是这样不行?
- 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 )
添加到数组.