Even though this was rejected and removed, it’s still possible:
//usr/bin/env zig run "$0"; exit
pub fn main() void {
std.debug.print("Hello, world\n", .{});
}
const std = @import("std");
The trick here is that the file is both a valid shell script and Zig program. The first line is interpreted by the shell, which runs zig run "$0"
, where $0
contains the path to the current file (this also works with other languages that use //
as comment tokens: C, Rust, etc.).
You can either run it with your shell
sh test.zig
or make the file executable
chmod +x test.zig
and “execute” it
./test.zig
You should probably never do this, but I just thought it was a fun little hack. That’s all- have a good day
Related: