Search code examples
bashcolorsprintfescaping

Escape only ansi color in printf


I want to print in console windows path with ansi color.

Like this:

file=read -r

printf "\033[32+++Path is \\\\uncshare\\testpath\\$file++\033[0m"

I need to escape any special symbol like \n, because if somebody type nested.txt as a name of the file, for example, it will be interpreted as new line,but i want to preserve ansi color.


Solution

  • printf replaces strings based on format specifiers, then you can do something like

    printf '\033[32+++Path is \\\\uncshare\\testpath\\%s++\033[0m' "$file"
    

    to avoid characters in file being interpreted.