# System() on windows

**URL:** <https://www.ruby-forum.com/t/system-on-windows/98570>\
**Category:** Ruby\
**Created:** [July 16, 2006, 4:11am UTC](https://www.ruby-forum.com/t/system-on-windows/98570 "2006-07-16T04:11:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 4:11am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/1 "2006-07-16T04:11:48Z")

</div>

I want to capture the string that system(“hostname”) produces on windows  
and store it in a variable. system() returns true or false. How can I  
make it return the result (the hostname as a string) instead?

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 4:22am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/2 "2006-07-16T04:22:17Z")

</div>

On Jul 15, 2006, at 10:12 PM, brad tilley wrote:

> I want to capture the string that system(“hostname”) produces on  
> windows  
> and store it in a variable. system() returns true or false. How can I  
> make it return the result (the hostname as a string) instead?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Use backticks:

hostname = `hostname`.chomp

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 8:54am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/3 "2006-07-16T08:54:08Z")

</div>

Logan C. wrote:

> On Jul 15, 2006, at 10:12 PM, brad tilley wrote:
> 
> > I want to capture the string that system(“hostname”) produces on  
> > windows  
> > and store it in a variable. system() returns true or false. How can I  
> > make it return the result (the hostname as a string) instead?
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> Use backticks:
> 
> hostname = `hostname`.chomp

If you want to catch all output including both STDOUT and STDERR you  
should do following trick

output = `someprogram 2>&1`

So using OS pipes you redirect STDERR to STDOUT and then run application  
and get STDOUT using `` function.

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 9:37am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/4 "2006-07-16T09:37:01Z")

</div>

> If you want to catch all output including both STDOUT and STDERR you  
> should do following trick
> 
> output = `someprogram 2>&1`
> 
> So using OS pipes you redirect STDERR to STDOUT and then run application  
> and get STDOUT using `` function.

Except that the OP said he is on Windows, and unless he is using cygwin  
to get bash/sh style redirection, that won’t work. Starndard Error  
isn’t captured by any of the ruby IO/Exec processes that I can find.

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 9:45am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/5 "2006-07-16T09:45:08Z")

</div>

Steve M. wrote:

> > If you want to catch all output including both STDOUT and STDERR you  
> > should do following trick
> > 
> > output = `someprogram 2>&1`
> > 
> > So using OS pipes you redirect STDERR to STDOUT and then run application  
> > and get STDOUT using `` function.
> 
> Except that the OP said he is on Windows, and unless he is using cygwin  
> to get bash/sh style redirection, that won’t work. Starndard Error  
> isn’t captured by any of the ruby IO/Exec processes that I can find.

Did you really try my recipe on Windows???

File.open(‘a.rb’,‘w’){|f|  
f.write \<\<-END  
STDOUT.puts ‘standard output stream’  
STDERR.puts ‘error output stream’  
END  
}

out = `ruby a.rb 2>&1`

puts “HERE IT IS OUTPUT:”  
puts out

=====OUTPUT=====  
HERE IT IS OUTPUT:  
standard output stream  
error output stream

---

<div class="post-metadata">

**Author:** ![brad7a0](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/53a042/32.png) [@brad7a0](https://www.ruby-forum.com/u/brad7a0)\
**Post date:** [July 16, 2006, 10:32am UTC](https://www.ruby-forum.com/t/system-on-windows/98570/6 "2006-07-16T10:32:12Z")

</div>

I stand corrected. I did try it, but got it twisted up. Yet more  
evidence that Windows has moved closer to a Unix syntax for the command  
shell. Handy to know if I ever end up doing development on Windows.
