I have a very simple chat-like tool that runs within a GNU screen
session. The screen
window is split, the top part is running tail -f file.txt
and the bottom part is running a script with the following content:
#!/bin/bash
while : ; do
read -p "Message: " msg
ctime=$(date +"%H:%M:%S")
echo "[${ctime}] User: ${msg}" >> file.txt
done
Very simple, but gets the job done with the requirements I have. There's only one problem: When I press the ESC or any of the arrow keys, it inserts an escape-sequence, like ^[[D
for example. And this messes up the file, resulting in terrible output.
So my question is simple: How can I escape the input from read
so it's safe to write to the file?
I've tried echo "[${ctime}] User: ${msg}" | strings >> file.txt
which made it a lot better, there were no big mess-ups anymore (e.g. nothing was overwritten or wrongly put out), but things are still not perfect (e.g. entering te^[[Dst
would turn into te\n[Dst
(the \n
being an actual new line)).