Reputation: 666
I need a system variable and assign an empty string value to it. I've tried
setx samplepassword ""
But upon echo %samplepassword%
, what should be an empty string is instead %samplepassword%
. Now I found this in the docs
Setting value of "" (empty quotes) will appear to delete the variable - it's not shown by SET but the variable name will remain in the registry.
How can I assign an empty system variable using setx?
Update:
I have checked the environment variables via the Windows GUI: Control Panel | System | Advanced | Environment Variables
and saw that the DB_PASSWORD
was indeed initialized with an empty string value. But why does executing echo %samplepassword%
give me the above output? I have opened a new instance of cmd to be precise.
Upvotes: 6
Views: 7554
Reputation: 6607
Worth pointing out that Windows does not support blank/empty values for environment values.
Try and define a new environment variable from Settings > System > About > Advanced System Settings > Environment Variables
. If no value is entered (see image below) then the OK
button is disabled.
Or try and create the environment variable in code (see C# example below) with a blank (or null
) value...and the environment variable is deleted!
// If not existing, the environment variable does NOT get defined
// If it exists, the environment variable is deleted
Environment.SetEnvironmentVariable(
"TestBlank",
string.Empty, // Or "null"
EnvironmentVariableTarget.User);
The minimum length of environment variable values is 1. The maximum length is (2^16 - 2) = 32766 (or Int16.MaxValue - 1
in C#). You must do this in code, though, as the Windows GUI limits you to 2047 characters. Why these limits? Not sure, probably historical (aka "lost in the mists of time").
So if you use set
or setx
with a blank value, you are actually deleting the variable (which is likely not your intention).
Upvotes: 4
Reputation: 1
From the answer above ...
set test=
echo test is: '%test%'
will return: test is: '%test%'
set test=?
echo test is: '%test%'
will return: test is: '?'
set test=?
echo test is: '%test:?=%'
will return: test is: ''
Upvotes: 0
Reputation: 196
I had the same problem getting a file name with or without a suffix. You can work around this with a string prefix and string extraction (see here), e.g.
not empty string:
set COUNT=two
set SUFFIX=?s
echo %COUNT% thing%SUFFIX:~1%
gives two things
empty string
set COUNT=one
set SUFFIX=?
echo %COUNT% thing%SUFFIX:~1%
gives one thing
With the %SUFFIX:~1%
you dismiss the string prefix (in this example ?
) and get the rest of the string. This rest can be empty.
Upvotes: 2
Reputation: 666
I think you have a misconception here. There is no way to assign an "empty string" to an environment (Batch) variable. The command set "samplepassword=" deletes the variable, so echo "%samplepassword%" just show "". You don't need setx command to do that, nor setx can modify this behavior. – Aacini Aug 9 at 4:39
TL;DR: Technically it is not possible to create an empty variable in the environment. – Aacini
Upvotes: 3
Reputation: 140186
Technically you can create an empty environment variable, but not with setx
.
Run that as administrator
reg add "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /v TESTENV /d ""
the registry key is added:
it even shows in environment variables system panel
But not in the list when invoking set
So even if it is possible, it is rather useless. However it could fool some programs testing if the env. variable exists by checking if registry key exists. So it could even be counter-productive and source of confusion.
Upvotes: 4