Escaping string for sending to command line or shell (and other uses)

In Ruby 1.9 the library Shellwords has some useful methods to help translate between the program and the command line interface. When sending strings to the cli, some characters are not well received as they are reserved for use by the shell program. To use the characters, you must escape them. This is where the Shellwords becomes useful.

Example to escape command with arguments

require 'shellwords'


open('|' + ['grep', pattern, *files].shelljoin) { |pipe|
  # ...
}

# or

open('|' + Shellwords.join(['grep', pattern, *files])) { |pipe|
  # ...
}

# or

url = "http://domain.com/path/to/page.html"
filename = "output.cfm"

command = ['/opt/local/bin/wget', url, '-O', filename].shelljoin
system(command)

Example of splitting string with quotes embedded

This is pretty cool as I could see this being useful for just making an array from a string that contains a mix of text and quoted text

require 'shellwords'

argv = Shellwords.split('here are "two words"')
argv #=> ["here", "are", "two words"]

or

argv = 'here are "two words"'.shellsplit
argv #=> ["here", "are", "two words"]
comments powered by Disqus