BASH - Reading Multiple Lines from Text File
bash, concatenation, text
Solution
You can translate the `\n`(newline) to ``(space):
$ text=$(tr '\n' ' ' <file.txt)
$ echo $text
this is line 1 this is line 2 this is line 3
If lines ends with `\r\n`, you can do this:
$ text=$(tr -d '\r' <file.txt | tr '\n' ' ')
Problem
i am trying to read a text file, say file.txt and it contains multiple lines. say the output of `file.txt` is ``` $ cat file.txt this is line 1 this is line 2 this is line 3 ``` I want to store the entire output as a variable say, `$text`. When the variable `$text` is echoed, the expected output is: ``` this is line 1 this is line 2 this is line 3 ``` my code is as follows ``` while read line do test="${LINE}" done < file.txt echo $test ``` the output i get is always only the last line. Is there a way to concatenate the multiple lines in file.txt as one long string?