Search code examples
bashprintf

How to print bold text using printf?


I want to print bold text using printf. How do I do it?

printf '%s\n' "\033[1m"bold_text"\033[0m"

doesn't work. It displays:

\033[1mbold_text\033[0m

However, the same string works fine with echo -e:

echo -e "\033[1m"bold_text"\033[0m"
bold_text

echo_command


Solution

  • The right answer is to use the %b format specifier instead of %s:

    printf '%b\n' "\033[1m"bold_text"\033[0m"
    

    From help printf:

    %b - expand backslash escape sequences in the corresponding argument