How to use Linux echo command

Linux distributions 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 background can be determined 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 programming 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 interpreter shell is also called bash shell. Bash is the standard shell in Linux distributions and the text window in which you enter your commands. The program echo is an elementary part of Ubuntu and preinstalled on every system.

How to output text and information in Linux with echo commands

In our first example, we want to output a text and the contents of variables in bash and terminal, respectively. 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 possibility 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 information 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 information. With printf and echo, however, only information can be output.

What are the control characters for echo?

Once you have understood the basic principle of the Linux echo command, the next step is to learn which control characters you can use in combination with the command. Control characters are characters 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 understands control characters 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 respective output.

Here is an overview of which control characters 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 (horizontal)
\v Tabulator (vertical)
\\ Backslash character output
\0nnn ASCII characters in octal form (sh and ksk only)
\nnn ACSII characters in octal form (bash only)

How can colours for font and background be determined with echo?

The Linux echo command can also be used to specify text attributes such as colours for the font and background when outputting text. This works by enclosing all characters 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 introduced with \033[, 31m here stands for the font colour red):

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

Now to reset all attributes, enter this string:

$ echo -e "\033[0m"

However, it becomes more readable if you define colours as variables in your bash script beforehand:

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 background 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 Background black
\033[41m Background red
\033[42m Background green
\033[43m Background yellow
\033[44m Background blue
\033[45m Background magenta
\033[46m Background turquoise
\033[47m Background greay

How do you set certain text properties using echo?

In addition to the colour of the font and background, the Linux echo command can also be used to specify text attributes such as boldface or underline. Here are the codes for various text properties:

Control character Meaning
\033[0m Reset all attributes
\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.

Was this article helpful?
Page top