Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to pass environment variables to exec shell command [duplicate]

Tags:

ruby

I'm using ruby 1.8.7 patch 249. Is the following the best/only way to pass environment variables to a shell command that I need to execute from my ruby program?

fork do           ENV['A'] = 'A'    exec "/bin/bash -c 'echo $A'" end  Process.wait 
like image 430
Kowshik Avatar asked Feb 20 '12 06:02

Kowshik


1 Answers

There is a really easy way:

system({"MYVAR" => "42"}, "echo $MYVAR") 

All credit for this goes to Avdi: https://stackoverflow.com/a/8301399/171933

like image 86
Johannes Fahrenkrug Avatar answered Sep 17 '22 01:09

Johannes Fahrenkrug