This is just a reference for legacy code. For new code, always use
Returns the standard output of running the given command in a subshell. This is an alias for `...`, and you can use string interpolation.
name = 'ls'
It does not escape anything you inject in the string.
If you want to find out if the call was successful you can ask
exec, but executes the given command in a subshell. Your script will go on. Returns:
trueif the command gives zero exit status
falsefor non zero exit status
if system 'cp', '/full/path/to/my_file', '/target/directory'
puts "You made it!"
puts "Something went wrong"
system (as well as
exec) has two distinct argument patterns:
a single string argument is passed to the shell like "Hey, lets pretend the user typed this. Please run it!"
>> system 'echo *'
This is equivalent to:
$ echo *
Note how the asterisk is interpreted by the shell, as would any other characters (like a semicolon which separates commands).
Output will be something like:
file1 file2 file3
when passed multiple arguments, Ruby will take the first as command and find it on the $PATH. It then invokes it, passing the remaining arguments as Strings:
system 'echo', '*'
This is equivalent to:
$ /bin/echo '*'
Since Ruby is passing all arguments as Strings, there will be no wildcard expansion or the like.
Output will be:
You cannot freely mix these styles, and
system will fail when passing it invalid arguments:
system 'bundle exec rails server', '-p 3000' # fails and returns nil
This is equivalent to running a command called "
bundle exec rails" (including spaces in its filename). There is usually no such command anywhere on the
Note that you should prefer the 2nd approach (list of arguments instead of putting them into a single command) unless you absolutely know what you are doing.
Replaces the current process. Take care: when an
exec command exits, it exits your script as well; i.e.
exec will be the last statement executed in your Ruby script. Output is written to
SystemCallError if the command couldn‘t execute.
puts "Creating directory..."
exec 'mkdir', 'new_directory'
# the code here and below will never be read
Takes the same parameters as
[env,] command [,options]
Optional hash with
:name => environment_name, e.g.
spawn(:name => 'development', 'echo hello world')
exec('cat * | grep denied')
exec *[ 'git', 'add', 'Gemfile' ]
exec ['ls', 'foo'], 'foo.bar', 'baz.bar'
The shell used is
/bin/sh on Unix-like systems,
ENV["COMSPEC"] on Windows NT series, and similar.