Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI escape sequences, how to enter escape character from keyboard? [closed]

Problem:

  • I recently found a page on changing the text colour in bash when echoing that was really helpful as it explained how to enter the escape as a single character through a sequence of key strokes. Unfortunately it was a school computer and the history was not saved. I have since not been able to find that page.

Question:

  • If anyone knows the sequence of keystrokes to enter the escape character ^[ it would be much appreciated.
like image 939
Ambiguities Avatar asked Nov 13 '22 01:11

Ambiguities


1 Answers

The escape key can be generated with the backslash sequence \e in a command which interprets backslash sequences.

eg:

echo -e "Text can be \e[1;41mred\e[m or \e[1;45mmagenta\e[m"
for back in {30..37}; do
  echo -n $back:
  for fore in {40..47}; do
    printf "\e[1;%2d;%2dm%2d\e[m " $back $fore $fore
  done
  printf "\n"
done

A possibility not illustrated above is the bash escape interpreted string: $'\e[1m'.


Although using vt escape sequences (as above) is easy and supported by most if not all commonly-used unix terminal emulators, die-hards will insist that you learn to use the tput command:

 printf "Here is a %sbold red%s word\n" "$(tput bold)""$(tput setf 4)" "$(tput sgr0)"

IMHO, figuring out the magic tput symbols (see man 5 terminfo on a debian/ubuntu system) is not as easy as looking up the xterm control sequences (google the last three words), but YMMV.

like image 157
rici Avatar answered Nov 14 '22 23:11

rici