Kernel#system
system([env,] command [, args...,]) -> true, false, or nil Returns:
true, false, or nil · Updated March 13, 2026 · Kernel Methods shell subprocess execution io
The system method executes an external command as a subprocess and returns its exit status. It runs the command directly—without spawning a shell interpreter—making it more secure than using backticks or %x{} for commands with untrusted input. The method blocks the current thread until the command completes, then returns a value indicating success or failure.
Unlike backticks, system does not capture the command’s stdout or stderr. The output goes directly to the parent’s stdout/stderr, which is useful when you want the subprocess output to appear in the terminal without interception.
Syntax
system(command)
system(command, arg1, arg2, ...)
system(env_var: value, command, arg1, ...)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
env | Hash | nil | Optional environment variables to set for the subprocess |
command | String | — | The command to execute. Can include spaces without shell interpretation |
args | Array | nil | Additional arguments passed as separate parameters to the command |
Examples
Basic usage
# Returns true if command succeeds (exit code 0)
system("ls")
# => true
# Returns false if command fails (non-zero exit code)
system("ls /nonexistent")
# => false
# Returns nil if command was not found
system("nonexistent_command")
# => nil
Passing arguments safely
# Arguments are passed directly to the executable, not interpreted by a shell
# This avoids shell injection vulnerabilities
system("echo", "Hello", "World")
# => true
# Terminal output: Hello World
# Compare with shell interpretation (dangerous with user input)
system("echo $HOME") # Shell expands $HOME
system("echo", "$HOME") # Prints literal "$HOME"
Setting environment variables
# Set custom environment variables for just this subprocess
system({ "RAILS_ENV" => "test" }, "rake", "test")
# => true
# The parent process's RAILS_ENV is unchanged
Common Patterns
Running a script and checking success
if system("ruby script.rb")
puts "Script succeeded"
else
puts "Script failed with exit code: #{$?.exitstatus}"
end
Checking the exit status
system("rake", "db:migrate")
case $?.exitstatus
when 0 then puts "Success"
when 1 then puts "Failed"
when 2 then puts "Runtime error"
end
Differences from similar methods
| Method | Captures Output | Shell Interpretation | Returns |
|---|---|---|---|
system | No | No | true/false/nil |
` ` | Yes | Yes | String |
%x{} | Yes | Yes | String |
exec | No | No | Does not return |
Errors
- Errno::ENOENT: Raised if the command executable is not found or not executable.
- ArgumentError: Raised if no command is provided.