Use /bin/bash for %x in Ruby

2019-08-09 05:21发布

%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

标签: ruby shell
2条回答
虎瘦雄心在
2楼-- · 2019-08-09 05:41

Write this somewhere in your Ruby code.

ENV["SHELL"] = "/bin/bash"
查看更多
戒情不戒烟
3楼-- · 2019-08-09 05:45

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"
查看更多
登录 后发表回答