# Execute a program from zig

**URL:** <https://ziggit.dev/t/execute-a-program-from-zig/5819>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [August 30, 2024, 8:06pm UTC](https://ziggit.dev/t/execute-a-program-from-zig/5819 "2024-08-30T20:06:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haxxflaxx](https://ziggit.dev/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@Haxxflaxx](https://ziggit.dev/u/Haxxflaxx)\
**Post date:** [August 30, 2024, 8:06pm UTC](https://ziggit.dev/t/execute-a-program-from-zig/5819/1 "2024-08-30T20:06:33Z")

</div>

I’m trying to build a disassembler and I want to call the assembler from the test cases to generate the binary to disassemble, but I can’t find how to actually call an external program.

I can find that there has been an `std.os.exec` but that seem to be moved or renamed as I can’t find it.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [August 30, 2024, 8:25pm UTC](https://ziggit.dev/t/execute-a-program-from-zig/5819/2 "2024-08-30T20:25:29Z")

</div>

Functions from `std.os` moved to `std.posix`.

Probably what you want is not the unix specific `exec` that replaces the current process, but the `spawn`, `spawnAndWait`, or `run` function of [std.process.Child](https://ziglang.org/documentation/master/std/#std.process.Child)

Test Example:  
_test.zig_

```zig
const std = @import("std");

test {
    const argv = [_][]const u8{ "echo", "Hello", "World" };
    var child = std.process.Child.init(&argv, std.testing.allocator);
    try child.spawn();
    const exit_code = child.wait();
    try std.testing.expectEqual(exit_code, std.process.Child.Term{ .Exited = 0 });
}

```

Run as:

```shell
> zig test test.zig
Hello World
All 1 tests passed.

```

---

<div class="post-metadata">

**Author:** ![Haxxflaxx](https://ziggit.dev/letter_avatar_proxy/v4/letter/h/71c47a/32.png) [@Haxxflaxx](https://ziggit.dev/u/Haxxflaxx)\
**Post date:** [August 31, 2024, 12:50pm UTC](https://ziggit.dev/t/execute-a-program-from-zig/5819/3 "2024-08-31T12:50:35Z")

</div>

Yes this works. I got a bit confused as it also has a `run` but that takes a lot of the same arguments as the `init`, but this works well.

It seems like I can use `collectOutput` to get the stdout as well.
