If you have a file that ends like this: $ tail -2 cordless_drill The example script below uses this technique to add the warranty disclosure to any file provided as an argument to the script or provided at the prompt: #!/bin/bash For this, you can use a multi-line echo command like one of those shown above. Adding text using a scriptĪnother option is to add the lines using a script. In this and earlier examples, you need to be careful not to overwrite the target file by using a single ">". Once the text file is ready, run a command like this to append it: $ cat warranty_disclaimer > cordless_drill How the buyer uses it nor for any damages caused during or by its use. Promise that it will work as advertised or that it will continue to workįor any particular period of time. The information provided about this product is not intended to provide any Here's an example using the addition of a warranty disclaimer to the end of a file detailing a product: $ cat warranty_disclaimer To add text from one file to the end of another, we could first create the file to be appended. Two other options are 1) putting the text into a separate file and appending the content of that file to the one you're updating, and 2) adding the text using a script. If you want to add a large amount of text, however, even the multi-line echo command above can require more effort than it's worth. Likely less inclined to cuddle it afterwards. You would be better off skinning a catfish and > likely less inclined to cuddle it afterwards." > cats > You would be better off skinning a catfish and
> but skinned cats are never very cuddly.
Here's an example of doing this on the command line: $ echo "There is more than one way to skin a cat
You can also just type each line separately as long as you don't end the quote until the end of the text. This type of multi-line echo requires that you start your echo command on one line with an opening double-quote ("), add quote-free lines and then close the echo command on the last line with a closing double-quote. More text more text to be added and so on and so on The -e option makes the echo command interpret the \n sequences as newlines, so your file will include these lines: $ tail -5 myfile One option is to add multiples lines with a single echo or printf command that includes newline characters like this: $ echo -e "text to be added to file \nmore text" > myfile $ printf "more text to be added \nand so on \nand so on \n" > myfile
When you want to build a script that will add a series of lines to a file, the echo and printf methods just shown can get a bit tiresome. Use a command like this with the \n to include it: $ printf "appended text\n" > myfile You can also use the printf command to append text, but keep in mind that by default it will not add a newline as if you hit the Enter key. The echo commands append lines of text to existing files in a straightforward way: $ echo "text to be added to file" > myfile