Skip to content

Instantly share code, notes, and snippets.

@JasonHewison
Last active May 18, 2021 00:42
Show Gist options
  • Save JasonHewison/d36f52bb2ee54208c8e894221aa5111b to your computer and use it in GitHub Desktop.
Save JasonHewison/d36f52bb2ee54208c8e894221aa5111b to your computer and use it in GitHub Desktop.
_color.sh | A Bash Color Function library
#!/usr/bin/env bash
__ansi_set_bold='1'
__ansi_set_dim='2'
__ansi_set_underlined='4'
__ansi_set_blink='5'
__ansi_set_inverted='7'
__ansi_set_hidden='8'
__ansi_reset_all='0'
__ansi_reset_bold='21'
__ansi_reset_dim='22'
__ansi_reset_underlined='24'
__ansi_reset_blink='25'
__ansi_reset_inverted='27'
__ansi_reset_hidden='28'
__ansi_color_default='39'
__ansi_color_black='30'
__ansi_color_red='31'
__ansi_color_green='32'
__ansi_color_yellow='33'
__ansi_color_blue='34'
__ansi_color_purple='35'
__ansi_color_cyan='36'
__ansi_color_lightgray='37'
__ansi_color_darkgray='90'
__ansi_color_lightred='91'
__ansi_color_lightgreen='92'
__ansi_color_lightyellow='93'
__ansi_color_lightblue='94'
__ansi_color_lightpurple='95'
__ansi_color_lightcyan='96'
__ansi_color_white='97'
__ansi_background_default='49'
__ansi_background_black='40'
__ansi_background_red='41'
__ansi_background_green='42'
__ansi_background_yellow='43'
__ansi_background_blue='44'
__ansi_background_purple='45'
__ansi_background_cyan='46'
__ansi_background_lightgray='47'
__ansi_background_darkgray='100'
__ansi_background_lightred='101'
__ansi_background_lightgreen='102'
__ansi_background_lightyellow='103'
__ansi_background_lightblue='104'
__ansi_background_lightpurple='105'
__ansi_background_lightcyan='106'
__ansi_background_white='107'
__ansi_weight_bold='1'
__ansi_weight_dim='2'
__ansi_weight_default='21;22'
set() {
ansi-parse 'set' $1
}
reset() {
ansi-parse 'reset' ${1:-all}
}
color() {
ansi-parse 'color' $1
}
background() {
ansi-parse 'background' $1
}
weight() {
ansi-parse 'weight' $1
}
bold() {
ansi-parse 'set' 'bold'
}
dim() {
ansi-parse 'set' 'dim'
}
underlined() {
ansi-parse 'set' 'underlined'
}
blink() {
ansi-parse 'set' 'blink'
}
inverted() {
ansi-parse 'set' 'inverted'
}
hidden() {
ansi-parse 'set' 'hidden'
}
ansi-parse() {
printf '\e['
local i="__ansi_$1_$2"
printf "${!i}"
printf "m"
}
# If the script is being run rather than sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
name="_$(color lightred)c$(color lightpurple)o$(color lightgreen)l$(color lightyellow)o$(color cyan)r$(color default).sh"
less -R << EndOfMessage
$(weight bold)$name$(reset)
└── $(underlined)$(color lightblue)Bash Color Functions library$(reset)
$(weight bold)$(underlined)1. Synopsis$(reset)
When sourced, the $name bash script provides your own bash
script with a variety of utility functions to give your terminal
output style.
$(weight bold)$(underlined)2. Installation$(reset)
To start using the $name functions you must $(color red)source$(color default) the script.
If $name is in the same folder as your script you can do
the following:
$(background lightgray)$(color black)DIR="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
$(background lightgray)$(color black)source \$DIR/_color.sh$(reset)
The $(color lightgreen)\$DIR$(color default) variable will be the current folder of your script, even
if it was called from a different folder.
$(weight bold)$(underlined)3. Functions$(reset)
$(underlined)3.1 color$(reset)
$(weight bold)SYNOPSIS$(weight default)
$(underlined)color$(reset underlined) <selected-colour>
$(weight bold)DESCRIPTION$(weight default)
The $(weight bold)color$(weight default) function changes the foreground colour of any
subsequently printed text
$(weight bold)AVAILABLE COLOURS$(weight default)
• $(color default)default$(color default)
• $(background white)$(color black)black$(color default)$(background default)
• $(color red)red$(color default)
• $(color green)green$(color default)
• $(color yellow)yellow$(color default)
• $(color blue)blue$(color default)
• $(color purple)purple$(color default)
• $(color cyan)cyan$(color default)
• $(color lightgray)lightgray$(color default)
• $(color darkgray)darkgray$(color default)
• $(color lightred)lightred$(color default)
• $(color lightgreen)lightgreen$(color default)
• $(color lightyellow)lightyellow$(color default)
• $(color lightblue)lightblue$(color default)
• $(color lightpurple)lightpurple$(color default)
• $(color lightcyan)lightcyan$(color default)
• $(background black)$(color white)white$(color default)$(background default)
$(weight bold)EXAMPLE$(weight default)
$(background lightgray)$(color black)echo \$"\$(color red)red \$(color green)green \$(color blue)blue \$(color default)default"$(reset)
$(color red)red $(color green)green $(color blue)blue $(color default)default$(reset)
$(underlined)3.2 background$(reset)
$(weight bold)SYNOPSIS$(weight default)
$(underlined)background$(reset underlined) <selected-colour>
$(weight bold)DESCRIPTION$(weight default)
The $(weight bold)background$(weight default) function changes the background colour of any
subsequently printed text
$(weight bold)AVAILABLE COLOURS$(weight default)
• $(background default)default$(background default)
• $(background black)black$(background default)
• $(background red)red$(background default)
• $(background green)green$(background default)
• $(color darkgray)$(background yellow)yellow$(background default)$(color default)
• $(background blue)blue$(background default)
• $(background purple)purple$(background default)
• $(background cyan)cyan$(background default)
• $(color darkgray)$(background lightgray)lightgray$(background default)$(color default)
• $(background darkgray)darkgray$(background default)
• $(background lightred)lightred$(background default)
• $(background lightgreen)lightgreen$(background default)
• $(background lightyellow)lightyellow$(background default)
• $(background lightblue)lightblue$(background default)
• $(background lightpurple)lightpurple$(background default)
• $(background lightcyan)lightcyan$(background default)
• $(background white)white$(background default)
$(weight bold)EXAMPLE$(weight default)
$(background lightgray)$(color black)echo \$"\$(background red)red \$(background green)green \$(background blue)blue \$(background default)default"$(reset)
$(background red)red $(background green)green $(background blue)blue $(background default)default$(reset)
$(underlined)3.3 weight$(reset)
$(weight bold)SYNOPSIS$(weight default)
$(underlined)weight$(reset underlined) <selected-weight>
$(weight bold)DESCRIPTION$(weight default)
The $(weight bold)weight$(weight default) function changes the weight of any
subsequently printed text
$(weight bold)NOTE$(weight default): Dim is not supported in all terminals.
$(weight bold)AVAILABLE WEIGHTS$(weight default)
• $(weight default)default$(weight default)
• $(weight dim)dim$(weight default)
• $(weight bold)bold$(weight default)
$(weight bold)EXAMPLE$(weight default)
$(background lightgray)$(color black)echo \$"\$(weight default)default \$(weight dim)dim \$(weight bold)bold"$(reset)
$(weight default)default $(weight dim)dim $(weight bold)bold$(reset)
$(underlined)3.4 set / reset$(reset)
$(weight bold)SYNOPSIS$(weight default)
$(underlined)set$(reset underlined) <option>
$(underlined)reset$(reset underlined) [<option>]
$(weight bold)DESCRIPTION$(weight default)
The $(weight bold)set$(weight default) and $(weight bold)reset$(weight default) functions apply and cancel the given
options effects.
When the $(weight bold)reset$(weight default) function has no arguments it defaults to "all"
and resets all options as well as foreground and background
colours.
$(weight bold)AVAILABLE OPTIONS$(weight default)
• all // reset only
• $(set bold)bold$(reset bold)
• $(set dim)dim$(reset dim)
• $(set underlined)underlined$(reset underlined)
• $(set blink)blink$(reset blink)
• $(set inverted)inverted$(reset inverted)
• $(set hidden)hidden$(reset hidden)
$(weight bold)EXAMPLE$(weight default)
$(background lightgray)$(color black)echo \$"\$(set underlined)underlined \$(set bold)bold \$(set inverted)inverted"$(reset)
$(set underlined)underlined $(set bold)bold $(set inverted)inverted$(reset)
$(underlined)3.5 Shorthand functions$(reset)
$(weight bold)DESCRIPTION$(weight default)
The following functions are shorthand for the functions
listed above.
$(weight bold)AVAILABLE SHORTHAND$(weight default)
• $(bold)bold$(reset bold)
• $(dim)dim$(reset dim)
• $(underlined)underlined$(reset underlined)
• $(blink)blink$(reset blink)
• $(inverted)inverted$(reset inverted)
• $(hidden)hidden$(reset hidden)
$(weight bold)EXAMPLE$(weight default)
$(background lightgray)$(color black)echo \$"\$(underlined)underlined \$(bold)bold \$(inverted)inverted"$(reset)
$(underlined)underlined $(bold)bold $(inverted)inverted$(reset)
$(weight bold)$(underlined)4. Motivation$(reset)
I frequently have to copy colour variables between bash script
files, or have a shared script that gets sourced containing them.
These functions are attempt to create a more standardised toolkit
that will hopefully make all of the output options easier to
remember and make my scripts look nicer.
EndOfMessage
fi

_color.sh

Bash Color Functions library

Synopsis

When sourced, the _color.sh bash script provides your own bash script with a variety of utility functions to give your terminal output style.

Installation

To install _color.sh run the following:

curl https://gist.githubusercontent.com/JasonHewison/d36f52bb2ee54208c8e894221aa5111b/raw/34c95e6f342cafa2eda48ba9ed0cc7e4ec36022a/_color.sh --output _color.sh
chmod +x _color.sh

NOTE: You can execute the script at any time to see all of the same documentation that is available in this gist by running ./_color.sh

To start using the _color.sh functions you must source the script.

If _color.sh is in the same folder as your script you can do the following:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $DIR/_color.sh

The $DIR variable will be the current folder of your script, even if it was called from a different folder.

Functions

color

color <selected-colour>

The color function changes the foreground colour of any subsequently printed text.

The following colours are available:

  • default
  • black
  • red
  • green
  • yellow
  • blue
  • purple
  • cyan
  • lightgray
  • darkgray
  • lightred
  • lightgreen
  • lightyellow
  • lightblue
  • lightpurple
  • lightcyan
  • white

e.g.

echo $"$(color red)red $(color green)green $(color blue)blue $(color default)default"

background

background <selected-colour>

The background function changes the background colour of any subsequently printed text

The following colours are available:

  • default
  • black
  • red
  • green
  • yellow
  • blue
  • purple
  • cyan
  • lightgray
  • darkgray
  • lightred
  • lightgreen
  • lightyellow
  • lightblue
  • lightpurple
  • lightcyan
  • white

e.g.

echo $"$(background red)red $(background green)green $(background blue)blue $(background default)default"

weight

weight <selected-weight>

The weight function changes the weight of any subsequently printed text

NOTE: Dim is not supported in all terminals.

The following weights are available.

  • default
  • bold
  • dim

e.g.

echo $"$(weight default)default $(weight dim)dim $(weight bold)bold"

set / reset

set <option> reset [<option>]

The set and reset functions apply and cancel the given options effects.

When the reset function has no arguments it defaults to "all" and resets all options as well as foreground and background colours.

The following options are available:

  • all (reset only)
  • bold
  • dim
  • underlined
  • blink
  • inverted
  • hidden

e.g.

echo $"$(set underlined)underlined$(result underlined) $(set bold)bold$(reset bold) $(inverted)inverted$(reset inverted)"

Shorthand functions

The following functions are shorthand for the functions listed above:

  • bold
  • dim
  • underlined
  • blink
  • inverted
  • hidden

e.g.

echo $"$(underlined)underlined $(bold)bold $(inverted)inverted$(reset)"

Motivation

I frequently have to copy colour variables between bash script files, or have a shared script that gets sourced containing them.

These functions are attempt to create a more standardised toolkit that will hopefully make all of the output options easier to remember and make my scripts look nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment