8

Following the post How can I write a null ASCII character (nul) to a file with a Windows batch script? there are indeed methods to write an ASCII NULL character (nul) to a file with a batch script.

However, I cannot find a way to store a NULL character to a variable.

Supposing the file null.nil contains a single NULL character, redirecting it into set /P does not seem work, the variable appears empty:

< "null.nil" set /P NULL=
set NULL

for /F does not appear to work either as it seems to dismiss every NULL character it encounters. Relying on the same file null.nil, the following results in an empty variable:

for /F %%S in ('type "null.nil"') do set NULL=%%S
set NULL

Changing the set syntax to set "NULL=%%S" or to set NULL=^%%S does not change anything, although the command line type "null.nil" does echo the NULL character (on my Windows 10 x64 machine; redirect type output to a file and view it with a hex. editor).

So how can I set a variable to a single NULL character? The NULL character can either be taken from a file (like null.nil) or generated by some smart hack, but I do not want it to be embedded in the batch file directly.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 4
    Just a guess... I'd suspect that internally env. variables are handled as null-terminated strings, and since a string containing nothing but a null char is empty (nothing as far as content), and setting an env. var to nothing clears it, you're going to have an issue here. :-) – Ken White Aug 04 '16 at 23:18
  • Unfortunately, you are right, @KenWhite; I just found a post here that confirms your reasonable suspicion: [Setting environment variables for a specific run of a specific process](http://stackoverflow.com/a/13092538) – aschipfl Aug 04 '16 at 23:30
  • If you somehow managed to `set a variable to a single NULL character` how do you foresee you could *use* that variable? – dxiv Aug 05 '16 at 03:36
  • I even tried to create a python 3 script and put a NULL char in an env. var, but child cmd wouldn't take it: undefined. So Ken White is probably right. It's like trying to put a null char in a C-string. – Jean-François Fabre Aug 05 '16 at 07:51
  • @dxiv, since environment variables turned out to be stored as null-terminated, it is of course not possible to read them; I was not aware about the storage format when I asked the question... – aschipfl Aug 05 '16 at 07:52
  • Why downvote? do you confuse a negative answer like "it is not possible" with downvoting? – aschipfl Aug 05 '16 at 07:54

1 Answers1

3

According to the page Environment Variables of Microsoft's MSDN Library and also to the post Setting environment variables for a specific run of a specific process, the environment block is stored in a NULL-terminated block of NULL-terminated strings -- here quickly illustrated in EBNF notation:

environment-block = { variable-definition } , null-char ;
variable-definition = variable-name , '=' , variable-value , null-char ;

Due to this storage format it is not possible to store a NULL character into an environment variable. Even if one managed to get a NULL written to the environment block it could not ever be read again.

Community
  • 1
  • 1
aschipfl
  • 33,626
  • 12
  • 54
  • 99