How to write a binary file using Bash?

elcuco picture elcuco · Apr 7, 2011 · Viewed 41.8k times · Source

My problem is that I need to create a file with this exact bytes: 48, 00, 49, 00.

I cannot use C, perl, other scripting language (the target is an embedded device). I tried this using awk, and in desktop it does work:

# awk  'BEGIN{ printf "%c%c%c%c", 48, 00, 49, 00 }' | hexdump
0000000 0030 0031                              
0000004

However the target platform is running busybox v1.13.2 and this code does not work there. The awk version there does not output ascii "0" (all other values are ok).

What are your recommendations?

Answer

yohann.martineau picture yohann.martineau · Oct 5, 2011

you can use the following command:

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