How to set environment variables with spaces?

Multi tool use
How to set environment variables with spaces?
I need to set values to a environmental variable using a batch file. I wrote the script for this:
@echo off
set value="Hello world"
setx -M srijani "%srijani%;%value%"
It gives the error:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.
I googled and found that while using white spaces we need to write it inside a double quotes.
set value="Hello world"
But, that is not working too.
Note: I am on Windows 7.
%srijani%
%value%
Standard mistake often made is using
set variable="value"
instead of set "variable=value"
. See for example Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation of the difference on where first quote is specified on line with command SET.– Mofi
Dec 18 '15 at 10:57
set variable="value"
set "variable=value"
@Mofi - that's the answer in fact...
– npocmaka
Dec 18 '15 at 12:27
A great advantage of Mofi's suggestion is, that
set "variable=value"
does not include the surrounding quotes in the value, so you need to care about them only when expanding (reading) the value like "%variable%"
, so any code becomes more consistent in terms of quotation...– aschipfl
Dec 18 '15 at 20:56
set "variable=value"
"%variable%"
2 Answers
2
The error output by command setx is caused by wrong usage of the quotes on assigning the string to variable value
.
value
The command is set and the parameter is variable=value
. As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &(){}^=;!'+,`~
. Those characters are displayed on last help page output by running in a command prompt window cmd /?
or help cmd
.
variable=value
&(){}^=;!'+,`~
cmd /?
help cmd
But wrong is here:
set value="Hello world"
With first double quote after the equal sign the entire parameter variable=value
of command set is not enclosed in double quotes.
variable=value
This results in interpreting the double quotes as part of the string to assign to variable with name value
. Everything from the equal sign to end of line including the double quotes and possibly existing trailing spaces and horizontal tabs is assigned here to variable value
instead of just the string Hello world
as expected.
value
value
Hello world
On expanding the line
setx -M srijani "%srijani%;%value%"
the result is therefore:
setx -M srijani "Value of variable srijani;"Hello world""
And command setx interprets the wrong quoted parameter as syntax error.
Correct would be using:
set "value=Hello world"
Now the entire parameter of command set is enclosed in double quotes. Therefore ignored on parsing the line are:
So just Hello world
is assigned to a variable with name value
.
Hello world
value
For more details about correct assignment of a string to an environment variable read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains also a simple demo batch code.
Some more information:
How an argument string containing 1 or more quotes somewhere in the middle is interpreted depends on command respectively application. The behavior on interpreting an argument with 1 or more "
within an argument string can vary depending on used compiler as explained in an answer on batch file: list rar file in specific folder and write result into text file and of course the source code of the command / application.
"
For most commands and applications the correct syntax is:
command "parameter in quotes"
"Path to applicationapp.exe" "parameter in quotes"
But there are applications which require quotes in the middle of an argument string. An example of such an application is Windows Explorer.
The following syntax is required to open an Explorer window from within a batch file with current directory displayed in window.
explorer.exe /e,"%CD%"
Not working are:
explorer.exe "/e,%CD%"
explorer.exe /e "%CD%"
So explorer.exe
requires that the directory to open is specified after /e,
with quotes in the middle of parameter string or it interprets "/e,%CD%"
respectively "/e %CD%"
as name of the directory with path to display in Explorer window.
explorer.exe
/e,
"/e,%CD%"
"/e %CD%"
See also Windows Explorer Command-Line Options:
The Windows Explorer command line options documented on those Microsoft support articles and being all the same on all 3 articles work also for Explorer on Windows Server 2003, Windows Vista, Windows Server 2008 R2, Windows 7, Windows Server 2012 R2, Windows 8.0, Windows 8.1, Windows 10, in other words for all 32-bit and 64-bit Windows.
setx foo ""this env var has spaces and double quotes at each end""
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what are the values of
%srijani%
and%value%
variables.– npocmaka
Dec 18 '15 at 10:35