shellescape(str) public

Escapes a string so that it can be safely used in a Bourne shell command line.

Note that a resulted string should be used unquoted and is not intended for use in double quotes nor in single quotes.

open("| grep #{Shellwords.escape(pattern)} file") { |pipe|
  # ...
}

String#shellescape is a shorthand for this function.

open("| grep #{pattern.shellescape} file") { |pipe|
  # ...
}

It is caller’s responsibility to encode the string in the right encoding for the shell environment where this string is used. Multibyte characters are treated as multibyte characters, not bytes.

Show source
Register or log in to add new notes.