c - Wordexp 与 & 的行为

标签 c posix expansion

有人知道包含 '|''&' 的字符串的 wordexp() 行为吗?

我使用 wordexp(str, &res, WRDE_UNDEF),但使用包含 '|''&' 的所有字符串 wordexp() 返回 WRDE_BADCHAR。我希望 wordexp() 不会报告此情况。

例如我希望 wordexp("a & b", &res, WRDE_UNDEF) 返回带有 3 个字符串 "a""&""b"wordexp_t .

最佳答案

非法字符( newline|&;<>(){ , } ) 需要转义。这是通过在它们前面加上 \ 前缀来完成的。 .

逐字记录 man wordexp :

The string argument

Since the expansion is the same as the expansion by the shell (see sh(1)) of the parameters to a command, the string s must not contain characters that would be illegal in shell command parameters. In particular, there must not be any unescaped newline or |, &, ;, <, >, (, ), {, } characters outside a command substitution or parameter substitution context.

关于c - Wordexp 与 & 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18259188/

相关文章:

CS50 复数无法编译

c - linux 2.6中的posix aio是否支持套接字文件描述符?

bash - 重建 $@ 并在 Bash 中保留正确的单词拆分

Vim 状态行不会从表达式中扩展颜色/高亮组

CUDA 如何在主机代码中声明常量内存时访问设备内核中的常量内存?

C 从整数中提取奇数位

c - 命令行错误消息的 "POSIX-defined format"是什么?哪个标准?

bash - 为什么这种花括号和双引号的组合在 bash 中不起作用?

c - 如何让子进程收到SIGSEGV后异常退出?

c - 如何在 Linux 中删除补充组?