I'm looking for a POSIX shell/bash command to determine if the OS architecture is 386
, amd64
, arm
, or arm64
?
Asked
Active
Viewed 6.6k times
6 Answers
78
I suggest using:
dpkg --print-architecture

Roman Panaget
- 1,578
- 12
- 21
-
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
-
1@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
-
69
uname -m
prints values such as x86_64
, i686
, arm
, or aarch64
.

ephemient
- 198,619
- 38
- 280
- 391
-
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
-
2
-
1This works great on macOS until you have the terminal app running through Rosetta... it will return x86_64... – C-Viorel Mar 17 '23 at 08:17
33
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
- 42,716
- 77
- 201
- 296
-
2While 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
-
-
1@DUzun: That's correct, x86_64 and amd64 are basically synonyms. AMD developed the ISA initially, then Intel implemented it, so some software still uses "amd64" as the architecture name. This is unrelated to whether your AMD64 CPU was sold by Intel, AMD, Via, Zhaoxin, or is emulated by QEMU or Bochs or whatever on totally different hardware. See [The most correct way to refer to 32-bit and 64-bit versions of programs for x86-related CPUs?](https://stackoverflow.com/q/53364320) – Peter Cordes Mar 29 '23 at 16:08
7
$ lscpu | grep Architecture
Architecture: x86_64
Or if you want to get only the value:
$ lscpu | awk '/Architecture:/{print $2}'
x86_64

Zstack
- 4,046
- 1
- 19
- 22
5
$ arch
Also works. Tested on Debian-based and RPM-based distros.

Ilia Sidorenko
- 2,157
- 3
- 26
- 30
-
4With `arch` you get the architecture of the current terminal. The real OS architecture may be different – Noam Nol Feb 16 '22 at 15:46
2
uname -m
This will display the machine hardware name, which will indicate the processor architecture.
If the output is "arm", it means the processor architecture is ARM-based. If the output is "x86_64", it means the processor architecture is AMD-based (also known as x86-64 or Intel 64).

Akshay Kakade
- 77
- 3
-
There's already [an answer](https://stackoverflow.com/questions/48678152/how-to-detect-386-amd64-arm-or-arm64-os-architecture-via-shell-bash/48679326#48679326) that suggests using `uname -m`. Actually two answers, one having a `switch` statement. – Peter Cordes Mar 29 '23 at 16:03