Bash echo colors (with function to pipe color to output)


Bash/echo color codes

No color     0
Black        0;30     Dark Gray     1;30
Blue         0;34     Light Blue    1;34
Green        0;32     Light Green   1;32
Cyan         0;36     Light Cyan    1;36
Red          0;31     Light Red     1;31
Purple       0;35     Light Purple  1;35
Brown/Orange 0;33     Yellow        1;33
Light Gray   0;37     White         1;37

Example

cyan='\033[0;36m'
NC='\033[0m'
echo -e "${cyan}Cyan colored text${NC}"

A color function

With this function you can pipe color to your output

cecho.sh

cecho(){
    BLACK="\033[0;30m"
    BLUE="\033[0;34m"
    GREEN="\033[0;32m"
    CYAN="\033[0;36m"
    RED="\033[0;31m"
    PURPLE="\033[0;35m"
    ORANGE="\033[0;33m"
    LGRAY="\033[0;37m"
    DGRAY="\033[1;30m"
    LBLUE="\033[1;34m"
    LGREEN="\033[1;32m"
    LCYAN="\033[1;36m"
    LRED="\033[1;31m"
    LPURPLE="\033[1;35m"
    YELLOW="\033[1;33m"
    WHITE="\033[1;37m"
    NORMAL="\033[m"

    color=\$${1:-NORMAL}

    echo -ne "$(eval echo ${color})"
    cat

    echo -ne "${NORMAL}"
}

Usage

$ source cecho.sh
$ echo "Hej" | cecho ORANGE

Screenshot

comments powered by Disqus