# How to capture output of linux command

**URL:** <https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600>\
**Category:** Ruby\
**Created:** [April 9, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600 "2010-04-09T13:33:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 1:33pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/1 "2010-04-09T13:33:41Z")

</div>

You can run linux command:

a = `ls /home`

and receive output in variable a.

But I would like /home to be variable. So I would invoke command like  
this:

a = `"ls #{dir}"`

This of course doesn’t work, but I hope you get the point.

a = system(“ls #{dir}”) works, but returns only true or false not the  
standard output of command.

How can I get standard output of the command and use variable as  
parameter?

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 1:42pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/2 "2010-04-09T13:42:13Z")

</div>

On Apr 9, 2010, at 04:33 , Damjan R. wrote:

> a = `"ls #{dir}"`
> 
> This of course doesn’t work, but I hope you get the point.

remove the double quotes

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 1:46pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/3 "2010-04-09T13:46:31Z")

</div>

Damjan R. wrote:

> You can run linux command:
> 
> a = `ls /home`
> 
> and receive output in variable a.
> 
> But I would like /home to be variable. So I would invoke command like  
> this:
> 
> a = `"ls #{dir}"`

As Ryan said, you need to remove the double quotes. You’re asking the  
shell to execute the “ls whatever” command. There’s no such command.

BTW, you actually want double quotes there, but put them around the  
parameter:

a = `ls "#{dir}"`

The purpose of the double quotes here is to protect against the case  
where the ‘dir’ variable contain spaces (which are meaningful to the  
shell --they separate tokens).

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 1:50pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/4 "2010-04-09T13:50:25Z")

</div>

Learn about Dir object. It has methods such as glob which should give  
you directory contents. Then you don’t have to care about stdout, nor  
quoting.

[http://whynotwiki.com/Comparison\_of\_Escape\_class\_and\_String.shell\_escape](http://whynotwiki.com/Comparison_of_Escape_class_and_String.shell_escape)

Have a look at the first example here

Marc

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/5 "2010-04-09T14:51:54Z")

</div>

Excerpts from Albert S.'s message of Fri Apr 09 13:46:34 +0200 2010:

> > 
> 
> The purpose of the double quotes here is to protect against the case  
> where the ‘dir’ variable contain spaces (which are meaningful to the  
> shell --they separate tokens).

If we got hat route you want ls – ${dir} to protect against directories  
called “-a” or such … And keep in mind that directories may contain  
slashes and such stuff on linux. eg try mkdir a\b  
thus using quotes is not enough. It may work for most cases you use  
though. Anyway I’d do it right - you never know.

Marc W.

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 2:42pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/6 "2010-04-09T14:42:32Z")

</div>

Marc W. wrote:

> Learn about Dir object. It has methods such as glob which should give  
> you directory contents. Then you don’t have to care about stdout, nor  
> quoting.
> 
> [http://whynotwiki.com/Comparison\_of\_Escape\_class\_and\_String.shell\_escape](http://whynotwiki.com/Comparison_of_Escape_class_and_String.shell_escape)
> 
> Have a look at the first example here
> 
> Marc

Thanks guys.

I used ls just as an example. I am actually calling smartctl command to  
find out SMART status of all disks on my computer.

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 9, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/7 "2010-04-09T18:55:56Z")

</div>

On Apr 9, 8:46 am, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## The purpose of the double quotes here is to protect against the case where the ‘dir’ variable contain spaces (which are meaningful to the shell --they separate tokens).
> 
> Posted viahttp://www.ruby-forum.com/.

Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.  
What if you (or someone) set dir = “~; rm -rf ~”? BAD.

So, using quotes is also safer.

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [April 12, 2010, 2:16am UTC](https://www.ruby-forum.com/t/how-to-capture-output-of-linux-command/186600/8 "2010-04-12T02:16:27Z")

</div>

Schneider wrote:

> On Apr 9, 8:46ï¿½am, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## The purpose of the double quotes here is to protect against the case where the ‘dir’ variable contain spaces (which are meaningful to the shell --they separate tokens).
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> Keep in mind that `ls #{dir}` (without the quotes) is also DANGEROUS.  
> What if you (or someone) set dir = “~; rm -rf ~”? BAD.
> 
> So, using quotes is also safer.

No, using quotes isn’t safer:

dir = ‘123" ; rm -rf ~ ; echo "’  
`ls "#{dir}"`

Results it doing ls “123” ; rm -rf ~ ; echo “”
