When the text directly contained color code for example:
a="\033[0;31mRED\033[0m"
echo -e $a
The terminal had no problem colorizing the text in red. But when I modified the color code indirectly:
#!/bin/bash
# define color mappings
declare -A colors=(
['r']='\033[0;31m' # Red (escaped for sed/printf)
['reset']='\033[0m' # Reset color
)
# Sample text
t="abc def [ghi] jkl
nmo [ttt] dfd
and more"
# colorize all [...] in red
echo -e "$t" | sed "s/\(\[[^]]*\]\)/\\${colors[r]}\1\\${colors[reset]}/g"
The result was
abc def \033[0;31m[ghi]\033[0m jkl
nmo \033[0;31m[ttt]\033[0m dfd
and more
The terminal did not colorize [...]s with red color at all but showed the raw color codes. It seems like the problem is related to the timing of variable expansion of some sort.
