350

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.

4 Answers 4

582

The most compatible way of doing this is using 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

21
  • 3
    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).
    – JamesRat
    May 27, 2010 at 21:00
  • 3
    If you have the need to underline text, you could add a variable. Notice, the backticks are being removed from comment formatting. Use the same format in the answer. UNDERLINE=tput smul
    – jayem
    Aug 13, 2013 at 17:51
  • 8
    tput is a great command with lots of commands for many different tasks. Jan 7, 2014 at 22:28
  • 1
    This tput command is much better than the escape characters. You do not need "-e" with echo and it works with the command read: read -p "this is ${bold}bold${normal} but this isn't"
    – Gael
    Jul 22, 2020 at 3:44
  • 2
    @DamonHill: Ah OK - look closely, it's not $(bold) it's ${bold}, i.e. curly brackets rather than round ones :)
    – psmears
    Apr 11, 2021 at 21:03
201

In order to apply a style on your string, you can use a command like:

echo -e '\033[1mYOUR_STRING\033[0m'

Explanation:

  • echo -e - The -e option means that escaped (backslashed) strings will be interpreted
  • \033 - escaped sequence represents beginning/ending of the style
  • lowercase m - indicates the end of the sequence
  • 1 - Bold attribute (see below for more)
  • [0m - resets all attributes, colors, formatting, etc.

The possible integers are:

  • 0 - Normal Style
  • 1 - Bold
  • 2 - Dim
  • 3 - Italic
  • 4 - Underlined
  • 5 - Blinking
  • 7 - Reverse
  • 8 - Invisible
7
  • 3
    Good explanation. Was looking around for what functionality [0m offers and this explains it well
    – Jeremy
    May 7, 2018 at 15:38
  • Though it should be \033[0m
    – Jeremy
    May 7, 2018 at 15:45
  • 1
    3 - italic, at least in konsole Jun 22, 2018 at 10:22
  • One wishes Strikethrough was supported on Ubuntu and some of the other popular platforms.
    – Dark Star1
    Jul 26, 2018 at 10:22
  • 2
    @WoodrowShigeru man terminfo
    – yong321
    Nov 2, 2021 at 19:18
54

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

6
  • Thanks. I found some other lists of escape sequences, but the one you linked to is very extensive!
    – JamesRat
    May 27, 2010 at 20:46
  • 21
    Don't forget to stop bold formatting at the end of the string: echo -e "\033[1mSome Text\033[0m" else the following lines of your terminal will be in bold too
    – mems
    Oct 8, 2014 at 16:29
  • This solution works even with PHP-CLI, that's an advantage against other solution.
    – David
    Jan 2, 2018 at 9:55
  • 3
    if you have trouble remembering \033 you can use \e like echo -e "\e[1msome text\e[0m" Aug 30, 2018 at 2:41
  • 1
    The octal escape sequence is allowed in JS (JavaScript)
    – user
    Oct 2, 2019 at 16:58
24

In theory like so:

# 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. 

But in practice it may be interpreted as "high intensity" color instead.

(source: http://unstableme.blogspot.com/2008/01/ansi-escape-sequences-for-writing-text.html)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.