I'm passing ENVs with JSON into Docker. I need to use a string on the shell for a parameter. In the shell, the right way to escape a single quote is by using '\\''
. So, "Tom's Market"
becomes "Tom'\\''s Market"
as in:
docker run -it -e FOO='["Tom'\''s Market"]' ...
Ruby's Shellwords does not appear to do this.
Shellwords.shellescape('["Tom\'s Market"]')
#=> \[\"Tom\'s\ Market\"\]
So I wanted to use simple find/replace:
'["Tom\'s Market"]'.gsub("'", "'\\''")
#=> ["Tom's Market"]'s Market"]
I don't know what's going on there. Can someone help me?
Well, backslash is how you escape in ruby strings. To escape it you need another backslash.
The is usually to add backslashes until it works
puts %q{["Tom's Market"]}.gsub("'", "'\\\\''")
# ["Tom'\''s Market"]
Shellwords.escape
的结果旨在不加引号地使用:
docker run -it -e FOO=\[\"Tom\'s\ Market\"\]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.