OS Module
The std/os module provides access to operating system information, environment variables, process control, and shell command execution.
Import
Section titled “Import”import { os } from "std/os";Platform Information
Section titled “Platform Information”os.platform(): string
Section titled “os.platform(): string”Returns the operating system name ("darwin", "linux", or "windows").
println(os.platform()); // "darwin" on macOSos.arch(): string
Section titled “os.arch(): string”Returns the CPU architecture ("amd64", "arm64", etc.).
println(os.arch()); // "arm64" on Apple Siliconos.hostname(): string
Section titled “os.hostname(): string”Returns the system hostname.
println(os.hostname()); // e.g. "my-macbook.local"os.cpus(): int
Section titled “os.cpus(): int”Returns the number of logical CPUs.
println(os.cpus()); // e.g. 10Environment Variables
Section titled “Environment Variables”os.env(key: string): string
Section titled “os.env(key: string): string”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"os.envAll(): any
Section titled “os.envAll(): any”Returns all environment variables as a map.
const allEnv = os.envAll();println(allEnv["HOME"]);println(allEnv["PATH"]);Directories
Section titled “Directories”os.cwd(): string
Section titled “os.cwd(): string”Returns the current working directory.
println(os.cwd()); // e.g. "/Users/dev/project"os.homeDir(): string
Section titled “os.homeDir(): string”Returns the user’s home directory.
println(os.homeDir()); // e.g. "/Users/username"os.tempDir(): string
Section titled “os.tempDir(): string”Returns the system temporary directory.
println(os.tempDir()); // e.g. "/tmp"Process Information
Section titled “Process Information”os.pid(): int
Section titled “os.pid(): int”Returns the current process ID.
println(os.pid()); // e.g. 12345os.args(): any
Section titled “os.args(): any”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]);}os.exit(code?: int): void
Section titled “os.exit(code?: int): void”Exits the process with the given exit code (default 0).
if (somethingFailed) { println("Fatal error!"); os.exit(1);}Shell Commands
Section titled “Shell Commands”os.exec(command: string): any
Section titled “os.exec(command: string): any”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"]);}Full Example
Section titled “Full Example”import { os } from "std/os";
// Print system infoprintln("Platform: " + os.platform());println("Architecture: " + os.arch());println("CPUs: " + string(os.cpus()));println("Hostname: " + os.hostname());println("PID: " + string(os.pid()));
// Working directoriesprintln("CWD: " + os.cwd());println("Home: " + os.homeDir());println("Temp: " + os.tempDir());
// Environment variablesos.setEnv("APP_ENV", "staging");println("APP_ENV: " + os.env("APP_ENV"));
// Execute a commandconst result = os.exec("echo 'Hello from Chuks!'");println(result["stdout"]); // "Hello from Chuks!\n"