Echo but retain double quotes

bash, echo, shell, ubuntu

Solution

Double quotes don't nest. Use single quotes:

echo '
SUBJECT="Text here"
EMAIL="email@domain.co.uk"
EMAILMESSAGE="/tmp/emailmessage.txt"
' > /root/email.txt

But this will add empty lines to the top and bottom of the file. If you don't want those:

echo 'SUBJECT="Text here"
EMAIL="email@domain.co.uk"
EMAILMESSAGE="/tmp/emailmessage.txt"' > /root/email.txt

Probably a cleaner solution is to use a "here document"

cat > email.txt <<'EOF'
SUBJECT="Text here"
EMAIL="email@domain.co.uk"
EMAILMESSAGE="/tmp/emailmessage.txt"
EOF

Problem

I'm trying to create a script which will echo varibles / text to a file - a snippet of this script is: ``` echo " SUBJECT="Text here" EMAIL="email@domain.co.uk" EMAILMESSAGE="/tmp/emailmessage.txt" " > /root/email.txt ``` This is working fine but all of the double quotes are being removed. Current output: ``` # cat /root/email.txt SUBJECT=Text here EMAIL=email@domain.co.uk EMAILMESSAGE=/tmp/emailmessage.txt ``` Desired output: ``` # cat /root/email.txt SUBJECT="Text here" EMAIL="email@domain.co.uk" EMAILMESSAGE="/tmp/emailmessage.txt" ``` Any ideas? Cheers

Original source