Standard Library
Operating System (os)

OS Module

The os module provides operating system interaction and environment variable access.

funcs

os.exec()

Execute shell commands:

let output = os.exec("echo 'Hello from shell'");
print(output);
 
let files = os.exec("ls -la");
print(files);

os.cwd()

Get current working directory:

let current_dir = os.cwd();
print("Working directory: " + current_dir);

os.chdir()

Change directory:

os.chdir("/path/to/directory");

os.platform()

Get platform name:

let platform = os.platform();  // "darwin", "linux", "windows"
print("Platform: " + platform);

os.arch()

Get architecture:

let arch = os.arch();  // "x64", "arm64"
print("Architecture: " + arch);

Examples

Environment Variables

Environment Variables

Access via the global env object:

let api_key = env.API_KEY;
let db_host = env.DATABASE_HOST;
let db_port = env.DATABASE_PORT;
 
print("Connecting to: " + db_host + ":" + db_port);

System Information

func print_system_info() {
    print("Platform: " + os.platform());
    print("Architecture: " + os.arch());
    print("User: " + env.USER);
    print("Home: " + env.HOME);
    print("Working directory: " + os.cwd());
}
 
print_system_info();

Execute Commands

// List files
let files = os.exec("ls");
print(files);
 
// Get disk usage
let disk_usage = os.exec("df -h");
print(disk_usage);
 
// Get system uptime
let uptime = os.exec("uptime");
print(uptime);

Best Practices

  • Validate environment variables exist
  • Be careful with os.exec() - validate inputs
  • Handle platform differences
  • Use environment variables for configuration

Next Steps

  • Examples - System integration examples