Friday, September 14th, 2007...10:13 am
Capturing STDERR with ruby backticks
Jump to Comments
I was writing a ruby script today that ran a linux command for every line in a file. The problem was the backtick operator in ruby only captures stdout, here's a little trick to get stderr as well. (*nix only)
out = `ourlinuxcmd 2>&1`
the 2>&1 bit tells the shell to redirect stderr to stdout







7 Comments
January 29th, 2009 at 4:47 pm
found this useful, thanks!
March 6th, 2010 at 9:11 am
This is actually just what I was looking for. This backticks thing opens up a whole world of Ruby scripting that I didn't know was possible.
August 10th, 2011 at 12:13 pm
Just tried this on both OS X and Windows XP, worked on both.
August 23rd, 2011 at 2:48 am
I do believe all of the ideas you've offered on your post. They're very convincing and can certainly work. Still, the posts are very brief for starters. May you please prolong them a little from subsequent time? Thanks for the post.
September 7th, 2011 at 5:39 am
Genius means little more than the faculty of perceiving in an unhabitual way.
June 14th, 2012 at 11:48 am
This works great with %x[cmd 2>&1] too! Thanks.
May 22nd, 2013 at 1:44 pm
Thank you dude.!
Leave a Reply