[Solved] How to randomly generate ANSI colors in Bash/Perl?


In bash, you need to use color escape sequences with echo -e

random_colors.sh

#!/bin/bash

TXT='the quick brown fox jumped over the lazy dog.'
WORDS=( $TXT )
for WORD in "${WORDS[@]}"; do
    let "i=$RANDOM % 256"
    echo -en "\e[38;5;${i}m$WORD \e[0m";
done;
echo

Running this 10 times:

for i in `seq 1 10`; do bash random_colors.sh; done

Output

Bash Color Output

To get a particular palette, you will need to restrict the set of color numbers.

ANSI Escape Codes for Colors

Colors and Formatting

solved How to randomly generate ANSI colors in Bash/Perl?