Run a Command Line with Custom Environment

Run a command line with custom environment

Open.popen3 optionally accepts a hash as the first argument (in which case your command would be the second argument:

cmd = 'a_prog --arg ... --arg2 ...'
Open3.popen3({"MYVAR" => "a_value"}, "#{cmd}") { |i,o,e|
output =
error =
# FIXME: don't want to *separate out* stderr like this
repr = "$ #{cmd}\n#{output}"

Open uses Process.spawn to start the command, so you can look at the documentation for Process.spawn to see all of it's options.

Running a command with specific environment settings - one-liner

set foo=bar&.\ foo=

It should be noted that batch files are parsed one line at the time, so one liners like this are problematic there (setlocal ENABLEDELAYEDEXPANSION can help out in those cases)

Setting an environment variable before a command in Bash is not working for the second command in a pipe

FOO=bar bash -c 'somecommand someargs | somecommand2'

Related Topics

Leave a reply