How to tell if a Linux system is big endian or little endian?
On a Big Endian-System (Solaris on SPARC)
$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6
0
On a little endian system (Linux on x86)
$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6
1
The solution above is clever and works great for Linux *86 and Solaris Sparc.
I needed a shell-only (no Perl) solution that also worked on AIX/Power and HPUX/Itanium. Unfortunately the last two don't play nice: AIX reports "6" and HPUX gives an empty line.
Using your solution, I was able to craft something that worked on all these Unix systems:
$ echo I | tr -d [:space:] | od -to2 | head -n1 | awk '{print $2}' | cut -c6
Regarding the Python solution someone posted, it does not work in Jython because the JVM treats everything as Big. If anyone can get it to work in Jython, please post!
Also, I found this, which explains the endianness of various platforms. Some hardware can operate in either mode depending on what the O/S selects: http://labs.hoffmanlabs.com/node/544
If you're going to use awk this line can be simplified to:
echo -n I | od -to2 | awk '{ print substr($2,6,1); exit}'
For small Linux boxes that don't have 'od' (say OpenWrt) then try 'hexdump':
echo -n I | hexdump -o | awk '{ print substr($2,6,1); exit}'
If you are on a fairly recent Linux machine (most anything after 2012) then lscpu
now contains this information:
$ lscpu | grep Endian
Byte Order: Little Endian
This was added to lscpu
in version 2.19, which is found in Fedora >= 17, CentOS >= 6.0, Ubuntu >= 12.04.
Note that I found this answer from this terrific answer on Unix.SE. That answer has a lot of relevant information, this post is just a summary of it.