如何在bash中将输出作为命令行参数传递?

如何在bash中将输出作为命令行参数传递?,bash,io-redirection,Bash,Io Redirection,我有两个步骤的bash命令: L=`wc -l testfile | cut -d' ' -f1` myprogram testfile $L testfile.out 长话短说,myprogram需要行数作为输入 我想把它合并成一行。 这不起作用,因为使用重定向到-将标准输出流作为文件而不是字符串传递 wc -l testfile | cut -d' ' -f1 | myprogram testfile - testfile.out 有没有办法将其合并为一行?使用流程替换: myprogr

我有两个步骤的bash命令:

L=`wc -l testfile | cut -d' ' -f1`
myprogram testfile $L testfile.out
长话短说,
myprogram
需要行数作为输入

我想把它合并成一行。

这不起作用,因为使用重定向到
-
将标准输出流作为文件而不是字符串传递

wc -l testfile | cut -d' ' -f1 | myprogram testfile - testfile.out

有没有办法将其合并为一行?

使用流程替换:

myprogram testfile $(wc -l < testfile) testfile.out
                   ^^^^^^^^^^^^^^^^^^^
myprogram testfile$(wc-l
这样,
wc-l
将与程序调用一起计算,您可以将这两个命令组合起来


注意
wc-l
只返回数字,因此不必执行
cut
或任何其他操作来清理输出。

使用进程替换:

myprogram testfile $(wc -l < testfile) testfile.out
                   ^^^^^^^^^^^^^^^^^^^
myprogram testfile$(wc-l
这样,
wc-l
将与程序调用一起计算,您可以将这两个命令组合起来


注意
wc-l
只返回数字,因此您不必执行
cut
或任何其他操作来清理输出。

或myprogram testfile““wc-l$()是首选-->。@CosminMihai在许多情况下,它们都是相同的,如果你想嵌套它们,那就很痛苦。如果你这样做的话,`这是不可能的!(同时,看看在注释:D)或myprogram testfile““wc-l$()是首选-->。@CosminMihai在很多情况下,它们都是一样的,如果你想嵌套它们,那会很痛苦。如果你这样做,那是不可能的!(还有,看看在注释中写它们有多复杂:D)