Search code examples
bashshellcolorsescaping

How do I escape this properly in bash?


C_CYAN='\033[36m'
echo -e "\\${C_CYAN}hello";

Expected:

\hello in with the word hello in cyan

Actual:

\033[36mhello


Solution

  • You can use printf:

    printf '\%bhello\n' "$C_CYAN"
    

    but as @chepner says in his comment it's better to use tput than hardcoding escape sequences:

    C_CYAN=$(tput setf 3)