bashlogical-operators

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


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)?


Solution

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

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