2

I'm trying to enable reverse video on bash / xterm such that the text is black but the foreground is white. But it's not working. Here's my command:

echo -n -e \\x1B\\x07maaa\\x1B[m

Any ideas why it's not behaving as expected?

neubert
  • 15,947
  • 24
  • 120
  • 212

4 Answers4

5

In the example

echo -n -e \\x1B\\x07maaa\\x1B[m

the \\x07m cannot be correct, since that would be an ASCII BEL. Perhaps you meant something like this:

echo -n -e \\x1B[7maaa\\x1B[m

The \\x1B is the escape character. All of the other characters are from the printable-ASCII range. This assumes that your echo interprets escapes in that manner (bash's does).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
2

Another way:

echo -n -e "\e[07mTEXT\e[0m"

Or:

printf "\e[07mTEXT\e[0m"
Jahid
  • 21,542
  • 10
  • 90
  • 108
1

More portable are

tput rev

for reverse mode and

tput sgr0

to clear reverse mode.

msw
  • 42,753
  • 9
  • 87
  • 112
  • true, but the question *was* about escape sequences – Thomas Dickey May 03 '15 at 16:55
  • I'm trying to make some updates to a vt100 terminal emulator so understanding how the ANSI escape codes work (vs. how tput works) is very beneficial. None-the-less, I was unaware of `tput`. – neubert May 03 '15 at 22:56
1

It will be far simpler to use bash's printf command (and as Thomas Dickey pointed out, you're using the wrong sequence to enable inverse video). Asking yourself how many backslashes you need to stack up is almost always the wrong question.

# \e[7m starts inverse video
# \e[m (short for \e[0m) resets all video parameters to their default values
printf '\e[7maaa\e[m'
chepner
  • 497,756
  • 71
  • 530
  • 681