Bash: How to Show Escape Sequence Colors

Submitted by admin on Sun, 09/04/2016 - 14:49
Bash Escape Sequence Color

Create the following script and run:
vi color.sh
chmod +x color.sh
./color.sh

The bash script:
#!/bin/bash

export txtrst='\x1b[00m' # Reset

echo
for i in {0..255} ; do
printf "\x1b[00;${i}m ${i}${txtrst}"
done
echo

echo
for i in {0..255} ; do
printf "\x1b[38;05;%03dm 38;05;%03d " ${i} ${i}
if [ $(( ${i} % 8 )) -eq 7 ] ; then
echo
fi
done
echo

Once you figure out the escape sequence color number, you can use as follows:
export txt1='\[\033[38;05;105m\]' # Purple Dark
export txt2='\[\033[38;05;75m\]' # Cyan Dark
export txt3='\[\033[38;05;98m\]' # Purple Dark
export txt4='\[\033[00;38m\]' # White

export txtrst='\[\033[00m\]' # Reset

# Purple
PS1="${txt1}[${txt2}\$(date +%m/%d) \$(date +%H:%M)${txt1}][${txt3}\u${txt1}@${txt3}\h${txt1}:${txt4}\w${txt1}]${txt4}\$ ${txtrst}"

Tags