Pipe an output into echo as an variable for calculation

35 viewsbashecholinuxpipeterminal
0

My goal is to divide 2 text files, each containing a single integer,
but limiting the answer to 2 decimal places.

I managed to do it with paste total.txt count.txt | awk '{printf "%.2fn", $1/$2}'

But I saw another way of using echo 'scale=2; <calculation here>' | bc -l

I couldn’t figure out how to pass the numbers from the previous pipe to echo as an variable/argument, I tried use xargs but didn’t know how to reference it.

I tried this: paste -d/ total.txt count.txt | bc -l | xargs echo 'scale=2;' | bc -l
and this: echo 'scale=2; $(paste -d/ total.txt count.txt)/1' | bc -l

My goal is to pipe output into echo as a variable for calculation or use commands inside echo , thank you.