Shellwords.shellescape implementation for Ruby 1.8

While in version 1.8.7 I have a version with backported Shellwords::shellescape , I know that this method is a 1.9 function and is definitely not supported in earlier versions of 1.8. Does anyone know where I can find, both in Gem form and in fragment form, a reliable standalone implementation of the Bourne-shell command for escaping Ruby?

+5
ruby bash shell escaping sh
Aug 20 '09 at 14:45
source share
2 answers

I ended up working with the Escape gem, which has the additional function of using quotes by default and only backslash escaping when necessary.

+5
Aug 24 '09 at 14:02
source share

You could just copy what you want from shellwords.rb in the boot of the Ruby subversion repository (which is GPLv2 'd):

  def shellescape(str) # An empty argument will be skipped, so return empty quotes. return "''" if str.empty? str = str.dup # Process as a single byte sequence because not all shell # implementations are multibyte aware. str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1") # A LF cannot be escaped with a backslash because a backslash + LF # combo is regarded as line continuation and simply ignored. str.gsub!(/\n/, "'\n'") return str end 
+9
Aug 24 '09 at 0:47
source share



All Articles