2

I need to find my OS (not hardware) is 32-bit / 64-bit. Which command is best?

  • uname -p
  • uname -i
  • uname -m
  • arch

All the above commands returns the same answer:

  • On 32 bit systems: i686/i386
  • On 64 bit systems: x86_64
slm
  • 369,824
Ram
  • 1,041
  • 1
    See my answer to this topic in this Q&A: http://unix.stackexchange.com/questions/77718/32-bit-64-bit-cpu-op-mode-on-linux, I already extensively covered all the methods for determining what you're asking about there. – slm Sep 02 '13 at 06:14
  • 1
    Note that it's usually a good idea to wait at least a day before accepting an answer, to give people from different time zones a chance to answer. By accepting an answer, you are effectively saying "this question has been answered to my satisfaction"; questions with accepted answers tend to receive much less attention from the community, potentially depriving you of even better answers than it has received at that point. – user Sep 02 '13 at 07:26

3 Answers3

5

I would recommend instead using getconf LONG_BIT.

[root@mymachine ~]# getconf LONG_BIT
64

This will clearly output either 32 or 64, depending on your installed kernel, whereas uname -m (and etc.) indicate the underlying hardware name.

See also the Stack Overflow question How to determine whether a given Linux is 32 bit or 64 bit?, but be sure to read the helpful commentary.

3

You're setting yourself up for future problems, framing the question that way.

4 decades of Unix wisdom tell us that the best path to portability is to test for specific features, not entire platforms.

Certainly you have in mind some specific distinction between 32- and 64-bit systems, but I assure you, this difference does not hold universally. There are many kinds of 64-bit. (And many kinds of 32-bit.)

You've already limited yourself to Linux, both by the tag on the question, but also by the way you ask the question. But, are you certain this script you are developing will never need to run on OS X, one of the BSDs, or even a "real" Unix system?

  • arch is a particularly poor method. It tells you about CPU types, not OS word sizes, and even then can be misleading. Under OS X on Intel, for example, it tells you i386, even on a 64-bit system. It isn't even available on all Linuxes.

  • uname -m and uname -p also tells you about the CPU type, rather than the OS's native word sizes.

  • uname -i isn't portable, and even on Linux, it can print "unknown".

The right answer depends on why you want to know. If it is because you are developing C software that needs to do the right thing on 64-bit, then you need to write a program that probes the behavior:

#include <stdio.h>

int main() {
    printf("long long = %d bits\n", sizeof(long long) * 8);
    printf("long = %d bits\n", sizeof(long) * 8);
    printf("void* = %d bits\n", sizeof(void*) * 8);
    printf("size_t = %d bits\n", sizeof(size_t) * 8);
    printf("int = %d bits\n", sizeof(int) * 8);

    return 0;
}

That program's output will vary among systems that supposedly share the same native word size. It will even vary when run on the same physical hardware, but under different operating systems.

Warren Young
  • 72,032
  • My application is supported only for 64 bit version. I need to write a shell script which should check the OS architecture if OS is 64 bit my application should start otherwise a validation message should be shown. – Ram Sep 02 '13 at 05:35
  • @Ram: If your software will run on a 32-bit system, but somehow requires a 64-bit system, you can test like I've shown above. If it won't even run on a 32-bit system, why do you need the test? Won't its failure to run be diagnostic enough? Most package managers will refuse to even install a CPU-mismatched package. – Warren Young Sep 02 '13 at 05:38
  • Thanks Warren Young. i understood your point. But My requirement is to check it in shell script. My application is started with a shell script if i allow to run in 32 bit machine it shows some errors . Users of that application is asking us to provide a proper validation message . – Ram Sep 02 '13 at 05:43
  • 1
    @Ram: Why are you rejecting the package manager solution? If your users cannot even install the CPU-mismatched package, problem solved. – Warren Young Sep 02 '13 at 05:48
1

My application is supported only for 64 bit version. I need to write a shell script which should check the OS architecture if OS is 64 bit my application should start otherwise a validation message should be shown.

In that case, what I would do is to write a tiny, dummy application. Something like this:

int main(int argc, char **argv)
{
    return 0;
}

Compile and link it the same way that you do your main application. Then write a shell script like the following:

#!/bin/sh
if ! ./test-platform-compatibility 2>&1 >/dev/null
then
  echo I refuse to run on anything but 64-bit!
  exit 1
fi
# launch your application here

What this effectively does is ensure that the kind of executable you provide can run on the user's system. This is similar to Warren Young's suggestion of "testing for capabilities": in this case, you are testing for the capability to execute the application's binary format.

user
  • 28,901