Bash scripting is quite popular is the easiest scripting language. Like any programming or scripting language, you come across printing text on the terminal. This can happen in numerous scenarios such as when you want to output the contents of a file or check the value of a variable. Programmers also debug their applications by printing the values of their variables on the console. Therefore, before we delve into bash scripting which will be another tutorial, let’s look at the different ways in which we could output text in the terminal.

Echo is the most important command that you need to know in order to output text on the terminal. As the name itself suggests, echo displays number or string on standard output in the terminal. It also has a number of options available as shown in the table below.

OptionsDefinition
-nDo not print the trailing newline
-EDisable interpretation of back-slash escaped characters
-eEnable interpretation of backslash escapes
\aAlert
\bBackspace
\cSuppress trailing newline
\eEscape
\fForm feed
\\backslash
\nNew line
\rCarriage return
\tHorizontal tab
\vVertical tab

According to the Linux documentation, the following is the syntax for echo command.

echo [option(s)][string(s)]

Now, we shall see the different ways in which we can output the text on the terminal.

Send Text to Standard Output

To output any string or number or text on the terminal, type the following command and press enter.

echo "Hello World"

The following output will be shown on the terminal

Send text to stdout with echo command

Print a Variable

Let’s declare a variable and prints its value on the terminal. Suppose x is a variable which we have initialized at 100.

x=100

Now, we will output the value of the variable on the terminal.

echo x

100 will be printed on the terminal. Likewise, you can also store a string in a variable and output it on the terminal.

Print content of a variable

Try it out and let us know if it was easy for you.

Remove Space between Words

This is one of my favorite option of echo as it removes all the space between different words in the sentences and jumbles them up together. In this feature, we will be using two of the options as mentioned in Table 1.

echo -e "Hello \bmy \bname \bis \bjohn \bDoe"

As you can see from the above example, we are enabling the interpretation of backslash escapes as well as adding backspace. The following output was shown.

Remove space between words with backspace

Output Word in New Line

This option of echo comes in really handy when you are working bash scripting. Mostly you need to move to the next line once you are done. Therefore, this is the best option to use for that.

echo -e "Hello \nmy \nname \nis \nJohn \nDoe"

The output will display each word in a separate line as shown in the screenshot below.

Add newlines to the text output

Output Text with Sound

This is a simple option of outputting text with bell or alert. To do this, type the following command.

echo -e "Hello \amy name is John Doe"

Make sure that your system’s volume is high enough for you to hear the tiny bell that sounds when the text is outputted on the terminal.

Remove Trailing New Line

Another option of the echo is to remove the trailing newline so that everything outputs on the same line. For this, we use “\c” option as shown in the figure below.

echo -e "Hello my name \cis John Doe"

The following output is shown

Remove trailing newline

Add a Carriage Return to the output

To add a specific carriage return in your output, we have “\r” option for this.

echo -e "Hello my name \ris John Doe"

The following output is shown to you on the terminal.

Add a Carriage Return to the output

Use Tabs in Output

While printing output on the terminal, you can add horizontal and vertical tabs as well. These come in handy for cleaner outputs. To add horizontal tabs, you have to add “\t” and for vertical tabs, add “\v”. We will be doing a sample for each of these and then a combined one.

echo -e "Hello my name \tis John Doe"

The output for this command will be shown as follows

Use tabs to format the text output

echo -e "Hello my name \vis John Doe"

The output for this command will be shown as follows

Use \v in bash output

Now we will combine this example for a set of sentences we have.

echo -e "Hello my name \vis John Doe. Hello! My name is \tJane Doe"

The following will be printed on the terminal.

Advanced formatting example

Well, that’s all the options that can be used for printing text on a terminal. This is an important feature to learn as it will help you further when you start working on bash scripting. Make sure that you implement each of the options and practice hard. Let us know if this tutorial helped you solve a problem.

Bash Scripting: How to Output and Format Text on Linux Shell