Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6858575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:10:34+00:00 2026-05-27T02:10:34+00:00

Windows user defined environment variable names can contain any character except = . Special

  • 0

Windows user defined environment variable names can contain any character except =.

Special characters can be included by escaping them. A simpler method is to simply enclose the entire SET expression within quotes. For example:

set "A weird & "complex" variable=My value"
set A weird ^& "complex" variable=My value

Both expressions above give the same result. The variable name is A weird & "complex" variable and the value is My value

The IF DEFINED construct is used to test if a variable is defined. Quotes don’t work for this test, special characters in the name (including quotes) must be escaped.

set "A&B=value"
if defined A^&B echo This works
if defined "A&B" echo This does not work

The above escaped test works just fine. The quoted test does not work

But how can I test if a variable containing spaces exists?

set "A B=value"
if defined A^ B echo this does not work!

It seems like the above should work, but it doesn’t!

I’m looking for an answer that does NOT involve expanding the variable using %A B% or !A B!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:10:35+00:00Added an answer on May 27, 2026 at 2:10 am

    Interessting question (I love this syntax base questions).

    Obviously you know how to check it with delayed expansion and also FOR-parameters works.

    @echo off
    setlocal
    set "AAA BBB=value"
    set ""AAA BBB"="
    set "AAA="
    for %%a in ("AAA BBB") do if defined %%~a echo FOR: This works
    
    setlocal EnableDelayedExpansion
    set "varname=AAA BBB"
    if defined !varname! echo Delayed: This works
    
    if defined %varname% ( echo percent: Never comes here 
    ) ELSE ( echo percent: Never comes here ? )
    
    if defined AAA^ BBB ( echo escape1: Never comes here
    ) ELSE ( echo escape1: fails )
    
    set AAA=Hello
    if defined AAA^ BBB ( 
       echo escape2: It only test for AAA the BBB will be "removed"
    ) ELSE ( echo escape2: fails )
    
    set "space= "
    if defined AAA!space!BBB echo inject space: This works
    
    if defined "AAA BBB"  (echo Quote1: Never comes here 
    ) ELSE ( echo Quote1: Fails )
    
    set ""AAA BBB"=value"
    if defined "AAA BBB" echo Quote2: This works, it checks for "AAA BBB" with quotes
    

    In my opionion, in the escape2 example the parser first split the line into tokens this way:
    <if> <defined> <AAA BBB> <echo ....
    But at the execution time of the if defined it rescan the <AAA BBB> token so it only gets the AAA.
    You can’t inject a second escape like AAA^^^ BBB as this only searches for the variable named AAA^

    I can’t see a solution without delaying/FOR, as the escaping of the space always fails.

    EDIT: It can also be solved with SET <varname>
    The solution of ijprest uses the SET command to test the variable without the need of escaping the varname.
    But it also shows interessting behaviour with spaces inside and at the end of a varname.

    It seems to follow these rules:
    SET varname searches for all variables beginning with varname, but first it removes all characters after the last space character of varname, and it removes all leading spaces.
    So you can’t search for variables with beginning with space (but it is also a bit tricky to create such a varname).

    The same behaviour is also active if the variablename is enclosed into quotes, but then exists one more rule.
    First remove all characters after the last quote, if there are at least two quotes.
    Use the text inside of the quotes, and use the “space”-rule.

    Sample.

    set    "   abc def ghi"  junk junk
    *** 1. removes the junk 
    set    "   abc def ghi"
    *** 2. removes the quotes
    set       abc def ghi
    *** 3. removes all after the last space, and the trailing spaces
    set abc def
    *** Search all variables beginning with abc def
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call one command(user defined) from C program(Windows). Can you tell me
Documentation states: Adds a user-defined custom member to an instance of a Windows PowerShell
In C++ on Windows, we use user defined message to in form UI to
what the built-in or user-defined function that I can use in Javascript or jQuery
I have the following situation: Windows Server environment with multiple user sessions. Windows Service
Despite primarily being a windows user, I am a huge fan of rsync. Now,
I am a Windows user, and I delete temp internet files from IE by
Since I've read Windows User Experience Interaction Guidelines (there's a PDF download avaliable) I've
I have a program that creates a Windows user account using the NetUserAdd() API
I need to grant rights to Windows user group Everyone to the HKCR hive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.