This question already has an answer here:

In bash, we can read an file into an array, so that each line in the file becomes an element of the array.

$ arr=( $(cat myfile) )

I was wonder how cat works so that it allows reading a file line as an array element? Does cat separate the file content into lines?

Does cat not just read all the content of the file at once, without separating it into lines, so that arr has just one single element, which is the entire content of the file? Thanks.

marked as duplicate by muru, Thomas, Kusalananda bash Nov 17 '18 at 13:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


It's not cat, it's the =() if you use ="" it would not be an array.

$ A="$(echo -e "a\nb")"
$ echo $A
a b
$ echo "$A"
$ B=( $(echo -e "a\nb") )
$ echo $B
$ echo "${B[*]}"
a b

Not the answer you're looking for? Browse other questions tagged or ask your own question.