I would like to know the server load divided by the amount of CPU cores in order to judge which servers are overloaded. I can get the amount of CPU cores with:

$ grep -c processor /proc/cpuinfo

I can get the server load with:

$ uptime | awk '{print $11}'

Now I would like to divide 0.47/2 to get 0.23. How would I know which manpage to read to do this? I'm really looking for how I can learn what the next step in finding the correct command is, I'm not asking for the command itself.

I've tried searching the question online and even Google couldn't steer me in the right direction. Thus, alternatively, which keywords should I be searching on? The obvious "linux, division, bash, output" did not help.


This should do the trick:

(awk '{printf "%s/", $2}' /proc/loadavg; grep -c processor /proc/cpuinfo;) | bc -l

Also, you should get the load from /proc/loadavg where the command uptime also gets it.

  • Accepting this answer because it taught me the most reusable knowledge. Thank you. – dotancohen Jun 10 '14 at 14:24
  • 1
    you can use nproc instead of grep -c processor /proc/cpuinfo – Flowryn Jan 18 '16 at 12:42

Just do bash scripting. Get both values to 2 different variables. Then by using command bc divide one from other. As simple as that.

  • 1
    Thank you, this got me 90% of the way there. How can I get rid of the , character in the uptime output? – dotancohen Jun 10 '14 at 7:02
  • Could always change the way you're getting load avg and use this - awk -F '[ ,]' ' { print $16 } ' <- which will return only the number rather than the number and the , – Lawrence Jun 10 '14 at 9:06
  • use cut command after to chop off ",". Use "," as Field separator and print first field.Like uptime | awk '{print $11}' | cut -d"," -f1 – Tingrammer Jun 10 '14 at 9:51
  • Thank you Tingrammer. I did accept chaos' answer as it taught me the most reusable knowledge, but I do appreciate your hinting me in the right direction and the refrain from just throwing me a fish. To be honest, I did have to google for how to get each individual value into a variable, but at least you pushed me in the right direction, as that is what I asked for. Thank you! – dotancohen Jun 10 '14 at 14:26
dc  <<< "${decimal_places}k $(uptime | awk '{print $11}'|  cut -d"," -f1) $(grep -c processor /proc/cpuinfo) /p"

This answer uses $() in bash so no temporary variables needed. It also uses dc, a calculator. We tell dc the precision using k, then give it 2 numbers, then ask it to divide them and print the answer.


Ksh and zsh have floating point arithmetic. Other shells including bash have only integer arithmetic (limited to 32 bits or 64 bits).

Every shell's man page has a section on arithmetic. If you want floating point calculations, it's harder to find. The primary way to search for man pages is the apropos command, which searches the pages' one-line descriptions. Since you're after something to use from the shell, you want section 1. apropos -s 1 float turns up nothing relevant on my Linux machine. What ends up working is

$ apropos -s 1 calculator
bc (1)               - An arbitrary precision calculator language
dc (1)               - an arbitrary precision calculator

The web has a lot more resources to offer. On the first page of http://www.google.com/search?q=bash+float, I find How to do integer & float calculations, in bash or other languages/frameworks? and now your question (and also several Stack Overflow threads). http://www.google.com/search?q=bash+division gives me several SO threads, the second one being how can I get a float division in bash?. You can also search on Unix Stack Exchange which has a question tagged and now also : How to do integer & float calculations, in bash or other languages/frameworks?

A common choice for floating point or large integer calculations is bc. It's a classic Unix utility which has been standardized.

Another standard utility that can do floating point computation and is perhaps easier to get started with than bc's precision-based model is awk. While awk is primarily designed for text processing, it can also do floating-point calculations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.