Daniel Vandersluis
Daniel Vandersluis

Reputation: 94284

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

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?

Upvotes: 10

Views: 6707

Answers (3)

Corban Brook
Corban Brook

Reputation: 21378

Why not just specify the full path the the zip executable.

Upvotes: 0

sepp2k
sepp2k

Reputation: 370435

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?

Upvotes: 10

Javier
Javier

Reputation: 62671

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

Upvotes: 4

Related Questions