Tuesday, January 22, 2008

ANSI Escape Sequences for bold and underline text - BASH


ANSI Escape Sequences for writing text in "BOLD" and "UNDERLINE" . A more about ANSI escape sequences can be found here

//BOLD

$ echo -e "\033[1mThis is a BOLD line\033[0m"
This is a BOLD line

#Using tput
tput bold
echo "This" #BOLD
tput sgr0 #Reset text attributes to normal without clear.
echo "This" #NORMAL

//UNDERLINE
$ echo -e "\033[4mThis is a underlined line.\033[0m"
This is a underlined line.

5 comments:

Srimay said...

Wow !!!! I never knew you can type in bold/underline in command prompt. This kinda looks like real amusing !!!

Robert said...

Here's a tricky one: I use /bin/ls -lhG to list files which provides color coding, but if I process the listing with sed (to highlight hard linked files), all the other color information gets stripped. Know a way around that?

-Rob
(using Mac OS X 10.6)

Honza said...

rharder:

echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'

Honza said...

rharder: Eh, I mean:

ls --color | sed ...

Works at least in Ubuntu's bash.

Robert said...

@Honza Thanks. I don't see where that would permit escape sequences to pass. I suppose I could make a convoluted regular expression to match the escape sequences and then try to reprint them with \1 or something. Sounds ugly. =(

© Jadu Saikia www.UNIXCL.com