Use /bin/bash for %x in Ruby

2019-08-09 05:27发布

问题:

%x{ echo hi } 

seems to fork off /bin/sh. I'd prefer /bin/bash. Is there a way to set that? The best thing I can think of is

%x {/bin/bash -c 'echo hi'}

but that doesn't to report output back like it should. Also, it's a general thing: I just never want /bin/sh, I always want /bin/bash

回答1:

I expect the default shell /bin/sh is hardcoded in order to be as portable as possible. To use bash, you could do something like this:

def bash(cmd)
  IO.popen(["/bin/bash", "-c", cmd]) {|io| io.read}
end

output = bash %q(cat <<< "hello world")
p output
"hello world\n"


回答2:

Write this somewhere in your Ruby code.

ENV["SHELL"] = "/bin/bash"


标签: ruby shell