Create a gist now

Instantly share code, notes, and snippets.

Shell Execution in Ruby
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Returns the result of the shell command
# Docs:
value = `echo 'hi'` # or uglier but valid => Kernel.`("echo 'hi'")
value = `#{cmd}` # or uglier but valid => Kernel.`("#{cmd}")
# 2. Built-in syntax, %x( cmd )
# Following the ``x'' character is a delimiter, which can be any character.
# If the delimiter is one of the characters ``('', ``['', ``{'', or ``<'',
# the literal consists of the characters up to the matching closing delimiter,
# taking account of nested delimiter pairs. For all other delimiters, the
# literal comprises the characters up to the next occurrence of the
# delimiter character. String interpolation #{ ... } is allowed.
# Returns the result of the shell command, just like the backticks
# Docs:
value = %x( echo 'hi' )
value = %x[ #{cmd} ]
# 3. Kernel#system
# Executes the given command in a subshell
# Return: true if the command was found and ran successfully, false otherwise
# Docs:
wasGood = system( "echo 'hi'" )
wasGood = system( cmd )
# 4. Kernel#exec
# Replaces the current process by running the given external command.
# Return: none, the current process is replaced and never continues
# Docs:
exec( "echo 'hi'" )
exec( cmd ) # Note: this will never be reached beacuse of the line above
# Extra Advice
# $? which is the same as $CHILD_STATUS (if you require 'english')
# Accesses the status of the last system executed command if
# you use the backticks, system() or %x{}.
# You can then access the ``exitstatus'' and ``pid'' properties
# More Reading

awesome summary - thanks!


Liked the extra advice :)


Just Awsesome


Thank you!


Very useful , Thx!


Thanks, very helpful !


In case you need to drop the ball when the command fails,


thanks man!


I'd suggest you to check if you are having memory/latency issues when executing shell commands.


thanks man!


Awesome man!


Thanks man, very interesting and useful 👍


Awesome man! thanks


@JosephPecoraro thanks for sharing that! Any idea how to get rid of trailing "\n"?

$ cd /tmp
tmp$ irb
irb(main):001:0> `pwd`
=> "/tmp\n"
irb(main):002:0>  %x( pwd )
=> "/tmp\n"

One way to achieve this is:

=> "/tmp"

But I am wondering if there's nicer way to do it.


@mkowaliszyn The backticks just simple redirect the stdout to the result. With the default way of backticks it won't get rid of \n for you.

You can just call strip to trim the \n.



With more than 1 line result you can:


@mkows late reply, but yes! String#chomp is a convenient method that exists for just such cases! It removes all trailing newlines from the string:

irb(main):001:0> `pwd`
=> "/tmp\n"
irb(main):002:0> `pwd`.chomp
=> "/tmp"

Very similar to strip / rstrip mention above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment