JSI Tip 1005. How can I test if an environment variable exists?


If you need to test for the existance of an environment variable, you might consider using:

if not "%var%"

"" @echo var=%var%

While this works, a better method is:

if defined var @echo var=%var%

Try this little test:


@echo off
setlocal
if not "%var%""" @echo line 3 %var%
if defined var @echo line 4 %var%
set var=hello world
if not "%var%"=="" @echo line 6 %var%
if defined var @echo line 7 %var%
endlocal

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish