tags:

views:

218

answers:

4

I'm writing a bash script that prints some text to the screen:

echo "Some Text"

Can I format the text? I would like to make it bold.

Thank you.

A: 

In theory like so: http://unstableme.blogspot.com/2008/01/ansi-escape-sequences-for-writing-text.html But in practice it may be interpreted as "high intensity" color instead.

roufamatic
A: 

echo -e "\033[1mThis should be bold text.\033[0m"

Nifle
+1  A: 

I assume bash is running on a vt100-compatible terminal in which the user did not explicitly turn off the support for formatting.

First, turn on support for special characters in echo, using -e option. Later, use ansi escape sequence ESC[1m, like:

echo -e "\033[1mSome Text"

More on ansi escape sequences for example here: ascii-table.com/ansi-escape-sequences-vt-100.php

Michał Trybus
Thanks. I found some other lists of escape sequences, but the one you linked to is very extensive!
ratberryjam
+9  A: 

The most compatible way of doing this is usingi tput to discover the right sequences to send to the terminal:

bold=`tput bold`
normal=`tput sgr0`

then you can use the variables $bold and $normal to format things:

echo "this is ${bold}bold${normal} but this isn't"

gives

this is bold but this isn't

psmears
That's pretty nifty. If I understand correctly, This is the same as inserting the escapes, but it would work in other terminals (not VT100).
ratberryjam
That's correct - it looks up the appropriate codes according to the value of TERM.
psmears
I love `tput`. One of my favorite little-known commands. +1
Norman Ramsey