I need to supply arbitrary bytes for an argv argument of a C program.

However, if the bytes are put within double-quotes, the zero-bytes are removed as can be seen below:

v="$(python -c 'import sys; sys.stdout.write("ABC\x18\x10\x00\x00\x10")')"
echo -n $v | hexdump -C
00000000  41 42 43 18 10 10                                 |ABC...|

Indeed, the double-quotes remove the zero-bytes, since hexdump shows them in its output:

python -c 'import sys; sys.stdout.write("ABC\x18\x10\x00\x00\x10")' | hexdump -C
00000000  41 42 43 18 10 00 00 10                           |ABC.....|

How can I supply these arbitrary bytes as program arguments?


I need to supply arbitrary bytes for an argv argument of a C program.

You cannot do that. The argv arguments are, by definition, zero-terminated strings, so they cannot contain any NUL byte. You'll have to pass the data to your program via a pipe, file, shared memory, etc, or do some escaping and unescaping.

Even if bash was supporting NUL-bytes in its strings (as zsh, perl or python do), there would be no way to pass those strings to other programs via command line arguments or environment variables.

  • What escaping could be done? – Shuzheng Feb 15 at 16:07
  • For instance, I have a C program where the user can specify via the command line the address of an abstract unix socket. Since such an adress starts with a NUL byte and can contain other NUL bytes, I use the convention that a @ means a null byte, and a backslash escapes the next char, so \@ for a literal @. That's very easy to reverse in place. You can come with your own convention -- for instance, treat binary data as iso-8859-1 and translate it to modified utf-8, where the null byte is encoded as \xc0\x80 ;-) – mosvy Feb 15 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.