# Run UNIX command

**URL:** <https://www.ruby-forum.com/t/run-unix-command/114197>\
**Category:** Ruby\
**Created:** [September 9, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197 "2007-09-09T15:57:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 9, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/1 "2007-09-09T15:57:14Z")

</div>

Hi,

I’m looking for a way to execute an UNIX command from a ruby program.  
For instance, when the user selected a .tar.gz file, using the untar  
command to decompress it.  
How can I do that?

Thanks in advance!  
pieter.

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 9, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/2 "2007-09-09T16:29:24Z")

</div>

> For instance, when the user selected a .tar.gz file, using the untar  
> command to decompress it.  
> How can I do that?
> 
> ## Thanks in advance! pieter.
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

There are several ways to make system calls:

$ irb  
irb(main):001:0\> exec “echo Hi”  
Hi  
$

exec replaces the current process with the given external command

$ irb  
irb(main):001:0\> system “echo Hi”  
Hi  
=\> true

system runs the external command and returns true on success and false  
on  
failure

irb(main):002:0\> `echo Hi`  
=\> “Hi\n”  
irb(main):003:0\> %x{ echo Hi }  
=\> “Hi\n”  
irb(main):004:0\>

backticks and the %x literal execute the external command and return its  
output.

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 9, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/3 "2007-09-09T16:58:42Z")

</div>

> $ irb  
> irb(main):001:0\> system “echo Hi”  
> Hi  
> =\> true

Thank you!

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 9, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/4 "2007-09-09T16:30:25Z")

</div>

Pieter J. wrote:

> Hi,
> 
> I’m looking for a way to execute an UNIX command from a ruby program.  
> For instance, when the user selected a .tar.gz file, using the untar  
> command to decompress it.  
> How can I do that?
> 
> Thanks in advance!  
> pieter.

There’s a variety of ways to do that, depending on your needs. The  
simplest is to use the Kernel.system method:

system(“ls”)

If you need the stdout output from the command, use backticks:

output = `ls`

If you need more control, check out IO.popen.

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 11, 2007, 6:18am UTC](https://www.ruby-forum.com/t/run-unix-command/114197/5 "2007-09-11T06:18:27Z")

</div>

Since the question has been answered, I’d like to pipe in my own two  
cents.

Remember what you pass to system() will not work as expected if the  
system  
running it does not have it. For example, if you use an option specific  
to a  
GNU Program that is not supported by a given POSIX system, it won’t work  
right. If you use system( ‘gedit’ ) and the user does not have gedit in  
their  
path, well joy for your program and users.

Ruby has a large standard library that can do much of what the basic  
UNIX and  
DOS commands can do. You should really take a look at FileUtils or  
ftools in  
ri before you decide to do some thing that will only work on a specific  
system  
family (e.g. GNU, BSD, POSIX, DOS). Other wise, why bother with a  
language  
that provides such things out of the box… It’ll just create more work  
later  
if you ever want to use your Program on say, an OS/2 or Windows machine.

system() is better for things that are just wasteful to write yourself  
or to  
much bother to do yourself in my humble opinion.

An example of using command specific to GNU’s tar program with the tar  
implementation on my OpenBSD system.

Terry@vectra-$ touch foo bar  
Terry@vectra-$ tar -cf test1.tar foo bar  
Terry@vectra-$ ls  
bar foo test1.tar  
Terry@vectra-$ tar --create --file=test2.tar foo bar  
tar: unknown option – -  
usage: tar {crtux}[014578befHhLmOoPpqsvwXZz]  
[blocking-factor | archive | replstr] [-C directory] [-I file]  
[file …]  
tar {-crtux} [-014578eHhLmOoPpqvwXZz] [-b blocking-factor]  
[-C directory] [-f archive] [-I file] [-s replstr] [file …]

TerryP.

–

> **[Fortran](https://en.wikipedia.org/wiki/FORTRAN#FORTRAN_quotations)**
>
> Fortran (/ˈfɔːrtræn/; formerly FORTRAN) is a general-purpose, compiled imperative programming language that is especially suited to numeric computation and scientific computing.
> Fortran was originally developed by IBM in the 1950s for scientific and engineering applications, and subsequently came to dominate scientific computing. It has been in use for over six decades in computationally intensive areas such as numerical weather prediction, finite element analysis, computational fluid dynamic...

Note the listing about ‘pi’ 🙂

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 11, 2007, 6:49am UTC](https://www.ruby-forum.com/t/run-unix-command/114197/6 "2007-09-11T06:49:29Z")

</div>

Quoth Terry P. on Monday 10 September 2007 09:17:54 pm:

> if you ever want to use your Program on say, an OS/2 or Windows machine  
> … then you’re a masochist? Who uses OS/2 anymore? 😛

Cheers,

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 11, 2007, 9:11pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/7 "2007-09-11T21:11:37Z")

</div>

in terminal on a mac you just open it and type  
ruby filename.rb and it will run it  
(does not work in irb mode)

2007/9/10, Konrad M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![pieterjongsma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/977dab/32.png) [@pieterjongsma](https://www.ruby-forum.com/u/pieterjongsma)\
**Post date:** [September 11, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/run-unix-command/114197/8 "2007-09-11T21:53:49Z")

</div>

Pieter J. wrote:

> Hi,
> 
> I’m looking for a way to execute an UNIX command from a ruby program.  
> For instance, when the user selected a .tar.gz file, using the untar  
> command to decompress it.  
> How can I do that?
> 
> Thanks in advance!  
> pieter.

Use ``. An example:

irb(main):042:0\> `date`  
=\> “Ter Set 11 16:50:30 BRT 2007\n”

Regards,
