xargs 参数枚举的这种用法可以更好地优化吗? 目的是在实际命令中间注入(inject)单个参数。
我愿意:
echo {1..3} | xargs -I{} sh -c 'for i in {};do echo line $i here;done'
或
echo {1..3} | for i in $(xargs -n1);do echo line $i here; done
我得到:
line 1 here
line 2 here
line 3 here
这就是我需要的,但我想知道是否可以避免循环和临时变量?
最佳答案
您需要用换行符分隔 xargs
的输入:
echo {1..3}$'\n' | xargs -I% echo line % here
对于数组扩展,您可以使用printf
:
ar=({1..3})
printf '%s\n' "${ar[@]}" | xargs -I% echo line % here
(如果只是为了输出,您可以在没有 xargs
的情况下使用它:
printf 'line %s here\n' "${ar[@]}"
)
关于linux - 优化 xargs 参数枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791764/