Skip to content

Instantly share code, notes, and snippets.

@akm
Created February 27, 2015 07:44
Show Gist options
  • Save akm/c6d06c1d81d8a7189c5e to your computer and use it in GitHub Desktop.
Save akm/c6d06c1d81d8a7189c5e to your computer and use it in GitHub Desktop.
single quote escape for bash
def sq_escape(str)
str.split(/\'/).map{|s| "'#{s}'"}.join("\\'")
end
sq_escape {"breakfast"=>"Bill's"}.to_json
#=> "'{\"breakfast\":\"Bill'\\''s\"}'"
puts sq_escape {"breakfast"=>"Bill's"}.to_json
#=> '{"breakfast":"Bill'\''s"}'
# # on bash
# $ echo '{"breakfast":"Bill'\''s"}'
# {"breakfast":"Bill's"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment