我可以从bash中的heredoc读取线吗?

前端之家收集整理的这篇文章主要介绍了我可以从bash中的heredoc读取线吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我正在尝试的.我想要的是最后一个回声,说“一二三四测试1 …”,因为它循环.它不工作读取行是空的.这里有什么细微的东西,还是这样不行?
  1. array=( one two three )
  2. echo ${array[@]}
  3. #one two three
  4. array=( ${array[@]} four )
  5. echo ${array[@]}
  6. #one two three four
  7.  
  8.  
  9. while read line; do
  10. array=( ${array[@]} $line )
  11. echo ${array[@]}
  12. done < <( echo <<EOM
  13. test1
  14. test2
  15. test3
  16. test4
  17. EOM
  18. )
我通常会写:
  1. while read line
  2. do
  3. array=( ${array[@]} $line )
  4. echo ${array[@]}
  5. done <<EOM
  6. test1
  7. test2
  8. test3
  9. test4
  10. EOM

或者更有可能:

  1. cat <<EOF |
  2. test1
  3. test2
  4. test3
  5. test4
  6. EOF
  7.  
  8. while read line
  9. do
  10. array=( ${array[@]} $line )
  11. echo ${array[@]}
  12. done

(请注意,具有管道的版本不一定适合于Bash,Bourne shell将在当前shell中运行while循环,但是Bash会在subshel​​l中运行它,至少默认情况下,在Bourne shell中,在循环中可以在主shell中可用;在Bash中,它们不是,第一个版本总是设置数组变量,以便循环后可以使用.)

您也可以使用:

  1. array+=( $line )

添加到数组.

猜你在找的Bash相关文章