当与“-I%”一起使用时,xargs 无法识别“-n1”
xargs doesn't recognize "-n1" when used with "-I%"
当仅使用 -n1
调用 xargs
时,xargs 对每个项目执行单独的 echo
命令:
$ echo 1 2 | xargs -n1
1
2
但是当 -n1
与 -I
选项一起使用时,它将要替换的字符串传递给 xargs
,它会将所有参数传递给单个 echo
命令, 有效忽略-n1
:
$ echo 1 2 | xargs -n1 -I% echo %
1 2
我的目标是使用不同的参数执行任意命令:
$ echo 1 2 | xargs -n1 -I% mycommand %
# What I want to achieve
mycommand 1
mycommand 2
但我对所看到的行为感到很困惑,所以:
- 为什么
xargs
貌似忽略了-n1
?
- 做我想做的事情的正确方法是什么?请注意,我不想在这样做时处理任何文件。
来自xargs(1)
:
-I replace-str
Replace occurrences of replace-str in the initial-arguments with
names read from standard input. Also, unquoted blanks do not
terminate input items; instead the separator is the newline
character. Implies -x and -L 1.
$ echo $'1\n2' | xargs -n1 -I% echo %
1
2
$ echo $'1\n2' | xargs -n1 -I% echo '*' %
* 1
* 2
当仅使用 -n1
调用 xargs
时,xargs 对每个项目执行单独的 echo
命令:
$ echo 1 2 | xargs -n1
1
2
但是当 -n1
与 -I
选项一起使用时,它将要替换的字符串传递给 xargs
,它会将所有参数传递给单个 echo
命令, 有效忽略-n1
:
$ echo 1 2 | xargs -n1 -I% echo %
1 2
我的目标是使用不同的参数执行任意命令:
$ echo 1 2 | xargs -n1 -I% mycommand %
# What I want to achieve
mycommand 1
mycommand 2
但我对所看到的行为感到很困惑,所以:
- 为什么
xargs
貌似忽略了-n1
? - 做我想做的事情的正确方法是什么?请注意,我不想在这样做时处理任何文件。
来自xargs(1)
:
-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. Implies -x and -L 1.
$ echo $'1\n2' | xargs -n1 -I% echo %
1
2
$ echo $'1\n2' | xargs -n1 -I% echo '*' %
* 1
* 2