使用空格作为 xargs 参数的分隔符
use whitespace as delimiter for xargs arguments
是否可以将带空格的字符串传递给 xargs 并使用每个 word 作为参数?
例如,我正在尝试这样的事情。但是这两个词都被视为一个参数。
echo foo bar | xargs -I {} sh -c 'echo item: {}'
我想用 shell 而不是 bash。否则,我认为数组可能会有帮助,但它们在 shell.
中不存在
您可以使用 -d
选项来指定分隔符:
echo 'foo bar' | xargs -I {} -d ' ' sh -c 'echo item: {}'
将导致:
item: foo
item: bar
--delimiter=delim, -d delim
Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and
hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. When processing the input, quotes and backslash are not special; every character in the
input is taken literally. The -d option disables any end-of-file string, which is treated like any other argument. You can use this option when the input consists of simply newline-separated items,
although it is almost always better to design your program to use --null where this is possible.
是否可以将带空格的字符串传递给 xargs 并使用每个 word 作为参数?
例如,我正在尝试这样的事情。但是这两个词都被视为一个参数。
echo foo bar | xargs -I {} sh -c 'echo item: {}'
我想用 shell 而不是 bash。否则,我认为数组可能会有帮助,但它们在 shell.
中不存在您可以使用 -d
选项来指定分隔符:
echo 'foo bar' | xargs -I {} -d ' ' sh -c 'echo item: {}'
将导致:
item: foo
item: bar
--delimiter=delim, -d delim
Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. Octal and hexadecimal escape codes are understood as for the printf command. Multibyte characters are not supported. When processing the input, quotes and backslash are not special; every character in the input is taken literally. The -d option disables any end-of-file string, which is treated like any other argument. You can use this option when the input consists of simply newline-separated items, although it is almost always better to design your program to use --null where this is possible.