LoginSignup
3
3

More than 5 years have passed since last update.

Rubyから外部コマンドを叩くときはShellwordsモジュールが便利

Posted at

Rubyから外部コマンドを実行するときはShellwordsモジュールが便利 - ブログのおんがえし

シェルから引数を受けとってまたシェルに外部コマンドとして渡すような場合、

require 'shellwords'

system("ls #{ARGV[0]}")

以下のようにしておくと空白文字などのシェルメタ文字が来ても動くようになります。

require 'shellwords'

system("ls #{Shellwords.escape(ARGV[0])}")
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3