remix logo

Hacker Remix

Run System Commands in Ruby – When to use which methods to run shell commands

25 points by thunderbong 8 months ago | 8 comments

Joker_vD 8 months ago

I can't help but notice that all of those methods take a single command line string as a parameter. That's arguably fine on Windows, but not on Linux. So: how is that string broken down into the array of strings for the ultimate execve() call, in Ruby? Does Ruby parse it on its own (using some dialect of shell for quoting), or does it invoke the shell to do the heavy lifting, or?..

masa331 8 months ago

There is also a variant of 'system' which takes multiple agruments. It invokes the program in the first argument and passes the other arguments directly without going through shell so no globs etc. can take place. But this is not mentioned in the article but it is in Ruby docs

aquasync 8 months ago

While the article says that backticks don't provide access to the exit status, you can retrieve it with $?.exitstatus.

rererereferred 8 months ago

Lately I've been using Bun shell for writing scripts, it's a nice alternative for those who prefer JavaScript syntax over Ruby: https://bun.sh/docs/runtime/shell

8 months ago