Run Another Program from C with execvp
Run another program from C with execvp on Linux: build argv, search PATH, handle failure, and combine it with fork and waitpid to run a command safely.

Running another program from C means replacing the program inside a process, and the call that does it is execvp. It takes a program name and an argument array, searches PATH the way a shell does, and, if it succeeds, never returns. This post shows how to build the argument array correctly, how to combine execvp with fork and waitpid so your own program survives, and how to read the two errors that cover most failures. It assumes GCC 14 on Linux 6.12 with glibc 2.40, and the complete guide to Unix processes places the call in the larger life cycle.
The reference pages are exec in POSIX.1-2024 and execvp(3) in the Linux man-pages.
What execvp does to your process
execvp(file, argv) loads the program named by file into the calling process. The old code, stack and heap vanish. The process ID, the parent, the current directory and the open file descriptors without the close-on-exec flag stay. Since the old program is gone, the call returns only on error, with -1.
The name decodes as exec + v + p. The v means the arguments come as a vector (an array of strings). The p means that if file contains no slash, the library searches the directories in the PATH environment variable for it. Without the p, you must give a path.
Build argv with a NULL terminator
argv is an array of char * ending with a null pointer. The first element is the program's own name by convention, and the rest are its arguments. Here is the shortest useful program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
char *argv[] = { "echo", "hello", "from", "execvp", NULL };
execvp(argv[0], argv);
perror("execvp");
return EXIT_FAILURE;
}The program replaces itself with echo, so only echo's output appears:
hello from execvpThe perror line never runs, because a successful execvp does not return. The NULL at the end of the array is not optional. The kernel and the new program count arguments by looking for it, so leaving it out makes the new program read whatever memory follows the array as more arguments.
Note what the shell does for you that execvp does not. There is no glob expansion, no quote handling and no variable substitution. Passing "*.c" as an argument gives the program a literal asterisk. Each element of the array is exactly one argument, so a filename with spaces stays one argument with no quoting needed, which is why exec is safer than building a command string for system.
Fork first, so your program keeps running
Replacing your own process is rarely what you want. The usual pattern forks a child, calls execvp in the child, and waits in the parent:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
static int run(char *const argv[])
{
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
execvp(argv[0], argv);
perror(argv[0]);
_exit(127);
}
int status;
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid");
return -1;
}
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
int main(void)
{
char *list[] = { "ls", "-d", "/etc", "/no/such/dir", NULL };
int rc = run(list);
printf("ls returned %d\n", rc);
return EXIT_SUCCESS;
}Standard error and standard output both reach the terminal, because the child inherited the descriptors. GNU ls returns 2 when a named path cannot be accessed:
ls: cannot access '/no/such/dir': No such file or directory
/etc
ls returned 2The order of the first two lines can vary because they go to different streams, and ls prints the error at the time it finds it. The child's _exit(127) after a failed execvp follows the shell convention for "command not found". It uses _exit, not exit, so the child does not flush stdio buffers that belong to the parent.
The two errors you will see
When execvp returns, errno explains why. Two codes cover most cases:
errno | Message | Cause |
|---|---|---|
ENOENT | No such file or directory | the program is not in any PATH directory, or a script's interpreter line points to a missing file |
EACCES | Permission denied | the file exists but is not executable, or a directory on the path is not searchable |
A quick test shows both. The program tries a name that does not exist and a file that exists but is not executable:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void try_exec(char *name)
{
char *argv[] = { name, NULL };
execvp(name, argv);
printf("%-14s -> %s\n", name, strerror(errno));
}
int main(void)
{
try_exec("no-such-tool");
try_exec("/etc/hostname");
return EXIT_SUCCESS;
}no-such-tool -> No such file or directory
/etc/hostname -> Permission deniedA third failure surprises people. If execvp finds a file without the #! line and the kernel cannot run it, glibc falls back to running it with /bin/sh, a historical behavior. That means a text file with the execute bit set can run as a shell script even when it has no interpreter line. Do not rely on it; start scripts with a proper #!/bin/sh.
How PATH lookup works
When file has no slash, execvp tries each directory in PATH in order, joining directory and name, and runs the first one that is executable. If PATH is unset, glibc uses a default path that begins with /bin:/usr/bin. Two consequences follow.
The search uses your environment, not a safe one. A program that runs as another user, or with elevated rights, must not call execvp with a bare name while PATH is under the attacker's control, because the attacker can place a program of that name first. Privileged code uses a full path with execv or execve.
The lookup also costs a system call per directory. When you run the same program thousands of times, resolve the path once and call execv with it.
Passing arguments you build at run time
Because argv is an array, you can assemble it from data. This example runs grep on a pattern and file chosen at run time, and the values may contain spaces or shell characters safely:
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "usage: %s PATTERN FILE\n", argv[0]);
return 2;
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return EXIT_FAILURE;
}
if (pid == 0) {
char *args[] = { "grep", "-n", "--", argv[1], argv[2], NULL };
execvp(args[0], args);
_exit(127);
}
int status;
waitpid(pid, &status, 0);
return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}If the file notes.txt holds the three lines alpha, a b c and a;rm -rf x, then ./search 'a;rm' notes.txt prints the matching line with its line number and returns grep's own exit code:
3:a;rm -rf xThe semicolon is just a character. No shell sees it, so nothing is run. The -- before the pattern tells grep that what follows is not an option, which stops a pattern beginning with - from being read as a flag. That protection belongs to grep, not to execvp, and it is a good habit with any program that takes user input.
When execvp is not the right call
If you already have a command as one string, such as ls -l | wc -l, execvp will not parse it; you would have to run sh -c with the string, which brings quoting and injection back. If you need a pipeline, build the pipes yourself, as in the complete guide. If the process must run with a clean, known environment, use execve with an explicit envp. And if you only start programs and never do work between fork and exec, posix_spawnp does the same job in one call.


