Detect CPU Architecture in Shell Scripts
·
1 min read
·
96
Words
·
-Views
-Comments
While writing an installer for Code Server, I needed to identify the machine’s CPU architecture to fetch the right tarball.
Code Server Packages
Different architectures require different builds:
Script
uname
reports the architecture:
arch() {
uname_m=$(uname -m)
case $uname_m in
aarch64) echo arm64 ;;
x86_64) echo amd64 ;;
*) echo "$uname_m" ;;
esac
}
ARCH=${ARCH:-$(arch)}
Once you know the architecture, downloading the matching package is trivial.
Cloud Server Architectures
Cloud providers (e.g., Tencent Cloud) display CPU architecture when you provision instances, so you can choose what you need up front:
Final Notes
That’s it—simple and effective.