-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.

Tim
  • 101,790

1 Answers1

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
user1133275
  • 5,574