# System Command not passing output to a value

**URL:** <https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912>\
**Category:** Ruby\
**Created:** [December 5, 2013, 6:12pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912 "2013-12-05T18:12:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [December 5, 2013, 6:12pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/1 "2013-12-05T18:12:18Z")

</div>

Hello,

I’m trying to write a script that will take the output of a system  
command and assign it to a variable whether it be a string, or array for  
later processing. The following block:

irb(main):002:0\> location = ‘C:\python\python332’  
=\> “C:\python\python332”  
irb(main):003:0\> location = location.gsub(’\’, ‘/’)  
=\> “C:/python/python332”  
irb(main):004:0\> value = `#{"#{location}/python -V"}`  
Python 3.3.2  
=\> “”  
irb(main):005:0\> puts value

=\> nil  
irb(main):006:0\> value = `#{"ruby -v"}`  
=\> “ruby 1.9.3p448 (2013-06-27) [i386-mingw32]\n”  
irb(main):007:0\> puts value  
ruby 1.9.3p448 (2013-06-27) [i386-mingw32]  
=\> nil

is what I am receiving. ruby -v works, yet python -V does not, and  
assigns nil. What would I be doing wrong in this instance?

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 5, 2013, 6:21pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/2 "2013-12-05T18:21:02Z")

</div>

On Thu, Dec 5, 2013 at 6:12 PM, Philip D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):004:0\> value = `#{"#{location}/python -V"}`  
> You cannot nest string interpolation. Try

value = `"#{location}/python" -V`

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 5, 2013, 6:21pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/3 "2013-12-05T18:21:16Z")

</div>

From the IRB responses, it looks like the command “python -V” is  
printing the output and returning an empty string, I don’t think there’s  
anything wrong with your code, it’s how “python -V” responds.

Of course, someone else may have a better understanding of this.

Edit: Nice catch as usual, Robert! I thought that command was getting  
through, since you can see the response given: “Python 3.3.2”

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 5, 2013, 7:56pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/4 "2013-12-05T19:56:51Z")

</div>

On 12/05/2013 09:20 AM, Robert K. wrote:

> > =\> “C:/python/python332”

Certainly you can nest string interpolation, although it’s silly in this  
case (and most cases I’ve seen):

2.0.0p247 :001 \> command = “ruby”  
=\> “ruby”  
2.0.0p247 :002 \> `#{"#{command} -v"}`  
=\> “ruby 2.0.0p247 (2013-06-27 revision 41674) [x86\_64-linux]\n”

In any case, OP’s issue is that “ruby -v” outputs to stdout, and “python  
-V” outputs to stderr which is not captured by backticks:

$ ruby -v \> /dev/null  
$ python -V \> /dev/null  
Python 2.7.5  
$ python -V &\> /dev/null  
$

A simple way to deal with this would be to use Open3:

$ irb  
2.0.0p247 :001 \> require ‘open3’  
=\> true  
2.0.0p247 :002 \> Open3.capture2e(“ruby -v”)  
=\> [“ruby 2.0.0p247 (2013-06-27 revision 41674) [x86\_64-linux]\n”,  
#\<Process::Status: pid 16154 exit 0\>]  
2.0.0p247 :003 \> Open3.capture2e(“python -V”)  
=\> [“Python 2.7.5\n”, #\<Process::Status: pid 16167 exit 0\>]

-Justin

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 6, 2013, 12:29am UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/5 "2013-12-06T00:29:43Z")

</div>

On 12/05/2013 11:17 AM, Philip D. wrote:

> Thank you Justin, yes, I use the nested interpolation to help with the  
> commands and save code, but I will look into the Open3 and see what I  
> can do with that. Thank you for the help. I’ll post back once I figure  
> out what I need to do.

Just to be totally explicit, these are equivalent:

`#{"ruby -v"}` is the same as `ruby -v`  
`#{"#{location}/python -V"}` is the same as `#{location}/python -V`

-Justin

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 5, 2013, 8:17pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/6 "2013-12-05T20:17:03Z")

</div>

Joel P. wrote in post #1129747:

> From the IRB responses, it looks like the command “python -V” is  
> printing the output and returning an empty string, I don’t think there’s  
> anything wrong with your code, it’s how “python -V” responds.
> 
> Of course, someone else may have a better understanding of this.
> 
> Edit: Nice catch as usual, Robert! I thought that command was getting  
> through, since you can see the response given: “Python 3.3.2”

Thank you for that information, it definitely sheds some light on that  
whole bit. I wouldn’t have known what to search for to find that out.

Justin C. wrote in post #1129754:

> On 12/05/2013 09:20 AM, Robert K. wrote:
> 
> > > =\> “C:/python/python332”
> 
> Certainly you can nest string interpolation, although it’s silly in this  
> case (and most cases I’ve seen):
> 
> 2.0.0p247 :001 \> command = “ruby”  
> =\> “ruby”  
> 2.0.0p247 :002 \> `#{"#{command} -v"}`  
> =\> “ruby 2.0.0p247 (2013-06-27 revision 41674) [x86\_64-linux]\n”
> 
> In any case, OP’s issue is that “ruby -v” outputs to stdout, and “python  
> -V” outputs to stderr which is not captured by backticks:
> 
> $ ruby -v \> /dev/null  
> $ python -V \> /dev/null  
> Python 2.7.5  
> $ python -V &\> /dev/null  
> $
> 
> A simple way to deal with this would be to use Open3:
> 
> $ irb  
> 2.0.0p247 :001 \> require ‘open3’  
> =\> true  
> 2.0.0p247 :002 \> Open3.capture2e(“ruby -v”)  
> =\> [“ruby 2.0.0p247 (2013-06-27 revision 41674) [x86\_64-linux]\n”,  
> #\<Process::Status: pid 16154 exit 0\>]  
> 2.0.0p247 :003 \> Open3.capture2e(“python -V”)  
> =\> [“Python 2.7.5\n”, #\<Process::Status: pid 16167 exit 0\>]
> 
> -Justin

Thank you Justin, yes, I use the nested interpolation to help with the  
commands and save code, but I will look into the Open3 and see what I  
can do with that. Thank you for the help. I’ll post back once I figure  
out what I need to do. I’m also on Windows, I’m not sure how I’d test  
the StdOut, and StdErr with Command Prompt.

---

<div class="post-metadata">

**Author:** ![railskater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3ab097/32.png) [@railskater](https://www.ruby-forum.com/u/railskater)\
**Post date:** [December 6, 2013, 3:11pm UTC](https://www.ruby-forum.com/t/system-command-not-passing-output-to-a-value/237912/7 "2013-12-06T15:11:32Z")

</div>

On Thu, Dec 5, 2013 at 7:54 PM, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 12/05/2013 09:20 AM, Robert K. wrote:
> 
> > On Thu, Dec 5, 2013 at 6:12 PM, Philip D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > 
> > 
> > You cannot nest string interpolation. Try

> Certainly you can nest string interpolation, although it’s silly in this case  
> (and most cases I’ve seen):

Thank you for correcting me! I was too quickly glancing over the  
expression and apparently overlooked the double quotes. And, yes, I  
agree: if nesting is necessary than string interpolation is probably  
not the best approach.

Cheers

robert
