# Backtick with variables?

**URL:** <https://www.ruby-forum.com/t/backtick-with-variables/48954>\
**Category:** Ruby\
**Created:** [November 27, 2005, 7:58pm UTC](https://www.ruby-forum.com/t/backtick-with-variables/48954 "2005-11-27T19:58:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sardaukary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@Sardaukary](https://www.ruby-forum.com/u/Sardaukary)\
**Post date:** [November 27, 2005, 7:58pm UTC](https://www.ruby-forum.com/t/backtick-with-variables/48954/1 "2005-11-27T19:58:03Z")

</div>

I want to run an external command and capture the output.

hosts =`net view`

will store the results from running net view (XP command to list  
machines on network) in a string called hosts.

How do I execute a command stored in a string and capture its output  
though?

---

<div class="post-metadata">

**Author:** ![Sardaukary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c4cdca/32.png) [@Sardaukary](https://www.ruby-forum.com/u/Sardaukary)\
**Post date:** [November 27, 2005, 8:10pm UTC](https://www.ruby-forum.com/t/backtick-with-variables/48954/2 "2005-11-27T20:10:08Z")

</div>

Hi,

From: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I want to run an external command and capture the output.
> 
> hosts =`net view`
> 
> will store the results from running net view (XP command to list  
> machines on network) in a string called hosts.
> 
> How do I execute a command stored in a string and capture its output  
> though?

cmd = “net view”  
hosts = `#{cmd}`

There’s also popen() if you would prefer to read the output from a  
pipe.

Regards,

Bill
