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

Jerold Schulman

January 18, 1999

1 Min Read
ITPro Today logo


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

if not "%var%"

<h1><b><a name="_echo_var_var_b_p_p_While_this_works_a_better_method_is_p_p_b_if_defined_var_echo_var_var_b_p_p_Try_this_little_test_p_pre_b_echo_off_setlocal_if_not_var_" id="_echo_var_var_b_p_p_While_this_works_a_better_method_is_p_p_b_if_defined_var_echo_var_var_b_p_p_Try_this_little_test_p_pre_b_echo_off_setlocal_if_not_var_">"" @echo var=%var%</a></b></h1>

While this works, a better method is:

if defined var @echo var=%var%

Try this little test:

@echo offsetlocalif not "%var%""" @echo line 3 %var%if defined var @echo line 4 %var%set var=hello worldif not "%var%"=="" @echo line 6 %var%if defined var @echo line 7 %var%endlocal


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like