Main Content

isenv

Determine if environment variable exists

Since R2022b

    Description

    example

    tf = isenv(varname) returns a logical array containing 1 (true) where the elements of the varname array are operating system environment variables, and 0 (false) where they are not. The logical array tf has the same dimensions as the input array.

    On most UNIX® platforms, an environment variable can exist with an empty value (""). On the Microsoft® Windows® platform, setting the value of an environment variable to "" is equivalent to removing the variable.

    Examples

    collapse all

    Create environment variable myVar, and then check that it exists.

    setenv("myVar","myValue");
    isenv("myVar")
    ans = logical
       1
    
    

    Remove myVar, and then check that it no longer exists.

    unsetenv("myVar");
    isenv("myVar")
    ans = logical
       0
    
    

    Create multiple environment variables, and then check that they exist.

    setenv(["Var1" "Var2" "Var3" "Var4"],["Val1" "Val2" "Val3" "Val4"]);
    isenv(["Var1" "Var2"; "Var3" "Var4"])
    ans = 2x2 logical array
    
       1   1
       1   1
    
    

    Remove two of the environment variables using unsetenv, and then check that they no longer exist.

    unsetenv(["Var1" "Var4"]);
    isenv(["Var1" "Var2"; "Var3" "Var4"])
    ans = 2x2 logical array
    
       0   1
       1   0
    
    

    You can also remove environment variables using setenv with missing. Change the value of one environment variable and remove another.

    setenv(["Var2" "Var3"],["ValB" missing]);
    isenv(["Var1" "Var2"; "Var3" "Var4"])
    ans = 2x2 logical array
    
       0   1
       0   0
    
    

    Input Arguments

    collapse all

    Environment variable names, specified as a string scalar, character vector, string array, or cell array of character vectors. If specified as a character array, a scalar logical array is returned.

    Example: "PATH"

    Extended Capabilities

    Version History

    Introduced in R2022b

    expand all