OT: ^ in redirection (windows)

M

Miki Tebeka

Hello All,

Can someone explain me the difference between:
echo 1 > 1.txt 2>&1
and
echo 1 > 1.txt 2>^&1

(Windows XP "cmd" shell)

Both produce 1.txt with the content 1.

(Sadly, I don't know how to search for ^ in google).

Thanks.
--
------------------------------------------------------------------------
Miki Tebeka <[email protected]>
http://tebeka.bizhat.com
The only difference between children and adults is the price of the toys


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (Cygwin)

iD8DBQFCyS8N8jAdENsUuJsRAiiWAKCAAElD0dNIFCvkxz+Kg66+9yusmACfbvYr
Io0ZXyqgtXyZWT/8r94bAqs=
=uUnG
-----END PGP SIGNATURE-----
 
D

Duncan Booth

Miki said:
Can someone explain me the difference between:
echo 1 > 1.txt 2>&1
and
echo 1 > 1.txt 2>^&1

(Windows XP "cmd" shell)

Both produce 1.txt with the content 1.

(Sadly, I don't know how to search for ^ in google).

The first of these joins stderr to stdout, but since there is no output to
stderr has no visible effect.

The second should prevent special treatment of the & character, but in this
particular case actually has no effect.

You can see the effects more clearly if you redirect a handle which
actually does have some output:

stdout redirected to stderr, but stderr still goes to console so no visible
effect:

C:\temp>echo hi 1>&2
hi

stdout redirected to stderr, then stderr redirected to a file, but stdout
still points at original stderr so no visible effect:

C:\temp>echo hi 1>&2 2>x.txt
hi

stderr redirected to a file, then stdout redirected to same file. Output
goes in a file:
C:\temp>echo hi 2>x.txt 1>&2

C:\temp>type x.txt
hi

Same as above. Using ^ to avoid special interpretation of the & has no
effect:
C:\temp>echo hi 2>x.txt 1>^&2

C:\temp>type x.txt
hi
 
B

Bengt Richter

The first of these joins stderr to stdout, but since there is no output to
stderr has no visible effect.

The second should prevent special treatment of the & character, but in this
particular case actually has no effect.

You can see the effects more clearly if you redirect a handle which
actually does have some output:

stdout redirected to stderr, but stderr still goes to console so no visible
effect:

C:\temp>echo hi 1>&2
hi

stdout redirected to stderr, then stderr redirected to a file, but stdout
still points at original stderr so no visible effect:

C:\temp>echo hi 1>&2 2>x.txt
hi

stderr redirected to a file, then stdout redirected to same file. Output
goes in a file:
C:\temp>echo hi 2>x.txt 1>&2

C:\temp>type x.txt
hi

Same as above. Using ^ to avoid special interpretation of the & has no
effect:
C:\temp>echo hi 2>x.txt 1>^&2

C:\temp>type x.txt
hi
You'd think ^ would be mentioned in
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx
but it seems not to be.

Regards,
Bengt Richter
 
D

Duncan Booth

Bengt said:
You'd think ^ would be mentioned in
http://www.microsoft.com/resources/documentation/windows/xp/all/pro
ddocs/en-us/redirection.mspx
but it seems not to be.

You mean you didn't think to look at

http://www.microsoft.com/resources/.../all/proddocs/en-us/ntcmds_shelloverview.mspx
?

(or from Help & Support centre in Windows search on 'shell' to get to the same page).

I think the point is that the ^ escape isn't part of the redirection syntax,
it is only useful when you want to not redirect (or not have multiple commands on
one line), so it isn't really obvious where it should be documented. That said,
they have done a pretty good job of hiding it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top