WORDEXP(3P)
๐ WORDEXP(3P) โ POSIX Programmer's Manual ๐
๐ธ PROLOG ๐ธ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ โ so consult the corresponding Linux manual page for details of Linux behavior โ or the interface may not be implemented on Linux at all. ๐
โจ NAME โจ
wordexp, wordfree โ perform word expansions. periodt.
๐ฆ SYNOPSIS ๐ฆ
#include <wordexp.h>
int wordexp(const char *restrict words, wordexp_t *restrict pwordexp,
int flags);
void wordfree(wordexp_t *pwordexp);
๐ DESCRIPTION ๐
okay bestie, here's the vibe: wordexp() shall perform word expansions as described in the Shell and Utilities volume of POSIX.1โ2017, Section 2.6, Word Expansions, subject to quoting as described in the Shell and Utilities volume of POSIX.1โ2017, Section 2.2, Quoting. Then it places the list of expanded words into the structure pointed to by pwordexp. โจ
The words argument is a pointer to a string containing one or more words to be expanded. The expansions shall be the same as would be performed by the command line interpreter if words were the part of a command line representing the arguments to a utility. So your application shall ensure that words does not contain an unquoted <newline> character or any of the unquoted shell special characters '|', '&', ';', '<', '>' โ except in the context of command substitution as specified in the Shell and Utilities volume of POSIX.1โ2017, Section 2.6.3, Command Substitution. It also shall not contain unquoted parentheses or braces, except in the context of command or variable substitution. Your application shall ensure that every member of words which it expects to have expanded by wordexp() does not contain an unquoted initial comment character. Your application shall also ensure that any words which it intends to be ignored (because they begin or continue a comment) are deleted from words. If the argument words contains an unquoted comment character (<number-sign>) that is the beginning of a token, wordexp() shall either treat the comment character as a regular character, or interpret it as a comment indicator and ignore the remainder of words. ๐
The structure type wordexp_t is defined in the <wordexp.h> header and includes at least the following members, bestie:
size_tโwe_wordcโ Count of words matched bywords.char **โwe_wordvโ Pointer to list of expanded words.size_tโwe_offsโ Slots to reserve at the beginning ofpwordexp->we_wordv.
The wordexp() function shall store the number of generated words into pwordexp->we_wordc and a pointer to a list of pointers to words in pwordexp->we_wordv. Each individual field created during field splitting (see the Shell and Utilities volume of POSIX.1โ2017, Section 2.6.5, Field Splitting) or pathname expansion (see the Shell and Utilities volume of POSIX.1โ2017, Section 2.6.6, Pathname Expansion) shall be a separate word in the pwordexp->we_wordv list. The words shall be in order as described in the Shell and Utilities volume of POSIX.1โ2017, Section 2.6, Word Expansions. The first pointer after the last word pointer shall be a null pointer. The expansion of special parameters described in the Shell and Utilities volume of POSIX.1โ2017, Section 2.5.2, Special Parameters is unspecified. ๐
It is the caller's responsibility to allocate the storage pointed to by pwordexp. The wordexp() function shall allocate other space as needed, including memory pointed to by pwordexp->we_wordv. The wordfree() function frees any memory associated with pwordexp from a previous call to wordexp().
The flags argument is used to control the behavior of wordexp(). The value of flags is the bitwise-inclusive OR of zero or more of the following constants, which are defined in <wordexp.h>:
WRDE_APPENDโ Append words generated to the ones from a previous call towordexp().WRDE_DOOFFSโ Make use ofpwordexp->we_offs. If this flag is set,pwordexp->we_offsis used to specify how many null pointers to add to the beginning ofpwordexp->we_wordv. In other words,pwordexp->we_wordvshall point topwordexp->we_offsnull pointers, followed bypwordexp->we_wordcword pointers, followed by a null pointer.WRDE_NOCMDโ If the implementation supports the utilities defined in the Shell and Utilities volume of POSIX.1โ2017, fail if command substitution, as specified in the Shell and Utilities volume of POSIX.1โ2017, Section 2.6.3, Command Substitution, is requested.WRDE_REUSEโ Thepwordexpargument was passed to a previous successful call towordexp(), and has not been passed towordfree(). The result shall be the same as if the application had calledwordfree()and then calledwordexp()withoutWRDE_REUSE.WRDE_SHOWERRโ Do not redirect stderr to/dev/null.WRDE_UNDEFโ Report error on an attempt to expand an undefined shell variable.
The WRDE_APPEND flag can be used to append a new set of words to those generated by a previous call to wordexp(). The following rules apply to applications when two or more calls to wordexp() are made with the same value of pwordexp and without intervening calls to wordfree():
- The first such call shall not set
WRDE_APPEND. All subsequent calls shall set it. - All of the calls shall set
WRDE_DOOFFS, or all shall not set it. - After the second and each subsequent call,
pwordexp->we_wordvshall point to a list containing the following:- a. Zero or more null pointers, as specified by
WRDE_DOOFFSandpwordexp->we_offs - b. Pointers to the words that were in the
pwordexp->we_wordvlist before the call, in the same order as before - c. Pointers to the new words generated by the latest call, in the specified order
- a. Zero or more null pointers, as specified by
- The count returned in
pwordexp->we_wordcshall be the total number of words from all of the calls. - The application can change any of the fields after a call to
wordexp(), but if it does it shall reset them to the original value before a subsequent call, using the samepwordexpvalue, towordfree()orwordexp()with theWRDE_APPENDorWRDE_REUSEflag.
If the implementation supports the utilities defined in the Shell and Utilities volume of POSIX.1โ2017, and words contains an unquoted character โ <newline>, '|', '&', ';', '<', '>', '(', ')', '{', '}' โ in an inappropriate context, wordexp() shall fail, and the number of expanded words shall be 0. ๐
Unless WRDE_SHOWERR is set in flags, wordexp() shall redirect stderr to /dev/null for any utilities executed as a result of command substitution while expanding words. If WRDE_SHOWERR is set, wordexp() may write messages to stderr if syntax errors are detected while expanding words, unless the stderr stream has wide orientation โ in which case the behavior is undefined. It is unspecified whether any write errors encountered while outputting such messages will affect the stderr error indicator or the value of errno.
The application shall ensure that if WRDE_DOOFFS is set, then pwordexp->we_offs has the same value for each wordexp() call and wordfree() call using a given pwordexp.
The results are unspecified if WRDE_APPEND and WRDE_REUSE are both specified. Don't do that, bestie. ๐ซถ
The following constants are defined as error return values:
WRDE_BADCHARโ One of the unquoted characters โ<newline>,'|','&',';','<','>','(',')','{','}'โ appears inwordsin an inappropriate context.WRDE_BADVALโ Reference to undefined shell variable whenWRDE_UNDEFis set inflags.WRDE_CMDSUBโ Command substitution requested whenWRDE_NOCMDwas set inflags.WRDE_NOSPACEโ Attempt to allocate memory failed.WRDE_SYNTAXโ Shell syntax error, such as unbalanced parentheses or unterminated string.
๐ฅ RETURN VALUE ๐ฅ
Upon successful completion, wordexp() shall return 0. Otherwise, a non-zero value, as described in <wordexp.h>, shall be returned to indicate an error. If wordexp() returns the value WRDE_NOSPACE, then pwordexp->we_wordc and pwordexp->we_wordv shall be updated to reflect any words that were successfully expanded. In other error cases, if the WRDE_APPEND flag was specified, pwordexp->we_wordc and pwordexp->we_wordv shall not be modified. ๐
The wordfree() function shall not return a value. It just does its thing. ๐
๐ ERRORS ๐
No errors are defined. Iconic, honestly.
The following sections are informative. โจ
๐ EXAMPLES ๐
None. ๐ญ (I know, bestie, I wanted examples too.)
๐ APPLICATION USAGE ๐
The wordexp() function is intended to be used by an application that wants to do all of the shell's expansions on a word or words obtained from a user. For example, if the application prompts for a pathname (or list of pathnames) and then uses wordexp() to process the input, the user could respond with anything that would be valid as input to the shell. Slay.
The WRDE_NOCMD flag is provided for applications that, for security or other reasons, want to prevent a user from executing shell commands. Disallowing unquoted shell special characters also prevents unwanted side-effects, such as executing a command or writing a file.
POSIX.1โ2008 does not require the wordexp() function to be thread-safe if passed an expression referencing an environment variable while any other thread is concurrently modifying any environment variable; see exec.
Even though the WRDE_SHOWERR flag allows the implementation to write messages to stderr during command substitution or syntax errors, this standard does not provide any way to detect write failures during the output of such messages.
Applications which use wide-character output functions with stderr should ensure that any calls to wordexp() do not write to stderr, by avoiding use of the WRDE_SHOWERR flag.
๐ญ RATIONALE ๐ญ
okay so bestie, here's the tea on why this whole function even exists ๐ซ
wordexp() got added as an alternative to glob(). The drama? People would NOT stop fighting over exactly which features belonged in glob(). Endless discourse, no resolution, very much giving group-chat-at-3am energy ๐ญ
So the hope was this: give the people both. Offer wordexp() โ which does all of the shell word expansions, but might be slow to run โ and also glob() โ which is faster, but only does pathname expansion, with no tilde or parameter expansion. That way, most applications get what they need and everybody chills out. โจ
And lowkey, while wordexp() could be implemented entirely as a plain library routine, the expectation is that most implementations just run a shell in a subprocess to do the expansion. main character behavior, honestly ๐
Two proposals, bestie ๐
Two different approaches got proposed for how you'd hand the required info to the shell and get results back. They're presented here as examples.
Proposal the first: extend echo ๐ธ
One idea: extend the echo utility by adding a -q option. That option would make echo add a <backslash> before every <backslash> and <blank> that shows up inside an argument. Then wordexp() could invoke the shell like this:
(void) strcpy(buffer, "echo -q");
(void) strcat(buffer, words);
if ((flags & WRDE_SHOWERR) == 0)
(void) strcat(buffer, "2>/dev/null");
f = popen(buffer, "r");
Then wordexp() would read the output that comes back, strip out the unquoted <backslash> characters, and split into words at unquoted <blank> characters. And if the WRDE_NOCMD flag was set, wordexp() would have to scan words before starting the subshell to make sure there'd be no command substitution happening. Either way, it would have to scan words for unquoted special characters. so much scanning, not the vibe ๐ฎโ๐จ
Proposal the second: new options for sh ๐ฆ
The other idea is to add these options to sh:
-wwordlist โ this option provides a wordlist expansion service to applications. The words in wordlist shall be expanded and the following written to standard output:- The count of the number of words after expansion, in decimal, followed by a null byte
- The number of bytes needed to represent the expanded words (not including null separators), in decimal, followed by a null byte
- The expanded words, each terminated by a null byte
If an error is encountered during word expansion, sh exits with a non-zero status after writing the former to report any words successfully expanded
-Pโ Run in ``protected'' mode. If specified with the-woption, no command substitution shall be performed.
With these options, wordexp() could be implemented pretty simply by creating a subprocess using fork() and executing sh with the line:
execl(<shell path>, "sh", "-P", "-w", words, (char *)0);
...after directing standard error to /dev/null.
the stderr situation ๐
It seemed objectionable for a library routine to write messages to standard error unless it was explicitly requested โ like, rude, honestly โ so wordexp() is required to redirect standard error to /dev/null to make sure no messages get generated, even for commands that get executed for command substitution. And if you do want those error messages written? Specify the WRDE_SHOWERR flag. ๐
the WRDE_REUSE situation โป๏ธ
The WRDE_REUSE flag lets the implementation dodge the expense of freeing and reallocating memory, if it's able to. And a minimal implementation can just call wordfree() whenever WRDE_REUSE is set. no shame in the simple route, periodt.
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
like, none. that's it. nothing on the horizon. we're done here bestie ๐
๐ SEE ALSO ๐
exec,fnmatch(),glob()- The Base Definitions volume of POSIX.1โ2017,
<wordexp.h> - The Shell and Utilities volume of POSIX.1โ2017, Chapter 2, Shell Command Language
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's a wrap, bestie ๐ this lil masterpiece is brought to you by IEEE/The Open Group, dated 2017, and it lives under the name WORDEXP(3P) โ go forth and expand those words responsibly, mwah ๐
๐๐ฆ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms