-3

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.

2
0

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"
a
b
$ B=( $(echo -e "a\nb") )
$ echo $B
a
$ echo "${B[*]}"
a b

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