Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are valid characters for Windows environment variable names and values?

Tags:

After some reseach, I found out that length limits for names are 255 and for values 32767 characters.

But which characters are allowed for names?
And which characters are allowed for values?

like image 492
joe Avatar asked Dec 17 '13 13:12

joe


People also ask

What characters are allowed in environment variables?

Only uppercase letters, lowercase letters, and underscores from this character set are allowed.

Which symbol is used with environment variable name?

Most Vector environment variables are set in the Vector symbol table (symbol. tbl) and are visible only with the user command ingprenv.

What are Windows environment variables?

An environment variable is a dynamic "object" on a computer, containing an editable value, which may be used by one or more software programs in Windows. Environment variables help programs know what directory to install files in, where to store temporary files, and where to find user profile settings.

What is the correct convention for an environment variable name?

The names of environment variables are case-sensitive; by convention they are uppercase. A shell variable is like an environment variable, except that it is not exported to new programs started from that shell.


2 Answers

About variable values: you can use most characters as variable values, including white space. If you use the special characters <, >, |, &, or ^, you must precede them with the escape character (^) or quotation marks. If you use quotation marks, they are included as part of the value because everything following the equal sign is taken as the value.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true

Check section "Setting environment variables".

About variable names: in my opinion, for best compatibility with every application, you should limit yourself to letters, numbers, underscore (_) and minus (-).

I'm quite sure that all POSIX valid characters for files are ok, but I did not found any evidence of this.


Concerning variable names names we need to also accept parenthesis since %ProgramFiles(x86)% is a well-known envar. From my experiments it seems that in addition to letters and digits characters, these characters are valid _(){}[]$*+-\/"#',;.@!? and these characters are not valid %<>^&|=:.

I didn't do an exhaustive search but just tested most common non alphanumeric characters.

And just for the fun of it you can name an envar %_(){}[]$*+-\/"#',;.@!?%:

C:\>set _(){}[]$*+-\/"#',;.@!?=xyz  C:\>echo %_(){}[]$*+-\/"#',;.@!?% xyz 
like image 114
Ghigo Avatar answered Sep 25 '22 13:09

Ghigo


It seems like <>^&| are also valid characters, as long as they are properly escaped:

C:\>set ^<^>^^^&^|=xyz  C:\>echo %<>^&|% xyz 
like image 36
user368683 Avatar answered Sep 21 '22 13:09

user368683