2

I am trying understand how terminal work, doing different tricks, like writing from one tty to another, changing settings tty1 from tty2, etc. Also, I try this stuff - change color by escape sequence directly, from keyboard. That is, not through echo -e '\e[0;31m', but by input from keyboard. It doesn’t work.

I do this steps:

  1. Open tty1 and tty2
  2. In the tty2 put bash to the sleep mode, by sleep 10m. Typing word 'one'.

enter image description here

  1. Go to tty1 do echo -n ^[[0;31m > /dev/tty2, First character ^[ get this way Ctrl + v Esc
  2. Return to tty2, typing word 'two'. Yes - color was changed to red, from another tty.

enter image description here

  1. Repeat steps 3,4, but with green color and word 'three'

enter image description here

  1. And finally, trying send escape sequence directly from keyboard, not another tty, typing ^[[0;37m in the tty2. I do everything the same way - Esc (Ctrl + v doesn't needed, because readline is sleeping), then [0;37m, but get this:

enter image description here

Question: Why does it work this way? All characters the same, terminal state the same, but in one case terminal get escape sequence, and in another case don't.

  • The terminal state may or may not be the same; use stty to inspect the terminal file. Otherwise one difference is that sleep is not doing anything with the input, while the output from echo or external data is written to the terminal. – thrig Jun 7 '17 at 23:06
3

When you run echo, you're sending output to the terminal. The terminal interprets escape sequences such as the one to change colors in the output that is sent to it. These escape sequences are meant to be sent by applications, this is why they are recognized in the output that comes from the application running in the terminal. Usually the application running in the terminal is the one started by the terminal emulator (your shell) and the ones started by this in turn, but if you run echo … >/dev/tty2 then echo is effectively “running in the terminal” (in the sense that its output is going to the terminal, which is what matters here).

When you press Esc [ etc., you're sending input to the terminal. The terminal does not interpret escape sequences such as the one to change colors in the input that it receives. The terminal does interpret escape sequences in input, but for a completely different purpose: they're a way to encode function key presses.

How do keyboard input and text output work? has some relevant background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.