od

od stands for “octal dump” but it is capable of much more than just octal processing. It processes data and represents it in any one of a variety of useful formats. When comparing the GNU and non-GNU implementations of factor for the next chapter, I used od to confirm that the original (non-GNU) implementation uses two “space” characters to pad the results, which it displays one per line, unlike GNU which displays them all on one line. At first, if you look at the output, it looks like a pair of spaces, but it’s impossible to tell if there’s any whitespace after the numbers, or exactly what form the padding takes. Piping this through od -a gives a fairly human-readable interpretation: 6 followed by a newline, then two spaces, a 2 and a newline, followed by two spaces, a 3 and a final newline.

factor 6
6
  2
  3
$ factor 6 | od -a
0000000   6  nl  sp  sp   2  nl  sp  sp   3  nl
0000012

Piping the same through od -x gives the same information in hexadecimal format. Because this is a little-endian architecture, the bytes are swapped around; the 0a36 represents 0x36=“6” then 0x0a=newline. The next byte-pair is two 0x20 characters, which are spaces. Then 0x32 is “2”, and 0x0a is newline again. 0x2020 is two more spaces, then 0x33 is “3”, and 0x0a is the final newline. The ascii(7) man page contains all of these ASCII characters with their decimal, hexadecimal, and octal representations.

factor 6 | od -x
0000000 0a36 2020 0a32 2020 0a33
0000012

There is also the -t “traditional” ...

Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.