Search code examples
shellnullmakefileescaping

How to escape a NULL byte as an argument to a shell command inside a Makefile


Inside a Makefile I run a shell command which I want to pass a NULL byte as argument. The following attempt fails:

echo $(shell /bin/echo -n $$'\x00' | ruby -e "puts STDIN.read.inspect")

It generates:

echo "$\\x00"

Instead I expected:

echo "\u0000"

How do I properly escape such a NULL byte?


Solution

  • Due to the execve(2) semantics it is not possible to pass a string containing a null byte as argument. Each argument string is terminated by null byte, therefore making it impossible to distinguish between the contained null byte and the end of the string.