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.

Using SET to set an empty value to a variable works nicely in a Windows XP .CMD script. However, the same SET command behaves differently in the same CMD interpreter when used directly from command-line:

Trying to test it in a CMD.EXE command-line:

C:\>set Q=

C:\>echo %Q%
%Q%

C:\>echo "%Q%"
"%Q%"

C:\>set Q=/Q

C:\>echo %Q%
/Q

C:\>echo "%Q%"
"/Q"

Trying to test it with a .CMD script like this:

set Q=
echo %Q%
echo "%Q%"
set Q=/Q
echo %Q%
echo "%Q%"

Running it:

C:\>c.cmd

C:\>set Q=

C:\>echo
ECHO is on.

C:\>echo ""
""

C:\>set Q=/Q

C:\>echo /Q
/Q

C:\>echo "/Q"
"/Q"

What am I missing?

Is that a nice joke from Redmond, or is there any sane explanation for this difference?

How am I supposed to test lines from a .CMD script when they behave differently when used in a command-line?

How do I properly set a variable to an empty string value in a CMD command-line and how do I reference that variable so that I get that empty string?

share|improve this question
    
Assigning empty string to variable deletes it. I am wondering if set changed its behaviour or the echo did since Windows XP. Is Q variable listed when you run set? –  Vlastimil Ovčáčík Dec 22 '13 at 15:02
    
@VlastimilOvčáčík: I am talking about Windows XP. SET deletes the variable when issued from the command line. It does not delete the variable when used in a .CMD file. –  Laszlo Valko Dec 22 '13 at 18:56
    
No, my point is that the set Q= command deletes the variable in both scenarios. –  Vlastimil Ovčáčík Dec 22 '13 at 21:37

2 Answers 2

tl;dr - Problem you got there is that echo %Q% expands to echo. Use echo.%Q%

Expanded Answer

echo command got 4 behaviors:

  • echo on - enables echoing commands.
  • echo off - disables echoing commands.
  • echo - shows state of echoing commands option.
  • echo ... - puts ... and newline on the screen.

If you pass variable to echo as an argument, and it's empty, it will expand to echo, and will show something like "ECHO is off."

Many people use echo. command to display empty string (read: output newline), but not everybody knows that echo. can be used to excplicitly specify that you want output behavior. e.g.:

  • echo.on - will output on and newline
  • echo.off - will output off and newline
  • echo. - will output newline
  • echo.something - will output "omething and newline
  • echo.%Q% - will output contents of %Q% whether or not it's ""/"on"/"off" or whatever else.

Keep in mind that there should not be space between . and arguments.

share|improve this answer

@Laszlo Valko: You don't say which CP you use, but I would "fake" the empty variable by using Alt-255, which is an "invisible" character, at least in CP 437. Using your example:

@echo off
::The line below contains the invisible character (Alt-255)
echo Q set to "Invisible"
set Q= 
set Q
echo %Q%
echo "%Q%"
echo Q set to Empty
set Q=
set Q
echo %Q%
echo "%Q%"
echo Q set to "/Q"
set Q=/Q
set Q
echo %Q%
echo "%Q%"

Using SET Q will show you all the variables that start with Q. Try SET C and you'll see what I mean. As you can see, when the variable is set to blank, it is deleted. When you do an ECHO %Q% the command interpreter thinks you're just saying ECHO which then it tells you the state: On or Off. Since I used @echo off at the beginning of the script, it reports that echo is Off.

Hope that helps!

share|improve this answer

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.