In Java, if you know for certain a file is very small, you can use readBytes() method to read the content in one go instead of read it line by line or using buffer.
Just wondering in shell script, I know we can do something like:
while read line do echo $line LINE = $line done < "test.file" echo $LINE If my test.file is like:
testline1 testline2 testline3 This only gives me the last line to $LINE. $LINE contains "testline3".
My question is: How can I read the whole file with multiple lines into one single variable,so I can get $LINE="testline1\ntestline2\ntestline3"?
DATA=$(cat file), but why would you want to? Just read the file whenever you need the content!LINE = $linedoesn't actually work; the space around the equals sign renders it invalid. Second, if you're manipulating text that you want to preserve literally, you should setIFSto null for your read calls (otherwise leading whitespace on each line will be nuked) and pass the-roption toreadand the-Eoption toecho(otherwise, any backslashes in your file will mess things up). See my answer below for example.-Esince that's Bash's default behavior forecho.