https://codeberg.org/roble/zig-vars/
I searched the docs and online for 10 seconds and didn’t find a way to expand environment variables in a string… so I made my own library! ![]()
I wanted this functionality for some other project I’m working on, but I figured variable expansion would be pretty reusable in other projects too.
const expanded = try vars.expandEnv(init.gpa, init.environ_map, "Hello, ${HOME}!");
defer init.gpa.free(expanded);
std.debug.print("{s}\n", .{expanded});
// prints:
// Hello, /home/me!
For my other project, I specifically wanted to do this. It’s just simple variable expansion. I don’t need anything fancy like detecting file paths and joining them. Just replace the var with the value.
const config_dir = try vars.expandEnv(init.gpa, init.environ_map, "$HOME/.config");
// -> /home/me/.config
Supported Zig versions
- 0.16.0
- 0.17.0-dev.1509+bb296ab9b