SET strips leading spaces from variable names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

SET strips leading spaces from variable names

#1 Post by dbenham » 08 Jan 2014 12:29

Old news for me:
- SET allows spaces in variable name. Edit: For example: set name =value includes trailing space in the variable name

New discoveries for me:
- SET strips leading spaces from variable names.
- Other leading token delimiters like comma and semicolon are not stripped.
- Using SET to list variable definitions is odd when name starts with token delimiter like colon or semicolon.

Code: Select all

@echo on
@setlocal
set test
set "   test=leading spaces"
set "   test   2   =leading, middle, and trailing spaces"
set ",test=leading comma"
set ";test=leading semicolon"
set test
set ,test
set ;test
set ,|findstr /b ", ;"
set ;|findstr /b ", ;"
@echo ----------------------------------------------------
@echo %%   test%%=%   test%
@echo %%   test   2   %%=%   test   2   %
@echo %%test%%=%test%
@echo %%test   2   %%=%test   2   %
@echo %%,test%%=%,test%
@echo %%;test%%=%;test%
--OUTPUT--

Code: Select all

D:\test>set test
Environment variable test not defined

D:\test>set "   test=leading spaces"

D:\test>set "   test   2   =leading, middle, and trailing spaces"

D:\test>set ",test=leading comma"

D:\test>set ";test=leading semicolon"

D:\test>set test
test=leading spaces
test   2   =leading, middle, and trailing spaces

D:\test>set ,test
test=leading spaces
test   2   =leading, middle, and trailing spaces

D:\test>set ;test
test=leading spaces
test   2   =leading, middle, and trailing spaces

D:\test>set ,  | findstr /b ", ;"
,test=leading comma
;test=leading semicolon

D:\test>set ;  | findstr /b ", ;"
,test=leading comma
;test=leading semicolon
----------------------------------------------------
%   test%=
%   test   2   %=
%test%=leading spaces
%test   2   %=leading, middle, and trailing spaces
%,test%=leading comma
%;test%=leading semicolon


Dave Benham
Last edited by dbenham on 08 Jan 2014 20:59, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SET strips leading spaces from variable names

#2 Post by dbenham » 08 Jan 2014 20:07

I ran into the leading space stripping when working on sub strings extraction from multiline output.

At one point during development I had parsing code that was not stripping unwanted space, so I ended up with assignments that looked like

Code: Select all

set " name=value"
and I was surprised that the resulting variable did not have a leading space in the name.


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: SET strips leading spaces from variable names

#3 Post by foxidrive » 09 Jan 2014 03:04

Conversely you can have a space at the start of a filename, but not at the end.

Interesting, Dave.

Post Reply