how to calculate the minimum of two variables simply in bash?

m-ric picture m-ric · May 2, 2012 · Viewed 32.1k times · Source

I have a bash script checking the number of CPUs on the platform to efficiently use -j option for make, repo, etc. I use this:

JOBS=$(cat /proc/cpuinfo | grep processor | tail -1 | sed "s,^.*:.*\([0-9].*\)$,\1,")
echo -e "4\n$JOBS" | sort -r | tail -1

It works fine. But, I am wondering if there was any built-in function which does the same thing (i.e. calculating the minimum, or maximum)?

Answer

mvds picture mvds · May 2, 2012

If you mean to get MAX(4,$JOBS), use this:

echo $((JOBS>4 ? JOBS : 4))