Json 如何在回音中使用引号?
我需要一些帮助。 我正试着做这样的事情Json 如何在回音中使用引号?,json,linux,shell,ubuntu,echo,Json,Linux,Shell,Ubuntu,Echo,我需要一些帮助。 我正试着做这样的事情 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,
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
}
使用反斜杠放置特殊字符,如下一行的
\“
或\n
,或选项卡的\t
echo“这是我的报价->\”试试这个
<?php
echo ('
"server":"127.0.0.1",
"server_port":80,
"password":"randompassword",
"timeout":60
');
?>
不要将echo
,而是将cat
与此处的文档一起使用:
cat >> /etc/ss/example.json <<EOF
{
"server":"$SERVER_HOST",
"server_port":"$SERVER_PORT",
"password":"$PASSWORD_CHOICE",
"timeout":$TIMEOUT_CHOICE
}
EOF
cat>/etc/ss/example.json您有两种方法:
- 第一个问题已经在其他回答中得到了回答,即使用其他引号(如果您使用了
”
,然后使用“
,或反之亦然),但如果您在外部使用”
,则通常会失去解释shell变量的可能性
- 第二种方法是去掉引号,并使用escape
\\
字符来转义shell解释,如中所示
- 在一些响应中使用转义字符(在引号中使用转义字符)的方法通常会产生不好的结果(取决于使用的shell,转义在单引号中甚至在双引号中都不起作用)。最好的方法是退出引号,转义,然后再次进入引号,如下所示
- 使用
cat(1)
和流中重定向输入的方法这是我正在使用的php shellOk尝试这个新的echo“{”“server”“\”:“127.0.0.1”“,“server”“\”:“127.0.0.1”“,“server”“port”“80”“密码”“密码”“密码”“随机密码”“超时”“60}令人惊讶。A+卓越A+惊人,请继续
echo "{
""\"server""\": ""\"127.0.0.1""\",
""\"server""\":""\"127.0.0.1""\",
""\"server_port""\":80,
""\"password""\":""\"randompassword""\",
""\"timeout""\":60
}"
cat >> /etc/ss/example.json <<EOF
{
"server":"$SERVER_HOST",
"server_port":"$SERVER_PORT",
"password":"$PASSWORD_CHOICE",
"timeout":$TIMEOUT_CHOICE
}
EOF
echo "{
"\""server"\"":"\""$SERVER_HOST"\"",
"\""server_port"\"":"\""$SERVER_PORT"\"",
"\""password"\"":"\""$PASSWORD_CHOICE"\"",
"\""timeout"\"":$TIMEOUT_CHOICE
}" >> /etc/ss/example.json