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
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:
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.
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.
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.