Skip to content

OS Module

The std/os module provides access to operating system information, environment variables, process control, and shell command execution.

import { os } from "std/os";

Returns the operating system name ("darwin", "linux", or "windows").

println(os.platform()); // "darwin" on macOS

Returns the CPU architecture ("amd64", "arm64", etc.).

println(os.arch()); // "arm64" on Apple Silicon

Returns the system hostname.

println(os.hostname()); // e.g. "my-macbook.local"

Returns the number of logical CPUs.

println(os.cpus()); // e.g. 10

Gets the value of an environment variable. Returns an empty string if not set.

const home = os.env("HOME");
println(home); // "/Users/username"

os.setEnv(key: string, value: string): bool

Section titled “os.setEnv(key: string, value: string): bool”

Sets an environment variable. Returns true on success.

os.setEnv("MY_APP_MODE", "production");
println(os.env("MY_APP_MODE")); // "production"

Returns all environment variables as a map.

const allEnv = os.envAll();
println(allEnv["HOME"]);
println(allEnv["PATH"]);

Returns the current working directory.

println(os.cwd()); // e.g. "/Users/dev/project"

Returns the user’s home directory.

println(os.homeDir()); // e.g. "/Users/username"

Returns the system temporary directory.

println(os.tempDir()); // e.g. "/tmp"

Returns the current process ID.

println(os.pid()); // e.g. 12345

Returns command-line arguments as an array.

const args = os.args();
for (var i = 0; i < len(args); i++) {
println("arg[" + string(i) + "]: " + args[i]);
}

Exits the process with the given exit code (default 0).

if (somethingFailed) {
println("Fatal error!");
os.exit(1);
}

Executes a shell command and returns a map with stdout, stderr, and exitCode.

const result = os.exec("ls -la");
println(result["stdout"]);
println(result["exitCode"]); // 0
const gitStatus = os.exec("git status --short");
if (gitStatus["exitCode"] == 0) {
println(gitStatus["stdout"]);
}
import { os } from "std/os";
// Print system info
println("Platform: " + os.platform());
println("Architecture: " + os.arch());
println("CPUs: " + string(os.cpus()));
println("Hostname: " + os.hostname());
println("PID: " + string(os.pid()));
// Working directories
println("CWD: " + os.cwd());
println("Home: " + os.homeDir());
println("Temp: " + os.tempDir());
// Environment variables
os.setEnv("APP_ENV", "staging");
println("APP_ENV: " + os.env("APP_ENV"));
// Execute a command
const result = os.exec("echo 'Hello from Chuks!'");
println(result["stdout"]); // "Hello from Chuks!\n"