Linux logo

The echo Command in Linux

The echo command in Linux is a versatile tool for displaying text, manipulating strings, and handling output within the command line.

This tutorial provides a comprehensive guide on the various applications of the echo command, covering essential syntax, text display, file manipulation, newline control, variable usage, and special characters.

Whether you’re a beginner or an experienced user, mastering these echo command techniques will enhance your efficiency in scripting and navigating the Linux terminal.

Explore the best practices outlined here to unleash the full potential of the echo command in your Linux endeavors.

How to use echo command in Linux

The general syntax for using the echo command in Linux:

$ echo [OPTION]… [STRING]…

For displaying the string by echo:

$ echo [string]

Let us have a look at a few example commands to understand how it works.

An example of displaying string using echo

For displaying text or string, you simply need to provide the string as follows:

$ echo just a test message

That’s it.

Adding/Appending text in a file example

If you want to use the echo command to append some text to a file then this is just like above plus giving the file name where you want to add text. For example:

$ echo just a test message >> file_name

This should add the “just a test message” to the file_name file.

Remove newline by –n option example

By default, the echo command output ends with a newline character. However, if you would like, you can change this behavior using the -n command line option.

Further Reading:  Why We Use Top Command in Linux?

For example:

$ echo -n "How to disable newline"
how to create a new line example

By using \n, you may add a new line from where it’s used.

$ echo -e "Hello \nand \nWelcome"
An example of tab spaces using \t

By using \t option, you may add horizontal tab spaces. For example:

$ echo -e "Hello \tand \tWelcome"
Declare a variable and display its value

Declare a variable and display its value. The example below shows hows:

$ a=5

$ echo variable a = $a


variable a = 5
Give a sound alert by \a

By using the option ‘\a’ — alert return with backspace interpreter ‘-e’ to produce sound.

$ echo -e "Hello and  \aWelcome Guys"
How echo handles backslash escapes?

When used in default mode, the echo command does not treat backslash escapes as special characters and hence doesn’t translate them in any special manner. For example, the following command will not add backspace but display as it is:

$ echo “Hello /band welcome”

 

In order to make it work, you may use the –e option i.e.:

$ echo -e “Hello /band welcome”
The \v option example

The \v option is used to make vertical tab spaces. An example command:

$ echo -e "Hello \vand \vWelcome
Echo * example

Echo * command will print all files/folders, like ls command.

$ echo *
Print specific files

Print files of a particular type. For example, if you want to print all ‘.pdf’ files, use the following command.

$ echo *.pdf

Best Practices for Using the Echo Command in Linux

1. Proper Syntax Usage:Follow the correct syntax for the echo command:

$ echo [OPTION]… [STRING]…

For displaying a string:

$ echo [string]

2. Displaying Text:

Use the echo command to display text or strings easily:

$ echo just a test message

3. Appending Text to a File:

Append text to a file using the echo command along with the file name:

$ echo just a test message >> file_name

4. Remove Newline with -n Option:

Use the -n option to remove the newline character at the end of the output:

$ echo -n “How to disable newline”

5. Creating New Lines:

Further Reading:  How to Check Disk Space in Linux OS

Utilize \n to create new lines:

$ echo -e “Hello \nand \nWelcome”

6. Tab Spaces with \t:

Add horizontal tab spaces using the \t option:

$ echo -e “Hello \tand \tWelcome”

7. Displaying Variables:

Declare a variable and display its value using the echo command:

$ a=5
$ echo variable a = $a

8. Sound Alert with \a:

Generate a sound alert using the \a option:

$ echo -e “Hello and \aWelcome Guys”

9. Handling Backslash Escapes:

Be aware that, in default mode, echo does not treat backslash escapes as special characters. Use the -e option to interpret them:

$ echo -e “Hello /band welcome”

10. Creating Vertical Tab Spaces with \v:
– Make use of the \v option to create vertical tab spaces:

$ echo -e “Hello \vand \vWelcome”

11. Listing Files and Folders:
– Echo * will print all files and folders similar to the ls command:

$ echo *

12. Printing Specific Files:
– Print specific files of a particular type, e.g., all ‘.pdf’ files:

$ echo *.pdf

FAQs on Echo Command

What is the echo command used for in Linux?
The echo command is primarily used to display lines of text or strings in the Linux command line. It is commonly employed in scripts, batch files, and various commands to insert text where needed.
Is the echo command specific to Linux?No, the echo command is found in many operating systems and is not specific to Linux. It is a fundamental command available in various commands like bash, ksh, and csh.
How do I display a simple message using the echo command?To display a simple message, use the following syntax:
$ echo just a test message

Can I append text to a file using the echo command?

Yes, you can append text to a file with the echo command. For example:

$ echo just a test message >> file_name

How can I remove the newline character from the echo command output?

To remove the newline character, use the -n option. For instance:

$ echo -n “How to disable newline”

How do I create a new line with the echo command?

Further Reading:  Linux Find: How to Search Files with Example Commands

Use the \n escape sequence to create a new line. Example:

$ echo -e “Hello \nand \nWelcome”

What is the purpose of the \t option in the echo command?

The \t option adds horizontal tab spaces. Example:

$ echo -e “Hello \tand \tWelcome”

How can I declare a variable and display its value with the echo command?

Declare a variable and display its value using the following:

$ a=5
$ echo variable a = $a

Can the echo command produce a sound alert?

Yes, the \a option produces a sound alert. Example:

$ echo -e “Hello and \aWelcome Guys”

How does the echo command handle backslash escapes?

In default mode, the echo command does not treat backslash escapes as special characters. To enable, use the -e option:

$ echo -e “Hello /and welcome”

What does the \v option do in the echo command?

The \v option creates vertical tab spaces. Example:

$ echo -e “Hello \vand \vWelcome”

How does the echo command handle wildcard characters like *?

The echo command can be used with * to print all files/folders similar to the ls command:

$ echo *

How can I print specific files using the echo command?

To print files of a particular type, such as all .pdf files, use:

$ echo *.pdf