Skip to content

Instantly share code, notes, and snippets.

@suewonjp
Last active January 6, 2017 11:29
Show Gist options
  • Save suewonjp/de4aa5cf0a358cf2907a34f977f7d1b4 to your computer and use it in GitHub Desktop.
Save suewonjp/de4aa5cf0a358cf2907a34f977f7d1b4 to your computer and use it in GitHub Desktop.
Bash - Simple echo function to print colorful text
### Font colors
red=1 green=2 yellow=3 blue=4 magenta=5 cyan=6
### Font attributes
bold=1 underline=4 reverse=7
preecholor() {
printf '\e[%s;3%dm' "$1" "$2"
}
postecholor() {
printf '%s\e[0m' "$*"
}
### Colorful echo; echo + color
### Usage: echolor [attr] [color] [arbitrary messages...]
### e.g. : echolor $underline $magenta Current dir is $PWD
echolor() {
preecholor "$1" "$2"
shift 2
postecholor "$*"
}
### More complicated example:
### $inputPath and $hostScript are omitted
if [ ! -f "${inputPath}" ]; then
printf "[ %s ][ %s ] The database file '%s' doesn't exist!\n[ %s ] For help, run %s\n" \
"${hostScript}" "$(echolor $bold $magenta ERROR)" \
"${inputPath}" \
"${hostScript}" "$(echolor $reverse $cyan "${hostScript}" -?)" && \
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment