hi guys I'm working on a project but i wanted to display the output text in color.
#!/bin/bash
printf "%s" "this is in red";
printf "%s" "this is in default color";
exit 0
this is the example and how would I display "this is in red" in red and leave the rest of the output text on default color?
i looked at some examples but they were all for echo and it did not really worked for printf or do i really have to use echo to make it work?
any help will be great. thanks
Color is achieved by sending ANSI escape sequences to the terminal that instruct the terminal to display the following text in a different color. printf makes it easy to generate the necessary character sequences, because it interprets (among other things) \e to be an ASCII escape character.
Specifically, you tell the terminal to start displaying red characters by print the sequence \e[31m (that's 5 characters: ASCII 27 (escape), [, 3, 1, and m). To tell the terminal to use the default color (whatever that is for the terminal), output \e[0m. Putting it together:
printf '\e[31m%s\e[0m' "this is in red"
printf "%s" "this is in default color"
                        Try below example
#!/bin/bash
printf "\x1b[31m\"this is in red\"\x1b[0m\n";
printf "this is in default color\n";
The output looks like:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With