Windows echo command can't echo a user-set variable
What did I do to开发者_运维技巧 screw up my CMD shell? Windows XP Pro, open a cmd window and do:
C:\>set tt = name
C:\>set tt
tt = name
C:\>echo %tt%
%tt%
C:\>echo %time%
14:13:28.67
The echo command doesn't work for some reason. I can echo the built-in variables just fine. Tried it on another computer and that works as expected
The set
command does not take spaces. The proper syntax would be:
set tt=name
What you have done in your example is set an environment variable tt<space>
. With that in mind, you can try this:
echo %tt %
and see your output.
The most upvoted answer here, accepted far ago, claims that:
"The
set
command does not take spaces."
But that is not correct: The %tt %
variable actually works: It can be set and referenced. (Despite it is confusing.)
Problem reproduced:
Indeed, on my Win7:
C:\>set os
OS=Windows_NT
C:\>set tt = name
C:\>set tt2= name
C:\>set tt3=name
C:\>set tt
tt = name
tt2= name
tt3=name
I tried and got:
C:\>echo "%os%"
"Windows_NT"
C:\>echo "%tt3%"
"name"
C:\>echo "%tt2%"
" name"
C:\>echo "%tt%"
"%tt%"
Resolved cases:
The intuitively expected variable %tt%
is not set. But %tt %
is set instead:
C:\>echo "%tt %"
" name"
Even more, with a space at the end of the value, set tt4 = name
:
C:\>echo "%tt4 %"
" name "
Conclusions:
The set
command does not trim()
:
- The space before "=" is included to the
var_name
. - The space after "=" is included to the
var_value
. - The space at the end of the
var_value
is included to it.
On the other hand:
- The space at the beginning of the
var_name
is not included to it, which is rather normal for command line arguments in general.
Have you tried setting the variable with no space between the equals? (set tt=name)
精彩评论