Posted: . At: 9:08 AM. This was 10 months ago. Post ID: 18229
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


An easy way to use text formatting in the bash shell.


Formatting text in the bash shell is very easy. This is an easy way to format your $PS1 and other text in scripts as well. This is an alternative to the escape sequences that are used to format text in older PS1 examples.

The example below is demonstrating how to use bold and dim faded text. This is using $(tput bold) and $(tput dim).

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~)-(192.168.1.5)echo "Hello $(tput bold)World$(tput sgr0). This $(tput dim)is not$(tput sgr0) bold."
Hello World. This is not bold.

The dim text will not be visible in the example, but try it in Gnome Terminal and it will work.

The $(tput rev) example will allow reversing text colors. The $(tput sitm) example will allow italic text in the Gnome Terminal.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~)-(192.168.1.5)echo "Hello $(tput rev)World$(tput sgr0). This $(tput sitm)is not$(tput sgr0) bold."
Hello World. This is not bold.

Using $(tput blink) and $(tput smul) allows blinking and underlined text respectively.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~)-(192.168.1.5)echo "Hello $(tput blink)World$(tput sgr0). This $(tput smul)is not$(tput sgr0) bold."
Hello World. This is not bold.

Using the $(tput invis) and $(tput smso) examples, this will create invisible and reverse text respectively.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~)-(192.168.1.5)echo "Hello $(tput invis)World$(tput sgr0). This $(tput smso)is not$(tput sgr0) bold."
Hello World. This is not bold.

To print all available colors using tput, use this script.

colors.sh
1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
for i in $(seq 0 7); do
    for j in $(seq 0 7); do
        tput setaf $i; 
        tput setab $j; 
        printf ' F:%s B:%s ' $i $j;
    done
 
    echo "$(tput sgr0)";
done

This script uses two nested loops to iterate through the foreground (setaf) and background (setab) color values (from 0 to 7), setting each combination and printing it out. The $(tput sgr0) at the end of each line resets colors back to default.

Please note: This will only work correctly if your terminal supports these ANSI escape sequences, which most modern terminals should. Also, not all systems interpret these numbers as exactly the same colors so actual output might slightly vary between different environments. Shown below is the output of this script, this is very useful to view all available colors for your terminal.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~/Documents)-(192.168.1.5)bash ./colors.sh 
 F:0 B:0  F:0 B:1  F:0 B:2  F:0 B:3  F:0 B:4  F:0 B:5  F:0 B:6  F:0 B:7 
 F:1 B:0  F:1 B:1  F:1 B:2  F:1 B:3  F:1 B:4  F:1 B:5  F:1 B:6  F:1 B:7 
 F:2 B:0  F:2 B:1  F:2 B:2  F:2 B:3  F:2 B:4  F:2 B:5  F:2 B:6  F:2 B:7 
 F:3 B:0  F:3 B:1  F:3 B:2  F:3 B:3  F:3 B:4  F:3 B:5  F:3 B:6  F:3 B:7 
 F:4 B:0  F:4 B:1  F:4 B:2  F:4 B:3  F:4 B:4  F:4 B:5  F:4 B:6  F:4 B:7 
 F:5 B:0  F:5 B:1  F:5 B:2  F:5 B:3  F:5 B:4  F:5 B:5  F:5 B:6  F:5 B:7 
 F:6 B:0  F:6 B:1  F:6 B:2  F:6 B:3  F:6 B:4  F:6 B:5  F:6 B:6  F:6 B:7 
 F:7 B:0  F:7 B:1  F:7 B:2  F:7 B:3  F:7 B:4  F:7 B:5  F:7 B:6  F:7 B:7 

Very nice script.

To print bold, italic, and underlined text the old way, this will work.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~/Documents)-(192.168.1.5)echo -e '\e[1;3;4mHello world\e[0m'
Hello World

And another example.

╭──(jcartwrightlocalhost)────╮
╰───────────────────────────╾╯(~/Documents)-(192.168.1.5)echo -e '\e[1;3;4mHello\e[0m \e[1;2;3mWorld.\e[0m'
Hello World.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.