回声但保留双引号 [英] Echo but retain double quotes
本文介绍了回声但保留双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个将变量/文本回显到文件的脚本-该脚本的片段为:
echo
SUBJECT =在此处输入文字
EMAIL = email@domain.co.uk
EMAILMESSAGE = / tmp / emailmessage.txt
> /root/email.txt
这很好,但是所有双引号都被删除了。 / p>
当前输出:
#cat /root/email.txt
SUBJECT =在此处输入文本
EMAIL=email@domain.co.uk
EMAILMESSAGE = / tmp / emailmessage.txt
所需的输出:
#cat /root/email.txt
SUBJECT =在此输入文字
EMAIL = email@domain.co.uk
EMAILMESSAGE = / tmp / emailmessage.txt
有什么想法吗?
欢呼声
解决方案
双引号不嵌套。使用单引号:
echo'
SUBJECT =在此处输入文字
EMAIL = email @ domain .co.uk
EMAILMESSAGE = / tmp / emailmessage.txt
'> /root/email.txt
但这会在文件的顶部和底部添加空行。如果您不想这样:
echo'SUBJECT =在此处输入文字
EMAIL = email @ domain .co.uk
EMAILMESSAGE = / tmp / emailmessage.txt'> /root/email.txt
可能更干净的解决方案是使用此处文档
猫> email.txt<<'EOF'
SUBJECT =在此输入文字
EMAIL = email@domain.co.uk
EMAILMESSAGE = / tmp / emailmessage.txt
EOF
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
解决方案
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
这篇关于回声但保留双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文