Home > Enterprise >  How to set my terminal's foreground / background temporarily via bash scripts?
How to set my terminal's foreground / background temporarily via bash scripts?

Time:12-29

My goal is to be able to colorize (or set underlining, etc) terminal window output from any source (I figure if ls works, all the rest will too) on an as-needed / as-desired basis via command line scripts. ...It's a given here that SOME programs may very well override this in their output, and insofar as that's an easy to address aspect of this question, that's in there, too - as in, I'd sort of like to know how that may pertain to any answer(s).

I already understand a heck of a lot of the background information as I use color in my shell scripts now. The issue is they don't persist when their shell exits; my focus in this question is on making functional a set of a few command line utilities that can let the user select from a set of known named colors, like Blue or Red, or even just give the correct code to do that, and leave all output to that terminal window in that situation until a reset is given, via a command line script, though surely if another command utility, like 'reset' were to reset it, so be it!

I believe this is likely possible through use of what for me at least are rarely touched elements of the environment like PS1.

I've touched that stuff so rarely, I really don't know what I'm doing there!

CodePudding user response:

On Linux, if you are using a scrolled terminal that responds to the SGR standard, colours instructions sent to that display stream will remain (persist) until you flush that display stream with the "clear" command.

The execution of the below script will illustrate that with one set of FG/BG colours, then the terminal defaults, then another set of FG/BG colours:

#!/bin/sh

### Useful References:
###     https://www.ecma-international.org/publications-and-standards/standards/ecma-48/
###     https://en.wikipedia.org/wiki/ANSI_escape_code
###
###     https://www.htmlcsscolor.com/hex/FFFF80
###     https://www.w3schools.com/colors/colors_picker.asp?colorhex=FFFF00
###     https://www.colorabout.com/color/hex/ffff80/
###     https://www.color-name.com/hex/ffff80
###     https://www.cpcwiki.eu/index.php/CPC_Palette
###     https://hexcolor.co/hex/ffff80
###     https://www.colorxs.com/color/hex-ffff80
###     https://encycolorpedia.com/ffff80
###     https://www.color-hex.com/color/ffff80
###     https://www.colorcodehex.com/ffff80.html
###     http://html-color.org/FFFF80

cat >sample_text.txt <<"EnDoFiNpUt"
The original specification only had 8 colors, and just gave them names. The SGR parameters 30–37 selected the foreground color, while 40–47 selected the background. Quite a few terminals implemented "bold" (SGR code 1) as a brighter color rather than a different font, thus providing 8 additional foreground colors. Usually you could not get these as background colors, though sometimes inverse video (SGR code 7) would allow that.
EnDoFiNpUt

    FG_color="\e[91m"       ### Red
    BG_color="\e[47m"       ### Gray
    echo "${FG_color}${BG_color}" 
    cat sample_text.txt 

    echo "\e[0m"            ### Environment Default
    cat sample_text.txt 

    FG_color="\e[30m"       ### Black
    BG_color="\e[102m"      ### Green
    echo "${FG_color}${BG_color}" 
    cat sample_text.txt 
    echo "\e[0m"

That will appear as follows (on my terminal):

enter image description here

  • Related