How do I read multiple lines from STDIN into a variable?

Posted by The Wicked Flea on Server Fault See other posts from Server Fault or by The Wicked Flea
Published on 2012-04-09T21:20:50Z Indexed on 2012/04/09 23:33 UTC
Read the original article Hit count: 198

Filed under:
|

I've been googling this question to no avail. I'm automating a build process here at work, and all I'm trying to do is get version numbers and a tiny description of the build which may be multi-line. The system this runs on is OSX 10.6.8.

I've seen everything from using CAT to processing each line as necessary. I can't figure out what I should use and why.

Attempts

read -d '' versionNotes

Results in garbled input if the user has to use the backspace key. Also there's no good way to terminate the input as ^D doesn't terminate and ^C just exits the process.

read -d 'END' versionNotes

Works... but still garbles the input if the backspace key is needed.

while read versionNotes
do
  echo "  $versionNotes" >> "source/application.yml"
done

Doesn't properly end the input (because I'm too late to look up matching against an empty string).

© Server Fault or respective owner

Related posts about bash

Related posts about scripting