简体   繁体   中英

How to pass output as command line argument in bash?

I have this two step bash command:

L=`wc -l testfile | cut -d' ' -f1`
myprogram testfile $L testfile.out

Long story short, myprogram needs the line count as an input.

I want to combine this into one line.

This does not work because using redirect | to - passes stdout stream as a file, not a string.

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

Is there a way to combine this into one line?

Use process substitution:

myprogram testfile $(wc -l < testfile) testfile.out
                   ^^^^^^^^^^^^^^^^^^^

This way, wc -l < testfile is evaluated together with the call of the program and you have both commands combined.

Note wc -l < file returns you just the number, so you don't have to do cut or any other thing to clean the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM