Prevent extra space when setting variable on Windows command line
Solution 1
set foo=hello&& echo test
works fine over here,
echo "%foo%"
prints
"hello"
:)
Solution 2
You can do this
set "foo=hello" && echo test
Solution 3
As indicated by the other answers,
spaces at the end of a set
command are significant,
so if you type
set 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 echo
ing 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 say
echo 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.
Solution 4
set "foo=hello"
This helped me heaps in this line
FOR /f "tokens=2 delims=><" %%a IN ('TYPE %GCV_CONFIG_FILE% ^| FIND "<%GCV_VALUE_NAME%>"') DO SET "%GCV_VARIABLE_NAME%=%%a" && echo %GCV_VARIABLE_NAME%=%%a >> %SessionLOG%
Took me a while to figure out how to remove space the end of the variable. Hope this helps someone
Related videos on Youtube

Comments
-
BruceBerry 3 months
When setting a variable like this:
> set foo=hello && echo test
then the value of the variable foo contains an extra unwanted space:
> echo "%foo%" "hello "
How do I prevent this extra space? It disappears when I omit the
&& echo test
part, but I need to use&&
for other reasons. -
BruceBerry about 13 yearsaccepted, but now I'm curious how you would set a value ending with &&. Escaping with ^ doesn't seem to work.
-
UNK about 13 yearsI can't figure it out either! Escaping just makes the && carry on to when you USE the variable, but putting ""s around them makes it accept the string fine - pity it then has ""s around it.
-
user1686 about 13 yearsAnd that is why people hate
cmd.exe
-
UNK about 13 yearsI dunno, with cygwin and a bit - a lot - of scripting in python and c it's pretty workable. Rather have a windows-y bash, but what are you gonna do.
-
Joey over 11 years@wcoenen: Just use
set "foo=hello&&" in that case. Quoting helps sometimes, you know :-). Of course, when using the variable you still need to be careful to not let the
&` through unquoted. If you know how many parsing passes run over your variables you can also embed the escape characters directly. -
dgo about 8 yearsThis is the answer. With delayedexpansion you can even do:
set "foo=hello" && echo !foo!
and it will returnhello
. This was helpful. Thanks -
jvverde over 6 yearsYes, this should be the right answer
-
Vomit IT - Chunky Mess Style about 5 yearsIs this intended to be a comment or an answer?
-
waterjuice about 4 yearsThis is the answer I was hoping for. The currently accepted answer while works makes for much less readable .bat files