Skip to main content

Colored output function to use with your bash scripts. It works by sending specific control codes to your terminal.



#!/usr/bin/env bash

##
# https://github.com/cowboy/dotfiles/blob/master/bin/dotfiles
##

header()  { echo -e "\n\033[1m$@\033[0m"; }
success() { echo -e " \033[1;32m✔\033[0m  $@"; }
error()   { echo -e " \033[1;31m✖\033[0m  $@"; }
arrow()   { echo -e " \033[1;34m➜\033[0m  $@"; }

##
# Add color output to your script
# https://raymii.org/s/snippets/Bash_Bits_Add_Color_Output_To_Your_Scripts.html
##

black()     { echo "$(tput setaf 0)$*$(tput setaf 9)"; }
red()       { echo "$(tput setaf 1)$*$(tput setaf 9)"; }
green()     { echo "$(tput setaf 2)$*$(tput setaf 9)"; }
yellow()    { echo "$(tput setaf 3)$*$(tput setaf 9)"; }
blue()      { echo "$(tput setaf 4)$*$(tput setaf 9)"; }
magenta()   { echo "$(tput setaf 5)$*$(tput setaf 9)"; }
cyan()      { echo "$(tput setaf 6)$*$(tput setaf 9)"; }
white()     { echo "$(tput setaf 7)$*$(tput setaf 9)"; }

#
# Usage

black   "This is black text"
red     "This is red text"
green   "This is green text"
yellow  "This is yellow text"
blue    "This is blue text"
magenta "This is magenta text"
cyan    "This is cyan text"
white   "This is white text"