1833031 Members
2402 Online
110049 Solutions
New Discussion

what is >&2?

 
SOLVED
Go to solution
SM_3
Super Advisor

what is >&2?

Hello

I know what 2> is
and 2>&1

But what is >2&

What would >2& do in

echo "Hello" >2&
10 REPLIES 10
RAC_1
Honored Contributor

Re: what is >&2?

redirect std out to std error.

So echo "Hello" >2&, would not produce any output.

Anil
There is no substitute to HARDWORK
Sridhar Bhaskarla
Honored Contributor
Solution

Re: what is >&2?

Hmm...

It will create a file called "2" running the echo command in background.

An & at the end will put the job in background.



-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Mel Burslan
Honored Contributor

Re: what is >&2?

this is more than like somebody fatfingering 2>&1 ...
As Sri indicated the command ending with >2& will send stdout to a file called 2 and do it in the background.
________________________________
UNIX because I majored in cryptology...
SM_3
Super Advisor

Re: what is >&2?

The Fundamentals of the Unix System (HP) has a few chapters explaining shell scripts and there are exercises to complete after each chapter.

In the solutions section for each chapter there is a script (mycp) that mimicks the cp command but a bit more comprehensive.
Anyway the script includes several >2&.

Here is the script.
Geoff Wild
Honored Contributor

Re: what is >&2?

Sridhar got it - it will create a file called 2 with the text Hello in it.

The & at the end means run in background...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
John Poff
Honored Contributor

Re: what is >&2?

Hi,

I tried it, and it looks like it just redirects standard output to standard error.

I used this command and got these results:

# fuser /sbin/sh >&2
/sbin/sh: 2018mt 4204mt


The output came back to the screen and it didn't try to run in background.

Now, if you wanted to redirect standard output to a file, and also redirect standard error to standard output (to that same file), you can do this:

# fuser /sbin/sh >/var/tmp/sh.txt 2>&1
# cat /var/tmp/sh.txt
/sbin/sh: 2018mt 4204mt


The 'fuser' command is a good one to try because the process IDs go to standard output and the flags go to standard error.

JP



John Poff
Honored Contributor

Re: what is >&2?

Never mind my previous post. I did '&2' instead of '2&'. My post was technically correct but functionally useless. ;)

JP
Fred Ruffet
Honored Contributor

Re: what is >&2?

This is probably a typo in your book as long as they make several echos in background to this "2" file using > and not >>...

Another point, is that it is strange for a book to use this when people more oftenly use 2>&1 than >&2, which seems to me more natural.

regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Mark Grant
Honored Contributor

Re: what is >&2?

It is worth pointing out that there are two other constructs and maybe you want to confirm that this isn't what the book is saying


&> file and >& filename are both equivalent to 2>&1
Never preceed any demonstration with anything more predictive than "watch this"
SM_3
Super Advisor

Re: what is >&2?

hello

I thought it was a typo but then I thought it can't be.

Well it looks like a typo.

thanks for the swift responses.