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.

link|improve this question

80% accept rate
feedback

2 Answers

up vote 1 down vote accepted

set foo=hello&& echo test

works fine over here,

echo "%foo%"

prints

"hello"

:)

link|improve this answer
accepted, but now I'm curious how you would set a value ending with &&. Escaping with ^ doesn't seem to work. – wcoenen Oct 20 '09 at 11:12
I 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. – Phoshi Oct 20 '09 at 11:17
1  
And that is why people hate cmd.exe – grawity Oct 20 '09 at 13:01
1  
I 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. – Phoshi Oct 20 '09 at 14:25
@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. – Joey Apr 25 '11 at 8:51
feedback

You can do this

set "foo=hello" && echo test

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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