Using ANSI-C quoting (bash)
$ string=$'this cat is green\e[12Ddog\e[4Cwhite' $ echo "$string" this dog is white
Explanation: the $'...' syntax is ANSI-C Quoting, which expands the escape sequences in the string (you can also use \033—I just like \e better). This works in cases where the text stored in a variable already has the escape sequences interpreted.
Using shell parameter expansion (bash)
$ string='this cat is green\e[12Ddog\e[4Cwhite' $ echo "${string@E}" this dog is white
Explanation: the ${variable@E} syntax expands the backslash escape sequence in variable.
With older versions of bash
These first two methods work for me in bash 5.2.26, but if you have an older version, you may have to do it with printf:
$ string='this cat is green\033[12Ddog\033[4Cwhite' $ printf "%b\n" "$string" this dog is white
Explanation: "%b" enables backslash interpretation for printf.
man ansi_codes, en.wikipedia.org/wiki/ANSI_escape_code ."\e\[[0-9;]*[A-Za-z]"is what I remember. But, if you're going to resolve 2 parameter cursor positioning commands, and^Hs, it's much harder