# Assigne command ouput to a variable

**URL:** <https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981>\
**Category:** ruby-talk\
**Created:** [7 November 2007 08:58 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981 "2007-11-07T08:58:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragoescu\_Daniel](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://rubytalk.org/u/Dragoescu_Daniel)\
**Post date:** [7 November 2007 08:58 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981/1 "2007-11-07T08:58:42Z")

</div>

Hello guys!

I am a newbie in ruby and i have a question. I run a system command and i  
want that the ouput of that command to be assigne to a variable, something  
like this:

var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or

var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1

but system return 0 or 1 if command is failed or succesful, and exec don't  
assigne the output to variable var1.

Can you help me? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [7 November 2007 09:14 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981/2 "2007-11-07T09:14:29Z")

</div>

Alle mercoledì 7 novembre 2007, Dragoescu Daniel ha scritto:

> Hello guys!
> 
> I am a newbie in ruby and i have a question. I run a system command and i  
> want that the ouput of that command to be assigne to a variable, something  
> like this:
> 
> var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or
> 
> var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1
> 
> but system return 0 or 1 if command is failed or succesful, and exec don't  
> assigne the output to variable var1.
> 
> Can you help me? Thanks in advance.

You can use backticks instead of system:

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [7 November 2007 09:17 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981/3 "2007-11-07T09:17:11Z")

</div>

Alle mercoledì 7 novembre 2007, Stefano Crocco ha scritto:

> Alle mercoledì 7 novembre 2007, Dragoescu Daniel ha scritto:  
> \> Hello guys!  
> \>  
> \> I am a newbie in ruby and i have a question. I run a system command and i  
> \> want that the ouput of that command to be assigne to a variable,  
> \> something like this:  
> \>  
> \> var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or  
> \>  
> \> var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1  
> \>  
> \> but system return 0 or 1 if command is failed or succesful, and exec  
> \> don't assigne the output to variable var1.  
> \>  
> \> Can you help me? Thanks in advance.
> 
> You can use backticks instead of system:

Sorry, I hit the send button by mistake.

You can use backticks instead of system:

var1 = `"du -s /mnt/hdd/ | cut -f1"`  
puts var1

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![Dragoescu\_Daniel](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://rubytalk.org/u/Dragoescu_Daniel)\
**Post date:** [7 November 2007 09:35 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981/4 "2007-11-07T09:35:55Z")

</div>

Hello!

It seems to work with:

> **···**
>
> ----  
> var1 = `du -s /mnt/hdd/ | cut -f1`  
> puts var1  
> ----  
> Thank you for the answare!
> 
> -----Original Message-----  
> From: Stefano Crocco [mailto:stefano.crocco@alice.it]  
> Sent: Wednesday, November 07, 2007 11:17 AM  
> To: ruby-talk ML  
> Subject: Re: Assigne command ouput to a variable
> 
> Alle mercoledì 7 novembre 2007, Stefano Crocco ha scritto:
> 
> > Alle mercoledì 7 novembre 2007, Dragoescu Daniel ha scritto:  
> > \> Hello guys!  
> > \>  
> > \> I am a newbie in ruby and i have a question. I run a system command  
> > \> and i want that the ouput of that command to be assigne to a  
> > \> variable, something like this:  
> > \>  
> > \> var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or  
> > \>  
> > \> var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1  
> > \>  
> > \> but system return 0 or 1 if command is failed or succesful, and exec  
> > \> don't assigne the output to variable var1.  
> > \>  
> > \> Can you help me? Thanks in advance.
> > 
> > You can use backticks instead of system:
> 
> Sorry, I hit the send button by mistake.
> 
> You can use backticks instead of system:
> 
> var1 = `"du -s /mnt/hdd/ | cut -f1"`  
> puts var1
> 
> I hope this helps
> 
> Stefano

---

<div class="post-metadata">

**Author:** ![Kristian\_Elof\_Sorens](https://avatars.discourse-cdn.com/v4/letter/k/4bbf92/32.png) [@Kristian\_Elof\_Sorens](https://rubytalk.org/u/Kristian_Elof_Sorens)\
**Post date:** [7 November 2007 09:51 UTC](https://rubytalk.org/t/assigne-command-ouput-to-a-variable/41981/5 "2007-11-07T09:51:41Z")

</div>

Hi

You can get both the output from the command and its return value:

var1 = `du -s /mnt/hdd/ | cut -f1`  
puts $?.exitstatus  
puts var1

&nbsp;&nbsp;Kristian

> **···**
>
> On Wed, 7 Nov 2007 18:35:55 +0900, "Dragoescu Daniel" \<daniel.dragoescu@axigen.com\> wrote:
> 
> > Hello!
> > 
> > It seems to work with:  
> > ----  
> > var1 = `du -s /mnt/hdd/ | cut -f1`  
> > puts var1  
> > ----  
> > Thank you for the answare!
> > 
> > -----Original Message-----  
> > From: Stefano Crocco [mailto:stefano.crocco@alice.it]  
> > Sent: Wednesday, November 07, 2007 11:17 AM  
> > To: ruby-talk ML  
> > Subject: Re: Assigne command ouput to a variable
> > 
> > Alle mercoledì 7 novembre 2007, Stefano Crocco ha scritto:
> > 
> > > Alle mercoledì 7 novembre 2007, Dragoescu Daniel ha scritto:  
> > > \> Hello guys!  
> > > \>  
> > > \> I am a newbie in ruby and i have a question. I run a system command  
> > > \> and i want that the ouput of that command to be assigne to a  
> > > \> variable, something like this:  
> > > \>  
> > > \> var1 = system("du -s /mnt/hdd/ | cut -f1") puts var1 or  
> > > \>  
> > > \> var1 = exec("du -s /mnt/hdd/ | cut -f1") puts var1  
> > > \>  
> > > \> but system return 0 or 1 if command is failed or succesful, and exec  
> > > \> don't assigne the output to variable var1.  
> > > \>  
> > > \> Can you help me? Thanks in advance.
> > > 
> > > You can use backticks instead of system:
> > 
> > Sorry, I hit the send button by mistake.
> > 
> > You can use backticks instead of system:
> > 
> > var1 = `"du -s /mnt/hdd/ | cut -f1"`  
> > puts var1
> > 
> > I hope this helps
> > 
> > Stefano
