Forum: Ruby A quick Mac OSX question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
699c00ad35f2755810b4aa5f423d73e2?d=identicon&s=25 Albert Schlef (alby)
on 2009-01-09 12:11
Does mac osx supports the "2>&1" syntax to redirect stderr?

In other words, does the following line of code work as expected in mac
osx?

  output = `some_command 2>&1`
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2009-01-09 12:24
(Received via mailing list)
On Jan 9, 2009, at 6:11 AM, Albert Schlef wrote:

> Does mac osx supports the "2>&1" syntax to redirect stderr?
>
> In other words, does the following line of code work as expected in
> mac
> osx?
>
>  output = `some_command 2>&1`
> --
> Posted via http://www.ruby-forum.com/.

Yes:

ratdog:~ mike$ uname -a
Darwin ratdog.local 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24
17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386
ratdog:~ mike$ irb
 >> output = `perl -e 'die "Aaargh!"'`
Aaargh! at -e line 1.
=> ""
 >> output = `perl -e 'die "Aaargh!"' 2>&1`
=> "Aaargh! at -e line 1.\n"


--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-01-09 13:30
(Received via mailing list)
Albert Schlef <albertschlef@gmail.com> writes:

> Does mac osx supports the "2>&1" syntax to redirect stderr?
>
> In other words, does the following line of code work as expected in mac
> osx?
>
>   output = `some_command 2>&1`

As long as you use bash instead of tcsh, yes.

bash -c ' output = `some_command 2>&1` ; echo "$output" '

Notice than nowdays we can use $(some_command 2>&1) instead of the
backquotes, which is nice because you can nest them:

bash -c ' msg=$(printf "%dnd result = %s" $(( 1 * 2 * 3 * 7 )) "$(echo
"Do not panic")" ) ; echo "$msg" '
42nd result = Do not panic


Or use chsh to switch to bash:
http://www.macshadows.com/kb/index.php?title=Chang...
This topic is locked and can not be replied to.