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.

  • please add what should be the expected output – Bharat Aug 28 '18 at 9:08
  • 1
    His question is obvious, see my answer. Don't care the uninformed people that downvoted it... – schily Aug 28 '18 at 9:20
  • The expected output is described in the first sentence. Not sure how else to 'present' the value otherwise. – Chris Stryczynski Aug 28 '18 at 9:25
  • 1
    Well you could have spelled correctly. (-: – JdeBP Aug 29 '18 at 0:09

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'
  • I never knew this was in octal notation. Thanks for the informative answer! – Chris Stryczynski Aug 28 '18 at 9:26
  • printf also understands hexadecimal notation: 10\x0010. – RudiC Aug 28 '18 at 9: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 9: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. – ImHere 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. – ImHere Aug 29 '18 at 20:22

Better use printf Why is printf better than echo? for many reasons:

$ printf '%b' 10 \\0 10

Or printf '%b' 10 '\0' 10 (and using od to see the byte values (or sed l or cat -A):

$ printf '%b' 10 '\0' 10 | od -An -c
   1   0  \0   1   0

Note that the string has no trailing \n (newline), as you requested.
If a newline is required, use:

$ printf '%b' 10 \\0 10 '\n' | sed l

I'm not sure how to separate / escape these values / characters?

The only safe way to separate the characters is to put them in separate arguments.

But using echo is complex and prone to failure as there are several versions of echo.

In bash (your tag in the question), echo needs the -e option to make it understand backslash values (and to see the byte values use od (or sed l or cat -A):

$ echo -e 10 '\0' 10 | od -An -c
   1   0      \0       1   0  \n

That works in bash, zsh, ksh and (GNU) /bin/echo (at least).

But echo is including one space character (ASCII character 32 dec (20 in hex 40 in octal)) that needs to be removed.

$ echo -e 10 '\0' 10 | tr -d ' ' | od -An -c
   1   0  \0   1   0  \n

And, the -e option is not interpreted by some implementations of echo (like dash):

$ dash -c 'echo -e 10 \\0 10'
-e 10  10

And even the backslash is not interpreted by some echo:

$ echo -e 10 '\0' 10
-e 10 \0 10 

And even if the POSIX spec define the backslash octal numbers being only up to three digits (that start with a zero):

Write an 8-bit value that is the zero, one, two, or three-digit octal number num.

$ zsh -c 'echo -e h \\00044 h | od -An -tx1c'
  68  20  04  34  20  68  0a
   h     004   4       h  \n

Less digits are also accepted:

$ zsh -c 'echo -e h \\4 h | od -An -tx1c'
  68  20  04  20  68  0a
   h     004       h  \n

And that is an interpretation, and, as is usual, any interpretation is a source for misunderstandings.

So, in short, avoid echo and use printf.

And even if printf may also interpret up to four octal digits (with the \b option) there could not be any misinterpretation if each value is a separate argument:

$ printf '%b' 10 '\55' 10 '\n'
  • printf does not really help. There are several implementations that do not handle \0 correctly and printf from bash expands \xnn sequences and thus is in conflict with POSIX. – schily Aug 30 '18 at 11:47
  • @schily In which present-day POSIX compatible systems is printf failing? – ImHere Aug 31 '18 at 6:32
  • This is a hard to answer question since you did not specify what kind of printf implementation you like to check. there are various shells with builtin printf and there are stand alone versions. A not complete list of problems is here: in-ulm.de/~mascheck/various/echo+printf – schily Aug 31 '18 at 13:19
  • In that page, no implementation fail with POSIX '%b'. Your claim is: several implementations that do not handle \0 correctly . Please provide clear evidence or I'll believe that you are just generating noise and distraction. @schily – ImHere Aug 31 '18 at 23:26
  • The problem with that table is that it does not check whether the coded nul byte (\0) is transferred and whether charactes after a nul byte in the %b argument are printed. You may like to check this with various shells. Also note that ksh93 implements %b in a very funny way in case that a field with is specified. – schily Sep 1 '18 at 11:11

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.