My question is raised from two different observations.
In cat, when I press ESC or other control keys, Bash uses caret notation to represent them. The figure tells I pressed ESC key, but Bash shows it as ^[.

Nevertheless, if I want to write colorful text in scripts, by echo for instance, it seems I have to use ANSI escape sequence.

The figure shows that caret notation does not work in echo, but ANSI escape sequence does.
Can I say that we must use ANSI escape sequence to enter ESC key, but Bash will use caret notation to represent it?
Is there a way to enter ESC key by caret notation? Can Bash represent an entered key in ANSI escape sequence?
Note I'm actually using ZSH. You are fine to talk about either shell.
If you type
$ echo 'CTRL+VESC[31mhello'
and when done it will show you
$ echo '^[[31mhello'
You probably need set -o vi in bash.
However, the best way to do that would be to use tput:
$ tput setaf 9; echo hello; tput sgr0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With