exec(3)
โจ exec(3) โ bestie, let's become a whole new program ๐
okay so this is the exec family from the Library Functions Manual, living in the Standard C library (libc, link with -lc). sit down, grab a strawberry ๐, we're gonna talk about the most dramatic thing a process can do: fully replacing itself. it's giving main character energy, honestly.
๐ธ NAME ๐ธ
execl, execlp, execle, execv, execvp, execvpe โ execute a file โจ
๐ SYNOPSIS ๐
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...
/*, (char *) NULL */);
int execlp(const char *file, const char *arg, ...
/*, (char *) NULL */);
int execle(const char *path, const char *arg, ...
/*, (char *) NULL, char *const envp[] */);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐
execvpe():_GNU_SOURCE
ngl if you wantexecvpe()you gotta define_GNU_SOURCE. no macro, no function, periodt.
๐ฆ DESCRIPTION ๐ฆ
so like. the exec() family of functions replaces the current process image with a new process image. that's the whole vibe. your process doesn't fork, doesn't spawn a child, doesn't "call" anything โ it becomes something else. literally a glow-up where you keep the PID but change everything about yourself. iconic.
the functions described on this manual page are layered on top of execve(2) โ so if you want the deep lore about how the current process image actually gets swapped out, go read execve(2). it's the moment, and these are the accessories. ๐ซถ
the initial argument for these functions is the name of a file that is to be executed.
you can group these functions by whatever letters follow the "exec" prefix โ and the letters are not random, bestie, they are the whole personality:
๐ l โ execl(), execlp(), execle() ๐
the const char *arg and every ellipsis after it can be thought of as arg0, arg1, ..., argn. together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. (that's your "l" โ it's a list, one arg at a time, cute and sequential.)
the first argument, by convention, should point to the filename associated with the file being executed.
and here's the thing you cannot forget: the list of arguments must be terminated by a null pointer, and because these are variadic functions, that pointer must be cast as (char *) NULL. miss it and you're giving your program undefined behaviour, which is not the serve you think it is. ๐ญ
by contrast with the 'l' functions, the 'v' functions (right below) specify the command-line arguments of the executed program as a vector.
๐ v โ execv(), execvp(), execvpe() ๐
the char *const argv[] argument is an array of pointers to null-terminated strings that represent the argument list available to the new program.
the first argument, by convention, should point to the filename associated with the file being executed.
and the array of pointers must be terminated by a null pointer. yes, again. that null is the load-bearing element of this entire operation. ๐
๐ e โ execle(), execvpe() ๐
the environment of the new process image is specified via the argument envp. the envp argument is an array of pointers to null-terminated strings and must be terminated by a null pointer. ("e" = environment, you got it bestie.)
all the other exec() functions (the ones that do not have an 'e' in the suffix) take the environment for the new process image from the external variable environ in the calling process.
๐ p โ execlp(), execvp(), execvpe() ๐
these functions duplicate the actions of the shell in searching for an executable file if the specified filename does not contain a slash (/) character. ("p" = PATH, you're so smart, I knew you'd get it.)
the file is sought in the colon-separated list of directory pathnames specified in the PATH environment variable. if this variable isn't defined, the path list defaults to a list that includes the directories returned by confstr(_CS_PATH) (which typically returns the value "/bin:/usr/bin") and possibly also the current working directory โ see VERSIONS below for the whole saga, because it is a saga.
execvpe() searches for the program using the value of PATH from the caller's environment, not from the envp argument. yes, that's a fun little gotcha. yes, it's intentional. no, you don't get to argue with it.
if the specified filename includes a slash character, then PATH is ignored, and the file at the specified pathname is executed.
in addition, certain errors are treated specially:
- if permission is denied for a file (the attempted
execve(2)failed with the errorEACCES), these functions will continue searching the rest of the search path. if no other file is found, however, they will return witherrnoset toEACCES. - if the header of a file isn't recognized (the attempted
execve(2)failed with the errorENOEXEC), these functions will execute the shell (/bin/sh) with the path of the file as its first argument. (if this attempt fails, no further searching is done.) so it's just like. handing the weird file to sh and being like "you deal with this." ๐
all other exec() functions (the ones which do not include 'p' in the suffix) take as their first argument a (relative or absolute) pathname that identifies the program to be executed.
๐ RETURN VALUE ๐
the exec() functions return only if an error has occurred. like, if it worked, you're gone bestie, you're somebody else now, there's no coming back, there's no return value, there's no "after."
the return value is -1, and errno is set to indicate the error.
so like, if this function returns, it already went wrong. that's the whole energy. ๐ญ
๐ฅ ERRORS ๐ฅ
all of these functions may fail and set errno for any of the errors specified for execve(2).
go read execve(2) for the full error list, I'm not about to copy-paste a whole other manpage into this one, that's not the vibe. ๐
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, see attributes(7).
execl(),execle(),execv()โ Thread safety: MT-Safeexeclp(),execvp(),execvpe()โ Thread safety: MT-Safe env
so the plain ones are just MT-Safe, and the PATH-searching ones are MT-Safe env โ because they're out here reading environment stuff while the rest of you are being chaotic. ๐
๐ธ VERSIONS ๐ธ
the default search path (used when the environment does not contain the variable PATH) shows some variation across systems. it generally includes /bin and /usr/bin (in that order) and may also include the current working directory. on some other systems, the current working directory is included after /bin and /usr/bin, as an anti-Trojan-horse measure.
the glibc implementation long followed the traditional default where the current working directory is included at the start of the search path. however, some code refactoring during the development of glibc 2.24 caused the current working directory to be dropped altogether from the default search path. this accidental behavior change is considered mildly beneficial, and won't be reverted. (accidental queen behaviour, honestly. it stays.)
the behavior of execlp() and execvp() when errors occur while attempting to execute the file is historic practice, but has not traditionally been documented and is not specified by the POSIX standard. BSD (and possibly other systems) do an automatic sleep and retry if ETXTBSY is encountered. Linux treats it as a hard error and returns immediately.
traditionally, the functions execlp() and execvp() ignored all errors except for the ones described above and ENOMEM and E2BIG, upon which they returned. they now return if any error other than the ones described above occurs.
๐ STANDARDS ๐
environexecl()execlp()execle()execv()execvp()- POSIX.1-2008.
execvpe()- GNU.
๐ฆ HISTORY ๐ฆ
environexecl()execlp()execle()execv()execvp()- POSIX.1-2001.
execvpe()- glibc 2.11.
๐ BUGS ๐
before glibc 2.24, execl() and execle() employed realloc(3) internally and were consequently not async-signal-safe, in violation of the requirements of POSIX.1. this was fixed in glibc 2.24. (we don't talk about the before times. ๐)
๐ฎ Architecture-specific details ๐ฎ
on sparc and sparc64, execv() is provided as a system call by the kernel (with the prototype shown above) for compatibility with SunOS. this function is not employed by the execv() wrapper function on those architectures.
๐ SEE ALSO ๐
sh(1), execve(2), execveat(2), fork(2), ptrace(2), fexecve(3), system(3), environ(7)
and that's the whole page, bestie ๐ซถ this one's from Linux man-pages 6.20, dated 2025-05-17. go forth, replace a process image, terminate your argument list with a (char *) NULL, and slay. โญ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms