38

How can I make some output bold in Ubuntu terminal

echo "text bold text"

text bold text

or the same from

cat my_file

3 Answers 3

51

Here's how:

echo -e "text \033[1mbold\033[0m text"

See "Colorizing" Scripts tutorial.

It's not possible to do for cat that way. cat will merely print the characters of the file onto standard out. The closest thing I can come up with is the following:

If you put text \033[1mbold\033[0m text you can do

echo -e `cat test.txt`
3
  • what I wanted in cat is the same. I do 'echo -e "text \033[1mbold\033[0m text" > myfile' and then 'cat myfile' thank you for showing this.
    – varsketiz
    May 26, 2011 at 9:07
  • 2
    More generally, "echo $(tput bold)" and "echo $(tput sgr0)" will work for any terminal, not just ANSI/VTxxx-compatible ones.
    – njd
    May 26, 2011 at 10:28
  • @njd, ah, good point.
    – aioobe
    May 26, 2011 at 10:31
6

Also checkout tput(1). Based on ncurses(3) which is a system abstraction for terminal manipulation (i.e. its purpose is to bring an API which works independent of architecture, OS, terminal, tty, etc.).

Example:

echo "$(tput bold)AHHH$(tput sgr0)"

Hehe, asked 10 years ago; I'm too useful.

0

You can "cat" a file in bold by doing command substitution.

echo -e "text \033[1m`cat my_file`\033[0m text"

"Command substitution reassigns the output of a command or even multiple commands; it literally plugs the command output into another context."

So the magic part here is

`cat my_file`  # or $(cat my_file)

http://tldp.org/LDP/abs/html/commandsub.html

Backticks will be portable to legacy shells, while I prefer to use $().

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .