1wordexp(3)                 Library Functions Manual                 wordexp(3)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

STANDARDS

133       POSIX.1-2008.
134

HISTORY

136       POSIX.1-2001.  glibc 2.1.
137

EXAMPLES

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

SEE ALSO

161       fnmatch(3), glob(3)
162
163
164
165Linux man-pages 6.04              2023-03-30                        wordexp(3)
Impressum