Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am writing a bash script to look for a file if it doesn't exist then create it and append this to it:

Host localhost
    ForwardAgent yes

So "line then new line 'tab' then text" I think its a sensitive format. I know you can do this:

cat temp.txt >> data.txt

But it seems weird since its two lines. Is there a way to append that in this format:

echo "hello" >> greetings.txt
share|improve this question
up vote 36 down vote accepted
printf '%s\n    %s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt

Or, if it's a literal tab that you want (rather than the four spaces in your question):

printf '%s\n\t%s\n' 'Host localhost' 'ForwardAgent yes' >> file.txt

You can achieve the same effect with echo, but exactly how varies from implementation to implementation, whereas printf is constant.

share|improve this answer
3  
With the gnu coreutils echo will be: echo -e "Host localhost\n\tForwardAgent yes" >> file.txt. -e will enable the interpretation of backslashes ;) – nwildner Jul 30 '14 at 17:01
1  
this doesn't work. %s means - literally interpreted string argument. – mikeserv Sep 5 '14 at 20:29
1  
You can use \n for newline in your format argument (where you have '%s'), but not in the rest. So your answer doesn't work. And putting it in the format argument instead gives issues with %. – derobert Sep 5 '14 at 20:38
# possibility 1:
echo "line 1" >> greetings.txt
echo "line 2" >> greetings.txt

# possibility 2:
echo "line 1
line 2" >> greetings.txt

# possibility 3:
cat <<EOT >> greetings.txt
line 1
line 2
EOT
share|improve this answer
1  
thanks Hauke Laging! possibility 3 is what i was looking for. sorry that its a silly question – TheLegend May 27 '13 at 14:57
6  
You got two good answers. You should mark the answer you really like as accepted. – unxnut May 27 '13 at 15:00
1  
@TheLegend That is called a "here document". Have a look at that paragraph in the man page. – Hauke Laging May 27 '13 at 15:00
1  
@ott-- You don't need a real subshell (i.e. can save one new process), this is enough: { echo "line 1" ; echo "line 2"; } >>greetings.txt – Hauke Laging May 27 '13 at 16:10
1  
@cikatomo In cat <<EOT the EOT is just a random string. Could be cat <<FOO, too. – Hauke Laging Jan 9 at 20:37
echo -e "Hello \nWorld \n" >> greetings.txt
share|improve this answer

SED can append a line to the end of a file like so:

sed -i '$ a text to be inserted' fileName.file
$ selects end of file, the a tells it to append, and after this comes the text that is to be inserted. Then of course the file name.


Source: http://www.yourownlinux.com/2015/04/sed-command-in-linux-append-and-insert-lines-to-file.html

==========EDIT============

Does this approach have any added benefit than other solutions?
Yes, this approach has the added benefit of appending to any files return in a search, such as this: find . -name "*.html" -exec sed -i '$ a </html>' {} \;

I used the above example to insert the ending html tag that was missing on every html page within a number of directories.

===================================

share|improve this answer
1  
If you would show the syntax for appending multiple lines to a file, then this would be an answer to the question (although not a very useful one).  All the other answers simply write the new text.  This answer reads the entire file and rewrites it, plus the added text.  Does this approach have any benefit over the others? – G-Man Mar 18 at 20:09
    
This only works with GNU sed. BSD sed bails with the error: command a expects \ followed by text. So don't use this for a scripted solution; it's liable to break. (Or do it properly as the BSD required syntax is POSIX compatible and will work on GNU sed as well. – Wildcard Mar 18 at 20:28
    
The other answers can be used with find as follows: find . -name "*.txt" -exec sh -c 'printf "%s\n\t%s\n" "Host localhost" "ForwardAgent yes" >> "$1"' sh {} ";". – G-Man Mar 19 at 4:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.