Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 '14 at 17:34
    
Yes, this should be the right answer – jvverde Jul 1 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .