# Assigne command ouput to a variable

**URL:** <https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478>\
**Category:** Ruby\
**Created:** [November 7, 2007, 10:12am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478 "2007-11-07T10:12:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragoescu\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://www.ruby-forum.com/u/Dragoescu_Daniel)\
**Post date:** [November 7, 2007, 10:12am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478/1 "2007-11-07T10:12:29Z")

</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:** ![Dragoescu\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://www.ruby-forum.com/u/Dragoescu_Daniel)\
**Post date:** [November 7, 2007, 10:15am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478/2 "2007-11-07T10:15:24Z")

</div>

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

> 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:** ![Dragoescu\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://www.ruby-forum.com/u/Dragoescu_Daniel)\
**Post date:** [November 7, 2007, 10:37am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478/3 "2007-11-07T10:37:03Z")

</div>

Hello!

## It seems to work with:

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

Thank you for the answare!

---

<div class="post-metadata">

**Author:** ![Dragoescu\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://www.ruby-forum.com/u/Dragoescu_Daniel)\
**Post date:** [November 7, 2007, 10:19am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478/4 "2007-11-07T10:19:30Z")

</div>

Alle mercoledì 7 novembre 2007, Stefano C. ha scritto:

> > 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://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@Dragoescu\_Daniel](https://www.ruby-forum.com/u/Dragoescu_Daniel)\
**Post date:** [November 7, 2007, 10:52am UTC](https://www.ruby-forum.com/t/assigne-command-ouput-to-a-variable/119478/5 "2007-11-07T10:52:15Z")

</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

Kristian

On Wed, 7 Nov 2007 18:35:55 +0900, “Dragoescu Daniel”
