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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With