Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby run shell command in a specific directory

I know how to run a shell command in Ruby like:

%x[#{cmd}] 

But, how do I specify a directory to run this command?

Is there a similar way of shelling out, similar to subprocess.Popen in Python:

subprocess.Popen(r'c:\mytool\tool.exe', cwd=r'd:\test\local') 

Thanks!

like image 905
icn Avatar asked Apr 13 '12 20:04

icn


People also ask

What does CD mean in Ruby?

cd means change directory.


1 Answers

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}] } 
like image 117
knut Avatar answered Sep 28 '22 07:09

knut