15 Practical Examples of Echo Command in Linux

Published on January 19, 2024

15 Practical Examples of ‘Echo’ Command in Linux

The Linux built-in echo command is widely used when working with shell scripts. You can comfortably use it to display strings on standard output, and there are numerous ways of using it for your scripting and other tasks. This post presents 15 practical examples of working with Linux’s “echo” command. Take a look!

Display Text

The commonly used and obvious way of working with the echo command is to display text to the standard output. You simply type the echo command followed by the text you want to display. You can optionally enclose the text or string, and once you press the enter key, the typed string will be displayed to the standard output, like in the example below.

Remove Spacing

For other options that you wish to use with the echo command, you must add the -e option, which helps with the interpretation of escaped characters that start with a backslash.

For instance, if you want to display a string while eliminating the word spacing, add the \b option between the target words. See the below example.

New line

Sometimes, you may run into a case where you want to add a new line after every word in your string. For that, we will use the \n character before every word you want to display in a new line. Note that you must include the -e option and the backslash character.

Add Tab Space

If you prefer using a tab space instead of a new line, you can use the \t for tab. Add it before every word in your string that you wish to display with an extra tab between them. The example below shows how we managed to get a horizontal tab between the words.

Vertical Tab Space

It’s also possible to get a vertical tab between the words in the string you wish to display. For a vertical tab, we will use the \v character before the target words. Watch the example below and note where we’ve added the \v and how the output appears once we display it to the terminal.

Feel free to play with the \v to create your desired string output.

Omit Trailing New Line

By default, running the echo command adds a trailing new line to separate the output. However, if you wish to omit this trailing new line, add the -n option with the echo command.

Your output will be displayed in the same line as in the example below.

Output File

With the echo command, instead of displaying the results to the standard output, you can choose to save them in a file. Simply add the redirect operator and the name of the output file you want to create.

Once the file is created, open it to display its contents. We’ve used the cat command to verify that the file contains the echoed string we added.

List Files and Folders

Similar to how we use the ls command to list files and folders in a directory, we can also use the echo command.

An ls command would give the following output.

Running the echo command with an asterisk also gives the same output.

List Specific Type

Suppose you want to list specific file types. You can specify the extension when running the echo command. Let’s quickly create text files using the touch command.

To only list text files in the current directory, we would run our echo command, as shown in the example below.

Carriage Return

If you want to return only a specified carriage in the displayed output, run the echo command and add the \r option. Any words before the \r will get omitted in the output.

Working with Variables

You can declare and initialize a variable and then use the echo command to display its value to the standard output. We’ve declared and initialized variables x and y below.

We’ve then echoed their value using the $ symbol to reference them.

Access Help Page

Like all commands, you can access the help page for the echo command to get more information about it and the different options it offers. Run the command below.

Copy

Display Value of a Shell Variable

When you have a shell variable, you can echo its value, similar to the user-defined variables we saw earlier. For instance, an environmental variable such as SHELL can be accessed as shown below.

Truncate

When displaying a string to the output, you can add the \c to specify where you wish to truncate the string. Values after the \c won’t get printed to the output.

Combined Options

The last example for this guide is to show you how you can combine different options for a better result. For instance, to add a new line while creating a tab space, use the \n\t options as in the image below.

Conclusion

The echo command has numerous functionalities, especially when working with scripts and bash files. This post demonstrates how you can use the echo command with different options to get different results. Practice with the presented examples to help you quickly understand how to use the echo command.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price