Use the Echo Command with Examples in Linux

Echo is a built-in Linux command-line utility that is used on standard output to display an argument i.e text or string. It is most generally used to display status text to the screen in batch files and shell scripts or used as a source part of a pipeline.

You have seen or used it frequently in Linux or Unix-like Operating systems mainly to print the string or path variables. Here this article will help you know a more possible way to use the echo command along with examples. To demonstrate examples in this article I have used the Ubuntu 22.04 LTS system.

The syntax of the echo command is as follows:

$ echo [Option] [Argument]

Displaying String on Standard Output

You can print text or any string using the echo command in the following way.

$ echo "This is an echo command example"

Display text using echo command

Also, you can print along with quotes by escaping it using the backslash (\) character.

$ echo "This is \"Linux\" echo example"

Quote and escape text correctly on Linux shell

Displaying Variables

The echo command can be used to display system variables such PATH, USERS, SHELL etc. You can print the variable data using ‘$’ along with the variable name.

$ echo $SHELL

Show content of a Linux bash variable

Append Text to the File

You can append the text to the file using ‘>’ and ‘>>’ operators. If you want to append text to the new file you can do so using the echo command. In the command ‘>’ operator overwrites the whole text in file whereas ‘>>’ operators append the text to the file.

$ echo "The way to get started is to quit talking and begin doing." >> quotes.txt

Appending text to a file on Linux bash

$ echo "Your time is limited, so don't waste it living someone else's life." > quotes.txt

Show file content with cat command

As you can see in the above example using ‘>’ operator overwrites the previously appended text.

Using ‘\t’ Horizontal & ‘\v’ Vertical Tab Backslash-escaped Character.

In the echo command, backslash-escaped characters are interpreted using the -e option to the command. Using ‘\t’ escaped character will display the text with tab space as shown in the example below.

$ echo -e ​​"This is tab space \tin command output"

Tab Backslash-escaped Character

Also, for vertical tab space execute the following command.

$ echo -e ​​"This is horizontal \vtab space \tin command output"

Vertical tab on bash

Printing Text to New Line Using ‘\n’ escaped character.

You can use the ‘\n’ escaped character to break and print the text in the next line. As you can see in the following example text is broken and print to next line from escaped character use.

$ echo -e "This is break point, \nfrom here line is break"

using newline character on Linux bash

Listing Files Using Pattern

The echo command supports wildcard characters to filter out the files using the pattern in the current working directory. In the following example I have used a pattern to filter out the .txt file.

$ echo The text files are: *.txt

Use wildcards in echo command

Using echo with Other Command

We can use the echo command to display other command output on the screen. In the following example I have printed date along with string using $(command) expression.

$ echo "Today date is: $(date)"

Include command output in echo

Using ‘\a’ alert escaped Character

In the echo command using alert (‘/a’) an escaped character will alert with sound when you use this character.

$ echo "First alert \a"

Using alert meta character on Linux shell

Conclusion

You could echo commands in many ways either for manipulating the command output or prompt navigation in the shell script. There are other uses of the echo command but in this article, I have included some of the echo usages which I find useful.

Leave a Comment