Skip to content
Advertisement

TCL Expect is injecting an extra null character to stdout

It seems that expect is adding an extra null character or something to stdout. For example:

$ expect -c 'spawn -noecho echo xyz; expect eof' | wc -c
5
$ echo xyz | wc -c
4

What is this extra character and how do I get rid of it?

Advertisement

Answer

It’s not a null character. You can use a tool like xxd to see exactly what output expect is producing:

$ expect -c 'spawn -noecho echo xyz; expect eof' | xxd
00000000: 7879 7a0d 0a                             xyz..

This shows that you are getting a standard CR/LF end-of-line terminator. I’m guessing this is being produced because the stdout of your echo process is attached to a tty device, which often involves additional filtering to modify end-of-line characters and so forth (see stty for details).

You can disable this behavior by putting the tty in raw mode, like this:

$ expect -c 'set stty_init raw; spawn -noecho echo xyz; expect eof' |   wc -c
4
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement