How to print bold text using printf?

Viewed 4254

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
1 Answers

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

Related