bash-colors icon indicating copy to clipboard operation
bash-colors copied to clipboard

Bash function to easily color/format text.

Bash Colors

πŸ™ˆ KISS: See kiss-colors.sh for a simpler version πŸ˜‰

Usage

$(c <flags>) inside an echo -e or printf
Or $(c) to reset

cecho <flags> <text> is a shortcut for echo -e "$(c <flags>)<text>$(c)"

Flags

 β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
 β”‚ Fg/Bg β”‚ Color          β”‚ Octal           β”‚   β”‚ Code  β”‚ Style           β”‚ Octal β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
 β”‚  K/k  β”‚ Black          β”‚ \e[ + 3/4  + 0m β”‚   β”‚  s/S  β”‚ Bold (strong)   β”‚ \e[1m β”‚
 β”‚  R/r  β”‚ Red            β”‚ \e[ + 3/4  + 1m β”‚   β”‚  d/D  β”‚ Dim             β”‚ \e[2m β”‚
 β”‚  G/g  β”‚ Green          β”‚ \e[ + 3/4  + 2m β”‚   β”‚  i/I  β”‚ Italic          β”‚ \e[3m β”‚
 β”‚  Y/y  β”‚ Yellow         β”‚ \e[ + 3/4  + 3m β”‚   β”‚  u/U  β”‚ Underline       β”‚ \e[4m β”‚
 β”‚  B/b  β”‚ Blue           β”‚ \e[ + 3/4  + 4m β”‚   β”‚  f/F  β”‚ Blink (flash)   β”‚ \e[5m β”‚
 β”‚  M/m  β”‚ Magenta        β”‚ \e[ + 3/4  + 5m β”‚   β”‚  n/N  β”‚ Negative        β”‚ \e[7m β”‚
 β”‚  C/c  β”‚ Cyan           β”‚ \e[ + 3/4  + 6m β”‚   β”‚  h/H  β”‚ Hidden          β”‚ \e[8m β”‚
 β”‚  W/w  β”‚ White          β”‚ \e[ + 3/4  + 7m β”‚   β”‚  t/T  β”‚ Strikethrough   β”‚ \e[9m β”‚
 β”œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
 β”‚  High intensity        β”‚ \e[ + 9/10 + *m β”‚   β”‚   0   β”‚ Reset           β”‚ \e[0m β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜
                                                 Uppercase = Reset a style: \e[2*m

The single parameter of the function is composed of one or several 1-character flags.
By convention in this order: reset, formats, foreground, background.
Or in regex language: 0?S?[sdiufn-]*[KRGYBMCW]?[krgybmcw]?

Remarks

  • Reset must come before the others… or it will undo immediately the formatting! So $(c 0s) and not $(c s0).
  • @TODO: h for high intensity is not yet implemented.
  • Quite slow 😞

Examples

  • printf "$(c sW)Bold white$(c) and normal"
  • echo -e "Normal text… $(c sRy)BOLD red text on yellow background… $(c w)now on white background… $(c 0u) reset and underline… $(c) and back to normal."
  • cecho Wb "White text on blue background"

References:

License

Licensed under the MIT License.