12

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

2
  • 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, 2013 at 11:43
  • i tried putting in \n but it came through as plain text. was doing all through linux terminal Dec 19, 2013 at 11:51

1 Answer 1

19

Always use double quotes for variables that contain whitespace:

echo "$FILE" > ...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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