Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that certain processors are Big Endian and others are Little Endian. But is there a command, bash script, python script or series of commands that can be used at the command line to determine if a system is Big Endian or Little Endian? Something like:

if <some code> then
    echo Big Endian
    echo Little Endian

Or is it more simple to just determine what processor the system is using and go with that to determine its Endianess?

share|improve this question

10 Answers 10

up vote 82 down vote accepted

On a Big Endian-System (Solaris on SPARC)

$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6 


On a little endian system (Linux on x86)

$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6 


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:

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}'
share|improve this answer
That's an upper-case I (eye) rather than a lower-case l (ell) by the way. – Dennis Williamson Jul 23 '10 at 17:13
thanks for the hint Dennis. I reformatted it – krissi Jul 23 '10 at 19:44
(Solaris) -> (Solaris, Sparc), although Sparc >= V9 is bi endian. – Cristian Ciupitu Sep 17 '10 at 22:13
Care to explain how it works? – Massimo Apr 1 at 11:40

Here is a more elegant python one-line script

python -c "import sys;sys.exit(0 if sys.byteorder=='big' else 1)"

exit code 0 means big endian and 1 means little endian

or just change sys.exit to print for a printable output

share|improve this answer
This won't work on RHEL 5.x/CentOS 5.x systems which are running Python 2.4.x. Here's a fix: python -c "import sys;sys.exit(int(sys.byteorder!='big'))" – JPaget Jun 14 '14 at 23:55

This Python script should work for you:

#!/usr/bin/env python
from struct import pack
if pack('@h', 1) == pack('<h', 1):
    print "Little Endian"
    print "Big Endian"
share|improve this answer
One liner: python -c "from struct import pack;import sys;sys.exit(int(pack('@h',1)==pack('<h',1)))". The exit code is 0 for big endian and 1 for little endian. – Cristian Ciupitu Sep 17 '10 at 20:51
Ha! Pretty neat! Thanks guys! – Viet Oct 25 '12 at 10:47

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.

share|improve this answer

The main answer can be simplified slightly using awk:

On a Big Endian system (Solaris, SPARC)

$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'

On a Little Endian system (Linux, Intel)

$ echo -n I | od -to2 | awk 'FNR==1{ print substr($2,6,1)}'

Newer Linux Kernels

As of version 2.19 of the util-linux package the command lscpu started including a field related to Endianness. So now you can simply use this command to find this out:

$ lscpu | grep -i byte
Byte Order:            Little Endian

This has been confirmed on Ubuntu 12.10 and CentOS 6. So I would be willing to assume that most 3.0+ Linux Kernels are now offering this.

On Debian/Ubuntu systems you can also use this command, not sure of when it became available:

$ dpkg-architecture | grep -i end


share|improve this answer
python -c "import sys; print(sys.byteorder)"

It would print the endianess of the system.

share|improve this answer

I found a way to do it in Jython. Since Jython (Python on the JVM) runs on a VM, it always reports big endian, regardless of the hardware.

This solution works for Linux, Solaris, AIX, and HPUX. Have not tested on Windows:

    from java.lang import System
    for property, value in dict(System.getProperties()).items():
        if property.endswith('cpu.endian'):
            return value
share|improve this answer

You can take advantage of ELF file format to determine the endianness of your system. For example, print an arbitrary executable in hex:

head a.out, and look at the sixth byte in the first line.

0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............

In this example, it is 01, according to ELF format, 01 is little endian and 02 is big endian.

share|improve this answer

A single-line command based on ELF format:
hexdump -s 5 -n 1 /bin/sh

share|improve this answer
Edit: -n 1, sorry ;) – fae Apr 1 at 11:44
This is the exact same method as a previous answer, which also provided more details than yours. – kasperd Apr 1 at 12:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.