Mr. 0x50 Mr. 0x50 - 2 months ago 37
Bash Question

Simple Mathematics in Shell/Bash (RAM Calculation)

The following code gets the amount of RAM

number=$(grep MemTotal /proc/meminfo | awk '{print $2;}')

Now, I want to divide this number by e.g. 2048. But how can I solve this in the same line? At the end, I need a number like 2.8 (one digit after point)


You can do it in same awk as

grep MemTotal /proc/meminfo | awk '{ printf "%0.1f\n", $1/2048;}'

Read more about GNU awk format-modifiers. Refer the sub-section under .prec