Ruby run shell command in a specific directory Ruby run shell command in a specific directory ruby ruby

Ruby run shell command in a specific directory


You can use the block-version of Dir.chdir. Inside the block you are in the requested directory, after the Block you are still in the previous directory:

Dir.chdir('mydir'){  %x[#{cmd}]}


Ruby 1.9.3 (blocking call):

require 'open3'Open3.popen3("pwd", :chdir=>"/") {|i,o,e,t|  p o.read.chomp #=> "/"}Dir.pwd #=> "/home/abe"


also, taking the shell route

%x[cd #{dir} && #{cmd}]