# Running Linux Command Through Ruby

**URL:** <https://www.ruby-forum.com/t/running-linux-command-through-ruby/129683>\
**Category:** Rails\
**Created:** [February 20, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/running-linux-command-through-ruby/129683 "2008-02-20T18:04:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 20, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/running-linux-command-through-ruby/129683/1 "2008-02-20T18:04:09Z")

</div>

I wonder if anyone knows how to run Unix/Linux commands from Ruby?

I understand that the Perl counterpart could be sth like the  
following.

- First I create a perl file example.pl

- Then inside the file I have the desired Linux commands.

#!/usr/local/bin/perl -w  
`ls -l`;  
`curl google.com`;

- When I run the file, those Linux commands would be executed.

Just wonder how I can do this in Ruby in Linux environment. Thanks a  
lot!

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 20, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/running-linux-command-through-ruby/129683/2 "2008-02-20T18:19:31Z")

</div>

On 20 Feb 2008, at 17:03, newbie wrote:

> #!/usr/local/bin/perl -w  
> `ls -l`;  
> `curl google.com`;
> 
> - When I run the file, those Linux commands would be executed.
> 
> Just wonder how I can do this in Ruby in Linux environment. Thanks a  
> lot!

exactly the same: by enclosing the command in backticks (`)  
you’ll get the output produced as the return value, and the exit code  
is in $?

Fred

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 20, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/running-linux-command-through-ruby/129683/3 "2008-02-20T19:39:19Z")

</div>

Very nice. Thanks!
