jozxyqk
jozxyqk

Reputation: 17256

Invisible terminal output is breaking a script

This is a continuation from here where I catpure all output to a terminal. This script ran fine on my machine, but on another I was hearing extra stuff when piping to espeak and not when writing to a file or printing it. This was a bit of a pain to debug but with a little trial and error, I think narrowed it down to ~/.bashrc->/etc/bashrc->/etc/profiles.d/vte.sh doing something which I think is setting PROMPT_COMMAND="__vte_prompt_command". At least I thought it was but PROMPT_COMMAND= doesn't fix after sourcing vte.sh.

At any rate, it appears some special characters are printed to change the window title (printf "\033]0;%s@%s:%s\007%s"... maybe?). These characters don't appear when writing to a file or as output in the terminal, but do when piping to apps such as espeak.

My first question is, how should I have detected the existence of these special characters? (I only found vte by trial and error, which seems pretty slow)

while read line
do
    espeak "$line" #can hear extra stuff
    echo "GOT: [$line]" #only expected output printed
done <input_pipe

My next question is, how can I ignore any invisible/special output in my loop? (and get only the caracters I would ultimately see in the terminal)

Upvotes: 0

Views: 113

Answers (1)

konsolebox
konsolebox

Reputation: 75458

You can try removing those characters with special parameter expansion:

line=${line//stringtodelete}
line=${line//[charstodelete]}

Try to run echo -n "$line" | hexdump -C to see the hex values of those characters.

Then you can do something like

$'\x01\x02abc'

Notes:

  • Bash replaces \[ and \] in PS1 with $'\x01' and $'\x02' respectively but I'm not sure if they're sent on actual output.
  • $'\x##' is hex form and $'\000' is oct form.

Upvotes: 1

Related Questions