Printing variable doesn't print \ | / -

ctomek

I am capturing Kafka startup script output into variable and while starting Kafka prints \ | / - to simulate spinning line. So when I do:

res=`./confluent start kafka`
echo $res

The result is:

kafka is [UP][UP] Starting kafka

But when I do:

echo $res > /tmp/res.txt

And open this file on Windows I get:

Starting zookeeper 
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\zookeeper is [[0;32mUP[0m] Starting kafka 
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-
\
|
/
-kafka is [[0;32mUP[0m]

When I open this file on Linux I get the sam result as with echo:

kafka is [UP][UP] Starting kafka

Why those characters are only visible on Windows?

cas

Try echo "$res" | hexdump -C or hexdump -C /tmp/res.txt - I'll bet you see lots of hex 0d bytes (carriage returns), one before or after each \, |, /, and -.

Why? A carriage-return without a line-feed returns the cursor to the beginning of the line, and anything printed after it overwrites what was already there.

Doing this is a common way to implement a text spinner at the beginning of a line (if done at the end of a line, the trick is to NOT print a LF or CR, and to use a Backspace character (^H, aka Ctrl-H, ASCII character 0x08) after each of the spinner characters, often with a small delay first.

Note: it's not uncommon to also see terminal control sequences like Erase Line (<ESC>[2K or hex 1b 5b 32 4b on a vt-100 compatible terminal) or Erase To End Of Line (ESC[K or hex 1b 5b 4b) near each carriage return. That's unlikely in this case, because they'd probably show up in some mangled form in the Windows editor.

In other words, the characters are in the output, they're just overwritten too fast for you to see. Your windows editor is probably "helpfully" converting the CRs to CR-LF.

BTW, unix uses LF only to end a line of text. Windows uses CR-LF. Old, pre-unix, Macs used to use CR only.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related