Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo but retain double quotes

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 protected]"
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 protected]
EMAILMESSAGE=/tmp/emailmessage.txt

Desired output:

# cat /root/email.txt
SUBJECT="Text here"
EMAIL="[email protected]"
EMAILMESSAGE="/tmp/emailmessage.txt"

Any ideas?

Cheers

like image 590
bsmoo Avatar asked Apr 10 '13 15:04

bsmoo


2 Answers

Double quotes don't nest. Use single quotes:

echo '
SUBJECT="Text here"
EMAIL="[email protected]"
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 protected]"
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 protected]"
EMAILMESSAGE="/tmp/emailmessage.txt"
EOF
like image 126
Keith Thompson Avatar answered Sep 19 '22 06:09

Keith Thompson


escaping double quotes will work

echo "a\"" 
output -> a"

try this:

echo "
SUBJECT=\"Text here\"
EMAIL=\"[email protected]\"
EMAILMESSAGE=\"/tmp/emailmessage.txt\"
" > /root/email.txt
like image 28
novomanish Avatar answered Sep 19 '22 06:09

novomanish