Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#system

system([env,] command... [,options], exception: false) → true, false or nil

Executes command… in a subshell. command… is one of following forms.

commandline

command line string which is passed to the standard shell

cmdname, arg1, ...

command name and one or more arguments (no shell)

[cmdname, argv0], arg1, ...

command name, argv[0] and zero or more arguments (no shell)

system returns true if the command gives zero exit status, false for non zero exit status. Returns nil if command execution fails. An error status is available in $?.

If the exception: true argument is passed, the method raises an exception instead of returning false or nil.

The arguments are processed in the same way as for Kernel#spawn.

The hash arguments, env and options, are same as exec and spawn. See Kernel#spawn for details.

Example
system("echo *")
system("echo", "*")

produces:

config.h main.rb
*

Error handling:

Example
system("cat nonexistent.txt")
# => false
system("catt nonexistent.txt")
# => nil

system("cat nonexistent.txt", exception: true)
# RuntimeError (Command failed with exit 1: cat)
system("catt nonexistent.txt", exception: true)
# Errno::ENOENT (No such file or directory - catt)

See Kernel#exec for the standard shell.

See also #syscall #tap #sub #test also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1