Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

set foo=hello&& echo test

works fine over here,

echo "%foo%"

prints

"hello"

:)

share|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

You can do this

set "foo=hello" && echo test

share|improve this answer
    
This is the answer. With delayedexpansion you can even do:set "foo=hello" && echo !foo! and it will return hello. This was helpful. Thanks –  user1167442 Oct 11 at 17:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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