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.
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
Note that normal will turn off all formatting (so if you have other formatting - such as colours - that will be disabled, too).
tput smultput is a great command with lots of commands for many different tasks.read -p "this is ${bold}bold${normal} but this isn't"$(bold) it's ${bold}, i.e. curly brackets rather than round ones :)In order to apply a style on your string, you can use a command like:
echo -e '\033[1mYOUR_STRING\033[0m'
Explanation:
-e option means that escaped (backslashed) strings will be interpretedThe possible integers are:
All this is per ANSI but is implemented by absolute majority of terminal emulators out there. An alternative way to style output is to rely on terminfo through use of the tput command. The latter will most likely output escape sequences which are ANSI anyway, unless you're at a really obscure and/or exotic type of terminal.
[0m offers and this explains it well\033[0m3 - italic, at least in konsoleI 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
echo -e "\033[1mSome Text\033[0m" else the following lines of your terminal will be in bold too\033 you can use \e like echo -e "\e[1msome text\e[0m"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)