new line in heredoc disappearing in file
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
Solution 1:
Always use double quotes for variables that contain whitespace:
echo "$FILE" > ...