Bash scripts are very popular and one of the simplest scripting languages. As with any programming or scripting language, you will sometimes output text to the terminal. This can happen in many scenarios, for example, when you want to print the contents of a file or check the value of a variable. Programmers also debug their applications by printing the values of their variables to the console. Before we get into Bash scripting, which will be another tutorial, let’s look at the various ways we can output text in the terminal.

Echo is the most important command you need to know to output text to the terminal. As the name implies, Echo prints numbers or strings to the standard output of the terminal. It also has a number of options, which are listed in the following table.

Options Definition
-n Do not print the trailing newline
-E Disable interpretation of back-slash escaped characters
-e Enable interpretation of backslash escapes
a Alert
b Backspace
c Suppress trailing newline
e Escape
f Form feed
\ backslash
n New line
r Carriage return
t Horizontal tab
v Vertical 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

<img alt="Send text to stdout with echo command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-72-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" src="data:image/svg xml,” width=”750″>

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.

<img alt="Print content of a variable" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-73-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

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

Remove Space between Words

This is one of my favorite options of the echo command 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.

<img alt="Remove space between words with backspace" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-74-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

Output Word in New Line

This option of echo comes in really handy when you are working with 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.

<img alt="Add newlines to the text output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-75-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

Output Text with Sound

This is a simple option of outputting text with a 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

<img alt="Remove trailing newline" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-76-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="Add a Carriage Return to the output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-77-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

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

<img alt="Use tabs to format the text output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-78-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

echo -e "Hello my name vis John Doe"

The output for this command will be shown as follows

<img alt="Use v in bash output" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-79-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

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.

<img alt="Advanced formatting example" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-80-1024×583.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="427" loading="lazy" src="data:image/svg xml,” width=”750″>

These are all options that can be used to print text in a terminal. This is an important feature to learn because it will help you when you start working on bash scripts. Make sure you use each of the options and practice diligently. Let us know if this tutorial helped you solve a problem.