Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created October 1, 2011 06:28
Show Gist options
  • Save brainopia/1255687 to your computer and use it in GitHub Desktop.
Save brainopia/1255687 to your computer and use it in GitHub Desktop.
The easiest way to escape nested bash commands
def bash_escape(argument)
"$'" + argument.gsub("\\'", '\\\\\&').gsub("'", "\\\\'") + "'"
end
ruby_code = "puts 'something'"
command = "ruby -e #{bash_escape ruby_code}"
puts "ssh server #{bash_escape command}" # => ssh server $'ruby -e $\'puts \\\'something\\\'\''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment