ballerina/system package

Package overview

This package contains functions to retrieve information about the system and the current users of the system. It provides information such as environment variables, username, user home directory path, and the current working directory.

Samples

The sample given below uses the functions in the package to get the system-level information.

import ballerina/io;
import ballerina/system;

function main(string... args) {
    //Get environment variables.
    io:println("Envirionment variable: " + system:getEnv("HTTP_PORT")); // Eg. “80”

    // Get the user account name.
    io:println("Username: " + system:getUsername());  //Eg. “john”

    // Get the user home path.
    io:println("User home: " + system:getUserHome());  //Eg. “/home/john”

    // Get the current directory path.
    io:println("Current directory: " + system:getCurrentDirectory());  //Eg. “/home/john/work”
}

Functions Summary

Return Type Function and Description
string getCurrentDirectory()

Returns the current working directory.

string getEnv(string name)

Returns the environment variable value associated with the provided name.

string getUserHome()

Returns the current user's home directory path.

string getUsername()

Returns the current user's name.

string uuid()

Returns a random UUID string.

public function getCurrentDirectory() returns (string)

Returns the current working directory.

Return Type Description
string

Current working directory or an empty string if the current working directory cannot be determined

public function getEnv(string name) returns (string)

Returns the environment variable value associated with the provided name.

Parameter Name Data Type Default Value Description
name string

Name of the environment variable

Return Type Description
string

Environment variable value if it exists, otherwise an empty string

public function getUserHome() returns (string)

Returns the current user's home directory path.

Return Type Description
string

Current user's home directory if it can be determined, an empty string otherwise

public function getUsername() returns (string)

Returns the current user's name.

Return Type Description
string

Current user's name if it can be determined, an empty string otherwise

public function uuid() returns (string)

Returns a random UUID string.

Return Type Description
string

The random string