Classic ways to execute shell code in Ruby

suggest change

Exec: exec replaces the current process, so Ruby code after a successful exec does not run.

exec 'echo "hello world"'

or

exec ('echo "hello world"')

The System Command:

system 'echo "hello world"'

Will output “hello world” in the command window.

or

system ('echo "hello world"')

system returns true if the command exits with status 0, false if it exits with another status, and nil if the command cannot be started.

result = system 'echo "hello world"'
puts result  # prints true

The backticks (`):

`echo "hello world"` prints hello world in the command window and returns that output as a string.

You can also catch the result.

result = `echo "hello world"`  
puts "We always code a " + result

IO.popen:

# Will get and return the current date from the system
IO.popen("date") { |f| puts f.gets }

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents