首页 > 解决方案 > 我如何在回声中添加引号?

问题描述

我需要一些帮助。我正在尝试做这样的事情

echo "{
    "server":"$SERVER_HOST",
    "server_port":"$SERVER_PORT",
    "password":"$PASSWORD_CHOICE",
    "timeout":$TIMEOUT_CHOICE
}" >> /etc/ss/example.json

我需要像这样的输出

{
    "server":"127.0.0.1",
    "server_port":80,
    "password":"randompassword",
    "timeout":60
}

但输出总是像

{
    server:127.0.0.1,
    server_port:80,
    password:randompassword,
    timeout:60
}

标签: jsonlinuxshellubuntuecho

解决方案


不要使用echo,而是cat使用此处的文档:

cat >> /etc/ss/example.json <<EOF
{
  "server":"$SERVER_HOST",
  "server_port":"$SERVER_PORT",
  "password":"$PASSWORD_CHOICE",
  "timeout":$TIMEOUT_CHOICE
}
EOF

推荐阅读