I'm looking for a POSIX shell/bash command to determine if the OS architecture is 386, amd64, arm, or arm64?
Asked
Active
Viewed 3.1k times
35
Justin
- 38,686
- 72
- 185
- 276
-
1OS architecture, or hardware architecture? – zzxyz Feb 08 '18 at 05:28
-
@zzxyz os. Thanks. – Justin Feb 08 '18 at 05:49
-
This is not quite a duplicate of https://stackoverflow.com/a/45125525, but it is similar. – ephemient Feb 08 '18 at 06:53
5 Answers
39
uname -m
prints values such as x86_64, i686, arm, or aarch64.
ephemient
- 189,938
- 36
- 271
- 385
-
1Humm, so on a Raspberry pi output is `armv7l`. Would that be arm or arm64? – Justin Feb 08 '18 at 06:57
-
3@Justin 64-bit was introduced in ARM v8, so `armv7l` means 32-bit little-endian ARM. That being said, if you're on an RPi3: the processor is actually 64-bit, but if your OS is in 32-bit mode it reports an older model. – ephemient Feb 08 '18 at 07:12
-
1
31
I suggest using:
dpkg --print-architecture
Roman Panaget
- 980
- 9
- 20
-
And more accurate: `uname -m` outputs `aarch64` on Raspberry instead of `arm64`. – Pouriya Zarbafian Aug 27 '21 at 09:21
-
1
-
@YoloPerdiem you're right, apparently they're the same thing. – Pouriya Zarbafian Feb 21 '22 at 09:09
-
1@PouriyaZarbafian That's not what I meant. They are not equivalent. https://github.com/lxc/lxc/issues/129 – Yolo Perdiem Feb 22 '22 at 16:07
-
@YoloPerdiem they're the same in the specific case that I was mentioning (Raspberry), but according to your link `dpkg --print-architecture` would be more accurate as the question is about OS architecture. – Pouriya Zarbafian Feb 23 '22 at 07:00
17
I went with the following:
architecture=""
case $(uname -m) in
i386) architecture="386" ;;
i686) architecture="386" ;;
x86_64) architecture="amd64" ;;
arm) dpkg --print-architecture | grep -q "arm64" && architecture="arm64" || architecture="arm" ;;
esac
Justin
- 38,686
- 72
- 185
- 276
-
1While you're at it, you might as well collapse the first two branches in your case logic to `i386 | i686) architecture="386" ;;` It's always good to include a catch-all branch at the end, too, to the effect of `*) echo "Unable to determine system architecture."; exit 1 ;;` – Peter J. Mello Jun 27 '21 at 02:34
-
3
$ lscpu | grep Architecture
Architecture: x86_64
Or if you want to get only the value:
$ lscpu | awk '/Architecture:/{print $2}'
x86_64
Zstack
- 2,748
- 1
- 15
- 19
3
$ arch
Also works. Tested on Debian-based and RPM-based distros.
Ilia Sidorenko
- 1,682
- 3
- 23
- 28
-
1With `arch` you get the architecture of the current terminal. The real OS architecture may be different – Noam Nol Feb 16 '22 at 15:46