Prevent extra space when setting variable on Windows command line

19,937

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 test
or 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 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

Share:
19,937

Related videos on Youtube

BruceBerry
Author by

BruceBerry

You can contact me at [email protected]

Updated on September 17, 2022

Comments

  • BruceBerry
    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
    BruceBerry about 13 years
    accepted, but now I'm curious how you would set a value ending with &&. Escaping with ^ doesn't seem to work.
  • UNK
    UNK about 13 years
    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.
  • user1686
    user1686 about 13 years
    And that is why people hate cmd.exe
  • UNK
    UNK about 13 years
    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.
  • Joey
    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
    dgo about 8 years
    This is the answer. With delayedexpansion you can even do:set "foo=hello" && echo !foo! and it will return hello. This was helpful. Thanks
  • jvverde
    jvverde over 6 years
    Yes, this should be the right answer
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style about 5 years
    Is this intended to be a comment or an answer?
  • waterjuice
    waterjuice about 4 years
    This is the answer I was hoping for. The currently accepted answer while works makes for much less readable .bat files