Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to do this:

$ echo '
File
Contents
' > my-file

The problem is that the resulting file contains a newline at the beginning and at the end.

I tried this but didn't work:

$ echo '\
File
Contents\
' > my-file

What can I do?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Maybe you can use a HERE document?

cat << '==end' > my-file
File
Contents
==end
share|improve this answer
    
Yes this looks good. But how to send the result to a file? Right now it's only printing it on the screen. –  ChocoDeveloper Jul 23 '12 at 17:03
    
@ChocoDeveloper: Use redirection. Answer updated. –  choroba Jul 23 '12 at 17:05
    
Ah, that was tricky. Thanks! –  ChocoDeveloper Jul 23 '12 at 17:06
add comment
printf "%s\n%s\n%s\n" "Line 1", "Line 2", "Line 3"
share|improve this answer
    
I need to be able to paste the file contents without any change, I can't separate it into lines. And I need to be able to execute this as a command, I don't want to open vim and paste it manually. –  ChocoDeveloper Jul 23 '12 at 16:51
    
I think I cannot understand what you want to do. Do you want to just copy some files to another one? –  Peter Jul 23 '12 at 16:52
    
I want to copy/paste file contents (ie: something I don't want to write by hand each time, like a config file) into a command to create the file, to automate an installation. –  ChocoDeveloper Jul 23 '12 at 16:54
    
Then, a good example, is to use grep. For example, you find the line you want and then, count the lines you want to copy: grep myline -A6 will copy from "myline" and the next 6 lines. –  Peter Jul 23 '12 at 16:55
add comment

I would use cat instead:

cat > file

Paste what you want to go into file and finish with Ctrl+d on an empty line.

share|improve this answer
    
But I want to do everything in a single command. If I can paste to a file, I could as well just have pasted into the file I need. –  ChocoDeveloper Jul 23 '12 at 16:57
    
@ChocoDeveloper: ok, you're probably looking for the heredoc option then, see choroba's answer but with > file after the first ==end. –  Thor Jul 23 '12 at 17:04
add comment

Another simple answer not mentioned is:

echo -e "Line 1\nLine 2"

The -e option causes echo to interpret backslash escape sequences.

share|improve this answer
add comment

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.