Skip to content

Instantly share code, notes, and snippets.

@ankr
Created February 7, 2015 12:16
Show Gist options
  • Save ankr/2e863f6f038eb414eb5f to your computer and use it in GitHub Desktop.
Save ankr/2e863f6f038eb414eb5f to your computer and use it in GitHub Desktop.
Colorized output in shell and bash scripts.

Colored output in bash script

Implementation varies between platforms, see below.

Usage

cecho "Hello world" yellow

White terminal?

This script assumes your terminal has a black background, to use with white terminals replace ;40m with ;47m for each color. (There are more colors, try them out!)

OSX

cecho() {
    black='\033[30;40m'
    red='\033[31;40m'
    green='\033[32;40m'
    yellow='\033[33;40m'
    blue='\033[34;40m'
    magenta='\033[35;40m'
    cyan='\033[36;40m'
    white='\033[37;40m'

    echo -e ${!2}$1
    tput sgr0
    return
}

Linux

cecho() {
    black='\E[30;40m'
    red='\E[31;40m'
    green='\E[32;40m'
    yellow='\E[33;40m'
    blue='\E[34;40m'
    magenta='\E[35;40m'
    cyan='\E[36;40m'
    white='\E[37;40m'

    echo -e ${!2}$1
    tput sgr0
    return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment