shell 使用包含特殊字符的变量的脚本

shell script using a variable that contain special characters

我在 shell 脚本中使用 Redis,我正在尝试设置键的值

这是我的脚本,运行良好:

:/# redis-cli JSON.SET etat . '{"name":"Eric"}'
Ok

但是当我使用包含我的 Json 的变量时,我得到的是:

:/# json="'{\"name\":\"Erci\"}'"
:/# ehco $json
'{"name":"Eric"}'
:/# redis-cli JSON.SET etat . $json
(error) ERR wrong number of arguments for 'JSON.SET' command

我试过了:

:/# redis-cli JSON.SET etat . ${json}
(error) ERR wrong number of arguments for 'JSON.SET' command

和:

:/# redis-cli JSON.SET etat . "$json"
(error) ERR JSON lexer error SPECIAL_EXPECTED at position 26

有人可以帮忙吗?

您不需要变量内的单刻度:

$ json="{\"name\":\"Erci\"}"
$ redis-cli JSON.SET etat . "$json"
OK
$ redis-cli JSON.GET etat
"{\"name\":\"Erci\"}"