How to create a variable that contains only a SPACE character?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Arianax
Posts: 2
Joined: 06 Apr 2020 14:38

How to create a variable that contains only a SPACE character?

#1 Post by Arianax » 16 May 2020 13:37

Hi all,

How do I create a string variable that contains only a single SPACE character?

Code: Select all

SET Variable= 
The above doesn't seem to do anything and the variable returns 'ECHO is OFF' when called with 'ECHO !Variable!'.

Do I need to use %Variable% instead?

Appreciated.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to create a variable that contains only a SPACE character?

#2 Post by penpen » 17 May 2020 03:04

Although i prefer usually encapsulating the variable assignemnt in deoblequotes (needs extensions to be enabled, which should be the default on most systems), what you did above is indeed a valid way of creating an environment variable with a single space character.
The echo-command just doesn't like spaces only lines (including an empty line) and messes up the output. You could prove that by replacing the space character with another character that 'echo' has no issues with.
However one workaround were, you could use a round opening bracket for the delimiter between the echo command and its arguments:

Code: Select all

@echo off
setlocal enableExtensions

SET Variable= 
set "Variable2= "
echo %Variable% %Variable2%
echo %Variable: =#% %Variable2: =;%
echo(%Variable% %Variable2%

goto :eof
penpen

Post Reply