| 1 |
'\" t |
| 2 |
.\" Copyright 2003, Andries E. Brouwer <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
.\" |
| 7 |
.TH wordexp 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
wordexp, wordfree \- perform word expansion like a posix-shell |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B "#include <wordexp.h>" |
| 16 |
.P |
| 17 |
.BI "int wordexp(const char *restrict " s ", wordexp_t *restrict " p \ |
| 18 |
", int " flags ); |
| 19 |
.BI "void wordfree(wordexp_t *" p ); |
| 20 |
.fi |
| 21 |
.P |
| 22 |
.RS -4 |
| 23 |
Feature Test Macro Requirements for glibc (see |
| 24 |
.BR feature_test_macros (7)): |
| 25 |
.RE |
| 26 |
.P |
| 27 |
.BR wordexp (), |
| 28 |
.BR wordfree (): |
| 29 |
.nf |
| 30 |
_XOPEN_SOURCE |
| 31 |
.fi |
| 32 |
.SH DESCRIPTION |
| 33 |
The function |
| 34 |
.BR wordexp () |
| 35 |
performs a shell-like expansion of the string |
| 36 |
.I s |
| 37 |
and returns the result in the structure pointed to by |
| 38 |
.IR p . |
| 39 |
The data type |
| 40 |
.I wordexp_t |
| 41 |
is a structure that at least has the fields |
| 42 |
.IR we_wordc , |
| 43 |
.IR we_wordv , |
| 44 |
and |
| 45 |
.IR we_offs . |
| 46 |
The field |
| 47 |
.I we_wordc |
| 48 |
is a |
| 49 |
.I size_t |
| 50 |
that gives the number of words in the expansion of |
| 51 |
.IR s . |
| 52 |
The field |
| 53 |
.I we_wordv |
| 54 |
is a |
| 55 |
.I "char\ **" |
| 56 |
that points to the array of words found. |
| 57 |
The field |
| 58 |
.I we_offs |
| 59 |
of type |
| 60 |
.I size_t |
| 61 |
is sometimes (depending on |
| 62 |
.IR flags , |
| 63 |
see below) used to indicate the number of initial elements in the |
| 64 |
.I we_wordv |
| 65 |
array that should be filled with NULLs. |
| 66 |
.P |
| 67 |
The function |
| 68 |
.BR wordfree () |
| 69 |
frees the allocated memory again. |
| 70 |
More precisely, it does not free |
| 71 |
its argument, but it frees the array |
| 72 |
.I we_wordv |
| 73 |
and the strings that points to. |
| 74 |
.SS The string argument |
| 75 |
Since the expansion is the same as the expansion by the shell (see |
| 76 |
.BR sh (1)) |
| 77 |
of the parameters to a command, the string |
| 78 |
.I s |
| 79 |
must not contain characters that would be illegal in shell command |
| 80 |
parameters. |
| 81 |
In particular, there must not be any unescaped |
| 82 |
newline or |, &, ;, <, >, (, ), {, } characters |
| 83 |
outside a command substitution or parameter substitution context. |
| 84 |
.P |
| 85 |
If the argument |
| 86 |
.I s |
| 87 |
contains a word that starts with an unquoted comment character #, |
| 88 |
then it is unspecified whether that word and all following words |
| 89 |
are ignored, or the # is treated as a non-comment character. |
| 90 |
.SS The expansion |
| 91 |
The expansion done consists of the following stages: |
| 92 |
tilde expansion (replacing \[ti]user by user's home directory), |
| 93 |
variable substitution (replacing $FOO by the value of the environment |
| 94 |
variable FOO), command substitution (replacing $(command) or \`command\` |
| 95 |
by the output of command), arithmetic expansion, field splitting, |
| 96 |
wildcard expansion, quote removal. |
| 97 |
.P |
| 98 |
The result of expansion of special parameters |
| 99 |
($@, $*, $#, $?, $\-, $$, $!, $0) is unspecified. |
| 100 |
.P |
| 101 |
Field splitting is done using the environment variable $IFS. |
| 102 |
If it is not set, the field separators are space, tab, and newline. |
| 103 |
.SS The output array |
| 104 |
The array |
| 105 |
.I we_wordv |
| 106 |
contains the words found, followed by a NULL. |
| 107 |
.SS The flags argument |
| 108 |
The |
| 109 |
.I flag |
| 110 |
argument is a bitwise inclusive OR of the following values: |
| 111 |
.TP |
| 112 |
.B WRDE_APPEND |
| 113 |
Append the words found to the array resulting from a previous call. |
| 114 |
.TP |
| 115 |
.B WRDE_DOOFFS |
| 116 |
Insert |
| 117 |
.I we_offs |
| 118 |
initial NULLs in the array |
| 119 |
.IR we_wordv . |
| 120 |
(These are not counted in the returned |
| 121 |
.IR we_wordc .) |
| 122 |
.TP |
| 123 |
.B WRDE_NOCMD |
| 124 |
Don't do command substitution. |
| 125 |
.TP |
| 126 |
.B WRDE_REUSE |
| 127 |
The argument |
| 128 |
.I p |
| 129 |
resulted from a previous call to |
| 130 |
.BR wordexp (), |
| 131 |
and |
| 132 |
.BR wordfree () |
| 133 |
was not called. |
| 134 |
Reuse the allocated storage. |
| 135 |
.TP |
| 136 |
.B WRDE_SHOWERR |
| 137 |
Normally during command substitution |
| 138 |
.I stderr |
| 139 |
is redirected to |
| 140 |
.IR /dev/null . |
| 141 |
This flag specifies that |
| 142 |
.I stderr |
| 143 |
is not to be redirected. |
| 144 |
.TP |
| 145 |
.B WRDE_UNDEF |
| 146 |
Consider it an error if an undefined shell variable is expanded. |
| 147 |
.SH RETURN VALUE |
| 148 |
On success, |
| 149 |
.BR wordexp () |
| 150 |
returns 0. |
| 151 |
On failure, |
| 152 |
.BR wordexp () |
| 153 |
returns one of the following nonzero values: |
| 154 |
.TP |
| 155 |
.B WRDE_BADCHAR |
| 156 |
Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }. |
| 157 |
.TP |
| 158 |
.B WRDE_BADVAL |
| 159 |
An undefined shell variable was referenced, and the |
| 160 |
.B WRDE_UNDEF |
| 161 |
flag |
| 162 |
told us to consider this an error. |
| 163 |
.TP |
| 164 |
.B WRDE_CMDSUB |
| 165 |
Command substitution requested, but the |
| 166 |
.B WRDE_NOCMD |
| 167 |
flag told us to consider this an error. |
| 168 |
.TP |
| 169 |
.B WRDE_NOSPACE |
| 170 |
Out of memory. |
| 171 |
.TP |
| 172 |
.B WRDE_SYNTAX |
| 173 |
Shell syntax error, such as unbalanced parentheses or |
| 174 |
unmatched quotes. |
| 175 |
.SH ATTRIBUTES |
| 176 |
For an explanation of the terms used in this section, see |
| 177 |
.BR attributes (7). |
| 178 |
.TS |
| 179 |
allbox; |
| 180 |
lb lb lbx |
| 181 |
l l l. |
| 182 |
Interface Attribute Value |
| 183 |
T{ |
| 184 |
.na |
| 185 |
.nh |
| 186 |
.BR wordexp () |
| 187 |
T} Thread safety T{ |
| 188 |
.na |
| 189 |
.nh |
| 190 |
MT-Unsafe race:utent const:env |
| 191 |
env sig:ALRM timer locale |
| 192 |
T} |
| 193 |
T{ |
| 194 |
.na |
| 195 |
.nh |
| 196 |
.BR wordfree () |
| 197 |
T} Thread safety MT-Safe |
| 198 |
.TE |
| 199 |
.P |
| 200 |
In the above table, |
| 201 |
.I utent |
| 202 |
in |
| 203 |
.I race:utent |
| 204 |
signifies that if any of the functions |
| 205 |
.BR setutent (3), |
| 206 |
.BR getutent (3), |
| 207 |
or |
| 208 |
.BR endutent (3) |
| 209 |
are used in parallel in different threads of a program, |
| 210 |
then data races could occur. |
| 211 |
.BR wordexp () |
| 212 |
calls those functions, |
| 213 |
so we use race:utent to remind users. |
| 214 |
.SH STANDARDS |
| 215 |
POSIX.1-2008. |
| 216 |
.SH HISTORY |
| 217 |
POSIX.1-2001. |
| 218 |
glibc 2.1. |
| 219 |
.SH EXAMPLES |
| 220 |
The output of the following example program |
| 221 |
is approximately that of "ls [a-c]*.c". |
| 222 |
.P |
| 223 |
.\" SRC BEGIN (wordexp.c) |
| 224 |
.EX |
| 225 |
#include <stdio.h> |
| 226 |
#include <stdlib.h> |
| 227 |
#include <wordexp.h> |
| 228 |
\& |
| 229 |
int |
| 230 |
main(void) |
| 231 |
{ |
| 232 |
wordexp_t p; |
| 233 |
char **w; |
| 234 |
\& |
| 235 |
wordexp("[a\-c]*.c", &p, 0); |
| 236 |
w = p.we_wordv; |
| 237 |
for (size_t i = 0; i < p.we_wordc; i++) |
| 238 |
printf("%s\[rs]n", w[i]); |
| 239 |
wordfree(&p); |
| 240 |
exit(EXIT_SUCCESS); |
| 241 |
} |
| 242 |
.EE |
| 243 |
.\" SRC END |
| 244 |
.SH SEE ALSO |
| 245 |
.BR fnmatch (3), |
| 246 |
.BR glob (3) |