popen(3)
๐ popen(3) โ bestie, let's talk about piping to a whole entire process ๐
okay so this is popen(3), a member of the Library Functions Manual squad, and I need you to sit down because this one is lowkey iconic. It's part of Standard C library (libc, -lc), it's been carrying Unix on its back for decades, and it's giving main character energy. โจ
๐ธ NAME ๐ธ
popen, pclose โ pipe stream to or from a process ๐ซถ
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
โจ SYNOPSIS โจ
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) ๐
popen(), pclose():
_POSIX_C_SOURCE >= 2
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
translation bestie: you gotta have_POSIX_C_SOURCEset to 2 or higher to get these, OR if you're on glibc 2.19 or older,_BSD_SOURCEor_SVID_SOURCEwill also do the trick ๐
๐ DESCRIPTION ๐
So what does popen() actually DO. Bestie. It opens a process by creating a pipe, forking, and invoking the shell. Three steps, one function, absolutely unhinged efficiency. ๐ฅ
Since a pipe is by definition unidirectional (<- remember this word, it comes back), the type argument may specify only reading or only writing, not both. So the resulting stream is correspondingly read-only or write-only. You do not get to have both. It's giving monogamous. ๐
The command argument is a pointer to a null-terminated string containing a shell command line. This command is passed to /bin/sh using the -c flag; interpretation, if any, is performed by the shell. (so like, globs and pipes and $VARS work, but that's the shell's doing, not popen's โ ikyk ๐)
The type argument is a pointer to a null-terminated string which must contain either the letter 'r' for reading or the letter 'w' for writing. Not optional, not negotiable, pick one.
Since glibc 2.9, this argument can additionally include the letter 'e', which causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying file descriptor. Why would you want that? See the description of the O_CLOEXEC flag in open(2) โ it's a whole vibe about not leaking fds into random children. ๐
The return value from popen() is a normal standard I/O stream in all respects save that it must be closed with pclose() rather than fclose(3). Like, it looks like a FILE *, it acts like a FILE *, but it has โจcommitment issuesโจ โ you must break up with it via pclose().
- Writing to such a stream writes to the standard input of the command ๐
- The command's standard output is the same as that of the process that called
popen(), unless this is altered by the command itself. - Conversely, reading from the stream reads the command's standard output, and the command's standard input is the same as that of the process that called
popen().
๐จ note that output popen() streams are block buffered by default. no cap. plan accordingly.
The pclose() function waits for the associated process to terminate and returns the exit status of the command as returned by wait4(2). So pclose is the one who actually closes the door and then waits outside to make sure your ex got home safe. ๐ฅฒ
๐ฆ RETURN VALUE ๐ฆ
popen(): on success, returns a pointer to an open stream that can be used to read or write to the pipe. If thefork(2)orpipe(2)calls fail, or if the function cannot allocate memory,NULLis returned. ๐pclose(): on success, returns the exit status of the command. Ifwait4(2)returns an error, or some other error is detected,-1is returned.- On failure, both functions set
errnoto indicate the error.
๐ ERRORS ๐
okay this part is important, listen up ๐ข
- The
popen()function does not seterrnoif memory allocation fails. (yeah. I know. rude. ๐ญ) - If the underlying
fork(2)orpipe(2)fails,errnois set to indicate the error. - If the
typeargument is invalid, and this condition is detected,errnois set toEINVAL. - If
pclose()cannot obtain the child status,errnois set toECHILD.
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
popen(),pclose()โ Thread safety:MT-Safe๐ซถ (yes bestie, both. safe. go off.)
โญ VERSIONS โญ
The 'e' value for type is a Linux extension. (so cute, so Linux-specific, so not portable โ you've been warned ๐ธ)
๐ STANDARDS ๐
POSIX.1-2008.
๐ฆ HISTORY ๐ฆ
POSIX.1-2001.
โ ๏ธ CAVEATS โ ๏ธ
Carefully read Caveats in system(3). Like, actually go read them. This is not a drill, it's a cross-reference, and cross-references in manpages are load-bearing. ๐ฅ
๐ BUGS ๐
ngl this section is giving "we tried our best" โ but you need to know it:
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process has done a buffered read, the command's input position may not be as expected. ๐ญ
Similarly, the output from a command opened for writing may become intermingled with that of the original process. โจchaosโจ
The latter can be avoided by calling fflush(3) before popen(). (that's your one little hack, use it ๐
)
Failure to execute the shell is indistinguishable from the shell's failure to execute the command, or an immediate exit of the command. The only hint is an exit status of 127. That's it. That's the whole hint. 127. Remember it. ๐ซก
๐ SEE ALSO ๐
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3), stdio(3), system(3)
๐ and that's the whole page bestie, go forth and pipe responsibly ๐
Linux man-pages 6.20, dated 2025-09-21 โ this page said what it said, and it ate. periodt. ๐ ๐๐
๐ฆ 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