10
May

Echo or Cat Multiple Lines or Paragraph of Text from within a Shell Script

If you find yourself needing to echo out multiple lines to the console, or even to another file, then you’ll want to use the following method which is much cleaner and much more efficient. It’s very useful in creating another document, script, or file without having to use echo for each line.

#!/bin/sh

cat > new_file << EOF
This will be line one
This will be line two
This will be line three
   This will be line four indented
Notice the absence of spaces on the next line
EOF

cat new_file

Basically, what the above script will do is use cat to write the following lines up to a delimiter, in this case EOF, to a file called new_file. The most important thing to keep in mind is that for this to work, there cannot be any spaces before the delimiter. The output of running this script looks like:

This will be line one
This will be line two
This will be line three
   This will be line four indented
Notice the absence of spaces on the next line

Naturally, this method will also work in the terminal command line and is not restricted to only shell scripts.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 2 Comments So Far

Share your thoughts, leave a comment!