Manipulating Binary Data with Bash

Bit Trip

© Lead Image © Yewkeo, 123RF.com

© Lead Image © Yewkeo, 123RF.com

Author(s):

Bash is known for admin utilities and text manipulation tools, but the venerable command shell included with most Linux systems also has some powerful commands for manipulating binary data.

One of the most versatile scripting environments available on Linux is the Bash shell. The core functionality of Bash includes many mechanisms for tasks such as string processing, mathematical computation, data I/O, and process management. When you couple Bash with the countless command-line utilities available for everything from image processing to virtual machine (VM) management, you have a very powerful scripting platform.

One thing that Bash is not generally known for is its ability to process data at the bit level; however, the Bash shell contains several powerful commands that allow you to manipulate and edit binary data. This article describes some of these binary commands and shows them at work in some practical situations.

Viewing and Converting Data

Two tools you can use to represent data in hexadecimal and binary format are hexdump and xxd. The hexdump utility provides many options for outputting hexadecimal data. Although xxd lacks some of the options available with hexdump, it does have one key feature hexdump lacks: In addition to letting you output hexadecimal values, xxd also has the ability to convert a hexadecimal string into binary data. The following command:

[...]