Skip to content

Instantly share code, notes, and snippets.

@Flushot
Last active June 6, 2025 21:12
Show Gist options
  • Save Flushot/e185e477467cfcfab25b14a365fa7aeb to your computer and use it in GitHub Desktop.
Save Flushot/e185e477467cfcfab25b14a365fa7aeb to your computer and use it in GitHub Desktop.
Get logical CPU count in a platform-independent way
#!/bin/bash
os="$(uname -s)"
case $os in
Darwin*|BSD*) sysctl -n hw.ncpu;; # macOS, BSD
Linux*) nproc --all;; # Linux
Solaris*) psrinfo -p;; # Solaris
Win*|CYGWIN*|MINGW*|MSYS_NT*) echo "$NUMBER_OF_PROCESSORS";; # Windows
*) >&2 echo "unsupported os: $os"; exit 1;; # Unsupported
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment