Type:
Module

Manipulates strings like the UNIX Bourne shell

This module manipulates strings according to the word parsing rules of the UNIX Bourne shell.

The shellwords() function was originally a port of shellwords.pl, but modified to conform to POSIX / SUSv3 (IEEE Std 1003.1-2001 [1]).

Usage

You can use shellwords to parse a string into a Bourne shell friendly Array.

require 'shellwords'

argv = Shellwords.split('three blind "mice"')
argv #=> ["three", "blind", "mice"]

Once you've required Shellwords, you can use the split alias String#shellsplit.

argv = "see how they run".shellsplit
argv #=> ["see", "how", "they", "run"]

Be careful you don't leave a quote unmatched.

argv = "they all ran after the farmer's wife".shellsplit
     #=> ArgumentError: Unmatched double quote: ...

In this case, you might want to use ::escape, or it's alias String#shellescape.

This method will escape the String for you to safely use with a Bourne shell.

argv = Shellwords.escape("special's.txt")
argv #=> "special\\s.txt"
system("cat " + argv)

Shellwords also comes with a core extension for Array, Array#shelljoin.

argv = %w{ls -lta lib}
system(argv.shelljoin)

You can use this method to create an escaped string out of an array of tokens separated by a space. In this example we'll use the literal shortcut for Array.new.

Authors

  • Wakou Aoyama

  • Akinori MUSHA <knu@iDaemons.org>

Contact

  • Akinori MUSHA <knu@iDaemons.org> (current maintainer)

Resources

1: IEEE Std 1003.1-2004

split

split(line) Class Public methods Alias for:

2015-05-14 22:36:08
escape

escape(str) Class Public methods Alias for:

2015-05-14 22:14:25
shellescape

shellescape(str) Class Public methods Escapes a string so that it can be safely

2015-05-14 22:22:05
shellsplit

shellsplit(line) Class Public methods Splits a string into an array of tokens

2015-05-14 22:29:16
join

join(array) Class Public methods Alias for:

2015-05-14 22:19:18
shelljoin

shelljoin(array) Class Public methods Builds a command line string from an argument

2015-05-14 22:24:15
shellwords

shellwords(line) Class Public methods Alias for:

2015-05-14 22:33:15