[ruby] Ruby, Difference between exec, system and %x() or Backticks

What is the difference between the following Ruby methods?

exec, system and %x() or Backticks

I know they are used to execute terminal commands programmatically via Ruby, but I'd like to know why there are three different ways to do this.

This question is related to ruby exec

The answer is


They do different things. exec replaces the current process with the new process and never returns. system invokes another process and returns its exit value to the current process. Using backticks invokes another process and returns the output of that process to the current process.


Here's a flowchart based on this answer. See also, using script to emulate a terminal.

enter image description here