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


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,
        colors#0x100, pairs#0x10000,
        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,
        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,
        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,

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).


Save it in a variable.


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 .