Search code examples
bashshelldouble-quotesheredoc

Using double quotes around shell variables in a heredoc


I write a bash script and get some user input with read. I want to put the variables, I got by read, in a file with cat << EOF >> file. My problem is that every variable get "double quots". How can I prevent this?

echo "Whats your name?"
read yourname

cat << EOF >> /path/to/file
Your Name is "${yourname}"

EOF

The content of the file is:

Your Name is "djgudb"

It should be:

Your Name is djgudb

Solution

  • Quotes have no syntactic meaning in heredocs, so don't put them there if you don't want them to be literal.

    echo "Whats your name?"
    read yourname
    
    cat << EOF >> /path/to/file
    Your Name is ${yourname}
    
    EOF