AVarf
AVarf

Reputation: 5189

How to make Ruby compatible with bash

I have a ruby script that uses open3 to run shell commands from inside the script and I am using Ubuntu 18.04 and bash.

When I run that script it produces errors such as 0: sh: 2: pushd: not found. I searched and one thing I found was https://stackoverflow.com/a/17044484/5553963 which suggested that we use ENV["SHELL"] = "/bin/bash" but when I checked my environment variable I already have that:

$ printenv "SHELL"
/bin/bash

How I can make this script working on my machine?

The first part of that script:

#!/usr/bin/env ruby

require 'open3'

def run(i, cmd)
    res = ""
    Open3.popen3(cmd) do |stdin, stdout, stderr, thread|

And my ruby version: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]

Upvotes: 0

Views: 384

Answers (1)

Stephan
Stephan

Reputation: 185

Presuming you want the shell builtin pushd, you'd need to explicitly call bash in your system command, as ruby doesn't reference your $SHELL variable. Something like:

require 'open3'
require 'shellwords'
Open3.popen3("bash -c #{Shellwords.escape(cmd)}")

should do the trick.

Upvotes: 5

Related Questions