Can I read line from a heredoc in bash?

前端 未结 3 1393
甜味超标
甜味超标 2021-02-13 22:43

Here\'s what I\'m trying. What I want is the last echo to say \"one two three four test1...\" as it loops. It\'s not working; read line is coming up em

3条回答
  •  日久生厌
    2021-02-13 22:53

    I would normally write:

    while read line
    do
        array=( ${array[@]} $line )
        echo ${array[@]}
    done <

    Or, even more likely:

    cat <

    (Note that the version with a pipe will not necessarily be suitable in Bash. The Bourne shell would run the while loop in the current shell, but Bash runs it in a subshell — at least by default. In the Bourne shell, the assignments made in the loop would be available in the main shell after the loop; in Bash, they are not. The first version always sets the array variable so it is available for use after the loop.)

    You could also use:

    array+=( $line )
    

    to add to the array.

提交回复
热议问题