大网数据广告活动
服务器运维问答

How to pass command output as several arguments to another command

· 52CE 我爱测

I have a command that produce a output like this: $./command1 word1 word2 word3 I want to pass this three words as arguments to another command like this: $ command2 word1 word2 word3 How to pass command1 output as three differen…

问题原文

原帖链接 · Addy · CC BY-SA 4.0

I have a command that produce a output like this:

$./command1
word1 word2 word3

I want to pass this three words as arguments to another command like this:

$ command2 word1 word2 word3

How to pass command1 output as three different arguments $1 $2 $3 to command2 ?

已采纳解答

原帖链接 · chaos · CC BY-SA 3.0

You can use xargs, with the -t flag xargs will be verbose and prints the commands it executes:

./command1 | xargs -t -n1 command2

-n1 defines the maximum arguments passed to every call of command2. This will execute:

command2 word1
command2 word2
command2 word3

If you want all as argument of one call of command2 use that:

./command1 | xargs -t command2

That calls command2 with 3 arguments:

command2 word1 word2 word3

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-11-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。