EDIT- einstein has just pointed out that it wasn't xxd adding it.. it was cmd, so (as I suspected.. xxd doesn't lie or add characters!).. But the rest of what I wrote addresses what you asked re what is xxd.
@agerman You're right, that is so strange.. When I | xxd it causes the problem
Code: Select all
C:\crp>(echo abc&&echo def)| xxd
0000000: 6162 6320 0d0a 6465 6620 0d0a abc ..def ..
C:\crp>(echo abc&&echo def)|xxd
0000000: 6162 6320 0d0a 6465 6620 0d0a abc ..def ..
Here(not piping to xxd), xxd reports it correctly
C:\crp>(echo abc&&echo def)>a.a
C:\crp>xxd a.a
0000000: 6162 630d 0a64 6566 0d0a abc..def..
As to what xxd is..
it's a program that reveals what is really in files.. I've no idea what happened there with | xxd but i'll give you examples of what xxd can do
Code: Select all
C:\blah>echo abc>a.a
C:\blah>type a.a
abc
C:\blah>xxd a.a
0000000: 6162 630d 0a abc..
C:\blah>
As above, you can use xxd to see that there is a new line after the abc
61 is hex for 'a', 0d0a is hex for the line separator that windows uses. \r\n character 13 then character 10
Suppose we open notepad and save a file as UTF-8 so Unicode as the mapping, but the particular encoding is 8 bits for characters whose unicode code is 8 bits like characters within ascii, but 16 or perhaps more, bits, for characters beyond ascii. (I think that's what UTF-8 is anyway). It's more sensible in terms of space, than UTF-16.
C:\blah>notepad.exe a.txt
write the text 'abc' in there and save it
cmd can't handle it well
If you want to see what is really inside the file
You can run these commands
Code: Select all
C:\blah>file a.txt
a.txt; UTF-8 Unicode (with BOM) text, with no line terminators
C:\blah>
C:\blah>xxd -p a.txt
efbbbf616263
below, each dot on the right is a character, represented by 2 nibbles(hex digits) s. ef bb bf 61 62 63 (61 62 63 are a b c)
http://www.asciitable.com/ <-- look in the hex column for 61 and 62 and 63, they're a,b,c
C:\blah>xxd a.txt
0000000: efbb bf61 6263 ...abc
C:\blah>
I never really learnt assembly language but many years ago I found out that the instruction MOV AX,3 can be encoded with certain characters, and so I typed the equivalent characters into notepad so that they'd get translated into the correct binary. But actually it'd have been much easier with xxd.
While you can't input binary into xxd, you can input hex.
Code: Select all
C:\blah>echo 616263| xxd -r -p
abc
C:\blah>
You could do echo 616263| xxd -r -p>a.txt
then open a.txt in notepad and see abc
you can add a new line
echo 6162630d0a| xxd -r -p >a.txt
A person could write an executable file in machine code with xxd! (if they knew assembly language that is!)
Suppose you have some characters in a file and you want to know what they are.. xxd shows you the truth.. what is in the file.
xxd seems to work fine within cygwin.. it's an oddity with xxd running natively within windows.. very strange
Code: Select all
C:\cygwin>cygwin<ENTER>
$ echo -e 'a\nb' | xxd -p
610a620a
$ exit
logout
Terminate batch job (Y/N)? y
C:\cygwin\bin>(echo a&& echo b)| xxd -p
61200d0a62200d0a
C:\cygwin\bin>
When you want to find out what is going on, what is really inside a file with just DOS commands or NT commands, you need some really extreme talent with FOR and Batch like you have..
The reason why I used xxd here on this forum, was just as a tool to see what was going on with the echo..
Little did I know, the native windows xxd seems to mess up when you | to it. That is very strange!
xxd is available in cygwin, or it is an EXE that bizarrely, comes with VIM 7(so one can download vim7 and get that exe). The 'file' command which I used here when describing xxd, (which identifies file types) is available from gnuwin32 or cygwin.