[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Construct ANSI colour codes
From: |
Greg Wooledge |
Subject: |
Re: Construct ANSI colour codes |
Date: |
Wed, 7 Apr 2021 17:03:16 -0400 |
Why are you making this so hard?
declare -A color=(
[black]="$(tput setaf 0)"
[red]="$(tput setaf 1)"
[green]="$(tput setaf 2)"
[yellow]="$(tput setaf 3)"
[blue]="$(tput setaf 4)"
[magenta]="$(tput setaf 5)"
[cyan]="$(tput setaf 6)"
[white]="$(tput setaf 7)"
)
sgr0="$(tput sgr0)"
echo "${color[green]}hello${sgr0} ${color[red]}world${sgr0}"
printf 'Price: %s$%.2f%s\n' "${color[blue]}" "$price" "$sgr0"
Or you can use scalar variables like "red" "blue" and so on, instead of
the associative array.
I gave two different examples for output. You can use either, or both,
or come up with your own.
If you insist on initializing the color variables inside a function,
and if you go with the array, then you might want to add the -g option
to make it global.
Re: Construct ANSI colour codes, Greg Wooledge, 2021/04/07
Re: Construct ANSI colour codes, Alex fxmbsw7 Ratchev, 2021/04/07