Celeb Glow
updates | March 16, 2026

How to pass variables to a HEREDOC in bash?

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

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.

ah, here it is in the man page (look 3.6.6):

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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