3

Is there a way to backup the current display color and formatting such that I can echo some custom color and then reset things back to the previous state?

For example:

echo -e '\e[31m'123; echo 456

This will color 123 as red but also 456.

I can easily reset the display color and formatting like this:

echo -e '\e[31m'123'\033[0m'; echo 456

But I don't want to reset the display but rather restore it to whatever it was before I changed it.

This is an issue where I want to echo arbitrary string somewhere within a larger string. But the example I provided for resetting the output assumes that there is no existing formatting.

My shell environment is Bash and I'm using an xterm-256color compatible terminal emulator

2 Answers 2

1

For the (ANSI-compatible) example shown,

tput op

If you were using the 256-color feature to change the color palette, this extra step would help:

tput oc

The former refers to color-pairs (a feature of curses applications, for which these terminal descriptions are written: bash doesn't contribute to that activity, but can use the results). The latter refers to the original colors (before changing the palette).

In the terminal description, those are provided by building-blocks, e.g., xterm+256color:

xterm+256color|xterm 256-color feature,
        ccc,
        colors#0x100, pairs#0x10000,
        initc=\E]4;%p1%d;rgb\:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*
              %{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\E\\,
        oc=\E]104\007,
        setab=\E[%?%p1%{8}%<%t4%p1%d%e%p1%{16}%<%t10%p1%{8}%-%d%e48;
              5;%p1%d%;m,
        setaf=\E[%?%p1%{8}%<%t3%p1%d%e%p1%{16}%<%t9%p1%{8}%-%d%e38;5
              ;%p1%d%;m,
        setb@, setf@,

and xterm-basic:

xterm-basic|modern xterm terminal emulator - common,
        OTbs, am, bce, km, mir, msgr, xenl, AX, XT,
        colors#8, cols#80, it#8, lines#24, pairs#64,
        acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
        bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
        clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=\r,
        csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
        cud=\E[%p1%dB, cud1=\n, cuf=\E[%p1%dC, cuf1=\E[C,
        cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
        cvvis=\E[?12;25h, dch=\E[%p1%dP, dch1=\E[P, dim=\E[2m,
        dl=\E[%p1%dM, dl1=\E[M, ech=\E[%p1%dX, ed=\E[J, el=\E[K,
        el1=\E[1K, flash=\E[?5h$<100/>\E[?5l, home=\E[H,
        hpa=\E[%i%p1%dG, ht=^I, hts=\EH, ich=\E[%p1%d@,
        il=\E[%p1%dL, il1=\E[L, ind=\n, invis=\E[8m,
        is2=\E[!p\E[?3;4l\E[4l\E>, kmous=\E[M, meml=\El,
        memu=\Em, op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM,
        rmacs=\E(B, rmam=\E[?7l, rmir=\E[4l, rmkx=\E[?1l\E>,
        rmm=\E[?1034l, rmso=\E[27m, rmul=\E[24m, rs1=\Ec,
        rs2=\E[!p\E[?3;4l\E[4l\E>, sc=\E7, setab=\E[4%p1%dm,
        setaf=\E[3%p1%dm,
        setb=\E[4%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}
             %=%t3%e%p1%d%;m,
        setf=\E[3%?%p1%{1}%=%t4%e%p1%{3}%=%t6%e%p1%{4}%=%t1%e%p1%{6}
             %=%t3%e%p1%d%;m,
        sgr=%?%p9%t\E(0%e\E(B%;\E[0%?%p6%t;1%;%?%p5%t;2%;%?%p2%t;4%;
            %?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m,
        sgr0=\E(B\E[m, smacs=\E(0, smam=\E[?7h, smir=\E[4h,
        smkx=\E[?1h\E=, smm=\E[?1034h, smso=\E[7m, smul=\E[4m,
        tbc=\E[3g, vpa=\E[%i%p1%dd, E3=\E[3J, use=ecma+italics,
        use=ansi+pp, use=xterm+kbs, use=xterm+alt+title,
        use=ansi+enq,

which is rather long. The color-feature could be separated into a new building-block, e.g., xterm+color, but there has been no need for this yet. If you are using an xterm "compatible" terminal, it has a different, more appropriate terminal description as noted in the ncurses FAQ. Most of those ultimately derive from klone+color.

If you want to save the color settings, it's possible to write a script to obtain this information. There are several example scripts in xterm sources. Some of those work with xterm "compatible" terminals. If I were demonstrating the query-feature for "ANSI-compatible" colors, I would use dynamic.sh (ymmv: some terminals are less xterm-compatible than others).

0

Save it in a variable.

red='\e[31m'
reset='\033[0m'

printf '%b %b' "$red"123 "$reset"456

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .