Passing binary data as arguments in bash Passing binary data as arguments in bash bash bash

Passing binary data as arguments in bash


Use the $'' quote style:

script $'\x02\xc5\xd8'

Test:

printf $'\x02\xc5\xd8' | hexdump -C00000000  02 c5 d8


script "`printf "\x02\xc5\xd8"`"script "`echo -e "\x02\xc5\xd8"`"

test:

# echo -n "`echo -e "\x02\xc5\xd8"`" | hexdump -C00000000  02 c5 d8                                          |...|


Bash is not good at dealing with binary data. I would recommend using base64 to encode it, and then decode it inside of the script.

Edited to provide an example:

script "$(printf '\x02\xc5\xd8' | base64 -)"

Inside of the script:

var=$(base64 -d -i <<<"$1")