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'm echoing a heredoc into a file, but the line breaks are being lost

Running this script

#!/bin/bash 

NAME="$1"

mkdir -p $NAME

FILE=$(cat <<SETVAR
name = "$NAME"
type = "test"
SETVAR
)

echo $FILE > $NAME/$NAME.txt

With the arg: foo, results in the file at foo/foo.txt containing

name = "foo" type = "test"

Any ideas? Thanks

share|improve this question
    
Is it on cygwin? is your result from a text editor or a cat? in other words, could it be that the newlines are here but not shown (because of the win/linux endlines differences)? Have you tried adding "\n"s? –  m4573r Dec 19 '13 at 11:43
    
i tried putting in \n but it came through as plain text. was doing all through linux terminal –  joevallender Dec 19 '13 at 11:51

1 Answer 1

up vote 1 down vote accepted

Always use double quotes for variables that contain whitespace:

echo "$FILE" > ...
share|improve this answer
    
ah - of course. great! thanks :) –  joevallender Dec 19 '13 at 11:52

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.