2

I'm trying to generate a string of 10 followed by a NULL (\0) followed by a 10.

However echo "10\010" does not seem to work (I'm guessing it generates two chars - a 10 followed by \010. I'm not sure how to separate / escape these values / characters?

I've also tried echo "10""/0""10" which had the same result.

I'm piping this output to a named pipe.

Chris Stryczynski
  • 5,178
  • 5
  • 40
  • 80

1 Answers1

2

In a POSIX compliant shell you could use:

echo '10\000010'

note that echo requires three octal numbers to follow a \0 to terminate an octal escape sequence.

The problem in your case is that bash is not POSIX compliant in this case as it does not implement XSI support that is required for a non-embedded UNIX variant.

bash however partially supports printf and so you could use:

printf '10\00010\n'
schily
  • 18,806
  • 5
  • 38
  • 60
  • I never knew this was in octal notation. Thanks for the informative answer! – Chris Stryczynski Aug 28 '18 at 09:26
  • `printf` also understands hexadecimal notation: `10\x0010`. – RudiC Aug 28 '18 at 09:48
  • No, `printf` in general does not understand hex notation. This only applies to `bash` and it's builtin command `printf`. – schily Aug 28 '18 at 09:51
  • 1
    The posix spec require up to three octal numbers **or less** [Write an 8-bit value that is the zero, one, two, or three-digit octal number num](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37_05). –  Aug 29 '18 at 20:18
  • bash echo expands backslash if called with the `-e` option **or** if the option `shopt -s xpg_echo` has been set. –  Aug 29 '18 at 20:22
  • The POSIX standard does not permit an `echo` implementation that honors a `-e` option and the POSIX standard requires to honor backslas escapes unless the shell has only been certified for embedded systems. BTW: it is possible to compile `bash` in a way that let it's `echo` implementation behave correctly. This is how Apple passed the POSIX certification. – schily Aug 30 '18 at 11:43