IT

How to determine if it is M1 in Terminal regardless of Rosetta settings

m1-detect-terminal-en

How to determine if it is M1 in Terminal regardless of Rosetta settings

When judging whether it is M1 or not in Terminal of Mac, I think that the following processing may be performed.

if [[ `uname -m` == 'arm64' ]]; then
  echo "This is arm64"
else
  echo "This is x86_64"
fi

If you refer to this, uname -m is a command to check the kernel version you are currently using, and when you use it in iOS development, it is mainly used to judge whether it is M1 or not.
However, this command will be x86_64 if the Terminal Rosetta setting is enabled, so it cannot be used to determine if it is an M1 when Rosetta is enabled. In this case, it can be determined by the following branch processing.

arch -arm64 uname -m
if [[ $? == 0 ]]; then
  echo "arm64"
else
  echo "x86_64"
fi

If you put it before the command to execute arch -arm64 or arch -x86_64, you can execute the command in each architecture regardless of Rosetta‘s settings.
But for non-M1 Macs, we get an error because I don’t have the arm64 architecture.
Therefore, it becomes false in the branch processing of the next if statement, and it is possible to judge whether it is M1 regardless of the Rosetta setting.
If there is a simpler way than this, I would appreciate it if you could tell me!

+1

COMMENT

Your email address will not be published. Required fields are marked *

CAPTCHA