2

When executing

echo 0 > test.txt

in cmd.exe, the output in test.txt is

0_

where _ means space. Why is there a space appended? Any way to prevent the appending of the space? Thanks!

2

1 Answer 1

8

The space that gets appended is the space between the "0" and the ">" in your command line. There is a non-obvious way to get rid of it: prepend the redirection to the command instead:

>test.txt echo 0
6
  • 1
    You can also just leave the space out (echo 0>test.txt) but your way is cuter. Mar 26, 2012 at 23:33
  • 1
    @HarryJohnston: Yes, you can, but did you try that command? Did you see the output? Single-digit numbers are special case when they are immediately followed by redirection. (They are treated as file handles.)
    – Andriy M
    Mar 27, 2012 at 6:08
  • @AndriyM: Very true. Good catch. Mar 27, 2012 at 20:14
  • @Jon: Works great thank you! Harry: I also tried that once, but like Andriy said - not working ;-) Mar 28, 2012 at 21:03
  • Hmm while this appends no space it appends a new line - any way to get rid of that? Apr 8, 2012 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.