Skip to content

Instantly share code, notes, and snippets.

@labbots
Last active May 6, 2020 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save labbots/bffdd156663ea214d35d081571407bdd to your computer and use it in GitHub Desktop.
Save labbots/bffdd156663ea214d35d081571407bdd to your computer and use it in GitHub Desktop.
Test script to understand bash script ANSII colour codes
#!/usr/bin/env bash
txtund=$(tput sgr 0 1) # Underline
txtbld=$(tput bold) # Bold
bldred=${txtbld}$(tput setaf 9) # red
bldblu=${txtbld}$(tput setaf 14) # blue
bldwht=${txtbld}$(tput setaf 10) # white
txtrst=$(tput sgr0) # Reset
info=${bldwht}*${txtrst} # Feedback
pass=${bldblu}*${txtrst}
warn=${bldred}*${txtrst}
ques=${bldblu}?${txtrst}
echo
echo -e "$(tput bold) reg bld und tput-command-colors$(tput sgr0)"
for i in $(seq 1 7); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
done
echo ' Bold $(tput bold)'
echo ' Underline $(tput sgr 0 1)'
echo ' Reset $(tput sgr0)'
echo
colors256() {
local c i j
printf "Colors 0 to 15 for the standard 16 colors\n"
for ((c = 0; c < 16; c++)); do
printf "|%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
done
printf "|\n\n"
printf "Colors 16 to 231 for 256 colors\n"
for ((i = j = 0; c < 232; c++, i++)); do
printf "|"
((i > 5 && (i = 0, ++j))) && printf " |"
((j > 5 && (j = 0, 1))) && printf "\b \n|"
printf "%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
done
printf "|\n\n"
printf "Greyscale 232 to 255 for 256 colors\n"
for ((; c < 256; c++)); do
printf "|%s%3d%s" "$(tput setaf "$c")" "$c" "$(tput sgr0)"
done
printf "|\n"
}
colors256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment