environment-variableschapel

How to read an environment variable from a Chapel program?


How can one read an environment variable within a Chapel program?


Solution

  • Environment variables can be accessed through the C interface by calling getenv, e.g.

    // Compiled and run on Chapel 1.22.0
    
    extern proc getenv(name : c_string) : c_string;
    
    var value = getenv('SOME_ENV_VAR'.c_str()):string;
    

    Anyone may try, test & extend this code online.

    For more environment variable APIs, see the user-developed EnvVariables module.