Linux dis­tri­bu­tions allow you to set up and control the system yourself. In this context, the bash echo command belongs to one of the most used commands. It is used in bash scripts and batch files to output a status text in a file or on your screen. We explain how the Linux echo command works and how, for example, colours for font and back­ground can be de­term­ined with it.

What is an echo command and how does it work?

The Linux echo command repeats what you have told it to repeat. The function is extremely simple, but required to do just that. For example, without echo, you would not get visible output from shell scripts. Shell is the user interface where you can enter different commands, such as the Linux tail command, the Linux head command, the Linux cat command or else the Linux echo command.

How exactly does the Linux echo command work and what is its syntax? Here is an example of the general syntax of the command:

echo [option] [string]

The basic operation of echo is the same in all pro­gram­ming languages. You make an input – which in most cases is in the form of a string – and this is received and output again unchanged.

Note

The user interface or command line in­ter­pret­er shell is also called bash shell. Bash is the standard shell in Linux dis­tri­bu­tions and the text window in which you enter your commands. The program echo is an ele­ment­ary part of Ubuntu and pre­in­stalled on every system.

How to output text and in­form­a­tion in Linux with echo commands

In our first example, we want to output a text and the contents of variables in bash and terminal, re­spect­ively. To write the text ‘This is an example’, enter the following code into the terminal:

$ echo This is an example 
This is an example

To avoid errors as much as possible, we recommend that you enclose the text in quotation marks:

$ echo “This is an example”
This is an example

In the next step we want to output a variable. Here it is enough to enter the name of the variable, such as PWD (for Print Working Directory or your working directory). The command would then look like this:

$ echo $PWD 
/Users/Name/Desktop

Then there is also the pos­sib­il­ity to combine the output of variables with text, as in this example:

$ echo “You are in the directory: $PWD” 
You are in the directory: /Users/Name/Desktop
Note

For the output of in­form­a­tion there is not only the bash echo command, but also printf and tput. Tput is the more complex tool and can also be used to reset in­form­a­tion. With printf and echo, however, only in­form­a­tion can be output.

What are the control char­ac­ters for echo?

Once you have un­der­stood the basic principle of the Linux echo command, the next step is to learn which control char­ac­ters you can use in com­bin­a­tion with the command. Control char­ac­ters are char­ac­ters that are not directly visible on the screen, but determine things like the beginning of text, the end of text, or line breaks.

Attention! Echo un­der­stands control char­ac­ters only if you set the -e option. In the following example, ‘\n’ is therefore mapped as text only. (The control character ‘\n’ signals newline or a text wrap).

$ echo “\n” 
\n

However, if you add -e, as in the following example, a text break happens in your text:

$ echo -e “\n”
Note

The echo command ends with a line break by default. To suppress this, you can set the control character \c at the end of the re­spect­ive output.

Here is an overview of which control char­ac­ters are available to you in bash:

Escape codes Meaning
\a Alarm sound
\b One character back
\c Suppress text wrapping
\f Back
\n Line break
\r Back to beginning of line
\t Tabulator (ho­ri­zont­al)
\v Tabulator (vertical)
\\ Backslash character output
\0nnn ASCII char­ac­ters in octal form (sh and ksk only)
\nnn ACSII char­ac­ters in octal form (bash only)

How can colours for font and back­ground be de­term­ined with echo?

The Linux echo command can also be used to specify text at­trib­utes such as colours for the font and back­ground when out­put­ting text. This works by enclosing all char­ac­ters in quotes, or by writing the colours in variables right away to make the string more readable. The first variant looks as follows (a colour is always in­tro­duced with \033[, 31m here stands for the font colour red):

$ echo -e "\033[31mThis is a red text"

Now to reset all at­trib­utes, enter this string:

$ echo -e "\033[0m"

However, it becomes more readable if you define colours as variables in your bash script be­fore­hand:

red='\033[31m'
reset='\033[0m'

You can simply use the colour as an echo command name:

$ echo -e "${red}This is a red text.${reset}And now the text is normal again."

Below is an overview of the different escape codes for the different font and back­ground colours:

Control character Meaning
\033[30m Font colour black
\033[31m Font colour red
\033[32m Font colour green
\033[33m Font colour yellow
\033[34m Font Colour blue
\033[35m Font Colour magenta
\033[36m Font Colour turquoise
\033[37m Font colour white
\033[40m Back­ground black
\033[41m Back­ground red
\033[42m Back­ground green
\033[43m Back­ground yellow
\033[44m Back­ground blue
\033[45m Back­ground magenta
\033[46m Back­ground turquoise
\033[47m Back­ground greay

How do you set certain text prop­er­ties using echo?

In addition to the colour of the font and back­ground, the Linux echo command can also be used to specify text at­trib­utes such as boldface or underline. Here are the codes for various text prop­er­ties:

Control character Meaning
\033[0m Reset all at­trib­utes
\033[1m Bold font
\033[4m Underline
\033[5m Flashing
\033[7m inverse display

If you now want to write the red text in bold, the code is as follows:

$ echo -e "\033[1;31mThis is a red text in bold."
Tip

We have also listed an overview of all the important Linux commands for you.

Go to Main Menu