20

I want to do something like this:

$ NAME=John
$ cat << '==end' > test
My name is $NAME
==end

$ cat test
My name is John

Any ideas?

1 Answer 1

32
cat <<EOF > test
My name is $NAME
EOF

or even

cat <<==end > test
My name is $NAME
==end

Worked for me.

Looks like when you take ==end in the ' variable doesn't substitute.

The Here Documents section of the manual says:

The format of here-documents is:

      <<[-]word
              here-document
      delimiter

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. [...]

2
  • You can also use double-quotes (cat << "==end" > test), but hard-quotes prevent substitution indeed.
    – Mat
    Aug 2, 2012 at 7:31
  • Double quotes also prevent parameter expansion
    – Quantum7
    Jun 12, 2020 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .