I have a Bash script that appends bytes written as hexadecimal values. I use echo
to write the bytes and it works
hex="1F"
byte="x$hex"
echo -en $byte >> "output.bin"
At some point I need to pad the file with a byte that could be anything from 00
to FF
. I want to specify the byte as a hex value and the total of repetitions.
I tried doing this with a for
loop but it just takes too long, especially since I need to add something like 65535 bytes sometimes.
byte="00"
total=65515
for (( i=1; i<=total; i++ ));
do
echo -en "x$byte" >> "output.bin"
done
I am looking for a more performant way that does not use a for
loop. At the moment I am stuck with something between printf
and echo
but instead of writing the values as binary it writes them as text
result=$(eval printf "x$byte%0.s" {1..$total})
echo -en $result >> "output.bin"
The result in the output file is 78 30 30
, which is the “x00” text, instead of 00
. If I directly use echo -en "x00" >> "output.bin"
, it does write one byte holding the 00
value and not the text “x00”. This is where I don’t know how to proceed in order to make it write the actual binary value