35

I'm looking for a POSIX shell/bash command to determine if the OS architecture is 386, amd64, arm, or arm64?

Justin
  • 38,686
  • 72
  • 185
  • 276

5 Answers5

39
uname -m

prints values such as x86_64, i686, arm, or aarch64.

ephemient
  • 189,938
  • 36
  • 271
  • 385
  • 1
    Humm, 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
    aarch64 is the same as arm64 (64-bit ARM) armv71 is 32-bit ARM – Alex W Apr 20 '20 at 00:36
31

I suggest using:

dpkg --print-architecture
Roman Panaget
  • 980
  • 9
  • 20
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
  • 1
    While 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
  • On my Intel machine `uname -m` == "x86_64" – DUzun Aug 07 '21 at 15:39
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
  • 1
    With `arch` you get the architecture of the current terminal. The real OS architecture may be different – Noam Nol Feb 16 '22 at 15:46