本文介绍了bash命令输出作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假定命令alpha
产生以下输出:
Assume that the command alpha
produces this output:
a b c
d
如果我运行命令
beta $(alpha)
然后beta
将使用四个参数"a"
,"b"
,"c"
和"d"
执行.
then beta
will be executed with four parameters, "a"
, "b"
, "c"
and "d"
.
但是如果我运行命令
beta "$(alpha)"
然后beta
将使用一个参数"a b c d"
执行.
then beta
will be executed with one parameter, "a b c d"
.
要使用两个参数"a b c"
和"d"
执行beta
,我应该写些什么.也就是说,如何强制$(alpha)
从alpha
的每条输出行返回一个参数?
What should I write in order to execute beta
with two parameters, "a b c"
and "d"
. That is, how do I force $(alpha)
to return one parameter per output line from alpha
?
推荐答案
如果您使用的是bash
4或更高版本,则类似于anubhava的答案.
Similar to anubhava's answer, if you are using bash
4 or later.
readarray -t args < <(alpha)
beta "${args[@]}"
这篇关于bash命令输出作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!