Home » Questions » Computers [ Ask a new question ]

Prevent extra space when setting variable on Windows command line

Prevent extra space when setting variable on Windows command line

When setting a variable like this:

Asked by: Guest | Views: 76
Total answers/comments: 2
Guest [Entry]

"set foo=hello&& echo test

works fine over here,

echo ""%foo%""

prints

""hello""

:)"
Guest [Entry]

"As indicated by the other answers,
spaces at the end of a set command are significant,
so if you typeset foo=hello & echo testor even

set foo=hello␣

(where ␣ represents a space),
%foo% gets set to the six-character string h, e, l, l, o,  .

I had a similar problem with space when echoing a string to an output file. 
Not surprisingly (in light of the above),

echo hello >myfile

writes the six-character string h, e, l, l, o,    (plus CR and LF)
to the file, while

echo hello> myfile

does not include the space. 
But spaces at the end of the command line are still a problem. 
If, for some reason, you sayecho hello>myfile␣
then you get a space at the end of the output. 
When I removed the space after the filename,
the space at the end of the output disappeared.

Weird, but this worked for me."