Module: Shellwords

Defined in:
lib/harbor/shellwords.rb

Overview

This is a backport of ruby-1.8.7’s Shellwords.escape function to 1.8.6 for safely escaping filenames to be passed directly to the shell.

Class Method Summary collapse

Class Method Details

.escape(str) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/harbor/shellwords.rb', line 8

def escape(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