Provided below is a function which colorizes every other argument, starting with the second. (Thus, a non-colorized prefix can be provided by passing a non-empty string in this first position, or a colorized one by leaving it empty, as here).
Notably, no subshells are involved in this code's execution except in the tput invocations used for demonstrative purposes. It is thus, while verbose, very low-overhead to execute.
biwhite=$(tput bold)$(tput setaf 7)
color_off=$(tput sgr0)
# colorize every other argument, starting from the 2nd.
colorize_words() {
local start_color end_color
: "${start_color:=$biwhite}" "${end_color:=$color_off}"
while (( $# )); do
printf '%s' "$1"
shift || break
printf '%s%s%s' "$start_color" "$1" "$end_color"
shift || break
done
printf '\n'
}
colorize_words "" "$USER" " at " "$HOME" " has path " "$PATH"
This can be customized by passing start_color and end_color values to an individual invocation; for example:
# this prints every other argument in red
start_color=$(tput setaf 1) colorize_words "hello " "cruel " world
zsh? ducks$USER,$HOMEand$PATHin the format string for the same reason as you might not want to just put$biwhiteand$color_offin the format string: if they contain percent signs, you have a problem.