Shengjie Shengjie - 1 year ago 70
Bash Question

How to read the file content into a variable in one go?

In Java, if you know for certain a file is very small, you can use

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
echo $line
LINE = $line
done < "test.file"
echo $LINE

If my test.file is like:


This only gives me the last line to
contains "testline3".

My question is: How can I read the whole file with multiple lines into one single variable,so I can get

Answer Source

Process the lines inside the loop instead of after it. If you really need the file in a variable:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download