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.

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

1 Answer 1

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'
6
  • I never knew this was in octal notation. Thanks for the informative answer! Aug 28, 2018 at 9:26
  • printf also understands hexadecimal notation: 10\x0010.
    – RudiC
    Aug 28, 2018 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, 2018 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.
    – user232326
    Aug 29, 2018 at 20:18
  • bash echo expands backslash if called with the -e option or if the option shopt -s xpg_echo has been set.
    – user232326
    Aug 29, 2018 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .