Linux dis­tri­b­u­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, colors for font and back­ground can be de­ter­mined 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­preter shell is also called bash shell. Bash is the standard shell in Linux dis­tri­b­u­tions and the text window in which you enter your commands. The program echo is an el­e­men­tary part of Ubuntu and pre­in­stalled on every system.

How to output text and in­for­ma­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­spec­tive­ly. 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­si­bil­i­ty 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­for­ma­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­for­ma­tion. With printf and echo, however, only in­for­ma­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­bi­na­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­spec­tive output.

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 (hor­i­zon­tal)
\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 colors for font and back­ground be de­ter­mined with echo?

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

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

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

$ echo -e "\033[0m"

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

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

You can simply use the color 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 colors:

Control character Meaning
\033[30m Font color black
\033[31m Font color red
\033[32m Font color green
\033[33m Font color yellow
\033[34m Font Color blue
\033[35m Font Color magenta
\033[36m Font Color turquoise
\033[37m Font color 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 gray

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

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

Control character Meaning
\033[0m Reset all at­trib­ut­es
\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