31

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

Franta
  • 986
  • 10
  • 17
jacobsee
  • 1,438
  • 4
  • 18
  • 34
  • 1
    Minor nitpick: `%time%` isn't an environment variable but rather a pseudo-variable that is dynamically evaluated. – Joey Dec 11 '09 at 00:20
  • Note that the reason `set tt` works to display the value of the variable is that `set var` displays all variables **beginning with** `var`. You may notice the space between `tt` and `=` in the system output. Also, the value of the variable includes the initial space. For example `echo last%tt %` would output `last name`, which could be what is desired. – GlennFromIowa Jul 10 '18 at 23:18

3 Answers3

73

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.

Gökhan Mete ERTÜRK
  • 3,378
  • 2
  • 19
  • 23
akf
  • 38,619
  • 8
  • 86
  • 96
10

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.
Franta
  • 986
  • 10
  • 17
  • Welcome to Stack Overflow, please [take the tour](https://stackoverflow.com/tour), make sure you read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) and update your answer with more information. – lordrhodos Jun 15 '17 at 07:56
  • This is simply a (wordy) duplicate of the accepted answer (from over seven years ago). – SiHa Jun 15 '17 at 12:17
  • I do not agree, that the question was answered, not fully: There is said that %tt % is wrong... But it works, it *can* be used. So now it is shown here, for every one. – Franta Jun 15 '17 at 15:32
  • 3
    The accepted answer showed an alternative, working syntax; however, the initial statement, "The `set` command does not take spaces" is incorrect. This answer (despite its wordiness) explains the nuances of how the `set` command actually works regarding spaces. – GlennFromIowa Jul 10 '18 at 23:12
6

Have you tried setting the variable with no space between the equals? (set tt=name)

Eric
  • 2,202
  • 2
  • 26
  • 30