import ballerina/io;
import ballerina/os;

public function main() {
    // Returns the environment variable value associated with the `HTTP_PORT`.
    string port = os:getEnv("HTTP_PORT");
    io:println("HTTP_PORT: ", port);

    // Returns the username of the current user.
    string username = os:getUsername();
    io:println("Username: ", username);

    // Returns the current user's home directory path.
    string userHome = os:getUserHome();
    io:println("Userhome: ", userHome);
}

Environment Variables

The os library provides functions to retrieve information about the OS and the current users of the OS.

For more information on the underlying module, see the OS module.

import ballerina/io;
import ballerina/os;
public function main() {
    string port = os:getEnv("HTTP_PORT");
    io:println("HTTP_PORT: ", port);

Returns the environment variable value associated with the HTTP_PORT.

    string username = os:getUsername();
    io:println("Username: ", username);

Returns the username of the current user.

    string userHome = os:getUserHome();
    io:println("Userhome: ", userHome);
}

Returns the current user’s home directory path.

bal run environment_variables.bal
HTTP_PORT: 5005
Username: Alex
Userhome: /Users/Alex