1WORDEXP(3)                 Linux Programmer's Manual                WORDEXP(3)
2
3
4

NAME

6       wordexp, wordfree - perform word expansion like a posix-shell
7

SYNOPSIS

9       #include <wordexp.h>
10
11       int wordexp(const char *restrict s, wordexp_t *restrict p, int flags);
12       void wordfree(wordexp_t *p);
13
14   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16       wordexp(), wordfree():
17           _XOPEN_SOURCE
18

DESCRIPTION

20       The  function wordexp() performs a shell-like expansion of the string s
21       and returns the result in the structure pointed to by p.  The data type
22       wordexp_t  is  a  structure  that  at  least  has  the fields we_wordc,
23       we_wordv, and we_offs.  The field we_wordc is a size_t that  gives  the
24       number of words in the expansion of s.  The field we_wordv is a char **
25       that points to the array of words found.  The  field  we_offs  of  type
26       size_t  is  sometimes  (depending on flags, see below) used to indicate
27       the number of initial elements in the we_wordv  array  that  should  be
28       filled with NULLs.
29
30       The  function  wordfree()  frees the allocated memory again.  More pre‐
31       cisely, it does not free its argument, but it frees the array  we_wordv
32       and the strings that points to.
33
34   The string argument
35       Since  the  expansion  is  the  same as the expansion by the shell (see
36       sh(1)) of the parameters to a command, the string s  must  not  contain
37       characters  that would be illegal in shell command parameters.  In par‐
38       ticular, there must not be any unescaped newline or |, &, ;, <,  >,  (,
39       ),  {, } characters outside a command substitution or parameter substi‐
40       tution context.
41
42       If the argument s contains a word that starts with an unquoted  comment
43       character #, then it is unspecified whether that word and all following
44       words are ignored, or the # is treated as a non-comment character.
45
46   The expansion
47       The expansion done consists of the following  stages:  tilde  expansion
48       (replacing  ~user by user's home directory), variable substitution (re‐
49       placing $FOO by the value of the  environment  variable  FOO),  command
50       substitution  (replacing  $(command) or `command` by the output of com‐
51       mand), arithmetic expansion, field splitting, wildcard expansion, quote
52       removal.
53
54       The  result of expansion of special parameters ($@, $*, $#, $?, $-, $$,
55       $!, $0) is unspecified.
56
57       Field splitting is done using the environment variable $IFS.  If it  is
58       not set, the field separators are space, tab, and newline.
59
60   The output array
61       The array we_wordv contains the words found, followed by a NULL.
62
63   The flags argument
64       The flag argument is a bitwise inclusive OR of the following values:
65
66       WRDE_APPEND
67              Append  the  words  found to the array resulting from a previous
68              call.
69
70       WRDE_DOOFFS
71              Insert we_offs initial NULLs in the array we_wordv.  (These  are
72              not counted in the returned we_wordc.)
73
74       WRDE_NOCMD
75              Don't do command substitution.
76
77       WRDE_REUSE
78              The  argument  p resulted from a previous call to wordexp(), and
79              wordfree() was not called.  Reuse the allocated storage.
80
81       WRDE_SHOWERR
82              Normally during command substitution  stderr  is  redirected  to
83              /dev/null.   This  flag specifies that stderr is not to be redi‐
84              rected.
85
86       WRDE_UNDEF
87              Consider it an error if an undefined shell variable is expanded.
88

RETURN VALUE

90       On success, wordexp() returns 0.  On failure, wordexp() returns one  of
91       the following nonzero values:
92
93       WRDE_BADCHAR
94              Illegal  occurrence of newline or one of |, &, ;, <, >, (, ), {,
95              }.
96
97       WRDE_BADVAL
98              An undefined shell variable was referenced, and  the  WRDE_UNDEF
99              flag told us to consider this an error.
100
101       WRDE_CMDSUB
102              Command  substitution requested, but the WRDE_NOCMD flag told us
103              to consider this an error.
104
105       WRDE_NOSPACE
106              Out of memory.
107
108       WRDE_SYNTAX
109              Shell syntax error, such as unbalanced parentheses or  unmatched
110              quotes.
111

VERSIONS

113       wordexp() and wordfree() are provided in glibc since version 2.1.
114

ATTRIBUTES

116       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
117       tributes(7).
118
119       ┌───────────┬───────────────┬──────────────────────────────────────────┐
120Interface  Attribute     Value                                    
121       ├───────────┼───────────────┼──────────────────────────────────────────┤
122wordexp()  │ Thread safety │ MT-Unsafe race:utent const:env env       │
123       │           │               │ sig:ALRM timer locale                    │
124       ├───────────┼───────────────┼──────────────────────────────────────────┤
125wordfree() │ Thread safety │ MT-Safe                                  │
126       └───────────┴───────────────┴──────────────────────────────────────────┘
127       In  the  above  table, utent in race:utent signifies that if any of the
128       functions setutent(3), getutent(3), or endutent(3) are used in parallel
129       in  different threads of a program, then data races could occur.  word‐
130       exp() calls those functions, so we use race:utent to remind users.
131

CONFORMING TO

133       POSIX.1-2001, POSIX.1-2008.
134

EXAMPLES

136       The output of the following example program is  approximately  that  of
137       "ls [a-c]*.c".
138
139       #include <stdio.h>
140       #include <stdlib.h>
141       #include <wordexp.h>
142
143       int
144       main(int argc, char **argv)
145       {
146           wordexp_t p;
147           char **w;
148
149           wordexp("[a-c]*.c", &p, 0);
150           w = p.we_wordv;
151           for (int i = 0; i < p.we_wordc; i++)
152               printf("%s\n", w[i]);
153           wordfree(&p);
154           exit(EXIT_SUCCESS);
155       }
156

SEE ALSO

158       fnmatch(3), glob(3)
159

COLOPHON

161       This  page  is  part of release 5.12 of the Linux man-pages project.  A
162       description of the project, information about reporting bugs,  and  the
163       latest     version     of     this    page,    can    be    found    at
164       https://www.kernel.org/doc/man-pages/.
165
166
167
168                                  2021-03-22                        WORDEXP(3)
Impressum