Reputation: 83
I need some help. I am trying to do something like this
echo "{
"server":"$SERVER_HOST",
"server_port":"$SERVER_PORT",
"password":"$PASSWORD_CHOICE",
"timeout":$TIMEOUT_CHOICE
}" >> /etc/ss/example.json
and i need the output like
{
"server":"127.0.0.1",
"server_port":80,
"password":"randompassword",
"timeout":60
}
but the output is always like
{
server:127.0.0.1,
server_port:80,
password:randompassword,
timeout:60
}
Upvotes: 2
Views: 552
Reputation: 12698
you have two approaches:
'
, then use "
, or viceversa) but you normally lose the possibility of interpreting shell variables, if you use '
externally.\\
character to escape the shell interpretation, as incat(1)
and in-flow redirecting input with <<
is shell dependant (not all shells allow this, so you have to be carefull)echo "{
"\""server"\"":"\""$SERVER_HOST"\"",
"\""server_port"\"":"\""$SERVER_PORT"\"",
"\""password"\"":"\""$PASSWORD_CHOICE"\"",
"\""timeout"\"":$TIMEOUT_CHOICE
}" >> /etc/ss/example.json
you have just to convert each "
inside the string into "\""
(if you have used double quotes to quote your string). The same also works for single quotes (in case you have used single quotes to quote your string), converting each '
into '\''
.
Upvotes: 0
Reputation: 22291
Don't use echo
, but cat
with a here-document:
cat >> /etc/ss/example.json <<EOF
{
"server":"$SERVER_HOST",
"server_port":"$SERVER_PORT",
"password":"$PASSWORD_CHOICE",
"timeout":$TIMEOUT_CHOICE
}
EOF
Upvotes: 2
Reputation: 362
use backslash to put special characters like \"
or \n
for nextline or \t
for tab
echo "this is my quotes -> \" <-"
the out put will be
this is my quotes -> " <-
Upvotes: 1