wordexp(3)
โจ wordexp(3) โจ
like, shell expansion but make it a C function ๐
okay bestie so you know how your shell does all that cute ~ and $FOO and *.c magic before it runs anything? wordexp is literally that, but callable from C. it's from the Library Functions Manual and it lives in the Standard C library (libc, -lc). it's giving "we have a shell at home" and the shell at home is THIS. ๐ฅ
๐ธ NAME ๐ธ
wordexp, wordfree โ perform word expansion like a posix-shell
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ฆ SYNOPSIS ๐ฆ
#include <wordexp.h>
int wordexp(const char *restrict s, wordexp_t *restrict p, int flags);
void wordfree(wordexp_t *p);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
wordexp(), wordfree():
_XOPEN_SOURCE
๐ DESCRIPTION ๐
so the function wordexp() performs a shell-like expansion of the string s and hands you back the result in the structure pointed to by p. ๐ซถ
the data type wordexp_t is a structure that at least has the fields we_wordc, we_wordv, and we_offs. let's break it down:
we_wordcโ asize_tgiving the number of words in the expansion ofs. it's the headcount, the guest list. ๐we_wordvโ achar **pointing to the array of words found. this is the main character, the actual content.we_offsโ typesize_t, and it is sometimes used (depending on flags, see below) to indicate how many initial elements in thewe_wordvarray should be filled withNULLs. it's the "leave some seats empty at the front" field. ๐
then the function wordfree() frees the allocated memory again. and ngl, be precise about this: it does not free its argument โ it frees the array we_wordv and the strings that it points to. the struct itself is your problem, bestie.
๐ The string argument ๐
since the expansion is the same as the expansion by the shell (see sh(1)) of the parameters to a command, the string s must not contain characters that would be illegal in shell command parameters. specifically, there must not be any unescaped newline or |, &, ;, <, >, (, ), {, } characters outside a command substitution or parameter substitution context. so like, don't be weird with it. ๐
if the argumentscontains a word that starts with an unquoted comment character#, then it is unspecified whether that word and all following words are ignored, or the#is treated as a non-comment character. yes, unspecified, no cap. don't build your life on it.
๐ The expansion ๐
the expansion done consists of the following stages, in order, and this is the whole lineup:
- tilde expansion โ replacing
~userby user's home directory - variable substitution โ replacing
$FOOby the value of the environment variableFOO - command substitution โ replacing
$(command)or`command`by the output ofcommand - arithmetic expansion
- field splitting
- wildcard expansion
- quote removal
the result of expansion of special parameters ($@, $*, $#, $?, $-, $$, $!, $0) is unspecified. ๐ญ
field splitting is done using the environment variable $IFS. if it is not set, the field separators are space, tab, and newline. iconic default behavior honestly.
โญ The output array โญ
the array we_wordv contains the words found, followed by a NULL. terminating NULL, as a treat. ๐
๐ The flags argument ๐
the flag argument is a bitwise inclusive OR of the following values:
WRDE_APPENDโ append the words found to the array resulting from a previous call. (stacking, we love a sequel)WRDE_DOOFFSโ insertwe_offsinitialNULLs in the arraywe_wordv. (these are not counted in the returnedwe_wordc. read that again, bestie, it matters)WRDE_NOCMDโ don't do command substitution.WRDE_REUSEโ the argumentpresulted from a previous call towordexp(), andwordfree()was not called. reuse the allocated storage.WRDE_SHOWERRโ normally during command substitutionstderris redirected to/dev/null. this flag specifies thatstderris not to be redirected. (show me the errors, i want the drama ๐ฅ)WRDE_UNDEFโ consider it an error if an undefined shell variable is expanded.
๐ฅ RETURN VALUE ๐ฅ
on success, wordexp() returns 0. periodt. โจ
on failure, wordexp() returns one of the following nonzero values:
WRDE_BADCHARโ illegal occurrence of newline or one of|,&,;,<,>,(,),{,}.WRDE_BADVALโ an undefined shell variable was referenced, and theWRDE_UNDEFflag told us to consider this an error.WRDE_CMDSUBโ command substitution requested, but theWRDE_NOCMDflag told us to consider this an error.WRDE_NOSPACEโ out of memory.WRDE_SYNTAXโ shell syntax error, such as unbalanced parentheses or unmatched quotes.
๐ง ATTRIBUTES ๐ง
for an explanation of the terms used in this section, see attributes(7).
wordexp()โ Thread safety:MT-Unsafe race:utent const:env env sig:ALRM timer localewordfree()โ Thread safety:MT-Safe
in the above list, utent in race:utent signifies that if any of the functions setutent(3), getutent(3), or endutent(3) are used in parallel in different threads of a program, then data races could occur. wordexp() calls those functions, so we use race:utent to remind users. consider this your friendly little heads-up note taped to the mirror. ๐
๐ STANDARDS ๐
POSIX.1-2008.
๐ธ HISTORY ๐ธ
POSIX.1-2001. glibc 2.1.
๐ EXAMPLES ๐
the output of the following example program is approximately that of "ls [a-c]*.c".
#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
int
main(void)
{
wordexp_t p;
char **w;
wordexp("[a-c]*.c", &p, 0);
w = p.we_wordv;
for (size_t i = 0; i < p.we_wordc; i++)
printf("%s\n", w[i]);
wordfree(&p);
exit(EXIT_SUCCESS);
}
๐ฆ SEE ALSO ๐ฆ
okay that's the whole thing, you ate it, you understood it, you're literally a shell expansion girly now ๐ซถ๐
this little love letter is from the Linux man-pages, version 6.20, dated 2026-02-08, for wordexp(3). mwah, go read glob(3) next, i'll wait. ๐โจ
๐ฆ 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: GPL-2.0-or-later
- Copyright 2003, Andries E. Brouwer <aeb@cwi.nl>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms