Reputation: 23
I have a chat script which is using
tail -f chatlog.txt
to display the chat. The messages are written so that when you echo it, it outputs as colored text.
chatlog.txt:
20:39 \033[4;33musername\033[0m: so with all of my experience
20:39 \033[4;33musername\033[0m: we shall prevail
20:40 \033[4;33musername\033[0m: the taxi jobs are very
20:40 \033[4;33musername\033[0m: yes
21:02 \033[4;34mJacob\033[0m has joined the chat!
if I display using this code it works fine:
var=$(tail chatlog.txt)
echo -e "$var"
But if I display it with tail -f chatlog.txt
there is no color. I have tried other solutions but none seemed to work.
Upvotes: 0
Views: 185
Reputation: 295383
Your output contains literal escape sequences; thus, all you need is a program which will recognize those and replace them with the characters they refer to. In POSIX-compliant shells, printf %b
will perform this operation.
Thus:
tail -f chatlog.txt | while IFS= read -r line; do printf '%b\n' "$line"; done
See BashFAQ #1 for a general discussion of the while read
mechanism. To call out some of the important points:
IFS=
prevents leading and trailing whitespace from being trimmed from lines.-r
argument to read
prevents literal backslashes from being removed by read
.Upvotes: 1