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
?
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.
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With