How to write a binary file using Bash?

POSIX AWK standard says that passing a 0 to AWK's printf with %c format can result in unspecified behaviour. However... POSIX echo also is very limited, and though octal and hexadecimal specifiers (and -n) will work on GNU echo and BASH built-in... They may not work everywhere. To maximize the chance that you get consistent behaviour on all POSIX systems, it is better to use the shell command line's printf than either of these.

$ printf '\060\000\061\000' | od -An -tx1
 30 00 31 00

This looks odd to me though... You may be wanting to output 0x48, 0x00, 0x49, 0x00 -- which looks like a pretty pilot number in octal:

$ printf '\110\000\111\000' | od -An -tx1
 48 00 49 00

you could try echo, that also allows arbitrary ascii chars (those numbers are octal numbers).

echo -n -e \\0060\\0000\\0061\\0000  | hexdump

you can use the following command:

echo -n -e \\x48\\x00\\x49\\x00 > myfile

Tags:

Bash

Awk

Busybox