There is a straightforward way:

grep -c ^processor /proc/cpuinfo

But it is wrong because it is not always work. More architectures will return correct number of CPUs using:

ls /sys/devices/system/cpu/ | grep -c 'cpu[0-9]\+'

But perhaps the best would be to use the following command

getconf _NPROCESSORS_ONLN

to detect the number of available CPUs on GNU+Linux.

(Thanks to Aron Xu for the hints)