Take the 2-minute tour ×
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.

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

3 Answers 3

up vote 8 down vote accepted
printf "Host localhost\n    ForwardAgent yes" >> file.txt

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

printf "Host localhost\n\tForwardAgent 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
    
thanks man i haven't even heard of printf before ill read up on that! –  TheLegend May 27 '13 at 15:00
    
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 at 17:01
# 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
    
thanks Hauke Laging! possibility 3 is what i was looking for. sorry that its a silly question –  TheLegend May 27 '13 at 14:57
4  
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
    
One more possibility is ( echo "line 1" ; echo "line 2" ) >>greetings.txt. –  ott-- May 27 '13 at 15:38
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
echo -e "Hello \nWorld \n" >> greetings.txt
share|improve this answer

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.