1 .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl)
3 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
9 .\" The GNU General Public License's references to "object code"
10 .\" and "executables" are to be interpreted as the output of any
11 .\" document formatting or typesetting system, including
12 .\" intermediate and printed output.
14 .\" This manual is distributed in the hope that it will be useful,
15 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 .\" GNU General Public License for more details.
19 .\" You should have received a copy of the GNU General Public
20 .\" License along with this manual; if not, see
21 .\" <http://www.gnu.org/licenses/>.
24 .TH WORDEXP 3 2021-03-22 "" "Linux Programmer's Manual"
26 wordexp, wordfree \- perform word expansion like a posix-shell
29 .B "#include <wordexp.h>"
31 .BI "int wordexp(const char *restrict " s ", wordexp_t *restrict " p \
33 .BI "void wordfree(wordexp_t *" p );
37 Feature Test Macro Requirements for glibc (see
38 .BR feature_test_macros (7)):
49 performs a shell-like expansion of the string
51 and returns the result in the structure pointed to by
55 is a structure that at least has the fields
64 that gives the number of words in the expansion of
70 that points to the array of words found.
75 is sometimes (depending on
77 see below) used to indicate the number of initial elements in the
79 array that should be filled with NULLs.
83 frees the allocated memory again.
84 More precisely, it does not free
85 its argument, but it frees the array
87 and the strings that points to.
88 .SS The string argument
89 Since the expansion is the same as the expansion by the shell (see
91 of the parameters to a command, the string
93 must not contain characters that would be illegal in shell command
95 In particular, there must not be any unescaped
96 newline or |, &, ;, <, >, (, ), {, } characters
97 outside a command substitution or parameter substitution context.
101 contains a word that starts with an unquoted comment character #,
102 then it is unspecified whether that word and all following words
103 are ignored, or the # is treated as a non-comment character.
105 The expansion done consists of the following stages:
106 tilde expansion (replacing \(tiuser by user's home directory),
107 variable substitution (replacing $FOO by the value of the environment
108 variable FOO), command substitution (replacing $(command) or \`command\`
109 by the output of command), arithmetic expansion, field splitting,
110 wildcard expansion, quote removal.
112 The result of expansion of special parameters
113 ($@, $*, $#, $?, $\-, $$, $!, $0) is unspecified.
115 Field splitting is done using the environment variable $IFS.
116 If it is not set, the field separators are space, tab, and newline.
120 contains the words found, followed by a NULL.
121 .SS The flags argument
124 argument is a bitwise inclusive OR of the following values:
127 Append the words found to the array resulting from a previous call.
132 initial NULLs in the array
134 (These are not counted in the returned
138 Don't do command substitution.
143 resulted from a previous call to
148 Reuse the allocated storage.
151 Normally during command substitution
155 This flag specifies that
157 is not to be redirected.
160 Consider it an error if an undefined shell variable is expanded.
167 returns one of the following nonzero values:
170 Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }.
173 An undefined shell variable was referenced, and the
176 told us to consider this an error.
179 Command substitution requested, but the
181 flag told us to consider this an error.
187 Shell syntax error, such as unbalanced parentheses or
193 are provided in glibc since version 2.1.
195 For an explanation of the terms used in this section, see
203 Interface Attribute Value
207 MT-Unsafe race:utent const:env
208 env sig:ALRM timer locale
212 T} Thread safety MT-Safe
221 signifies that if any of the functions
226 are used in parallel in different threads of a program,
227 then data races could occur.
229 calls those functions,
230 so we use race:utent to remind users.
232 POSIX.1-2001, POSIX.1-2008.
234 The output of the following example program
235 is approximately that of "ls [a-c]*.c".
243 main(int argc, char *argv[])
248 wordexp("[a\-c]*.c", &p, 0);
250 for (int i = 0; i < p.we_wordc; i++)
251 printf("%s\en", w[i]);