Test for 64-bit Capability

UPDATE (10/8/08): The following one-liner will work on ppc or intel boxes and will return 1 if the computer is 64-bit capable or 0 if it is not. UPDATE 2 (12/11/08):  Reader Ted suggested suppressing stderr for cleaner output. I amended the code below to include his suggestion.

sysctl hw.optional 2> /dev/null | awk -F': ' '/64/ {print $2}'

The arch tool is an easy way to test whether a Mac’s processor is intel or ppc, but it does not draw a distinction between the different types of intel processors. It will return i386 whether it’s a Core Duo, Core 2 Duo, or Xeon. We can use the fact that the Core Duo is only intel processor found in Macs incapable of running 64-bit code to write a script that extend arch to test for 64-bit capability.

    #!/bin/bash

    proc=$(/usr/sbin/system_profiler SPHardwareDataType | \
    /usr/bin/awk -F': ' '/Processor Name:/ {print $2}')

    if [ "$(/usr/bin/arch)" == "i386" ]; then
        if [ "$proc" != "Intel Core Duo" ]; then
            /bin/echo $(/usr/bin/arch)-64
        else
            /bin/echo $(/usr/bin/arch)-32
        fi
    else
        /bin/echo $(/usr/bin/arch)
    fi

This script will return i386-32 for intel processors limited to 32 bits, i386-64 for 64-bit capable intel processors, and ppc for non-intel processors.

I’ve called this script archbits and made it downloadable here.

2 Responses to “Test for 64-bit Capability”

  1. Ted Says:

    This is really cool, as I wondered about this the other day, knowing there was an easy answer somewhere. And I end up here randomly following links in my RSS feed today.

    When I run this on an MBP running 10.5.5, I get:
    > sysctl hw.optional | awk -F ‘: ‘ ‘/64/ {print $2}’
    second level name optional in hw.optional is invalid
    1

    There is some error message being returned by the sysctl command. I updated it to:

    >sysctl hw.optional 2> /dev/null | awk -F ‘: ‘ ‘/64/ {print $2}’
    1

    That takes out the extra error line.


Leave a Reply