Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify the shell to use for a ruby system call?

Tags:

bash

shell

ruby

I am trying to run commands from ruby via system (or by using backticks), but am running into problems. When I try to call a command, the shell is unable to find it, even though I know it works if I call it straight. For example:

`zip`
>> sh: zip: command not found

The problem seems to be that ruby is using the sh shell, in which $PATH is not set correctly, rather than bash, and I am not sure why. The user my application is running under is set up to use bash by default.

Is there any way to tell ruby to use bash instead of sh?

like image 224
Daniel Vandersluis Avatar asked Aug 06 '09 15:08

Daniel Vandersluis


People also ask

How do you call a shell script in Ruby?

First, note that when Ruby calls out to a shell, it typically calls /bin/sh , not Bash. Some Bash syntax is not supported by /bin/sh on all systems. This is like many other languages, including Bash, PHP, and Perl. Returns the result (i.e. standard output) of the shell command.

What is Ruby command?

Ruby command is a free and open source programming language; it is flexible and is feature rich. As the name suggests, ruby indeed is a jewel language which comes at a very low entry cost. Its plug and play capability and also easily readable syntax makes it very user-friendly.


2 Answers

As far as I know, the only way to do that is to explicitly invoke the shell, e.g.

`bash -c zip`

or

`#{ ENV['SHELL'] } -c zip`

Or with system: system("bash", "-c", command)

However ruby (and all processes spawned by ruby) should inherit the parent processes' environment and thus have $PATH set correctly even when using another shell. Do you maybe run ruby from a cron job or init script or the like, where PATH is simply not set correctly?

like image 170
sepp2k Avatar answered Nov 15 '22 16:11

sepp2k


i'm guessing that Ruby uses the system() C function, which uses /bin/sh. In most systems, /bin/sh is just a symlink to other shell, so you can change to whatever you want

like image 39
Javier Avatar answered Nov 15 '22 18:11

Javier